Documentation

ScalarArray.Notation

Notation for in-place programs #

The root module ScalarArray doesn't import this one, because it makes inplace a keyword. Outside of inplace … do blocks it adds nothing else: no other keyword, and no other term. The statements are parsed only in a block, the terms of the expansion aren't terms that can be written, and inplace_bound, the tactic that proves the bounds of indices, isn't a keyword.

inplace a do … writes an in-place program (XArray.InPlace) on the array a in a small imperative language, and runs it, which returns the program's result and the new array, or hands them to a continuation then k. It works for all 13 array types, and for their slices (see Slices):

inplace a do
  for j in 0...len with (c := 0) (b := 0) do
    let u := a[xs + j]
    let v := a[ys + j]
    let (s, c') := addc u v c
    let (d, b') := subb u v b
    a[xs + j] := s
    a[ts + j] := d
    c := c'
    b := b'
  return (c, b)

Statements #

A loop body can assign only its own variables: to update a variable across a loop, carry it, as with (c := c).

Results #

inplace a do … returns the pair of the program's result and the new array. With then k after the statements, it is k x a' h instead, for the result x, the new array a' and h : a'.size = a.size:

inplace a do
  for j in 0...len with (c := 0) (b := 0) do
    …
  return (c, b)
then fun (c, b) a' h => g c b a' h

The program calls k where it ends, inside its last loop if it ends with one, so the result goes into no pair, and scalars such as the carries above reach k unboxed. h lets k run another program on a' with the bounds proved for a.

Slices #

On a slice s of an array (an XArray.Slice), inplace s do … reads and writes the slice's elements, at indices counted from the start of the slice, with bounds i.toNat < s.size, and s.usize is its size as a USize. The program runs on the slice's array in place, with XArray.Slice.runInPlaceThenOf, and returns the new slice, on the new array; with then k, k gets the new slice and h : s'.size = s.size. let x ← m works on arrays only.

def double (s : UInt32Array.Slice) : UInt32Array.Slice :=
  (inplace s do
    for j in 0...s.usize do
      s[j] := 2 * s[j]).2

The program reads and writes the array at off + i, where off is the slice's start as a USize. When the array's size is less than USize.size, as it always is at runtime, that is the element i of the slice.

Proofs #

A program is defined by its expansion, which runInPlaceThenOf_eq and the run lemmas of XArray.InPlace.Tail take apart (ScalarArrayTest/Notation.lean has these examples):

How the notation expands may change in later versions, so proofs that take a program apart may need adjusting after an upgrade.

Compilation #

The program expands to a tail program (XArray.InPlace.Tail) on arrays of size n, run with a.runInPlaceThenOf n h, where n and a.usize are bound to variables before the program runs, and the program uses them in place of a.size and a.usize. So nothing in the compiled program refers to a, which the compiler would otherwise keep alive past the point where the program makes the array exclusive, and the array would be copied. The program ends with Tail.done x k, where k is the then continuation or makes the pair. Its statements expand to the tail programs readThen, writeThen, ite, dite and the loops Tail.forIn, Tail.forIn2, … and Tail.forInRev, …, in continuation-passing style: a loop body ends by calling next on the loop's variables, which runs the next step, and the loop ends by calling the rest of the program, so the variables are arguments of both and stay unboxed. continue calls next, and break calls the rest of the program, which a loop that breaks binds to a local function. A nested loop ends by calling the outer loop's next, so the two compile to mutually recursive loops with no allocation, and the program compiles to the same loops as a hand-written one. The rest of the program after an if becomes a local function of the mutable variables, which the compiler turns into a join point: an if in a loop allocates nothing.

Bounds #

The bound of each index is proved by the tactic inplace_bound, from the hypotheses in context:

It looks inside conjunctions. A loop body has both bounds of its index, lo.toNat ≤ j.toNat and j.toNat < hi.toNat, in context, and the program has a.usize.toNat ≤ a.size, so a.usize works as a loop bound with no other hypothesis. A bound that needs more is given by a have before the index, or by a hypothesis around the program, and a bound that inplace_bound can't prove is reported with its goal. For example, in this left shift of 64-bit limbs by 1111 bits:

if h : 18 ≤ a.size then
  (inplace a do
    for j in reverse 18...a.usize do
      a[j] := (a[j - 17] <<< 23) ||| (a[j - 18] >>> 41)
    a[17] := a[0] <<< 23
    for j in 0...17 do
      a[j] := 0).2
else UInt64Array.mk (Array.replicate a.size 0)

Limits #

theorem ScalarArray.Notation.toNat_add_lt_of_window {n : Nat} {off len j : USize} (hw : off.toNat + len.toNat ≤ n) (hj : j.toNat < len.toNat) :
(off + j).toNat < n

The index off + j of a window of len elements at off is in bounds, for j < len. For internal use by the inplace notation.

theorem ScalarArray.Notation.toNat_add_lt_of_window_of_lt {N n : Nat} {off i : USize} (hw : off.toNat + n ≤ N) (hi : i.toNat < n) :
(off + i).toNat < N

The index off + i of a window of n elements at off is in bounds, for i < n: the bound of the indices of a program on a slice, in the slice's array. For internal use by the inplace notation.

@[inline]

The number of steps of the range lo...hi by s, for s > 0: of the indices lo + s * k below hi. For internal use by the inplace notation.

Instances For
    @[inline]
    def ScalarArray.Notation.withExit {α : Sort u} {β : Sort v} (f : α → β) (k : α) :
    β

    f k. The notation inplace writes a loop that may break as withExit (fun brk => loop) k, with the rest k of the program after the loop, which break calls: elaborated as the arguments of withExit, the loop comes first and fixes the type of k. For internal use by the inplace notation.

    Instances For
      theorem ScalarArray.Notation.step_le {lo hi s k : USize} (hk : k.toNat < (stepCount lo hi s).toNat) :
      lo.toNat ≤ (lo + s * k).toNat

      The index lo + s * k of the step k of lo...hi by s is at least lo. For internal use by the inplace notation.

      theorem ScalarArray.Notation.step_lt {lo hi s k : USize} (hk : k.toNat < (stepCount lo hi s).toNat) :
      (lo + s * k).toNat < hi.toNat

      The index lo + s * k of the step k of lo...hi by s is below hi. For internal use by the inplace notation.

      theorem ScalarArray.Notation.le_of_lt_succ {hi j : USize} (_h : hi.toNat + 1 < USize.size) (hj : j.toNat < (hi + 1).toNat) :

      The index of a loop over lo...hi + 1, which is lo...=hi, is at most hi. The proof doesn't need hi + 1 not to wrap around, but the loop does, so the notation passes that fact here too. For internal use by the inplace notation.

      theorem ScalarArray.Notation.lt_of_succ_le {lo j : USize} (h : lo.toNat + 1 < USize.size) (hj : (lo + 1).toNat ≤ j.toNat) :

      The index of a loop over lo + 1...hi, which is lo<...hi, is above lo. For internal use by the inplace notation.

      Proves the bound i.toNat < n of an index in an inplace program: for i = off + j, from a window hypothesis off.toNat + len.toNat ≤ n and the loop bound j.toNat < len.toNat, or else by lia from the hypotheses in context, or else by grind, for USize arithmetic that may wrap around. Conjunctions in context are split first.

      Instances For

        Elaborates inplace_bound: runs its proof search, and reports a failure with the bound.

        Instances For

          A statement of an inplace program.

          Instances For

            A sequence of statements of an inplace program.

            Instances For

              a[i] := e writes e at i.

              Instances For

                let x ← m runs the in-place program m (an XArray.InPlace n α, for the program's size n) and binds its result to x.

                Instances For

                  let mut x := e declares the mutable variable x, and let mut x : t := e gives its type.

                  Instances For

                    let x : t := e binds the value of the pure term e of type t to x.

                    Instances For

                      let p := e binds the value of the pure term e to the pattern p.

                      Instances For

                        have h : t := p adds the fact h to the context, for the bounds of later indices; as in terms, h defaults to this, and t to the type of p.

                        Instances For

                          x := e assigns the mutable variable x.

                          Instances For

                            (x, y) := e assigns the mutable variables of the pattern (x, y).

                            Instances For

                              if c then … else … runs one of two blocks.

                              Instances For

                                if let p := e then … else … runs the first block if e matches the pattern p, with its variables, and the second one otherwise.

                                Instances For

                                  unless c do … runs the block if c doesn't hold.

                                  Instances For

                                    The variables of a loop, with (x := init) (y : t := init) ….

                                    Instances For

                                      A loop over j in a range such as lo...hi or reverse lo...hi, by steps of s with by s, with the variables of its with clause; for h : j in … names the bounds of j.

                                      Instances For

                                        A loop that runs while c holds, with the variables of its with clause.

                                        Instances For

                                          break leaves the innermost loop, with the current values of its variables.

                                          Instances For

                                            continue goes on with the next step of the innermost loop.

                                            Instances For

                                              An alternative | p => … of a match statement.

                                              Instances For

                                                match e with | p => … | q => … runs the block of the first pattern that e matches.

                                                Instances For

                                                  return e ends the program with the result e, and return with ().

                                                  Instances For

                                                    x += e isn't a statement of an inplace program: this reports it, with what to write.

                                                    Instances For

                                                      x -= e isn't a statement of an inplace program: this reports it, with what to write.

                                                      Instances For

                                                        x *= e isn't a statement of an inplace program: this reports it, with what to write.

                                                        Instances For

                                                          An application f x …, such as pure x, isn't a statement of an inplace program: this reports it, with the statements that are, rather than a parse error.

                                                          Instances For

                                                            inplace a do … is the in-place program on a that the statements describe, run on a: it returns the program's result and the new array, or, with then k, k on them and the new array's size. See the module documentation of ScalarArray.Notation.

                                                            Instances For

                                                              The internal terms of the expansion of an inplace program. They aren't terms of the term category, so they can't be written outside of the expansion, and their names aren't keywords.

                                                              Instances For

                                                                inplace_index i is the USize index i of a read or a write of an inplace program, with an error that says so for an index of another type, such as a Nat.

                                                                Instances For

                                                                  inplace_elem a v is the value v of a write to the array or slice a of an inplace program, elaborated as an element of a, so that a type error names v.

                                                                  Instances For

                                                                    inplace_choose a e f is e if a is an array and f if it is a slice: inplace a do … expands to both programs, and elaborates the one for the type of a.

                                                                    Instances For

                                                                      Elaborates inplace_choose.

                                                                      Instances For

                                                                        Elaborates inplace_index.

                                                                        Instances For