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 include "nat/permutation.ma".
16 include "nat/count.ma".
18 let rec map_iter_p n p (g:nat \to nat) (a:nat) f \def
23 [true \Rightarrow f (g (S k)) (map_iter_p k p g a f)
24 |false \Rightarrow map_iter_p k p g a f]
27 theorem eq_map_iter_p: \forall g1,g2:nat \to nat.
28 \forall p:nat \to bool.
29 \forall f:nat \to nat \to nat. \forall a,n:nat.
30 (\forall m:nat. m \le n \to g1 m = g2 m) \to
31 map_iter_p n p g1 a f = map_iter_p n p g2 a f.
33 [simplify.reflexivity.
34 |simplify.elim (p (S n1))
37 |simplify.apply H.intros.apply H1.
40 |simplify.apply H.intros.apply H1.
46 (* useful since simplify simpifies too much *)
48 theorem map_iter_p_O: \forall p.\forall g.\forall f. \forall a:nat.
49 map_iter_p O p g a f = a.
50 intros.simplify.reflexivity.
53 theorem map_iter_p_S_true: \forall p.\forall g.\forall f. \forall a,n:nat.
55 map_iter_p (S n) p g a f = f (g (S n)) (map_iter_p n p g a f).
56 intros.simplify.rewrite > H.reflexivity.
59 theorem map_iter_p_S_false: \forall p.\forall g.\forall f. \forall a,n:nat.
61 map_iter_p (S n) p g a f = map_iter_p n p g a f.
62 intros.simplify.rewrite > H.reflexivity.
65 (* map_iter examples *)
66 definition pi_p \def \lambda p. \lambda n.
67 map_iter_p n p (\lambda n.n) (S O) times.
69 lemma pi_p_S: \forall n.\forall p.
72 [true \Rightarrow (S n)*(pi_p p n)
73 |false \Rightarrow (pi_p p n)
78 lemma lt_O_pi_p: \forall n.\forall p.
84 [change with (O < (S n1)*(pi_p p n1)).
85 rewrite >(times_n_O n1).
86 apply lt_times[apply le_n|assumption]
96 (bool_to_nat (p (S m))) + (card m p)].
98 lemma count_card: \forall p.\forall n.
99 p O = false \to count (S n) p = card n p.
101 [simplify.rewrite > H. reflexivity
104 apply eq_f.assumption
108 lemma count_card1: \forall p.\forall n.
109 p O = false \to p n = false \to count n p = card n p.
110 intros 3.apply (nat_case n)
111 [intro.simplify.rewrite > H. reflexivity
112 |intros.rewrite > (count_card ? ? H).
113 simplify.rewrite > H1.reflexivity
117 lemma a_times_pi_p: \forall p. \forall a,n.
118 exp a (card n p) * pi_p p n = map_iter_p n p (\lambda n.a*n) (S O) times.
120 [simplify.reflexivity
121 |simplify.apply (bool_elim ? (p (S n1)))
124 (a*exp a (card n1 p) * ((S n1) * (pi_p p n1)) =
125 a*(S n1)*map_iter_p n1 p (\lambda n.a*n) (S O) times).
133 definition permut_p \def \lambda f. \lambda p:nat\to bool. \lambda n.
134 \forall i. i \le n \to p i = true \to ((f i \le n \land p (f i) = true)
135 \land (\forall j. p j = true \to j \le n \to i \neq j \to (f i \neq f j))).
137 definition extentional_eq_n \def \lambda f,g:nat \to nat.\lambda n.
138 \forall x. x \le n \to f x = g x.
140 lemma extentional_eq_n_to_permut_p: \forall f,g. \forall p. \forall n.
141 extentional_eq_n f g n\to permut_p f p n \to permut_p g p n.
142 intros.unfold permut_p.
147 [rewrite < (H i H2).assumption
148 |rewrite < (H i H2).assumption
151 unfold.intro.apply (H5 j H6 H7 H8).
153 rewrite > (H j H7).assumption
157 theorem permut_p_compose: \forall f,g.\forall p.\forall n.
158 permut_p f p n \to permut_p g p n \to permut_p (compose ? ? ? g f) p n.
159 intros.unfold permut_p.intros.
162 elim (H1 (f i) H6 H7).
166 [unfold compose.assumption
167 |unfold compose.rewrite < H11.reflexivity
172 [elim (H j H13 H12).elim H15.rewrite < H18.reflexivity
173 |elim (H j H13 H12).elim H15.assumption.
174 |apply (H5 j H12 H13 H14)
179 theorem permut_p_S_to_permut_p: \forall f.\forall p.\forall n.
180 permut_p f p (S n) \to (f (S n)) = (S n) \to permut_p f p n.
185 [elim (H i (le_S i n H2) H3).split
187 elim (le_to_or_lt_eq (f i) (S n))
188 [apply le_S_S_to_le.assumption
189 |absurd (f i = (S n))
193 [rewrite < H8.assumption
196 apply (not_le_Sn_n n).rewrite < H9.assumption
204 elim (H i (le_S i n H2) H3).
206 [assumption|apply le_S.assumption|assumption]
210 lemma permut_p_transpose: \forall p.\forall i,j,n.
211 i \le n \to j \le n \to p i = p j \to
212 permut_p (transpose i j) p n.
213 unfold permut_p.intros.
217 apply (eqb_elim i1 i)
219 apply (eqb_elim i1 j)
220 [simplify.intro.assumption
221 |simplify.intro.assumption
224 apply (eqb_elim i1 j)
225 [simplify.intro.assumption
226 |simplify.intro.assumption
230 apply (eqb_elim i1 i)
232 apply (eqb_elim i1 j)
233 [simplify.intro.rewrite < H6.assumption
234 |simplify.intro.rewrite < H2.rewrite < H5.assumption
237 apply (eqb_elim i1 j)
238 [simplify.intro.rewrite > H2.rewrite < H6.assumption
239 |simplify.intro.assumption
245 apply (injective_transpose ? ? ? ? H8).
249 theorem eq_map_iter_p_k: \forall f,g.\forall p.\forall a,k,n:nat.
250 p (S n-k) = true \to (\forall i. (S n)-k < i \to i \le (S n) \to (p i) = false) \to
251 map_iter_p (S n) p g a f = map_iter_p (S n-k) p g a f.
254 [rewrite < minus_n_O.reflexivity
257 rewrite > map_iter_p_S_false
259 |apply H2[simplify.apply lt_O_S.|apply le_n]
262 rewrite > map_iter_p_S_false
270 |apply H2[autobatch|apply le_n]
276 theorem eq_map_iter_p_a: \forall p.\forall f.\forall g. \forall a,n:nat.
277 (\forall i.i \le n \to p i = false) \to map_iter_p n p g a f = a.
280 [simplify.reflexivity
281 |rewrite > map_iter_p_S_false
284 apply H1.apply le_S.assumption
290 theorem eq_map_iter_p_transpose: \forall p.\forall f.associative nat f \to
291 symmetric2 nat nat f \to \forall g. \forall a,k,n:nat. k < n \to
292 (p (S n) = true) \to (p (n-k)) = true \to (\forall i. n-k < i \to i \le n \to (p i) = false)
293 \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.
296 [intro.absurd (k < O)
297 [assumption|apply le_to_not_lt.apply le_O_n]
299 rewrite > (map_iter_p_S_true ? ? ? ? ? H3).
300 rewrite > (map_iter_p_S_true ? ? ? ? ? H3).
301 rewrite > (eq_map_iter_p_k ? ? ? ? ? ? H4 H5).
302 rewrite > (eq_map_iter_p_k ? ? ? ? ? ? H4 H5).
303 generalize in match H4.
306 rewrite > (map_iter_p_S_true ? ? ? ? ? H6).
307 rewrite > (map_iter_p_S_true ? ? ? ? ? H6).
308 rewrite > transpose_i_j_j.
309 rewrite > transpose_i_j_i.
310 cut (map_iter_p (m-k) p g a f =
311 map_iter_p (m-k) p (\lambda x.g (transpose (S(m-k)) (S(S m)) x)) a f)
314 rewrite < H1 in \vdash (? ? (? % ?) ?).
317 |apply eq_map_iter_p.
318 intros.unfold transpose.
319 cut (eqb m1 (S (m-k)) =false)
320 [cut (eqb m1 (S (S m)) =false)
324 |apply not_eq_to_eqb_false.
326 apply (le_to_lt_to_lt ? m)
327 [apply (trans_le ? (m-k))
328 [assumption|autobatch]
329 |apply le_S.apply le_n
332 |apply not_eq_to_eqb_false.
337 |apply le_S_S_to_le.assumption
342 theorem eq_map_iter_p_transpose1: \forall p.\forall f.associative nat f \to
343 symmetric2 nat nat f \to \forall g. \forall a,k1,k2,n:nat. O < k1 \to k1 < k2 \to k2 \le n \to
344 (p k1) = true \to (p k2) = true \to (\forall i. k1 < i \to i < k2 \to (p i) = false)
345 \to map_iter_p n p g a f = map_iter_p n p (\lambda m. g (transpose k1 k2 m)) a f.
349 [assumption|apply lt_to_not_le.apply (trans_lt ? k1 ? H2 H3)]
350 |apply (eqb_elim (S n1) k2)
354 cut (k1 = n1 - (n1 -k1))
356 apply (eq_map_iter_p_transpose p f H H1 g a (n1-k1))
357 [cut (k1 \le n1)[autobatch|autobatch]
359 |rewrite < Hcut.assumption
360 |rewrite < Hcut.intros.
361 apply (H9 i H10).unfold.autobatch
369 [apply (bool_elim ? (p (S n1)))
371 rewrite > map_iter_p_S_true
372 [rewrite > map_iter_p_S_true
373 [cut ((transpose k1 k2 (S n1)) = (S n1))
377 [elim (le_to_or_lt_eq ? ? H6)
379 |absurd (S n1=k2)[apply sym_eq.assumption|assumption]
386 rewrite > (not_eq_to_eqb_false ? ? Hcut).
387 rewrite > (not_eq_to_eqb_false ? ? H4).
395 rewrite > map_iter_p_S_false
396 [rewrite > map_iter_p_S_false
398 [elim (le_to_or_lt_eq ? ? H6)
400 |absurd (S n1=k2)[apply sym_eq.assumption|assumption]
423 lemma decidable_n:\forall p.\forall n.
424 (\forall m. m \le n \to (p m) = false) \lor
425 (\exists m. m \le n \land (p m) = true \land
426 \forall i. m < i \to i \le n \to (p i) = false).
429 [apply (bool_elim ? (p O))
431 apply (ex_intro ? ? O).
433 [split[apply le_n|assumption]
434 |intros.absurd (O<i)[assumption|apply le_to_not_lt.assumption]
437 intros.apply (le_n_O_elim m H1).assumption
439 |apply (bool_elim ? (p (S n1)))
441 apply (ex_intro ? ? (S n1)).
443 [split[apply le_n|assumption]
444 |intros.absurd (S n1<i)[assumption|apply le_to_not_lt.assumption]
449 elim (le_to_or_lt_eq m (S n1) H3)
450 [apply H1.apply le_S_S_to_le.assumption
451 |rewrite > H4.assumption
454 elim H1.elim H3.elim H4.
455 apply (ex_intro ? ? a).
457 [split[apply le_S.assumption|assumption]
458 |intros.elim (le_to_or_lt_eq i (S n1) H9)
459 [apply H5[assumption|apply le_S_S_to_le.assumption]
460 |rewrite > H10.assumption
468 lemma decidable_n1:\forall p.\forall n,j. j \le n \to (p j)=true \to
469 (\forall m. j < m \to m \le n \to (p m) = false) \lor
470 (\exists m. j < m \land m \le n \land (p m) = true \land
471 \forall i. m < i \to i \le n \to (p i) = false).
473 elim (decidable_n p n)
477 apply not_eq_true_false.
482 apply (nat_compare_elim j a)
485 apply (ex_intro ? ? a).
491 [assumption|assumption]
504 apply not_eq_true_false.
507 apply (H6 j H2).assumption
513 lemma decidable_n2:\forall p.\forall n,j. j \le n \to (p j)=true \to
514 (\forall m. j < m \to m \le n \to (p m) = false) \lor
515 (\exists m. j < m \land m \le n \land (p m) = true \land
516 \forall i. j < i \to i < m \to (p i) = false).
520 apply (le_n_O_elim j H).intros.
522 [assumption|apply lt_to_not_le.assumption]
523 |elim (le_to_or_lt_eq ? ? H1)
526 [apply (bool_elim ? (p (S n1)))
529 apply (ex_intro ? ? (S n1)).
533 [assumption|apply le_n]
544 elim (le_to_or_lt_eq ? ? H7)
546 [assumption|apply le_S_S_to_le.assumption]
547 |rewrite > H8.assumption
555 apply (ex_intro ? ? a).
558 [split[assumption|apply le_S.assumption]
579 theorem eq_map_iter_p_transpose2: \forall p.\forall f.associative nat f \to
580 symmetric2 nat nat f \to \forall g. \forall a,k,n:nat. O < k \to k \le n \to
581 (p (S n) = true) \to (p k) = true
582 \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.
584 cut (k = (S n)-(S n -k))
585 [generalize in match H3.clear H3.
586 generalize in match g.
587 generalize in match H2.clear H2.
589 (*generalize in match Hcut.clear Hcut.*)
590 (* generalize in match H3.clear H3.*)
591 (* something wrong here
592 rewrite > Hcut in \vdash (?\rarr ?\rarr %). *)
593 apply (nat_elim1 (S n - k)).
595 elim (decidable_n2 p n (S n -m) H4 H6)
596 [apply (eq_map_iter_p_transpose1 p f H H1 f1 a)
603 [assumption|apply le_S_S_to_le.assumption]
610 (map_iter_p (S n) p (\lambda i.f1 (transpose a1 (S n) (transpose (S n -m) a1 i))) a f))
612 (map_iter_p (S n) p (\lambda i.f1 (transpose a1 (S n) i)) a f))
613 [cut (a1 = (S n -(S n -a1)))
616 [apply lt_plus_to_lt_minus
617 [apply le_S.assumption
619 apply lt_minus_to_lt_plus.
623 apply (trans_lt ? (S n -m))[assumption|assumption]
624 |rewrite < Hcut1.assumption
626 |rewrite < Hcut1.assumption
628 |apply minus_m_minus_mn.
629 apply le_S.assumption
631 |apply (eq_map_iter_p_transpose1 p f H H1)
634 |apply le_S.assumption
641 (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))
642 [cut (a1 = (S n) -(S n -a1))
644 [apply lt_plus_to_lt_minus
645 [apply le_S.assumption
647 apply lt_minus_to_lt_plus.
651 apply (trans_lt ? (S n -m))[assumption|assumption]
652 |rewrite < Hcut1.assumption
654 |rewrite < Hcut1.assumption
656 |apply minus_m_minus_mn.
657 apply le_S.assumption
659 |apply eq_map_iter_p.
660 cut (a1 = (S n) -(S n -a1))
664 rewrite < transpose_i_j_j_i.
665 rewrite > (transpose_i_j_j_i (S n -m)).
666 rewrite > (transpose_i_j_j_i a1 (S n)).
667 rewrite > (transpose_i_j_j_i (S n -m)).
671 apply (not_le_Sn_n n).
672 rewrite < H12.assumption
674 apply (not_le_Sn_n n).
675 rewrite > H12.assumption
677 apply (not_le_Sn_n a1).
678 rewrite < H12 in \vdash (? (? %) ?).assumption
680 |apply minus_m_minus_mn.
681 apply le_S.assumption
686 |apply minus_m_minus_mn.
687 apply le_S.assumption
691 theorem eq_map_iter_p_transpose3: \forall p.\forall f.associative nat f \to
692 symmetric2 nat nat f \to \forall g. \forall a,k,n:nat. O < k \to k \le (S n) \to
693 (p (S n) = true) \to (p k) = true
694 \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.
696 elim (le_to_or_lt_eq ? ? H3)
697 [apply (eq_map_iter_p_transpose2 p f H H1 g a k n H2)
698 [apply le_S_S_to_le.assumption|assumption|assumption]
702 apply eq_f.apply sym_eq. apply transpose_i_i.
706 lemma permut_p_O: \forall p.\forall h.\forall n.
707 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).
708 intros.unfold permut_p in H.
709 apply not_le_to_lt.unfold.intro.
710 elim (H (S m) H2 H3).
712 absurd (p (h (S m)) = true)
714 |apply (le_n_O_elim ? H4).
716 apply not_eq_true_false.
717 rewrite < H9.rewrite < H1.reflexivity
721 theorem eq_map_iter_p_permut: \forall p.\forall f.associative nat f \to
722 symmetric2 nat nat f \to \forall n.\forall g. \forall h.\forall a:nat.
723 permut_p h p n \to p O = false \to
724 map_iter_p n p g a f = map_iter_p n p (compose ? ? ? g h) a f .
727 [simplify.reflexivity
728 |apply (bool_elim ? (p (S n1)))
730 apply (trans_eq ? ? (map_iter_p (S n1) p (\lambda m.g ((transpose (h (S n1)) (S n1)) m)) a f))
731 [unfold permut_p in H3.
732 elim (H3 (S n1) (le_n ?) H5).
734 apply (eq_map_iter_p_transpose3 p f H H1 g a (h(S n1)) n1)
735 [apply (permut_p_O ? ? ? H3 H4)
736 [apply le_n|assumption]
741 |apply (trans_eq ? ? (map_iter_p (S n1) p (\lambda m.
742 (g(transpose (h (S n1)) (S n1)
743 (transpose (h (S n1)) (S n1) (h m)))) ) a f))
744 [rewrite > (map_iter_p_S_true ? ? ? ? ? H5).
745 rewrite > (map_iter_p_S_true ? ? ? ? ? H5).
747 [rewrite > transpose_i_j_j.
748 rewrite > transpose_i_j_i.
749 rewrite > transpose_i_j_j.
751 |apply (H2 (\lambda m.(g(transpose (h (S n1)) (S n1) m))) ?)
756 unfold permut_p in H3.
757 elim (H3 i (le_S ? ? H6) H7).
759 elim (le_to_or_lt_eq ? ? H10)
761 rewrite > (not_eq_to_eqb_false ? ? (lt_to_not_eq ? ? H8)).
762 cut (h i \neq h (S n1))
763 [rewrite > (not_eq_to_eqb_false ? ? Hcut).
771 unfold.apply le_S_S.assumption
775 apply (eqb_elim (S n1) (h (S n1)))
777 absurd (h i = h (S n1))
784 unfold.apply le_S_S.assumption
789 rewrite > (not_eq_to_eqb_false ? ? H12).
790 rewrite > (eq_to_eqb_true ? ? (refl_eq ? (S n1))).
792 elim (H3 (S n1) (le_n ? ) H5).
794 elim (le_to_or_lt_eq ? ? H15)
795 [apply le_S_S_to_le.assumption
798 apply sym_eq.assumption
803 unfold permut_p in H3.
805 apply (eqb_elim (h i) (S n1))
807 apply (eqb_elim (h i) (h (S n1)))
808 [intro.simplify.assumption
810 elim (H3 (S n1) (le_n ? ) H5).
814 apply (eqb_elim (h i) (h (S n1)))
815 [intro.simplify.assumption
817 elim (H3 i (le_S ? ? H6) H7).
822 |simplify.intros.unfold Not.intro.
823 unfold permut_p in H3.
824 elim (H3 i (le_S i ? H6) H7).
825 apply (H13 j H8 (le_S j ? H9) H10).
826 apply (injective_transpose ? ? ? ? H11)
831 |apply eq_map_iter_p.
833 rewrite > transpose_transpose.reflexivity
837 rewrite > (map_iter_p_S_false ? ? ? ? ? H5).
838 rewrite > (map_iter_p_S_false ? ? ? ? ? H5).
841 unfold permut_p in H3.
843 elim (H3 i (le_S i ? H6) H7).
847 [elim (le_to_or_lt_eq ? ? H10)
848 [apply le_S_S_to_le.assumption
849 |absurd (p (h i) = true)
853 unfold.intro.apply not_eq_true_false.
854 apply sym_eq.assumption
861 [assumption|apply (le_S ? ? H13)|assumption]