Library ICSemantics

Semantics for IC

We define a small-step operation semantics and an axiomatic semantics for IC. For the axiomatic semantics we provide a recursively defined function computing the WP predicate. We show the equivalence of all three representations.
Require Import Facts States ICSyntax.
Set Implicit Arguments.
Unset Strict Implicit.

Module ICSemantics (Sigma : State).
Module ICSyn := ICSyntax.ICSyntax Sigma.
Export ICSyn.

Implicit Types (P : Pred state) (Q QQ : NPred state) (x y z : state).
Implicit Types (a : action) (b : guard) (s t : term).

Small-step Semantics

Definition conf := (term × state)%type.
Inductive step : confconfProp :=
| step_act a s x :
    step (Act a s, x) (s, a x)
| step_def s t x:
    step (Def s t, x) (t.[Def s s/], x)
| step_if_true b s t x :
    b x
    step (If b s t, x) (s, x)
| step_if_false b s t x :
    ~~b x
    step (If b s t, x) (t, x).
Notation mstep := (star step).

Axiomatic Semantics

Inductive eval Q : termPred state :=
| eval_act a s x :
    eval Q s (a x) →
    eval Q (Act a s) x
| eval_if_true b s t x :
    b x
    eval Q s x
    eval Q (If b s t) x
| eval_if_false b s t x :
    ~~b x
    eval Q t x
    eval Q (If b s t) x
| eval_jump f x :
    Q f x
    eval Q (Jump f) x
| eval_def P s t x :
    P <<= eval Q (Def s s) →
    eval (P .: Q) t x
    eval Q (Def s t) x.

WP Semantics

Fixpoint wp Q s :=
  match s with
    | Act a swp Q s \o a
    | If b s tfun sigmaif b sigma then wp Q s sigma else wp Q t sigma
    | Jump fQ f
    | Def s t
      let F P := wp (P .: Q) s in
      wp (Fix F .: Q) t

Lemma wp_mono Q QQ :
  ( n, Q n <<= QQ n) → ( s, wp Q s <<= wp QQ s).
Proof with eauto.
  moveh1 s. elim: s Q QQ h1 ⇒ [|b s ihs t iht|s ihs t iht|] Q QQ h1 /=...
  - movex. case: ifP...
  - apply: iht ⇒ -[|n]/=... apply: fix_monoP. by apply: ihs ⇒ -[].

Lemma wp_equiv Q QQ s x :
  ( n x, Q n x QQ n x) → (wp Q s x wp QQ s x).
  moveh. split; apply: wp_mono; firstorder.

Lemma wp_cons_mono Q s :
  monotone (fun Pwp (P .: Q) s).
  moveP1 P2 h. by apply: wp_mono ⇒ -[].

Lemma wp_fix P Q s :
  wp (P .: Q) s <<= PFix (fun Pwp (P .: Q) s) <<= P.
  moveh x. elim=>{x}x _ ih sx. exact/h/wp_cons_mono.

Equivalence of the axiomatic and wp semantics

Lemma wp_eval Q s :
  wp Q s <<= eval Q s.
Proof with eauto using eval.
  elim: s Q ⇒ [a s ihs|b s ihs t iht|s ihs t iht|f] Q x /=...
  case: ifPn... set P := Fix _. movetx. apply: (eval_def (P := P))...
  apply: wp_fix. move=>{x tx} x sx. eapply eval_def...

Lemma eval_wp Q s :
  eval Q s <<= wp Q s.
  movex evs. elim: evs ⇒ //={Q s x}
    [Q e s t x ->|Q e s t x/negbTE->|Q P s t x _ le _]//.
  apply: wp_cons_mono xx/le. exact: fix_fold.

Theorem coincidence Q s x : eval Q s x wp Q s x.
Proof. split; by [exact: eval_wp|exact: wp_eval]. Qed.

Compatibility with substitutions

