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 "list/list.ma".
16 include "decidable_kit/eqtype.ma".
17 include "nat/plus.ma".
19 (* ### some functions on lists ### *)
21 definition count : ∀T : eqType.∀f : T → bool.∀l : list T. nat :=
23 foldr T nat (λx,acc. match (f x) with [ true ⇒ S acc | false ⇒ acc ]) O l.
25 let rec mem (d : eqType) (x : d) (l : list d) on l : bool ≝
28 | cons y tl ⇒ orb (cmp d x y) (mem d x tl)].
30 (* ### eqtype for lists ### *)
31 let rec lcmp (d : eqType) (l1,l2 : list d) on l1 : bool ≝
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)]].
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]
49 lemma lcmpP : ∀d:eqType.∀l1,l2:list d. eq_compatible (list d) l1 l2 (lcmp 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;
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;]]]]
71 definition list_eqType : eqType → eqType ≝ λd:eqType.mk_eqType ? ? (lcmpP d).