Lvc.Coherence.Coherence

Require Import CSet Le.

Require Import Plus Util AllInRel Map.
Require Import Val Var Env EnvTy IL Annotation Liveness Restrict Bisim MoreExp SetOperations.
Require Import DecSolve RenamedApart LabelsDefined.

Set Implicit Arguments.

Definition of Coherence: srd


Inductive srd : list (option (set var)) → stmtann (set var) → Prop :=
 | srdExp DL x e s lv al
    : srd (restrict DL (lv\{{x}})) s al
    → srd DL (stmtLet x e s) (ann1 lv al)
  | srdIf DL e s t lv als alt
    : srd DL s als
    → srd DL t alt
    → srd DL (stmtIf e s t) (ann2 lv als alt)
  | srdRet e DL lv
    : srd DL (stmtReturn e) (ann0 lv)
  | srdGoto DL lv f Y
    : get DL (counted f) (Some )
    → srd DL (stmtApp f Y) (ann0 lv)
 | srdExtern DL x f Y s lv al
    : srd (restrict DL (lv\{{x}})) s al
    → srd DL (stmtExtern x f Y s) (ann1 lv al)
  | srdLet DL s t Z lv als alt
    : srd (restrict (Some (getAnn als \ of_list Z)::DL) (getAnn als \ of_list Z))
          s als
    → srd (Some (getAnn als \ of_list Z)::DL) t alt
    → srd DL (stmtFun Z s t) (ann2 lv als alt).


Coherence is decidable


Definition srd_dec DL s a
  : Computable (srd DL s a).


Some monotonicity properties


Lemma srd_monotone (DL DL´ : list (option (set var))) s a
 : srd DL s a
   → PIR2 (fstNoneOrR Equal) DL DL´
   → srd DL´ s a.

Lemma srd_monotone2 (DL DL´ : list (option (set var))) s a
 : srd DL s a
   → PIR2 (fstNoneOrR (flip Subset)) DL DL´
   → srd DL´ s a.

Every renamed apart program is coherent

Note that this lemma also builds the liveness annotation: It exploits that we can always claim more variables live

In a coherent program, the globals of every function that can eventually be called are live


Lemma srd_globals_live s DL AL alv f
: live_sound Imperative AL s alv
  → srd DL s alv
  → PIR2 eqReq DL AL
  → isCalled s f
  → lv, get DL (counted f) (Some lv) lv getAnn alv.

On a coherent program a liveness analysis which is sound imperatively is also sound functionally.

Definition of invariance


Definition invariant (s:stmt) :=
   (E:onv var), bisim (nil:list F.block,E,s) (nil:list I.block,E,s).

Agreement Invariant


Definition rd_agree (DL:list (option (set var)))
           L (E:onv val)
  := n blk , get L n blkget DL n (Some ) →
                      agree_on eq E (F.block_E blk).

Lemma rd_agree_update DL L E G x v
 (RA:rd_agree DL L E)
  : rd_agree (restrict DL (G \ {{x}})) L (E [x <- v]).

Lemma rd_agree_update_list DL L E (G :set var) Z n vl
 (RA:rd_agree DL L E)
 (ZD:of_list Z [=] )
 (LEQ:length Z = length vl)
 (AG:agree_on eq E )
: rd_agree (restrict (drop n DL) ) (drop n L) ([Z <-- vl]).

Context coherence for IL/F contexts: approxF


Inductive approxF
  : list (option (set var)) → list (set var × list var) → F.blockProp :=
  approxI´ AL DL o Z E s
  : ( G, o = Some Gof_list Z G [=]
            a, getAnn a [=] (G of_list Z)
                 srd (restrict (Some G::AL) G) s a
                 live_sound Imperative DL s a)
     → approxF (o::AL) DL (F.blockI E Z s).

Stability under restriction

Lemma approx_restrict AL DL L G
: AIR21 approxF AL DL L
  → AIR21 approxF (restrict AL G) DL L.


Preservation properties

The rough slogan of what we show here is that the set of coherent states is closed under reduction

Lemma srd_preservation (E :onv val) L s DL (G:set var) DA a e
  (SRD:srd DA s a)
  (RA:rd_agree DA L E)
  (A: AIR21 approxF DA DL L)
  (LV:live_sound Imperative DL s a)
  (S:F.step (L, E, s) e (,,))
  : DA´ DL´ , srd DA´
                    rd_agree DA´
                    AIR21 approxF DA´ DL´ .

context coherence for imperative states (not required in the soundess proof)

Inductive approxI
  : list (option (set var)) → list (set var × list var) → I.blockProp :=
  approxII´ AL DL o Z s i
  : ( G, o = Some Gof_list Z G [=]
            a, getAnn a [=] (G of_list Z)
                 srd (restrict (Some G::AL) G) s a
                 live_sound i DL s a)
     → approxI (o::AL) DL (I.blockI Z s).

Lemma approxI_restrict AL DL L G
: AIR21 approxI AL DL L
  → AIR21 approxI (restrict AL G) DL L.

Lemma srd_preservation_I (E :onv val) L s DL (G:set var) DA a e i
  (SRD:srd DA s a)
  (A: AIR21 approxI DA DL L)
  (LV:live_sound i DL s a)
  (S:I.step (L, E, s) e (,,))
  : DL´ DA´ , srd DA´ AIR21 approxI DA´ DL´ .

Main Theorem about Coherence

stripB removes the environment from a closure
Definition stripB (b:F.block) : I.block :=
  match b with
    | F.blockI E Z sI.blockI Z s
  end.

Definition strip := List.map stripB.

Lemma drop_strip n L
  : drop n (strip L) = strip (drop n L).

The Bisimulation candidate.

Inductive srdSim : F.stateI.stateProp :=
  | srdSimI (E EI:onv val) L s AL DL a
  (SRD:srd AL s a)
  (RA:rd_agree AL L E)
  (A: AIR21 approxF AL DL L)
  (AG:agree_on eq (getAnn a) E EI)
  (LV:live_sound Imperative DL s a)
  (ER:PIR2 eqReq AL DL)
  : srdSim (L, E, s) (strip L, EI,s).

The bisimulation is indeed a bisimulation

Lemma srdSim_sim σ1 σ2
  : srdSim σ1 σ2bisim σ1 σ2.

Coherence implies invariance