set "baseuri" "cic:/matita/nat/minimization".
include "nat/minus.ma".
-include "datatypes/bool.ma".
let rec max i f \def
match (f i) with
simplify.right.split.reflexivity.reflexivity.
qed.
+theorem le_max_n : \forall f: nat \to bool. \forall n:nat.
+max n f \le n.
+intros.elim n.rewrite > max_O_f.apply le_n.
+simplify.elim (f (S n1)).simplify.apply le_n.
+simplify.apply le_S.assumption.
+qed.
+
+theorem le_to_le_max : \forall f: nat \to bool. \forall n,m:nat.
+n\le m \to max n f \le max m f.
+intros.elim H.
+apply le_n.
+apply trans_le ? (max n1 f).apply H2.
+cut (f (S n1) = true \land max (S n1) f = (S n1)) \lor
+(f (S n1) = false \land max (S n1) f = max n1 f).
+elim Hcut.elim H3.
+rewrite > H5.
+apply le_S.apply le_max_n.
+elim H3.rewrite > H5.apply le_n.
+apply max_S_max.
+qed.
+
+theorem f_m_to_le_max: \forall f: nat \to bool. \forall n,m:nat.
+m\le n \to f m = true \to m \le max n f.
+intros 3.elim n.apply le_n_O_elim m H.
+apply le_O_n.
+apply le_n_Sm_elim m n1 H1.
+intro.apply trans_le ? (max n1 f).
+apply H.apply le_S_S_to_le.assumption.assumption.
+apply le_to_le_max.apply le_n_Sn.
+intro.simplify.rewrite < H3.
+rewrite > H2.simplify.apply le_n.
+qed.
+
definition max_spec \def \lambda f:nat \to bool.\lambda n: nat.
ex nat (\lambda i:nat. (le i n) \land (f i = true)) \to
(f n) = true \land (\forall i. i < n \to (f i = false)).
-(* perche' si blocca per mezzo minuto qui ??? *)
theorem f_max_true : \forall f:nat \to bool. \forall n:nat.
ex nat (\lambda i:nat. (le i n) \land (f i = true)) \to f (max n f) = true.
intros 2.
(f (S n1) = b) \to (f ([\lambda b:bool.nat] match b in bool with
[ true \Rightarrow (S n1)
| false \Rightarrow (max n1 f)])) = true) ? ? ?.
-reflexivity.
simplify.intro.assumption.
simplify.intro.apply H.
elim H1.elim H3.generalize in match H5.
split.apply le_S_S_to_le.assumption.assumption.
intros.apply False_ind.apply not_eq_true_false ?.
rewrite < H2.rewrite < H7.rewrite > H6. reflexivity.
+reflexivity.
qed.
theorem lt_max_to_false : \forall f:nat \to bool.
(f (m-(S n)) = b) \to (f ([\lambda b:bool.nat] match b in bool with
[ true \Rightarrow m-(S n)
| false \Rightarrow (min_aux n m f)])) = true) ? ? ?.
-reflexivity.
simplify.intro.assumption.
simplify.intro.apply H.
elim H1.elim H3.elim H4.
absurd f a = false.rewrite < H8.assumption.
rewrite > H5.
apply not_eq_true_false.
+reflexivity.
qed.
theorem lt_min_aux_to_false : \forall f:nat \to bool.
elim H3.
elim le_to_or_lt_eq (n -(S n1)) m.
apply H.apply lt_minus_S_n_to_le_minus_n.assumption.
-rewrite < H6.assumption.assumption.
+rewrite < H6.assumption.
rewrite < H7.assumption.
+assumption.
qed.
theorem le_min_aux : \forall f:nat \to bool.