]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/software/matita/library/list/list.ma
more work on q
[helm.git] / helm / software / matita / library / list / list.ma
index f214ff3efa11b6b2559bfa302f2d5dfb4174495d..e4787fe8422fcf55a0888cc9da3b4d237239ff0c 100644 (file)
 (*                                                                        *)
 (**************************************************************************)
 
-set "baseuri" "cic:/matita/list/".
 include "logic/equality.ma".
+include "datatypes/bool.ma".
 include "higher_order_defs/functions.ma".
+include "nat/plus.ma".
+include "nat/orders.ma".
 
 inductive list (A:Type) : Type :=
   | nil: list A
   | cons: A -> list A -> list A.
 
 notation "hvbox(hd break :: tl)"
-  right associative with precedence 46
+  right associative with precedence 47
   for @{'cons $hd $tl}.
 
 notation "[ list0 x sep ; ]"
@@ -32,15 +34,14 @@ notation "hvbox(l1 break @ l2)"
   right associative with precedence 47
   for @{'append $l1 $l2 }.
 
-interpretation "nil" 'nil = (cic:/matita/list/list.ind#xpointer(1/1/1) _).
+interpretation "nil" 'nil = (cic:/matita/list/list/list.ind#xpointer(1/1/1) _).
 interpretation "cons" 'cons hd tl =
-  (cic:/matita/list/list.ind#xpointer(1/1/2) _ hd tl).
+  (cic:/matita/list/list/list.ind#xpointer(1/1/2) _ hd tl).
 
 (* theorem test_notation: [O; S O; S (S O)] = O :: S O :: S (S O) :: []. *)
 
 theorem nil_cons:
-  \forall A:Type.\forall l:list A.\forall a:A.
-    a::l <> [].
+  \forall A:Type.\forall l:list A.\forall a:A. a::l ≠ [].
   intros;
   unfold Not;
   intros;
@@ -62,7 +63,7 @@ definition tail := \lambda A:Type. \lambda l: list A.
   [ nil => []
   | (cons hd tl) => tl].
 
-interpretation "append" 'append l1 l2 = (cic:/matita/list/append.con _ l1 l2).
+interpretation "append" 'append l1 l2 = (cic:/matita/list/list/append.con _ l1 l2).
 
 theorem append_nil: \forall A:Type.\forall l:list A.l @ [] = l.
   intros;
@@ -92,6 +93,13 @@ theorem cons_append_commute:
   reflexivity;
 qed.
 
+lemma append_cons:\forall A.\forall a:A.\forall l,l1. 
+l@(a::l1)=(l@[a])@l1.
+intros.
+rewrite > associative_append.
+reflexivity.
+qed.
+
 inductive permutation (A:Type) : list A -> list A -> Prop \def
   | refl : \forall l:list A. permutation ? l l
   | swap : \forall l:list A. \forall x,y:A. 
@@ -133,3 +141,63 @@ simplify.
 reflexivity.
 qed.
 *)
+
+definition nth ≝
+  λA:Type.
+    let rec nth l d n on n ≝
+      match n with
+      [ O ⇒
+         match l with
+         [ nil ⇒ d
+         | cons (x : A) _ ⇒ x
+         ]
+      | S n' ⇒ nth (tail ? l) d n']
+    in nth.
+  
+definition map ≝
+  λA,B:Type.λf:A→B.
+  let rec map (l : list A) on l : list B ≝
+    match l with [ nil ⇒ nil ? | cons x tl ⇒ f x :: (map tl)]
+  in map.
+  
+definition foldr ≝
+  λA,B:Type.λf:A→B→B.λb:B.
+  let rec foldr (l : list A) on l : B := 
+    match l with [ nil ⇒ b | (cons a l) ⇒ f a (foldr l)]
+  in foldr.
+   
+definition length ≝ λT:Type.λl:list T.foldr T nat (λx,c.S c) O l.
+
+definition filter \def 
+  \lambda T:Type.\lambda l:list T.\lambda p:T \to bool.
+  foldr T (list T) 
+    (\lambda x,l0.match (p x) with [ true => x::l0 | false => l0]) [] l.
+
+definition iota : nat → nat → list nat ≝
+  λn,m. nat_rect (λ_.list ?) (nil ?) (λx,acc.cons ? (n+x) acc) m.
+  
+(* ### induction principle for functions visiting 2 lists in parallel *)
+lemma list_ind2 : 
+  ∀T1,T2:Type.∀l1:list T1.∀l2:list T2.∀P:list T1 → list T2 → Prop.
+  length ? l1 = length ? l2 →
+  (P (nil ?) (nil ?)) → 
+  (∀tl1,tl2,hd1,hd2. P tl1 tl2 → P (hd1::tl1) (hd2::tl2)) → 
+  P l1 l2.
+intros (T1 T2 l1 l2 P Hl Pnil Pcons);
+elim l1 in Hl l2 ⊢ % 1 (l2 x1); [ cases l2; intros (Hl); [assumption| simplify in Hl; destruct Hl]]
+intros 3 (tl1 IH l2); cases l2; [1: simplify; intros 1 (Hl); destruct Hl] 
+intros 1 (Hl); apply Pcons; apply IH; simplify in Hl; destruct Hl; assumption;
+qed.
+
+lemma eq_map : ∀A,B,f,g,l. (∀x.f x = g x) → map A B f l = map A B g l.
+intros (A B f g l Efg); elim l; simplify; [1: reflexivity ];
+rewrite > (Efg a); rewrite > H; reflexivity;  
+qed.
+
+lemma le_length_filter : \forall A,l,p.length A (filter A l p) \leq length A l.
+intros;elim l
+  [simplify;apply le_n
+  |simplify;apply (bool_elim ? (p a));intro
+     [simplify;apply le_S_S;assumption
+     |simplify;apply le_S;assumption]]
+qed.