val for_loop_full
      (start: US.t)
      (finish: US.t{US.v start <= US.v finish})
      (inv: (nat_at_most finish -> vprop))
      (inv_sel: (i: nat_at_most finish -> t_of (inv i) -> prop))
      (body:
          (i: u32_between start finish
              -> Steel unit
                  (inv (US.v i))
                  (fun _ -> inv (US.v i + 1))
                  (requires fun h -> inv_sel (US.v i) (h (inv (US.v i))))
                  (ensures
                    fun h0 _ h1 ->
                      inv_sel (US.v i) (h0 (inv (US.v i))) /\
                      inv_sel (US.v i + 1) (h1 (inv (US.v i + 1))))))
    : Steel unit
      (inv (US.v start))
      (fun _ -> inv (US.v finish))
      (requires fun h -> inv_sel (US.v start) (h (inv (US.v start))))
      (ensures
        fun h0 _ h1 ->
          inv_sel (US.v start) (h0 (inv (US.v start))) /\
          inv_sel (US.v finish) (h1 (inv (US.v finish))))
val for_loop_full
      (start: US.t)
      (finish: US.t{US.v start <= US.v finish})
      (inv: (nat_at_most finish -> vprop))
      (inv_sel: (i: nat_at_most finish -> t_of (inv i) -> prop))
      (body:
          (i: u32_between start finish
              -> Steel unit
                  (inv (US.v i))
                  (fun _ -> inv (US.v i + 1))
                  (requires fun h -> inv_sel (US.v i) (h (inv (US.v i))))
                  (ensures
                    fun h0 _ h1 ->
                      inv_sel (US.v i) (h0 (inv (US.v i))) /\
                      inv_sel (US.v i + 1) (h1 (inv (US.v i + 1))))))
    : Steel unit
      (inv (US.v start))
      (fun _ -> inv (US.v finish))
      (requires fun h -> inv_sel (US.v start) (h (inv (US.v start))))
      (ensures
        fun h0 _ h1 ->
          inv_sel (US.v start) (h0 (inv (US.v start))) /\
          inv_sel (US.v finish) (h1 (inv (US.v finish))))
let for_loop_full
  (start:US.t)
  (finish:US.t { US.v start <= US.v finish })
  (inv: nat_at_most finish -> vprop)
  (inv_sel: (i:nat_at_most finish) -> t_of (inv i) -> prop)
  (body:
    (i:u32_between start finish ->
    Steel unit
    (inv (US.v i))
    (fun _ -> inv (US.v i + 1))
    (requires fun h -> inv_sel (US.v i) (h (inv (US.v i))))
    (ensures fun h0 _ h1 ->
      inv_sel (US.v i) (h0 (inv (US.v i))) /\
      inv_sel (US.v i + 1) (h1 (inv (US.v i + 1)))
    )))
: Steel unit
    (inv (US.v start))
    (fun _ -> inv (US.v finish))
    (requires fun h -> inv_sel (US.v start) (h (inv (US.v start))))
    (ensures fun h0 _ h1 ->
      inv_sel (US.v start) (h0 (inv (US.v start))) /\
      inv_sel (US.v finish) (h1 (inv (US.v finish)))
    )
= AT.intro_vrefine (inv (US.v start)) (inv_sel (US.v start));
  for_loop start finish
    (fun n -> inv n `vrefine` inv_sel n)
    (fun i ->
      AT.elim_vrefine (inv (US.v i)) (inv_sel (US.v i));
      body i;
      AT.intro_vrefine (inv (US.v i + 1)) (inv_sel (US.v i + 1))
    );
  AT.elim_vrefine (inv (US.v finish)) (inv_sel (US.v finish))
(*
   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.Effect.Common
open Steel.Effect
module AT = Steel.Effect.Atomic
module US = FStar.SizeT

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

let nat_at_most (f:US.t)
  = x:nat{ x <= US.v f }

let u32_between (s f:US.t)
  = x:US.t { US.v s <= US.v x /\ US.v x < US.v f}

/// for_loop: for (i = start; i < finish; i++) inv { body i }
val 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 ->
                          SteelT unit
                          (inv (US.v i))
                          (fun _ -> inv (US.v i + 1))))
  : SteelT unit
      (inv (US.v start))
      (fun _ -> inv (US.v finish))

inline_for_extraction
/
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 FStar.Ghost.fsti.checked
Steel.Loops.fsti

    start: FStar.SizeT.t ->
    finish: FStar.SizeT.t{FStar.SizeT.v start <= FStar.SizeT.v finish} ->
    inv: (_: Steel.Loops.nat_at_most finish -> Steel.Effect.Common.vprop) ->
    inv_sel:
      (i: Steel.Loops.nat_at_most finish -> _: Steel.Effect.Common.t_of (inv i) -> Prims.prop) ->
    body: (i: Steel.Loops.u32_between start finish -> Steel.Effect.Steel Prims.unit)
  -> Steel.Effect.Steel Prims.unit
FStar.SizeT.t Prims.b2t Prims.op_LessThanOrEqual FStar.SizeT.v Steel.Loops.nat_at_most Steel.Effect.Common.vprop Steel.Effect.Common.t_of Prims.prop Steel.Loops.u32_between Prims.unit Prims.op_Addition Steel.Effect.Common.rmem Prims.l_and Steel.Effect.Atomic.elim_vrefine FStar.Ghost.hide FStar.Set.set Steel.Memory.iname FStar.Set.empty Steel.Loops.for_loop Steel.Effect.Common.vrefine Steel.Effect.Atomic.intro_vrefine
let for_loop_full
      (start: US.t)
      (finish: US.t{US.v start <= US.v finish})
      (inv: (nat_at_most finish -> vprop))
      (inv_sel: (i: nat_at_most finish -> t_of (inv i) -> prop))
      (body:
          (i: u32_between start finish
              -> Steel unit
                  (inv (US.v i))
                  (fun _ -> inv (US.v i + 1))
                  (requires fun h -> inv_sel (US.v i) (h (inv (US.v i))))
                  (ensures
                    fun h0 _ h1 ->
                      inv_sel (US.v i) (h0 (inv (US.v i))) /\
                      inv_sel (US.v i + 1) (h1 (inv (US.v i + 1))))))
    : Steel unit
      (inv (US.v start))
      (fun _ -> inv (US.v finish))
      (requires fun h -> inv_sel (US.v start) (h (inv (US.v start))))
      (ensures
        fun h0 _ h1 ->
          inv_sel (US.v start) (h0 (inv (US.v start))) /\
          inv_sel (US.v finish) (h1 (inv (US.v finish)))) =
  
AT.intro_vrefine (inv (US.v start)) (inv_sel (US.v start));
for_loop start
  finish
  (fun n -> (inv n) `vrefine` (inv_sel n))
  (fun i ->
      AT.elim_vrefine (inv (US.v i)) (inv_sel (US.v i));
      body i;
      AT.intro_vrefine (inv (US.v i + 1)) (inv_sel (US.v i + 1)));
AT.elim_vrefine (inv (US.v finish)) (inv_sel (US.v finish))