Documentation

ScalarArray.Stride

Strides and the models of loops #

The parts of the specification of loops that name no element type, shared by all the arrays:

A loop computes the USize copies of the offset and the stride once, before it starts: toNat_idx_toUSize gives the Nat value of s.off.toUSize + s.inc.toUSize * j for a step j of a stride that fits in an array in memory.

Iteration over a range #

@[irreducible]
def ScalarArray.iterRange {α : Sort u} (f : Nat → α → α) (lo hi : Nat) (x : α) :
α

iterRange f lo hi x applies f lo, f (lo + 1), …, f (hi - 1) to x, in this order: the model of a loop from lo up to hi - 1 whose step j maps its state by f j. It is x when hi ≤ lo.

Instances For
    theorem ScalarArray.iterRange_of_lt {α : Sort u} (f : Nat → α → α) {lo hi : Nat} (h : lo < hi) (x : α) :
    iterRange f lo hi x = iterRange f (lo + 1) hi (f lo x)
    theorem ScalarArray.iterRange_of_le {α : Sort u} (f : Nat → α → α) {lo hi : Nat} (h : hi ≤ lo) (x : α) :
    iterRange f lo hi x = x
    @[simp]
    theorem ScalarArray.iterRange_self {α : Sort u} (f : Nat → α → α) (lo : Nat) (x : α) :
    iterRange f lo lo x = x
    theorem ScalarArray.iterRange_succ {α : Sort u} (f : Nat → α → α) {lo hi : Nat} (h : lo ≤ hi) (x : α) :
    iterRange f lo (hi + 1) x = f hi (iterRange f lo hi x)

    The last step of iterRange comes last.

    theorem ScalarArray.iterRange_split {α : Sort u} (f : Nat → α → α) {lo mid hi : Nat} (h₁ : lo ≤ mid) (h₂ : mid ≤ hi) (x : α) :
    iterRange f lo hi x = iterRange f mid hi (iterRange f lo mid x)

    iterRange splits at any mid between its bounds.

    theorem ScalarArray.iterRange_congr {α : Sort u} {f g : Nat → α → α} {lo hi : Nat} (h : ∀ (i : Nat), lo ≤ i → i < hi → f i = g i) (x : α) :
    iterRange f lo hi x = iterRange g lo hi x

    iterRange depends only on the steps in its range.

    theorem ScalarArray.iterRange_invariant {α : Sort u} (p : α → Prop) {f : Nat → α → α} {lo hi : Nat} (hf : ∀ (i : Nat), lo ≤ i → i < hi → ∀ (x : α), p x → p (f i x)) {x : α} (hx : p x) :
    p (iterRange f lo hi x)

    A property that each step in the range preserves holds after iterRange.

    theorem ScalarArray.apply_iterRange {α : Sort u} {β : Sort v} (φ : α → β) {f : Nat → α → α} {g : Nat → β → β} {lo hi : Nat} (h : ∀ (i : Nat), lo ≤ i → i < hi → ∀ (x : α), φ (f i x) = g i (φ x)) (x : α) :
    φ (iterRange f lo hi x) = iterRange g lo hi (φ x)

    A map φ that takes each step f i to g i takes iterRange f to iterRange g.

    @[irreducible]
    def ScalarArray.iterRangeRev {α : Sort u} (f : Nat → α → α) (lo hi : Nat) (x : α) :
    α

    iterRangeRev f lo hi x applies f (hi - 1), f (hi - 2), …, f lo to x, in this order: the model of a loop from hi - 1 down to lo whose step j maps its state by f j. It is x when hi ≤ lo.

    Instances For
      theorem ScalarArray.iterRangeRev_of_lt {α : Sort u} (f : Nat → α → α) {lo hi : Nat} (h : lo < hi) (x : α) :
      iterRangeRev f lo hi x = iterRangeRev f lo (hi - 1) (f (hi - 1) x)
      theorem ScalarArray.iterRangeRev_of_le {α : Sort u} (f : Nat → α → α) {lo hi : Nat} (h : hi ≤ lo) (x : α) :
      iterRangeRev f lo hi x = x
      @[simp]
      theorem ScalarArray.iterRangeRev_self {α : Sort u} (f : Nat → α → α) (lo : Nat) (x : α) :
      iterRangeRev f lo lo x = x
      theorem ScalarArray.iterRangeRev_of_lt_left {α : Sort u} (f : Nat → α → α) {lo hi : Nat} (h : lo < hi) (x : α) :
      iterRangeRev f lo hi x = f lo (iterRangeRev f (lo + 1) hi x)

      The step lo of iterRangeRev comes last.

      theorem ScalarArray.iterRangeRev_split {α : Sort u} (f : Nat → α → α) {lo mid hi : Nat} (h₁ : lo ≤ mid) (h₂ : mid ≤ hi) (x : α) :
      iterRangeRev f lo hi x = iterRangeRev f lo mid (iterRangeRev f mid hi x)

      iterRangeRev splits at any mid between its bounds.

      theorem ScalarArray.iterRangeRev_congr {α : Sort u} {f g : Nat → α → α} {lo hi : Nat} (h : ∀ (i : Nat), lo ≤ i → i < hi → f i = g i) (x : α) :
      iterRangeRev f lo hi x = iterRangeRev g lo hi x

      iterRangeRev depends only on the steps in its range.

      theorem ScalarArray.iterRangeRev_invariant {α : Sort u} (p : α → Prop) {f : Nat → α → α} {lo hi : Nat} (hf : ∀ (i : Nat), lo ≤ i → i < hi → ∀ (x : α), p x → p (f i x)) {x : α} (hx : p x) :
      p (iterRangeRev f lo hi x)

      A property that each step in the range preserves holds after iterRangeRev.

      theorem ScalarArray.apply_iterRangeRev {α : Sort u} {β : Sort v} (φ : α → β) {f : Nat → α → α} {g : Nat → β → β} {lo hi : Nat} (h : ∀ (i : Nat), lo ≤ i → i < hi → ∀ (x : α), φ (f i x) = g i (φ x)) (x : α) :
      φ (iterRangeRev f lo hi x) = iterRangeRev g lo hi (φ x)

      A map φ that takes each step f i to g i takes iterRangeRev f to iterRangeRev g.

      theorem ScalarArray.iterRange_prod_of_snd {α : Type u} {β : Type v} {f : Nat → α × β → α × β} {g : Nat → α → α} {lo hi : Nat} {a : β} (h : ∀ (i : Nat), lo ≤ i → i < hi → ∀ (b : α), f i (b, a) = (g i b, a)) (b : α) :
      iterRange f lo hi (b, a) = (iterRange g lo hi b, a)

      A loop whose steps i map its variable by g i and leave the array a alone, such as a loop that only reads, maps (b, a) to (iterRange g lo hi b, a).

      theorem ScalarArray.iterRangeRev_prod_of_snd {α : Type u} {β : Type v} {f : Nat → α × β → α × β} {g : Nat → α → α} {lo hi : Nat} {a : β} (h : ∀ (i : Nat), lo ≤ i → i < hi → ∀ (b : α), f i (b, a) = (g i b, a)) (b : α) :
      iterRangeRev f lo hi (b, a) = (iterRangeRev g lo hi b, a)

      iterRange_prod_of_snd for iterRangeRev.

      Strides #

      The indices off + inc * i of a strided vector, such as the incx of a BLAS routine, the diagonal of a matrix or a column of a matrix stored by columns. It is plain data: Stride.Fits s n size says that its first n indices are distinct and less than size.

      • off : Nat

        The index of the step 0.

      • inc : Nat

        The distance between the indices of two consecutive steps.

      Instances For
        def ScalarArray.instDecidableEqStride.decEq (x✝ x✝¹ : Stride) :
        Decidable (x✝ = x✝¹)
        Instances For
          @[instance_reducible]

          The index off + inc * i of the step i.

          Instances For
            @[simp]
            theorem ScalarArray.Stride.idx_succ (s : Stride) (i : Nat) :
            s.idx (i + 1) = s.idx i + s.inc

            The stride from the step k of s on, with the same increment.

            Instances For
              @[simp]
              theorem ScalarArray.Stride.off_shift (s : Stride) (k : Nat) :
              (s.shift k).off = s.idx k
              @[simp]
              theorem ScalarArray.Stride.inc_shift (s : Stride) (k : Nat) :
              (s.shift k).inc = s.inc
              @[simp]
              theorem ScalarArray.Stride.idx_shift (s : Stride) (k i : Nat) :
              (s.shift k).idx i = s.idx (k + i)
              structure ScalarArray.Stride.Fits (s : Stride) (n size : Nat) :

              The first n steps of s are at distinct indices, less than size: the stride is positive, unless there is at most one step, and the index of the last step is less than size.

              • inc_pos : 0 < s.inc ∨ n ≤ 1

                The stride is positive, unless there is at most one step.

              • idx_last_lt : n = 0 ∨ s.idx (n - 1) < size

                The index of the last step is less than size.

              Instances For
                @[instance_reducible]
                instance ScalarArray.Stride.instDecidableFits (s : Stride) (n size : Nat) :
                Decidable (s.Fits n size)
                theorem ScalarArray.Stride.Fits.idx_lt {s : Stride} {n size : Nat} (h : s.Fits n size) {i : Nat} (hi : i < n) :
                s.idx i < size

                The index of each of the first n steps is less than size.

                theorem ScalarArray.Stride.Fits.idx_inj {s : Stride} {n size : Nat} (h : s.Fits n size) {i j : Nat} (hi : i < n) (hj : j < n) (e : s.idx i = s.idx j) :
                i = j

                The first n steps are at distinct indices.

                theorem ScalarArray.Stride.Fits.le_size {s : Stride} {n size : Nat} (h : s.Fits n size) :
                n ≤ size

                n steps that fit in size indices are at most size steps.

                theorem ScalarArray.Stride.Fits.mono {s : Stride} {n size : Nat} (h : s.Fits n size) {m : Nat} (hm : m ≤ n) :
                s.Fits m size

                Fewer steps fit too.

                theorem ScalarArray.Stride.Fits.shift {s : Stride} {n size : Nat} (h : s.Fits n size) (k : Nat) :
                (s.shift k).Fits (n - k) size

                The steps of s from k on fit, as the steps of s.shift k.

                theorem ScalarArray.Stride.toNat_idx_toUSize {s : Stride} {n size : Nat} (h : s.Fits n size) (hs : size < USize.size) {i : USize} (hi : i.toNat < n) :

                The USize index s.off.toUSize + s.inc.toUSize * i of a step i < n of a stride that fits in an array of USize size, as every array in memory has, doesn't wrap around.

                theorem ScalarArray.Stride.toNat_idx_toUSize_lt {s : Stride} {n size : Nat} (h : s.Fits n size) (hs : size < USize.size) {i : USize} (hi : i.toNat < n) :
                (s.off.toUSize + s.inc.toUSize * i).toNat < size

                The USize index of a step i < n is less than size.

                Disjoint strides #

                theorem ScalarArray.Stride.idx_ne_idx_of_same_inc {s t : Stride} (hinc : s.inc = t.inc) (h₁ : s.off < t.off) (h₂ : t.off < s.off + s.inc) (i j : Nat) :
                s.idx i ≠ t.idx j

                Two strides with the same increment, whose offsets differ by less than it, never meet: for example two rows of a matrix stored by columns, whose increment is the leading dimension.

                theorem ScalarArray.Stride.idx_ne_idx_of_lt_off {s t : Stride} {n : Nat} (h : s.idx (n - 1) < t.off) {i : Nat} (hi : i < n) (j : Nat) :
                s.idx i ≠ t.idx j

                The first n steps of s never meet t when they all come before the offset of t.