1 | include "basics/list.ma". |
---|
2 | include "basics/types.ma". |
---|
3 | include "arithmetics/nat.ma". |
---|
4 | |
---|
5 | let rec foldl (A: Type[0]) (B: Type[0]) (f: A → B → A) (a: A) (l: list B) on l ≝ |
---|
6 | match l with |
---|
7 | [ nil ⇒ a |
---|
8 | | cons hd tl ⇒ foldl A B f (f a hd) tl |
---|
9 | ]. |
---|
10 | |
---|
11 | definition flatten ≝ |
---|
12 | λA: Type[0]. |
---|
13 | λl: list (list A). |
---|
14 | foldl ? ? (append ?) [ ] l. |
---|
15 | |
---|
16 | definition if_then_else ≝ |
---|
17 | λA: Type[0]. |
---|
18 | λb: bool. |
---|
19 | λt: A. |
---|
20 | λf: A. |
---|
21 | match b with |
---|
22 | [ true ⇒ t |
---|
23 | | false ⇒ f |
---|
24 | ]. |
---|
25 | |
---|
26 | let rec rev (A: Type[0]) (l: list A) on l ≝ |
---|
27 | match l with |
---|
28 | [ nil ⇒ nil A |
---|
29 | | cons hd tl ⇒ (rev A tl) @ [ hd ] |
---|
30 | ]. |
---|
31 | |
---|
32 | (* |
---|
33 | notation "hvbox('if' b 'then' t 'else' f)" |
---|
34 | non associative with precedence 83 |
---|
35 | for @{ 'if_then_else $b $t $f }. |
---|
36 | *) |
---|
37 | notation > "'if' term 19 e 'then' term 19 t 'else' term 48 f" non associative with precedence 19 for @{ 'if_then_else $e $t $f }. |
---|
38 | notation < "'if' \nbsp term 19 e \nbsp 'then' \nbsp term 19 t \nbsp 'else' \nbsp term 48 f \nbsp" non associative with precedence 19 for @{ 'if_then_else $e $t $f }. |
---|
39 | |
---|
40 | interpretation "Bool if_then_else" 'if_then_else b t f = (if_then_else ? b t f). |
---|
41 | |
---|
42 | let rec fold_left_i_aux (A: Type[0]) (B: Type[0]) |
---|
43 | (f: nat → A → B → A) (x: A) (i: nat) (l: list B) on l ≝ |
---|
44 | match l with |
---|
45 | [ nil ⇒ x |
---|
46 | | cons hd tl ⇒ fold_left_i_aux A B f (f i x hd) (S i) tl |
---|
47 | ]. |
---|
48 | |
---|
49 | definition fold_left_i ≝ λA,B,f,x. fold_left_i_aux A B f x O. |
---|
50 | |
---|
51 | lemma eq_rect_Type0_r : |
---|
52 | ∀A: Type[0]. |
---|
53 | ∀a:A. |
---|
54 | ∀P: ∀x:A. eq ? x a → Type[0]. P a (refl A a) → ∀x: A.∀p:eq ? x a. P x p. |
---|
55 | #A #a #P #H #x #p |
---|
56 | generalize in match H |
---|
57 | generalize in match P |
---|
58 | cases p |
---|
59 | // |
---|
60 | qed. |
---|
61 | |
---|
62 | |
---|
63 | notation "hvbox(t⌈o ↦ h⌉)" |
---|
64 | with precedence 45 |
---|
65 | for @{ match (? : $o=$h) with [ refl ⇒ $t ] }. |
---|
66 | |
---|
67 | definition function_apply ≝ |
---|
68 | λA, B: Type[0]. |
---|
69 | λf: A → B. |
---|
70 | λa: A. |
---|
71 | f a. |
---|
72 | |
---|
73 | notation "f break $ x" |
---|
74 | left associative with precedence 99 |
---|
75 | for @{ 'function_apply $f $x }. |
---|
76 | |
---|
77 | interpretation "Function application" 'function_apply f x = (function_apply ? ? f x). |
---|
78 | |
---|
79 | let rec iterate (A: Type[0]) (f: A → A) (a: A) (n: nat) on n ≝ |
---|
80 | match n with |
---|
81 | [ O ⇒ a |
---|
82 | | S o ⇒ f (iterate A f a o) |
---|
83 | ]. |
---|
84 | |
---|
85 | notation > "hvbox('let' 〈ident x,ident y〉 ≝ t 'in' s)" |
---|
86 | with precedence 10 |
---|
87 | for @{ match $t with [ pair ${ident x} ${ident y} ⇒ $s ] }. |
---|
88 | |
---|
89 | notation "⊥" with precedence 90 |
---|
90 | for @{ match ? in False with [ ] }. |
---|
91 | |
---|
92 | let rec exclusive_disjunction (b: bool) (c: bool) on b ≝ |
---|
93 | match b with |
---|
94 | [ true ⇒ |
---|
95 | match c with |
---|
96 | [ false ⇒ true |
---|
97 | | true ⇒ false |
---|
98 | ] |
---|
99 | | false ⇒ |
---|
100 | match c with |
---|
101 | [ false ⇒ false |
---|
102 | | true ⇒ true |
---|
103 | ] |
---|
104 | ]. |
---|
105 | |
---|
106 | definition ltb ≝ |
---|
107 | λm, n: nat. |
---|
108 | leb (S m) n. |
---|
109 | |
---|
110 | definition geb ≝ |
---|
111 | λm, n: nat. |
---|
112 | ltb n m. |
---|
113 | |
---|
114 | definition gtb ≝ |
---|
115 | λm, n: nat. |
---|
116 | leb n m. |
---|
117 | |
---|
118 | (* dpm: unless I'm being stupid, this isn't defined in the stdlib? *) |
---|
119 | let rec eq_nat (n: nat) (m: nat) on n: bool ≝ |
---|
120 | match n with |
---|
121 | [ O ⇒ match m with [ O ⇒ true | _ ⇒ false ] |
---|
122 | | S n' ⇒ match m with [ S m' ⇒ eq_nat n' m' | _ ⇒ false ] |
---|
123 | ]. |
---|
124 | |
---|
125 | (* dpm: conflicts with library definitions |
---|
126 | interpretation "Nat less than" 'lt m n = (ltb m n). |
---|
127 | interpretation "Nat greater than" 'gt m n = (gtb m n). |
---|
128 | interpretation "Nat greater than eq" 'geq m n = (geb m n). |
---|
129 | *) |
---|
130 | |
---|
131 | let rec division_aux (m: nat) (n : nat) (p: nat) ≝ |
---|
132 | match ltb n (S p) with |
---|
133 | [ true ⇒ O |
---|
134 | | false ⇒ |
---|
135 | match m with |
---|
136 | [ O ⇒ O |
---|
137 | | (S q) ⇒ S (division_aux q (n - (S p)) p) |
---|
138 | ] |
---|
139 | ]. |
---|
140 | |
---|
141 | definition division ≝ |
---|
142 | λm, n: nat. |
---|
143 | match n with |
---|
144 | [ O ⇒ S m |
---|
145 | | S o ⇒ division_aux m m o |
---|
146 | ]. |
---|
147 | |
---|
148 | notation "hvbox(n break ÷ m)" |
---|
149 | right associative with precedence 47 |
---|
150 | for @{ 'division $n $m }. |
---|
151 | |
---|
152 | interpretation "Nat division" 'division n m = (division n m). |
---|
153 | |
---|
154 | let rec modulus_aux (m: nat) (n: nat) (p: nat) ≝ |
---|
155 | match leb n p with |
---|
156 | [ true ⇒ n |
---|
157 | | false ⇒ |
---|
158 | match m with |
---|
159 | [ O ⇒ n |
---|
160 | | S o ⇒ modulus_aux o (n - (S p)) p |
---|
161 | ] |
---|
162 | ]. |
---|
163 | |
---|
164 | definition modulus ≝ |
---|
165 | λm, n: nat. |
---|
166 | match n with |
---|
167 | [ O ⇒ m |
---|
168 | | S o ⇒ modulus_aux m m o |
---|
169 | ]. |
---|
170 | |
---|
171 | notation "hvbox(n break 'mod' m)" |
---|
172 | right associative with precedence 47 |
---|
173 | for @{ 'modulus $n $m }. |
---|
174 | |
---|
175 | interpretation "Nat modulus" 'modulus m n = (modulus m n). |
---|
176 | |
---|
177 | definition divide_with_remainder ≝ |
---|
178 | λm, n: nat. |
---|
179 | pair ? ? (m ÷ n) (modulus m n). |
---|
180 | |
---|
181 | let rec exponential (m: nat) (n: nat) on n ≝ |
---|
182 | match n with |
---|
183 | [ O ⇒ S O |
---|
184 | | S o ⇒ m * exponential m o |
---|
185 | ]. |
---|
186 | |
---|
187 | interpretation "Nat exponential" 'exp n m = (exponential n m). |
---|
188 | |
---|
189 | notation "hvbox(a break ⊎ b)" |
---|
190 | left associative with precedence 50 |
---|
191 | for @{ 'disjoint_union $a $b }. |
---|
192 | interpretation "sum" 'disjoint_union A B = (Sum A B). |
---|
193 | |
---|
194 | theorem less_than_or_equal_monotone: |
---|
195 | ∀m, n: nat. |
---|
196 | m ≤ n → (S m) ≤ (S n). |
---|
197 | #m #n #H |
---|
198 | elim H |
---|
199 | /2/ |
---|
200 | qed. |
---|
201 | |
---|
202 | theorem less_than_or_equal_b_complete: |
---|
203 | ∀m, n: nat. |
---|
204 | leb m n = false → ¬(m ≤ n). |
---|
205 | #m; |
---|
206 | elim m; |
---|
207 | normalize |
---|
208 | [ #n #H |
---|
209 | destruct |
---|
210 | | #y #H1 #z |
---|
211 | cases z |
---|
212 | normalize |
---|
213 | [ #H |
---|
214 | /2/ |
---|
215 | | /3/ |
---|
216 | ] |
---|
217 | ] |
---|
218 | qed. |
---|
219 | |
---|
220 | theorem less_than_or_equal_b_correct: |
---|
221 | ∀m, n: nat. |
---|
222 | leb m n = true → m ≤ n. |
---|
223 | #m |
---|
224 | elim m |
---|
225 | // |
---|
226 | #y #H1 #z |
---|
227 | cases z |
---|
228 | normalize |
---|
229 | [ #H |
---|
230 | destruct |
---|
231 | | #n #H lapply (H1 … H) /2/ |
---|
232 | ] |
---|
233 | qed. |
---|
234 | |
---|
235 | definition less_than_or_equal_b_elim: |
---|
236 | ∀m, n: nat. |
---|
237 | ∀P: bool → Type[0]. |
---|
238 | (m ≤ n → P true) → (¬(m ≤ n) → P false) → P (leb m n). |
---|
239 | #m #n #P #H1 #H2; |
---|
240 | lapply (less_than_or_equal_b_correct m n) |
---|
241 | lapply (less_than_or_equal_b_complete m n) |
---|
242 | cases (leb m n) |
---|
243 | /3/ |
---|
244 | qed. |
---|