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