]> matita.cs.unibo.it Git - helm.git/blob - matita/library/list/list.ma
tagged 0.5.0-rc1
[helm.git] / matita / library / list / list.ma
1 (**************************************************************************)
2 (*       ___                                                              *)
3 (*      ||M||                                                             *)
4 (*      ||A||       A project by Andrea Asperti                           *)
5 (*      ||T||                                                             *)
6 (*      ||I||       Developers:                                           *)
7 (*      ||T||         The HELM team.                                      *)
8 (*      ||A||         http://helm.cs.unibo.it                             *)
9 (*      \   /                                                             *)
10 (*       \ /        This file is distributed under the terms of the       *)
11 (*        v         GNU General Public License Version 2                  *)
12 (*                                                                        *)
13 (**************************************************************************)
14
15 set "baseuri" "cic:/matita/list/".
16 include "logic/equality.ma".
17 include "datatypes/bool.ma".
18 include "higher_order_defs/functions.ma".
19 include "nat/plus.ma".
20 include "nat/orders.ma".
21
22 inductive list (A:Type) : Type :=
23   | nil: list A
24   | cons: A -> list A -> list A.
25
26 notation "hvbox(hd break :: tl)"
27   right associative with precedence 46
28   for @{'cons $hd $tl}.
29
30 notation "[ list0 x sep ; ]"
31   non associative with precedence 90
32   for ${fold right @'nil rec acc @{'cons $x $acc}}.
33
34 notation "hvbox(l1 break @ l2)"
35   right associative with precedence 47
36   for @{'append $l1 $l2 }.
37
38 interpretation "nil" 'nil = (cic:/matita/list/list/list.ind#xpointer(1/1/1) _).
39 interpretation "cons" 'cons hd tl =
40   (cic:/matita/list/list/list.ind#xpointer(1/1/2) _ hd tl).
41
42 (* theorem test_notation: [O; S O; S (S O)] = O :: S O :: S (S O) :: []. *)
43
44 theorem nil_cons:
45   \forall A:Type.\forall l:list A.\forall a:A.
46     a::l <> [].
47   intros;
48   unfold Not;
49   intros;
50   destruct H.
51 qed.
52
53 let rec id_list A (l: list A) on l :=
54   match l with
55   [ nil => []
56   | (cons hd tl) => hd :: id_list A tl ].
57
58 let rec append A (l1: list A) l2 on l1 :=
59   match l1 with
60   [ nil => l2
61   | (cons hd tl) => hd :: append A tl l2 ].
62
63 definition tail := \lambda A:Type. \lambda l: list A.
64   match l with
65   [ nil => []
66   | (cons hd tl) => tl].
67
68 interpretation "append" 'append l1 l2 = (cic:/matita/list/list/append.con _ l1 l2).
69
70 theorem append_nil: \forall A:Type.\forall l:list A.l @ [] = l.
71   intros;
72   elim l;
73   [ reflexivity;
74   | simplify;
75     rewrite > H;
76     reflexivity;
77   ]
78 qed.
79
80 theorem associative_append: \forall A:Type.associative (list A) (append A).
81   intros; unfold; intros;
82   elim x;
83   [ simplify;
84     reflexivity;
85   | simplify;
86     rewrite > H;
87     reflexivity;
88   ]
89 qed.
90
91 theorem cons_append_commute:
92   \forall A:Type.\forall l1,l2:list A.\forall a:A.
93     a :: (l1 @ l2) = (a :: l1) @ l2.
94   intros;
95   reflexivity;
96 qed.
97
98 lemma append_cons:\forall A.\forall a:A.\forall l,l1. 
99 l@(a::l1)=(l@[a])@l1.
100 intros.
101 rewrite > associative_append.
102 reflexivity.
103 qed.
104
105 inductive permutation (A:Type) : list A -> list A -> Prop \def
106   | refl : \forall l:list A. permutation ? l l
107   | swap : \forall l:list A. \forall x,y:A. 
108               permutation ? (x :: y :: l) (y :: x :: l)
109   | trans : \forall l1,l2,l3:list A.
110               permutation ? l1 l2 -> permut1 ? l2 l3 -> permutation ? l1 l3
111 with permut1 : list A -> list A -> Prop \def
112   | step : \forall l1,l2:list A. \forall x,y:A. 
113       permut1 ? (l1 @ (x :: y :: l2)) (l1 @ (y :: x :: l2)).
114
115 include "nat/nat.ma".  
116    
117 definition x1 \def S O.
118 definition x2 \def S x1.
119 definition x3 \def S x2.
120    
121 theorem tmp : permutation nat (x1 :: x2 :: x3 :: []) (x1 :: x3 :: x2 :: []).
122   apply (trans ? (x1 :: x2 :: x3 :: []) (x1 :: x2 :: x3 :: []) ?).
123   apply refl.
124   apply (step ? (x1::[]) [] x2 x3).
125   qed. 
126
127
128 (*
129 theorem nil_append_nil_both:
130   \forall A:Type.\forall l1,l2:list A.
131     l1 @ l2 = [] \to l1 = [] \land l2 = [].
132 *)
133
134 (*
135 include "nat/nat.ma".
136
137 theorem test_notation: [O; S O; S (S O)] = O :: S O :: S (S O) :: []. 
138 reflexivity.
139 qed.
140
141 theorem test_append: [O;O;O;O;O;O] = [O;O;O] @ [O;O] @ [O].
142 simplify.
143 reflexivity.
144 qed.
145 *)
146
147 let rec nth (A:Type) l d n on n ≝
148  match n with
149   [ O ⇒
150      match l with
151       [ nil ⇒ d
152       | cons (x : A) _ ⇒ x
153       ]
154   | S n' ⇒ nth A (tail ? l) d n'
155   ].
156   
157 let rec map (A,B:Type) (f: A → B) (l : list A) on l : list B ≝
158   match l with [ nil ⇒ nil ? | cons x tl ⇒ f x :: (map A B f tl)].
159   
160 let rec foldr (A,B:Type) (f : A → B → B) (b : B) (l : list A) on l : B := 
161   match l with [ nil ⇒ b | (cons a l) ⇒ f a (foldr ? ? f b l)].
162    
163 definition length ≝ λT:Type.λl:list T.foldr T nat (λx,c.S c) O l.
164
165 definition filter \def 
166   \lambda T:Type.\lambda l:list T.\lambda p:T \to bool.
167   foldr T (list T) 
168     (\lambda x,l0.match (p x) with [ true => x::l0 | false => l0]) [] l.
169
170 definition iota : nat → nat → list nat ≝
171   λn,m. nat_rect (λ_.list ?) (nil ?) (λx,acc.cons ? (n+x) acc) m.
172   
173 (* ### induction principle for functions visiting 2 lists in parallel *)
174 lemma list_ind2 : 
175   ∀T1,T2:Type.∀l1:list T1.∀l2:list T2.∀P:list T1 → list T2 → Prop.
176   length ? l1 = length ? l2 →
177   (P (nil ?) (nil ?)) → 
178   (∀tl1,tl2,hd1,hd2. P tl1 tl2 → P (hd1::tl1) (hd2::tl2)) → 
179   P l1 l2.
180 intros (T1 T2 l1 l2 P Hl Pnil Pcons);
181 generalize in match Hl; clear Hl; generalize in match l2; clear l2;
182 elim l1 1 (l2 x1); [ cases l2; intros (Hl); [assumption| simplify in Hl; destruct Hl]]
183 intros 3 (tl1 IH l2); cases l2; [1: simplify; intros 1 (Hl); destruct Hl] 
184 intros 1 (Hl); apply Pcons; apply IH; simplify in Hl; destruct Hl; assumption;
185 qed.
186
187 lemma eq_map : ∀A,B,f,g,l. (∀x.f x = g x) → map A B f l = map A B g l.
188 intros (A B f g l Efg); elim l; simplify; [1: reflexivity ];
189 rewrite > (Efg t); rewrite > H; reflexivity;  
190 qed.
191
192 lemma le_length_filter : \forall A,l,p.length A (filter A l p) \leq length A l.
193 intros;elim l
194   [simplify;apply le_n
195   |simplify;apply (bool_elim ? (p t));intro
196      [simplify;apply le_S_S;assumption
197      |simplify;apply le_S;assumption]]
198 qed.