val ist_witnessed : p:
      FStar.Preorder.predicate FStar.Monotonic.Heap.heap
        {FStar.Preorder.stable p ImmutableSTwHeaps.heap_rel}
  -> Type0
let ist_witnessed (p:predicate heap{stable p heap_rel}) = witnessed heap_rel p
(*
   Copyright 2008-2018 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 ImmutableSTwHeaps

open FStar.Heap
open FStar.Preorder
open FStar.Monotonic.Witnessed

//giving ourselves two non-ghost versions of the heap sel/upd functions
assume val sel: h:heap -> r:ref 'a -> Tot (x:'a{x == Heap.sel h r})
assume val upd: h:heap -> r:ref 'a -> v:'a -> Tot (h':heap{h' == Heap.upd h r v})

(* Two heaps are related if the latter differs from the former only by new 
   allocated references and all existing content is preserved unchanged. *)

let heap_rel (h0:heap) (h1:heap) = 
  forall a (r: ref a) . contains h0 r ==> contains h1 r /\ sel h0 r == sel h1 r


(* *************************************************** *)

(* 
   A temporary definition of preorder-indexed state 
   monads specialized to the immutable references 
   instance, in order to make sub-effecting to work. 
   Using (FStar.Heap.heap) and (heap_rel) for the 
   statespace and the relation on it, which otherwise 
   would be given by parameters.
*)


(* Preconditions, postconditions and WPs for the preorder-indexed state monad. *)

let ist_pre  (state:Type)          = state -> Type0
let ist_post (state:Type) (a:Type) = a -> state -> Type0
let ist_wp   (state:Type) (a:Type) = ist_post state a -> Tot (ist_pre state)


(* A WP-style preorder-indexed state monad specialised for the immutable references instance. *)

new_effect ISTATE = STATE_h heap


(* DIV is a sub-effect/sub-monad of the immutable references instance of the preorder-indexed monad. *)

unfold let lift_div_istate (state:Type) (rel:preorder state)
                           (a:Type) (wp:pure_wp a) (p:ist_post state a) (s:state) = wp (fun x -> p x s)
sub_effect DIV ~> ISTATE = lift_div_istate heap heap_rel


(*A pre- and postcondition version of this preorder-indexed state monad. *)

effect IST    (a:Type) 
              (pre:ist_pre heap) 
	      (post:(heap -> Tot (ist_post heap a))) 
       =
       ISTATE a (fun p s0 -> pre s0 /\ (forall x s1 . pre s0 /\ post s0 x s1 ==> p x s1))


(* A box-like modality for witnessed stable predicates for IST. *)
/
prims.fst.checked FStar.Preorder.fst.checked FStar.Pervasives.fsti.checked FStar.Monotonic.Witnessed.fsti.checked FStar.Heap.fst.checked
ImmutableSTwHeaps.fst

    p:
      FStar.Preorder.predicate FStar.Monotonic.Heap.heap
        {FStar.Preorder.stable p ImmutableSTwHeaps.heap_rel}
  -> Type0
FStar.Preorder.predicate FStar.Monotonic.Heap.heap FStar.Preorder.stable ImmutableSTwHeaps.heap_rel FStar.Monotonic.Witnessed.witnessed
let ist_witnessed (p: predicate heap {stable p heap_rel}) =
  
witnessed heap_rel p