]> matita.cs.unibo.it Git - helm.git/blob - helm/software/matita/library/list/list.ma
Preparing for 0.5.9 release.
[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 47
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 = (nil ?).
38 interpretation "cons" 'cons hd tl = (cons ? hd tl).
39
40 (* theorem test_notation: [O; S O; S (S O)] = O :: S O :: S (S O) :: []. *)
41
42 theorem nil_cons:
43   \forall A:Type.\forall l:list A.\forall a:A. a::l ≠ [].
44   intros;
45   unfold Not;
46   intros;
47   destruct H.
48 qed.
49
50 let rec id_list A (l: list A) on l :=
51   match l with
52   [ nil => []
53   | (cons hd tl) => hd :: id_list A tl ].
54
55 let rec append A (l1: list A) l2 on l1 :=
56   match l1 with
57   [ nil => l2
58   | (cons hd tl) => hd :: append A tl l2 ].
59
60 definition tail := \lambda A:Type. \lambda l: list A.
61   match l with
62   [ nil => []
63   | (cons hd tl) => tl].
64
65 interpretation "append" 'append l1 l2 = (append ? l1 l2).
66
67 theorem append_nil: \forall A:Type.\forall l:list A.l @ [] = l.
68   intros;
69   elim l;
70   [ reflexivity;
71   | simplify;
72     rewrite > H;
73     reflexivity;
74   ]
75 qed.
76
77 theorem associative_append: \forall A:Type.associative (list A) (append A).
78   intros; unfold; intros;
79   elim x;
80   [ simplify;
81     reflexivity;
82   | simplify;
83     rewrite > H;
84     reflexivity;
85   ]
86 qed.
87
88 theorem cons_append_commute:
89   \forall A:Type.\forall l1,l2:list A.\forall a:A.
90     a :: (l1 @ l2) = (a :: l1) @ l2.
91   intros;
92   reflexivity;
93 qed.
94
95 lemma append_cons:\forall A.\forall a:A.\forall l,l1. 
96 l@(a::l1)=(l@[a])@l1.
97 intros.
98 rewrite > associative_append.
99 reflexivity.
100 qed.
101
102 inductive permutation (A:Type) : list A -> list A -> Prop \def
103   | refl : \forall l:list A. permutation ? l l
104   | swap : \forall l:list A. \forall x,y:A. 
105               permutation ? (x :: y :: l) (y :: x :: l)
106   | trans : \forall l1,l2,l3:list A.
107               permutation ? l1 l2 -> permut1 ? l2 l3 -> permutation ? l1 l3
108 with permut1 : list A -> list A -> Prop \def
109   | step : \forall l1,l2:list A. \forall x,y:A. 
110       permut1 ? (l1 @ (x :: y :: l2)) (l1 @ (y :: x :: l2)).
111
112 (*
113
114 definition x1 \def S O.
115 definition x2 \def S x1.
116 definition x3 \def S x2.
117    
118 theorem tmp : permutation nat (x1 :: x2 :: x3 :: []) (x1 :: x3 :: x2 :: []).
119   apply (trans ? (x1 :: x2 :: x3 :: []) (x1 :: x2 :: x3 :: []) ?).
120   apply refl.
121   apply (step ? (x1::[]) [] x2 x3).
122   qed. 
123
124 theorem nil_append_nil_both:
125   \forall A:Type.\forall l1,l2:list A.
126     l1 @ l2 = [] \to l1 = [] \land l2 = [].
127
128 theorem test_notation: [O; S O; S (S O)] = O :: S O :: S (S O) :: []. 
129 reflexivity.
130 qed.
131
132 theorem test_append: [O;O;O;O;O;O] = [O;O;O] @ [O;O] @ [O].
133 simplify.
134 reflexivity.
135 qed.
136
137 *)
138
139 definition nth ≝
140   λA:Type.
141     let rec nth l d n on n ≝
142       match n with
143       [ O ⇒
144          match l with
145          [ nil ⇒ d
146          | cons (x : A) _ ⇒ x
147          ]
148       | S n' ⇒ nth (tail ? l) d n']
149     in nth.
150   
151 definition map ≝
152   λA,B:Type.λf:A→B.
153   let rec map (l : list A) on l : list B ≝
154     match l with [ nil ⇒ nil ? | cons x tl ⇒ f x :: (map tl)]
155   in map.
156   
157 definition foldr ≝
158   λA,B:Type.λf:A→B→B.λb:B.
159   let rec foldr (l : list A) on l : B := 
160     match l with [ nil ⇒ b | (cons a l) ⇒ f a (foldr l)]
161   in foldr.
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 elim l1 in Hl l2 ⊢ % 1 (l2 x1); [ cases l2; intros (Hl); [assumption| simplify in Hl; destruct Hl]]
182 intros 3 (tl1 IH l2); cases l2; [1: simplify; intros 1 (Hl); destruct Hl] 
183 intros 1 (Hl); apply Pcons; apply IH; simplify in Hl; destruct Hl; assumption;
184 qed.
185
186 lemma eq_map : ∀A,B,f,g,l. (∀x.f x = g x) → map A B f l = map A B g l.
187 intros (A B f g l Efg); elim l; simplify; [1: reflexivity ];
188 rewrite > (Efg a); rewrite > H; reflexivity;  
189 qed.
190
191 lemma le_length_filter : \forall A,l,p.length A (filter A l p) \leq length A l.
192 intros;elim l
193   [simplify;apply le_n
194   |simplify;apply (bool_elim ? (p a));intro
195      [simplify;apply le_S_S;assumption
196      |simplify;apply le_S;assumption]]
197 qed.
198
199 lemma length_append : ∀A,l,m.length A (l@m) = length A l + length A m.
200 intros;elim l
201 [reflexivity
202 |simplify;rewrite < H;reflexivity]
203 qed.