source: src/ASM/Util.ma @ 715

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

Restored rev from Util as it appears that list reversal is not a part of the standard library, but a local modification.

File size: 5.1 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: conflicts with library definitions
119interpretation "Nat less than" 'lt m n = (ltb m n).
120interpretation "Nat greater than" 'gt m n = (gtb m n).
121interpretation "Nat greater than eq" 'geq m n = (geb m n).
122*)
123
124let rec division_aux (m: nat) (n : nat) (p: nat) ≝
125  match ltb n (S p) with
126    [ true ⇒ O
127    | false ⇒
128      match m with
129        [ O ⇒ O
130        | (S q) ⇒ S (division_aux q (n - (S p)) p)
131        ]
132    ].
133   
134definition division ≝
135  λm, n: nat.
136    match n with
137      [ O ⇒ S m
138      | S o ⇒ division_aux m m o
139      ].
140     
141notation "hvbox(n break ÷ m)"
142  right associative with precedence 47
143  for @{ 'division $n $m }.
144 
145interpretation "Nat division" 'division n m = (division n m).
146
147let rec modulus_aux (m: nat) (n: nat) (p: nat) ≝
148  match leb n p with
149    [ true ⇒ n
150    | false ⇒
151      match m with
152        [ O ⇒ n
153        | S o ⇒ modulus_aux o (n - (S p)) p
154        ]
155    ].
156   
157definition modulus ≝
158  λm, n: nat.
159    match n with
160      [ O ⇒ m
161      | S o ⇒ modulus_aux m m o
162      ].
163   
164notation "hvbox(n break 'mod' m)"
165  right associative with precedence 47
166  for @{ 'modulus $n $m }.
167 
168interpretation "Nat modulus" 'modulus m n = (modulus m n).
169
170definition divide_with_remainder ≝
171  λm, n: nat.
172    pair ? ? (m ÷ n) (modulus m n).
173   
174let rec exponential (m: nat) (n: nat) on n ≝
175  match n with
176    [ O ⇒ S O
177    | S o ⇒ m * exponential m o
178    ].
179
180interpretation "Nat exponential" 'exp n m = (exponential n m).
181   
182notation "hvbox(a break ⊎ b)"
183 left associative with precedence 50
184for @{ 'disjoint_union $a $b }.
185interpretation "sum" 'disjoint_union A B = (Sum A B).
186
187theorem less_than_or_equal_monotone:
188  ∀m, n: nat.
189    m ≤ n → (S m) ≤ (S n).
190 #m #n #H
191 elim H
192 /2/
193qed.
194
195theorem less_than_or_equal_b_complete:
196  ∀m, n: nat.
197    leb m n = false → ¬(m ≤ n).
198 #m;
199 elim m;
200 normalize
201 [ #n #H
202   destruct
203 | #y #H1 #z
204   cases z
205   normalize
206   [ #H
207     /2/
208   | /3/
209   ]
210 ]
211qed.
212
213theorem less_than_or_equal_b_correct:
214  ∀m, n: nat.
215    leb m n = true → m ≤ n.
216 #m
217 elim m
218 //
219 #y #H1 #z
220 cases z
221 normalize
222 [ #H
223   destruct
224 | #n #H lapply (H1 … H) /2/
225 ]
226qed.
227
228definition less_than_or_equal_b_elim:
229 ∀m, n: nat.
230 ∀P: bool → Type[0].
231   (m ≤ n → P true) → (¬(m ≤ n) → P false) → P (leb m n).
232 #m #n #P #H1 #H2;
233 lapply (less_than_or_equal_b_correct m n)
234 lapply (less_than_or_equal_b_complete m n)
235 cases (leb m n)
236 /3/
237qed.
Note: See TracBrowser for help on using the repository browser.