1 include "basics/list.ma".
2 include "basics/types.ma".
3 include "arithmetics/nat.ma".
5 include "utilities/pair.ma".
6 include "ASM/JMCoercions.ma".
8 (* let's implement a daemon not used by automation *)
9 inductive DAEMONXXX: Type[0] ≝ K1DAEMONXXX: DAEMONXXX | K2DAEMONXXX: DAEMONXXX.
10 axiom IMPOSSIBLE: K1DAEMONXXX = K2DAEMONXXX.
11 example daemon: False. generalize in match IMPOSSIBLE; #IMPOSSIBLE destruct(IMPOSSIBLE) qed.
12 example not_implemented: False. cases daemon qed.
14 notation "⊥" with precedence 90
15 for @{ match ? in False with [ ] }.
29 (* dpm: unless I'm being stupid, this isn't defined in the stdlib? *)
30 let rec eq_nat (n: nat) (m: nat) on n: bool ≝
32 [ O ⇒ match m with [ O ⇒ true | _ ⇒ false ]
33 | S n' ⇒ match m with [ S m' ⇒ eq_nat n' m' | _ ⇒ false ]
37 (A: Type[0]) (f: A → bool) (l: list A)
41 | cons hd tl ⇒ f hd ∧ forall A f tl
45 (A: Type[0]) (k: nat) (l: list A)
52 | S k' ⇒ hd :: prefix A k' tl
57 (A: Type[0]) (B: Type[0]) (C: Type[0]) (f: A → B → C → A) (accu: A)
58 (left: list B) (right: list C) (proof: |left| = |right|)
60 match left return λx. |x| = |right| → A with
62 match right return λx. |[ ]| = |x| → A with
63 [ nil ⇒ λnil_nil_prf. accu
64 | cons hd tl ⇒ λcons_nil_absrd. ?
66 | cons hd tl ⇒ λcons_prf.
67 match right return λx. |hd::tl| = |x| → A with
68 [ nil ⇒ λcons_nil_absrd. ?
69 | cons hd' tl' ⇒ λcons_cons_prf.
70 fold_left2 … f (f accu hd hd') tl tl' ?
73 [ 1: normalize in cons_nil_absrd;
74 destruct(cons_nil_absrd)
75 | 2: normalize in cons_nil_absrd;
76 destruct(cons_nil_absrd)
77 | 3: normalize in cons_cons_prf;
83 let rec remove_n_first_internal
84 (i: nat) (A: Type[0]) (l: list A) (n: nat)
91 | _ ⇒ remove_n_first_internal (S i) A tl n
95 definition remove_n_first ≝
99 remove_n_first_internal 0 A l n.
101 let rec foldi_from_until_internal
102 (A: Type[0]) (i: nat) (res: ?) (rem: list A) (m: nat) (f: nat → list A → A → list A)
109 | _ ⇒ foldi_from_until_internal A (S i) (f i res e) tl m f
113 definition foldi_from_until ≝
120 foldi_from_until_internal A 0 a (remove_n_first A n l) m f.
122 definition foldi_from ≝
128 foldi_from_until A n (|l|) f a l.
130 definition foldi_until ≝
136 foldi_from_until A 0 m f a l.
143 foldi_from_until A 0 (|l|) f a l.
149 match l return λx. 0 < |x| → A with
150 [ nil ⇒ λnil_absrd. ?
151 | cons hd tl ⇒ λcons_prf. hd
153 normalize in nil_absrd;
159 definition tail_safe ≝
163 match l return λx. 0 < |x| → list A with
164 [ nil ⇒ λnil_absrd. ?
165 | cons hd tl ⇒ λcons_prf. tl
167 normalize in nil_absrd;
174 (A: Type[0]) (l: list A) (index: nat) (proof: index ≤ |l|)
176 match index return λx. x ≤ |l| → (list A) × (list A) with
177 [ O ⇒ λzero_prf. 〈[], l〉
178 | S index' ⇒ λsucc_prf.
179 match l return λx. S index' ≤ |x| → (list A) × (list A) with
180 [ nil ⇒ λnil_absrd. ?
181 | cons hd tl ⇒ λcons_prf.
182 let 〈l1, l2〉 ≝ split A tl index' ? in
186 [1: normalize in nil_absrd;
187 cases(not_le_Sn_O index')
190 |2: normalize in cons_prf;
197 (elt_type: Type[0]) (index: nat) (the_list: list elt_type)
198 (proof: index < | the_list |)
200 match index return λs. s < | the_list | → elt_type with
202 match the_list return λt. 0 < | t | → elt_type with
203 [ nil ⇒ λnil_absurd. ?
204 | cons hd tl ⇒ λcons_proof. hd
207 match the_list return λt. S index' < | t | → elt_type with
208 [ nil ⇒ λnil_absurd. ?
210 λcons_proof. nth_safe elt_type index' tl ?
213 [ normalize in nil_absurd;
214 cases (not_le_Sn_O 0)
216 elim (ABSURD nil_absurd)
217 | normalize in nil_absurd;
218 cases (not_le_Sn_O (S index'))
220 elim (ABSURD nil_absurd)
221 | normalize in cons_proof
227 definition last_safe ≝
229 λthe_list: list elt_type.
230 λproof : 0 < | the_list |.
231 nth_safe elt_type (|the_list| - 1) the_list ?.
236 (A: Type[0]) (B: Type[0]) (left: list A) (right: list B) on left ≝
238 [ nil ⇒ 〈〈[ ], [ ]〉, 〈[ ], right〉〉
241 [ nil ⇒ 〈〈[ ], left〉, 〈[ ], [ ]〉〉
243 let 〈cleft, cright〉 ≝ reduce A B tl tl' in
244 let 〈commonl, restl〉 ≝ cleft in
245 let 〈commonr, restr〉 ≝ cright in
246 〈〈hd :: commonl, restl〉, 〈hd' :: commonr, restr〉〉
255 Σret: ((list A) × (list A)) × ((list A) × (list A)). | \fst (\fst ret) | = | \fst (\snd ret) |.
258 let rec reduce_strong
259 (A: Type[0]) (B: Type[0]) (left: list A) (right: list B)
260 on left : Σret: ((list A) × (list A)) × ((list B) × (list B)). |\fst (\fst ret)| = |\fst (\snd ret)| ≝
262 [ nil ⇒ 〈〈[ ], [ ]〉, 〈[ ], right〉〉
265 [ nil ⇒ 〈〈[ ], left〉, 〈[ ], [ ]〉〉
267 let 〈cleft, cright〉 ≝ reduce_strong A B tl tl' in
268 let 〈commonl, restl〉 ≝ cleft in
269 let 〈commonr, restr〉 ≝ cright in
270 〈〈hd :: commonl, restl〉, 〈hd' :: commonr, restr〉〉
276 generalize in match (sig2 … (reduce_strong A B tl tl1));
277 >p2 >p3 >p4 normalize in ⊢ (% → ?)
283 (A: Type[0]) (B: Type[0]) (C: Type[0]) (f: A → B → C)
284 (left: list A) (right: list B) on left ≝
288 [ nil ⇒ Some ? (nil C)
295 match map2_opt A B C f tl tl' with
297 | Some tail ⇒ Some ? (f hd hd' :: tail)
303 (A: Type[0]) (B: Type[0]) (C: Type[0]) (f: A → B → C)
304 (left: list A) (right: list B) (proof: | left | = | right |) on left ≝
305 match left return λx. | x | = | right | → list C with
307 match right return λy. | [] | = | y | → list C with
308 [ nil ⇒ λnil_prf. nil C
312 match right return λy. | hd::tl | = | y | → list C with
313 [ nil ⇒ λnil_absrd. ?
314 | cons hd' tl' ⇒ λcons_prf. (f hd hd') :: map2 A B C f tl tl' ?
317 [1: normalize in cons_absrd;
319 |2: normalize in nil_absrd;
321 |3: normalize in cons_prf;
328 (A: Type[0]) (B: Type[0]) (C: Type[0]) (D: Type[0]) (f: A → B → C → D)
329 (left: list A) (centre: list B) (right: list C)
330 (prflc: |left| = |centre|) (prfcr: |centre| = |right|) on left ≝
331 match left return λx. |x| = |centre| → list D with
333 match centre return λx. |x| = |right| → list D with
334 [ nil ⇒ λnil_nil_prf.
335 match right return λx. |nil ?| = |x| → list D with
336 [ nil ⇒ λnil_nil_nil_prf. nil D
337 | cons hd tl ⇒ λcons_nil_nil_absrd. ?
339 | cons hd tl ⇒ λnil_cons_absrd. ?
341 | cons hd tl ⇒ λcons_prf.
342 match centre return λx. |x| = |right| → list D with
343 [ nil ⇒ λcons_nil_absrd. ?
344 | cons hd' tl' ⇒ λcons_cons_prf.
345 match right return λx. |right| = |x| → |cons ? hd' tl'| = |x| → list D with
346 [ nil ⇒ λrefl_prf. λcons_cons_nil_absrd. ?
347 | cons hd'' tl'' ⇒ λrefl_prf. λcons_cons_cons_prf.
348 (f hd hd' hd'') :: (map3 A B C D f tl tl' tl'' ? ?)
349 ] (refl ? (|right|)) cons_cons_prf
352 [ 1: normalize in cons_nil_nil_absrd;
353 destruct(cons_nil_nil_absrd)
354 | 2: generalize in match nil_cons_absrd;
355 \ 5prfcr
\ 6\ 5nil_prf #hyp="" normalize="" hyp;="" destruct(hyp)="" |="" 3:="" generalize="" in="" match="" cons_nil_absrd;=""
\ 6\ 5prfcr
\ 6\ 5cons_prf #hyp="" hyp;="" destruct(hyp)="" 4:="" cons_cons_nil_absrd;="" destruct(cons_cons_nil_absrd)="" 5:="" normalize="" destruct(cons_cons_cons_prf)="" assumption="" |="" 6:="" generalize="" in="" match="" cons_cons_cons_prf;=""
\ 6\ 5refl_prf
\ 6\ 5prfcr
\ 6\ 5cons_prf #hyp="" normalize="" hyp;="" destruct(hyp)="" @sym_eq="" assumption="" ]="" lemma="" eq_rect_type0_r="" :="" ∀a:="" ∀a:a.="" ∀p:="" ∀x:a.="" eq="" type[0].="" (refl="" a="" →="" ∀x:="" a.∀p:eq="" ?="" a.="" x="" p.="" #a="" #h="" #x="" #p="" h="" generalize="" in="" match="" cases="" p="" qed.="" let="" rec="" safe_nth="" (a:="" type[0])="" (n:="" nat)="" (l:="" list="" a)="" (p:="" n=""
\ 6< length A l) on n: A ≝
356 match n return λo. o < length A l → A with
358 match l return λm. 0 < length A m → A with
360 | cons hd tl ⇒ λprf1. hd
363 match l return λm. S n' < length A m → A with
365 | cons hd tl ⇒ λprf2. safe_nth A n' tl ?
377 cases (not_le_Sn_O O)
382 cases (not_le_Sn_O (S n'))
388 let rec nub_by_internal (A: Type[0]) (f: A → A → bool) (l: list A) (n: nat) on n ≝
399 hd :: nub_by_internal A f (filter ? (λy. notb (f y hd)) tl) n
407 nub_by_internal A f l (length ? l).
409 let rec member (A: Type[0]) (eq: A → A → bool) (a: A) (l: list A) on l ≝
412 | cons hd tl ⇒ orb (eq a hd) (member A eq a tl)
415 let rec take (A: Type[0]) (n: nat) (l: list A) on n: list A ≝
421 | cons hd tl ⇒ hd :: take A n tl
425 let rec drop (A: Type[0]) (n: nat) (l: list A) on n ≝
431 | cons hd tl ⇒ drop A n tl
435 definition list_split ≝
439 〈take A n l, drop A n l〉.
441 let rec mapi_internal (A: Type[0]) (B: Type[0]) (n: nat) (f: nat → A → B)
442 (l: list A) on l: list B ≝
445 | cons hd tl ⇒ (f n hd) :: (mapi_internal A B (n + 1) f tl)
452 mapi_internal A B 0 f l.
455 (A: Type[0]) (B: Type[0]) (left: list A) (right: list B)
462 | cons hd' tl' ⇒ 〈hd, hd'〉 :: zip_pottier A B tl tl'
467 (A: Type[0]) (B: Type[0]) (left: list A) (right: list B) (prf: |left| = |right|)
469 match left return λx. |x| = |right| → list (A × B) with
471 match right return λx. |[ ]| = |x| → list (A × B) with
472 [ nil ⇒ λnil_nil_prf. [ ]
473 | cons hd tl ⇒ λnil_cons_absrd. ?
475 | cons hd tl ⇒ λcons_prf.
476 match right return λx. |hd::tl| = |x| → list (A × B) with
477 [ nil ⇒ λcons_nil_absrd. ?
478 | cons hd' tl' ⇒ λcons_cons_prf. 〈hd, hd'〉 :: zip_safe A B tl tl' ?
481 [ 1: normalize in nil_cons_absrd;
482 destruct(nil_cons_absrd)
483 | 2: normalize in cons_nil_absrd;
484 destruct(cons_nil_absrd)
485 | 3: normalize in cons_cons_prf;
491 let rec zip (A: Type[0]) (B: Type[0]) (l: list A) (r: list B) on l: option (list (A × B)) ≝
493 [ nil ⇒ Some ? (nil (A × B))
498 match zip ? ? tl tl' with
500 | Some tail ⇒ Some ? (〈hd, hd'〉 :: tail)
505 let rec foldl (A: Type[0]) (B: Type[0]) (f: A → B → A) (a: A) (l: list B) on l ≝
508 | cons hd tl ⇒ foldl A B f (f a hd) tl
518 foldl A B H acc (pre@[hd]) = (H (foldl A B H acc pre) hd).
519 #A #B #H #acc #pre generalize in match acc; -acc; elim pre
521 | #hd #tl #IH #acc #X normalize; @IH ]
530 foldl A B H acc (pre@suff) = (foldl A B H (foldl A B H acc pre) suff).
531 #A #B #H #acc #suff elim suff
533 | #hd #tl #IH #pre whd in ⊢ (???%) <(foldl_step … H ??) applyS (IH (pre@[hd])) ]
539 foldr ? ? (append ?) [ ] l.
541 let rec rev (A: Type[0]) (l: list A) on l ≝
544 | cons hd tl ⇒ (rev A tl) @ [ hd ]
550 |(l @ r)| = |l| + |r|.
570 rev A (l @ r) = rev A r @ rev A l.
573 [ normalize >append_nil %
589 >(append_length A (rev A TL) [HD])
594 lemma nth_append_first:
596 ∀n:nat.∀l1,l2:list A.∀d:A.
597 n < |l1| → nth n A (l1@l2) d = nth n A l1 d.
599 generalize in match n; -n; elim l1
600 [ normalize #k #Hk @⊥ @(absurd … Hk) @not_le_Sn_O
601 | #h #t #Hind #k normalize
603 [ #Hk normalize @refl
604 | #k #Hk normalize @(Hind k) @le_S_S_to_le @Hk
609 lemma nth_append_second:
610 ∀A: Type[0].∀n.∀l1,l2:list A.∀d.n ≥ length A l1 ->
611 nth n A (l1@l2) d = nth (n - length A l1) A l2 d.
613 generalize in match n; -n; elim l1
614 [ normalize #k #Hk <(minus_n_O) @refl
615 | #h #t #Hind #k normalize
617 [ #Hk @⊥ @(absurd (S (|t|) ≤ 0)) [ @Hk | @not_le_Sn_O ]
618 | #k #Hk normalize @(Hind k) @le_S_S_to_le @Hk
624 notation > "'if' term 19 e 'then' term 19 t 'else' term 48 f" non associative with precedence 19
625 for @{ match $e in bool with [ true ⇒ $t | false ⇒ $f] }.
626 notation < "hvbox('if' \nbsp term 19 e \nbsp break 'then' \nbsp term 19 t \nbsp break 'else' \nbsp term 48 f \nbsp)" non associative with precedence 19
627 for @{ match $e with [ true ⇒ $t | false ⇒ $f] }.
629 let rec fold_left_i_aux (A: Type[0]) (B: Type[0])
630 (f: nat → A → B → A) (x: A) (i: nat) (l: list B) on l ≝
633 | cons hd tl ⇒ fold_left_i_aux A B f (f i x hd) (S i) tl
636 definition fold_left_i ≝ λA,B,f,x. fold_left_i_aux A B f x O.
638 notation "hvbox(t⌈o ↦ h⌉)"
640 for @{ match (? : $o=$h) with [ refl ⇒ $t ] }.
642 definition function_apply ≝
648 notation "f break $ x"
649 left associative with precedence 99
650 for @{ 'function_apply $f $x }.
652 interpretation "Function application" 'function_apply f x = (function_apply ? ? f x).
654 let rec iterate (A: Type[0]) (f: A → A) (a: A) (n: nat) on n ≝
657 | S o ⇒ f (iterate A f a o)
660 (* Yeah, I probably ought to do something more general... *)
661 notation "hvbox(\langle term 19 a, break term 19 b, break term 19 c\rangle)"
662 with precedence 90 for @{ 'triple $a $b $c}.
663 interpretation "Triple construction" 'triple x y z = (pair ? ? (pair ? ? x y) z).
665 notation "hvbox(\langle term 19 a, break term 19 b, break term 19 c, break term 19 d\rangle)"
666 with precedence 90 for @{ 'quadruple $a $b $c $d}.
667 interpretation "Quadruple construction" 'quadruple w x y z = (pair ? ? (pair ? ? w x) (pair ? ? y z)).
669 notation > "hvbox('let' 〈ident w,ident x,ident y,ident z〉 ≝ t 'in' s)"
671 for @{ match $t with [ pair ${fresh wx} ${fresh yz} ⇒ match ${fresh wx} with [ pair ${ident w} ${ident x} ⇒ match ${fresh yz} with [ pair ${ident y} ${ident z} ⇒ $s ] ] ] }.
673 notation > "hvbox('let' 〈ident x,ident y,ident z〉 ≝ t 'in' s)"
675 for @{ match $t with [ pair ${fresh xy} ${ident z} ⇒ match ${fresh xy} with [ pair ${ident x} ${ident y} ⇒ $s ] ] }.
677 notation < "hvbox('let' \nbsp hvbox(〈ident x,ident y〉\nbsp ≝ break t \nbsp 'in' \nbsp) break s)"
679 for @{ match $t with [ pair (${ident x}:$ignore) (${ident y}:$ignora) ⇒ $s ] }.
686 (∀lft, rgt. p = 〈lft,rgt〉 → P 〈lft,rgt〉 (T lft rgt)) →
687 P p (let 〈lft, rgt〉 ≝ p in T lft rgt).
694 ∀P: A×B → C → C' → Prop.
695 (∀lft, rgt. p = 〈lft,rgt〉 → P 〈lft,rgt〉 (T lft rgt) (T' lft rgt)) →
696 P p (let 〈lft, rgt〉 ≝ p in T lft rgt) (let 〈lft, rgt〉 ≝ p in T' lft rgt).
698 lemma pair_destruct_1:
699 ∀A,B.∀a:A.∀b:B.∀c. 〈a,b〉 = c → a = \fst c.
703 lemma pair_destruct_2:
704 ∀A,B.∀a:A.∀b:B.∀c. 〈a,b〉 = c → b = \snd c.
709 let rec exclusive_disjunction (b: bool) (c: bool) on b ≝
723 (* dpm: conflicts with library definitions
724 interpretation "Nat less than" 'lt m n = (ltb m n).
725 interpretation "Nat greater than" 'gt m n = (gtb m n).
726 interpretation "Nat greater than eq" 'geq m n = (geb m n).
729 let rec division_aux (m: nat) (n : nat) (p: nat) ≝
730 match ltb n (S p) with
735 | (S q) ⇒ S (division_aux q (n - (S p)) p)
739 definition division ≝
743 | S o ⇒ division_aux m m o
746 notation "hvbox(n break ÷ m)"
747 right associative with precedence 47
748 for @{ 'division $n $m }.
750 interpretation "Nat division" 'division n m = (division n m).
752 let rec modulus_aux (m: nat) (n: nat) (p: nat) ≝
758 | S o ⇒ modulus_aux o (n - (S p)) p
766 | S o ⇒ modulus_aux m m o
769 notation "hvbox(n break 'mod' m)"
770 right associative with precedence 47
771 for @{ 'modulus $n $m }.
773 interpretation "Nat modulus" 'modulus m n = (modulus m n).
775 definition divide_with_remainder ≝
777 pair ? ? (m ÷ n) (modulus m n).
779 let rec exponential (m: nat) (n: nat) on n ≝
782 | S o ⇒ m * exponential m o
785 interpretation "Nat exponential" 'exp n m = (exponential n m).
787 notation "hvbox(a break ⊎ b)"
788 left associative with precedence 50
789 for @{ 'disjoint_union $a $b }.
790 interpretation "sum" 'disjoint_union A B = (Sum A B).
792 theorem less_than_or_equal_monotone:
794 m ≤ n → (S m) ≤ (S n).
800 theorem less_than_or_equal_b_complete:
802 leb m n = false → ¬(m ≤ n).
818 theorem less_than_or_equal_b_correct:
820 leb m n = true → m ≤ n.
829 | #n #H lapply (H1 … H) /2/
833 definition less_than_or_equal_b_elim:
836 (m ≤ n → P true) → (¬(m ≤ n) → P false) → P (leb m n).
838 lapply (less_than_or_equal_b_correct m n)
839 lapply (less_than_or_equal_b_complete m n)
844 lemma inclusive_disjunction_true:
846 (orb b c) = true → b = true ∨ c = true.
859 lemma conjunction_true:
861 andb b c = true → b = true ∧ c = true.
872 lemma eq_true_false: false=true → False.
877 lemma inclusive_disjunction_b_true: ∀b. orb b true = true.
883 definition bool_to_Prop ≝
884 λb. match b with [ true ⇒ True | false ⇒ False ].
886 coercion bool_to_Prop: ∀b:bool. Prop ≝ bool_to_Prop on _b:bool to Type[0].
888 lemma eq_false_to_notb: ∀b. b = false → ¬ b.
894 |l1 @ l2| = |l1| + |l2|.
897 | #hd #tl #IH #l2 normalize
\ 5ih ]="" qed.=""
\ 6\ 5/ih
\ 6\ 5/cons_prf
\ 6\ 5/prfcr
\ 6\ 5/refl_prf
\ 6\ 5/cons_prf
\ 6\ 5/prfcr
\ 6\ 5/nil_prf
\ 6\ 5/prfcr
\ 6