Safe Haskell | Safe-Inferred |
---|---|
Language | GHC2021 |
Scott-encoded lists and ListLike typeclass
Synopsis
- data PList (a :: S -> Type) (s :: S)
- class PListLike (list :: (S -> Type) -> S -> Type) where
- type PElemConstraint list (a :: S -> Type) :: Constraint
- pelimList :: PElemConstraint list a => (Term s a -> Term s (list a) -> Term s r) -> Term s r -> Term s (list a) -> Term s r
- pcons :: PElemConstraint list a => Term s (a :--> (list a :--> list a))
- pnil :: PElemConstraint list a => Term s (list a)
- phead :: PElemConstraint list a => Term s (list a :--> a)
- ptail :: PElemConstraint list a => Term s (list a :--> list a)
- pnull :: PElemConstraint list a => Term s (list a :--> PBool)
- type PIsListLike list a = (PListLike list, PElemConstraint list a)
- pconvertLists :: forall f g a s. (PIsListLike f a, PIsListLike g a) => Term s (f a :--> g a)
- pshowList :: forall list a s. (PShow a, PIsListLike list a) => Term s (list a :--> PString)
- plistEquals :: (PIsListLike list a, PEq a) => Term s (list a :--> (list a :--> PBool))
- pelem :: (PIsListLike list a, PEq a) => Term s (a :--> (list a :--> PBool))
- plength :: PIsListLike list a => Term s (list a :--> PInteger)
- ptryIndex :: PIsListLike list a => Natural -> Term s (list a) -> Term s a
- pdrop :: PIsListLike list a => Natural -> Term s (list a) -> Term s (list a)
- pfind :: PIsListLike l a => Term s ((a :--> PBool) :--> (l a :--> PMaybe a))
- pelemAt :: PIsListLike l a => Term s (PInteger :--> (l a :--> a))
- (#!!) :: PIsListLike l a => Term s (l a) -> Term s PInteger -> Term s a
- psingleton :: PIsListLike list a => Term s (a :--> list a)
- puncons :: PIsListLike list a => Term s (list a :--> PMaybe (PPair a (list a)))
- ptryUncons :: PIsListLike list a => Term s (list a :--> PPair a (list a))
- pconcat :: PIsListLike list a => Term s (list a :--> (list a :--> list a))
- pzipWith :: (PListLike list, PElemConstraint list a, PElemConstraint list b, PElemConstraint list c) => Term s ((a :--> (b :--> c)) :--> (list a :--> (list b :--> list c)))
- pzipWith' :: (PListLike list, PElemConstraint list a, PElemConstraint list b, PElemConstraint list c) => (Term s a -> Term s b -> Term s c) -> Term s (list a :--> (list b :--> list c))
- pzip :: (PListLike list, PElemConstraint list a, PElemConstraint list b, PElemConstraint list (PPair a b)) => Term s (list a :--> (list b :--> list (PPair a b)))
- pmap :: (PListLike list, PElemConstraint list a, PElemConstraint list b) => Term s ((a :--> b) :--> (list a :--> list b))
- pfilter :: PIsListLike list a => Term s ((a :--> PBool) :--> (list a :--> list a))
- precList :: PIsListLike list a => (Term s (list a :--> r) -> Term s a -> Term s (list a) -> Term s r) -> (Term s (list a :--> r) -> Term s r) -> Term s (list a :--> r)
- pfoldr :: PIsListLike list a => Term s ((a :--> (b :--> b)) :--> (b :--> (list a :--> b)))
- pfoldr' :: PIsListLike list a => (forall s. Term s a -> Term s b -> Term s b) -> Term s (b :--> (list a :--> b))
- pfoldrLazy :: PIsListLike list a => Term s ((a :--> (PDelayed b :--> b)) :--> (b :--> (list a :--> b)))
- pfoldl :: PIsListLike list a => Term s ((b :--> (a :--> b)) :--> (b :--> (list a :--> b)))
- pfoldl' :: PIsListLike list a => (forall s. Term s b -> Term s a -> Term s b) -> Term s (b :--> (list a :--> b))
- pall :: PIsListLike list a => Term s ((a :--> PBool) :--> (list a :--> PBool))
- pany :: PIsListLike list a => Term s ((a :--> PBool) :--> (list a :--> PBool))
- preverse :: forall (l :: (S -> Type) -> S -> Type) (a :: S -> Type) (s :: S). PIsListLike l a => Term s (l a :--> l a)
- pcheckSorted :: forall (l :: (S -> Type) -> S -> Type) (a :: S -> Type) (s :: S). (PIsListLike l a, POrd a) => Term s (l a :--> PBool)
Documentation
data PList (a :: S -> Type) (s :: S) Source #
Instances
class PListLike (list :: (S -> Type) -> S -> Type) where Source #
Plutarch types that behave like lists.
type PElemConstraint list (a :: S -> Type) :: Constraint Source #
pelimList :: PElemConstraint list a => (Term s a -> Term s (list a) -> Term s r) -> Term s r -> Term s (list a) -> Term s r Source #
Canonical eliminator for list-likes.
pcons :: PElemConstraint list a => Term s (a :--> (list a :--> list a)) Source #
Cons an element onto an existing list.
pnil :: PElemConstraint list a => Term s (list a) Source #
The empty list
phead :: PElemConstraint list a => Term s (list a :--> a) Source #
Return the first element of a list. Partial, throws an error upon encountering an empty list.
ptail :: PElemConstraint list a => Term s (list a :--> list a) Source #
Take the tail of a list, meaning drop its head. Partial, throws an error upon encountering an empty list.
pnull :: PElemConstraint list a => Term s (list a :--> PBool) Source #
O(1) . Check if a list is empty
Instances
type PIsListLike list a = (PListLike list, PElemConstraint list a) Source #
'PIsListLike list a' constraints list
be a PListLike
with valid element type, a
.
pconvertLists :: forall f g a s. (PIsListLike f a, PIsListLike g a) => Term s (f a :--> g a) Source #
O(n) . Convert from any ListLike to any ListLike, provided both lists' element constraints are met.
pshowList :: forall list a s. (PShow a, PIsListLike list a) => Term s (list a :--> PString) Source #
Comparison
plistEquals :: (PIsListLike list a, PEq a) => Term s (list a :--> (list a :--> PBool)) Source #
O(min(n, m)) . Check if two lists are equal.
Query
pelem :: (PIsListLike list a, PEq a) => Term s (a :--> (list a :--> PBool)) Source #
O(n) . Check if element is in the list
plength :: PIsListLike list a => Term s (list a :--> PInteger) Source #
O(n) . Count the number of elements in the list
ptryIndex :: PIsListLike list a => Natural -> Term s (list a) -> Term s a Source #
Index a BuiltinList, throwing an error if the index is out of bounds.
pdrop :: PIsListLike list a => Natural -> Term s (list a) -> Term s (list a) Source #
Drop the first n fields of a List.
The term will be statically generated as
repeated applications of ptail
, which will be more
efficient in many circumstances.
pfind :: PIsListLike l a => Term s ((a :--> PBool) :--> (l a :--> PMaybe a)) Source #
O(n) . like haskell level find
but on plutarch level
pelemAt :: PIsListLike l a => Term s (PInteger :--> (l a :--> a)) Source #
O(n) . Like Haskell level (!!)
but on the Plutarch level, not infix and
with arguments reversed, errors if the specified index is greater than or equal
to the lists length
(#!!) :: PIsListLike l a => Term s (l a) -> Term s PInteger -> Term s a Source #
O(n) . Like Haskell level (!!)
but on the plutarch level
Construction
psingleton :: PIsListLike list a => Term s (a :--> list a) Source #
O(1) . Create a singleton list from an element
Deconstruction
puncons :: PIsListLike list a => Term s (list a :--> PMaybe (PPair a (list a))) Source #
Extract head and tail of the list, if list is not empty.
ptryUncons :: PIsListLike list a => Term s (list a :--> PPair a (list a)) Source #
Extract head and tail of the list, throws error if list is empty.
Combine
pconcat :: PIsListLike list a => Term s (list a :--> (list a :--> list a)) Source #
O(n) . Concatenate two lists
Example: > pconcat # psingleton x # psingleton y == plistLiteral [x, y]
pconcat exhibits identities with empty lists such that > forall x. pconcat # pnil # x == x > forall x. pconcat # x # pnil == x
pzipWith :: (PListLike list, PElemConstraint list a, PElemConstraint list b, PElemConstraint list c) => Term s ((a :--> (b :--> c)) :--> (list a :--> (list b :--> list c))) Source #
O(min(n, m)) . Zip two lists together with a passed function.
If the lists are of differing lengths, cut to the shortest.
pzipWith' :: (PListLike list, PElemConstraint list a, PElemConstraint list b, PElemConstraint list c) => (Term s a -> Term s b -> Term s c) -> Term s (list a :--> (list b :--> list c)) Source #
Like pzipWith
but with Haskell-level merge function.
pzip :: (PListLike list, PElemConstraint list a, PElemConstraint list b, PElemConstraint list (PPair a b)) => Term s (list a :--> (list b :--> list (PPair a b))) Source #
O(min(n, m)) . Zip two lists together, creating pairs of the elements.
If the lists are of differing lengths, cut to the shortest.
Traversals
pmap :: (PListLike list, PElemConstraint list a, PElemConstraint list b) => Term s ((a :--> b) :--> (list a :--> list b)) Source #
O(n) . Map a function over a list of elements
pfilter :: PIsListLike list a => Term s ((a :--> PBool) :--> (list a :--> list a)) Source #
O(n) . Filter elements from a list that don't match the predicate.
Catamorphisms
precList :: PIsListLike list a => (Term s (list a :--> r) -> Term s a -> Term s (list a) -> Term s r) -> (Term s (list a :--> r) -> Term s r) -> Term s (list a :--> r) Source #
Like pelimList
, but with a fixpoint recursion hatch.
pfoldr :: PIsListLike list a => Term s ((a :--> (b :--> b)) :--> (b :--> (list a :--> b))) Source #
O(n) . Fold on a list right-associatively.
pfoldr' :: PIsListLike list a => (forall s. Term s a -> Term s b -> Term s b) -> Term s (b :--> (list a :--> b)) Source #
The same as pfoldr'
, but with Haskell-level reduction function.
pfoldrLazy :: PIsListLike list a => Term s ((a :--> (PDelayed b :--> b)) :--> (b :--> (list a :--> b))) Source #
O(n) . Fold on a list right-associatively, with opportunity for short circuting.
May short circuit when given reducer function is lazy in its second argument.
pfoldl :: PIsListLike list a => Term s ((b :--> (a :--> b)) :--> (b :--> (list a :--> b))) Source #
O(n) . Fold on a list left-associatively.
pfoldl' :: PIsListLike list a => (forall s. Term s b -> Term s a -> Term s b) -> Term s (b :--> (list a :--> b)) Source #
The same as pfoldl
, but with Haskell-level reduction function.
Special Folds
pall :: PIsListLike list a => Term s ((a :--> PBool) :--> (list a :--> PBool)) Source #
O(n) . Check that predicate holds for all elements in a list.
pany :: PIsListLike list a => Term s ((a :--> PBool) :--> (list a :--> PBool)) Source #
O(n) . Check that predicate holds for any element in a list.
Modification
preverse :: forall (l :: (S -> Type) -> S -> Type) (a :: S -> Type) (s :: S). PIsListLike l a => Term s (l a :--> l a) Source #
O(n) . Reverse a list-like structure.
@since WIP