summaryrefslogtreecommitdiff
path: root/theories/ZArith/Zmisc.v
blob: f625f762f8d2149b1b8329d234f78dc7a973abf7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
(************************************************************************)
(*  v      *   The Coq Proof Assistant  /  The Coq Development Team     *)
(* <O___,, *   INRIA - CNRS - LIX - LRI - PPS - Copyright 1999-2010     *)
(*   \VV/  **************************************************************)
(*    //   *      This file is distributed under the terms of the       *)
(*         *       GNU Lesser General Public License Version 2.1        *)
(************************************************************************)

(*i $Id: Zmisc.v 13323 2010-07-24 15:57:30Z herbelin $ i*)

Require Import Wf_nat.
Require Import BinInt.
Require Import Zcompare.
Require Import Zorder.
Require Import Bool.
Open Local Scope Z_scope.

(**********************************************************************)
(** Iterators *)

(** [n]th iteration of the function [f] *)

Fixpoint iter_pos (n:positive) (A:Type) (f:A -> A) (x:A) : A :=
  match n with
    | xH => f x
    | xO n' => iter_pos n' A f (iter_pos n' A f x)
    | xI n' => f (iter_pos n' A f (iter_pos n' A f x))
  end.

Definition iter (n:Z) (A:Type) (f:A -> A) (x:A) :=
  match n with
    | Z0 => x
    | Zpos p => iter_pos p A f x
    | Zneg p => x
  end.

Theorem iter_nat_of_P :
  forall (p:positive) (A:Type) (f:A -> A) (x:A),
    iter_pos p A f x = iter_nat (nat_of_P p) A f x.
Proof.
  intro n; induction n as [p H| p H| ];
    [ intros; simpl in |- *; rewrite (H A f x);
      rewrite (H A f (iter_nat (nat_of_P p) A f x));
	rewrite (ZL6 p); symmetry  in |- *; apply f_equal with (f := f);
	  apply iter_nat_plus
      | intros; unfold nat_of_P in |- *; simpl in |- *; rewrite (H A f x);
	rewrite (H A f (iter_nat (nat_of_P p) A f x));
	  rewrite (ZL6 p); symmetry  in |- *; apply iter_nat_plus
      | simpl in |- *; auto with arith ].
Qed.

Lemma iter_nat_of_Z : forall n A f x, 0 <= n ->
  iter n A f x = iter_nat (Zabs_nat n) A f x.
intros n A f x; case n; auto.
intros p _; unfold iter, Zabs_nat; apply iter_nat_of_P.
intros p abs; case abs; trivial.
Qed.

Theorem iter_pos_plus :
  forall (p q:positive) (A:Type) (f:A -> A) (x:A),
    iter_pos (p + q) A f x = iter_pos p A f (iter_pos q A f x).
Proof.
  intros n m; intros.
  rewrite (iter_nat_of_P m A f x).
  rewrite (iter_nat_of_P n A f (iter_nat (nat_of_P m) A f x)).
  rewrite (iter_nat_of_P (n + m) A f x).
  rewrite (nat_of_P_plus_morphism n m).
  apply iter_nat_plus.
Qed.

(** Preservation of invariants : if [f : A->A] preserves the invariant [Inv],
    then the iterates of [f] also preserve it. *)

Theorem iter_nat_invariant :
  forall (n:nat) (A:Type) (f:A -> A) (Inv:A -> Prop),
    (forall x:A, Inv x -> Inv (f x)) ->
    forall x:A, Inv x -> Inv (iter_nat n A f x).
Proof.
  simple induction n; intros;
    [ trivial with arith
      | simpl in |- *; apply H0 with (x := iter_nat n0 A f x); apply H;
	trivial with arith ].
Qed.

Theorem iter_pos_invariant :
  forall (p:positive) (A:Type) (f:A -> A) (Inv:A -> Prop),
    (forall x:A, Inv x -> Inv (f x)) ->
    forall x:A, Inv x -> Inv (iter_pos p A f x).
Proof.
  intros; rewrite iter_nat_of_P; apply iter_nat_invariant; trivial with arith.
Qed.