doc-next-gen

Init.Data.Array.Subarray

Module docstring

{}

Subarray structure
(α : Type u)
Full source
/--
A region of some underlying array.

A subarray contains an array together with the start and end indices of a region of interest.
Subarrays can be used to avoid copying or allocating space, while being more convenient than
tracking the bounds by hand. The region of interest consists of every index that is both greater
than or equal to `start` and strictly less than `stop`.
-/
structure Subarray (α : Type u) where
  /-- The underlying array. -/
  array : Array α
  /-- The starting index of the region of interest (inclusive). -/
  start : Nat
  /-- The ending index of the region of interest (exclusive). -/
  stop : Nat
  /--
  The starting index is no later than the ending index.

  The ending index is exclusive. If the starting and ending indices are equal, then the subarray is
  empty.
  -/
  start_le_stop : start ≤ stop
  /-- The stopping index is no later than the end of the array.

  The ending index is exclusive. If it is equal to the size of the array, then the last element of
  the array is in the subarray.
  -/
  stop_le_array_size : stop ≤ array.size
Subarray
Informal description
A subarray is a structure representing a contiguous region of an underlying array, specified by a start index (inclusive) and an end index (exclusive). It provides a convenient way to work with a segment of an array without copying or allocating additional space. The region includes all indices $i$ such that $\text{start} \leq i < \text{stop}$.
Subarray.size definition
(s : Subarray α) : Nat
Full source
/--
Computes the size of the subarray.
-/
def size (s : Subarray α) : Nat :=
  s.stop - s.start
Size of a subarray
Informal description
For a subarray $s$ of type $\alpha$, the size is defined as the difference between the end index $s.\text{stop}$ and the start index $s.\text{start}$.
Subarray.size_le_array_size theorem
{s : Subarray α} : s.size ≤ s.array.size
Full source
theorem size_le_array_size {s : Subarray α} : s.size ≤ s.array.size := by
  let {array, start, stop, start_le_stop, stop_le_array_size} := s
  simp [size]
  apply Nat.le_trans (Nat.sub_le stop start)
  assumption
Subarray Size Bound: $|s| \leq |\text{array}|$
Informal description
For any subarray $s$ of type $\alpha$, the size of $s$ is less than or equal to the size of the underlying array, i.e., $s.\text{size} \leq s.\text{array}.\text{size}$.
Subarray.get definition
(s : Subarray α) (i : Fin s.size) : α
Full source
/--
Extracts an element from the subarray.

The index is relative to the start of the subarray, rather than the underlying array.
-/
def get (s : Subarray α) (i : Fin s.size) : α :=
  have : s.start + i.val < s.array.size := by
   apply Nat.lt_of_lt_of_le _ s.stop_le_array_size
   have := i.isLt
   simp [size] at this
   rw [Nat.add_comm]
   exact Nat.add_lt_of_lt_sub this
  s.array[s.start + i.val]
Element access in a subarray
Informal description
Given a subarray $s$ of type $\alpha$ and an index $i$ of type $\text{Fin } s.\text{size}$ (a natural number less than the size of the subarray), the function returns the element at position $s.\text{start} + i$ in the underlying array. The index is interpreted relative to the start of the subarray.
Subarray.instGetElemNatLtSize instance
: GetElem (Subarray α) Nat α fun xs i => i < xs.size
Full source
instance : GetElem (Subarray α) Nat α fun xs i => i < xs.size where
  getElem xs i h := xs.get ⟨i, h⟩
Element Access in Subarrays via Natural Number Indices
Informal description
For any subarray `xs` of type `α` and natural number index `i`, the notation `xs[i]` is valid and returns an element of type `α` if and only if `i` is less than the size of the subarray `xs`.
Subarray.getD definition
(s : Subarray α) (i : Nat) (v₀ : α) : α
Full source
/--
Extracts an element from the subarray, or returns a default value `v₀` when the index is out of
bounds.

The index is relative to the start and end of the subarray, rather than the underlying array.
-/
@[inline] def getD (s : Subarray α) (i : Nat) (v₀ : α) : α :=
  if h : i < s.size then s[i] else v₀
Default element access in a subarray
Informal description
Given a subarray $s$ of type $\alpha$, a natural number index $i$, and a default value $v_0$ of type $\alpha$, the function returns the element at position $i$ within the subarray if $i$ is within bounds (i.e., $i < s.\text{size}$), otherwise it returns the default value $v_0$. The index $i$ is interpreted relative to the start of the subarray.
Subarray.get! abbrev
[Inhabited α] (s : Subarray α) (i : Nat) : α
Full source
/--
Extracts an element from the subarray, or returns a default value when the index is out of bounds.

