Require Import Arith.
Fixpoint iter X (f:X->X) n x := match n with
O => x
| S n' => f(iter X f n' x)
end.
Fixpoint iter_tr X (f:X->X) n x := match n with
O => x
| S n' => f(iter_tr X f n' x)
end.
Proposition Iter_tr: forall X f n x,
iter X f n x = iter_tr X f n x.
Proof. induction n.
trivial.
intros. simpl. rewrite IHn. reflexivity.
Qed.
iter_tr computes more efficiently,
but iter is better for proofs.
Implicit Arguments iter [X].
Proposition iter_com: forall X f n (x:X),
iter f n (f x) = f(iter f n x).
Proof. intros X f. induction n.
trivial.
intro. simpl. rewrite IHn. trivial.
Qed.
Proposition iter_plus: forall x y,
x+y = iter S x y.
Proof. induction x.
trivial.
intro. simpl. rewrite IHx. trivial.
Qed.
Proposition iter_times: forall x y a,
x*y + a = iter (fun a => a+y) x a.
Proof. induction x.
trivial.
intros. simpl. rewrite <- IHx. ring.
Qed.
Goal forall x y, x*y= iter (fun a => a+y) x 0.
intros. rewrite <- iter_times. ring.
Fixpoint fac n := match n with
0 => 1
| S n' => S n' * fac n'
end.
Definition facStep p := (S(fst p), fst p*snd p).
Proposition fac_iter : forall n,
(S n, fac n) = iter facStep n (1,1).
Proof. induction n.
trivial.
simpl. rewrite <- IHn. trivial.
Qed.
Goal forall n, fac n = snd(iter facStep n (1,1)).
intro. rewrite <- fac_iter. reflexivity.
This page has been generated by coqdoc