val equiv_ext_right (p q r: slprop)
    : Lemma (requires q `Mem.equiv` r) (ensures Mem.((p `star` q) `equiv` (p `star` r)))
let equiv_ext_right (p q r:slprop)
  : Lemma
      (requires q `Mem.equiv` r)
      (ensures Mem.((p `star` q) `equiv` (p `star` r)))
  = let open Steel.Memory in
    calc (equiv) {
      p `star` q;
         (equiv) { star_commutative p q }
      q `star` p;
         (equiv) { equiv_extensional_on_star q r p }
      r `star` p;
         (equiv) { star_commutative p r }
      p `star` r;
    }
val equiv_ext_right (p q r: slprop)
    : Lemma (requires q `Mem.equiv` r) (ensures Mem.((p `star` q) `equiv` (p `star` r)))
let equiv_ext_right (p q r: slprop)
    : Lemma (requires q `Mem.equiv` r) (ensures Mem.((p `star` q) `equiv` (p `star` r))) =
let open Steel.Memory in
calc (equiv) {
  p `star` q;
  (equiv) { star_commutative p q }
  q `star` p;
  (equiv) { equiv_extensional_on_star q r p }
  r `star` p;
  (equiv) { star_commutative p r }
  p `star` r;
}
Steel.HigherReference.fst.checked
Steel.PCMReference.fsti.checked Steel.PCMFrac.fst.checked Steel.Memory.fsti.checked Steel.Effect.Atomic.fsti.checked Steel.Effect.fsti.checked prims.fst.checked FStar.Real.fsti.checked FStar.Pervasives.Native.fst.checked FStar.Pervasives.fsti.checked FStar.PCM.fst.checked FStar.NMSTTotal.fst.checked FStar.Ghost.fsti.checked FStar.Classical.fsti.checked FStar.Calc.fsti.checked
Steel.HigherReference.fst
Steel.Memory.slprop FStar.Calc.calc_finish Steel.Memory.equiv Steel.Memory.star Prims.Cons FStar.Preorder.relation Prims.Nil Prims.unit FStar.Calc.calc_step FStar.Calc.calc_init FStar.Calc.calc_pack Steel.Memory.star_commutative Prims.squash Steel.Memory.equiv_extensional_on_star FStar.Pervasives.pattern
(*
   Copyright 2020 Microsoft Research

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*)

module Steel.HigherReference
open FStar.Ghost
open Steel.Memory
open Steel.Effect.Atomic
open Steel.Effect
open FStar.PCM
open Steel.PCMFrac
open FStar.Real
module RP = Steel.PCMReference

#set-options "--ide_id_info_off"

module Mem = Steel.Memory

