val while_loop'
      (inv: (Ghost.erased bool -> vprop))
      ($cond: (unit -> STT bool (exists_ inv) (fun b -> inv b)))
      ($body: (unit -> STT unit (inv true) (fun _ -> exists_ inv)))
    : SE.SteelT unit (SEA.h_exists inv) (fun _ -> inv false)
let while_loop' (inv: Ghost.erased bool -> vprop)
                ($cond: (unit -> STT bool
                                     (exists_ inv)
                                     (fun b -> inv b)))
                ($body: (unit -> STT unit
                                     (inv true)
                                     (fun _ -> exists_ inv)))
  : SE.SteelT unit
              (SEA.h_exists inv)
              (fun _ -> inv false)
  = L.while_loop
       inv
       (coerce_cond inv cond)
       (coerce_body inv body)
val while_loop'
      (inv: (Ghost.erased bool -> vprop))
      ($cond: (unit -> STT bool (exists_ inv) (fun b -> inv b)))
      ($body: (unit -> STT unit (inv true) (fun _ -> exists_ inv)))
    : SE.SteelT unit (SEA.h_exists inv) (fun _ -> inv false)
let while_loop'
      (inv: (Ghost.erased bool -> vprop))
      ($cond: (unit -> STT bool (exists_ inv) (fun b -> inv b)))
      ($body: (unit -> STT unit (inv true) (fun _ -> exists_ inv)))
    : SE.SteelT unit (SEA.h_exists inv) (fun _ -> inv false) =
L.while_loop inv (coerce_cond inv cond) (coerce_body inv body)
Steel.ST.Loops.fst.checked
Steel.ST.Util.fsti.checked Steel.ST.Coercions.fsti.checked Steel.Loops.fsti.checked Steel.Effect.Atomic.fsti.checked Steel.Effect.fsti.checked prims.fst.checked FStar.SizeT.fsti.checked FStar.Pervasives.fsti.checked FStar.Ghost.fsti.checked
Steel.ST.Loops.fst
FStar.Ghost.erased Prims.bool Steel.Effect.Common.vprop Prims.unit Steel.ST.Util.exists_ FStar.Ghost.hide Steel.Loops.while_loop Steel.ST.Loops.coerce_cond Steel.ST.Loops.coerce_body Steel.Effect.Atomic.h_exists
(*
   Copyright 2021 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.ST.Loops
module US = FStar.SizeT
open Steel.ST.Util
open Steel.ST.Coercions
module L = Steel.Loops
module SE = Steel.Effect
module SEA = Steel.Effect.Atomic
open FStar.Ghost

(* This module provides some common iterative looping combinators *)

let for_loop' (start:US.t)
              (finish:US.t { US.v start <= US.v finish })
              (inv: nat_at_most finish -> vprop)
              ($body:
                    (i:u32_between start finish ->
                          STT unit
                          (inv (US.v i))
                          (fun _ -> inv (US.v i + 1))))
  : SE.SteelT unit
      (inv (US.v start))
      (fun _ -> inv (US.v finish))
  = let body (i:u32_between start finish)
      : SE.SteelT unit
                  (inv (US.v i))
                  (fun _ -> inv (US.v i + 1))
      = body i
    in
    L.for_loop start finish inv body //lift works for body; NS 12/10: Need to eta expand for the lift

let for_loop (start:US.t)
             (finish:US.t { US.v start <= US.v finish })
             (inv: nat_at_most finish -> vprop)
             (body:
                    (i:u32_between start finish ->
                          STT unit
                          (inv (US.v i))
                          (fun _ -> inv (US.v i + 1))))
  : STT unit
      (inv (US.v start))
      (fun _ -> inv (US.v finish))
  = coerce_steel (fun _ -> for_loop' start finish inv body); ()

let elim_h_exists (#a:Type) #o (p:a -> vprop)
  : STGhostT (Ghost.erased a) o
            (SEA.h_exists p)
            (fun v -> p v)
  = let x = coerce_ghost SEA.witness_exists in x

let intro_h_exists (#a:Type) #o (v:Ghost.erased a) (p:a -> vprop)
  : STGhostT unit o
            (p v)
            (fun _ -> SEA.h_exists p)
  = let _ = coerce_ghost (fun _ -> SEA.intro_exists_erased v p) in ()

let exists_to_h_exists (#a:Type) #o (p:a -> vprop)
  : STGhostT unit o (exists_ p)
                  (fun _ -> SEA.h_exists p)
  = let w = elim_exists () in
    intro_h_exists w p

let coerce_cond (inv: Ghost.erased bool -> vprop)
                (cond: (unit -> STT bool
                                   (exists_ inv)
                                   (fun b -> inv b)))
                (_:unit)
  : SE.SteelT bool (SEA.h_exists inv) (fun b -> inv b)
  = let w = elim_h_exists inv in
    intro_exists_erased w inv;
    cond ()

let coerce_body (inv: Ghost.erased bool -> vprop)
                (body: (unit -> STT unit
                                   (inv true)
                                   (fun _ -> exists_ inv)))
                (_:unit)
  : SE.SteelT unit (inv true) (fun _ -> SEA.h_exists inv)
  = let b = body () in
    let w = elim_exists () in
    intro_h_exists w inv;
    return b

/// while_loop: while (cond()) { body () }
let while_loop' (inv: Ghost.erased bool -> vprop)
                ($cond: (unit -> STT bool
                                     (exists_ inv)
                                     (fun b -> inv b)))
                ($body: (unit -> STT unit
                                     (inv true)
                                     (fun _ -> exists_ inv)))
  : SE.SteelT unit
              (SEA.h_exists inv)
val while_loop'
      (inv: (Ghost.erased bool -> vprop))
      ($cond: (unit -> STT bool (exists_ inv) (fun b -> inv b)))
      ($body: (unit -> STT unit (inv true) (fun _ -> exists_ inv)))
    : SE.SteelT unit (SEA.h_exists inv) (fun _ -> inv false)

    inv: (_: FStar.Ghost.erased Prims.bool -> Steel.Effect.Common.vprop) ->
    $cond: (_: Prims.unit -> Steel.ST.Effect.STT Prims.bool) ->
    $body: (_: Prims.unit -> Steel.ST.Effect.STT Prims.unit)
  -> Steel.Effect.SteelT Prims.unit