val 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 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
val 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 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
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.FractionalPermission.perm Steel.Effect.Common.vprop Steel.HigherReference.ref Steel.Effect.Atomic.return FStar.Ghost.hide FStar.Set.set Steel.Memory.iname FStar.Set.empty Steel.Effect.Common.VStar Steel.HigherReference.pts_to Prims.unit Steel.Effect.Atomic.rewrite_slprop FStar.Ghost.reveal Steel.Memory.mem Steel.HigherReference.read FStar.Ghost.erased Steel.Effect.Atomic.witness_exists Steel.Effect.Atomic.h_exists Steel.Effect.Common.star
(*
   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))
val 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)
q: (_: a -> Steel.Effect.Common.vprop) -> r: Steel.HigherReference.ref a -> Steel.Effect.SteelT a