val extract_pure
      (#a #uses #p #f: _)
      (r: ref a p)
      (v: Ghost.erased a)
      (h: Ghost.erased (history a p))
    : SteelGhostT (_: unit{history_val h v f})
      uses
      (pts_to_body r f v h)
      (fun _ -> pts_to_body r f v h)
let extract_pure #a #uses #p #f
                 (r:ref a p)
                 (v:Ghost.erased a)
                 (h:Ghost.erased (history a p))
  : SteelGhostT (_:unit{history_val h v f})
           uses
           (pts_to_body r f v h)
           (fun _ -> pts_to_body r f v h)
  = rewrite_slprop
      (pts_to_body r f v h)
      (PR.pts_to r h `star` pure (history_val h v f))
      (fun _ -> ());
    elim_pure (history_val h v f);
    rewrite_slprop (PR.pts_to r h) (pts_to_body r f v h) (fun m ->
      emp_unit (M.pts_to r h);
      pure_star_interp (M.pts_to r h) (history_val h v f) m
    )
val extract_pure
      (#a #uses #p #f: _)
      (r: ref a p)
      (v: Ghost.erased a)
      (h: Ghost.erased (history a p))
    : SteelGhostT (_: unit{history_val h v f})
      uses
      (pts_to_body r f v h)
      (fun _ -> pts_to_body r f v h)
let extract_pure #a #uses #p #f (r: ref a p) (v: Ghost.erased a) (h: Ghost.erased (history a p))
    : SteelGhostT (_: unit{history_val h v f})
      uses
      (pts_to_body r f v h)
      (fun _ -> pts_to_body r f v h) =
rewrite_slprop (pts_to_body r f v h)
  ((PR.pts_to r h) `star` (pure (history_val h v f)))
  (fun _ -> ());
elim_pure (history_val h v f);
rewrite_slprop (PR.pts_to r h)
  (pts_to_body r f v h)
  (fun m ->
      emp_unit (M.pts_to r h);
      pure_star_interp (M.pts_to r h) (history_val h v f) m)
Steel.MonotonicHigherReference.fst.checked
Steel.Preorder.fst.checked Steel.PCMReference.fsti.checked Steel.Memory.fsti.checked Steel.FractionalPermission.fst.checked Steel.Effect.Atomic.fsti.checked Steel.Effect.fsti.checked prims.fst.checked FStar.Real.fsti.checked FStar.Preorder.fst.checked FStar.Pervasives.fsti.checked FStar.PCM.fst.checked FStar.Ghost.fsti.checked
Steel.MonotonicHigherReference.fst
Steel.Memory.inames FStar.Preorder.preorder Steel.FractionalPermission.perm Steel.MonotonicHigherReference.ref FStar.Ghost.erased Steel.Preorder.history Steel.Effect.Atomic.rewrite_slprop Steel.PCMReference.pts_to Steel.Preorder.pcm_history FStar.Ghost.reveal Steel.MonotonicHigherReference.pts_to_body Steel.Memory.mem Steel.Memory.pure_star_interp Steel.Memory.pts_to Steel.Preorder.history_val Prims.unit Steel.Memory.emp_unit Steel.Effect.Atomic.elim_pure Steel.Effect.Common.star Steel.Effect.Common.pure Steel.Effect.Common.vprop
(*
   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.MonotonicHigherReference

open FStar.Ghost
open FStar.PCM
open Steel.Memory
open Steel.Effect.Atomic
open Steel.Effect
open Steel.PCMReference
open Steel.FractionalPermission
open Steel.Preorder

module Preorder = FStar.Preorder
module Q = Steel.Preorder
module M = Steel.Memory
module PR = Steel.PCMReference
open FStar.Real

#set-options "--ide_id_info_off"

let ref a p = M.ref (history a p) pcm_history

[@@__reduce__]
let pts_to_body #a #p (r:ref a p) (f:perm) (v:Ghost.erased a) (h:history a p) =
      PR.pts_to r h `star`
      pure (history_val h v f)

let pts_to' (#a:Type) (#p:Preorder.preorder a) (r:ref a p) (f:perm) (v:Ghost.erased a) =
    h_exists (pts_to_body r f v)

let pts_to_sl r f v = hp_of (pts_to' r f v)

let intro_pure #a #p #f
                 (r:ref a p)
                 (v:a)
                 (h:history a p { history_val h v f })
  : SteelT unit
           (PR.pts_to r h)
           (fun _ -> pts_to_body r f v h)
  = rewrite_slprop (PR.pts_to r h) (pts_to_body _ _ _ _) (fun m ->
      emp_unit (M.pts_to r h);
      pure_star_interp (M.pts_to r h) (history_val h v f) m)

let intro_pure_full #a #p #f
                 (r:ref a p)
                 (v:a)
                 (h:history a p { history_val h v f })
  : SteelT unit
           (PR.pts_to r h)
           (fun _ -> pts_to r f v)
  = intro_pure #a #p #f r v h;
    intro_exists h (pts_to_body r f v)


let alloc (#a:Type) (p:Preorder.preorder a) (v:a)
  = let h = Current [v] full_perm in
    assert (compatible pcm_history h h);
    let x : ref a p = alloc h in
    intro_pure_full x v h;
    x

let extract_pure #a #uses #p #f
                 (r:ref a p)
                 (v:Ghost.erased a)
                 (h:Ghost.erased (history a p))
  : SteelGhostT (_:unit{history_val h v f})
           uses
           (pts_to_body r f v h)
val extract_pure
      (#a #uses #p #f: _)
      (r: ref a p)
      (v: Ghost.erased a)
      (h: Ghost.erased (history a p))
    : SteelGhostT (_: unit{history_val h v f})
      uses
      (pts_to_body r f v h)
      (fun _ -> pts_to_body r f v h)

    r: Steel.MonotonicHigherReference.ref a p ->
    v: FStar.Ghost.erased a ->
    h: FStar.Ghost.erased (Steel.Preorder.history a p)
  -> Steel.Effect.Atomic.SteelGhostT
    (_: Prims.unit{Steel.Preorder.history_val (FStar.Ghost.reveal h) v f})