The index is relative to the start and end of the subarray, rather than the underlying array. The
default value is that provided by the `Inhabited α` instance.
-/
abbrev get! [Inhabited α] (s : Subarray α) (i : Nat) : α :=
  getD s i default
Default-Access Element Retrieval for Subarrays
Informal description
Given a subarray $s$ of type $\alpha$ (where $\alpha$ is an inhabited type) and a natural number index $i$, the function returns the element at position $i$ within the subarray if $i$ is within bounds (i.e., $i < s.\text{size}$), otherwise it returns the default value of type $\alpha$.
Subarray.popFront definition
(s : Subarray α) : Subarray α
Full source
/--
Shrinks the subarray by incrementing its starting index if possible, returning it unchanged if not.

Examples:
 * `#[1,2,3].toSubarray.popFront.toArray = #[2, 3]`
 * `#[1,2,3].toSubarray.popFront.popFront.toArray = #[3]`
 * `#[1,2,3].toSubarray.popFront.popFront.popFront.toArray = #[]`
 * `#[1,2,3].toSubarray.popFront.popFront.popFront.popFront.toArray = #[]`
-/
def popFront (s : Subarray α) : Subarray α :=
  if h : s.start < s.stop then
    { s with start := s.start + 1, start_le_stop := Nat.le_of_lt_succ (Nat.add_lt_add_right h 1) }
  else
    s
Shrink subarray by incrementing start index
Informal description
Given a subarray `s` of type `Subarray α`, the operation `popFront` attempts to shrink the subarray by incrementing its starting index by 1, provided the starting index is less than the stopping index. If the starting index is not less than the stopping index, the subarray is returned unchanged. More formally, if `s.start < s.stop`, then `popFront s` returns a new subarray with `start` incremented by 1 and the `start_le_stop` property adjusted accordingly. Otherwise, it returns `s` unchanged.
Subarray.empty definition
: Subarray α
Full source
/--
The empty subarray.

This empty subarray is backed by an empty array.
-/
protected def empty : Subarray α where
  array := #[]
  start := 0
  stop := 0
  start_le_stop := Nat.le_refl 0
  stop_le_array_size := Nat.le_refl 0
Empty subarray
Informal description
The empty subarray is a subarray structure where the underlying array is empty, and both the start and stop indices are set to 0. This ensures that the subarray represents no elements from the underlying array.
Subarray.instEmptyCollection instance
: EmptyCollection (Subarray α)
Full source
instance : EmptyCollection (Subarray α) :=
  ⟨Subarray.empty⟩
Empty Subarray Notation
Informal description
The empty collection notation `∅` (or `{}`) can be used for subarrays, representing a subarray with no elements.
Subarray.instInhabited instance
: Inhabited (Subarray α)
Full source
instance : Inhabited (Subarray α) :=
  ⟨{}⟩
