]> matita.cs.unibo.it Git - helm.git/blob - matita/library/decidable_kit/list_aux.ma
tagged 0.5.0-rc1
[helm.git] / matita / library / decidable_kit / list_aux.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 set "baseuri" "cic:/matita/decidable_kit/list_aux/".
16
17 include "list/list.ma".
18 include "decidable_kit/eqtype.ma".
19 include "nat/plus.ma".
20
21 (* ### some functions on lists ### *)
22
23 definition count : ∀T : eqType.∀f : T → bool.∀l : list T. nat :=
24   λT:eqType.λf,l. 
25   foldr T nat (λx,acc. match (f x) with [ true ⇒ S acc | false ⇒ acc ]) O l.
26      
27 let rec mem (d : eqType) (x : d) (l : list d) on l : bool ≝
28   match l with
29   [ nil ⇒ false 
30   | cons y tl ⇒ orb (cmp d x y) (mem d x tl)].
31
32 (* ### eqtype for lists ### *)
33 let rec lcmp (d : eqType) (l1,l2 : list d) on l1 : bool ≝
34   match l1 with
35   [ nil ⇒ match l2 with [ nil ⇒ true | (cons _ _) ⇒ false]
36   | (cons x1 tl1) ⇒ match l2 with 
37       [ nil ⇒ false | (cons x2 tl2) ⇒ andb (cmp d x1 x2) (lcmp d tl1 tl2)]].
38            
39 lemma lcmp_length : 
40   ∀d:eqType.∀l1,l2:list d.
41  lcmp ? l1 l2 = true → length ? l1 = length ? l2.
42 intros 2 (d l1); elim l1 1 (l2 x1);
43 [1: cases l2; simplify; intros; [reflexivity|destruct H] 
44 |2: intros 3 (tl1 IH l2); cases (l2); intros; [1:simplify in H; destruct H]
45     simplify; (* XXX la apply non fa simplify? *) 
46     apply congr_S; apply (IH l);
47     (* XXX qualcosa di enorme e' rotto! la regola di convertibilita?! *)
48     simplify in H; cases (b2pT ? ? (andbP ? ?) H); assumption]
49 qed.
50
51 lemma lcmpP : ∀d:eqType.∀l1,l2:list d. eq_compatible (list d) l1 l2 (lcmp d l1 l2).
52 intros (d l1 l2);
53 generalize in match (refl_eq ? (lcmp d l1 l2));
54 generalize in match (lcmp d l1 l2) in ⊢ (? ? ? % → %); intros 1 (c);
55 cases c; intros (H); [ apply reflect_true | apply reflect_false ]
56 [ lapply (lcmp_length ? ? ? H) as Hl;
57   generalize in match H; clear H;
58   apply (list_ind2 ? ? ? ? ? Hl); [1: intros; reflexivity]
59   simplify; intros (tl1 tl2 hd1 hd2 IH H); cases (b2pT ? ? (andbP ? ?) H);
60   rewrite > (IH H2); rewrite > (b2pT ? ? (eqP d ? ?) H1); reflexivity
61 | generalize in match H; clear H; generalize in match l2; clear l2;
62   elim l1 1 (l1 x1);
63    [ cases l1; simplify; [intros; destruct H | unfold Not; intros; destruct H1;]
64    | intros 3 (tl1 IH l2); cases l2;
65      [ unfold Not; intros; destruct H1;
66      | simplify;  intros;
67        cases (b2pT ? ? (andbPF ? ?) (p2bT ? ? (negbP ?) H)); clear H;
68        [ intros; lapply (b2pF ? ? (eqP d ? ?) H1) as H'; clear H1;
69          destruct H; apply H'; reflexivity;
70        | intros; lapply (IH ? H1) as H'; destruct H;
71          apply H'; reflexivity;]]]]
72 qed.
73     
74 definition list_eqType : eqType → eqType ≝ λd:eqType.mk_eqType ? ? (lcmpP d).