1 (**************************************************************************)
4 (* ||A|| A project by Andrea Asperti *)
6 (* ||I|| Developers: *)
7 (* ||T|| The HELM team. *)
8 (* ||A|| http://helm.cs.unibo.it *)
10 (* \ / This file is distributed under the terms of the *)
11 (* v GNU General Public License Version 2 *)
13 (**************************************************************************)
15 include "nat/minus.ma".
16 include "list/list.ma".
18 interpretation "list nth" 'nth = (nth _).
19 interpretation "list nth" 'nth_appl l d i = (nth _ l d i).
20 notation "\nth" with precedence 90 for @{'nth}.
21 notation < "\nth \nbsp term 90 l \nbsp term 90 d \nbsp term 90 i"
22 with precedence 69 for @{'nth_appl $l $d $i}.
24 definition make_list ≝
26 let rec make_list (n:nat) on n ≝
27 match n with [ O ⇒ nil ? | S m ⇒ def m :: make_list m]
30 interpretation "\mk_list appl" 'mk_list_appl f n = (make_list _ f n).
31 interpretation "\mk_list" 'mk_list = (make_list _).
32 notation "\mk_list" with precedence 90 for @{'mk_list}.
33 notation < "\mk_list \nbsp term 90 f \nbsp term 90 n"
34 with precedence 69 for @{'mk_list_appl $f $n}.
36 notation "\len" with precedence 90 for @{'len}.
37 interpretation "len" 'len = (length _).
38 notation < "\len \nbsp term 90 l" with precedence 69 for @{'len_appl $l}.
39 interpretation "len appl" 'len_appl l = (length _ l).
41 lemma mk_list_ext: ∀T:Type.∀f1,f2:nat→T.∀n. (∀x.x<n → f1 x = f2 x) → \mk_list f1 n = \mk_list f2 n.
42 intros 4;elim n; [reflexivity] simplify; rewrite > H1; [2: apply le_n]
43 apply eq_f; apply H; intros; apply H1; apply (trans_le ??? H2); apply le_S; apply le_n;
46 lemma len_mk_list : ∀T:Type.∀f:nat→T.∀n.\len (\mk_list f n) = n.
47 intros; elim n; [reflexivity] simplify; rewrite > H; reflexivity;
50 record rel (rel_T : Type) : Type ≝ {
51 rel_op :2> rel_T → rel_T → Prop
54 record trans_rel : Type ≝ {
57 o_tra : ∀x,y,z: o_T.o_rel x y → o_rel y z → o_rel x z
60 lemma trans: ∀r:trans_rel.∀x,y,z:r.r x y → r y z → r x z.
64 inductive sorted (lt : trans_rel): list (o_T lt) → Prop ≝
65 | sorted_nil : sorted lt []
66 | sorted_one : ∀x. sorted lt [x]
67 | sorted_cons : ∀x,y,tl. lt x y → sorted lt (y::tl) → sorted lt (x::y::tl).
69 lemma nth_nil: ∀T,i.∀def:T. \nth [] def i = def.
70 intros; elim i; simplify; [reflexivity;] assumption; qed.
72 lemma len_append: ∀T:Type.∀l1,l2:list T. \len (l1@l2) = \len l1 + \len l2.
73 intros; elim l1; [reflexivity] simplify; rewrite < H; reflexivity;
76 coinductive non_empty_list (A:Type) : list A → Type :=
77 | show_head: ∀x,l. non_empty_list A (x::l).
79 lemma len_gt_non_empty :
80 ∀T.∀l:list T.O < \len l → non_empty_list T l.
81 intros; cases l in H; [intros; cases (not_le_Sn_O ? H);] intros; constructor 1;
84 lemma sorted_tail: ∀r,x,l.sorted r (x::l) → sorted r l.
85 intros; inversion H; intros; [destruct H1;|destruct H1;constructor 1;]
86 destruct H4; assumption;
89 lemma sorted_skip: ∀r,x,y,l. sorted r (x::y::l) → sorted r (x::l).
90 intros (r x y l H1); inversion H1; intros; [1,2: destruct H]
91 destruct H4; inversion H2; intros; [destruct H4]
92 [1: destruct H4; constructor 2;
93 |2: destruct H7; constructor 3;
94 [ apply (o_tra ? ??? H H4); | apply (sorted_tail ??? H2);]]
97 lemma sorted_tail_bigger : ∀r,x,l,d.sorted r (x::l) → ∀i. i < \len l → r x (\nth l d i).
98 intros 4; elim l; [ cases (not_le_Sn_O i H1);]
100 [2: intros; apply (H ? n);[apply (sorted_skip ???? H1)|apply le_S_S_to_le; apply H2]
101 |1: intros; inversion H1; intros; [1,2: destruct H3]
102 destruct H6; simplify; assumption;]
106 lemma lt_n_plus_n_Sm : ∀n,m:nat.n < n + S m.
107 intros; rewrite > sym_plus; apply (le_S_S n (m+n)); alias id "le_plus_n" = "cic:/matita/nat/le_arith/le_plus_n.con".
108 apply (le_plus_n m n); qed.
110 lemma nth_append_lt_len:
111 ∀T:Type.∀l1,l2:list T.∀def.∀i.i < \len l1 → \nth (l1@l2) def i = \nth l1 def i.
112 intros 4; elim l1; [cases (not_le_Sn_O ? H)] cases i in H H1; simplify; intros;
113 [reflexivity| rewrite < H;[reflexivity] apply le_S_S_to_le; apply H1]
116 lemma nth_append_ge_len:
117 ∀T:Type.∀l1,l2:list T.∀def.∀i.
118 \len l1 ≤ i → \nth (l1@l2) def i = \nth l2 def (i - \len l1).
119 intros 4; elim l1; [ rewrite < minus_n_O; reflexivity]
120 cases i in H1; simplify; intros; [cases (not_le_Sn_O ? H1)]
121 apply H; apply le_S_S_to_le; apply H1;
125 ∀T:Type.∀l1,l2:list T.∀def,x.
126 \nth (l1@x::l2) def (\len l1) = x.
127 intros 2; elim l1;[reflexivity] simplify; apply H; qed.
129 lemma sorted_head_smaller:
130 ∀r,l,p,d. sorted r (p::l) → ∀i.i < \len l → r p (\nth l d i).
131 intros 2 (r l); elim l; [cases (not_le_Sn_O ? H1)] cases i in H2; simplify; intros;
132 [1: inversion H1; [1,2: simplify; intros; destruct H3] intros; destruct H6; assumption;
133 |2: apply (H p ?? n ?); [apply (sorted_skip ???? H1)] apply le_S_S_to_le; apply H2]
136 alias symbol "lt" = "natural 'less than'".
138 ∀r,l1,l2,p,d. sorted r (l1@p::l2) →
139 (∀i. i < \len l1 → r (\nth l1 d i) p) ∧
140 (∀i. i < \len l2 → r p (\nth l2 d i)).
141 intros 2 (r l); elim l;
142 [1: split; [intros; cases (not_le_Sn_O ? H1);] intros;
143 apply sorted_head_smaller; assumption;
144 |2: simplify in H1; cases (H ?? d (sorted_tail ??? H1));
145 lapply depth = 0 (sorted_head_smaller ??? d H1) as Hs;
146 split; simplify; intros;
147 [1: cases i in H4; simplify; intros;
148 [1: lapply depth = 0 (Hs (\len l1)) as HS;
149 rewrite > nth_len in HS; apply HS;
150 rewrite > len_append; simplify; apply lt_n_plus_n_Sm;
151 |2: apply (H2 n); apply le_S_S_to_le; apply H4]
152 |2: apply H3; assumption]]
155 coinductive cases_bool (p:bool) : bool → CProp ≝
156 | case_true : p = true → cases_bool p true
157 | cases_false : p = false → cases_bool p false.
159 lemma case_b : ∀A:Type.∀f:A → bool. ∀x.cases_bool (f x) (f x).
160 intros; cases (f x);[left;|right] reflexivity;
163 coinductive break_spec (T : Type) (n : nat) (l : list T) : list T → CProp ≝
164 | break_to: ∀l1,x,l2. \len l1 = n → l = l1 @ [x] @ l2 → break_spec T n l l.
166 lemma list_break: ∀T,n,l. n < \len l → break_spec T n l l.
168 [1: elim l in H; [cases (not_le_Sn_O ? H)]
169 apply (break_to ?? ? [] a l1); reflexivity;
170 |2: cases (H l); [2: apply lt_S_to_lt; assumption;] cases l2 in H3; intros;
171 [1: rewrite < H2 in H1; rewrite > H3 in H1; rewrite > append_nil in H1;
172 rewrite > len_append in H1; rewrite > plus_n_SO in H1;
173 cases (not_le_Sn_n ? H1);
174 |2: apply (break_to ?? ? (l1@[x]) t l3);
175 [2: simplify; rewrite > associative_append; assumption;
176 |1: rewrite < H2; rewrite > len_append; rewrite > plus_n_SO; reflexivity]]]
179 include "logic/cprop_connectives.ma".
182 λP.λp:∃x:nat.P x.match p with [ex_introT p _ ⇒ p].
184 definition inject_N ≝ λP.λp:nat.λh:P p. ex_introT ? P p h.
185 coercion inject_N with 0 1 nocomposites.
187 coinductive find_spec (T:Type) (P:T→bool) (l:list T) (d:T) (res : nat) : nat → CProp ≝
189 ∀i. i < \len l → P (\nth l d i) = true → res = i →
190 (∀j. j < i → P (\nth l d j) = false) → find_spec T P l d res i
191 | not_found: ∀i. i = \len l → res = i →
192 (∀j.j < \len l → P (\nth l d j) = false) → find_spec T P l d res i.
194 lemma find_lemma : ∀T.∀P:T→bool.∀l:list T.∀d.∃i.find_spec ? P l d i i.
197 let rec aux (acc: nat) (l : list T) on l : nat ≝
202 [ false ⇒ aux (S acc) tl
206 ∀story. story @ l1 = l → acc = \len story →
207 find_spec ? P story d acc acc →
208 find_spec ? P (story @ l1) d p p);
209 [4: clearbody find; cases (find 0 l);
210 lapply (H [] (refl_eq ??) (refl_eq ??)) as K;
211 [2: apply (not_found ?? [] d); intros; try reflexivity; cases (not_le_Sn_O ? H1);
212 |1: cases K; clear K;
213 [2: exists[apply (\len l)]
214 apply not_found; try reflexivity; apply H3;
215 |1: exists[apply i] apply found; try reflexivity; assumption;]]
216 |1: intros; cases (aux (S n) l2); simplify; clear aux;
217 lapply depth = 0 (H5 (story@[t])) as K; clear H5;
218 change with (find_spec ? P (story @ ([t] @ l2)) d w w);
219 rewrite < associative_append; apply K; clear K;
220 [1: rewrite > associative_append; apply H2;
221 |2: rewrite > H3; rewrite > len_append; rewrite > sym_plus; reflexivity;
222 |3: cases H4; clear H4; destruct H7;
223 [2: rewrite > H5; rewrite > (?:S (\len story) = \len (story @ [t])); [2:
224 rewrite > len_append; rewrite > sym_plus; reflexivity;]
225 apply not_found; try reflexivity; intros; cases (cmp_nat (S j) (\len story));
226 [1: rewrite > (nth_append_lt_len ????? H8); apply H7; apply H8;
227 |2: rewrite > (nth_append_ge_len ????? (le_S_S_to_le ?? H8));
228 rewrite > (?: j - \len story = 0);[assumption]
229 rewrite > (?:j = \len story);[rewrite > minus_n_n; reflexivity]
230 apply le_to_le_to_eq; [2: apply le_S_S_to_le; assumption;]
231 rewrite > len_append in H4;rewrite > sym_plus in H4;
232 apply le_S_S_to_le; apply H4;]
233 |1: rewrite < H3 in H5; cases (not_le_Sn_n ? H5);]]
234 |2: intros; cases H4; clear H4;
235 [1: destruct H7; rewrite > H3 in H5; cases (not_le_Sn_n ? H5);
236 |2: apply found; try reflexivity;
237 [1: rewrite > len_append; simplify; rewrite > H5; apply lt_n_plus_n_Sm;
238 |2: rewrite > H5; rewrite > nth_append_ge_len; [2: apply le_n]
239 rewrite < minus_n_n; assumption;
240 |3: intros; rewrite > H5 in H4; rewrite > nth_append_lt_len; [2:assumption]
241 apply H7; assumption]]
242 |3: intros; rewrite > append_nil; assumption;]
245 lemma find : ∀T:Type.∀P:T→bool.∀l:list T.∀d:T.nat.
246 intros; cases (find_lemma ? f l t); apply w; qed.
248 lemma cases_find: ∀T,P,l,d. find_spec T P l d (find T P l d) (find T P l d).
249 intros; unfold find; cases (find_lemma T P l d); simplify; assumption; qed.
251 lemma list_elim_with_len:
252 ∀T:Type.∀P: nat → list T → CProp.
253 P O [] → (∀l,a,n.P (\len l) l → P (S n) (a::l)) →
255 intros;elim l; [assumption] simplify; apply H1; apply H2;
259 ∀r,l. sorted r l → ∀i,d. S i < \len l → r (\nth l d i) (\nth l d (S i)).
261 [1: cases (not_le_Sn_O ? H1);
262 |2: simplify in H1; cases (not_le_Sn_O ? (le_S_S_to_le ?? H1));
263 |3: simplify; cases i in H4; intros; [apply H1]
264 apply H3; apply le_S_S_to_le; apply H4]
268 λT:Type.λd.λl:list T. \nth l d (pred (\len l)).
270 notation > "\last" non associative with precedence 90 for @{'last}.
271 notation < "\last d l" non associative with precedence 70 for @{'last2 $d $l}.
272 interpretation "list last" 'last = (last _).
273 interpretation "list last applied" 'last2 d l = (last _ d l).
276 λT:Type.λd.λl:list T.\nth l d O.
278 notation > "\hd" non associative with precedence 90 for @{'hd}.
279 notation < "\hd d l" non associative with precedence 70 for @{'hd2 $d $l}.
280 interpretation "list head" 'hd = (head _).
281 interpretation "list head applied" 'hd2 d l = (head _ d l).