Documentation

ScalarArray.UInt8.Stride

Updates at index maps, and whole loops, on UInt8Array #

a.modifyAt idx lo hi g is a with g i a[idx i] written at the index idx i, for each i from lo up to hi - 1, in this order. It is the model of a loop that updates the elements at a map of indices: a stride a.modifyAt s.idx lo hi g, for a ScalarArray.Stride s, the diagonal of a matrix, a packed triangle or a permutation. When idx is injective on the range, which Stride.Fits.idx_inj gives for a stride, the element at idx i is g i a[idx i] (getElem_modifyAt_idx). The elements at no index of the range are unchanged (getElem_modifyAt_of_forall_ne). copySlice_eq_modifyAt gives a copy within bounds as a modifyAt at the indices destOff + i, so that a kernel can copy contiguous data with copySlice and strided data with a loop, and specify both the same way.

The whole-loop lemmas of InPlace.Tail run a loop in continuation-passing style from any step j₀, where its one-step equation Tail.forInFrom_eq runs one step:

Two updates at a time have models too. a.modifyAt2 ia ib lo hi f g reads the elements at ia i and ib i and writes f i u v and g i u v back, for two index maps of one array, such as two rows of a matrix, and zipModifyAt a b ia ib lo hi f g does the same on two arrays. When the maps are injective on the range, and for modifyAt2 never meet, modifyAt2_eq and zipModifyAt_eq give them as modifyAts, whose lemmas then apply. Tail.run_forInFrom_modify2 runs the loop for j in lo...hi do let u := a[ea j]; let v := a[eb j]; a[ea j] := …; a[eb j] := … as modifyAt2.

The strided loops modifyStride, modifyStride2 and zipModifyStride run these models on strides as single loops, which make each array exclusive once and then store without checks. They are specified by their models with @[csimp].

Updates at index maps #

def ScalarArray.UInt8Array.modifyAt (a : UInt8Array) (idx : Nat → Nat) (lo hi : Nat) (g : Nat → UInt8 → UInt8) :

The array a with g i a[idx i] written at the index idx i, for each i from lo up to hi - 1, in this order, where g i sees the earlier writes. Indices out of bounds are skipped. It is the model of a loop that updates the elements at the indices idx i, such as the steps s.idx i of a stride s.

