val intro_chan_inv_cond_stepT (vs vr: chan_val)
    : SteelT unit (chan_inv_step vr vs) (fun _ -> chan_inv_cond vs vr)
let intro_chan_inv_cond_stepT (vs vr:chan_val)
  : SteelT unit (chan_inv_step vr vs)
                (fun _ -> chan_inv_cond vs vr)
  = Steel.Utils.extract_pure (chan_inv_step_p vr vs);
    rewrite_slprop (chan_inv_step vr vs) (chan_inv_cond vs vr) (fun _ -> ())
val intro_chan_inv_cond_stepT (vs vr: chan_val)
    : SteelT unit (chan_inv_step vr vs) (fun _ -> chan_inv_cond vs vr)
let intro_chan_inv_cond_stepT (vs vr: chan_val)
    : SteelT unit (chan_inv_step vr vs) (fun _ -> chan_inv_cond vs vr) =
Steel.Utils.extract_pure (chan_inv_step_p vr vs);
rewrite_slprop (chan_inv_step vr vs) (chan_inv_cond vs vr) (fun _ -> ())
Steel.Channel.Simplex.fst.checked
Steel.Utils.fst.checked Steel.SpinLock.fsti.checked Steel.MonotonicHigherReference.fsti.checked Steel.Memory.fsti.checked Steel.HigherReference.fsti.checked Steel.FractionalPermission.fst.checked Steel.Effect.Atomic.fsti.checked Steel.Effect.fsti.checked Steel.Channel.Protocol.fst.checked prims.fst.checked FStar.Pervasives.Native.fst.checked FStar.Pervasives.fsti.checked
Steel.Channel.Simplex.fst
Steel.Channel.Simplex.chan_val Steel.Effect.Atomic.rewrite_slprop FStar.Ghost.hide FStar.Set.set Steel.Memory.iname FStar.Set.empty Steel.Channel.Simplex.chan_inv_step Steel.Channel.Simplex.chan_inv_cond Steel.Memory.mem Prims.unit Steel.Utils.extract_pure Steel.Channel.Simplex.chan_inv_step_p 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.Channel.Simplex
module P = Steel.Channel.Protocol
open Steel.SpinLock
open Steel.Memory
open Steel.Effect.Atomic
open Steel.Effect
open Steel.HigherReference
open Steel.FractionalPermission
module MRef = Steel.MonotonicHigherReference
module H = Steel.HigherReference

let sprot = p:prot { more p }

noeq
type chan_val = {
  chan_prot : sprot;
  chan_msg  : msg_t chan_prot;
  chan_ctr  : nat
}

let mref a p = MRef.ref a p
let trace_ref (p:prot) = mref (partial_trace_of p) extended_to

noeq
type chan_t (p:prot) = {
  send: ref chan_val;
  recv: ref chan_val;
  trace: trace_ref p;
}

let half : perm = half_perm full_perm

let step (s:sprot) (x:msg_t s) = step s x

let chan_inv_step_p (vrecv vsend:chan_val) : prop =
  (vsend.chan_prot == step vrecv.chan_prot vrecv.chan_msg /\
   vsend.chan_ctr == vrecv.chan_ctr + 1)

let chan_inv_step (vrecv vsend:chan_val) : vprop =
  pure (chan_inv_step_p vrecv vsend)

let chan_inv_cond (vsend:chan_val) (vrecv:chan_val) : vprop =
    if vsend.chan_ctr = vrecv.chan_ctr
    then pure (vsend == vrecv)
    else chan_inv_step vrecv vsend

let trace_until_prop #p (r:trace_ref p) (vr:chan_val) (tr: partial_trace_of p) : vprop =
  MRef.pts_to r full_perm tr `star`
  pure (until tr == step vr.chan_prot vr.chan_msg)

let trace_until #p (r:trace_ref p) (vr:chan_val) =
  h_exists (trace_until_prop r vr)

let chan_inv_recv #p (c:chan_t p) (vsend:chan_val) =
  h_exists (fun (vrecv:chan_val) ->
      pts_to c.recv half vrecv `star`
      trace_until c.trace vrecv `star`
      chan_inv_cond vsend vrecv)

let chan_inv #p (c:chan_t p) : vprop =
  h_exists (fun (vsend:chan_val) ->
    pts_to c.send half vsend `star` chan_inv_recv c vsend)

let intro_chan_inv_cond_eqT (vs vr:chan_val)
  : Steel unit emp
               (fun _ -> chan_inv_cond vs vr)
               (requires fun _ -> vs == vr)
               (ensures fun _ _ _ -> True)
  = intro_pure (vs == vs);
    rewrite_slprop (chan_inv_cond vs vs) (chan_inv_cond vs vr) (fun _ -> ())

let intro_chan_inv_cond_stepT (vs vr:chan_val)
  : SteelT unit (chan_inv_step vr vs)
val intro_chan_inv_cond_stepT (vs vr: chan_val)
    : SteelT unit (chan_inv_step vr vs) (fun _ -> chan_inv_cond vs vr)
vs: Steel.Channel.Simplex.chan_val -> vr: Steel.Channel.Simplex.chan_val
  -> Steel.Effect.SteelT Prims.unit