Lemma wp_ren Q (xi : varvar) s :
  wp Q s.[ren xi] = wp (xi >>> Q) s.
  elim: s Q xi ⇒ [e s ihs|e s ihs t iht|s ihs t iht|f] Q xi //=.
  - by rewrite ihs.
  - by rewrite ihs iht.
  - asimpl; rewrite iht; f_equal; asimpl. f_ext ⇒ -[|x]//=.
    f_equal. f_extP. rewrite ihs. autosubst.

Lemma wp_weaken P Q s : wp (P .: Q) s.[ren (+1)] = wp Q s.
Proof. exact: wp_ren. Qed.

Lemma wp_subst Q (theta : varterm) s :
  wp Q s.[theta] = wp (wp Q \o theta) s.
  elim: s Q theta ⇒ [e s ihs|e s ihs t iht|s ihs t iht|f] Q theta //=.
  - by rewrite ihs.
  - by rewrite ihs iht.
  - rewrite iht. f_equal. f_ext ⇒ -[|x]//=; asimpl; last by exact: wp_weaken.
    f_equal. f_extP. rewrite ihs. f_equal. f_ext ⇒ -[|y] //=.
    asimpl; exact: wp_weaken.

Correspondence between the operational and axiomatic semantics

Lemma step_admissible Q s t x y :
  step (s,x) (t,y) → (wp Q s x wp Q t y).
  moveh. inv h ⇒ //=; last first. by rewrite ifN. by rewrite H0.
  move: s0 t0s t. rewrite wp_subst. split; apply: wp_mono ⇒ -[]//=.
  apply: fix_unfold. exact: wp_cons_mono. exact: fix_fold.

Lemma mstep_admissible Q f s x y :
  mstep (s,x) (Jump f,y) → (wp Q s x Q f y).
  suff: p q, mstep p q → (wp Q p.1 p.2 wp Q q.1 q.2).
  exact. move⇒ {f s x y} p q. elim⇒ //{q}-[s x] [t y]/= _ ih st.
  rewrite ih. exact: step_admissible.

Definition terminates s x :=
   f y, mstep (s,x) (Jump f,y).

Lemma step_terminates s t x y :
  step (s,x) (t,y)terminates t yterminates s x.
  movest [z [f h]]. z, f. exact: starRS st h.

Lemma eval_terminates Q s x :
  eval Q s x theta,
    ( f, Q f <<= terminates (theta f)) →
    terminates s.[theta] x.
  elim⇒ {Q s x}//=; eauto using step_terminates, step.
  moveQ P s t x _ ih1 _ ih2 theta h.
  apply: step_terminates (step_def _ _ _) _. asimpl.
  apply: ih2 ⇒ -[|//]/=y py. exact: ih1.

Corollary eval_terminates_id Q s :
  eval Q s <<= terminates s.
  movex ev. rewrite -[s]subst_id. apply: eval_terminates ev _ _.
  move⇒ {Q s x} f x _. f, x. exact: starxx.

Theorem correspondence Q s x :
  wp Q s x f y, mstep (s,x) (Jump f,y) Q f y.
  split⇒ [h1|[tau[f[h1 h2]]]].
  - move: (h1) ⇒ /wp_eval/eval_terminates_id[f[y h2]]. f,y.
    split⇒ //. by rewrite -(mstep_admissible _ h2).
  - by rewrite (mstep_admissible _ h1).

Consequences of the correspondence result

Definition point_pred f x : NPred state := fun g yf = g x = y.

Corollary wp_step_equiv f s x y :
  mstep (s,x) (Jump f,y) wp (point_pred f y) s x.
  rewrite correspondence. split ⇒ [h|[g[z[h1[->->]]]]] //. by f,y.

Corollary wp_terminates s x :
  wp (@Top _) s x terminates s x.
  rewrite correspondence. by firstorder.

Corollary wp_det Q s x :
  wp Q s x f y, wp (point_pred f y) s x Q f y.
  rewrite correspondence. split⇒ -[f[y[h1 h2]]]; f, y; split⇒ //.
    by rewrite -wp_step_equiv. by rewrite wp_step_equiv.

End ICSemantics.