]> matita.cs.unibo.it Git - helm.git/blob - matita/library/list/list.ma
b7a38ed991a1e60439396fe5ca6121aad9800ec8
[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
16 include "logic/equality.ma".
17 include "higher_order_defs/functions.ma".
18
19 inductive list (A:Type) : Type :=
20   | nil: list A
21   | cons: A -> list A -> list A.
22
23 notation "hvbox(hd break :: tl)"
24   right associative with precedence 46
25   for @{'cons $hd $tl}.
26
27 notation "[ list0 x sep ; ]"
28   non associative with precedence 90
29   for ${fold right @'nil rec acc @{'cons $x $acc}}.
30
31 notation "hvbox(l1 break @ l2)"
32   right associative with precedence 47
33   for @{'append $l1 $l2 }.
34
35 interpretation "nil" 'nil = (cic:/matita/list/list.ind#xpointer(1/1/1) _).
36 interpretation "cons" 'cons hd tl =
37   (cic:/matita/list/list.ind#xpointer(1/1/2) _ hd tl).
38
39 (* theorem test_notation: [O; S O; S (S O)] = O :: S O :: S (S O) :: []. *)
40
41 theorem nil_cons:
42   \forall A:Type.\forall l:list A.\forall a:A.
43     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 = (cic:/matita/list/append.con _ 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 inductive permutation (A:Type) : list A -> list A -> Prop \def
96   | refl : \forall l:list A. permutation ? l l
97   | swap : \forall l:list A. \forall x,y:A. 
98               permutation ? (x :: y :: l) (y :: x :: l)
99   | trans : \forall l1,l2,l3:list A.
100               permutation ? l1 l2 -> permut1 ? l2 l3 -> permutation ? l1 l3
101 with permut1 : list A -> list A -> Prop \def
102   | step : \forall l1,l2:list A. \forall x,y:A. 
103       permut1 ? (l1 @ (x :: y :: l2)) (l1 @ (y :: x :: l2)).
104
105 include "nat/nat.ma".  
106    
107 definition x1 \def S O.
108 definition x2 \def S x1.
109 definition x3 \def S x2.
110    
111 theorem tmp : permutation nat (x1 :: x2 :: x3 :: []) (x1 :: x3 :: x2 :: []).
112   apply (trans ? (x1 :: x2 :: x3 :: []) (x1 :: x2 :: x3 :: []) ?).
113   apply refl.
114   apply (step ? (x1::[]) [] x2 x3).
115   qed. 
116
117
118 (*
119 theorem nil_append_nil_both:
120   \forall A:Type.\forall l1,l2:list A.
121     l1 @ l2 = [] \to l1 = [] \land l2 = [].
122 *)
123
124 (*
125 include "nat/nat.ma".
126
127 theorem test_notation: [O; S O; S (S O)] = O :: S O :: S (S O) :: []. 
128 reflexivity.
129 qed.
130
131 theorem test_append: [O;O;O;O;O;O] = [O;O;O] @ [O;O] @ [O].
132 simplify.
133 reflexivity.
134 qed.
135 *)
136
137 let rec nth (A:Type) l d n on n ≝
138  match n with
139   [ O ⇒
140      match l with
141       [ nil ⇒ d
142       | cons (x : A) _ ⇒ x
143       ]
144   | S n' ⇒ nth A (tail ? l) d n'
145   ].