Safe Haskell | None |
---|---|
Language | Haskell98 |
Data.Vector.Fixed.Cont
Contents
Description
API for Church-encoded vectors. Implementation of function from Data.Vector.Fixed module uses these function internally in order to provide shortcut fusion.
- data S n
- data Z
- type family Add n m :: *
- class (ToNat a ~ b, ToPeano b ~ a) => NatIso a b
- type family ToPeano (b :: Nat) :: * where ...
- type family ToNat (a :: *) :: Nat where ...
- type N1 = S Z
- type N2 = S N1
- type N3 = S N2
- type N4 = S N3
- type N5 = S N4
- type N6 = S N5
- type family Fn n a b
- newtype Fun n a b = Fun {}
- class Arity n where
- apply :: Arity n => (forall k. t (S k) -> (a, t k)) -> t n -> ContVec n a
- applyM :: (Monad m, Arity n) => (forall k. t (S k) -> m (a, t k)) -> t n -> m (ContVec n a)
- constFun :: Fun n a b -> Fun (S n) a b
- curryFirst :: Fun (S n) a b -> a -> Fun n a b
- uncurryFirst :: (a -> Fun n a b) -> Fun (S n) a b
- curryLast :: Arity n => Fun (S n) a b -> Fun n a (a -> b)
- curryMany :: forall n k a b. Arity n => Fun (Add n k) a b -> Fun n a (Fun k a b)
- apLast :: Arity n => Fun (S n) a b -> a -> Fun n a b
- shuffleFun :: Arity n => (b -> Fun n a r) -> Fun n a (b -> r)
- withFun :: (Fun n a b -> Fun n a b) -> Fun (S n) a b -> Fun (S n) a b
- type family Dim (v :: * -> *)
- class Arity (Dim v) => Vector v a where
- class (Vector (v n) a, Dim (v n) ~ n) => VectorN v n a
- length :: forall v a. Arity (Dim v) => v a -> Int
- class Index k n where
- newtype ContVec n a = ContVec (forall r. Fun n a r -> r)
- cvec :: (Vector v a, Dim v ~ n) => v a -> ContVec n a
- fromList :: Arity n => [a] -> ContVec n a
- fromList' :: forall n a. Arity n => [a] -> ContVec n a
- fromListM :: forall n a. Arity n => [a] -> Maybe (ContVec n a)
- toList :: Arity n => ContVec n a -> [a]
- replicate :: Arity n => a -> ContVec n a
- replicateM :: (Arity n, Monad m) => m a -> m (ContVec n a)
- generate :: Arity n => (Int -> a) -> ContVec n a
- generateM :: (Monad m, Arity n) => (Int -> m a) -> m (ContVec n a)
- unfoldr :: Arity n => (b -> (a, b)) -> b -> ContVec n a
- basis :: (Num a, Arity n) => Int -> ContVec n a
- empty :: ContVec Z a
- cons :: a -> ContVec n a -> ContVec (S n) a
- consV :: ContVec (S Z) a -> ContVec n a -> ContVec (S n) a
- snoc :: Arity n => a -> ContVec n a -> ContVec (S n) a
- concat :: (Arity n, Arity k, Arity (Add n k)) => ContVec n a -> ContVec k a -> ContVec (Add n k) a
- mk1 :: a -> ContVec N1 a
- mk2 :: a -> a -> ContVec N2 a
- mk3 :: a -> a -> a -> ContVec N3 a
- mk4 :: a -> a -> a -> a -> ContVec N4 a
- mk5 :: a -> a -> a -> a -> a -> ContVec N5 a
- map :: Arity n => (a -> b) -> ContVec n a -> ContVec n b
- imap :: Arity n => (Int -> a -> b) -> ContVec n a -> ContVec n b
- mapM :: (Arity n, Monad m) => (a -> m b) -> ContVec n a -> m (ContVec n b)
- imapM :: (Arity n, Monad m) => (Int -> a -> m b) -> ContVec n a -> m (ContVec n b)
- mapM_ :: (Arity n, Monad m) => (a -> m b) -> ContVec n a -> m ()
- imapM_ :: (Arity n, Monad m) => (Int -> a -> m b) -> ContVec n a -> m ()
- scanl :: Arity n => (b -> a -> b) -> b -> ContVec n a -> ContVec (S n) b
- scanl1 :: Arity n => (a -> a -> a) -> ContVec n a -> ContVec n a
- sequence :: (Arity n, Monad m) => ContVec n (m a) -> m (ContVec n a)
- sequence_ :: (Arity n, Monad m) => ContVec n (m a) -> m ()
- distribute :: (Functor f, Arity n) => f (ContVec n a) -> ContVec n (f a)
- collect :: (Functor f, Arity n) => (a -> ContVec n b) -> f a -> ContVec n (f b)
- distributeM :: (Monad m, Arity n) => m (ContVec n a) -> ContVec n (m a)
- collectM :: (Monad m, Arity n) => (a -> ContVec n b) -> m a -> ContVec n (m b)
- tail :: ContVec (S n) a -> ContVec n a
- reverse :: Arity n => ContVec n a -> ContVec n a
- zipWith :: Arity n => (a -> b -> c) -> ContVec n a -> ContVec n b -> ContVec n c
- zipWith3 :: Arity n => (a -> b -> c -> d) -> ContVec n a -> ContVec n b -> ContVec n c -> ContVec n d
- izipWith :: Arity n => (Int -> a -> b -> c) -> ContVec n a -> ContVec n b -> ContVec n c
- izipWith3 :: Arity n => (Int -> a -> b -> c -> d) -> ContVec n a -> ContVec n b -> ContVec n c -> ContVec n d
- zipWithM :: (Arity n, Monad m) => (a -> b -> m c) -> ContVec n a -> ContVec n b -> m (ContVec n c)
- zipWithM_ :: (Arity n, Monad m) => (a -> b -> m c) -> ContVec n a -> ContVec n b -> m ()
- izipWithM :: (Arity n, Monad m) => (Int -> a -> b -> m c) -> ContVec n a -> ContVec n b -> m (ContVec n c)
- izipWithM_ :: (Arity n, Monad m) => (Int -> a -> b -> m c) -> ContVec n a -> ContVec n b -> m ()
- runContVec :: Fun n a r -> ContVec n a -> r
- head :: Arity (S n) => ContVec (S n) a -> a
- index :: Arity n => Int -> ContVec n a -> a
- element :: (Arity n, Functor f) => Int -> (a -> f a) -> ContVec n a -> f (ContVec n a)
- elementTy :: (Arity n, Index k n, Functor f) => k -> (a -> f a) -> ContVec n a -> f (ContVec n a)
- vector :: (Vector v a, Dim v ~ n) => ContVec n a -> v a
- foldl :: Arity n => (b -> a -> b) -> b -> ContVec n a -> b
- foldl1 :: Arity (S n) => (a -> a -> a) -> ContVec (S n) a -> a
- foldr :: Arity n => (a -> b -> b) -> b -> ContVec n a -> b
- ifoldl :: Arity n => (b -> Int -> a -> b) -> b -> ContVec n a -> b
- ifoldr :: Arity n => (Int -> a -> b -> b) -> b -> ContVec n a -> b
- foldM :: (Arity n, Monad m) => (b -> a -> m b) -> b -> ContVec n a -> m b
- ifoldM :: (Arity n, Monad m) => (b -> Int -> a -> m b) -> b -> ContVec n a -> m b
- sum :: (Num a, Arity n) => ContVec n a -> a
- minimum :: (Ord a, Arity (S n)) => ContVec (S n) a -> a
- maximum :: (Ord a, Arity (S n)) => ContVec (S n) a -> a
- and :: Arity n => ContVec n Bool -> Bool
- or :: Arity n => ContVec n Bool -> Bool
- all :: Arity n => (a -> Bool) -> ContVec n a -> Bool
- any :: Arity n => (a -> Bool) -> ContVec n a -> Bool
- find :: Arity n => (a -> Bool) -> ContVec n a -> Maybe a
- gfoldl :: forall c v a. (Vector v a, Data a) => (forall x y. Data x => c (x -> y) -> x -> c y) -> (forall x. x -> c x) -> v a -> c (v a)
- gunfold :: forall con c v a. (Vector v a, Data a) => (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> con -> c (v a)
Type-level numbers
Successor of n
Type level zero
Type family for sum of unary natural numbers.
Isomorphism between Peano number and Nats
It's only become possible to define isomorphism between Peano
number and built-in Nat
number in GHC 7.8. It's however
impossible to define their properties inductively. So Peano number
are used everywhere.
class (ToNat a ~ b, ToPeano b ~ a) => NatIso a b #
Isomorphism between two representations of natural numbers
Synonyms for small numerals
N-ary functions
Newtype wrapper which is used to make Fn
injective. It's also a
reader monad.
Type class for handling n-ary functions.
Methods
accum :: (forall k. t (S k) -> a -> t k) -> (t Z -> b) -> t n -> Fun n a b #
Left fold over n elements exposed as n-ary function. These elements are supplied as arguments to the function.
applyFun :: (forall k. t (S k) -> (a, t k)) -> t n -> Fn n a b -> (b, t Z) #
Apply all parameters to the function.
applyFunM :: Monad m => (forall k. t (S k) -> m (a, t k)) -> t n -> m (ContVec n a, t Z) #
Apply all parameters to the function using monadic actions. Note that for identity monad it's same as applyFun. Ignoring newtypes:
forall b. Fn n a b -> b ~ ContVecn n a
Arity of function.
reverseF :: Fun n a b -> Fun n a b #
Reverse order of parameters.
uncurryMany :: Fun (Add n k) a b -> Fun n a (Fun k a b) #
Uncurry n first parameters of n-ary function
gunfoldF :: Data a => (forall b x. Data b => c (b -> x) -> c x) -> T_gunfold c r a n -> c r #
Worker function for gunfold
Arguments
:: Arity n | |
=> (forall k. t (S k) -> (a, t k)) | Get value to apply to function |
-> t n | Initial value |
-> ContVec n a | N-ary function |
Apply all parameters to the function.
Arguments
:: (Monad m, Arity n) | |
=> (forall k. t (S k) -> m (a, t k)) | Get value to apply to function |
-> t n | Initial value |
-> m (ContVec n a) |
Apply all parameters to the function using monadic actions.
Combinators
curryFirst :: Fun (S n) a b -> a -> Fun n a b #
Curry first parameter of n-ary function
uncurryFirst :: (a -> Fun n a b) -> Fun (S n) a b #
Uncurry first parameter of n-ary function
curryMany :: forall n k a b. Arity n => Fun (Add n k) a b -> Fun n a (Fun k a b) #
Curry n first parameters of n-ary function
apLast :: Arity n => Fun (S n) a b -> a -> Fun n a b #
Apply last parameter to function. Unlike apFun
we need to
traverse all parameters but last hence Arity
constraint.
shuffleFun :: Arity n => (b -> Fun n a r) -> Fun n a (b -> r) #
Move function parameter to the result of N-ary function.
withFun :: (Fun n a b -> Fun n a b) -> Fun (S n) a b -> Fun (S n) a b #
Recursive step for the function
Vector type class
type family Dim (v :: * -> *) #
Size of vector expressed as type-level natural.
Instances
type Dim Complex # | |
type Dim Empty # | |
type Dim Only # | |
type Dim ((,) a) # | |
type Dim (Proxy *) # | |
type Dim (ContVec n) # | |
type Dim (VecList n) # | |
type Dim (Vec n) # | |
type Dim (Vec n) # | |
type Dim (Vec n) # | |
type Dim (Vec n) # | |
type Dim ((,,) a b) # | |
type Dim ((,,,) a b c) # | |
type Dim ((,,,,) a b c d) # | |
type Dim ((,,,,,) a b c d e) # | |
type Dim ((,,,,,,) a b c d e f) # | |
class Arity (Dim v) => Vector v a where #
Type class for vectors with fixed length. Instance should provide two functions: one to create vector and another for vector deconstruction. They must obey following law:
inspect v construct = v
Methods
construct :: Fun (Dim v) a (v a) #
N-ary function for creation of vectors.
inspect :: v a -> Fun (Dim v) a b -> b #
Deconstruction of vector.
basicIndex :: v a -> Int -> a #
Optional more efficient implementation of indexing. Shouldn't
be used directly, use !
instead.
Instances
RealFloat a => Vector Complex a # | |
Vector Empty a # | |
Vector Only a # | |
(~) * b a => Vector ((,) b) a # | Note this instance (and other instances for tuples) is
essentially monomorphic in element type. Vector type v of 2
element tuple |
Vector (Proxy *) a # | |
Arity n => Vector (ContVec n) a # | |
Arity n => Vector (VecList n) a # | |
Arity n => Vector (Vec n) a # | |
(Arity n, Prim a) => Vector (Vec n) a # | |
(Arity n, Storable a) => Vector (Vec n) a # | |
Unbox n a => Vector (Vec n) a # | |
((~) * b a, (~) * c a) => Vector ((,,) b c) a # | |
((~) * b a, (~) * c a, (~) * d a) => Vector ((,,,) b c d) a # | |
((~) * b a, (~) * c a, (~) * d a, (~) * e a) => Vector ((,,,,) b c d e) a # | |
((~) * b a, (~) * c a, (~) * d a, (~) * e a, (~) * f a) => Vector ((,,,,,) b c d e f) a # | |
((~) * b a, (~) * c a, (~) * d a, (~) * e a, (~) * f a, (~) * g a) => Vector ((,,,,,,) b c d e f g) a # | |
class (Vector (v n) a, Dim (v n) ~ n) => VectorN v n a #
Vector parametrized by length. In ideal world it should be:
forall n. (Arity n, Vector (v n) a, Dim (v n) ~ n) => VectorN v a
Alas polymorphic constraints aren't allowed in haskell.
length :: forall v a. Arity (Dim v) => v a -> Int #
Length of vector. Function doesn't evaluate its argument.
Type class for indexing of vector when index value is known at compile time.
Vector as continuation
Vector represented as continuation. Alternative wording: it's Church encoded N-element vector.
Construction of ContVec
cvec :: (Vector v a, Dim v ~ n) => v a -> ContVec n a #
Convert regular vector to continuation based one.
fromList :: Arity n => [a] -> ContVec n a #
Convert list to continuation-based vector. Will throw error if list is shorter than resulting vector.
fromList' :: forall n a. Arity n => [a] -> ContVec n a #
Same as fromList
bu throws error is list doesn't have same
length as vector.
fromListM :: forall n a. Arity n => [a] -> Maybe (ContVec n a) #
Convert list to continuation-based vector. Will fail with
Nothing
if list doesn't have right length.
replicate :: Arity n => a -> ContVec n a #
Execute monadic action for every element of vector. Synonym for pure
.
replicateM :: (Arity n, Monad m) => m a -> m (ContVec n a) #
Execute monadic action for every element of vector.
generate :: Arity n => (Int -> a) -> ContVec n a #
Generate vector from function which maps element's index to its value.
generateM :: (Monad m, Arity n) => (Int -> m a) -> m (ContVec n a) #
Generate vector from monadic function which maps element's index to its value.
Constructors
consV :: ContVec (S Z) a -> ContVec n a -> ContVec (S n) a #
Prepend single element vector to another vector.
concat :: (Arity n, Arity k, Arity (Add n k)) => ContVec n a -> ContVec k a -> ContVec (Add n k) a #
Concatenate vector
Transformations
imap :: Arity n => (Int -> a -> b) -> ContVec n a -> ContVec n b #
Apply function to every element of the vector and its index.
mapM :: (Arity n, Monad m) => (a -> m b) -> ContVec n a -> m (ContVec n b) #
Monadic map over vector.
imapM :: (Arity n, Monad m) => (Int -> a -> m b) -> ContVec n a -> m (ContVec n b) #
Apply monadic function to every element of the vector and its index.
mapM_ :: (Arity n, Monad m) => (a -> m b) -> ContVec n a -> m () #
Apply monadic action to each element of vector and ignore result.
imapM_ :: (Arity n, Monad m) => (Int -> a -> m b) -> ContVec n a -> m () #
Apply monadic action to each element of vector and its index and ignore result.
sequence :: (Arity n, Monad m) => ContVec n (m a) -> m (ContVec n a) #
Evaluate every action in the vector from left to right.
sequence_ :: (Arity n, Monad m) => ContVec n (m a) -> m () #
Evaluate every action in the vector from left to right and ignore result.
Zips
zipWith :: Arity n => (a -> b -> c) -> ContVec n a -> ContVec n b -> ContVec n c #
Zip two vector together using function.
zipWith3 :: Arity n => (a -> b -> c -> d) -> ContVec n a -> ContVec n b -> ContVec n c -> ContVec n d #
Zip three vectors together
izipWith :: Arity n => (Int -> a -> b -> c) -> ContVec n a -> ContVec n b -> ContVec n c #
Zip two vector together using function which takes element index as well.
izipWith3 :: Arity n => (Int -> a -> b -> c -> d) -> ContVec n a -> ContVec n b -> ContVec n c -> ContVec n d #
Zip three vectors together
zipWithM :: (Arity n, Monad m) => (a -> b -> m c) -> ContVec n a -> ContVec n b -> m (ContVec n c) #
Zip two vector together using monadic function.
izipWithM :: (Arity n, Monad m) => (Int -> a -> b -> m c) -> ContVec n a -> ContVec n b -> m (ContVec n c) #
Zip two vector together using monadic function which takes element index as well..
Running ContVec
runContVec :: Fun n a r -> ContVec n a -> r #
Run continuation vector. It's same as inspect
but with
arguments flipped.
Getters
element :: (Arity n, Functor f) => Int -> (a -> f a) -> ContVec n a -> f (ContVec n a) #
Twan van Laarhoven lens for continuation based vector
elementTy :: (Arity n, Index k n, Functor f) => k -> (a -> f a) -> ContVec n a -> f (ContVec n a) #
Twan van Laarhoven's lens for element of vector with statically known index.
Vector construction
Folds
ifoldl :: Arity n => (b -> Int -> a -> b) -> b -> ContVec n a -> b #
Left fold over continuation vector.
ifoldr :: Arity n => (Int -> a -> b -> b) -> b -> ContVec n a -> b #
Right fold over continuation vector
foldM :: (Arity n, Monad m) => (b -> a -> m b) -> b -> ContVec n a -> m b #
Monadic left fold over continuation vector.
ifoldM :: (Arity n, Monad m) => (b -> Int -> a -> m b) -> b -> ContVec n a -> m b #
Monadic left fold over continuation vector.
Special folds
all :: Arity n => (a -> Bool) -> ContVec n a -> Bool #
Determines whether all elements of vector satisfy predicate.
any :: Arity n => (a -> Bool) -> ContVec n a -> Bool #
Determines whether any of element of vector satisfy predicate.