Open Scope pred.
Program Definition getsn (n:nat) (stk stk':stack) (Q:pred world) : pred world :=
fun pr => let (p,r) := pr in
level p >= n -> exists n', n' < n /\
exists p', exists r', stepN n' p p' r stk r' stk' /\
Q (p',r').
Next Obligation.
hnf. simpl; intros.
destruct a. destruct a'.
intro.
cut (S (level p0) = level p).
intro. rewrite <- H2 in H0 .
spec H0. omega.
destruct H0 as [n' [? ?]].
exists n'. split; auto.
admit.
admit.
Qed.
Program Definition haltsn (n : nat) (stk : stack) : pred world :=
fun pr => let (p,r) := pr in
(level p >= n -> eventually_halts_n n p r stk).
Next Obligation.
unfold hereditary; intros.
destruct a as [p r'].
destruct a' as [p' r''].
intros.
unfold prog in *.
spec H0.
apply age_level in H; simpl in H; omega.
hnf in H.
simpl in H.
unfold prog_lookup, prog in *.
remember (age1 p).
icase o.
rewrite K.knot_age1 in Heqo.
remember (K.unsquash p).
destruct p0.
icase n0.
inv H.
inv Heqo.
destruct H0 as [px [rx [? ?]]].
repeat rewrite K.knot_level in *.
rewrite <- Heqp0 in H.
rewrite K.unsquash_squash in H1.
remember (K.unsquash px).
destruct p0.
simpl in *.
destruct n1.
omegac.
exists (K.squash (n1, f0)).
exists rx.
split.
repeat rewrite K.knot_level in *.
repeat rewrite K.unsquash_squash.
trivial.
generalize (stepstar_approxprog 1 _ _ (K.squash (n0,f)) (K.squash (n1, f0)) _ _ stk _ _ H0); intro.
spec H2.
exists (K.squash (n0,f)). split; try reflexivity.
hnf. rewrite K.knot_age1.
rewrite <- Heqp0. trivial.
spec H2.
exists (K.squash (n1,f0)). split; try reflexivity.
hnf. rewrite K.knot_age1.
rewrite <- Heqp1. trivial.
spec H2. apply stack_approx_rel_refl.
destruct H2 as [s2' [? ?]].
inversion H3. subst s2'.
trivial.
Qed.
Definition guardsn (n : nat) (P : pred world) (stk : stack) : pred world :=
P --> haltsn n stk.
Definition termMeas :=
{ R : store -> nat -> Prop |
forall x y y', R x y -> R x y' -> y = y' }.
Program Definition code (l : label) (c : instruction) : pred prog :=
fun p =>
exists i, prog_lookup p l = Some i /\ instr_approx_rel (level p) c i.
Next Obligation.
unfold hereditary; intros.
hnf in H.
unfold prog_lookup, prog in *.
destruct H0 as [i [? ?]].
exists (fmap_instr (K.approx (level a')) c).
split.
2: red; symmetry; apply collapse_instr_approx_same.
rewrite K.knot_age1 in H.
remember (K.unsquash a).
destruct p.
icase n.
inv H.
rewrite K.knot_level in *.
rewrite <- Heqp in H1.
rewrite K.unsquash_squash.
simpl in *.
unfold KnotInput.fmap.
erewrite fmap_eqn.
2: apply H0.
red in H1.
replace n with (min n (S n)) by (apply min_l; auto with arith).
do 2 rewrite <- collapse_instr_approx.
rewrite H1.
trivial.
Qed.
Program Definition evalTM (t : termMeas) (n : nat) : pred world :=
fun w => let (_, r) := w in
proj1_sig t r n.
Next Obligation.
unfold hereditary; intros.
destruct a. destruct a'.
hnf in H. simpl in H. unfold prog in *.
icase (age1 p).
inv H. trivial.
Qed.
Program Definition embedWP (P : pred world) : pred prog :=
fun p => forall r, P (p,r).
Next Obligation.
unfold hereditary; intros.
rewrite <- (nec_useless P) in H0.
apply (H0 r (a',r)).
simpl. rewrite worldNec_unfold. intuition.
Qed.
(* Definition of the hoare relation and rules *)
Definition hoare' (n':nat) (G:pred prog) (R P:pred world) (c:instruction) (Q:pred world) :=
forall p n k stk,
G p ->
(forall r', guardsn n R stk       (p,r')) ->
(forall r', guardsn n Q (k::stk)  (p,r')) ->
(forall r,  guardsn (n'+n) P ((c ;; k)::stk) (p,r)).
Definition hoare (G:pred prog) (R P:pred world) (c:instruction) (Q:pred world) :=
forall p k stk,
G p -> forall r p', necR p p' -> P (p',r) -> exists n,
getsn n ((c;;k)::stk) (k::stk) Q (p',r)  \/
getsn n ((c;;k)::stk) stk R (p',r).

Lemma hoare_seq2 : forall G R P Q S i1 i2,
hoare G R P i1 Q ->
hoare G R Q i2 S ->
hoare G R P (i1 ;; i2) S.