Lvc.Coherence.Restrict

Require Import CSet Util Get Drop Var Map Infra.Relations AllInRel.

Set Implicit Arguments.

Definition restr (G:set var) (o:option (set var)) :=
  match o with
    | NoneNone
    | Some if [ G] then Some else None
  end.

Lemma restr_iff G o
  : restr G o = Some G o = Some .

Lemma restr_idem G o
  : Grestr (restr G o) = restr o.

Lemma restr_comm o G
  : restr (restr G o) = restr G (restr o).

Instance restr_morphism
  : Proper (Equal ==> option_eq Equal ==> option_eq Equal) restr.

Instance restr_morphism_eq
  : Proper (Equal ==> eq ==> eq) restr.

Definition restrict (DL:list (option (set var))) (G:set var)
  := List.map (restr G) DL.

Lemma restrict_idem DL G
  : G restrict (restrict DL ) G = restrict DL G.

Lemma restrict_incl G DL
 : Grestrict (Some ::DL) G = Some ::restrict DL G.

Lemma restrict_not_incl G DL
 : ¬ Grestrict (Some ::DL) G = None::restrict DL G.

Lemma restrict_comm DL G
: restrict (restrict DL G) = restrict (restrict DL ) G.

Instance restrict_morphism
  : Proper (PIR2 (option_eq Equal) ==>
                    Equal ==> PIR2 (option_eq Equal)) restrict.

Instance restrict_morphism_eq
  : Proper (eq ==> Equal ==> eq) restrict.

Fixpoint bounded (DL:list (option (set var))) (G:set var) :=
  match DL with
    | nilTrue
    | Some ::DL G bounded DL G
    | None::DLbounded DL G
  end.

Instance bounded_morphism_subset
  : Proper (eq ==> Subset ==> impl) bounded.

Instance bounded_morphism
  : Proper (eq ==> Equal ==> iff) bounded.

Lemma bounded_get DL G n
  : bounded DL Gget DL n (Some ) → G.

Lemma bounded_restrict DL G
  : Gbounded (restrict DL ) G.

Lemma bounded_restrict_eq DL G
  : G bounded DL Grestrict DL = DL.

Lemma restrict_subset2 DL DL´ G
: PIR2 (fstNoneOrR (flip Subset)) DL DL´
  → G
  → PIR2 (fstNoneOrR (flip Subset)) (restrict DL G) (restrict DL´ ).

Lemma restrict_subset DL DL´ G
: PIR2 (fstNoneOrR Equal) DL DL´
  → G
  → PIR2 (fstNoneOrR Equal) (restrict DL G) (restrict DL´ ).

Lemma restr_comp_meet G o
  : restr (restr G o) = restr (G ) o.

Lemma restrict_comp_meet DL G
  : restrict (restrict DL G) = restrict DL (G ).

Definition lookup_set_option (ϱ:varvar) (x:option (set var)) : option (set var):=
  match x with
    | NoneNone
    | Some xSome (lookup_set ϱ x)
  end.

Definition map_lookup (ϱ:varvar) := List.map (lookup_set_option ϱ).

Definition live_global (p:set var × list var) := Some (fst p \ of_list (snd p)).
Definition live_globals (Lv:list (set var × list var)) := List.map live_global Lv.

Lemma bounded_map_lookup G (ϱ: varvar) DL
  : bounded DL Gbounded (map_lookup ϱ DL) (lookup_set ϱ G).

Lemma restrict_incl_ext DL G D
: bounded DL D
   → G D [=] D
   → restrict DL G = restrict DL .

Lemma list_eq_special DL ϱ A B
: A B
  → lookup_set ϱ A
  → PIR2 (fstNoneOrR Equal)
         (map_lookup ϱ (restrict DL A))
         (restrict (map_lookup ϱ (restrict DL B)) ).

Lemma list_eq_fstNoneOrR_incl DL ϱ A B
: A B
  PIR2 (fstNoneOrR Equal)
       (map_lookup ϱ (restrict DL A))
       (map_lookup ϱ (restrict DL B)).

Lemma restrict_app L s
: restrict (L++) s = restrict L s ++ restrict s.

Lemma restrict_length L s
: length (restrict L s) = length L.

Lemma bounded_app L s
: bounded (L++) s bounded L s bounded s.

Inductive fstNoneOrR´ {X Y:Type} (R:XYProp)
  : option XYProp :=
| fstNone´ (y:Y) : fstNoneOrR´ R None y
| bothR´ (x:X) (y:Y) : R x yfstNoneOrR´ R (Some x) y
.

Definition eqReq := (fstNoneOrR´ (fun (s : set var) (t : set var × list var) ⇒
                                   s [=] fst t \ of_list (snd t))).

Lemma restrict_eqReq DL DL´ G
: PIR2 eqReq DL DL´
  → PIR2 eqReq (restrict DL G) DL´.

Lemma restrict_get DL lv n s
: get (restrict DL lv) n s
  → get DL n (Some s) s lv.