]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/library/list/sort.ma
Yet another semantics for simplify.
[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 lemma insert_sorted:
94   \forall A:Set. \forall le:A\to A\to bool.
95   (\forall a,b:A. le a b = false \to le b a = true) \to
96   \forall l:list A. \forall x:A.
97     ordered A le l = true \to ordered A le (insert A le x l) = true.
98   intros 5 (A le H l x).
99   elim l;
100   [ 2: simplify;
101        apply (bool_elim ? (le x s));
102        [ intros;
103          simplify;
104          fold simplify (ordered ? le (s::l1));
105          apply andb_elim;
106          rewrite > H3;
107          assumption;
108        | generalize in match H2;
109          clear H1; clear H2;
110          generalize in match s;
111          clear s;
112          elim l1
113          [ simplify;
114            rewrite > (H x a H2);
115            reflexivity;
116          | simplify in \vdash (? ? (? ? ? %) ?);
117             apply (bool_elim ? (le x s));
118             [ intros;
119               simplify;
120               fold simplify (ordered A le (s::l2));
121               apply andb_elim;
122               rewrite > (H x a H3);
123               simplify;
124               fold simplify (ordered A le (s::l2));
125               apply andb_elim;
126               rewrite > H4;
127               apply (ordered_injective A le (a::s::l2));
128               assumption;
129             | intros;
130               simplify;
131               fold simplify (ordered A le (s::(insert A le x l2)));
132               apply andb_elim;
133               simplify in H2;
134               fold simplify (ordered A le (s::l2)) in H2;
135               generalize in match H2;
136               apply (andb_elim (le a s));
137               elim (le a s);
138               [ change with (ordered A le (s::l2) = true \to ordered A le (s::insert A le x l2) = true);
139                 intros;
140                 apply (H1 s);
141                 assumption;
142               | simplify; intros; assumption
143               ]
144            ]
145          ]
146       ]
147    | simplify; reflexivity;
148    ]
149 qed.
150
151 theorem insertionsort_sorted:
152   ∀A:Set.
153   ∀le:A → A → bool.∀eq:A → A → bool.
154   (∀a,b:A. le a b = false → le b a = true) \to
155   ∀l:list A.
156   ordered A le (insertionsort A le l) = true.
157   intros 5 (A le eq le_tot l).
158   elim l;
159   [ simplify;
160     reflexivity;
161   | apply (insert_sorted A le le_tot (insertionsort A le l1) s);
162     assumption;
163   ]
164 qed.
165
166 (*
167 theorem insertionsort_correct:
168   ∀A:Set.
169   ∀le:A → A → bool.∀eq:A → A → bool.
170   (∀a,b:A. le a b = false → le b a = true) \to
171   ∀l,l':list A.
172   l' = insertionsort A le l
173   \to ordered A le l' = true
174     ∧ ((\forall x:A. mem A eq x l = true \to mem A eq x l' = true)
175         ∧ (\forall x:A. mem A eq x l = true \to mem A eq x l' = true)).
176   intros;
177   split;
178   [ rewrite > H1; 
179     apply (insertionsort_sorted A le eq H);
180   | split;
181     [ TO BE CONTINUED ...
182 *)