Idris2Doc : Data.Array.Index

Data.Array.Index

(source)
This module provides several ways to safely index into an
array of known size. All conversions between different index
types are optimized away by the compiler, because they are
all of the same structure during code generation: An encoding
of natural numbers which the Idris compiler converts to a
native integer representation.

The main type for indexing into an array of size `n` is `Fin n`,
representing a natural number strictly smaller than `n`.

As an alternative, we can use a natural number `k` directly, together
with a proof of type `LT k n`, showing that `k` is strictly smaller
than `n`. Such numbers can be converted directly to `Fin n` by means
of function `Data.Nat.natToFinLT`. This way of indexing is very
useful for iterating over the whole array from then end: We start
with `n` itself together with an erased proof of type `LTE n n`, which
can be generated automatically. By pattern matching on the current
position, we can safely access all positions in the array until
we arrive at 0. See the implementation of `foldr` (a private
function called `foldrI`) in module `Data.Array.Indexed` for an
example how this is used.

It is only slightly harder to iterate over an array from the
front. This is where data type `Ix m n` comes into play: It's
another way of saying that `m <= n` holds, but it works in
the oposite direction than `LTE`: It's zero constructor `IZ` proofs
that `n <= n` for all `n`, while its successor constructor
proofs that from `S k <= n` follows `k <= n`. This means, that
for a given `k`, a values of type `Ix k n` corresponds to the
values `n - k`. This allows us to recurse over a natural number
while keeping an auto-implicit proof of type `Ix k n`, and use
this proof for indexing into the array.
See the implementation of `foldl` (a private
function called `foldlI`) in module `Data.Array.Indexed` for an
example how this is used.

Reexports

importpublic Data.DPair
importpublic Data.Fin
importpublic Data.Maybe0
importpublic Data.Nat

Definitions

0ltLemma : (0k : Nat) -> (0m : Nat) -> (0n : Nat) ->k+Sm=n->LTkn
Totality: total
Visibility: export
0lteLemma : (0k : Nat) -> (0m : Nat) -> (0n : Nat) ->k+m=n->LTEkn
Totality: total
Visibility: export
0lteSuccPlus : (k : Nat) ->LTE (Sk) (k+Sm)
Totality: total
Visibility: export
dataSuffix : Lista->Lista->Type
Totality: total
Visibility: public export
Constructors:
Same : Suffixxsxs
Uncons : Suffix (x::xs) ys->Suffixxsys
suffixToNat : Suffixxsys->Nat
Totality: total
Visibility: public export
0suffixLemma : (s : Suffixxsys) ->suffixToNats+lengthxs=lengthys
Totality: total
Visibility: export
0suffixLT : (s : Suffix (x::xs) ys) ->LT (suffixToNats) (lengthys)
Totality: total
Visibility: export
suffixToFin : Suffix (x::xs) ys->Fin (lengthys)
Totality: total
Visibility: public export
dataIx : Nat->Nat->Type
  A data type for safely indexing into an array from the
front during in a fuction recursing on natural number `m`.

This is another way to proof that `m <= n`.

Totality: total
Visibility: public export
Constructors:
IZ : Ixnn
IS : Ix (Sm) n->Ixmn

Hints:
ReflexiveNatIx
TransitiveNatIx
ixToNat : Ixmn->Nat
  O(1) conversion from `Ix m n` to `Nat`. The result equals `n - m`.

Totality: total
Visibility: public export
succIx : Ixmn->Ix (Sm) (Sn)
  If `m <= n` then `S m <= S n`.

Totality: total
Visibility: public export
natToIx : (n : Nat) ->Ix0n
  Convert a natural number to the corresponding `Ix 0 n`
so that `n === ixToNat (natToIx n)` as shown in
`ixLemma`.

Totality: total
Visibility: public export
natToIx1 : (n : Nat) ->Ix1 (Sn)
  Convert a natural number to the corresponding `Ix 1 (S n)`,
the largest value strictly smaller than `S n`.

This is similar to `Data.Fin.last`.

Totality: total
Visibility: public export
0ixLemma : (x : Ixmn) ->ixToNatx+m=n
  Proof that for an index `x` of type `Ix m n` `ixToNat x` equals `n - m`.

