val while_loop (inv: Ghost.erased bool -> vprop)
               (cond: (unit -> SteelT bool
                                     (AT.h_exists inv)
                                     (fun b -> inv b)))
               (body: (unit -> SteelT unit
                                     (inv true)
                                     (fun _ -> AT.h_exists inv)))
  : SteelT unit
    (AT.h_exists inv)
     (fun _ -> inv false)
let rec while_loop inv cond body =
  let b = cond () in
  if b
  then (
     AT.change_equal_slprop (inv b) (inv true);
     body();
     while_loop inv cond body
  )
  else (
     AT.change_equal_slprop (inv b) (inv false);
     AT.return ()
  )
val while_loop (inv: Ghost.erased bool -> vprop)
               (cond: (unit -> SteelT bool
                                     (AT.h_exists inv)
                                     (fun b -> inv b)))
               (body: (unit -> SteelT unit
                                     (inv true)
                                     (fun _ -> AT.h_exists inv)))
  : SteelT unit
    (AT.h_exists inv)
     (fun _ -> inv false)
let rec while_loop inv cond body =
let b = cond () in
if b
then
  (AT.change_equal_slprop (inv b) (inv true);
    body ();
    while_loop inv cond body)
else
  (AT.change_equal_slprop (inv b) (inv false);
    AT.return ())
Steel.Loops.fst.checked
Steel.Reference.fsti.checked Steel.Memory.fsti.checked Steel.Effect.Common.fsti.checked Steel.Effect.Atomic.fsti.checked Steel.Effect.fsti.checked prims.fst.checked FStar.SizeT.fsti.checked FStar.Pervasives.fsti.checked
Steel.Loops.fst
FStar.Ghost.erased Prims.bool Steel.Effect.Common.vprop Prims.unit Steel.Effect.Atomic.h_exists FStar.Ghost.hide Steel.Loops.while_loop Steel.Effect.Atomic.change_equal_slprop FStar.Set.set Steel.Memory.iname FStar.Set.empty Steel.Effect.Atomic.return
(*
   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.Loops
open Steel.Memory
open Steel.Effect.Common
open Steel.Effect
open Steel.Reference
module AT = Steel.Effect.Atomic
module US = FStar.SizeT

val for_loop' (start:US.t)
              (finish:US.t { US.v start <= US.v finish })
              (current:US.t { US.v start <= US.v current /\
                               US.v current <= US.v finish })
              (inv: nat_at_most finish -> vprop)
              (body:
                    (i:u32_between start finish ->
                          SteelT unit
                          (inv (US.v i))
                          (fun _ -> inv (US.v i + 1))))
  : SteelT unit
      (inv (US.v current))
      (fun _ -> inv (US.v finish))

#push-options "--fuel 0 --ifuel 0"
let rec for_loop' start finish current inv body
  = if current = finish then (
       AT.change_equal_slprop (inv _) (inv _);
       AT.return ()
    )
    else (
      let _: squash (US.v current < US.v finish) = () in //fails without this
      body current;
      let current' = US.(current +^ 1sz) in
      AT.change_equal_slprop (inv (US.v current + 1))
                             (inv (US.v current'));
      for_loop' start finish current' inv body
    )
#pop-options

(* produces 11 queries *)
let for_loop start finish inv body = for_loop' start finish start inv body
val while_loop (inv: Ghost.erased bool -> vprop)
               (cond: (unit -> SteelT bool
                                     (AT.h_exists inv)
                                     (fun b -> inv b)))
               (body: (unit -> SteelT unit
                                     (inv true)
                                     (fun _ -> AT.h_exists inv)))
  : SteelT unit
    (AT.h_exists inv)
     (fun _ -> inv false)

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