]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/library/list/sort.ma
New proof based on an hand-made functional induction.
[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 theorem insert_ind:
53  ∀A:Set. ∀le: A → A → bool. ∀x:A.
54   ∀P: list A → Prop.
55    ∀l:list A. P (insert A le x l).
56  intros (A le x P H l).
57  apply (
58  let rec insert_ind (l: list A) ≝
59   match l in list return λl.P (insert A le x l) with
60    [ nil ⇒ (? : P [x])
61    | (cons he l') ⇒
62        match le x he return λb.P (match b with [ true ⇒ x::he::l' | false ⇒ he::(insert A le x l') ]) with
63         [ true ⇒ (H2 : P (x::he::l'))
64         | false ⇒ (? : P (he::(insert A le x l')))
65         ]
66    ]
67  in
68   insert_ind l).
69 qed.
70 *)
71
72 let rec insertionsort (A:Set) (le: A → A → bool) (l: list A) on l ≝
73  match l with
74   [ nil ⇒ []
75   | (cons he l') ⇒
76       let l'' ≝ insertionsort A le l' in
77        insert A le he l''
78   ].
79
80 lemma ordered_injective:
81   ∀ A:Set. ∀ le:A → A → bool.
82   ∀ l:list A.
83   ordered A le l = true
84   \to ordered A le (tail A l) = true.
85   intros 3 (A le l).
86   elim l
87   [ simplify; reflexivity;
88   | simplify;
89     generalize in match H1;
90     clear H1;
91     elim l1;
92     [ simplify; reflexivity;
93     | cut ((le s s1 \land ordered A le (s1::l2)) = true);
94       [ generalize in match Hcut; 
95         apply andb_elim;
96         elim (le s s1);
97         [ simplify;
98           fold simplify (ordered ? le (s1::l2));
99           intros; assumption;
100         | simplify;
101           intros (Habsurd);
102           apply False_ind;
103           apply (not_eq_true_false);
104           symmetry;
105           assumption
106         ]
107       | exact H2;
108       ]
109     ]
110   ].
111 qed.
112
113
114 lemma insert_sorted:
115   \forall A:Set. \forall le:A\to A\to bool.
116   (\forall a,b:A. le a b = false \to le b a = true) \to
117   \forall l:list A. \forall x:A.
118     ordered A le l = true \to ordered A le (insert A le x l) = true.
119  intros 5 (A le H l x).
120  apply (
121   let rec insert_ind (l: list A) \def
122   match l in list
123   return
124     λli.
125      l = li → ordered ? le li = true →
126       ordered ? le (insert A le x li) = true
127   with
128    [ nil ⇒ (? : l = [] → ordered ? le [] = true → ordered ? le [x] = true)
129    | (cons he l') ⇒
130        match le x he
131        return
132         λb. le x he = b → l = he::l' →
133          ordered ? le (he::l') = true → ordered ? le
134           (match b with 
135             [ true ⇒ x::he::l'
136             | false ⇒ he::(insert A le x l') ]) = true
137        with
138         [ true ⇒
139             (? :
140               le x he = true →
141                l = he::l' →
142                 ordered ? le (he::l') = true →
143                  ordered ? le (x::he::l') = true)
144         | false ⇒
145            let res ≝ insert_ind l' in
146             (? :                  
147                   le x he = false → l = he::l' →
148                    ordered ? le (he::l') = true →
149                     ordered ? le (he::(insert ? le x l')) = true)           
150         ]
151        (refl_eq ? (le x he))
152    ] (refl_eq ? l) in insert_ind l);
153   
154   intros; simplify;
155   [ rewrite > insert_ind;
156     [ generalize in match (H ? ? H1); clear H1; intro;
157       generalize in match H3; clear H3;
158       elim l'; simplify;
159       [ rewrite > H4;
160         reflexivity
161       | elim (le x s); simplify;
162         [ rewrite > H4;
163           reflexivity
164         | simplify in H3;
165           rewrite > (andb_true_true ? ? H3);
166           reflexivity
167         ]
168       ]
169       
170     | apply (ordered_injective ? ? ? H3)
171     ]
172   | rewrite > H1;
173     rewrite > H3;
174     reflexivity
175   | reflexivity
176   ].
177 qed.
178   
179 theorem insertionsort_sorted:
180   ∀A:Set.
181   ∀le:A → A → bool.∀eq:A → A → bool.
182   (∀a,b:A. le a b = false → le b a = true) \to
183   ∀l:list A.
184   ordered A le (insertionsort A le l) = true.
185   intros 5 (A le eq le_tot l).
186   elim l;
187   [ simplify;
188     reflexivity;
189   | apply (insert_sorted A le le_tot (insertionsort A le l1) s);
190     assumption;
191   ]
192 qed.