val pts_to_not_null (#a:Type u#1)
                    (x:ref a)
                    (p:perm)
                    (v:a)
                    (m:mem)
  : Lemma (requires interp (pts_to_sl x p v) m)
          (ensures x =!= null)
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
val pts_to_not_null (#a:Type u#1)
                    (x:ref a)
                    (p:perm)
                    (v:a)
                    (m:mem)
  : Lemma (requires interp (pts_to_sl x p v) m)
          (ensures x =!= null)
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
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.HigherReference.ref Steel.FractionalPermission.perm Steel.Memory.mem Steel.Memory.pts_to_not_null Steel.PCMFrac.fractional Steel.PCMFrac.pcm_frac FStar.Pervasives.Native.Some FStar.Pervasives.Native.tuple2 FStar.Pervasives.Native.Mktuple2 FStar.Ghost.reveal FStar.Ghost.hide Prims.unit Steel.Memory.affine_star Steel.Effect.Common.hp_of Steel.HigherReference.pts_to_raw Steel.Memory.pure Steel.HigherReference.perm_ok Steel.Memory.interp Steel.HigherReference.pts_to_sl Prims.squash Prims.l_not Prims.eq2 Steel.HigherReference.null Prims.Nil 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)
val pts_to_not_null (#a:Type u#1)
                    (x:ref a)
                    (p:perm)
                    (v:a)
                    (m:mem)
  : Lemma (requires interp (pts_to_sl x p v) m)
          (ensures x =!= null)
x: Steel.HigherReference.ref a -> p: Steel.FractionalPermission.perm -> v: a -> m: Steel.Memory.mem
  -> FStar.Pervasives.Lemma (requires Steel.Memory.interp (Steel.HigherReference.pts_to_sl x p v) m)
      (ensures ~(x == Steel.HigherReference.null))