source: src/ASM/Util.ma @ 746

Last change on this file since 746 was 746, checked in by mulligan, 9 years ago

Changes to bitvectortrieset: equality on sets. Added new file for translation of variable set functor. other small changes.

File size: 5.4 KB
Line 
1include "basics/list.ma".
2include "basics/types.ma".
3include "arithmetics/nat.ma".
4
5let 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
11definition flatten ≝
12  λA: Type[0].
13  λl: list (list A).
14    foldl ? ? (append ?) [ ] l.
15
16definition 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
26let 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(*
33notation "hvbox('if' b 'then' t 'else' f)"
34  non associative with precedence 83
35  for @{ 'if_then_else $b $t $f }.
36*)
37notation > "'if' term 19 e 'then' term 19 t 'else' term 48 f" non associative with precedence 19 for @{ 'if_then_else $e $t $f }.
38notation < "'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
40interpretation "Bool if_then_else" 'if_then_else b t f = (if_then_else ? b t f).
41
42let 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
49definition fold_left_i ≝ λA,B,f,x. fold_left_i_aux A B f x O.
50
51lemma 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  //
60qed.
61
62
63notation "hvbox(t⌈o ↦ h⌉)"
64  with precedence 45
65  for @{ match (? : $o=$h) with [ refl ⇒ $t ] }.
66
67definition function_apply ≝
68  λA, B: Type[0].
69  λf: A → B.
70  λa: A.
71    f a.
72   
73notation "f break $ x"
74  left associative with precedence 99
75  for @{ 'function_apply $f $x }.
76 
77interpretation "Function application" 'function_apply f x = (function_apply ? ? f x).
78
79let 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   
85notation > "hvbox('let' 〈ident x,ident y〉 ≝ t 'in' s)"
86 with precedence 10
87for @{ match $t with [ pair ${ident x} ${ident y} ⇒ $s ] }.
88
89notation "⊥" with precedence 90
90  for @{ match ? in False with [ ] }.
91
92let 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   
106definition ltb ≝
107  λm, n: nat.
108    leb (S m) n.
109   
110definition geb ≝
111  λm, n: nat.
112    ltb n m.
113
114definition gtb ≝
115  λm, n: nat.
116    leb n m.
117   
118(* dpm: unless I'm being stupid, this isn't defined in the stdlib? *)
119let 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
126interpretation "Nat less than" 'lt m n = (ltb m n).
127interpretation "Nat greater than" 'gt m n = (gtb m n).
128interpretation "Nat greater than eq" 'geq m n = (geb m n).
129*)
130
131let 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   
141definition division ≝
142  λm, n: nat.
143    match n with
144      [ O ⇒ S m
145      | S o ⇒ division_aux m m o
146      ].
147     
148notation "hvbox(n break ÷ m)"
149  right associative with precedence 47
150  for @{ 'division $n $m }.
151 
152interpretation "Nat division" 'division n m = (division n m).
153
154let 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   
164definition modulus ≝
165  λm, n: nat.
166    match n with
167      [ O ⇒ m
168      | S o ⇒ modulus_aux m m o
169      ].
170   
171notation "hvbox(n break 'mod' m)"
172  right associative with precedence 47
173  for @{ 'modulus $n $m }.
174 
175interpretation "Nat modulus" 'modulus m n = (modulus m n).
176
177definition divide_with_remainder ≝
178  λm, n: nat.
179    pair ? ? (m ÷ n) (modulus m n).
180   
181let 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
187interpretation "Nat exponential" 'exp n m = (exponential n m).
188   
189notation "hvbox(a break ⊎ b)"
190 left associative with precedence 50
191for @{ 'disjoint_union $a $b }.
192interpretation "sum" 'disjoint_union A B = (Sum A B).
193
194theorem 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/
200qed.
201
202theorem 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 ]
218qed.
219
220theorem 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 ]
233qed.
234
235definition 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/
244qed.
Note: See TracBrowser for help on using the repository browser.