]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/library/list/sort.ma
Lost of redundant typing hints removed from the functional induction term.
[helm.git] / helm / matita / library / list / sort.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/list/sort/".
16
17 include "datatypes/bool.ma".
18 include "datatypes/constructors.ma".
19 include "list/list.ma".
20
21 let rec mem (A:Set) (eq: A → A → bool) x (l: list A) on l ≝
22  match l with
23   [ nil ⇒ false
24   | (cons a l') ⇒
25     match eq x a with
26      [ true ⇒ true
27      | false ⇒ mem A eq x l'
28      ]
29   ].
30   
31 let rec ordered (A:Set) (le: A → A → bool) (l: list A) on l ≝
32  match l with
33   [ nil ⇒ true
34   | (cons x l') ⇒
35      match l' with
36       [ nil ⇒ true
37       | (cons y l'') ⇒
38           le x y \land ordered A le l'
39       ]
40   ].
41   
42 let rec insert (A:Set) (le: A → A → bool) x (l: list A) on l ≝
43  match l with
44   [ nil ⇒ [x]
45   | (cons he l') ⇒
46       match le x he with
47        [ true ⇒ x::l
48        | false ⇒ he::(insert A le x l')
49        ]
50   ].
51
52 let rec insertionsort (A:Set) (le: A → A → bool) (l: list A) on l ≝
53  match l with
54   [ nil ⇒ []
55   | (cons he l') ⇒
56       let l'' ≝ insertionsort A le l' in
57        insert A le he l''
58   ].
59
60 lemma ordered_injective:
61   ∀ A:Set. ∀ le:A → A → bool.
62   ∀ l:list A.
63   ordered A le l = true
64   \to ordered A le (tail A l) = true.
65   intros 3 (A le l).
66   elim l
67   [ simplify; reflexivity;
68   | simplify;
69     generalize in match H1;
70     clear H1;
71     elim l1;
72     [ simplify; reflexivity;
73     | cut ((le s s1 \land ordered A le (s1::l2)) = true);
74       [ generalize in match Hcut; 
75         apply andb_elim;
76         elim (le s s1);
77         [ simplify;
78           fold simplify (ordered ? le (s1::l2));
79           intros; assumption;
80         | simplify;
81           intros (Habsurd);
82           apply False_ind;
83           apply (not_eq_true_false);
84           symmetry;
85           assumption
86         ]
87       | exact H2;
88       ]
89     ]
90   ].
91 qed.
92
93
94 lemma insert_sorted:
95   \forall A:Set. \forall le:A\to A\to bool.
96   (\forall a,b:A. le a b = false \to le b a = true) \to
97   \forall l:list A. \forall x:A.
98     ordered A le l = true \to ordered A le (insert A le x l) = true.
99  intros 5 (A le H l x).
100  apply (
101   let rec insert_ind (l: list A) \def
102   match l in list
103   return
104     λli.
105      l = li → ordered ? le li = true →
106       ordered ? le (insert A le x li) = true
107   with
108    [ nil ⇒ ?
109    | (cons he l') ⇒
110        match le x he
111        return
112         λb. le x he = b → l = he::l' →
113          ordered ? le (he::l') = true → ordered ? le
114           (match b with 
115             [ true ⇒ x::he::l'
116             | false ⇒ he::(insert A le x l') ]) = true
117        with
118         [ true ⇒ ?
119         | false ⇒ let res ≝ insert_ind l' in ?         
120         ]
121        (refl_eq ? (le x he))
122    ] (refl_eq ? l) in insert_ind l);
123   
124   intros; simplify;
125   [ rewrite > insert_ind;
126     [ generalize in match (H ? ? H1); clear H1; intro;
127       generalize in match H3; clear H3;
128       elim l'; simplify;
129       [ rewrite > H4;
130         reflexivity
131       | elim (le x s); simplify;
132         [ rewrite > H4;
133           reflexivity
134         | simplify in H3;
135           rewrite > (andb_true_true ? ? H3);
136           reflexivity
137         ]
138       ]
139       
140     | apply (ordered_injective ? ? ? H3)
141     ]
142   | rewrite > H1;
143     rewrite > H3;
144     reflexivity
145   | reflexivity
146   ].
147 qed.
148   
149 theorem insertionsort_sorted:
150   ∀A:Set.
151   ∀le:A → A → bool.∀eq:A → A → bool.
152   (∀a,b:A. le a b = false → le b a = true) \to
153   ∀l:list A.
154   ordered A le (insertionsort A le l) = true.
155   intros 5 (A le eq le_tot l).
156   elim l;
157   [ simplify;
158     reflexivity;
159   | apply (insert_sorted A le le_tot (insertionsort A le l1) s);
160     assumption;
161   ]
162 qed.