1 (**************************************************************************)
4 (* ||A|| A project by Andrea Asperti *)
6 (* ||I|| Developers: *)
7 (* ||T|| A.Asperti, C.Sacerdoti Coen, *)
8 (* ||A|| E.Tassi, S.Zacchiroli *)
10 (* \ / This file is distributed under the terms of the *)
11 (* v GNU Lesser General Public License Version 2.1 *)
13 (**************************************************************************)
15 set "baseuri" "cic:/matita/library_autobatch/nat/map_iter_p.ma".
17 include "auto/nat/permutation.ma".
18 include "auto/nat/count.ma".
20 let rec map_iter_p n p (g:nat \to nat) (a:nat) f \def
25 [true \Rightarrow f (g (S k)) (map_iter_p k p g a f)
26 |false \Rightarrow map_iter_p k p g a f]
29 theorem eq_map_iter_p: \forall g1,g2:nat \to nat.
30 \forall p:nat \to bool.
31 \forall f:nat \to nat \to nat. \forall a,n:nat.
32 (\forall m:nat. m \le n \to g1 m = g2 m) \to
33 map_iter_p n p g1 a f = map_iter_p n p g2 a f.
65 (* useful since simplify simpifies too much *)
67 theorem map_iter_p_O: \forall p.\forall g.\forall f. \forall a:nat.
68 map_iter_p O p g a f = a.
71 (*simplify.reflexivity.*)
74 theorem map_iter_p_S_true: \forall p.\forall g.\forall f. \forall a,n:nat.
76 map_iter_p (S n) p g a f = f (g (S n)) (map_iter_p n p g a f).
77 intros.simplify.rewrite > H.reflexivity.
80 theorem map_iter_p_S_false: \forall p.\forall g.\forall f. \forall a,n:nat.
82 map_iter_p (S n) p g a f = map_iter_p n p g a f.
83 intros.simplify.rewrite > H.reflexivity.
86 (* map_iter examples *)
87 definition pi_p \def \lambda p. \lambda n.
88 map_iter_p n p (\lambda n.n) (S O) times.
90 lemma pi_p_S: \forall n.\forall p.
93 [true \Rightarrow (S n)*(pi_p p n)
94 |false \Rightarrow (pi_p p n)
99 lemma lt_O_pi_p: \forall n.\forall p.
108 [ change with (O < (S n1)*(pi_p p n1)).
110 (*rewrite >(times_n_O n1).
120 let rec card n p \def
124 (bool_to_nat (p (S m))) + (card m p)].
126 lemma count_card: \forall p.\forall n.
127 p O = false \to count (S n) p = card n p.
137 (*qui autobatch non chiude un goal chiuso invece dal solo assumption*)
142 lemma count_card1: \forall p.\forall n.
143 p O = false \to p n = false \to count n p = card n p.
151 | intros.rewrite > (count_card ? ? H).
159 lemma a_times_pi_p: \forall p. \forall a,n.
160 exp a (card n p) * pi_p p n = map_iter_p n p (\lambda n.a*n) (S O) times.
167 apply (bool_elim ? (p (S n1)))
170 (a*exp a (card n1 p) * ((S n1) * (pi_p p n1)) =
171 a*(S n1)*map_iter_p n1 p (\lambda n.a*n) (S O) times).
175 (*la chiamata di autobatch in questo punto dopo circa 8 minuti non aveva
176 * ancora generato un risultato
183 definition permut_p \def \lambda f. \lambda p:nat\to bool. \lambda n.
184 \forall i. i \le n \to p i = true \to ((f i \le n \land p (f i) = true)
185 \land (\forall j. p j = true \to j \le n \to i \neq j \to (f i \neq f j))).
187 definition extentional_eq_n \def \lambda f,g:nat \to nat.\lambda n.
188 \forall x. x \le n \to f x = g x.
190 lemma extentional_eq_n_to_permut_p: \forall f,g. \forall p. \forall n.
191 extentional_eq_n f g n\to permut_p f p n \to permut_p g p n.
199 [ rewrite < (H i H2).
201 | rewrite < (H i H2).
207 apply (H5 j H6 H7 H8).
214 theorem permut_p_compose: \forall f,g.\forall p.\forall n.
215 permut_p f p n \to permut_p g p n \to permut_p (compose ? ? ? g f) p n.
221 elim (H1 (f i) H6 H7).
235 [ elim (H j H13 H12).
240 | elim (H j H13 H12).
243 | apply (H5 j H12 H13 H14)
249 theorem permut_p_S_to_permut_p: \forall f.\forall p.\forall n.
250 permut_p f p (S n) \to (f (S n)) = (S n) \to permut_p f p n.
255 [ elim (H i (le_S i n H2) H3).
258 elim (le_to_or_lt_eq (f i) (S n))
260 (*apply le_S_S_to_le.
262 | absurd (f i = (S n))
270 | unfold.intro.rewrite > H8 in H2.
271 apply (not_le_Sn_n n).rewrite < H9.
282 elim (H i (le_S i n H2) H3).
293 lemma permut_p_transpose: \forall p.\forall i,j,n.
294 i \le n \to j \le n \to p i = p j \to
295 permut_p (transpose i j) p n.
301 apply (eqb_elim i1 i)
303 apply (eqb_elim i1 j)
304 [ simplify.intro.assumption
305 | simplify.intro.assumption
308 apply (eqb_elim i1 j)
309 [ simplify.intro.assumption
310 | simplify.intro.assumption
314 apply (eqb_elim i1 i)
316 apply (eqb_elim i1 j)
328 apply (eqb_elim i1 j)
344 apply (injective_transpose ? ? ? ? H8)*)
349 theorem eq_map_iter_p_k: \forall f,g.\forall p.\forall a,k,n:nat.
350 p (S n-k) = true \to (\forall i. (S n)-k < i \to i \le (S n) \to (p i) = false) \to
351 map_iter_p (S n) p g a f = map_iter_p (S n-k) p g a f.
355 (*rewrite < minus_n_O.
357 | apply (nat_case n1)
359 rewrite > map_iter_p_S_false
369 rewrite > map_iter_p_S_false
390 theorem eq_map_iter_p_a: \forall p.\forall f.\forall g. \forall a,n:nat.
391 (\forall i.i \le n \to p i = false) \to map_iter_p n p g a f = a.
397 | rewrite > map_iter_p_S_false
411 theorem eq_map_iter_p_transpose: \forall p.\forall f.associative nat f \to
412 symmetric2 nat nat f \to \forall g. \forall a,k,n:nat. k < n \to
413 (p (S n) = true) \to (p (n-k)) = true \to (\forall i. n-k < i \to i \le n \to (p i) = false)
414 \to map_iter_p (S n) p g a f = map_iter_p (S n) p (\lambda m. g (transpose (n-k) (S n) m)) a f.
421 (*apply le_to_not_lt.
425 rewrite > (map_iter_p_S_true ? ? ? ? ? H3).
426 rewrite > (map_iter_p_S_true ? ? ? ? ? H3).
427 rewrite > (eq_map_iter_p_k ? ? ? ? ? ? H4 H5).
428 rewrite > (eq_map_iter_p_k ? ? ? ? ? ? H4 H5).
429 generalize in match H4.
432 rewrite > (map_iter_p_S_true ? ? ? ? ? H6).
433 rewrite > (map_iter_p_S_true ? ? ? ? ? H6).
434 rewrite > transpose_i_j_j.
435 rewrite > transpose_i_j_i.
436 cut (map_iter_p (m-k) p g a f =
437 map_iter_p (m-k) p (\lambda x.g (transpose (S(m-k)) (S(S m)) x)) a f)
440 rewrite < H1 in \vdash (? ? (? % ?) ?).
444 | apply eq_map_iter_p.
447 cut (eqb m1 (S (m-k)) =false)
448 [ cut (eqb m1 (S (S m)) =false)
452 | apply not_eq_to_eqb_false.
454 apply (le_to_lt_to_lt ? m)
456 (*apply (trans_le ? (m-k))
465 | apply not_eq_to_eqb_false.
473 (*apply le_S_S_to_le.
479 theorem eq_map_iter_p_transpose1: \forall p.\forall f.associative nat f \to
480 symmetric2 nat nat f \to \forall g. \forall a,k1,k2,n:nat. O < k1 \to k1 < k2 \to k2 \le n \to
481 (p k1) = true \to (p k2) = true \to (\forall i. k1 < i \to i < k2 \to (p i) = false)
482 \to map_iter_p n p g a f = map_iter_p n p (\lambda m. g (transpose k1 k2 m)) a f.
488 (*apply lt_to_not_le.
489 apply (trans_lt ? k1 ? H2 H3)*)
491 | apply (eqb_elim (S n1) k2)
495 cut (k1 = n1 - (n1 -k1))
497 apply (eq_map_iter_p_transpose p f H H1 g a (n1-k1))
498 [ cut (k1 \le n1);autobatch
512 (*apply plus_to_minus.
517 [ apply (bool_elim ? (p (S n1)))
519 rewrite > map_iter_p_S_true
520 [ rewrite > map_iter_p_S_true
521 [ cut ((transpose k1 k2 (S n1)) = (S n1))
525 [ elim (le_to_or_lt_eq ? ? H6)
539 rewrite > (not_eq_to_eqb_false ? ? Hcut).
540 rewrite > (not_eq_to_eqb_false ? ? H4).
548 rewrite > map_iter_p_S_false
549 [ rewrite > map_iter_p_S_false
551 [ elim (le_to_or_lt_eq ? ? H6)
553 | (*l'invocazione di autobatch qui genera segmentation fault*)
574 | apply le_to_not_lt.
583 lemma decidable_n:\forall p.\forall n.
584 (\forall m. m \le n \to (p m) = false) \lor
585 (\exists m. m \le n \land (p m) = true \land
586 \forall i. m < i \to i \le n \to (p i) = false).
589 [ apply (bool_elim ? (p O))
592 apply (ex_intro ? ? O).
603 (*apply le_to_not_lt.
610 apply (le_n_O_elim m H1).
613 | apply (bool_elim ? (p (S n1)))
616 apply (ex_intro ? ? (S n1)).
626 (*apply le_to_not_lt.
633 elim (le_to_or_lt_eq m (S n1) H3)
646 apply (ex_intro ? ? a).
655 elim (le_to_or_lt_eq i (S n1) H9)
659 | apply le_S_S_to_le.
672 lemma decidable_n1:\forall p.\forall n,j. j \le n \to (p j)=true \to
673 (\forall m. j < m \to m \le n \to (p m) = false) \lor
674 (\exists m. j < m \land m \le n \land (p m) = true \land
675 \forall i. m < i \to i \le n \to (p i) = false).
677 elim (decidable_n p n)
678 [ absurd ((p j)=true)
682 apply not_eq_true_false.
690 apply (nat_compare_elim j a)
693 apply (ex_intro ? ? a).
711 (*qui la tattica autobatch non conclude il goal, concluso invece con l'invocazione
712 *della sola tattica assumption
720 apply not_eq_true_false.
725 apply (H6 j H2).assumption*)
731 lemma decidable_n2:\forall p.\forall n,j. j \le n \to (p j)=true \to
732 (\forall m. j < m \to m \le n \to (p m) = false) \lor
733 (\exists m. j < m \land m \le n \land (p m) = true \land
734 \forall i. j < i \to i < m \to (p i) = false).
738 apply (le_n_O_elim j H).
743 (*apply lt_to_not_le.
746 | elim (le_to_or_lt_eq ? ? H1)
749 [ apply (bool_elim ? (p (S n1)))
752 apply (ex_intro ? ? (S n1)).
771 elim (le_to_or_lt_eq ? ? H7)
775 | apply le_S_S_to_le.
788 apply (ex_intro ? ? a).
799 | (*qui autobatch non chiude il goal, chiuso invece mediante l'invocazione
800 *della sola tattica assumption
806 (*apply le_S_S_to_le.
813 | apply le_to_not_lt.
821 (* tutti d spostare *)
822 theorem lt_minus_to_lt_plus:
823 \forall n,m,p. n - m < p \to n < m + p.
825 apply (nat_elim2 ? ? ? ? n m)
831 (*rewrite < minus_n_O.
844 theorem lt_plus_to_lt_minus:
845 \forall n,m,p. m \le n \to n < m + p \to n - m < p.
847 apply (nat_elim2 ? ? ? ? n m)
850 apply (le_n_O_elim ? H).
862 (*apply le_S_S_to_le.
864 | apply le_S_S_to_le.
870 theorem minus_m_minus_mn: \forall n,m. n\le m \to n=m-(m-n).
874 (*apply plus_to_minus.
878 theorem eq_map_iter_p_transpose2: \forall p.\forall f.associative nat f \to
879 symmetric2 nat nat f \to \forall g. \forall a,k,n:nat. O < k \to k \le n \to
880 (p (S n) = true) \to (p k) = true
881 \to map_iter_p (S n) p g a f = map_iter_p (S n) p (\lambda m. g (transpose k (S n) m)) a f.
883 cut (k = (S n)-(S n -k))
884 [ generalize in match H3.
886 generalize in match g.
887 generalize in match H2.
890 (*generalize in match Hcut.clear Hcut.*)
891 (* generalize in match H3.clear H3.*)
892 (* something wrong here
893 rewrite > Hcut in \vdash (?\rarr ?\rarr %). *)
894 apply (nat_elim1 (S n - k)).
896 elim (decidable_n2 p n (S n -m) H4 H6)
897 [ apply (eq_map_iter_p_transpose1 p f H H1 f1 a)
909 | apply le_S_S_to_le.
919 (map_iter_p (S n) p (\lambda i.f1 (transpose a1 (S n) (transpose (S n -m) a1 i))) a f))
920 [ apply (trans_eq ? ?
921 (map_iter_p (S n) p (\lambda i.f1 (transpose a1 (S n) i)) a f))
922 [ cut (a1 = (S n -(S n -a1)))
925 [ apply lt_plus_to_lt_minus
929 | rewrite < sym_plus.
931 (*apply lt_minus_to_lt_plus.
936 (*apply (trans_lt ? (S n -m));
946 (*apply minus_m_minus_mn.
950 | apply (eq_map_iter_p_transpose1 p f H H1)
958 | (*qui autobatch non chiude il goal, chiuso dall'invocazione della singola
964 | apply (trans_eq ? ?
965 (map_iter_p (S n) p (\lambda i.f1 (transpose a1 (S n) (transpose (S n -m) a1 (transpose (S n -(S n -a1)) (S n) i)))) a f))
966 [ cut (a1 = (S n) -(S n -a1))
968 [ apply lt_plus_to_lt_minus
972 | rewrite < sym_plus.
974 (*apply lt_minus_to_lt_plus.
979 (*apply (trans_lt ? (S n -m))
991 (*apply minus_m_minus_mn.
995 | apply eq_map_iter_p.
996 cut (a1 = (S n) -(S n -a1))
1000 rewrite < transpose_i_j_j_i.
1001 rewrite > (transpose_i_j_j_i (S n -m)).
1002 rewrite > (transpose_i_j_j_i a1 (S n)).
1003 rewrite > (transpose_i_j_j_i (S n -m)).
1008 apply (not_le_Sn_n n).
1013 apply (not_le_Sn_n n).
1018 apply (not_le_Sn_n a1).
1019 rewrite < H12 in \vdash (? (? %) ?).
1023 (*apply minus_m_minus_mn.
1031 (*apply minus_m_minus_mn.
1037 theorem eq_map_iter_p_transpose3: \forall p.\forall f.associative nat f \to
1038 symmetric2 nat nat f \to \forall g. \forall a,k,n:nat. O < k \to k \le (S n) \to
1039 (p (S n) = true) \to (p k) = true
1040 \to map_iter_p (S n) p g a f = map_iter_p (S n) p (\lambda m. g (transpose k (S n) m)) a f.
1042 elim (le_to_or_lt_eq ? ? H3)
1043 [ apply (eq_map_iter_p_transpose2 p f H H1 g a k n H2);autobatch
1044 (*[ apply le_S_S_to_le.
1050 apply eq_map_iter_p.
1055 apply transpose_i_i.*)
1059 lemma permut_p_O: \forall p.\forall h.\forall n.
1060 permut_p h p n \to p O = false \to \forall m. (S m) \le n \to p (S m) = true \to O < h(S m).
1062 unfold permut_p in H.
1063 apply not_le_to_lt.unfold.
1065 elim (H (S m) H2 H3).
1067 absurd (p (h (S m)) = true)
1069 | apply (le_n_O_elim ? H4).
1072 (*l'invocazione di autobatch in questo punto genera segmentation fault*)
1073 apply not_eq_true_false.
1074 (*l'invocazione di autobatch in questo punto genera segmentation fault*)
1076 (*l'invocazione di autobatch in questo punto genera segmentation fault*)
1078 (*l'invocazione di autobatch in questo punto genera segmentation fault*)
1083 theorem eq_map_iter_p_permut: \forall p.\forall f.associative nat f \to
1084 symmetric2 nat nat f \to \forall n.\forall g. \forall h.\forall a:nat.
1085 permut_p h p n \to p O = false \to
1086 map_iter_p n p g a f = map_iter_p n p (compose ? ? ? g h) a f .
1092 | apply (bool_elim ? (p (S n1)))
1094 apply (trans_eq ? ? (map_iter_p (S n1) p (\lambda m.g ((transpose (h (S n1)) (S n1)) m)) a f))
1095 [ unfold permut_p in H3.
1096 elim (H3 (S n1) (le_n ?) H5).
1099 apply (eq_map_iter_p_transpose3 p f H H1 g a (h(S n1)) n1)
1100 [ apply (permut_p_O ? ? ? H3 H4);autobatch
1108 | apply (trans_eq ? ? (map_iter_p (S n1) p (\lambda m.
1109 (g(transpose (h (S n1)) (S n1)
1110 (transpose (h (S n1)) (S n1) (h m)))) ) a f))
1111 [ rewrite > (map_iter_p_S_true ? ? ? ? ? H5).
1112 rewrite > (map_iter_p_S_true ? ? ? ? ? H5).
1114 [ rewrite > transpose_i_j_j.
1115 rewrite > transpose_i_j_i.
1116 rewrite > transpose_i_j_j.
1118 | apply (H2 (\lambda m.(g(transpose (h (S n1)) (S n1) m))) ?)
1124 unfold permut_p in H3.
1125 elim (H3 i (le_S ? ? H6) H7).
1128 elim (le_to_or_lt_eq ? ? H10)
1130 rewrite > (not_eq_to_eqb_false ? ? (lt_to_not_eq ? ? H8)).
1131 cut (h i \neq h (S n1))
1132 [ rewrite > (not_eq_to_eqb_false ? ? Hcut).
1135 (*apply le_S_S_to_le.
1140 | apply lt_to_not_eq.
1148 apply (eqb_elim (S n1) (h (S n1)))
1150 absurd (h i = h (S n1))
1157 | apply lt_to_not_eq.
1166 rewrite > (not_eq_to_eqb_false ? ? H12).
1167 rewrite > (eq_to_eqb_true ? ? (refl_eq ? (S n1))).
1169 elim (H3 (S n1) (le_n ? ) H5).
1171 elim (le_to_or_lt_eq ? ? H15)
1173 (*apply le_S_S_to_le.
1185 unfold permut_p in H3.
1187 apply (eqb_elim (h i) (S n1))
1189 apply (eqb_elim (h i) (h (S n1)))
1191 (*NB: qui autobatch non chiude il goal*)
1196 elim (H3 (S n1) (le_n ? ) H5).
1202 apply (eqb_elim (h i) (h (S n1)))
1204 (*NB: qui autobatch non chiude il goal*)
1209 elim (H3 i (le_S ? ? H6) H7).
1220 unfold permut_p in H3.
1221 elim (H3 i (le_S i ? H6) H7).
1222 apply (H13 j H8 (le_S j ? H9) H10).
1223 apply (injective_transpose ? ? ? ? H11)
1228 | apply eq_map_iter_p.
1231 (*rewrite > transpose_transpose.
1236 rewrite > (map_iter_p_S_false ? ? ? ? ? H5).
1237 rewrite > (map_iter_p_S_false ? ? ? ? ? H5).
1240 unfold permut_p in H3.
1242 elim (H3 i (le_S i ? H6) H7).
1246 [ elim (le_to_or_lt_eq ? ? H10)
1248 (*apply le_S_S_to_le.assumption*)
1249 | absurd (p (h i) = true)
1254 (*l'invocazione di autobatch qui genera segmentation fault*)
1255 apply not_eq_true_false.
1256 (*l'invocazione di autobatch qui genera segmentation fault*)
1258 (*l'invocazione di autobatch qui genera segmentation fault*)
1267 | apply (le_S ? ? H13)