| Safe Haskell | None |
|---|---|
| Language | GHC2021 |
Plutarch.Array
Description
Since: 1.12.0
Synopsis
- data PPullArray (a :: S -> Type) (s :: S)
- piota :: forall (s :: S). Term s PNatural -> Term s (PPullArray PNatural)
- pgenerate :: forall (a :: S -> Type) (s :: S). Term s PNatural -> Term s (PInteger :--> a) -> Term s (PPullArray a)
- pfromArray :: forall (a :: S -> Type) (s :: S). Term s (PArray a) -> Term s (PPullArray a)
- pfromList :: forall (a :: S -> Type) (s :: S). Term s (PBuiltinList a) -> Term s (PPullArray a)
- pmapArray :: forall (a :: S -> Type) (b :: S -> Type) (s :: S). Term s (a :--> b) -> Term s (PPullArray a) -> Term s (PPullArray b)
- pimapArray :: forall (a :: S -> Type) (b :: S -> Type) (s :: S). Term s (PInteger :--> (a :--> b)) -> Term s (PPullArray a) -> Term s (PPullArray b)
- ptakeArray :: forall (a :: S -> Type) (s :: S). Term s PNatural -> Term s (PPullArray a) -> Term s (PPullArray a)
- pdropArray :: forall (a :: S -> Type) (s :: S). Term s PNatural -> Term s (PPullArray a) -> Term s (PPullArray a)
- pzipWithArray :: forall (a :: S -> Type) (b :: S -> Type) (c :: S -> Type) (s :: S). Term s (a :--> (b :--> c)) -> Term s (PPullArray a) -> Term s (PPullArray b) -> Term s (PPullArray c)
- pizipWithArray :: forall (a :: S -> Type) (b :: S -> Type) (c :: S -> Type) (s :: S). Term s (PInteger :--> (a :--> (b :--> c))) -> Term s (PPullArray a) -> Term s (PPullArray b) -> Term s (PPullArray c)
- pfoldArray :: forall (a :: S -> Type) (b :: S -> Type) (s :: S). Term s (b :--> (a :--> b)) -> Term s b -> Term s (PPullArray a) -> Term s b
- prfoldArray :: forall (a :: S -> Type) (b :: S -> Type) (s :: S). Term s (b :--> (a :--> b)) -> Term s b -> Term s (PPullArray a) -> Term s b
- ppullArrayToList :: forall (a :: S -> Type) (s :: S). PlutusType (PBuiltinList a) => Term s (PPullArray a) -> Term s (PBuiltinList a)
- ppullArrayToSOPList :: forall (a :: S -> Type) (s :: S). Term s (PPullArray a) -> Term s (PList a)
Type
data PPullArray (a :: S -> Type) (s :: S) Source #
A pull array, represented as its Boehm-Berrarducci encoding. Put another way, a pull array is a function which can be 'materialized' to produce the elements of that array, in order.
Pull arrays give efficient linear transformations, in exchange for no ability to index them without evaluation. Pull arrays are best used when you need to perform a lot of transformations, with a fold or materialization at the end, as they fuse away all intermediate values. We achieve this by using Boehm-Berrarducci encodings, which means that every pull array is essentially a lambda onchain.
Since: 1.12.0
Instances
Functions
Introduction
piota :: forall (s :: S). Term s PNatural -> Term s (PPullArray PNatural) Source #
Given a length n, construct the pull array equivalent of [0, 1, ... n -
1].
\(Theta(1)\) space and time complexity.
Since: 1.12.0
pgenerate :: forall (a :: S -> Type) (s :: S). Term s PNatural -> Term s (PInteger :--> a) -> Term s (PPullArray a) Source #
Given a length and a function from indexes to values, construct the pull array of that length, each of whose indexes stores the value computed by that function.
\(Theta(1)\) space and time complexity.
Since: 1.12.0
pfromArray :: forall (a :: S -> Type) (s :: S). Term s (PArray a) -> Term s (PPullArray a) Source #
Given a builtin array, construct the equivalent pull array.
\(\Theta(1)\) space and time complexity.
Since: 1.12.0
pfromList :: forall (a :: S -> Type) (s :: S). Term s (PBuiltinList a) -> Term s (PPullArray a) Source #
Given a builtin list, construct the equivalent pull array. Uses
plistToArray internally.
\(\Theta(1)\) space complexity, \(\Theta(n)\) time complexity.
Since: 1.12.0
Transformation
Linear
pmapArray :: forall (a :: S -> Type) (b :: S -> Type) (s :: S). Term s (a :--> b) -> Term s (PPullArray a) -> Term s (PPullArray b) Source #
Given a 'transformation function' and a pull array, construct a new pull array where each element of the argument array has been transformed without moving it.
\(\Theta(1)\) space and time complexity.
Since: 1.12.0
pimapArray :: forall (a :: S -> Type) (b :: S -> Type) (s :: S). Term s (PInteger :--> (a :--> b)) -> Term s (PPullArray a) -> Term s (PPullArray b) Source #
As pmapArray, but with an index-aware 'transformer function'.
Since: 1.12.0
Affine
ptakeArray :: forall (a :: S -> Type) (s :: S). Term s PNatural -> Term s (PPullArray a) -> Term s (PPullArray a) Source #
Given a size limit \(k\) and a pull array of length \(n\), construct a new pull array that consists of the first \(\min \{k, n\}\) elements of the argument pull array, at the same indexes.
\(\Theta(1)\) space and time complexity.
Since: 1.12.0
pdropArray :: forall (a :: S -> Type) (s :: S). Term s PNatural -> Term s (PPullArray a) -> Term s (PPullArray a) Source #
Given a desired number of discarded elements \(k\) and a pull array of length (n), construct a new pull array that consists of all but the first (min {k, n}) elements of the argument pull array, with indexes appropriately offset.
\(\Theta(1)\) space and time complexity.
Since: 1.12.0
Combination
pzipWithArray :: forall (a :: S -> Type) (b :: S -> Type) (c :: S -> Type) (s :: S). Term s (a :--> (b :--> c)) -> Term s (PPullArray a) -> Term s (PPullArray b) -> Term s (PPullArray c) Source #
Given a 'combining function' and two pull arrays, produce a new pull array whose length is the minimum of the lengths of the arguments, and whose elements are applications of the 'combining function' at the respective indexes of the argument arrays.
\(\Theta(1)\) space and time complexity.
Since: 1.12.0
pizipWithArray :: forall (a :: S -> Type) (b :: S -> Type) (c :: S -> Type) (s :: S). Term s (PInteger :--> (a :--> (b :--> c))) -> Term s (PPullArray a) -> Term s (PPullArray b) -> Term s (PPullArray c) Source #
As pzipWithArray, but with an index-aware 'combining function'.
Since: 1.12.0
Elimination
Folds
pfoldArray :: forall (a :: S -> Type) (b :: S -> Type) (s :: S). Term s (b :--> (a :--> b)) -> Term s b -> Term s (PPullArray a) -> Term s b Source #
Given a 'combining function' and a starting value, reduce the argument array by repeatedly combining elements with the starting value. This is a left fold: thus, it will start at the lowest index and work its way upward.
Assuming \(\Theta(k)\) cost in space, and \(\Theta(\ell)\) cost in time, per application of the 'combining function', \(\Theta(kn)\) space complexity and \(\Theta(k\ell)\) time complexity.
Since: 1.13.0
prfoldArray :: forall (a :: S -> Type) (b :: S -> Type) (s :: S). Term s (b :--> (a :--> b)) -> Term s b -> Term s (PPullArray a) -> Term s b Source #
As pfoldArray, but from the highest index working downward.
Since: 1.13.0
Conversions
ppullArrayToList :: forall (a :: S -> Type) (s :: S). PlutusType (PBuiltinList a) => Term s (PPullArray a) -> Term s (PBuiltinList a) Source #
Convert a pull array to a builtin list. Prefer using this function to either kind of fold, as it is faster.
If you want to construct a builtin array instead, use this function
together with plistToArray.
\(\Theta(n)\) space and time complexity.
Since: 1.12.0