1 (**************************************************************************)
4 (* ||A|| A project by Andrea Asperti *)
6 (* ||I|| Developers: *)
7 (* ||T|| The HELM team. *)
8 (* ||A|| http://helm.cs.unibo.it *)
10 (* \ / This file is distributed under the terms of the *)
11 (* v GNU General Public License Version 2 *)
13 (**************************************************************************)
15 include "logic/pts.ma".
17 ninductive list (A:Type[0]) : Type[0] ≝
19 | cons: A -> list A -> list A.
21 notation "hvbox(hd break :: tl)"
22 right associative with precedence 47
25 notation "[ list0 x sep ; ]"
26 non associative with precedence 90
27 for ${fold right @'nil rec acc @{'cons $x $acc}}.
29 notation "hvbox(l1 break @ l2)"
30 right associative with precedence 47
31 for @{'append $l1 $l2 }.
33 interpretation "nil" 'nil = (nil ?).
34 interpretation "cons" 'cons hd tl = (cons ? hd tl).
36 nlet rec append A (l1: list A) l2 on l1 ≝
39 | cons hd tl ⇒ hd :: append A tl l2 ].
41 interpretation "append" 'append l1 l2 = (append ? l1 l2).
43 nlet rec id_list A (l: list A) on l ≝
46 | cons hd tl ⇒ hd :: id_list A tl ].
49 ndefinition tail ≝ λA:Type[0].λl:list A.
54 nlet rec flatten S (l : list (list S)) on l : list S ≝
55 match l with [ nil ⇒ [ ] | cons w tl ⇒ w @ flatten ? tl ].
57 ntheorem append_nil: ∀A:Type.∀l:list A.l @ [] = l.
59 #a;#l1;#IH;nnormalize;//;
62 ntheorem associative_append: ∀A:Type[0].associative (list A) (append A).
65 ##|#a;#x1;#H;nnormalize;//]
68 ntheorem cons_append_commute:
69 ∀A:Type[0].∀l1,l2:list A.∀a:A.
70 a :: (l1 @ l2) = (a :: l1) @ l2.
74 nlemma append_cons: ∀A.∀a:A.∀l,l1. l@(a::l1)=(l@[a])@l1.
75 #A;#a;#l;#l1;nrewrite > (associative_append ????);//;
78 (*ninductive permutation (A:Type) : list A -> list A -> Prop \def
79 | refl : \forall l:list A. permutation ? l l
80 | swap : \forall l:list A. \forall x,y:A.
81 permutation ? (x :: y :: l) (y :: x :: l)
82 | trans : \forall l1,l2,l3:list A.
83 permutation ? l1 l2 -> permut1 ? l2 l3 -> permutation ? l1 l3
84 with permut1 : list A -> list A -> Prop \def
85 | step : \forall l1,l2:list A. \forall x,y:A.
86 permut1 ? (l1 @ (x :: y :: l2)) (l1 @ (y :: x :: l2)).*)
90 definition x1 \def S O.
91 definition x2 \def S x1.
92 definition x3 \def S x2.
94 theorem tmp : permutation nat (x1 :: x2 :: x3 :: []) (x1 :: x3 :: x2 :: []).
95 apply (trans ? (x1 :: x2 :: x3 :: []) (x1 :: x2 :: x3 :: []) ?).
97 apply (step ? (x1::[]) [] x2 x3).
100 theorem nil_append_nil_both:
101 \forall A:Type.\forall l1,l2:list A.
102 l1 @ l2 = [] \to l1 = [] \land l2 = [].
104 theorem test_notation: [O; S O; S (S O)] = O :: S O :: S (S O) :: [].
108 theorem test_append: [O;O;O;O;O;O] = [O;O;O] @ [O;O] @ [O].
115 nlet rec nth A l d n on l ≝
118 | cons (x:A) tl ⇒ match n with
120 | S n' ⇒ nth A tl d n' ] ].
122 nlet rec map A B f l on l ≝
123 match l with [ nil ⇒ nil B | cons (x:A) tl ⇒ f x :: map A B f tl ].
125 nlet rec foldr (A,B:Type[0]) (f : A → B → B) (b:B) l on l ≝
126 match l with [ nil ⇒ b | cons (a:A) tl ⇒ f a (foldr A B f b tl) ].
128 ndefinition length ≝ λT:Type[0].λl:list T.foldr T nat (λx,c.S c) O l.
131 λT:Type[0].λl:list T.λp:T → bool.
133 (λx,l0.match (p x) with [ true => x::l0 | false => l0]) [] l.
135 ndefinition iota : nat → nat → list nat ≝
136 λn,m. nat_rect_Type0 (λ_.list ?) (nil ?) (λx,acc.cons ? (n+x) acc) m.
138 (* ### induction principle for functions visiting 2 lists in parallel *)
140 ∀T1,T2:Type[0].∀l1:list T1.∀l2:list T2.∀P:list T1 → list T2 → Prop.
141 length ? l1 = length ? l2 →
142 (P (nil ?) (nil ?)) →
143 (∀tl1,tl2,hd1,hd2. P tl1 tl2 → P (hd1::tl1) (hd2::tl2)) →
145 #T1;#T2;#l1;#l2;#P;#Hl;#Pnil;#Pcons;
146 ngeneralize in match Hl; ngeneralize in match l2;
149 nnormalize;#t2;#tl2;#H;ndestruct;
150 ##|#t1;#tl1;#IH;#l2;ncases l2
151 ##[nnormalize;#H;ndestruct
152 ##|#t2;#tl2;#H;napply Pcons;napply IH;nnormalize in H;ndestruct;//]
156 nlemma eq_map : ∀A,B,f,g,l. (∀x.f x = g x) → map A B f l = map A B g l.
158 nelim l; nnormalize;//;
161 nlemma le_length_filter : ∀A,l,p.length A (filter A l p) ≤ length A l.
162 #A;#l;#p;nelim l;nnormalize
164 ##|#a;#tl;#IH;ncases (p a);nnormalize;
170 nlemma length_append : ∀A,l,m.length A (l@m) = length A l + length A m.
173 ##|#H;#tl;#IH;nnormalize;nrewrite < IH;//]
176 ninductive in_list (A:Type): A → (list A) → Prop ≝
177 | in_list_head : ∀ x,l.(in_list A x (x::l))
178 | in_list_cons : ∀ x,y,l.(in_list A x l) → (in_list A x (y::l)).
180 ndefinition incl : \forall A.(list A) \to (list A) \to Prop \def
181 \lambda A,l,m.\forall x.in_list A x l \to in_list A x m.
183 notation "hvbox(a break ∉ b)" non associative with precedence 45
184 for @{ 'notmem $a $b }.
186 interpretation "list member" 'mem x l = (in_list ? x l).
187 interpretation "list not member" 'notmem x l = (Not (in_list ? x l)).
188 interpretation "list inclusion" 'subseteq l1 l2 = (incl ? l1 l2).
190 naxiom not_in_list_nil : \forall A,x.\lnot in_list A x [].
191 (*intros.unfold.intro.inversion H
192 [intros;lapply (sym_eq ? ? ? H2);destruct Hletin
196 naxiom in_list_cons_case : \forall A,x,a,l.in_list A x (a::l) \to
197 x = a \lor in_list A x l.
198 (*intros;inversion H;intros
199 [destruct H2;left;reflexivity
200 |destruct H4;right;assumption]
203 nlemma in_list_tail : \forall A,l,x,y.
204 in_list A x (y::l) \to x \neq y \to in_list A x l.
205 #A;#l;#x;#y;#H;#Hneq;
207 ##[#x1;#l1;#Hx;#Hl;ndestruct;nelim Hneq;#Hfalse;
209 ##|#x1;#y1;#l1;#H1;#_;#Hx;#Heq;ndestruct;//;
213 naxiom in_list_singleton_to_eq : \forall A,x,y.in_list A x [y] \to x = y.
214 (*intros;elim (in_list_cons_case ? ? ? ? H)
216 |elim (not_in_list_nil ? ? H1)]
219 naxiom in_list_to_in_list_append_l: \forall A.\forall x:A.
220 \forall l1,l2.in_list ? x l1 \to in_list ? x (l1@l2).
224 |apply in_list_cons;assumption
228 naxiom in_list_to_in_list_append_r: \forall A.\forall x:A.
229 \forall l1,l2. in_list ? x l2 \to in_list ? x (l1@l2).
233 |apply in_list_cons;apply H;assumption
237 naxiom in_list_append_to_or_in_list: \forall A:Type.\forall x:A.
238 \forall l,l1. in_list ? x (l@l1) \to in_list ? x l \lor in_list ? x l1.
242 |simplify in H1.inversion H1;intros; destruct;
243 [left.apply in_list_head
245 [left.apply in_list_cons. assumption
253 nlet rec mem (A:Type) (eq: A → A → bool) x (l: list A) on l ≝
259 | false ⇒ mem A eq x l'
263 naxiom mem_true_to_in_list :
265 (\forall x,y.equ x y = true \to x = y) \to
266 \forall x,l.mem A equ x l = true \to in_list A x l.
268 [simplify in H1;destruct H1
269 |simplify in H2;apply (bool_elim ? (equ x a))
270 [intro;rewrite > (H ? ? H3);apply in_list_head
271 |intro;rewrite > H3 in H2;simplify in H2;
272 apply in_list_cons;apply H1;assumption]]
275 naxiom in_list_to_mem_true :
277 (\forall x.equ x x = true) \to
278 \forall x,l.in_list A x l \to mem A equ x l = true.
280 [elim (not_in_list_nil ? ? H1)
282 [simplify;rewrite > H;reflexivity
283 |simplify;rewrite > H4;apply (bool_elim ? (equ a1 a2));intro;reflexivity]].
286 naxiom in_list_filter_to_p_true : \forall A,l,x,p.
287 in_list A x (filter A l p) \to p x = true.
289 [simplify in H;elim (not_in_list_nil ? ? H)
290 |simplify in H1;apply (bool_elim ? (p a));intro;rewrite > H2 in H1;
292 [elim (in_list_cons_case ? ? ? ? H1)
293 [rewrite > H3;assumption
298 naxiom in_list_filter : \forall A,l,p,x.in_list A x (filter A l p) \to in_list A x l.
300 [simplify in H;elim (not_in_list_nil ? ? H)
301 |simplify in H1;apply (bool_elim ? (p a));intro;rewrite > H2 in H1;
303 [elim (in_list_cons_case ? ? ? ? H1)
304 [rewrite > H3;apply in_list_head
305 |apply in_list_cons;apply H;assumption]
306 |apply in_list_cons;apply H;assumption]]
309 naxiom in_list_filter_r : \forall A,l,p,x.
310 in_list A x l \to p x = true \to in_list A x (filter A l p).
312 [elim (not_in_list_nil ? ? H)
313 |elim (in_list_cons_case ? ? ? ? H1)
314 [rewrite < H3;simplify;rewrite > H2;simplify;apply in_list_head
315 |simplify;apply (bool_elim ? (p a));intro;simplify;
316 [apply in_list_cons;apply H;assumption
317 |apply H;assumption]]]
320 naxiom incl_A_A: ∀T,A.incl T A A.
321 (*intros.unfold incl.intros.assumption.
324 naxiom incl_append_l : ∀T,A,B.incl T A (A @ B).
325 (*unfold incl; intros;autobatch.
328 naxiom incl_append_r : ∀T,A,B.incl T B (A @ B).
329 (*unfold incl; intros;autobatch.
332 naxiom incl_cons : ∀T,A,B,x.incl T A B → incl T (x::A) (x::B).
333 (*unfold incl; intros;elim (in_list_cons_case ? ? ? ? H1);autobatch.
336 nlet rec foldl (A,B:Type[0]) (f:A → B → A) (a:A) (l:list B) on l ≝
339 | cons b bl ⇒ foldl A B f (f a b) bl ].
341 nlet rec foldl2 (A,B,C:Type[0]) (f:A → B → C → A) (a:A) (bl:list B) (cl:list C) on bl ≝
344 | cons b0 bl0 ⇒ match cl with
346 | cons c0 cl0 ⇒ foldl2 A B C f (f a b0 c0) bl0 cl0 ] ].
348 nlet rec foldr2 (A,B : Type[0]) (X : Type[0]) (f: A → B → X → X) (x:X)
349 (al : list A) (bl : list B) on al : X ≝
352 | cons a al1 ⇒ match bl with
354 | cons b bl1 ⇒ f a b (foldr2 ??? f x al1 bl1) ] ].
356 nlet rec rev (A:Type[0]) (l:list A) on l ≝
359 | cons hd tl ⇒ (rev A tl)@[hd] ].
361 notation > "hvbox(a break \liff b)"
362 left associative with precedence 25
365 notation "hvbox(a break \leftrightarrow b)"
366 left associative with precedence 25
369 interpretation "logical iff" 'iff x y = (iff x y).
371 ndefinition coincl : ∀A.list A → list A → Prop ≝ λA,l1,l2.∀x.x ∈ l1 ↔ x ∈ l2.
373 notation > "hvbox(a break ≡ b)"
374 non associative with precedence 45
377 notation < "hvbox(term 46 a break ≡ term 46 b)"
378 non associative with precedence 45
381 interpretation "list coinclusion" 'equiv x y = (coincl ? x y).
383 nlemma refl_coincl : ∀A.∀l:list A.l ≡ l.
387 nlemma coincl_rev : ∀A.∀l:list A.l ≡ rev ? l.
389 ##[##1,3:#H;napply False_ind;ncases (not_in_list_nil ? x);
391 ##|#a l0 IH H;ncases (in_list_cons_case ???? H);#H1
392 ##[napply in_list_to_in_list_append_r;nrewrite > H1;@
393 ##|napply in_list_to_in_list_append_l;/2/
395 ##|#a l0 IH H;ncases (in_list_append_to_or_in_list ???? H);#H1
397 ##|nrewrite > (in_list_singleton_to_eq ??? H1);@
402 nlemma not_in_list_nil_r : ∀A.∀l:list A.l = [] → ∀x.x ∉ l.
404 ##[#;napply not_in_list_nil
405 ##|#a l0 IH Hfalse;ndestruct (Hfalse)
409 nlemma eq_filter_append :
410 ∀A,p,l1,l2.filter A (l1@l2) p = filter A l1 p@filter A l2 p.
413 ##|#a0 l0 IH;nwhd in ⊢ (??%(??%?));ncases (p a0)
414 ##[nwhd in ⊢ (??%%);nrewrite < IH;@
415 ##|nwhd in ⊢ (??%(??%?));nrewrite < IH;@
421 ∀A,B:Type[0].∀f:A→B.∀P:B → Prop.
422 ∀al.(∀a.a ∈ al → P (f a)) →
423 ∀b. b ∈ map ?? f al → P b.
425 ##[#H1 b Hfalse;napply False_ind;
426 ncases (not_in_list_nil ? b);#H2;napply H2;napply Hfalse;
427 ##|#a1 al1 IH H1 b Hin;nwhd in Hin:(???%);ncases (in_list_cons_case ???? Hin);
428 ##[#e;nrewrite > e;napply H1;@
430 ##[#a2 Hin2;napply H1;@2;//;
438 ∀A,B,C,f,g,l.map B C f (map A B g l) = map A C (λx.f (g x)) l.
441 ##|#a0 al0 IH;nchange in ⊢ (??%%) with (cons ???);
446 naxiom incl_incl_to_incl_append :
447 ∀A.∀l1,l2,l1',l2':list A.l1 ⊆ l1' → l2 ⊆ l2' → l1@l2 ⊆ l1'@l2'.
449 naxiom eq_map_append : ∀A,B,f,l1,l2.map A B f (l1@l2) = map A B f l1@map A B f l2.
451 naxiom not_in_list_to_mem_false :
453 (∀x,y.equ x y = true → x = y) →
454 ∀x:A.∀l. x ∉ l → mem A equ x l = false.
456 nlet rec list_forall (A:Type[0]) (l:list A) (p:A → bool) on l : bool ≝
459 | cons a al ⇒ p a ∧ list_forall A al p ].
462 ∀A,B,f,g,xl.(∀x.x ∈ xl → f x = g x) → map A B f xl = map A B g xl.
465 ##|#a al IH H1;nwhd in ⊢ (??%%);napply eq_f2
467 ##|napply IH;#x Hx;napply H1;@2;//
472 nlemma x_in_map_to_eq :
473 ∀A,B,f,x,l.x ∈ map A B f l → ∃x'.x = f x' ∧ x' ∈ l.
475 ##[#H;ncases (not_in_list_nil ? x);#H1;napply False_ind;napply (H1 H)
476 ##|#a l0 IH H;ncases (in_list_cons_case ???? H);#H1
477 ##[nrewrite > H1;@ a;@;@
478 ##|ncases (IH H1);#a0;*;#H2 H3;@a0;@
484 nlemma list_forall_false :
485 ∀A:Type[0].∀x,xl,p. p x = false → x ∈ xl → list_forall A xl p = false.
486 #A x xl p H1;nelim xl
487 ##[#Hfalse;napply False_ind;ncases (not_in_list_nil ? x);#H2;napply (H2 Hfalse)
488 ##|#x0 xl0 IH H2;ncases (in_list_cons_case ???? H2);#H3
489 ##[nwhd in ⊢ (??%?);nrewrite < H3;nrewrite > H1;@
490 ##|nwhd in ⊢ (??%?);ncases (p x0)
491 ##[nrewrite > (IH H3);@
498 nlemma list_forall_true :
499 ∀A:Type[0].∀xl,p. (∀x.x ∈ xl → p x = true) → list_forall A xl p = true.
502 ##|#x0 xl0 IH H1;nwhd in ⊢ (??%?);nrewrite > (H1 …)
503 ##[napply IH;#x Hx;napply H1;@2;//