Default Element for Subarrays
Informal description
Every subarray type has a default element.
Subarray.forIn opaque
{α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (s : Subarray α) (b : β) (f : α → β → m (ForInStep β)) : m β
Full source
/--
The implementation of `ForIn.forIn` for `Subarray`, which allows it to be used with `for` loops in
`do`-notation.
-/
-- TODO: provide reference implementation
@[implemented_by Subarray.forInUnsafe]
protected opaque forIn {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (s : Subarray α) (b : β) (f : α → β → m (ForInStep β)) : m β :=
  pure b
Monadic Iteration over Subarrays
Informal description
Given a monad `m`, a subarray `s` of type `α`, an initial state `b` of type `β`, and a function `f : α → β → m (ForInStep β)`, the operation `forIn` performs a monadic iteration over the elements of the subarray `s`. The function `f` is applied to each element of `s` and the current state, producing a monadic action that returns either: - `yield b'` to continue iteration with the new state `b'`, or - `done b'` to terminate early with the final state `b'`. The operation returns the final state after iteration completes or terminates early.
Subarray.instForIn instance
: ForIn m (Subarray α) α
Full source
instance : ForIn m (Subarray α) α where
  forIn := Subarray.forIn
Monadic Iteration over Subarrays
Informal description
For any monad `m` and type `α`, a subarray of elements of type `α` can be iterated over monadically, where each element in the subarray is processed sequentially in the monadic context.
Subarray.foldlM definition
{α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : β → α → m β) (init : β) (as : Subarray α) : m β
Full source
/--
Folds a monadic operation from left to right over the elements in a subarray.

An accumulator of type `β` is constructed by starting with `init` and monadically combining each
element of the subarray with the current accumulator value in turn. The monad in question may permit
early termination or repetition.

Examples:
```lean example
#eval #["red", "green", "blue"].toSubarray.foldlM (init := "") fun acc x => do
  let l ← Option.guard (· ≠ 0) x.length
  return s!"{acc}({l}){x} "
```
```output
some "(3)red (5)green (4)blue "
```
```lean example
#eval #["red", "green", "blue"].toSubarray.foldlM (init := 0) fun acc x => do
  let l ← Option.guard (· ≠ 5) x.length
  return s!"{acc}({l}){x} "
```
```output
none
```
-/
@[inline]
def foldlM {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : β → α → m β) (init : β) (as : Subarray α) : m β :=
  as.array.foldlM f (init := init) (start := as.start) (stop := as.stop)
Monadic left fold over a subarray
Informal description
Given a monad `m`, a subarray `as` of type `α`, an initial value `init` of type `β`, and a monadic function `f : β → α → m β`, the operation `foldlM` performs a left fold over the elements of the subarray `as`. Starting with `init`, it sequentially applies `f` to each element of `as` and the current accumulator value, updating the accumulator with the result of the monadic computation. The operation returns the final accumulator value after processing all elements of the subarray. This operation allows for early termination or repetition within the monadic context, depending on the behavior of `f` and the monad `m`.
Subarray.foldrM definition
{α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : α → β → m β) (init : β) (as : Subarray α) : m β
Full source
/--
Folds a monadic operation from right to left over the elements in a subarray.

An accumulator of type `β` is constructed by starting with `init` and monadically combining each
element of the subarray with the current accumulator value in turn, moving from the end to the
start. The monad in question may permit early termination or repetition.

Examples:
```lean example
#eval #["red", "green", "blue"].toSubarray.foldrM (init := "") fun x acc => do
  let l ← Option.guard (· ≠ 0) x.length
  return s!"{acc}({l}){x} "
```
```output
some "(4)blue (5)green (3)red "
```
```lean example
#eval #["red", "green", "blue"].toSubarray.foldrM (init := 0) fun x acc => do
  let l ← Option.guard (· ≠ 5) x.length
  return s!"{acc}({l}){x} "
```
```output
none
```
-/
@[inline]
def foldrM {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f : α → β → m β) (init : β) (as : Subarray α) : m β :=
  as.array.foldrM f (init := init) (start := as.stop) (stop := as.start)
Monadic right fold over a subarray
Informal description
Given a monad `m`, a subarray `as` of type `α`, an initial value `init` of type `β`, and a monadic function `f : α → β → m β`, the operation `foldrM` performs a right fold over the elements of the subarray `as`. Starting with `init`, it sequentially applies `f` to each element of `as` (from the end to the start) and the current accumulator value, updating the accumulator with the result of the monadic computation. The operation returns the final accumulator value after processing all elements of the subarray. This operation allows for early termination or repetition within the monadic context, depending on the behavior of `f` and the monad `m`.
Subarray.anyM definition
{α : Type u} {m : Type → Type w} [Monad m] (p : α → m Bool) (as : Subarray α) : m Bool
Full source
/--
Checks whether any of the elements in a subarray satisfy a monadic Boolean predicate.

The elements are tested starting at the lowest index and moving up. The search terminates as soon as
an element that satisfies the predicate is found.

Example:
```lean example
#eval #["red", "green", "blue", "orange"].toSubarray.popFront.anyM fun x => do
  IO.println x
  pure (x == "blue")
```
```output
green
blue
```
```output
true
```
-/
@[inline]
def anyM {α : Type u} {m : Type → Type w} [Monad m] (p : α → m Bool) (as : Subarray α) : m Bool :=
  as.array.anyM p (start := as.start) (stop := as.stop)
Monadic any predicate for subarrays
Informal description
Given a monadic predicate `p : α → m Bool` and a subarray `as : Subarray α`, the function `Subarray.anyM` checks whether any element in the subarray satisfies the predicate `p`. The elements are tested in order from the lowest index to the highest, and the search terminates as soon as an element satisfies the predicate. The result is a monadic computation producing a Boolean value indicating whether any element satisfied the predicate.
Subarray.allM definition
{α : Type u} {m : Type → Type w} [Monad m] (p : α → m Bool) (as : Subarray α) : m Bool
Full source
/--
Checks whether all of the elements in a subarray satisfy a monadic Boolean predicate.

The elements are tested starting at the lowest index and moving up. The search terminates as soon as
an element that does not satisfy the predicate is found.

Example:
```lean example
#eval #["red", "green", "blue", "orange"].toSubarray.popFront.allM fun x => do
  IO.println x
  pure (x.length == 5)
```
```output
green
blue
```
```output
false
```
-/
@[inline]
def allM {α : Type u} {m : Type → Type w} [Monad m] (p : α → m Bool) (as : Subarray α) : m Bool :=
  as.array.allM p (start := as.start) (stop := as.stop)
Monadic all predicate for subarrays
Informal description
Given a monadic predicate `p : α → m Bool` and a subarray `as : Subarray α`, the function `Subarray.allM` checks whether all elements in the subarray satisfy the predicate `p`. The elements are tested in order from the lowest index to the highest, and the search terminates as soon as an element fails the predicate. The result is a monadic computation producing a Boolean value indicating whether all elements satisfied the predicate.
Subarray.forM definition
{α : Type u} {m : Type v → Type w} [Monad m] (f : α → m PUnit) (as : Subarray α) : m PUnit
Full source
/--
Runs a monadic action on each element of a subarray.

The elements are processed starting at the lowest index and moving up.
-/
@[inline]
def forM {α : Type u} {m : Type v → Type w} [Monad m] (f : α → m PUnit) (as : Subarray α) : m PUnit :=
  as.array.forM f (start := as.start) (stop := as.stop)
Monadic iteration over a subarray
Informal description
Given a monadic action `f` and a subarray `as`, this function applies `f` to each element of `as` in order (from the lowest index to the highest). The monadic action `f` takes an element of type `α` and returns a computation in the monad `m` producing a value of type `PUnit` (the unit type). The function processes the elements of the subarray starting from the start index and moving forward to the end index.
Subarray.forRevM definition
{α : Type u} {m : Type v → Type w} [Monad m] (f : α → m PUnit) (as : Subarray α) : m PUnit
Full source
/--
Runs a monadic action on each element of a subarray, in reverse order.

The elements are processed starting at the highest index and moving down.
-/
@[inline]
def forRevM {α : Type u} {m : Type v → Type w} [Monad m] (f : α → m PUnit) (as : Subarray α) : m PUnit :=
  as.array.forRevM f (start := as.stop) (stop := as.start)
Reverse-order monadic iteration over a subarray
Informal description
Given a monadic action `f` and a subarray `as`, this function applies `f` to each element of `as` in reverse order (from the highest index to the lowest). The monadic action `f` takes an element of type `α` and returns a computation in the monad `m` producing a value of type `PUnit` (the unit type). The function processes the elements of the subarray starting from the end index and moving backward to the start index.
Subarray.foldl definition
{α : Type u} {β : Type v} (f : β → α → β) (init : β) (as : Subarray α) : β
Full source
/--
Folds an operation from left to right over the elements in a subarray.

An accumulator of type `β` is constructed by starting with `init` and combining each
element of the subarray with the current accumulator value in turn.

Examples:
 * `#["red", "green", "blue"].toSubarray.foldl (· + ·.length) 0 = 12`
 * `#["red", "green", "blue"].toSubarray.popFront.foldl (· + ·.length) 0 = 9`
-/
@[inline]
def foldl {α : Type u} {β : Type v} (f : β → α → β) (init : β) (as : Subarray α) : β :=
  Id.run <| as.foldlM f (init := init)
Left fold over a subarray
Informal description
Given a subarray `as` of type `α`, an initial value `init` of type `β`, and a function `f : β → α → β`, the operation `foldl` performs a left fold over the elements of the subarray `as`. Starting with `init`, it sequentially applies `f` to each element of `as` (from the start to the end) and the current accumulator value, updating the accumulator with the result. The operation returns the final accumulator value after processing all elements of the subarray.
Subarray.foldr definition
{α : Type u} {β : Type v} (f : α → β → β) (init : β) (as : Subarray α) : β
Full source
/--
Folds an operation from right to left over the elements in a subarray.

An accumulator of type `β` is constructed by starting with `init` and combining each element of the
subarray with the current accumulator value in turn, moving from the end to the start.

Examples:
 * `#eval #["red", "green", "blue"].toSubarray.foldr (·.length + ·) 0 = 12`
 * `#["red", "green", "blue"].toSubarray.popFront.foldlr (·.length + ·) 0 = 9`
-/
@[inline]
def foldr {α : Type u} {β : Type v} (f : α → β → β) (init : β) (as : Subarray α) : β :=
  Id.run <| as.foldrM f (init := init)
Right fold over a subarray
Informal description
Given a subarray `as` of type `α`, an initial value `init` of type `β`, and a function `f : α → β → β`, the operation `foldr` performs a right fold over the elements of the subarray `as`. Starting with `init`, it sequentially applies `f` to each element of `as` (from the end to the start) and the current accumulator value, updating the accumulator with the result. The operation returns the final accumulator value after processing all elements of the subarray.
Subarray.any definition
{α : Type u} (p : α → Bool) (as : Subarray α) : Bool
Full source
/--
Checks whether any of the elements in a subarray satisfy a Boolean predicate.

The elements are tested starting at the lowest index and moving up. The search terminates as soon as
an element that satisfies the predicate is found.
-/
@[inline]
def any {α : Type u} (p : α → Bool) (as : Subarray α) : Bool :=
  Id.run <| as.anyM p
Existence of satisfying element in subarray
Informal description
Given a predicate `p : α → Bool` and a subarray `as : Subarray α`, the function `Subarray.any` checks whether any element in the subarray satisfies the predicate `p`. The elements are tested in order from the lowest index to the highest, and the search terminates as soon as an element satisfies the predicate. The result is a Boolean value indicating whether any element satisfied the predicate.
Subarray.all definition
{α : Type u} (p : α → Bool) (as : Subarray α) : Bool
Full source
/--
Checks whether all of the elements in a subarray satisfy a Boolean predicate.

The elements are tested starting at the lowest index and moving up. The search terminates as soon as
an element that does not satisfy the predicate is found.
-/
@[inline]
def all {α : Type u} (p : α → Bool) (as : Subarray α) : Bool :=
  Id.run <| as.allM p
All elements in subarray satisfy predicate
Informal description
Given a predicate `p : α → Bool` and a subarray `as : Subarray α`, the function `Subarray.all` checks whether all elements in the subarray satisfy the predicate `p`. The elements are tested in order from the lowest index to the highest, and the search terminates as soon as an element fails the predicate. The result is a Boolean value indicating whether all elements satisfied the predicate.
Subarray.findSomeRevM? definition
{α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (as : Subarray α) (f : α → m (Option β)) : m (Option β)
Full source
/--
Applies a monadic function to each element in a subarray in reverse order, stopping at the first
element for which the function succeeds by returning a value other than `none`. The succeeding value
is returned, or `none` if there is no success.

Example:
```lean example
#eval #["red", "green", "blue"].toSubarray.findSomeRevM? fun x => do
  IO.println x
  return Option.guard (· = 5) x.length
```
```output
blue
green
```
```output
some 5
```
-/
@[inline]
def findSomeRevM? {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (as : Subarray α) (f : α → m (Option β)) : m (Option β) :=
  let rec @[specialize] find : (i : Nat) → i ≤ as.size → m (Option β)
    | 0,   _ => pure none
    | i+1, h => do
      have : i < as.size := Nat.lt_of_lt_of_le (Nat.lt_succ_self _) h
      let r ← f as[i]
      match r with
      | some _ => pure r
      | none   =>
        have : i ≤ as.size := Nat.le_of_lt this
        find i this
  find as.size (Nat.le_refl _)
Reverse monadic find with optional result in subarray
Informal description
Given a monad `m`, a subarray `as` of type `α`, and a monadic function `f : α → m (Option β)`, the function `Subarray.findSomeRevM?` applies `f` to each element of `as` in reverse order (from the last element to the first) until it finds an element for which `f` returns `some b` for some `b : β`. It then returns `some b` wrapped in the monad `m`. If no such element is found, it returns `none` wrapped in `m`.
Subarray.findRevM? definition
{α : Type} {m : Type → Type w} [Monad m] (as : Subarray α) (p : α → m Bool) : m (Option α)
Full source
/--
Applies a monadic Boolean predicate to each element in a subarray in reverse order, stopping at the
first element that satisfies the predicate. The element that satisfies the predicate is returned, or
`none` if no element satisfies it.

Example:
```lean example
#eval #["red", "green", "blue"].toSubarray.findRevM? fun x => do
  IO.println x
  return (x.length = 5)
```
```output
blue
green
```
```output
some 5
```
-/
@[inline]
def findRevM? {α : Type} {m : Type → Type w} [Monad m] (as : Subarray α) (p : α → m Bool) : m (Option α) :=
  as.findSomeRevM? fun a => return if (← p a) then some a else none
Reverse monadic find in subarray
Informal description
Given a monad `m`, a subarray `as` of type `α`, and a monadic predicate `p : α → m Bool`, the function `Subarray.findRevM?` applies `p` to each element of `as` in reverse order (from the last element to the first) until it finds an element for which `p` returns `true`. It then returns `some a` wrapped in the monad `m`, where `a` is the found element. If no such element is found, it returns `none` wrapped in `m`.
Subarray.findRev? definition
{α : Type} (as : Subarray α) (p : α → Bool) : Option α
Full source
/--
Tests each element in a subarray with a Boolean predicate in reverse order, stopping at the first
element that satisfies the predicate. The element that satisfies the predicate is returned, or
`none` if no element satisfies the predicate.

Examples:
 * `#["red", "green", "blue"].toSubarray.findRev? (·.length ≠ 4) = some "green"`
 * `#["red", "green", "blue"].toSubarray.findRev? (fun _ => true) = some "blue"`
 * `#["red", "green", "blue"].toSubarray 0 0 |>.findRev? (fun _ => true) = none`
-/
@[inline]
def findRev? {α : Type} (as : Subarray α) (p : α → Bool) : Option α :=
  Id.run <| as.findRevM? p
Reverse find in subarray
Informal description
Given a subarray `as` of type `α` and a predicate `p : α → Bool`, the function `Subarray.findRev?` checks each element of `as` in reverse order (from last to first) and returns the first element that satisfies `p` as `some a`. If no such element is found, it returns `none`. **Examples:** - `#["red", "green", "blue"].toSubarray.findRev? (·.length ≠ 4) = some "green"` - `#["red", "green", "blue"].toSubarray.findRev? (fun _ => true) = some "blue"` - `#["red", "green", "blue"].toSubarray 0 0 |>.findRev? (fun _ => true) = none`
Array.ofSubarray definition
(s : Subarray α) : Array α
Full source
/--
Allocates a new array that contains the contents of the subarray.
-/
@[coe]
def ofSubarray (s : Subarray α) : Array α := Id.run do
  let mut as := mkEmpty (s.stop - s.start)
  for a in s do
    as := as.push a
  return as
Array from subarray
Informal description
The function creates a new array containing the elements of the subarray `s` in the same order. It allocates a new array with size equal to the length of the subarray (from start index to end index) and copies all elements from the subarray into it.
Array.instCoeSubarray instance
: Coe (Subarray α) (Array α)
Full source
instance : Coe (Subarray α) (Array α) := ⟨ofSubarray⟩
Coercion from Subarray to Array
Informal description
For any type $\alpha$, there is a canonical way to coerce a subarray of $\alpha$ to an array of $\alpha$. This coercion creates a new array containing the elements of the subarray in the same order.
Subarray.toArray definition
(s : Subarray α) : Array α
Full source
@[inherit_doc Array.ofSubarray]
def Subarray.toArray (s : Subarray α) : Array α :=
  Array.ofSubarray s
Conversion from subarray to array
Informal description
The function creates a new array containing the elements of the subarray `s` in the same order. It is equivalent to `Array.ofSubarray s`.
instAppendSubarray instance
: Append (Subarray α)
Full source
instance : Append (Subarray α) where
  append x y :=
   let a := x.toArray ++ y.toArray
   a.toSubarray 0 a.size
Append Operation on Subarrays
Informal description
For any type $\alpha$, the type of subarrays of $\alpha$ is equipped with a canonical append operation that concatenates two subarrays.
instReprSubarray instance
[Repr α] : Repr (Subarray α)
Full source
instance [Repr α] : Repr (Subarray α) where
  reprPrec s  _ := repr s.toArray ++ ".toSubarray"
Representation Format for Subarrays
Informal description
For any type $\alpha$ with a representation format `[Repr α]`, the subarray structure `Subarray α` inherits a representation format where a subarray is displayed in the same format as its underlying array elements.
instToStringSubarray instance
[ToString α] : ToString (Subarray α)
Full source
instance [ToString α] : ToString (Subarray α) where
  toString s := toString s.toArray
String Representation of Subarrays
Informal description
For any type $\alpha$ with a string representation, the subarrays of $\alpha$ also have a string representation.