-
Notifications
You must be signed in to change notification settings - Fork 11
/
equality-Nat-crib.rkt
304 lines (247 loc) · 6.59 KB
/
equality-Nat-crib.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#lang pie
;; Exercises on Nat equality from Chapter 8 and 9 of The Little Typer
(claim +
(-> Nat Nat
Nat))
(define +
(λ (a b)
(rec-Nat a
b
(λ (_ b+a-k)
(add1 b+a-k)))))
;; Exercise 8.1
;; Define a function called zero+n=n that states and proves that
;; 0+n = n for all Nat n.
(claim zero+n=n
(Π ([n Nat])
(= Nat (+ zero n) n)))
(define zero+n=n
(λ (n)
(same n)))
;; Exercise 8.2
;;
;; Define a function called a=b->a+n=b+n that states and proves that
;; a = b implies a+n = b+n for all Nats a, b, n.
(claim a=b->a+n=b+n
(Π ([a Nat]
[b Nat]
[n Nat])
(-> (= Nat a b)
(= Nat (+ a n) (+ b n)))))
(define a=b->a+n=b+n
(λ (a b n)
(λ (a=b)
(replace a=b
(λ (k)
(= Nat (+ a n) (+ k n)))
(same (+ a n))))))
;; Exercise 8.3
;;
;; Define a function called plusAssociative that states and proves that
;; + is associative.
(claim plusAssociative
(Π ([n Nat]
[m Nat]
[k Nat])
(= Nat (+ k (+ n m)) (+ (+ k n) m))))
(claim mot-plusAssociative
(-> Nat Nat Nat
U))
(define mot-plusAssociative
(λ (m p n)
(= Nat (+ n (+ m p)) (+ (+ n m) p))))
(claim base-plusAssociative
(Π ([m Nat]
[p Nat])
(mot-plusAssociative m p zero)))
(define base-plusAssociative
(λ (m p)
(same (+ m p))))
(claim step-plusAssociative
(Π ([m Nat]
[p Nat]
[n Nat])
(-> (mot-plusAssociative m p n)
(mot-plusAssociative m p (add1 n)))))
(define step-plusAssociative
(λ (m p n)
(λ (plusAssociative-n)
(cong plusAssociative-n (+ 1)))))
(define plusAssociative
(λ (m p n)
(ind-Nat n
(mot-plusAssociative m p)
(base-plusAssociative m p)
(step-plusAssociative m p))))
;; Exercise 9.1
;;
;; Define a function called same-cons that states and proves that
;; if you cons the same value to the front of two equal Lists then
;; the resulting Lists are also equal.
(claim same-cons
(Π ([E U]
[l1 (List E)]
[l2 (List E)]
[e E])
(-> (= (List E) l1 l2)
(= (List E) (:: e l1) (:: e l2)))))
(define same-cons
(λ (E l1 l2 e)
(λ (prf)
(replace prf
(λ (l)
(= (List E) (:: e l1) (:: e l)))
(same (:: e l1))))))
;; Exercise 9.2
;;
;; Define a function called same-lists that states and proves that
;; if two values, e1 and e2, are equal and two lists, l1 and l2 are
;; equal then the two lists, (:: e1 l1) and (:: e2 l2) are also equal.
(claim same-lists
(Π ([E U]
[l1 (List E)]
[l2 (List E)]
[e1 E]
[e2 E])
(-> (= E e1 e2) (= (List E) l1 l2)
(= (List E) (:: e1 l1) (:: e2 l2)))))
(define same-lists
(λ (E l1 l2 e1 e2)
(λ (e1=e2 l1=l2)
(replace e1=e2
(λ (k)
(= (List E) (:: e1 l1) (:: k l2)))
(same-cons E l1 l2 e1 l1=l2)))))
#;(define same-lists
(λ (E l1 l2 e1 e2)
(λ (prf-e prf-l)
(replace prf-e
(λ (k)
(= (List E) (:: e1 l1) (:: k l2)))
(same-cons E l1 l2 e1 prf-l)))))
;; Exercise 9.3 (was previously called Exercise 8.4)
;;
;; Define a function called plusCommutative that states and proves that
;; + is commutative
;;
;; Bonus: Write the solution using the trans elimiator instead of
;; the replace elimiator.
;; https://docs.racket-lang.org/pie/index.html#%28def._%28%28lib._pie%2Fmain..rkt%29._trans%29%29
(claim plusCommutative
(Π ([n Nat]
[m Nat])
(= Nat (+ n m) (+ m n))))
(claim mot-plusCommutative
(-> Nat Nat
U))
(define mot-plusCommutative
(λ (n m)
(= Nat (+ n m) (+ m n))))
(claim base-plusCommutative
(Π ([n Nat])
(mot-plusCommutative n zero)))
(claim mot-base-plusCommutative
(-> Nat
U))
(define mot-base-plusCommutative
(λ (n)
(mot-plusCommutative n zero)))
(claim base-base-plusCommutative
(= Nat zero zero))
(define base-base-plusCommutative
(same zero))
(claim step-base-plusCommutative
(Π ([n Nat])
(-> (mot-base-plusCommutative n)
(mot-base-plusCommutative (add1 n)))))
(define step-base-plusCommutative
(λ (n)
(λ (n+0=0+n)
(cong n+0=0+n (+ 1)))))
(define base-plusCommutative
(λ (n)
(ind-Nat n
mot-base-plusCommutative
base-base-plusCommutative
step-base-plusCommutative)))
(claim add1-right
(Π ([n Nat]
[m Nat])
(= Nat
(+ m (add1 n))
(add1 (+ m n)))))
(claim mot-add1-right
(-> Nat Nat
U))
(define mot-add1-right
(λ (n m)
(= Nat
(+ m (add1 n))
(add1 (+ m n)))))
(claim base-add1-right
(Π ([n Nat])
(mot-add1-right n zero)))
(define base-add1-right
(λ (n)
(same (add1 n))))
(claim step-add1-right
(Π ([n Nat]
[m Nat])
(-> (mot-add1-right n m)
(mot-add1-right n (add1 m)))))
(define step-add1-right
(λ (n m)
(λ (add1-right-n-m)
(cong add1-right-n-m (+ 1)))))
(define add1-right
(λ (n m)
(ind-Nat m
(mot-add1-right n)
(base-add1-right n)
(step-add1-right n))))
(claim step-plusCommutative
(Π ([n Nat]
[m Nat])
(-> (mot-plusCommutative n m)
(mot-plusCommutative n (add1 m)))))
(claim mot-step-plusCommutative
(-> Nat Nat Nat
U))
(define mot-step-plusCommutative
(λ (n m k)
(= Nat
k
(add1 (+ m n)))))
(define step-plusCommutative
(λ (n m)
(λ (n+m=m+n)
(replace (symm (add1-right m n))
(mot-step-plusCommutative n m)
(cong n+m=m+n (+ 1))))))
(define plusCommutative
(λ (n m)
(ind-Nat m
(mot-plusCommutative n)
(base-plusCommutative n)
(step-plusCommutative n))))
;; Bonus
(claim step-plusCommutative-bonus
(Π ([n Nat]
[m Nat])
(-> (mot-plusCommutative n m)
(mot-plusCommutative n (add1 m)))))
(claim add1-left
(Π ([n Nat]
[m Nat])
(= Nat
(add1 (+ m n))
(+ (add1 m) n))))
(define add1-left
(λ (n m)
(same (add1 (+ m n)))))
(define step-plusCommutative-bonus
(λ (n m)
(λ (n+m=m+n)
(trans (trans (add1-right m n)
(cong n+m=m+n (+ 1)))
(add1-left n m)))))