Library CompOptCert.Iteration
Bounded and unbounded iterators
This modules defines several Coq encodings of a general "while" loop.
The loop is presented in functional style as the iteration
of a step function of type A → B + A:
We first implement the case where termination is guaranteed because
the current state a decreases at each iteration.
let rec iterate step a = match step a with | inl b -> b | inr a' -> iterate step a'This iteration cannot be defined directly in Coq using Fixpoint, because Coq is a logic of total functions, and therefore we must guarantee termination of the loop.
Terminating iteration
Module WfIter.
Section ITERATION.
Variables A B: Type.
Variable step: A → B + A.
Variable ord: A → A → Prop.
Hypothesis ord_wf: well_founded ord.
Hypothesis step_decr: ∀ a a', step a = inr _ a' → ord a' a.
Definition step_info (a: A) : {b | step a = inl _ b} + {a' | step a = inr _ a' & ord a' a}.
Proof.
caseEq (step a); intros. left; ∃ b; auto. right; ∃ a0; auto.
Defined.
Definition iterate_F (a: A) (rec: ∀ a', ord a' a → B) : B :=
match step_info a with
| inl (exist b P) ⇒ b
| inr (exist2 a' P Q) ⇒ rec a' Q
end.
Definition iterate (a: A) : B := Fix ord_wf iterate_F a.
We now prove an invariance property iterate_prop, similar to the Hoare
logic rule for "while" loops.
Variable P: A → Prop.
Variable Q: B → Prop.
Hypothesis step_prop:
∀ a : A, P a →
match step a with inl b ⇒ Q b | inr a' ⇒ P a' end.
Lemma iterate_prop:
∀ a, P a → Q (iterate a).
Proof.
intros a0. pattern a0. apply well_founded_ind with (R := ord). auto.
intros. unfold iterate; rewrite unroll_Fix. unfold iterate_F.
destruct (step_info x) as [[b U] | [a' U V]].
exploit step_prop; eauto. rewrite U; auto.
apply H. auto. exploit step_prop; eauto. rewrite U; auto.
Qed.
End ITERATION.
End WfIter.
Bounded iteration
Module PrimIter.
Section ITERATION.
Variables A B: Type.
Variable step: A → B + A.
Definition num_iterations := 1000000000000%positive.
The simple definition of bounded iteration is:
Fixpoint iterate (niter: nat) (a: A) {struct niter} : option B := match niter with | O => None | S niter' => match step a with | inl b => b | inr a' => iterate niter' a' end end.This function is structural recursive over the parameter niter (number of iterations), represented here as a Peano integer (type nat). However, we want to use very large values of niter. As Peano integers, these values would be much too large to fit in memory. Therefore, we must express iteration counts as a binary integer (type positive). However, Peano induction over type positive is not structural recursion, so we cannot define iterate as a Coq fixpoint and must use Noetherian recursion instead.
Definition iter_step (x: positive)
(next: ∀ y, Plt y x → A → option B)
(s: A) : option B :=
match peq x xH with
| left EQ ⇒ None
| right NOTEQ ⇒
match step s with
| inl res ⇒ Some res
| inr s' ⇒ next (Pos.pred x) (Ppred_Plt x NOTEQ) s'
end
end.
Definition iter: positive → A → option B := Fix Plt_wf iter_step.
We now prove the invariance property iterate_prop.
Variable P: A → Prop.
Variable Q: B → Prop.
Hypothesis step_prop:
∀ a : A, P a →
match step a with inl b ⇒ Q b | inr a' ⇒ P a' end.
Lemma iter_prop:
∀ n a b, P a → iter n a = Some b → Q b.
Proof.
apply (well_founded_ind Plt_wf
(fun p ⇒ ∀ a b, P a → iter p a = Some b → Q b)).
intros. unfold iter in H1. rewrite unroll_Fix in H1. unfold iter_step in H1.
destruct (peq x 1). discriminate.
specialize (step_prop a H0).
destruct (step a) as [b'|a'] eqn:?.
inv H1. auto.
apply H with (Pos.pred x) a'. apply Ppred_Plt; auto. auto. auto.
Qed.
Lemma iterate_prop:
∀ a b, iterate a = Some b → P a → Q b.
Proof.
intros. apply iter_prop with num_iterations a; assumption.
Qed.
End ITERATION.
End PrimIter.