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