-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter10-2-3-smallertrans.rkt
152 lines (126 loc) · 3.22 KB
/
chapter10-2-3-smallertrans.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
#lang pie
;; In the following exercises we'll use the function called <= that takes two
;; Nat arguments a, b and evaluates to a type representing the proposition
;; that a is less than or equal to b.
;(claim <=
; (-> Nat Nat
; U))
;
;(define <=
; (λ (a b)
; (Σ ([k Nat])
; (= Nat (+ k a) b))))
;; Define a function called <=-trans that states and proves that <= is
;; transitive.
;(claim <=-trans
; (Π ([a Nat]
; [b Nat]
; [c Nat])
; (-> (<= a b)
; (<= b c)
; (<= a c))))
(claim +
(-> Nat Nat Nat))
(define +
(lambda (x y)
(rec-Nat x
y
(lambda (_ y+x-1)
(add1 y+x-1)))))
;; plus-assoc: (= Nat (+ k (+ n m)) (+ (+ k n) m)))
(claim mot-plus-assoc
(-> Nat Nat Nat U))
(define mot-plus-assoc
(lambda (n m k)
(= Nat (+ k (+ n m)) (+ (+ k n) m))))
(claim base-plus-assoc
(Pi ((n Nat) (m Nat))
(= Nat (+ 0 (+ n m)) (+ (+ 0 n) m))))
(define base-plus-assoc
(lambda (n m)
(same (+ n m))))
(claim step-plus-assoc
(Pi ((n Nat) (m Nat) (k-1 Nat))
(-> (mot-plus-assoc n m k-1)
(mot-plus-assoc n m (add1 k-1)))))
(define step-plus-assoc
(lambda (n m k-1)
(lambda (mot-plus-assoc-k-1)
(cong mot-plus-assoc-k-1 (+ 1)))))
(claim plus-assoc
(Pi ((k Nat) (n Nat) (m Nat))
(= Nat (+ k (+ n m)) (+ (+ k n) m))))
(define plus-assoc
(lambda (k n m)
(ind-Nat k
(mot-plus-assoc n m)
(base-plus-assoc n m)
(step-plus-assoc n m))))
;; End of preamble
(claim <=
(-> Nat Nat
U))
(define <=
(lambda (a b)
(Sigma ((k Nat))
(= Nat (+ k a) b))))
(claim <=-trans
(Pi ((a Nat)
(b Nat)
(c Nat))
(-> (<= a b)
(<= b c)
(<= a c))))
;; there exists k where k + a == b
;; there exists j where j + b == c
;; so we can get j + k + a == c
;; which is (j + k) + a == c
;; which means there exists a (j + k) where (j + k) + a == c
;; which is our goal
(claim sum-trans
(Pi ((a Nat) (b Nat) (c Nat) (k Nat) (j Nat))
(-> (= Nat (+ k a) b)
(= Nat (+ j b) c)
(= Nat (+ j (+ k a)) c))))
(define sum-trans
(lambda (a b c k j)
(lambda (k+a=b j+b=c)
(replace (symm k+a=b)
(lambda (here) (= Nat (+ j here) c))
j+b=c))))
;; there exists (car a<=b) where (car a<=b) + a == b
;; there exists (car b<=c) where (car b<=c) + b == c
;; we need to swap things around for replace to work
;; (symm (cdr a<=b)) = (= Nat b (+ (car a<=b) a))
;; after replace we have
;; (= Nat
;; (+
;; (car b<=c)
;; (+ (car a<=b) a))
;; c)
;; but we need
;; (= Nat
;; (+
;; (+ (car b<=c) (car a<=b))
;; a)
;; c)
;; which requires associativity plus-assoc
;; (= Nat (+ k (+ n m)) (+ (+ k n) m))
(claim plus-trans
(Pi ((a Nat) (b Nat) (c Nat) (k Nat) (j Nat))
(-> (= Nat (+ k a) b)
(= Nat (+ j b) c)
(= Nat (+ (+ j k) a) c))))
(define plus-trans
(lambda (a b c k j)
(lambda (k+a=b j+b=c)
(replace (plus-assoc j k a)
(lambda (here) (= Nat here c))
(sum-trans a b c k j k+a=b j+b=c)))))
(define <=-trans
(lambda (a b c)
(lambda (a<=b b<=c)
(cons
(+ (car b<=c) (car a<=b))
(plus-trans a b c (car a<=b) (car b<=c) (cdr a<=b) (cdr b<=c))
))))