1
(************************************************************************)
2
(* v * The Coq Proof Assistant / The Coq Development Team *)
3
(* <O___,, * CNRS-Ecole Polytechnique-INRIA Futurs-Universite Paris Sud *)
4
(* \VV/ **************************************************************)
5
(* // * This file is distributed under the terms of the *)
6
(* * GNU Lesser General Public License Version 2.1 *)
7
(************************************************************************)
9
(*i $Id: Zmisc.v 11072 2008-06-08 16:13:37Z herbelin $ i*)
11
Require Import Wf_nat.
12
Require Import BinInt.
13
Require Import Zcompare.
14
Require Import Zorder.
16
Open Local Scope Z_scope.
18
(**********************************************************************)
21
(** [n]th iteration of the function [f] *)
23
Fixpoint iter_pos (n:positive) (A:Type) (f:A -> A) (x:A) {struct n} : A :=
26
| xO n' => iter_pos n' A f (iter_pos n' A f x)
27
| xI n' => f (iter_pos n' A f (iter_pos n' A f x))
30
Definition iter (n:Z) (A:Type) (f:A -> A) (x:A) :=
33
| Zpos p => iter_pos p A f x
37
Theorem iter_nat_of_P :
38
forall (p:positive) (A:Type) (f:A -> A) (x:A),
39
iter_pos p A f x = iter_nat (nat_of_P p) A f x.
41
intro n; induction n as [p H| p H| ];
42
[ intros; simpl in |- *; rewrite (H A f x);
43
rewrite (H A f (iter_nat (nat_of_P p) A f x));
44
rewrite (ZL6 p); symmetry in |- *; apply f_equal with (f := f);
46
| intros; unfold nat_of_P in |- *; simpl in |- *; rewrite (H A f x);
47
rewrite (H A f (iter_nat (nat_of_P p) A f x));
48
rewrite (ZL6 p); symmetry in |- *; apply iter_nat_plus
49
| simpl in |- *; auto with arith ].
52
Theorem iter_pos_plus :
53
forall (p q:positive) (A:Type) (f:A -> A) (x:A),
54
iter_pos (p + q) A f x = iter_pos p A f (iter_pos q A f x).
57
rewrite (iter_nat_of_P m A f x).
58
rewrite (iter_nat_of_P n A f (iter_nat (nat_of_P m) A f x)).
59
rewrite (iter_nat_of_P (n + m) A f x).
60
rewrite (nat_of_P_plus_morphism n m).
64
(** Preservation of invariants : if [f : A->A] preserves the invariant [Inv],
65
then the iterates of [f] also preserve it. *)
67
Theorem iter_nat_invariant :
68
forall (n:nat) (A:Type) (f:A -> A) (Inv:A -> Prop),
69
(forall x:A, Inv x -> Inv (f x)) ->
70
forall x:A, Inv x -> Inv (iter_nat n A f x).
72
simple induction n; intros;
74
| simpl in |- *; apply H0 with (x := iter_nat n0 A f x); apply H;
78
Theorem iter_pos_invariant :
79
forall (p:positive) (A:Type) (f:A -> A) (Inv:A -> Prop),
80
(forall x:A, Inv x -> Inv (f x)) ->
81
forall x:A, Inv x -> Inv (iter_pos p A f x).
83
intros; rewrite iter_nat_of_P; apply iter_nat_invariant; trivial with arith.