let ref a = Mem.ref (fractional a) pcm_frac
let null #a = Mem.null #(fractional a) #pcm_frac
let is_null #a r = Mem.is_null #(fractional a) #pcm_frac r
let perm_ok p : prop = (p.v <=. one == true) /\ True
let pts_to_raw_sl (#a:Type) (r:ref a) (p:perm) (v:erased a) : slprop =
  Mem.pts_to r (Some (Ghost.reveal v, p))
let pts_to_raw (#a:Type) (r:ref a) (p:perm) (v:erased a) : vprop =
  to_vprop (Mem.pts_to r (Some (Ghost.reveal v, p)))
[@@__reduce__]
let pts_to' (#a:Type u#1) (r:ref a) (p:perm) (v:erased a) : vprop = pts_to_raw r p v `star` pure (perm_ok p)
let pts_to_sl #a r p v  = hp_of (pts_to' r p v)

let abcd_acbd (a b c d:slprop)
  : Lemma (Mem.(((a `star` b) `star` (c `star` d)) `equiv`
           ((a `star` c) `star` (b `star` d))))
  = let open Steel.Memory in
    calc (equiv) {
    ((a `star` b) `star` (c `star` d));
      (equiv) { star_associative a b (c `star` d) }
    ((a `star` (b `star` (c `star` d))));
      (equiv) { star_associative b c d;
                star_congruence a (b `star` (c `star` d))
                                a ((b `star` c) `star` d) }
    (a `star` ((b `star` c) `star` d));
      (equiv) { star_commutative  b c;
                star_congruence (b `star` c) d (c `star` b) d;
                star_congruence a ((b `star` c) `star` d)
                                a ((c `star` b) `star` d) }
    (a `star` ((c `star` b) `star` d));
      (equiv) { star_associative c b d;
                star_congruence a ((c `star` b) `star` d)
                                a (c `star` (b `star` d)) }
    (a `star` (c `star` (b `star` d)));
      (equiv) { star_associative a c (b `star` d) }
    ((a `star` c) `star` (b `star` d));
   }

let pts_to_ref_injective
      (#a: Type u#1)
      (r: ref a)
      (p0 p1:perm)
      (v0 v1:a)
      (m:mem)
    : Lemma
      (requires
        interp (pts_to_sl r p0 v0 `Mem.star` pts_to_sl r p1 v1) m)
      (ensures v0 == v1)
    = let open Steel.Memory in
      abcd_acbd (hp_of (pts_to_raw r p0 v0))
                (pure (perm_ok p0))
                (hp_of (pts_to_raw r p1 v1))
                (pure (perm_ok p1));
      Mem.affine_star (hp_of (pts_to_raw r p0 v0) `star` hp_of (pts_to_raw r p1 v1))
                      (pure (perm_ok p0) `star` pure (perm_ok p1)) m;
      Mem.pts_to_compatible r (Some (Ghost.reveal v0, p0))
                              (Some (Ghost.reveal v1, p1))
                              m

let pts_to_not_null (#a:Type u#1)
                    (r:ref a)
                    (p:perm)
                    (v:a)
                    (m:mem)
  : Lemma (requires interp (pts_to_sl r p v) m)
          (ensures r =!= null)
  = Mem.affine_star (hp_of (pts_to_raw r p v)) (Mem.pure (perm_ok p)) m;
    Mem.pts_to_not_null r (Some (Ghost.reveal v, p)) m

let pts_to_witinv (#a:Type) (r:ref a) (p:perm) : Lemma (is_witness_invariant (pts_to_sl r p)) =
  let aux (x y : erased a) (m:mem)
    : Lemma (requires (interp (pts_to_sl r p x) m /\ interp (pts_to_sl r p y) m))
            (ensures  (x == y))
    =
    Mem.pts_to_join r (Some (Ghost.reveal x, p)) (Some (Ghost.reveal y, p)) m
  in
  Classical.forall_intro_3 (fun x y -> Classical.move_requires (aux x y))

let higher_ref_pts_to_injective_eq #a #opened #p0 #p1 #v0 #v1 r =
  extract_info_raw (pts_to r p0 v0 `star` pts_to r p1 v1) (v0 == v1)
    (fun m -> pts_to_ref_injective r p0 p1 v0 v1 m);
  rewrite_slprop (pts_to r p1 v1) (pts_to r p1 v0) (fun _ -> ())

let pts_to_framon (#a:Type) (r:ref a) (p:perm) : Lemma (is_frame_monotonic (pts_to_sl r p)) =
  pts_to_witinv r p

let intro_pts_to (p:perm) #a #uses (#v:erased a) (r:ref a)
  : SteelGhost unit uses
                (pts_to_raw r p v)
                (fun _ -> pts_to r p v)
                (requires fun _ -> perm_ok p)
                (ensures fun _ _ _ -> True)
  = intro_pure (perm_ok p);
    rewrite_slprop (pts_to' r p v) (pts_to r p v) (fun _ -> ())

let alloc #a x =
  let v = Some (x, full_perm) in
  assert (FStar.PCM.composable pcm_frac v None);
  assert (compatible pcm_frac v v);
  let r = RP.alloc v in
  rewrite_slprop (RP.pts_to r v) (pts_to r full_perm x)
    (fun m ->
      emp_unit (hp_of (pts_to_raw r full_perm x));
      pure_star_interp (hp_of (pts_to_raw r full_perm x)) (perm_ok full_perm) m
    );
  extract_info_raw (pts_to r full_perm x) (~ (is_null r))
    (fun m -> pts_to_not_null r full_perm x m);
   return r

let read (#a:Type) (#p:perm) (#v:erased a) (r:ref a)
  = let v1 : erased (fractional a) = Ghost.hide (Some (Ghost.reveal v, p)) in
    rewrite_slprop (pts_to r p v) (RP.pts_to r v1 `star` pure (perm_ok p)) (fun _ -> ());
    elim_pure (perm_ok p);
    let v2 = RP.read r v1 in
    rewrite_slprop (RP.pts_to r v1) (pts_to r p v)
      (fun m ->
        emp_unit (hp_of (pts_to_raw r p v));
        pure_star_interp (hp_of (pts_to_raw r p v)) (perm_ok p) m);
    assert (compatible pcm_frac v1 v2);
    let Some (x, _) = v2 in
    rewrite_slprop (pts_to r p v) (pts_to r p x) (fun _ -> ());
    return x

let atomic_read (#opened:_) (#a:Type) (#p:perm) (#v:erased a) (r:ref a)
  = let v1 : erased (fractional a) = Ghost.hide (Some (Ghost.reveal v, p)) in
    rewrite_slprop (pts_to r p v) (RP.pts_to r v1 `star` pure (perm_ok p)) (fun _ -> ());
    elim_pure (perm_ok p);

    let v2 = RP.atomic_read r v1 in
    rewrite_slprop (RP.pts_to r v1) (pts_to r p v)
      (fun m ->
        emp_unit (hp_of (pts_to_raw r p v));
        pure_star_interp (hp_of (pts_to_raw r p v)) (perm_ok p) m);
    assert (compatible pcm_frac v1 v2);
    let Some (x, _) = v2 in
    rewrite_slprop (pts_to r p v) (pts_to r p x) (fun _ -> ());
    return x

let read_refine (#a:Type) (#p:perm) (q:a -> vprop) (r:ref a)
  : SteelT a (h_exists (fun (v:a) -> pts_to r p v `star` q v))
                (fun v -> pts_to r p v `star` q v)
  = let vs:erased a = witness_exists () in

    rewrite_slprop (pts_to r p (Ghost.hide (Ghost.reveal vs))) (pts_to r p vs) (fun _ -> ());

    let v = read r in

    rewrite_slprop (q vs) (q v) (fun _ -> ());
    return v

let write (#a:Type) (#v:erased a) (r:ref a) (x:a)
  : SteelT unit (pts_to r full_perm v) (fun _ -> pts_to r full_perm x)
  = let v_old : erased (fractional a) = Ghost.hide (Some (Ghost.reveal v, full_perm)) in
    let v_new : fractional a = Some (x, full_perm) in
    rewrite_slprop (pts_to r full_perm v) (RP.pts_to r v_old `star` pure (perm_ok full_perm)) (fun _ -> ());

    elim_pure (perm_ok full_perm);

    RP.write r v_old v_new;
    rewrite_slprop (RP.pts_to r v_new) (pts_to r full_perm x)
        (fun m -> emp_unit (hp_of (pts_to_raw r full_perm x));
          pure_star_interp (hp_of (pts_to_raw r full_perm x)) (perm_ok full_perm) m)

let atomic_write #opened #a #v r x
  = let v_old : erased (fractional a) = Ghost.hide (Some (Ghost.reveal v, full_perm)) in
    let v_new : fractional a = Some (x, full_perm) in
    rewrite_slprop (pts_to r full_perm v) (RP.pts_to r v_old `star` pure (perm_ok full_perm)) (fun _ -> ());

    elim_pure (perm_ok full_perm);

    RP.atomic_write r v_old v_new;
    rewrite_slprop (RP.pts_to r v_new) (pts_to r full_perm x)
        (fun m -> emp_unit (hp_of (pts_to_raw r full_perm x));
          pure_star_interp (hp_of (pts_to_raw r full_perm x)) (perm_ok full_perm) m)

let free (#a:Type) (#v:erased a) (r:ref a)
  : SteelT unit (pts_to r full_perm v) (fun _ -> emp)
  = let v_old : erased (fractional a) = Ghost.hide (Some (Ghost.reveal v, full_perm)) in
    rewrite_slprop
      (pts_to r full_perm v)
      (RP.pts_to r v_old `star` pure (perm_ok full_perm))
      (fun _ -> ());
    elim_pure (perm_ok full_perm);
    RP.free r v_old;
    drop (RP.pts_to r (Mkpcm'?.one (Mkpcm?.p pcm_frac)))

let share_atomic_raw #a #uses (#p:perm) (r:ref a{perm_ok p}) (v0:erased a)
  : SteelGhostT unit uses
                (pts_to_raw r p v0)
                (fun _ -> pts_to_raw r (half_perm p) v0 `star` pts_to_raw r (half_perm p) v0)
  = rewrite_slprop
      (pts_to_raw r p v0)
      (RP.pts_to r _)
      (fun _ -> ());
    RP.split r (Some (Ghost.reveal v0, p)) (Some (Ghost.reveal v0, half_perm p)) (Some (Ghost.reveal v0, half_perm p));
    rewrite_slprop
      (RP.pts_to r _)
      (pts_to_raw r (half_perm p) v0)
      (fun _ -> ());
    rewrite_slprop
      (RP.pts_to r _)
      (pts_to_raw r (half_perm p) v0)
      (fun _ -> ())

let share (#a:Type) #uses (#p:perm) (#v:erased a) (r:ref a)
  : SteelGhostT unit uses
               (pts_to r p v)
               (fun _ -> pts_to r (half_perm p) v `star` pts_to r (half_perm p) v)
  = let v_old : erased (fractional a) = Ghost.hide (Some (Ghost.reveal v, p)) in
    rewrite_slprop
      (pts_to r p v)
      (pts_to' r p v)
      (fun _ -> ());
    elim_pure (perm_ok p);
    share_atomic_raw r v;
    intro_pts_to (half_perm p) r;
    intro_pts_to (half_perm p) r

let gather_atomic_raw (#a:Type) (#uses:_) (#p0 #p1:perm) (r:ref a) (v0:erased a) (v1:erased a)
  : SteelGhostT (_:unit{v0==v1 /\ perm_ok (sum_perm p0 p1)}) uses
                 (pts_to_raw r p0 v0 `star` pts_to_raw r p1 v1)
                 (fun _ -> pts_to_raw r (sum_perm p0 p1) v0)
  = 
    rewrite_slprop
      (pts_to_raw r p0 v0)
      (RP.pts_to r (Ghost.reveal (Some (Ghost.reveal v0, p0))))
      (fun _ -> ());
    rewrite_slprop
      (pts_to_raw r p1 v1)
      (RP.pts_to r (Ghost.reveal (Some (Ghost.reveal v1, p1))))
      (fun _ -> ());
    let _ = RP.gather r (Some (Ghost.reveal v0, p0)) (Some (Ghost.reveal v1, p1)) in
    rewrite_slprop
      (RP.pts_to r _)
      (pts_to_raw r (sum_perm p0 p1) v0)
      (fun _ -> ())

let gather (#a:Type) (#uses:_) (#p0:perm) (#p1:perm) (#v0 #v1:erased a) (r:ref a)
  = let v0_old : erased (fractional a) = Ghost.hide (Some (Ghost.reveal v0, p0)) in
    let v1_old : erased (fractional a) = Ghost.hide (Some (Ghost.reveal v1, p1)) in
    rewrite_slprop
      (pts_to r p0 v0)
      (pts_to_raw r p0 v0 `star` pure (perm_ok p0))
      (fun _ -> ());
    rewrite_slprop
      (pts_to r p1 v1)
      (pts_to_raw r p1 v1 `star` pure (perm_ok p1))
      (fun _ -> ());
    elim_pure (perm_ok p0);
    elim_pure (perm_ok p1);
    let _ = gather_atomic_raw r v0 v1 in
    intro_pts_to (sum_perm p0 p1) r

let cas_provides #t (r:ref t) (v:Ghost.erased t) (v_new:t) (b:bool) =
    if b then pts_to_sl r full_perm v_new else pts_to_sl r full_perm v

let equiv_ext_right (p q r:slprop)
  : Lemma
      (requires q `Mem.equiv` r)
val equiv_ext_right (p q r: slprop)
    : Lemma (requires q `Mem.equiv` r) (ensures Mem.((p `star` q) `equiv` (p `star` r)))
p: Steel.Memory.slprop -> q: Steel.Memory.slprop -> r: Steel.Memory.slprop
  -> FStar.Pervasives.Lemma (requires Steel.Memory.equiv q r)
      (ensures Steel.Memory.equiv (Steel.Memory.star p q) (Steel.Memory.star p r))