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