Instances For
    @[simp]
    theorem ScalarArray.UInt8Array.size_modifyAt (a : UInt8Array) (idx : Nat → Nat) (lo hi : Nat) (g : Nat → UInt8 → UInt8) :
    (a.modifyAt idx lo hi g).size = a.size
    theorem ScalarArray.UInt8Array.modifyAt_of_le (a : UInt8Array) (idx : Nat → Nat) {lo hi : Nat} (h : hi ≤ lo) (g : Nat → UInt8 → UInt8) :
    a.modifyAt idx lo hi g = a
    @[simp]
    theorem ScalarArray.UInt8Array.modifyAt_self (a : UInt8Array) (idx : Nat → Nat) (lo : Nat) (g : Nat → UInt8 → UInt8) :
    a.modifyAt idx lo lo g = a
    theorem ScalarArray.UInt8Array.modifyAt_of_lt (a : UInt8Array) (idx : Nat → Nat) {lo hi : Nat} (h : lo < hi) (g : Nat → UInt8 → UInt8) :
    a.modifyAt idx lo hi g = (a.set! (idx lo) (g lo a[idx lo]!)).modifyAt idx (lo + 1) hi g

    The step lo of modifyAt comes first.

    theorem ScalarArray.UInt8Array.modifyAt_succ (a : UInt8Array) (idx : Nat → Nat) {lo hi : Nat} (h : lo ≤ hi) (g : Nat → UInt8 → UInt8) :
    a.modifyAt idx lo (hi + 1) g = (a.modifyAt idx lo hi g).set! (idx hi) (g hi (a.modifyAt idx lo hi g)[idx hi]!)

    The step hi of modifyAt comes last.

    theorem ScalarArray.UInt8Array.modifyAt_split (a : UInt8Array) (idx : Nat → Nat) {lo mid hi : Nat} (h₁ : lo ≤ mid) (h₂ : mid ≤ hi) (g : Nat → UInt8 → UInt8) :
    a.modifyAt idx lo hi g = (a.modifyAt idx lo mid g).modifyAt idx mid hi g

    modifyAt splits at any mid between its bounds.

    theorem ScalarArray.UInt8Array.modifyAt_congr (a : UInt8Array) {idx idx' : Nat → Nat} {lo hi : Nat} {g g' : Nat → UInt8 → UInt8} (hidx : ∀ (i : Nat), lo ≤ i → i < hi → idx i = idx' i) (hg : ∀ (i : Nat), lo ≤ i → i < hi → g i = g' i) :
    a.modifyAt idx lo hi g = a.modifyAt idx' lo hi g'

    modifyAt depends only on the indices and the updates in its range.

    theorem ScalarArray.UInt8Array.getElem_modifyAt_of_forall_ne (a : UInt8Array) (idx : Nat → Nat) {lo hi : Nat} (g : Nat → UInt8 → UInt8) {p : Nat} (hp : ∀ (i : Nat), lo ≤ i → i < hi → idx i ≠ p) (h : p < (a.modifyAt idx lo hi g).size) :
    (a.modifyAt idx lo hi g)[p] = a[p]

    An element at no index idx i of the range is unchanged.

    theorem ScalarArray.UInt8Array.getElem_modifyAt_idx (a : UInt8Array) (idx : Nat → Nat) {lo hi : Nat} (g : Nat → UInt8 → UInt8) (hinj : ∀ (i : Nat), lo ≤ i → i < hi → ∀ (j : Nat), lo ≤ j → j < hi → idx i = idx j → i = j) {i : Nat} (hi₁ : lo ≤ i) (hi₂ : i < hi) (h : idx i < (a.modifyAt idx lo hi g).size) :
    (a.modifyAt idx lo hi g)[idx i] = g i a[idx i]

    The element at the index idx i of a step i of the range is g i of the element of a, when idx is injective on the range.

    theorem ScalarArray.UInt8Array.modifyAt_succ_of_injOn (a : UInt8Array) (idx : Nat → Nat) {lo hi : Nat} (h : lo ≤ hi) (g : Nat → UInt8 → UInt8) (hinj : ∀ (i : Nat), lo ≤ i → i < hi + 1 → ∀ (j : Nat), lo ≤ j → j < hi + 1 → idx i = idx j → i = j) :
    a.modifyAt idx lo (hi + 1) g = (a.set! (idx hi) (g hi a[idx hi]!)).modifyAt idx lo hi g

    The step hi of modifyAt can come first instead of last, when idx is injective on the range, since then no other step reads or writes its element.

    Strides #

    theorem ScalarArray.UInt8Array.getElem_modifyAt_stride (a : UInt8Array) {s : Stride} {n : Nat} (hs : s.Fits n a.size) {lo hi : Nat} (hn : hi ≤ n) (g : Nat → UInt8 → UInt8) {i : Nat} (hi₁ : lo ≤ i) (hi₂ : i < hi) :
    (a.modifyAt s.idx lo hi g)[s.idx i] = g i a[s.idx i]

    The element at the step i of a stride that fits in the array.

    Copies #

    theorem ScalarArray.UInt8Array.copySlice_eq_modifyAt (src : UInt8Array) (srcOff : Nat) (dest : UInt8Array) (destOff len : Nat) (exact : Bool) (h₁ : srcOff + len ≤ src.size) (h₂ : destOff + len ≤ dest.size) :
    src.copySlice srcOff dest destOff len exact = dest.modifyAt (fun (x : Nat) => destOff + x) 0 len fun (i : Nat) (x : UInt8) => src[srcOff + i]!

    copySlice within bounds writes the elements of src at the indices destOff + i.

    Two updates at a time #

    Two index maps on one array #

    def ScalarArray.UInt8Array.modifyAt2 (a : UInt8Array) (ia ib : Nat → Nat) (lo hi : Nat) (f g : Nat → UInt8 → UInt8 → UInt8) :

    The array a where, for each i from lo up to hi - 1, in this order, u := a[ia i]! and v := a[ib i]! are read, and then f i u v is written at the index ia i and g i u v at the index ib i, where each step sees the earlier writes. Indices out of bounds read default and are not written. It is the model of a loop that updates two strides of one array together, such as a plane rotation of two rows of a matrix. When the two index maps are injective on the range and never meet, modifyAt2_eq gives it as two modifyAts.

    Instances For
      @[simp]
      theorem ScalarArray.UInt8Array.size_modifyAt2 (a : UInt8Array) (ia ib : Nat → Nat) (lo hi : Nat) (f g : Nat → UInt8 → UInt8 → UInt8) :
      (a.modifyAt2 ia ib lo hi f g).size = a.size
      theorem ScalarArray.UInt8Array.modifyAt2_of_le (a : UInt8Array) (ia ib : Nat → Nat) {lo hi : Nat} (h : hi ≤ lo) (f g : Nat → UInt8 → UInt8 → UInt8) :
      a.modifyAt2 ia ib lo hi f g = a
      @[simp]
      theorem ScalarArray.UInt8Array.modifyAt2_self (a : UInt8Array) (ia ib : Nat → Nat) (lo : Nat) (f g : Nat → UInt8 → UInt8 → UInt8) :
      a.modifyAt2 ia ib lo lo f g = a
      theorem ScalarArray.UInt8Array.modifyAt2_of_lt (a : UInt8Array) (ia ib : Nat → Nat) {lo hi : Nat} (h : lo < hi) (f g : Nat → UInt8 → UInt8 → UInt8) :
      a.modifyAt2 ia ib lo hi f g = ((a.set! (ia lo) (f lo a[ia lo]! a[ib lo]!)).set! (ib lo) (g lo a[ia lo]! a[ib lo]!)).modifyAt2 ia ib (lo + 1) hi f g

      The step lo of modifyAt2 comes first.

      theorem ScalarArray.UInt8Array.modifyAt2_split (a : UInt8Array) (ia ib : Nat → Nat) {lo mid hi : Nat} (h₁ : lo ≤ mid) (h₂ : mid ≤ hi) (f g : Nat → UInt8 → UInt8 → UInt8) :
      a.modifyAt2 ia ib lo hi f g = (a.modifyAt2 ia ib lo mid f g).modifyAt2 ia ib mid hi f g

      modifyAt2 splits at any mid between its bounds.

      theorem ScalarArray.UInt8Array.modifyAt2_congr (a : UInt8Array) {ia ia' ib ib' : Nat → Nat} {lo hi : Nat} {f f' g g' : Nat → UInt8 → UInt8 → UInt8} (hia : ∀ (i : Nat), lo ≤ i → i < hi → ia i = ia' i) (hib : ∀ (i : Nat), lo ≤ i → i < hi → ib i = ib' i) (hf : ∀ (i : Nat), lo ≤ i → i < hi → f i = f' i) (hg : ∀ (i : Nat), lo ≤ i → i < hi → g i = g' i) :
      a.modifyAt2 ia ib lo hi f g = a.modifyAt2 ia' ib' lo hi f' g'

      modifyAt2 depends only on the indices and the updates in its range.

      theorem ScalarArray.UInt8Array.modifyAt2_eq (a : UInt8Array) {ia ib : Nat → Nat} {lo hi : Nat} (f g : Nat → UInt8 → UInt8 → UInt8) (hia : ∀ (i : Nat), lo ≤ i → i < hi → ∀ (j : Nat), lo ≤ j → j < hi → ia i = ia j → i = j) (hib : ∀ (i : Nat), lo ≤ i → i < hi → ∀ (j : Nat), lo ≤ j → j < hi → ib i = ib j → i = j) (hd : ∀ (i : Nat), lo ≤ i → i < hi → ∀ (j : Nat), lo ≤ j → j < hi → ia i ≠ ib j) :
      a.modifyAt2 ia ib lo hi f g = (a.modifyAt ia lo hi fun (i : Nat) (u : UInt8) => f i u a[ib i]!).modifyAt ib lo hi fun (i : Nat) (v : UInt8) => g i a[ia i]! v

      When ia and ib are injective on the range and never meet, each step of modifyAt2 reads only elements that no other step writes, so it is the modifyAt at ia followed by the modifyAt at ib, both reading the other elements from a.

      Two arrays #

      def ScalarArray.UInt8Array.zipModifyAt (a b : UInt8Array) (ia ib : Nat → Nat) (lo hi : Nat) (f g : Nat → UInt8 → UInt8 → UInt8) :

      The arrays a and b where, for each i from lo up to hi - 1, in this order, u := a[ia i]! and v := b[ib i]! are read, and then f i u v is written at the index ia i of a and g i u v at the index ib i of b, where each step sees the earlier writes. Indices out of bounds read default and are not written. It is the model of a loop that updates two arrays together, such as a plane rotation of two vectors. When the two index maps are injective on the range, zipModifyAt_eq gives it as a modifyAt of each array.

      Instances For
        @[simp]
        theorem ScalarArray.UInt8Array.size_zipModifyAt_fst (a b : UInt8Array) (ia ib : Nat → Nat) (lo hi : Nat) (f g : Nat → UInt8 → UInt8 → UInt8) :
        (a.zipModifyAt b ia ib lo hi f g).fst.size = a.size
        @[simp]
        theorem ScalarArray.UInt8Array.size_zipModifyAt_snd (a b : UInt8Array) (ia ib : Nat → Nat) (lo hi : Nat) (f g : Nat → UInt8 → UInt8 → UInt8) :
        (a.zipModifyAt b ia ib lo hi f g).snd.size = b.size
        theorem ScalarArray.UInt8Array.zipModifyAt_of_le (a b : UInt8Array) (ia ib : Nat → Nat) {lo hi : Nat} (h : hi ≤ lo) (f g : Nat → UInt8 → UInt8 → UInt8) :
        a.zipModifyAt b ia ib lo hi f g = (a, b)
        @[simp]
        theorem ScalarArray.UInt8Array.zipModifyAt_self (a b : UInt8Array) (ia ib : Nat → Nat) (lo : Nat) (f g : Nat → UInt8 → UInt8 → UInt8) :
        a.zipModifyAt b ia ib lo lo f g = (a, b)
        theorem ScalarArray.UInt8Array.zipModifyAt_of_lt (a b : UInt8Array) (ia ib : Nat → Nat) {lo hi : Nat} (h : lo < hi) (f g : Nat → UInt8 → UInt8 → UInt8) :
        a.zipModifyAt b ia ib lo hi f g = (a.set! (ia lo) (f lo a[ia lo]! b[ib lo]!)).zipModifyAt (b.set! (ib lo) (g lo a[ia lo]! b[ib lo]!)) ia ib (lo + 1) hi f g

        The step lo of zipModifyAt comes first.

        theorem ScalarArray.UInt8Array.zipModifyAt_split (a b : UInt8Array) (ia ib : Nat → Nat) {lo mid hi : Nat} (h₁ : lo ≤ mid) (h₂ : mid ≤ hi) (f g : Nat → UInt8 → UInt8 → UInt8) :
        a.zipModifyAt b ia ib lo hi f g = (a.zipModifyAt b ia ib lo mid f g).fst.zipModifyAt (a.zipModifyAt b ia ib lo mid f g).snd ia ib mid hi f g

        zipModifyAt splits at any mid between its bounds.

        theorem ScalarArray.UInt8Array.zipModifyAt_congr (a b : UInt8Array) {ia ia' ib ib' : Nat → Nat} {lo hi : Nat} {f f' g g' : Nat → UInt8 → UInt8 → UInt8} (hia : ∀ (i : Nat), lo ≤ i → i < hi → ia i = ia' i) (hib : ∀ (i : Nat), lo ≤ i → i < hi → ib i = ib' i) (hf : ∀ (i : Nat), lo ≤ i → i < hi → f i = f' i) (hg : ∀ (i : Nat), lo ≤ i → i < hi → g i = g' i) :
        a.zipModifyAt b ia ib lo hi f g = a.zipModifyAt b ia' ib' lo hi f' g'

        zipModifyAt depends only on the indices and the updates in its range.

        theorem ScalarArray.UInt8Array.zipModifyAt_eq (a b : UInt8Array) {ia ib : Nat → Nat} {lo hi : Nat} (f g : Nat → UInt8 → UInt8 → UInt8) (hia : ∀ (i : Nat), lo ≤ i → i < hi → ∀ (j : Nat), lo ≤ j → j < hi → ia i = ia j → i = j) (hib : ∀ (i : Nat), lo ≤ i → i < hi → ∀ (j : Nat), lo ≤ j → j < hi → ib i = ib j → i = j) :
        a.zipModifyAt b ia ib lo hi f g = (a.modifyAt ia lo hi fun (i : Nat) (u : UInt8) => f i u b[ib i]!, b.modifyAt ib lo hi fun (i : Nat) (v : UInt8) => g i a[ia i]! v)

        When ia and ib are injective on the range, each step of zipModifyAt reads only elements that no other step writes, so it is a modifyAt of each array, reading the elements of the other array from the arrays it started with.

        Strides #

        theorem ScalarArray.UInt8Array.getElem_modifyAt2_stride_left (a : UInt8Array) {sa sb : Stride} {n : Nat} (ha : sa.Fits n a.size) (hb : sb.Fits n a.size) (hd : ∀ (i : Nat), i < n → ∀ (j : Nat), j < n → sa.idx i ≠ sb.idx j) {lo hi : Nat} (hn : hi ≤ n) (f g : Nat → UInt8 → UInt8 → UInt8) {i : Nat} (hi₁ : lo ≤ i) (hi₂ : i < hi) :
        (a.modifyAt2 sa.idx sb.idx lo hi f g)[sa.idx i] = f i a[sa.idx i] a[sb.idx i]

        The element at the step i of the first stride of modifyAt2, for disjoint strides.

        theorem ScalarArray.UInt8Array.getElem_modifyAt2_stride_right (a : UInt8Array) {sa sb : Stride} {n : Nat} (ha : sa.Fits n a.size) (hb : sb.Fits n a.size) (hd : ∀ (i : Nat), i < n → ∀ (j : Nat), j < n → sa.idx i ≠ sb.idx j) {lo hi : Nat} (hn : hi ≤ n) (f g : Nat → UInt8 → UInt8 → UInt8) {i : Nat} (hi₁ : lo ≤ i) (hi₂ : i < hi) :
        (a.modifyAt2 sa.idx sb.idx lo hi f g)[sb.idx i] = g i a[sa.idx i] a[sb.idx i]

        The element at the step i of the second stride of modifyAt2, for disjoint strides.

        theorem ScalarArray.UInt8Array.getElem_zipModifyAt_stride_fst (a b : UInt8Array) {sa sb : Stride} {n : Nat} (ha : sa.Fits n a.size) (hb : sb.Fits n b.size) {lo hi : Nat} (hn : hi ≤ n) (f g : Nat → UInt8 → UInt8 → UInt8) {i : Nat} (hi₁ : lo ≤ i) (hi₂ : i < hi) :
        (a.zipModifyAt b sa.idx sb.idx lo hi f g).fst[sa.idx i] = f i a[sa.idx i] b[sb.idx i]

        The element at the step i of the stride of the first array of zipModifyAt.

        theorem ScalarArray.UInt8Array.getElem_zipModifyAt_stride_snd (a b : UInt8Array) {sa sb : Stride} {n : Nat} (ha : sa.Fits n a.size) (hb : sb.Fits n b.size) {lo hi : Nat} (hn : hi ≤ n) (f g : Nat → UInt8 → UInt8 → UInt8) {i : Nat} (hi₁ : lo ≤ i) (hi₂ : i < hi) :
        (a.zipModifyAt b sa.idx sb.idx lo hi f g).snd[sb.idx i] = g i a[sa.idx i] b[sb.idx i]

        The element at the step i of the stride of the second array of zipModifyAt.

        Whole loops #

        theorem ScalarArray.UInt8Array.InPlace.Tail.run_forInFrom {n : Nat} {γ : Type w} {β : Type v} (lo hi j₀ : USize) (hlo : lo.toNat ≤ j₀.toNat) (init : β) (body : (j : USize) → lo.toNat ≤ j.toNat → j.toNat < hi.toNat → (β → Tail n γ) → β → Tail n γ) (k : β → Tail n γ) (f : Nat → β × UInt8Array → β × UInt8Array) (hf : ∀ (j : Nat) (x : β × UInt8Array), (f j x).snd.size = x.snd.size) (hbody : ∀ (j : USize) (hl : lo.toNat ≤ j.toNat) (hj : j.toNat < hi.toNat) (next : β → Tail n γ) (b : β) (a : UInt8Array) (h : a.size = n), (body j hl hj next b).run a h = (next (f j.toNat (b, a)).fst).run (f j.toNat (b, a)).snd ⋯) (a : UInt8Array) (h : a.size = n) :
        (forInFrom lo hi j₀ hlo init body k).run a h = (k (iterRange f j₀.toNat hi.toNat (init, a)).fst).run (iterRange f j₀.toNat hi.toNat (init, a)).snd ⋯

        Runs the loop Tail.forInFrom lo hi j₀ hlo init body k whose step j maps its variable b and the array a by f j.toNat (b, a), before it runs the next step: it maps them by iterRange f j₀.toNat hi.toNat, and then runs k.

        theorem ScalarArray.UInt8Array.InPlace.Tail.run_forInFrom_unit {n : Nat} {γ : Type w} (lo hi j₀ : USize) (hlo : lo.toNat ≤ j₀.toNat) (body : (j : USize) → lo.toNat ≤ j.toNat → j.toNat < hi.toNat → (Unit → Tail n γ) → Unit → Tail n γ) (k : Unit → Tail n γ) (F : Nat → UInt8Array → UInt8Array) (hF : ∀ (j : Nat) (a : UInt8Array), (F j a).size = a.size) (hbody : ∀ (j : USize) (hl : lo.toNat ≤ j.toNat) (hj : j.toNat < hi.toNat) (next : Unit → Tail n γ) (a : UInt8Array) (h : a.size = n), (body j hl hj next ()).run a h = (next ()).run (F j.toNat a) ⋯) (a : UInt8Array) (h : a.size = n) :
        (forInFrom lo hi j₀ hlo () body k).run a h = (k ()).run (iterRange F j₀.toNat hi.toNat a) ⋯

        Runs the loop Tail.forInFrom lo hi j₀ hlo () body k with no variable whose step j maps the array a by F j.toNat, before it runs the next step: it maps the array by iterRange F j₀.toNat hi.toNat, and then runs k. This is the case of a loop of loops, where F j is the model of the inner loop.

        theorem ScalarArray.UInt8Array.InPlace.Tail.run_forInFrom_modify {n : Nat} {γ : Type w} (lo hi j₀ : USize) (hlo : lo.toNat ≤ j₀.toNat) (e : USize → USize) (idx : Nat → Nat) (he : ∀ (j : USize), lo.toNat ≤ j.toNat → j.toNat < hi.toNat → (e j).toNat = idx j.toNat) (p : ∀ (j : USize), lo.toNat ≤ j.toNat → j.toNat < hi.toNat → (e j).toNat < n) (g : (j : USize) → lo.toNat ≤ j.toNat → j.toNat < hi.toNat → UInt8 → UInt8) (G : Nat → UInt8 → UInt8) (hg : ∀ (j : USize) (hl : lo.toNat ≤ j.toNat) (hj : j.toNat < hi.toNat) (v : UInt8), g j hl hj v = G j.toNat v) (k : Unit → Tail n γ) (a : UInt8Array) (h : a.size = n) :
        (forInFrom lo hi j₀ hlo () (fun (j : USize) (hl : lo.toNat ≤ j.toNat) (hj : j.toNat < hi.toNat) (next : Unit → Tail n γ) (x : Unit) => readThen (e j) ⋯ fun (v : UInt8) => writeThen (e j) ⋯ (g j hl hj v) (next ())) k).run a h = (k ()).run (a.modifyAt idx j₀.toNat hi.toNat G) ⋯

        Runs the loop for j in lo...hi do a[e j] := g a[e j] of the inplace notation, from the step j₀, where the value of the index e j is idx j.toNat and g may use the bounds of j: it maps the array by modifyAt idx j₀.toNat hi.toNat G, and then runs k.

        theorem ScalarArray.UInt8Array.InPlace.Tail.run_forInFrom_write {n : Nat} {γ : Type w} (lo hi j₀ : USize) (hlo : lo.toNat ≤ j₀.toNat) (e : USize → USize) (idx : Nat → Nat) (he : ∀ (j : USize), lo.toNat ≤ j.toNat → j.toNat < hi.toNat → (e j).toNat = idx j.toNat) (p : ∀ (j : USize), lo.toNat ≤ j.toNat → j.toNat < hi.toNat → (e j).toNat < n) (g : (j : USize) → lo.toNat ≤ j.toNat → j.toNat < hi.toNat → UInt8) (G : Nat → UInt8) (hg : ∀ (j : USize) (hl : lo.toNat ≤ j.toNat) (hj : j.toNat < hi.toNat), g j hl hj = G j.toNat) (k : Unit → Tail n γ) (a : UInt8Array) (h : a.size = n) :
        (forInFrom lo hi j₀ hlo () (fun (j : USize) (hl : lo.toNat ≤ j.toNat) (hj : j.toNat < hi.toNat) (next : Unit → Tail n γ) (x : Unit) => writeThen (e j) ⋯ (g j hl hj) (next ())) k).run a h = (k ()).run (a.modifyAt idx j₀.toNat hi.toNat fun (i : Nat) (x : UInt8) => G i) ⋯

        Runs the loop for j in lo...hi do a[e j] := g of the inplace notation, from the step j₀, where the value of the index e j is idx j.toNat and g may use the bounds of j: it maps the array by modifyAt idx j₀.toNat hi.toNat fun i _ => G i, and then runs k.

        theorem ScalarArray.UInt8Array.InPlace.Tail.run_forInFrom_modify2 {n : Nat} {γ : Type w} (lo hi j₀ : USize) (hlo : lo.toNat ≤ j₀.toNat) (ea eb : USize → USize) (ia ib : Nat → Nat) (hea : ∀ (j : USize), lo.toNat ≤ j.toNat → j.toNat < hi.toNat → (ea j).toNat = ia j.toNat) (heb : ∀ (j : USize), lo.toNat ≤ j.toNat → j.toNat < hi.toNat → (eb j).toNat = ib j.toNat) (pa : ∀ (j : USize), lo.toNat ≤ j.toNat → j.toNat < hi.toNat → (ea j).toNat < n) (pb : ∀ (j : USize), lo.toNat ≤ j.toNat → j.toNat < hi.toNat → (eb j).toNat < n) (f g : (j : USize) → lo.toNat ≤ j.toNat → j.toNat < hi.toNat → UInt8 → UInt8 → UInt8) (F G : Nat → UInt8 → UInt8 → UInt8) (hf : ∀ (j : USize) (hl : lo.toNat ≤ j.toNat) (hj : j.toNat < hi.toNat) (u v : UInt8), f j hl hj u v = F j.toNat u v) (hg : ∀ (j : USize) (hl : lo.toNat ≤ j.toNat) (hj : j.toNat < hi.toNat) (u v : UInt8), g j hl hj u v = G j.toNat u v) (k : Unit → Tail n γ) (a : UInt8Array) (h : a.size = n) :
        (forInFrom lo hi j₀ hlo () (fun (j : USize) (hl : lo.toNat ≤ j.toNat) (hj : j.toNat < hi.toNat) (next : Unit → Tail n γ) (x : Unit) => readThen (ea j) ⋯ fun (u : UInt8) => readThen (eb j) ⋯ fun (v : UInt8) => writeThen (ea j) ⋯ (f j hl hj u v) (writeThen (eb j) ⋯ (g j hl hj u v) (next ()))) k).run a h = (k ()).run (a.modifyAt2 ia ib j₀.toNat hi.toNat F G) ⋯

        Runs the loop for j in lo...hi do let u := a[ea j]; let v := a[eb j]; a[ea j] := f u v; a[eb j] := g u v of the inplace notation, from the step j₀, where the values of the indices ea j and eb j are ia j.toNat and ib j.toNat, and f and g may use the bounds of j: it maps the array by modifyAt2 ia ib j₀.toNat hi.toNat F G, and then runs k.

        Whole loops down #

        theorem ScalarArray.UInt8Array.InPlace.Tail.run_forInRevFrom {n : Nat} {γ : Type w} {β : Type v} (lo hi j₀ : USize) (hj₀ : j₀.toNat ≤ hi.toNat) (init : β) (body : (j : USize) → lo.toNat ≤ j.toNat → j.toNat < hi.toNat → (β → Tail n γ) → β → Tail n γ) (k : β → Tail n γ) (f : Nat → β × UInt8Array → β × UInt8Array) (hf : ∀ (j : Nat) (x : β × UInt8Array), (f j x).snd.size = x.snd.size) (hbody : ∀ (j : USize) (hl : lo.toNat ≤ j.toNat) (hj : j.toNat < hi.toNat) (next : β → Tail n γ) (b : β) (a : UInt8Array) (h : a.size = n), (body j hl hj next b).run a h = (next (f j.toNat (b, a)).fst).run (f j.toNat (b, a)).snd ⋯) (a : UInt8Array) (h : a.size = n) :
        (forInRevFrom lo hi j₀ hj₀ init body k).run a h = (k (iterRangeRev f lo.toNat j₀.toNat (init, a)).fst).run (iterRangeRev f lo.toNat j₀.toNat (init, a)).snd ⋯

        Runs the loop Tail.forInRevFrom lo hi j₀ hj₀ init body k, from j₀ - 1 down to lo, whose step j maps its variable b and the array a by f j.toNat (b, a), before it runs the next step: it maps them by iterRangeRev f lo.toNat j₀.toNat, and then runs k.

        theorem ScalarArray.UInt8Array.InPlace.Tail.run_forInRevFrom_unit {n : Nat} {γ : Type w} (lo hi j₀ : USize) (hj₀ : j₀.toNat ≤ hi.toNat) (body : (j : USize) → lo.toNat ≤ j.toNat → j.toNat < hi.toNat → (Unit → Tail n γ) → Unit → Tail n γ) (k : Unit → Tail n γ) (F : Nat → UInt8Array → UInt8Array) (hF : ∀ (j : Nat) (a : UInt8Array), (F j a).size = a.size) (hbody : ∀ (j : USize) (hl : lo.toNat ≤ j.toNat) (hj : j.toNat < hi.toNat) (next : Unit → Tail n γ) (a : UInt8Array) (h : a.size = n), (body j hl hj next ()).run a h = (next ()).run (F j.toNat a) ⋯) (a : UInt8Array) (h : a.size = n) :
        (forInRevFrom lo hi j₀ hj₀ () body k).run a h = (k ()).run (iterRangeRev F lo.toNat j₀.toNat a) ⋯

        Runs the loop Tail.forInRevFrom lo hi j₀ hj₀ () body k with no variable, from j₀ - 1 down to lo, whose step j maps the array a by F j.toNat, before it runs the next step: it maps the array by iterRangeRev F lo.toNat j₀.toNat, and then runs k.

        theorem ScalarArray.UInt8Array.InPlace.Tail.run_forInRevFrom_modify {n : Nat} {γ : Type w} (lo hi j₀ : USize) (hj₀ : j₀.toNat ≤ hi.toNat) (e : USize → USize) (idx : Nat → Nat) (he : ∀ (j : USize), lo.toNat ≤ j.toNat → j.toNat < hi.toNat → (e j).toNat = idx j.toNat) (hinj : ∀ (i : Nat), lo.toNat ≤ i → i < j₀.toNat → ∀ (j : Nat), lo.toNat ≤ j → j < j₀.toNat → idx i = idx j → i = j) (p : ∀ (j : USize), lo.toNat ≤ j.toNat → j.toNat < hi.toNat → (e j).toNat < n) (g : (j : USize) → lo.toNat ≤ j.toNat → j.toNat < hi.toNat → UInt8 → UInt8) (G : Nat → UInt8 → UInt8) (hg : ∀ (j : USize) (hl : lo.toNat ≤ j.toNat) (hj : j.toNat < hi.toNat) (v : UInt8), g j hl hj v = G j.toNat v) (k : Unit → Tail n γ) (a : UInt8Array) (h : a.size = n) :
        (forInRevFrom lo hi j₀ hj₀ () (fun (j : USize) (hl : lo.toNat ≤ j.toNat) (hj : j.toNat < hi.toNat) (next : Unit → Tail n γ) (x : Unit) => readThen (e j) ⋯ fun (v : UInt8) => writeThen (e j) ⋯ (g j hl hj v) (next ())) k).run a h = (k ()).run (a.modifyAt idx lo.toNat j₀.toNat G) ⋯

        Runs the loop for j in reverse lo...hi do a[e j] := g a[e j] of the inplace notation, from j₀ - 1 down to lo, where the value of the index e j is idx j.toNat, which is injective on the range, and g may use the bounds of j: it maps the array by modifyAt idx lo.toNat j₀.toNat G, and then runs k.

        theorem ScalarArray.UInt8Array.InPlace.Tail.run_forInRevFrom_write {n : Nat} {γ : Type w} (lo hi j₀ : USize) (hj₀ : j₀.toNat ≤ hi.toNat) (e : USize → USize) (idx : Nat → Nat) (he : ∀ (j : USize), lo.toNat ≤ j.toNat → j.toNat < hi.toNat → (e j).toNat = idx j.toNat) (hinj : ∀ (i : Nat), lo.toNat ≤ i → i < j₀.toNat → ∀ (j : Nat), lo.toNat ≤ j → j < j₀.toNat → idx i = idx j → i = j) (p : ∀ (j : USize), lo.toNat ≤ j.toNat → j.toNat < hi.toNat → (e j).toNat < n) (g : (j : USize) → lo.toNat ≤ j.toNat → j.toNat < hi.toNat → UInt8) (G : Nat → UInt8) (hg : ∀ (j : USize) (hl : lo.toNat ≤ j.toNat) (hj : j.toNat < hi.toNat), g j hl hj = G j.toNat) (k : Unit → Tail n γ) (a : UInt8Array) (h : a.size = n) :
        (forInRevFrom lo hi j₀ hj₀ () (fun (j : USize) (hl : lo.toNat ≤ j.toNat) (hj : j.toNat < hi.toNat) (next : Unit → Tail n γ) (x : Unit) => writeThen (e j) ⋯ (g j hl hj) (next ())) k).run a h = (k ()).run (a.modifyAt idx lo.toNat j₀.toNat fun (i : Nat) (x : UInt8) => G i) ⋯

        Runs the loop for j in reverse lo...hi do a[e j] := g of the inplace notation, from j₀ - 1 down to lo, where the value of the index e j is idx j.toNat, which is injective on the range, and g may use the bounds of j: it maps the array by modifyAt idx lo.toNat j₀.toNat fun i _ => G i, and then runs k.

        theorem ScalarArray.UInt8Array.InPlace.run_zipForInFrom {n m : Nat} (hi j₀ : USize) (ea eb : USize → USize) (ia ib : Nat → Nat) (hia : ∀ (j : USize), j.toNat < hi.toNat → (ea j).toNat = ia j.toNat) (hib : ∀ (j : USize), j.toNat < hi.toNat → (eb j).toNat = ib j.toNat) (hea : ∀ (j : USize), j.toNat < hi.toNat → (ea j).toNat < n) (heb : ∀ (j : USize), j.toNat < hi.toNat → (eb j).toNat < m) (f g : UInt8 → UInt8 → UInt8) (a b : UInt8Array) (ha : a.size = n) (hb : b.size = m) :
        zipForInFrom hi ea eb hea heb f g j₀ a b ha hb = a.zipModifyAt b ia ib j₀.toNat hi.toNat (fun (x : Nat) => f) fun (x : Nat) => g

        zipForInFrom runs as zipModifyAt, from its step j₀, when the values of the indices ea j and eb j are ia j.toNat and ib j.toNat.

        Strided loops #

        The loops modifyStride, modifyStride2 and zipModifyStride update the elements at the first n steps of strides. Each makes its arrays exclusive once, before it starts, and then updates them in place without checks. Each is specified by a modifyAt model, by @[csimp], and runs as the model when an array has USize.size elements or more, which no array in memory has.

        a with f applied to the element at each of the first n steps of the stride s, which fits in a: the model a.modifyAt s.idx 0 n fun _ => f, run as one loop.

        Instances For
          theorem ScalarArray.UInt8Array.modifyStride_eq (a : UInt8Array) (s : Stride) (n : Nat) (f : UInt8 → UInt8) (h : s.Fits n a.size) :
          a.modifyStride s n f h = a.modifyAt s.idx 0 n fun (x : Nat) => f
          @[simp]
          theorem ScalarArray.UInt8Array.size_modifyStride (a : UInt8Array) (s : Stride) (n : Nat) (f : UInt8 → UInt8) (h : s.Fits n a.size) :
          (a.modifyStride s n f h).size = a.size
          @[inline]

          For internal use only.

          Instances For
            def ScalarArray.UInt8Array.modifyStride2 (a : UInt8Array) (sa sb : Stride) (n : Nat) (f g : UInt8 → UInt8 → UInt8) (_ha : sa.Fits n a.size) (_hb : sb.Fits n a.size) :

            a with, at each of the first n steps i of the strides sa and sb, which fit in a, f u v written at sa.idx i and g u v at sb.idx i, where u and v are the elements there: the model a.modifyAt2 sa.idx sb.idx 0 n (fun _ => f) (fun _ => g), run as one loop, such as a plane rotation of two rows of a matrix. modifyAt2_eq gives the elements when the two strides never meet, which Stride.idx_ne_idx_of_same_inc and Stride.idx_ne_idx_of_lt_off give.

            Instances For
              theorem ScalarArray.UInt8Array.modifyStride2_eq (a : UInt8Array) (sa sb : Stride) (n : Nat) (f g : UInt8 → UInt8 → UInt8) (ha : sa.Fits n a.size) (hb : sb.Fits n a.size) :
              a.modifyStride2 sa sb n f g ha hb = a.modifyAt2 sa.idx sb.idx 0 n (fun (x : Nat) => f) fun (x : Nat) => g
              @[simp]
              theorem ScalarArray.UInt8Array.size_modifyStride2 (a : UInt8Array) (sa sb : Stride) (n : Nat) (f g : UInt8 → UInt8 → UInt8) (ha : sa.Fits n a.size) (hb : sb.Fits n a.size) :
              (a.modifyStride2 sa sb n f g ha hb).size = a.size
              @[inline]
              def ScalarArray.UInt8Array.modifyStride2Impl (a : UInt8Array) (sa sb : Stride) (n : Nat) (f g : UInt8 → UInt8 → UInt8) (ha : sa.Fits n a.size) (hb : sb.Fits n a.size) :

              For internal use only.

              Instances For
                def ScalarArray.UInt8Array.zipModifyStride (a b : UInt8Array) (sa sb : Stride) (n : Nat) (f g : UInt8 → UInt8 → UInt8) (_ha : sa.Fits n a.size) (_hb : sb.Fits n b.size) :

                The arrays a and b with, at each of the first n steps i of the strides sa, which fits in a, and sb, which fits in b, f u v written at sa.idx i in a and g u v at sb.idx i in b, where u and v are the elements there: the model zipModifyAt a b sa.idx sb.idx 0 n (fun _ => f) (fun _ => g), run as one loop, such as a plane rotation of two vectors. a and b may be the same array: the model treats them as two arrays, and so does the loop, which then copies one of them.

                Instances For
                  theorem ScalarArray.UInt8Array.zipModifyStride_eq (a b : UInt8Array) (sa sb : Stride) (n : Nat) (f g : UInt8 → UInt8 → UInt8) (ha : sa.Fits n a.size) (hb : sb.Fits n b.size) :
                  a.zipModifyStride b sa sb n f g ha hb = a.zipModifyAt b sa.idx sb.idx 0 n (fun (x : Nat) => f) fun (x : Nat) => g
                  @[simp]
                  theorem ScalarArray.UInt8Array.size_zipModifyStride_fst (a b : UInt8Array) (sa sb : Stride) (n : Nat) (f g : UInt8 → UInt8 → UInt8) (ha : sa.Fits n a.size) (hb : sb.Fits n b.size) :
                  (a.zipModifyStride b sa sb n f g ha hb).fst.size = a.size
                  @[simp]
                  theorem ScalarArray.UInt8Array.size_zipModifyStride_snd (a b : UInt8Array) (sa sb : Stride) (n : Nat) (f g : UInt8 → UInt8 → UInt8) (ha : sa.Fits n a.size) (hb : sb.Fits n b.size) :
                  (a.zipModifyStride b sa sb n f g ha hb).snd.size = b.size
                  @[inline]
                  def ScalarArray.UInt8Array.zipModifyStrideImpl (a b : UInt8Array) (sa sb : Stride) (n : Nat) (f g : UInt8 → UInt8 → UInt8) (ha : sa.Fits n a.size) (hb : sb.Fits n b.size) :

                  For internal use only.

                  Instances For