1 (**************************************************************************)
4 (* ||A|| A project by Andrea Asperti *)
6 (* ||I|| Developers: *)
7 (* ||T|| A.Asperti, C.Sacerdoti Coen, *)
8 (* ||A|| E.Tassi, S.Zacchiroli *)
10 (* \ / This file is distributed under the terms of the *)
11 (* v GNU Lesser General Public License Version 2.1 *)
13 (**************************************************************************)
15 (* ********************************************************************** *)
16 (* Progetto FreeScale *)
18 (* Sviluppato da: Ing. Cosimo Oliboni, oliboni@cs.unibo.it *)
19 (* Sviluppo: 2008-2010 *)
21 (* ********************************************************************** *)
23 include "common/comp.ma".
24 include "common/option.ma".
25 include "common/nat.ma".
31 ninductive list (A:Type) : Type ≝
33 | cons: A → list A → list A.
35 nlet rec append A (l1: list A) l2 on l1 ≝
38 | (cons hd tl) ⇒ cons A hd (append A tl l2) ].
40 notation "hvbox(hd break :: tl)"
41 right associative with precedence 47
44 notation "[ list0 x sep ; ]"
45 non associative with precedence 90
46 for ${fold right @'nil rec acc @{'cons $x $acc}}.
48 notation "hvbox(l1 break @ l2)"
49 right associative with precedence 47
50 for @{'append $l1 $l2 }.
52 interpretation "nil" 'nil = (nil ?).
53 interpretation "cons" 'cons hd tl = (cons ? hd tl).
54 interpretation "append" 'append l1 l2 = (append ? l1 l2).
56 nlemma list_destruct_1 : ∀T.∀x1,x2:T.∀y1,y2:list T.cons T x1 y1 = cons T x2 y2 → x1 = x2.
57 #T; #x1; #x2; #y1; #y2; #H;
58 nchange with (match cons T x2 y2 with [ nil ⇒ False | cons a _ ⇒ x1 = a ]);
64 nlemma list_destruct_2 : ∀T.∀x1,x2:T.∀y1,y2:list T.cons T x1 y1 = cons T x2 y2 → y1 = y2.
65 #T; #x1; #x2; #y1; #y2; #H;
66 nchange with (match cons T x2 y2 with [ nil ⇒ False | cons _ b ⇒ y1 = b ]);
72 nlemma list_destruct_cons_nil : ∀T.∀x:T.∀y:list T.cons T x y = nil T → False.
74 nchange with (match cons T x y with [ nil ⇒ True | cons a b ⇒ False ]);
80 nlemma list_destruct_nil_cons : ∀T.∀x:T.∀y:list T.nil T = cons T x y → False.
82 nchange with (match cons T x y with [ nil ⇒ True | cons a b ⇒ False ]);
88 nlemma append_nil : ∀T:Type.∀l:list T.(l@[]) = l.
92 ##[ ##1: napply refl_eq
99 nlemma associative_list : ∀T.associative (list T) (append T).
103 ##[ ##1: napply refl_eq
110 nlemma cons_append_commute : ∀T:Type.∀l1,l2:list T.∀a:T.a :: (l1 @ l2) = (a :: l1) @ l2.
116 nlemma append_cons_commute : ∀T:Type.∀a:T.∀l,l1:list T.l @ (a::l1) = (l@[a]) @ l1.
118 nrewrite > (associative_list T l [a] l1);
124 nlet rec len_list (T:Type) (l:list T) on l ≝
125 match l with [ nil ⇒ O | cons _ t ⇒ S (len_list T t) ].
128 ndefinition is_empty_list ≝
129 λT:Type.λl:list T.match l with [ nil ⇒ True | cons _ _ ⇒ False ].
131 ndefinition isb_empty_list ≝
132 λT:Type.λl:list T.match l with [ nil ⇒ true | cons _ _ ⇒ false ].
134 ndefinition isnot_empty_list ≝
135 λT:Type.λl:list T.match l with [ nil ⇒ False | cons _ _ ⇒ True ].
137 ndefinition isnotb_empty_list ≝
138 λT:Type.λl:list T.match l with [ nil ⇒ false | cons _ _ ⇒ true ].
141 nlet rec reverse_list (T:Type) (l:list T) on l ≝
144 | cons h t ⇒ (reverse_list T t)@[h]
148 ndefinition get_first_list ≝
149 λT:Type.λl:list T.match l with
151 | cons h _ ⇒ Some ? h ].
154 ndefinition get_last_list ≝
155 λT:Type.λl:list T.match reverse_list T l with
157 | cons h _ ⇒ Some ? h ].
160 ndefinition cut_first_list ≝
161 λT:Type.λl:list T.match l with
166 ndefinition cut_last_list ≝
167 λT:Type.λl:list T.match reverse_list T l with
169 | cons _ t ⇒ reverse_list T t ].
172 nlet rec apply_f_list (T1,T2:Type) (l:list T1) (f:T1 → T2) on l ≝
175 | cons h t ⇒ cons T2 (f h) (apply_f_list T1 T2 t f) ].
178 nlet rec fold_right_list (T1,T2:Type) (f:T1 → T2 → T2) (acc:T2) (l:list T1) on l ≝
181 | cons h t ⇒ f h (fold_right_list T1 T2 f acc t)
184 (* double fold right *)
185 nlemma fold_right_list2_aux1 :
186 ∀T.∀h,t.len_list T [] = len_list T (h::t) → False.
190 ndestruct (*napply (nat_destruct_0_S ? H)*).
193 nlemma fold_right_list2_aux2 :
194 ∀T.∀h,t.len_list T (h::t) = len_list T [] → False.
198 ndestruct (*napply (nat_destruct_S_0 ? H)*).
201 nlemma fold_right_list2_aux3 :
202 ∀T.∀h,h',t,t'.len_list T (h::t) = len_list T (h'::t') → len_list T t = len_list T t'.
203 #T; #h; #h'; #t; #t';
206 ##[ ##1: nnormalize; #H; napply refl_eq
207 ##| ##2: #a; #l'; #H; #H1;
208 nchange in H1:(%) with ((S O) = (S (S (len_list T l'))));
209 ndestruct (*nelim (nat_destruct_0_S ? (nat_destruct_S_S … H1))*)
210 ##| ##3: #a; #l'; #H; #H1;
211 nchange in H1:(%) with ((S (S (len_list T l'))) = (S O));
212 ndestruct (*nelim (nat_destruct_S_0 ? (nat_destruct_S_S … H1))*)
213 ##| ##4: #a; #l; #H; #a1; #l1; #H1; #H2;
214 nchange in H2:(%) with ((S (S (len_list T l1))) = (S (S (len_list T l))));
215 nchange with ((S (len_list T l1)) = (S (len_list T l)));
216 nrewrite > (nat_destruct_S_S … H2);
221 nlet rec fold_right_list2 (T1,T2:Type) (f:T1 → T1 → T2 → T2) (acc:T2) (l1:list T1) on l1 ≝
223 return λl1.Πl2.len_list T1 l1 = len_list T1 l2 → T2
225 [ nil ⇒ λl2.match l2 return λl2.len_list T1 [] = len_list T1 l2 → T2 with
226 [ nil ⇒ λp:len_list T1 [] = len_list T1 [].acc
227 | cons h t ⇒ λp:len_list T1 [] = len_list T1 (h::t).
228 False_rect_Type0 ? (fold_right_list2_aux1 T1 h t p)
230 | cons h t ⇒ λl2.match l2 return λl2.len_list T1 (h::t) = len_list T1 l2 → T2 with
231 [ nil ⇒ λp:len_list T1 (h::t) = len_list T1 [].
232 False_rect_Type0 ? (fold_right_list2_aux2 T1 h t p)
233 | cons h' t' ⇒ λp:len_list T1 (h::t) = len_list T1 (h'::t').
234 f h h' (fold_right_list2 T1 T2 f acc t t' (fold_right_list2_aux3 T1 h h' t t' p))
238 nlet rec bfold_right_list2 (T1:Type) (f:T1 → T1 → bool) (l1,l2:list T1) on l1 ≝
240 [ nil ⇒ match l2 with
241 [ nil ⇒ true | cons h t ⇒ false ]
242 | cons h t ⇒ match l2 with
243 [ nil ⇒ false | cons h' t' ⇒ (f h h') ⊗ (bfold_right_list2 T1 f t t')
248 nlet rec nth_list (T:Type) (l:list T) (n:nat) on l ≝
251 | cons h t ⇒ match n with
252 [ O ⇒ Some ? h | S n' ⇒ nth_list T t n' ]
256 ndefinition abs_list_aux1 : ∀T:Type.∀n.((len_list T []) > n) = true → False.
257 #T; nnormalize; #n; #H; ndestruct (*napply (bool_destruct … H)*). nqed.
259 ndefinition abs_list_aux2 : ∀T:Type.∀h:T.∀t:list T.∀n.((len_list T (h::t)) > (S n) = true) → ((len_list T t) > n) = true.
260 #T; #h; #t; #n; nnormalize; #H; napply H. nqed.
262 nlet rec abs_list (T:Type) (l:list T) on l ≝
264 return λl.Πn.(((len_list T l) > n) = true) → T
266 [ nil ⇒ λn.λp:(((len_list T []) > n) = true).False_rect_Type0 ? (abs_list_aux1 T n p)
269 [ O ⇒ λp:(((len_list T (h::t)) > O) = true).h
270 | S n' ⇒ λp:(((len_list T (h::t)) > (S n')) = true).
271 abs_list T t n' (abs_list_aux2 T h t n' p)
275 (* esempio: abs_list ? [ 1; 2; 3 ; 4 ] 0 (refl_eq …) = 1. *)
277 nlemma symmetric_lenlist : ∀T.∀l1,l2:list T.len_list T l1 = len_list T l2 → len_list T l2 = len_list T l1.
280 ##[ ##1: #l2; ncases l2; nnormalize;
281 ##[ ##1: #H; napply refl_eq
282 ##| ##2: #h; #t; #H; ndestruct (*nelim (nat_destruct_0_S ? H)*)
284 ##| ##2: #h; #l2; ncases l2; nnormalize;
285 ##[ ##1: #H; #l; #H1; nrewrite < H1; napply refl_eq
286 ##| ##2: #h; #l; #H; #l3; #H1; nrewrite < H1; napply refl_eq
291 nlemma symmetric_foldrightlist2_aux :
292 ∀T1,T2:Type.∀f:T1 → T1 → T2 → T2.
293 (∀x,y,z.f x y z = f y x z) →
294 (∀acc:T2.∀l1,l2:list T1.
295 ∀H1:(len_list T1 l1 = len_list T1 l2).
296 ∀H2:(len_list T1 l2 = len_list T1 l1).
297 (fold_right_list2 T1 T2 f acc l1 l2 H1 = fold_right_list2 T1 T2 f acc l2 l1 H2)).
298 #T1; #T2; #f; #H; #acc; #l1;
300 ##[ ##1: #l2; ncases l2;
301 ##[ ##1: nnormalize; #H1; #H2; napply refl_eq
302 ##| ##2: #h; #l; #H1; #H2;
303 nchange in H1:(%) with (O = (S (len_list ? l)));
304 ndestruct (*nelim (nat_destruct_0_S ? H1)*)
306 ##| ##2: #h3; #l3; #H1; #l2; ncases l2;
307 ##[ ##1: #H2; #H3; nchange in H2:(%) with ((S (len_list ? l3)) = O);
308 ndestruct (*nelim (nat_destruct_S_0 ? H1)*)
309 ##| ##2: #h4; #l4; #H2; #H3;
310 nchange in H2:(%) with ((S (len_list ? l3)) = (S (len_list ? l4)));
311 nchange in H3:(%) with ((S (len_list ? l4)) = (S (len_list ? l3)));
312 nchange with ((f h3 h4 (fold_right_list2 T1 T2 f acc l3 l4 (fold_right_list2_aux3 T1 h3 h4 l3 l4 ?))) =
313 (f h4 h3 (fold_right_list2 T1 T2 f acc l4 l3 (fold_right_list2_aux3 T1 h4 h3 l4 l3 ?))));
314 nrewrite < (H1 l4 (fold_right_list2_aux3 T1 h3 h4 l3 l4 H2) (fold_right_list2_aux3 T1 h4 h3 l4 l3 H3));
315 nrewrite > (H h3 h4 (fold_right_list2 T1 T2 f acc l3 l4 ?));
321 nlemma symmetric_foldrightlist2 :
322 ∀T1,T2:Type.∀f:T1 → T1 → T2 → T2.
323 (∀x,y,z.f x y z = f y x z) →
324 (∀acc:T2.∀l1,l2:list T1.∀H:len_list T1 l1 = len_list T1 l2.
325 fold_right_list2 T1 T2 f acc l1 l2 H = fold_right_list2 T1 T2 f acc l2 l1 (symmetric_lenlist T1 l1 l2 H)).
326 #T1; #T2; #f; #H; #acc; #l1; #l2; #H1;
327 nrewrite > (symmetric_foldrightlist2_aux T1 T2 f H acc l1 l2 H1 (symmetric_lenlist T1 l1 l2 H1));
331 nlemma symmetric_bfoldrightlist2 :
332 ∀T1:Type.∀f:T1 → T1 → bool.
333 (∀x,y.f x y = f y x) →
335 bfold_right_list2 T1 f l1 l2 = bfold_right_list2 T1 f l2 l1).
338 ##[ ##1: #l2; ncases l2;
339 ##[ ##1: nnormalize; napply refl_eq
340 ##| ##2: #hh2; #ll2; nnormalize; napply refl_eq
342 ##| ##2: #hh1; #ll1; #H1; #l2; ncases l2;
343 ##[ ##1: nnormalize; napply refl_eq
344 ##| ##2: #hh2; #ll2; nnormalize;
346 nrewrite > (H hh1 hh2);
352 nlemma bfoldrightlist2_to_eq :
353 ∀T1:Type.∀f:T1 → T1 → bool.
354 (∀x,y.(f x y = true → x = y)) →
356 (bfold_right_list2 T1 f l1 l2 = true → l1 = l2)).
359 ##[ ##1: #l2; ncases l2;
360 ##[ ##1: #H1; napply refl_eq
361 ##| ##2: #hh2; #ll2; nnormalize; #H1;
362 ndestruct (*napply (bool_destruct … H1)*)
364 ##| ##2: #hh1; #ll1; #H1; #l2; ncases l2;
365 ##[ ##1: nnormalize; #H2;
366 ndestruct (*napply (bool_destruct … H2)*)
367 ##| ##2: #hh2; #ll2; #H2;
368 nchange in H2:(%) with (((f hh1 hh2)⊗(bfold_right_list2 T f ll1 ll2)) = true);
369 nrewrite > (H hh1 hh2 (andb_true_true_l … H2));
370 nrewrite > (H1 ll2 (andb_true_true_r … H2));
376 nlemma eq_to_bfoldrightlist2 :
377 ∀T1:Type.∀f:T1 → T1 → bool.
378 (∀x,y.(x = y → f x y = true)) →
380 (l1 = l2 → bfold_right_list2 T1 f l1 l2 = true)).
383 ##[ ##1: #l2; ncases l2;
384 ##[ ##1: #H1; nnormalize; napply refl_eq
385 ##| ##2: #hh2; #ll2; #H1;
386 (* !!! ndestruct: assert false *)
387 nelim (list_destruct_nil_cons ??? H1)
389 ##| ##2: #hh1; #ll1; #H1; #l2; ncases l2;
391 (* !!! ndestruct: assert false *)
392 nelim (list_destruct_cons_nil ??? H2)
393 ##| ##2: #hh2; #ll2; #H2; nnormalize;
394 nrewrite > (list_destruct_1 … H2);
395 nrewrite > (H hh2 hh2 (refl_eq …));
397 nrewrite > (H1 ll2 (list_destruct_2 … H2));
403 nlemma bfoldrightlist2_to_lenlist :
405 (∀l1,l2:list T.bfold_right_list2 T f l1 l2 = true → len_list T l1 = len_list T l2).
408 ##[ ##1: #l2; ncases l2;
409 ##[ ##1: nnormalize; #H; napply refl_eq
410 ##| ##2: nnormalize; #hh; #tt; #H;
411 ndestruct (*napply (bool_destruct … H)*)
413 ##| ##2: #hh; #tt; #H; #l2; ncases l2;
414 ##[ ##1: nnormalize; #H1;
415 ndestruct (*napply (bool_destruct … H1)*)
416 ##| ##2: #hh1; #tt1; #H1; nnormalize;
417 nrewrite > (H tt1 ?);
418 ##[ ##1: napply refl_eq
419 ##| ##2: nchange in H1:(%) with ((? ⊗ (bfold_right_list2 T f tt tt1)) = true);
420 napply (andb_true_true_r … H1)
426 nlemma decidable_list :
427 ∀T.(∀x,y:T.decidable (x = y)) →
428 (∀x,y:list T.decidable (x = y)).
430 ##[ ##1: #y; ncases y;
431 ##[ ##1: nnormalize; napply (or2_intro1 (? = ?) (? ≠ ?) (refl_eq …))
432 ##| ##2: #hh2; #tt2; nnormalize; napply (or2_intro2 (? = ?) (? ≠ ?) ?);
434 (* !!! ndestruct: assert false *)
435 napply (list_destruct_nil_cons T … H1)
437 ##| ##2: #hh1; #tt1; #H1; #y; ncases y;
438 ##[ ##1: nnormalize; napply (or2_intro2 (? = ?) (? ≠ ?) ?);
440 (* !!! ndestruct: assert false *)
441 napply (list_destruct_cons_nil T … H2)
442 ##| ##2: #hh2; #tt2; nnormalize; napply (or2_elim (hh1 = hh2) (hh1 ≠ hh2) ? (H …));
443 ##[ ##2: #H2; napply (or2_intro2 (? = ?) (? ≠ ?) ?);
444 nnormalize; #H3; napply (H2 (list_destruct_1 T … H3))
445 ##| ##1: #H2; napply (or2_elim (tt1 = tt2) (tt1 ≠ tt2) ? (H1 tt2));
446 ##[ ##2: #H3; napply (or2_intro2 (? = ?) (? ≠ ?) ?);
447 nnormalize; #H4; napply (H3 (list_destruct_2 T … H4))
448 ##| ##1: #H3; napply (or2_intro1 (? = ?) (? ≠ ?) ?);
449 nrewrite > H2; nrewrite > H3; napply refl_eq
456 nlemma nbfoldrightlist2_to_neq :
457 ∀T1:Type.∀f:T1 → T1 → bool.
458 (∀x,y.(f x y = false → x ≠ y)) →
460 (bfold_right_list2 T1 f l1 l2 = false → l1 ≠ l2)).
463 ##[ ##1: #l2; ncases l2;
464 ##[ ##1: nnormalize; #H1;
465 ndestruct (*napply (bool_destruct … H1)*)
466 ##| ##2: #hh2; #ll2; #H1; nnormalize; #H2;
467 (* !!! ndestruct: assert false *)
468 napply (list_destruct_nil_cons T … H2)
470 ##| ##2: #hh1; #ll1; #H1; #l2; ncases l2;
471 ##[ ##1: #H2; nnormalize; #H3;
472 (* !!! ndestruct: assert false *)
473 napply (list_destruct_cons_nil T … H3)
474 ##| ##2: #hh2; #ll2; #H2; nnormalize; #H3;
475 nchange in H2:(%) with (((f hh1 hh2)⊗(bfold_right_list2 T f ll1 ll2)) = false);
476 napply (H1 ll2 ? (list_destruct_2 T … H3));
477 napply (or2_elim ??? (andb_false2 … H2) );
478 ##[ ##1: #H4; napply (absurd (hh1 = hh2) …);
479 ##[ ##1: nrewrite > (list_destruct_1 T … H3); napply refl_eq
480 ##| ##2: napply (H … H4)
482 ##| ##2: #H4; napply H4
488 nlemma list_destruct :
489 ∀T.(∀x,y:T.decidable (x = y)) →
490 (∀h1,h2:T.∀l1,l2:list T.
491 (h1::l1) ≠ (h2::l2) → h1 ≠ h2 ∨ l1 ≠ l2).
492 #T; #H; #h1; #h2; #l1; nelim l1;
493 ##[ ##1: #l2; ncases l2;
494 ##[ ##1: #H1; napply (or2_intro1 (h1 ≠ h2) ([] ≠ []) …);
495 nnormalize; #H2; nrewrite > H2 in H1:(%);
496 nnormalize; #H1; napply (H1 (refl_eq …))
497 ##| ##2: #hh2; #ll2; #H1; napply (or2_intro2 (h1 ≠ h2) ([] ≠ (hh2::ll2)) …);
499 (* !!! ndestruct: assert false *)
500 napply (list_destruct_nil_cons T … H2)
502 ##| ##2: #hh1; #ll1; #H1; #l2; ncases l2;
503 ##[ ##1: #H2; napply (or2_intro2 (h1 ≠ h2) ((hh1::ll1) ≠ []) …);
505 (* !!! ndestruct: assert false *)
506 napply (list_destruct_cons_nil T … H3)
507 ##| ##2: #hh2; #ll2; #H2;
508 napply (or2_elim (h1 = h2) (h1 ≠ h2) ? (H h1 h2) …);
509 ##[ ##2: #H3; napply (or2_intro1 (h1 ≠ h2) ((hh1::ll1) ≠ (hh2::ll2)) H3)
510 ##| ##1: #H3; napply (or2_intro2 (h1 ≠ h2) ((hh1::ll1) ≠ (hh2::ll2) …));
511 nrewrite > H3 in H2:(%); #H2;
512 nnormalize; #H4; nrewrite > (list_destruct_1 T … H4) in H2:(%); #H2;
513 nrewrite > (list_destruct_2 T … H4) in H2:(%); #H2;
514 napply (H2 (refl_eq …))
520 nlemma neq_to_nbfoldrightlist2 :
521 ∀T:Type.∀f:T → T → bool.
522 (∀x,y:T.decidable (x = y)) →
523 (∀x,y.(x ≠ y → f x y = false)) →
525 (l1 ≠ l2 → bfold_right_list2 T f l1 l2 = false)).
526 #T; #f; #H; #H1; #l1;
528 ##[ ##1: #l2; ncases l2;
529 ##[ ##1: nnormalize; #H2; nelim (H2 (refl_eq …))
530 ##| ##2: #hh2; #ll2; nnormalize; #H2; napply refl_eq
532 ##| ##2: #hh1; #ll1; #H2; #l2; ncases l2;
533 ##[ ##1: nnormalize; #H3; napply refl_eq
534 ##| ##2: #hh2; #ll2; #H3;
535 nchange with (((f hh1 hh2)⊗(bfold_right_list2 T f ll1 ll2)) = false);
536 napply (or2_elim (hh1 ≠ hh2) (ll1 ≠ ll2) ? (list_destruct T H … H3) …);
537 ##[ ##1: #H4; nrewrite > (H1 hh1 hh2 H4); nnormalize; napply refl_eq
538 ##| ##2: #H4; nrewrite > (H2 ll2 H4);
539 nrewrite > (symmetric_andbool (f hh1 hh2) false);
540 nnormalize; napply refl_eq
546 nlemma isbemptylist_to_isemptylist : ∀T,l.isb_empty_list T l = true → is_empty_list T l.
550 ##[ ##1: #H; napply I
551 ##| ##2: #x; #l; #H; ndestruct (*napply (bool_destruct … H)*)
555 nlemma isnotbemptylist_to_isnotemptylist : ∀T,l.isnotb_empty_list T l = true → isnot_empty_list T l.
559 ##[ ##1: #H; ndestruct (*napply (bool_destruct … H)*)
560 ##| ##2: #x; #l; #H; napply I
564 nlemma list_is_comparable : comparable → comparable.
565 #T; napply (mk_comparable (list T));
567 ##| napply (λx.false)
568 ##| napply (bfold_right_list2 T (eqc T))
569 ##| napply (bfoldrightlist2_to_eq … (eqc T));
571 ##| napply (eq_to_bfoldrightlist2 … (eqc T));
573 ##| napply (nbfoldrightlist2_to_neq … (eqc T));
574 napply (neqc_to_neq T)
575 ##| napply (neq_to_nbfoldrightlist2 … (eqc T));
576 ##[ napply (decidable_c T)
577 ##| napply (neq_to_neqc T)
579 ##| napply decidable_list;
580 napply (decidable_c T)
581 ##| napply symmetric_bfoldrightlist2;
582 napply (symmetric_eqc T)
586 unification hint 0 ≔ S: comparable;
588 X ≟ (list_is_comparable S)
589 (*********************************************) ⊢