The closure library, part II: loops #
The generic reasoning principle for loop (Eval.loop_of_invariant: an invariant, a
strictly decreasing measure and a uniform bound on the body's cost give a run of the loop
with cost ≤ (μ + 1) · (B + 1)), and the list and unary-numeral primitives of the library
proved by direct induction: reversal onto an accumulator (revOntoProg, revProg) and
length in unary (lenProg). Lists are cons-chains (Data.list); unary numerals are lists
of nils (Data.ofNat).
Conventions: every program is closed and takes its input in variable 0; a loop's state
is the input itself or a pair assembled by let_ in front of the loop; the body stops with
cons nil result and continues with cons (cons nil nil) state'. Costs are stated as
bounds of the form (iterations + 1) · (S + c) where S is any number dominating the sizes
involved, so that the bound is one atom for omega throughout an induction.
Lists as data #
Lists of data as cons-chains.
Equations
Instances For
The loop principle #
Loop with an invariant and a measure. If from every state s satisfying I the
body either stops (with a result satisfying Q) or continues to a state s' satisfying
I with μ s' < μ s, at cost at most B in both cases, then loop b from any state
satisfying I stops with a result satisfying Q, at cost at most (μ s + 1) · (B + 1).
Reversal onto an accumulator #
Body of revOntoProg: on state cons xs acc, stop with acc if xs is empty,
otherwise move the head of xs onto acc.
Equations
- One or more equations did not get rendered due to their size.
Instances For
revOntoProg on cons xs acc computes reverse xs ++ acc.
Instances For
One iteration of revOntoBody on a non-empty list.
Length in unary #
Body of lenProg: on state cons xs acc, stop with acc if xs is empty, otherwise
drop the head of xs and prepend a nil to acc. Running it to completion prepends
|xs| copies of nil to acc: with acc = nil this is the length in unary, with acc a
unary numeral it is addition, with acc = [true] it is the bit string of 2 ^ |xs|.
Equations
- One or more equations did not get rendered due to their size.