Totality: total
Visibility: export
0ixLT : (x : Ix (Sm) n) ->LT (ixToNatx) n
  Proof an `Ix (S m) n` corresponds to a natural number
strictly smaller than `n` and can therefore be used as an index
into an array of size `n`.

Totality: total
Visibility: export
0ixLTE : (x : Ixmn) ->LTE (ixToNatx) n
  Proof an `Ix m n` corresponds to a natural number
smaller than or equal to `n

Totality: total
Visibility: export
ixToFin : Ix (Sm) n->Finn
  From lemma `ixLT` follows, that we can convert an `Ix (S m) n` to
a `Fin n` corresponding to the same natural numbers. All conversions
involved are optimized away by the identity optimizer during code
generation.

Totality: total
Visibility: public export
0finToNatLT : (x : Finn) ->LT (finToNatx) n
Totality: total
Visibility: export
0SubLength : Nat->Type
  This type is used to cut off a portion of
a `ByteString`. It must be no larger than the number
of elements in the ByteString

Totality: total
Visibility: public export
sublength : (k : Nat) -> {auto0_ : LTEkn} ->SubLengthn
Totality: total
Visibility: export
fromFin : Finn->SubLengthn
Totality: total
Visibility: export
fromIx : Ixmn->SubLengthn
Totality: total
Visibility: export
0refl : LTEnn
Totality: total
Visibility: export
0lsl : LTE (Sm) n=>LTEmn
Totality: total
Visibility: export
0lteOpReflectsLTE : (m : Nat) -> (n : Nat) ->m<=n=True->LTEmn
Totality: total
Visibility: export
0ltOpReflectsLT : (m : Nat) -> (n : Nat) ->m<n=True->LTmn
Totality: total
Visibility: export
0eqOpReflectsEquals : (m : Nat) -> (n : Nat) ->m==n=True->m=n
Totality: total
Visibility: export
tryLT : (k : Nat) ->Maybe0 (LTkn)
Totality: total
Visibility: export
tryLTE : (k : Nat) ->Maybe0 (LTEkn)
Totality: total
Visibility: export
tryNatToFin : Nat->Maybe (Fink)
  Tries to convert a natural number to a `Fin k`.

Totality: total
Visibility: export
tryFinToFin : Finn->Maybe (Fink)
  Tries to convert a `Fin n` to a `Fin k`.

Totality: total
Visibility: export
0minusLTE : (k : Nat) -> (m : Nat) ->LTE (minuskm) k
Totality: total
Visibility: export
0minusFinLT : (n : Nat) -> (x : Finn) ->LT (minusn (S (finToNatx))) n
Totality: total
Visibility: export
0minusLT : (x : Nat) -> (m : Nat) -> (n : Nat) ->LTx (minusnm) ->LT (x+m) n
Totality: total
Visibility: export
inc : Fin (minusnm) ->Finn
Totality: total
Visibility: export
0ltAddLeft : LTkn->LTk (m+n)
Totality: total
Visibility: export
0lteAddLeft : (n : Nat) ->LTEn (m+n)
Totality: total
Visibility: export
0plusMinus : (m : Nat) -> (n : Nat) ->LTEmn->m+minusnm=n
Totality: total
Visibility: export
0eqLTE : (m : Nat) -> (n : Nat) ->m=n->LTEmn
Totality: total
Visibility: export
0dropLemma : (k : Nat) -> (n : Nat) ->LTE (plus (minusn (minusnk)) (minusnk)) n
Totality: total
Visibility: export
0plusMinusLTE : (m : Nat) -> (n : Nat) ->LTEmn->LTE (m+minusnm) n
Totality: total
Visibility: export
trans : Ixkm->Ixmn->Ixkn
  `Suffix` is a reflexive and transitive relation.

Performance: This is integer addition at runtime.

Totality: total
Visibility: public export
0castBits8LT : (x : Bits8) ->LT (castx) 256
Totality: total
Visibility: export
bits8ToFin : Bits8->Fin256
  Every `Bits8` value can be safely cast to a `Fin 256`.

Totality: total
Visibility: export