]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/library/nat/minimization.ma
New entries in nat: factorial.ma minimization.ma primes.ma primes1.ma
[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 include "datatypes/bool.ma".
19
20 let rec max i f \def
21   match (f i) with 
22   [ true \Rightarrow i
23   | false \Rightarrow 
24       match i with 
25       [ O \Rightarrow O
26       | (S j) \Rightarrow max j f ]].
27
28 theorem max_O_f : \forall f: nat \to bool. max O f = O.
29 intro. simplify.
30 elim f O.
31 simplify.reflexivity.
32 simplify.reflexivity.
33 qed. 
34
35 theorem max_S_max : \forall f: nat \to bool. \forall n:nat.
36 (f (S n) = true \land max (S n) f = (S n)) \lor 
37 (f (S n) = false \land max (S n) f = max n f).
38 intros.simplify.elim (f (S n)).
39 simplify.left.split.reflexivity.reflexivity.
40 simplify.right.split.reflexivity.reflexivity.
41 qed.
42
43 definition max_spec \def \lambda f:nat \to bool.\lambda n: nat.
44 ex nat (\lambda i:nat. (le i n) \land (f i = true)) \to
45 (f n) = true \land (\forall i. i < n \to (f i = false)).
46
47 (* perche' si blocca per mezzo minuto qui ??? *)
48 theorem f_max_true : \forall f:nat \to bool. \forall n:nat.
49 ex nat (\lambda i:nat. (le i n) \land (f i = true)) \to f (max n f) = true. 
50 intros 2.
51 elim n.elim H.elim H1.generalize in match H3.
52 apply le_n_O_elim a H2.intro.simplify.rewrite > H4.
53 simplify.assumption.
54 simplify.
55 apply bool_ind (\lambda b:bool.
56 (f (S n1) = b) \to (f ([\lambda b:bool.nat] match b in bool with
57 [ true \Rightarrow (S n1)
58 | false  \Rightarrow (max n1 f)])) = true) ? ? ?.
59 reflexivity.
60 simplify.intro.assumption.
61 simplify.intro.apply H.
62 elim H1.elim H3.generalize in match H5.
63 apply le_n_Sm_elim a n1 H4.
64 intros.
65 apply ex_intro nat ? a.
66 split.apply le_S_S_to_le.assumption.assumption.
67 intros.apply False_ind.apply not_eq_true_false ?.
68 rewrite < H2.rewrite < H7.rewrite > H6. reflexivity.
69 qed.
70
71 theorem lt_max_to_false : \forall f:nat \to bool. 
72 \forall n,m:nat. (max n f) < m \to m \leq n \to f m = false.
73 intros 2.
74 elim n.absurd le m O.assumption.
75 cut O < m.apply lt_O_n_elim m Hcut.exact not_le_Sn_O.
76 rewrite < max_O_f f.assumption.
77 generalize in match H1.
78 (* ?? non posso generalizzare su un goal implicativo ?? *)
79 elim max_S_max f n1.
80 elim H3.
81 absurd m \le S n1.assumption.
82 apply lt_to_not_le.rewrite < H6.assumption.
83 elim H3.
84 apply le_n_Sm_elim m n1 H2.
85 intro.
86 apply H.rewrite < H6.assumption.
87 apply le_S_S_to_le.assumption.
88 intro.rewrite > H7.assumption.
89 qed.
90
91 let rec min_aux off n f \def
92   match f (n-off) with 
93   [ true \Rightarrow (n-off)
94   | false \Rightarrow 
95       match off with
96       [ O \Rightarrow n
97       | (S p) \Rightarrow min_aux p n f]].
98
99 definition min : nat \to (nat \to bool) \to nat \def
100 \lambda n.\lambda f. min_aux n n f.
101
102 theorem min_aux_O_f: \forall f:nat \to bool. \forall i :nat.
103 min_aux O i f = i.
104 intros.simplify.rewrite < minus_n_O.
105 elim f i.
106 simplify.reflexivity.
107 simplify.reflexivity.
108 qed.
109
110 theorem min_O_f : \forall f:nat \to bool.
111 min O f = O.
112 intro.apply min_aux_O_f f O.
113 qed.
114
115 theorem min_aux_S : \forall f: nat \to bool. \forall i,n:nat.
116 (f (n -(S i)) = true \land min_aux (S i) n f = (n - (S i))) \lor 
117 (f (n -(S i)) = false \land min_aux (S i) n f = min_aux i n f).
118 intros.simplify.elim (f (n - (S i))).
119 simplify.left.split.reflexivity.reflexivity.
120 simplify.right.split.reflexivity.reflexivity.
121 qed.
122
123 theorem f_min_aux_true: \forall f:nat \to bool. \forall off,m:nat.
124 ex nat (\lambda i:nat. (le (m-off) i) \land (le i m) \land (f i = true)) \to
125 f (min_aux off m f) = true. 
126 intros 2.
127 elim off.elim H.elim H1.elim H2.
128 cut a = m.
129 rewrite > min_aux_O_f f.rewrite < Hcut.assumption.
130 apply antisym_le a m .assumption.rewrite > minus_n_O m.assumption.
131 simplify.
132 apply bool_ind (\lambda b:bool.
133 (f (m-(S n)) = b) \to (f ([\lambda b:bool.nat] match b in bool with
134 [ true \Rightarrow m-(S n)
135 | false  \Rightarrow (min_aux n m f)])) = true) ? ? ?.
136 reflexivity.
137 simplify.intro.assumption.
138 simplify.intro.apply H.
139 elim H1.elim H3.elim H4.
140 elim (le_to_or_lt_eq (m-(S n)) a H6). 
141 apply ex_intro nat ? a.
142 split.split.
143 apply lt_minus_S_n_to_le_minus_n.assumption.
144 assumption.assumption.
145 absurd f a = false.rewrite < H8.assumption.
146 rewrite > H5.
147 apply not_eq_true_false.
148 qed.
149
150 theorem lt_min_aux_to_false : \forall f:nat \to bool. 
151 \forall n,off,m:nat. (n-off) \leq m \to m < (min_aux off n f) \to f m = false.
152 intros 3.
153 elim off.absurd le n m.rewrite > minus_n_O.assumption.
154 apply lt_to_not_le.rewrite < min_aux_O_f f n.assumption.
155 generalize in match H1.
156 elim min_aux_S f n1 n.
157 elim H3.
158 absurd n - S n1 \le m.assumption.
159 apply lt_to_not_le.rewrite < H6.assumption.
160 elim H3.
161 elim le_to_or_lt_eq (n -(S n1)) m.
162 apply H.apply lt_minus_S_n_to_le_minus_n.assumption.
163 rewrite < H6.assumption.assumption.
164 rewrite < H7.assumption.
165 qed.
166
167 theorem le_min_aux : \forall f:nat \to bool. 
168 \forall n,off:nat. (n-off) \leq (min_aux off n f).
169 intros 3.
170 elim off.rewrite < minus_n_O.
171 rewrite > min_aux_O_f f n.apply le_n.
172 elim min_aux_S f n1 n.
173 elim H1.rewrite > H3.apply le_n.
174 elim H1.rewrite > H3.
175 apply trans_le (n-(S n1)) (n-n1) ?.
176 apply monotonic_le_minus_r.
177 apply le_n_Sn.
178 assumption.
179 qed.
180