]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/library/nat/minimization.ma
ocaml 3.09 transition
[helm.git] / helm / matita / library / nat / minimization.ma
1 (**************************************************************************)
2 (*       ___                                                                *)
3 (*      ||M||                                                             *)
4 (*      ||A||       A project by Andrea Asperti                           *)
5 (*      ||T||                                                             *)
6 (*      ||I||       Developers:                                           *)
7 (*      ||T||       A.Asperti, C.Sacerdoti Coen,                          *)
8 (*      ||A||       E.Tassi, S.Zacchiroli                                 *)
9 (*      \   /                                                             *)
10 (*       \ /        Matita is distributed under the terms of the          *)
11 (*        v         GNU Lesser General Public License Version 2.1         *)
12 (*                                                                        *)
13 (**************************************************************************)
14
15 set "baseuri" "cic:/matita/nat/minimization".
16
17 include "nat/minus.ma".
18
19 let rec max i f \def
20   match (f i) with 
21   [ true \Rightarrow i
22   | false \Rightarrow 
23       match i with 
24       [ O \Rightarrow O
25       | (S j) \Rightarrow max j f ]].
26
27 theorem max_O_f : \forall f: nat \to bool. max O f = O.
28 intro. simplify.
29 elim (f O).
30 simplify.reflexivity.
31 simplify.reflexivity.
32 qed. 
33
34 theorem max_S_max : \forall f: nat \to bool. \forall n:nat.
35 (f (S n) = true \land max (S n) f = (S n)) \lor 
36 (f (S n) = false \land max (S n) f = max n f).
37 intros.simplify.elim (f (S n)).
38 simplify.left.split.reflexivity.reflexivity.
39 simplify.right.split.reflexivity.reflexivity.
40 qed.
41
42 theorem le_max_n : \forall f: nat \to bool. \forall n:nat.
43 max n f \le n.
44 intros.elim n.rewrite > max_O_f.apply le_n.
45 simplify.elim (f (S n1)).simplify.apply le_n.
46 simplify.apply le_S.assumption.
47 qed.
48
49 theorem le_to_le_max : \forall f: nat \to bool. \forall n,m:nat.
50 n\le m  \to max n f \le max m f.
51 intros.elim H.
52 apply le_n.
53 apply (trans_le ? (max n1 f)).apply H2.
54 cut ((f (S n1) = true \land max (S n1) f = (S n1)) \lor 
55 (f (S n1) = false \land max (S n1) f = max n1 f)).
56 elim Hcut.elim H3.
57 rewrite > H5.
58 apply le_S.apply le_max_n.
59 elim H3.rewrite > H5.apply le_n.
60 apply max_S_max.
61 qed.
62
63 theorem f_m_to_le_max: \forall f: nat \to bool. \forall n,m:nat.
64 m\le n \to f m = true \to m \le max n f.
65 intros 3.elim n.apply (le_n_O_elim m H).
66 apply le_O_n.
67 apply (le_n_Sm_elim m n1 H1).
68 intro.apply (trans_le ? (max n1 f)).
69 apply H.apply le_S_S_to_le.assumption.assumption.
70 apply le_to_le_max.apply le_n_Sn.
71 intro.simplify.rewrite < H3. 
72 rewrite > H2.simplify.apply le_n.
73 qed.
74
75
76 definition max_spec \def \lambda f:nat \to bool.\lambda n: nat.
77 \exists i. (le i n) \land (f i = true) \to
78 (f n) = true \land (\forall i. i < n \to (f i = false)).
79
80 theorem f_max_true : \forall f:nat \to bool. \forall n:nat.
81 (\exists i:nat. le i n \land f i = true) \to f (max n f) = true. 
82 intros 2.
83 elim n.elim H.elim H1.generalize in match H3.
84 apply (le_n_O_elim a H2).intro.simplify.rewrite > H4.
85 simplify.assumption.
86 simplify.
87 apply (bool_ind (\lambda b:bool.
88 (f (S n1) = b) \to (f (match b in bool with
89 [ true \Rightarrow (S n1)
90 | false  \Rightarrow (max n1 f)])) = true)).
91 simplify.intro.assumption.
92 simplify.intro.apply H.
93 elim H1.elim H3.generalize in match H5.
94 apply (le_n_Sm_elim a n1 H4).
95 intros.
96 apply (ex_intro nat ? a).
97 split.apply le_S_S_to_le.assumption.assumption.
98 intros.apply False_ind.apply not_eq_true_false.
99 rewrite < H2.rewrite < H7.rewrite > H6. reflexivity.
100 reflexivity.
101 qed.
102
103 theorem lt_max_to_false : \forall f:nat \to bool. 
104 \forall n,m:nat. (max n f) < m \to m \leq n \to f m = false.
105 intros 2.
106 elim n.absurd (le m O).assumption.
107 cut (O < m).apply (lt_O_n_elim m Hcut).exact not_le_Sn_O.
108 rewrite < (max_O_f f).assumption.
109 generalize in match H1.
110 elim (max_S_max f n1).
111 elim H3.
112 absurd (m \le S n1).assumption.
113 apply lt_to_not_le.rewrite < H6.assumption.
114 elim H3.
115 apply (le_n_Sm_elim m n1 H2).
116 intro.
117 apply H.rewrite < H6.assumption.
118 apply le_S_S_to_le.assumption.
119 intro.rewrite > H7.assumption.
120 qed.
121
122 let rec min_aux off n f \def
123   match f (n-off) with 
124   [ true \Rightarrow (n-off)
125   | false \Rightarrow 
126       match off with
127       [ O \Rightarrow n
128       | (S p) \Rightarrow min_aux p n f]].
129
130 definition min : nat \to (nat \to bool) \to nat \def
131 \lambda n.\lambda f. min_aux n n f.
132
133 theorem min_aux_O_f: \forall f:nat \to bool. \forall i :nat.
134 min_aux O i f = i.
135 intros.simplify.rewrite < minus_n_O.
136 elim (f i).reflexivity.
137 simplify.reflexivity.
138 qed.
139
140 theorem min_O_f : \forall f:nat \to bool.
141 min O f = O.
142 intro.apply (min_aux_O_f f O).
143 qed.
144
145 theorem min_aux_S : \forall f: nat \to bool. \forall i,n:nat.
146 (f (n -(S i)) = true \land min_aux (S i) n f = (n - (S i))) \lor 
147 (f (n -(S i)) = false \land min_aux (S i) n f = min_aux i n f).
148 intros.simplify.elim (f (n - (S i))).
149 simplify.left.split.reflexivity.reflexivity.
150 simplify.right.split.reflexivity.reflexivity.
151 qed.
152
153 theorem f_min_aux_true: \forall f:nat \to bool. \forall off,m:nat.
154 (\exists i. le (m-off) i \land le i m \land f i = true) \to
155 f (min_aux off m f) = true. 
156 intros 2.
157 elim off.elim H.elim H1.elim H2.
158 cut (a = m).
159 rewrite > (min_aux_O_f f).rewrite < Hcut.assumption.
160 apply (antisym_le a m).assumption.rewrite > (minus_n_O m).assumption.
161 simplify.
162 apply (bool_ind (\lambda b:bool.
163 (f (m-(S n)) = b) \to (f (match b in bool with
164 [ true \Rightarrow m-(S n)
165 | false  \Rightarrow (min_aux n m f)])) = true)).
166 simplify.intro.assumption.
167 simplify.intro.apply H.
168 elim H1.elim H3.elim H4.
169 elim (le_to_or_lt_eq (m-(S n)) a H6). 
170 apply (ex_intro nat ? a).
171 split.split.
172 apply lt_minus_S_n_to_le_minus_n.assumption.
173 assumption.assumption.
174 absurd (f a = false).rewrite < H8.assumption.
175 rewrite > H5.
176 apply not_eq_true_false.
177 reflexivity.
178 qed.
179
180 theorem lt_min_aux_to_false : \forall f:nat \to bool. 
181 \forall n,off,m:nat. (n-off) \leq m \to m < (min_aux off n f) \to f m = false.
182 intros 3.
183 elim off.absurd (le n m).rewrite > minus_n_O.assumption.
184 apply lt_to_not_le.rewrite < (min_aux_O_f f n).assumption.
185 generalize in match H1.
186 elim (min_aux_S f n1 n).
187 elim H3.
188 absurd (n - S n1 \le m).assumption.
189 apply lt_to_not_le.rewrite < H6.assumption.
190 elim H3.
191 elim (le_to_or_lt_eq (n -(S n1)) m).
192 apply H.apply lt_minus_S_n_to_le_minus_n.assumption.
193 rewrite < H6.assumption.
194 rewrite < H7.assumption.
195 assumption.
196 qed.
197
198 theorem le_min_aux : \forall f:nat \to bool. 
199 \forall n,off:nat. (n-off) \leq (min_aux off n f).
200 intros 3.
201 elim off.rewrite < minus_n_O.
202 rewrite > (min_aux_O_f f n).apply le_n.
203 elim (min_aux_S f n1 n).
204 elim H1.rewrite > H3.apply le_n.
205 elim H1.rewrite > H3.
206 apply (trans_le (n-(S n1)) (n-n1)).
207 apply monotonic_le_minus_r.
208 apply le_n_Sn.
209 assumption.
210 qed.
211
212 theorem le_min_aux_r : \forall f:nat \to bool. 
213 \forall n,off:nat. (min_aux off n f) \le n.
214 intros.
215 elim off.simplify.rewrite < minus_n_O.
216 elim (f n).simplify.apply le_n.
217 simplify.apply le_n.
218 simplify.elim (f (n -(S n1))).
219 simplify.apply le_plus_to_minus.
220 rewrite < sym_plus.apply le_plus_n.
221 simplify.assumption.
222 qed.