In-place programs on Int8Array #
Int8Array.InPlace n α is a program that reads and writes an array of size n in place and
returns a value of type α. It is a monad, and programs are built from:
read i h,readThen i h k,write i h vandwriteThen i h v k, at aUSizeindexiwith its boundh(toNat_add_lt_of_windowgives the bound ofoff + jin a window);InPlace.iteandInPlace.dite, which branch inside one program;- the loops
forIn lo hiandforInRev lo hi, which run a body for eachjfromloup tohi - 1, or fromhi - 1down tolo, threading an accumulator. The body gets both bounds ofj,lo.toNat ≤ j.toNatandj.toNat < hi.toNat.forIn2andforInRev2thread two accumulators as separate arguments, so that scalars such as carries stay unboxed; forInBind,forIn2Bind,forInRevBindandforInRev2Bind, a loop followed by the rest of the program, which the loop calls itself, so that its last accumulators reach the rest unboxed.
A program runs on an array with a.runInPlace m or m.run a h, which make the array exclusive
once, before the program runs. runForInThen, runForInRevThen and runForIn2Then run a single
loop and hand its last accumulators and the new array to a continuation, without a result pair.
A tail program InPlace.Tail n γ is a program that ends by handing its result and the new array to
a continuation k, which returns a γ, where run would return the pair of them: Tail.done x k
ends it, Tail.bind m t runs an in-place program m before it, and Tail.readThen,
Tail.writeThen, Tail.ite, Tail.dite and the loops Tail.forInBind, … are those of InPlace.
The continuation runs inside the program's last loop, so its last accumulators reach k unboxed,
and no result pair is built. A tail program runs with a.runInPlaceThen t,
a.runInPlaceThenOf n h t or t.run a h, and on the window of a slice s in its array with
s.runInPlaceThenOf n h t.
InPlace.zipForInFrom, for internal use, is the loop of the two-array updates zipModifyStride:
it threads two states, one for each array.
Tail programs also have loops in continuation-passing style, Tail.forIn lo hi init body k and
Tail.forInRev, with up to four variables in Tail.forIn2, Tail.forIn3, Tail.forIn4, and so
on. Their body is itself a tail program, which ends by calling a continuation next on the next
variables, and the loop ends with k. Each variable is an argument of the loop, so scalars stay
unboxed, and a nested loop, whose k calls the outer next, compiles to mutually recursive loops
with no allocation. They are specified by their one-step equations (Tail.forInFrom_eq, …).
The array is never visible to a program: InPlace and its state have private constructors, so the
only code that touches the state is the code in this module, which passes it on linearly. That is
what lets write store without checking that the array is exclusive. The in-place folds of
ScalarArray.Int8.InPlace are programs of this kind, and the notation inplace a do … of
ScalarArray.Notation writes programs of this kind.
The loops must be the loops of this module. A program that calls itself recursively compiles to a
closure for each step, and for in do notation boxes its mutable variables at each step, so both
are much slower than forIn, whose step is inlined into one loop with unboxed accumulators.
Likewise, if c then t else e on programs builds both branches as closures, where InPlace.ite
branches inside the program.
Programs are specified by InPlace.run, whose behavior the run_* lemmas give. Each loop has a
one-step equation (forInFrom_eq, forInRevFrom_eq, …), and each Bind loop and runFor*Then
an _eq lemma in terms of the plain loop. bind_eq and pure_eq turn InPlace.bind and
InPlace.pure, which the notation writes, into >>= and pure.
The state of an in-place program on arrays of size n: the array itself. Its constructor is
private, so that no code outside this module can hold a state, or run a program on one.
- array : ScalarArray.Int8Array
Instances For
A program that reads and writes an array of size n in place and returns a value of type α.
See the module documentation.
Instances For
read i h followed by k on the element, for a program k in any universe: read itself returns
a Int8, so in do notation it binds only in programs whose results are in Type.
Instances For
The index off + j of the window of len elements at off is in bounds, for j < len. This is
the bound that read and write need in a loop over a window.
Stores v at index i. It compiles to one store, without checking that the array is exclusive:
run made it exclusive, and only this module's code holds it since.
Instances For
Runs t if c holds and e otherwise. Unlike if c then t else e, which chooses a program and
then runs it, it compiles to a branch inside one program, without building the programs of the two
branches as closures.
Instances For
Runs the program m on the array a, and returns its result and the new array. The array is made
exclusive first, so it is copied once if it is shared, and updated in place otherwise.
Instances For
Loops #
Runs body j hlo hj b for each j from j₀ up to hi - 1, threading the accumulator b from
init. forIn is the case j₀ = lo. It is specified by forInFrom_eq.
Instances For
Runs body j hlo hj b for each j from lo up to hi - 1, threading the accumulator b from
init, and returns the last accumulator. The body gets both bounds of j. It runs no step when
hi ≤ lo.
Instances For
forInFrom with two accumulators. It is specified by forIn2From_eq.
Instances For
forIn with two accumulators, which the loop keeps as two arguments, so that each one stays
unboxed when its type is a scalar, such as a carry and a borrow. A pair accumulator in forIn
would be allocated at every step.
Instances For
Runs body j hlo hj b for each j from j₀ - 1 down to lo, threading the accumulator b
from init. forInRev is the case j₀ = hi. It is specified by forInRevFrom_eq.
Instances For
Runs body j hlo hj b for each j from hi - 1 down to lo, threading the accumulator b from
init, and returns the last accumulator. The body gets both bounds of j. It runs no step when
hi ≤ lo.
Instances For
forInRevFrom with two accumulators. It is specified by forInRev2From_eq.
Instances For
forInRev with two accumulators, kept as two loop arguments like those of forIn2.
Instances For
Specification #
InPlace.pure is pure, so that run_pure applies to programs written with it.
forIn2From is forInFrom on the pair of accumulators.
One step of forInRevFrom.
forInRev2From is forInRevFrom on the pair of accumulators.
Running a loop straight into a continuation #
Runs forIn lo hi init body on a, then g on the last accumulator and the new array, which the
loop calls itself: unlike g on the result of run, it builds no pair.
Instances For
runForInThen for forInRev.
Instances For
runForInThen for forIn2, with the two accumulators handed to g apart.
Instances For
Loops that run straight into the rest of a program #
forIn lo hi init body >>= k, with the loop calling k itself on its last accumulator: the loop
returns no pair, so a scalar accumulator stays unboxed into k. It is specified by forInBind_eq.
Instances For
forInBind for forIn2, with the two last accumulators handed to k apart.
Instances For
forIn2Bind for forInRev2.
Instances For
Tail programs #
A tail program on arrays of size n, which returns a γ: an in-place program that ends by handing
the new array to a continuation, instead of returning it with a result. The continuation runs where
the program ends, inside its last loop if it ends with one, so the program's last values reach it
unboxed, and no result pair is built. A tail program is built from in-place programs with
Tail.bind, Tail.readThen, Tail.ite, Tail.dite, the loops Tail.forInBind,
Tail.forIn2Bind, Tail.forInRevBind and Tail.forInRev2Bind, and ends with Tail.done x k.
It runs with Tail.run or runInPlaceThen, and inplace a do … then k writes one.
- fn : ScalarArray.Int8Array.InPlace.State n → γ
Instances For
Runs t if c holds and e otherwise, as a branch inside one program, like InPlace.ite.
Instances For
forIn lo hi init body followed by the tail program k on its last accumulator, which the loop
calls itself. It is specified by forInBind_eq.
Instances For
Tail.forInBind for forIn2, with the two last accumulators handed to k apart.
Instances For
Tail.forInBind for forInRev2, with the two last accumulators handed to k apart.
Instances For
Loops in continuation-passing style #
Tail.forIn from j₀ instead of lo. It is specified by Tail.forInFrom_eq.
Instances For
A loop in continuation-passing style, for each j from lo up to hi - 1, followed by the rest
k of the program. The body gets j, both its bounds, the continuation next and the current
variables, and ends by calling next on the next variables, which runs the next step, or k after
the last one. So the variables are arguments of the loop, and a scalar stays unboxed, and a nested
loop, whose k calls the outer next, allocates nothing either. A body that doesn't call next
leaves the loop early. Tail.forIn2, Tail.forIn3 and Tail.forIn4 take two, three and four
variables, each its own argument.
Instances For
Tail.forInRev from j₀ - 1 down to lo instead of from hi - 1. It is specified by
Tail.forInRevFrom_eq.
Instances For
Tail.forIn2 from j₀ instead of lo. It is specified by Tail.forIn2From_eq.
Instances For
Tail.forIn with two variables, which the loop keeps as 2 arguments.
Instances For
Tail.forInRev2 from j₀ - 1 down to lo instead of from hi - 1. It is specified by
Tail.forInRev2From_eq.
Instances For
Tail.forIn2 from hi - 1 down to lo.
Instances For
Tail.forIn3 from j₀ instead of lo. It is specified by Tail.forIn3From_eq.
Instances For
Tail.forIn with three variables, which the loop keeps as 3 arguments.
Instances For
Tail.forInRev3 from j₀ - 1 down to lo instead of from hi - 1. It is specified by
Tail.forInRev3From_eq.
Instances For
Tail.forIn3 from hi - 1 down to lo.
Instances For
Tail.forIn4 from j₀ instead of lo. It is specified by Tail.forIn4From_eq.
Instances For
Tail.forIn with four variables, which the loop keeps as 4 arguments.
Instances For
Tail.forInRev4 from j₀ - 1 down to lo instead of from hi - 1. It is specified by
Tail.forInRev4From_eq.
Instances For
Tail.forIn4 from hi - 1 down to lo.
Instances For
Runs the tail program t on the array a, and returns what its continuation returns. The array
is made exclusive first, so it is copied once if it is shared, and updated in place otherwise.
Instances For
Loops in continuation-passing style #
One step of Tail.forInRevFrom.
One step of Tail.forIn2From.
One step of Tail.forInRev2From.
One step of Tail.forIn3From.
One step of Tail.forInRev3From.
One step of Tail.forIn4From.
One step of Tail.forInRev4From.
Two arrays #
For internal use only: the loop of the two-array updates zipModifyStride. For each j from j₀
up to hi - 1, it reads u := a[ea j] and v := b[eb j], then writes f u v at ea j in a
and g u v at eb j in b. Each array is made exclusive once, before the loop, and then updated
in place without checks. It is specified by zipForInFrom_eq.
Instances For
The steps of zipForInFrom, one at a time.
Runs the tail program t on a, and returns what its continuation returns: t.run a rfl. The
array is made exclusive first, so it is copied once if it is shared, and updated in place otherwise.
Instances For
Runs the tail program t on a, for a size n of a: t.run a h. When n is a variable bound
to a.size before the program, the program's types don't mention a, so that the compiler doesn't
keep a alive through the program: if it did, the array would no longer be exclusive when the
program makes it so, and it would be copied. inplace a do … runs its programs this way.
Instances For
The window of the slice at s.start.toUSize fits in its array.
Runs a tail program on the elements of the slice s, for a size n of s, in place in the
slice's array. t N off hw mk hmk is a program on the array, of size N, that works on the window
of n elements at off, where hw bounds the window; its continuation builds the new slice with
mk a' h, on the new array a', of size n by hmk. When the array's size is less than
USize.size, off is the slice's start. inplace s do … runs its programs on slices this way.