2 ||M|| This file is part of HELM, an Hypertextual, Electronic
3 ||A|| Library of Mathematics, developed at the Computer Science
4 ||T|| Department of the University of Bologna, Italy.
8 \ / This file is distributed under the terms of the
9 \ / GNU General Public License Version 2
10 V_______________________________________________________________ *)
12 include "basics/types.ma".
13 include "arithmetics/nat.ma".
15 inductive list (A:Type[0]) : Type[0] :=
17 | cons: A -> list A -> list A.
19 notation "hvbox(hd break :: tl)"
20 right associative with precedence 47
23 notation "[ list0 x sep ; ]"
24 non associative with precedence 90
25 for ${fold right @'nil rec acc @{'cons $x $acc}}.
27 notation "hvbox(l1 break @ l2)"
28 right associative with precedence 47
29 for @{'append $l1 $l2 }.
31 interpretation "nil" 'nil = (nil ?).
32 interpretation "cons" 'cons hd tl = (cons ? hd tl).
34 definition not_nil: ∀A:Type[0].list A → Prop ≝
35 λA.λl.match l with [ nil ⇒ True | cons hd tl ⇒ False ].
38 ∀A:Type[0].∀l:list A.∀a:A. a::l ≠ [].
39 #A #l #a @nmk #Heq (change with (not_nil ? (a::l))) >Heq //
43 let rec id_list A (l: list A) on l :=
46 | (cons hd tl) => hd :: id_list A tl ]. *)
48 let rec append A (l1: list A) l2 on l1 ≝
51 | cons hd tl ⇒ hd :: append A tl l2 ].
53 definition hd ≝ λA.λl: list A.λd:A.
54 match l with [ nil ⇒ d | cons a _ ⇒ a].
56 definition tail ≝ λA.λl: list A.
57 match l with [ nil ⇒ [] | cons hd tl ⇒ tl].
59 interpretation "append" 'append l1 l2 = (append ? l1 l2).
61 theorem append_nil: ∀A.∀l:list A.l @ [] = l.
62 #A #l (elim l) normalize // qed.
64 theorem associative_append:
65 ∀A.associative (list A) (append A).
66 #A #l1 #l2 #l3 (elim l1) normalize // qed.
69 ntheorem cons_append_commute:
70 ∀A:Type.∀l1,l2:list A.∀a:A.
71 a :: (l1 @ l2) = (a :: l1) @ l2.
74 theorem append_cons:∀A.∀a:A.∀l,l1.l@(a::l1)=(l@[a])@l1.
75 #A #a #l #l1 >associative_append // qed.
77 theorem nil_append_elim: ∀A.∀l1,l2: list A.∀P:?→?→Prop.
78 l1@l2=[] → P (nil A) (nil A) → P l1 l2.
79 #A #l1 #l2 #P (cases l1) normalize //
83 theorem nil_to_nil: ∀A.∀l1,l2:list A.
84 l1@l2 = [] → l1 = [] ∧ l2 = [].
85 #A #l1 #l2 #isnil @(nil_append_elim A l1 l2) /2/
90 let rec map (A,B:Type[0]) (f: A → B) (l:list A) on l: list B ≝
91 match l with [ nil ⇒ nil ? | cons x tl ⇒ f x :: (map A B f tl)].
93 let rec foldr (A,B:Type[0]) (f:A → B → B) (b:B) (l:list A) on l :B ≝
94 match l with [ nil ⇒ b | cons a l ⇒ f a (foldr A B f b l)].
98 foldr T (list T) (λx,l0.if p x then x::l0 else l0) (nil T).
100 (* compose f [a1;...;an] [b1;...;bm] =
101 [f a1 b1; ... ;f an b1; ... ;f a1 bm; f an bm] *)
103 definition compose ≝ λA,B,C.λf:A→B→C.λl1,l2.
104 foldr ?? (λi,acc.(map ?? (f i) l2)@acc) [ ] l1.
106 lemma filter_true : ∀A,l,a,p. p a = true →
107 filter A p (a::l) = a :: filter A p l.
108 #A #l #a #p #pa (elim l) normalize >pa normalize // qed.
110 lemma filter_false : ∀A,l,a,p. p a = false →
111 filter A p (a::l) = filter A p l.
112 #A #l #a #p #pa (elim l) normalize >pa normalize // qed.
114 theorem eq_map : ∀A,B,f,g,l. (∀x.f x = g x) → map A B f l = map A B g l.
115 #A #B #f #g #l #eqfg (elim l) normalize // qed.
117 let rec dprodl (A:Type[0]) (f:A→Type[0]) (l1:list A) (g:(∀a:A.list (f a))) on l1 ≝
120 | cons a tl ⇒ (map ??(mk_Sig ?? a) (g a)) @ dprodl A f tl g
123 (**************************** length ******************************)
125 let rec length (A:Type[0]) (l:list A) on l ≝
128 | cons a tl ⇒ S (length A tl)].
130 notation "|M|" non associative with precedence 60 for @{'norm $M}.
131 interpretation "norm" 'norm l = (length ? l).
133 lemma length_append: ∀A.∀l1,l2:list A.
135 #A #l1 elim l1 // normalize /2/
138 (****************************** nth ********************************)
139 let rec nth n (A:Type[0]) (l:list A) (d:A) ≝
142 |S m ⇒ nth m A (tail A l) d].
144 lemma nth_nil: ∀A,a,i. nth i A ([]) a = a.
145 #A #a #i elim i normalize //
148 (**************************** fold *******************************)
150 let rec fold (A,B:Type[0]) (op:B → B → B) (b:B) (p:A→bool) (f:A→B) (l:list A) on l :B ≝
154 if p a then op (f a) (fold A B op b p f l)
155 else fold A B op b p f l].
157 notation "\fold [ op , nil ]_{ ident i ∈ l | p} f"
159 for @{'fold $op $nil (λ${ident i}. $p) (λ${ident i}. $f) $l}.
161 notation "\fold [ op , nil ]_{ident i ∈ l } f"
163 for @{'fold $op $nil (λ${ident i}.true) (λ${ident i}. $f) $l}.
165 interpretation "\fold" 'fold op nil p f l = (fold ? ? op nil p f l).
168 ∀A,B.∀a:A.∀l.∀p.∀op:B→B→B.∀nil.∀f:A→B. p a = true →
169 \fold[op,nil]_{i ∈ a::l| p i} (f i) =
170 op (f a) \fold[op,nil]_{i ∈ l| p i} (f i).
171 #A #B #a #l #p #op #nil #f #pa normalize >pa // qed.
174 ∀A,B.∀a:A.∀l.∀p.∀op:B→B→B.∀nil.∀f.
175 p a = false → \fold[op,nil]_{i ∈ a::l| p i} (f i) =
176 \fold[op,nil]_{i ∈ l| p i} (f i).
177 #A #B #a #l #p #op #nil #f #pa normalize >pa // qed.
180 ∀A,B.∀a:A.∀l.∀p.∀op:B→B→B.∀nil.∀f:A →B.
181 \fold[op,nil]_{i ∈ l| p i} (f i) =
182 \fold[op,nil]_{i ∈ (filter A p l)} (f i).
183 #A #B #a #l #p #op #nil #f elim l //
184 #a #tl #Hind cases(true_or_false (p a)) #pa
185 [ >filter_true // > fold_true // >fold_true //
186 | >filter_false // >fold_false // ]
189 record Aop (A:Type[0]) (nil:A) : Type[0] ≝
191 nill:∀a. op nil a = a;
192 nilr:∀a. op a nil = a;
193 assoc: ∀a,b,c.op a (op b c) = op (op a b) c
196 theorem fold_sum: ∀A,B. ∀I,J:list A.∀nil.∀op:Aop B nil.∀f.
197 op (\fold[op,nil]_{i∈I} (f i)) (\fold[op,nil]_{i∈J} (f i)) =
198 \fold[op,nil]_{i∈(I@J)} (f i).
199 #A #B #I #J #nil #op #f (elim I) normalize
200 [>nill //|#a #tl #Hind <assoc //]
203 (********************** lhd and ltl ******************************)
205 let rec lhd (A:Type[0]) (l:list A) n on n ≝ match n with
207 | S n ⇒ match l with [ nil ⇒ nil … | cons a l ⇒ a :: lhd A l n ]
210 let rec ltl (A:Type[0]) (l:list A) n on n ≝ match n with
212 | S n ⇒ ltl A (tail … l) n
215 lemma lhd_nil: ∀A,n. lhd A ([]) n = [].
219 lemma ltl_nil: ∀A,n. ltl A ([]) n = [].
220 #A #n elim n normalize //
223 lemma lhd_cons_ltl: ∀A,n,l. lhd A l n @ ltl A l n = l.
225 #n #IHn #l elim l normalize //
228 lemma length_ltl: ∀A,n,l. |ltl A l n| = |l| - n.
230 #n #IHn *; normalize /2/