Safe Haskell | None |
---|---|
Language | Haskell2010 |
Data.Vector.Fixed
Description
Generic API for vectors with fixed length.
For encoding of vector size library uses Peano naturals defined in the library. At come point in the future it would make sense to switch to new GHC type level numerals.
Common pitfalls
Library provide instances for tuples. But there's a catch. Tuples
are monomorphic in element type. Let consider 2-tuple (Int,Int)
.
Vector type v
is (,) Int
and only allowed element type is
Int
. Because of that we cannot change element type and following
code will fail:
>>> map (== 1) ((1,2) :: (Int,Int)) <interactive>:3:1: Couldn't match type `Int' with `Bool' In the expression: F.map (== 1) ((1, 2) :: (Int, Int)) In an equation for `it': it = map (== 1) ((1, 2) :: (Int, Int))
To make it work we need to change vector type as well. Functions from module Data.Vector.Fixed.Generic provide this functionality.
>>> map (== 1) ((1,2) :: (Int,Int)) :: (Bool,Bool) (True,False)
Synopsis
- type family Dim (v :: Type -> Type) :: Nat
- class Arity (Dim v) => Vector (v :: Type -> Type) a where
- class (Vector (v n) a, Dim (v n) ~ n) => VectorN (v :: Nat -> Type -> Type) (n :: Nat) a
- type Arity (n :: Nat) = (ArityPeano (Peano n), KnownNat n, Peano (n + 1) ~ 'S (Peano n))
- newtype Fun (n :: PeanoNum) a b = Fun {}
- length :: KnownNat (Dim v) => v a -> Int
- mk0 :: (Vector v a, Dim v ~ 0) => v a
- mk1 :: (Vector v a, Dim v ~ 1) => a -> v a
- mk2 :: (Vector v a, Dim v ~ 2) => a -> a -> v a
- mk3 :: (Vector v a, Dim v ~ 3) => a -> a -> a -> v a
- mk4 :: (Vector v a, Dim v ~ 4) => a -> a -> a -> a -> v a
- mk5 :: (Vector v a, Dim v ~ 5) => a -> a -> a -> a -> a -> v a
- mk6 :: (Vector v a, Dim v ~ 6) => a -> a -> a -> a -> a -> a -> v a
- mk7 :: (Vector v a, Dim v ~ 7) => a -> a -> a -> a -> a -> a -> a -> v a
- mk8 :: (Vector v a, Dim v ~ 8) => a -> a -> a -> a -> a -> a -> a -> a -> v a
- mkN :: forall proxy v a. Vector v a => proxy (v a) -> Fn (Peano (Dim v)) a (v a)
- pattern V1 :: (Vector v a, Dim v ~ 1) => a -> v a
- pattern V2 :: (Vector v a, Dim v ~ 2) => a -> a -> v a
- pattern V3 :: (Vector v a, Dim v ~ 3) => a -> a -> a -> v a
- pattern V4 :: (Vector v a, Dim v ~ 4) => a -> a -> a -> a -> v a
- data ContVec (n :: Nat) a
- empty :: ContVec 0 a
- vector :: forall v a (n :: Nat). (Vector v a, Dim v ~ n) => ContVec n a -> v a
- cvec :: forall v a (n :: Nat). (Vector v a, Dim v ~ n) => v a -> ContVec n a
- replicate :: Vector v a => a -> v a
- replicateM :: (Vector v a, Applicative f) => f a -> f (v a)
- generate :: Vector v a => (Int -> a) -> v a
- generateM :: (Applicative f, Vector v a) => (Int -> f a) -> f (v a)
- unfoldr :: Vector v a => (b -> (a, b)) -> b -> v a
- basis :: (Vector v a, Num a) => Int -> v a
- head :: (Vector v a, 1 <= Dim v) => v a -> a
- tail :: (Vector v a, Vector w a, Dim v ~ (Dim w + 1)) => v a -> w a
- cons :: (Vector v a, Vector w a, Dim w ~ (Dim v + 1)) => a -> v a -> w a
- snoc :: (Vector v a, Vector w a, Dim w ~ (Dim v + 1)) => a -> v a -> w a
- concat :: (Vector v a, Vector u a, Vector w a, (Dim v + Dim u) ~ Dim w, Peano (Dim v + Dim u) ~ Add (Peano (Dim v)) (Peano (Dim u))) => v a -> u a -> w a
- reverse :: Vector v a => v a -> v a
- (!) :: Vector v a => v a -> Int -> a
- index :: forall v a (k :: Nat) proxy. (Vector v a, KnownNat k, (k + 1) <= Dim v) => v a -> proxy k -> a
- set :: forall v a (k :: Nat) proxy. (Vector v a, KnownNat k, (k + 1) <= Dim v) => proxy k -> a -> v a -> v a
- element :: (Vector v a, Functor f) => Int -> (a -> f a) -> v a -> f (v a)
- elementTy :: forall v a (k :: Nat) f proxy. (Vector v a, KnownNat k, (k + 1) <= Dim v, Functor f) => proxy k -> (a -> f a) -> v a -> f (v a)
- eq :: (Vector v a, Eq a) => v a -> v a -> Bool
- ord :: (Vector v a, Ord a) => v a -> v a -> Ordering
- map :: (Vector v a, Vector v b) => (a -> b) -> v a -> v b
- mapM :: (Vector v a, Vector v b, Applicative f) => (a -> f b) -> v a -> f (v b)
- mapM_ :: (Vector v a, Applicative f) => (a -> f b) -> v a -> f ()
- imap :: (Vector v a, Vector v b) => (Int -> a -> b) -> v a -> v b
- imapM :: (Vector v a, Vector v b, Applicative f) => (Int -> a -> f b) -> v a -> f (v b)
- imapM_ :: (Vector v a, Applicative f) => (Int -> a -> f b) -> v a -> f ()
- scanl :: (Vector v a, Vector w b, Dim w ~ (Dim v + 1)) => (b -> a -> b) -> b -> v a -> w b
- scanl1 :: Vector v a => (a -> a -> a) -> v a -> v a
- sequence :: (Vector v a, Vector v (f a), Applicative f) => v (f a) -> f (v a)
- sequence_ :: (Vector v (f a), Applicative f) => v (f a) -> f ()
- sequenceA :: (Vector v a, Vector v (f a), Applicative f) => v (f a) -> f (v a)
- traverse :: (Vector v a, Vector v b, Applicative f) => (a -> f b) -> v a -> f (v b)
- distribute :: (Vector v a, Vector v (f a), Functor f) => f (v a) -> v (f a)
- collect :: (Vector v a, Vector v b, Vector v (f b), Functor f) => (a -> v b) -> f a -> v (f b)
- foldl :: Vector v a => (b -> a -> b) -> b -> v a -> b
- foldr :: Vector v a => (a -> b -> b) -> b -> v a -> b
- foldl1 :: (Vector v a, 1 <= Dim v) => (a -> a -> a) -> v a -> a
- fold :: (Vector v m, Monoid m) => v m -> m
- foldMap :: (Vector v a, Monoid m) => (a -> m) -> v a -> m
- ifoldl :: Vector v a => (b -> Int -> a -> b) -> b -> v a -> b
- ifoldr :: Vector v a => (Int -> a -> b -> b) -> b -> v a -> b
- foldM :: (Vector v a, Monad m) => (b -> a -> m b) -> b -> v a -> m b
- ifoldM :: (Vector v a, Monad m) => (b -> Int -> a -> m b) -> b -> v a -> m b
- sum :: (Vector v a, Num a) => v a -> a
- maximum :: (Vector v a, 1 <= Dim v, Ord a) => v a -> a
- minimum :: (Vector v a, 1 <= Dim v, Ord a) => v a -> a
- and :: Vector v Bool => v Bool -> Bool
- or :: Vector v Bool => v Bool -> Bool
- all :: Vector v a => (a -> Bool) -> v a -> Bool
- any :: Vector v a => (a -> Bool) -> v a -> Bool
- find :: Vector v a => (a -> Bool) -> v a -> Maybe a
- zipWith :: (Vector v a, Vector v b, Vector v c) => (a -> b -> c) -> v a -> v b -> v c
- zipWith3 :: (Vector v a, Vector v b, Vector v c, Vector v d) => (a -> b -> c -> d) -> v a -> v b -> v c -> v d
- zipWithM :: (Vector v a, Vector v b, Vector v c, Applicative f) => (a -> b -> f c) -> v a -> v b -> f (v c)
- zipWithM_ :: (Vector v a, Vector v b, Applicative f) => (a -> b -> f c) -> v a -> v b -> f ()
- izipWith :: (Vector v a, Vector v b, Vector v c) => (Int -> a -> b -> c) -> v a -> v b -> v c
- izipWith3 :: (Vector v a, Vector v b, Vector v c, Vector v d) => (Int -> a -> b -> c -> d) -> v a -> v b -> v c -> v d
- izipWithM :: (Vector v a, Vector v b, Vector v c, Applicative f) => (Int -> a -> b -> f c) -> v a -> v b -> f (v c)
- izipWithM_ :: (Vector v a, Vector v b, Vector v c, Applicative f, Vector v (f c)) => (Int -> a -> b -> f c) -> v a -> v b -> f ()
- newtype StorableViaFixed (v :: k -> Type) (a :: k) = StorableViaFixed (v a)
- defaultAlignemnt :: Storable a => v a -> Int
- defaultSizeOf :: (Storable a, Vector v a) => v a -> Int
- defaultPeek :: (Storable a, Vector v a) => Ptr (v a) -> IO (v a)
- defaultPoke :: (Storable a, Vector v a) => Ptr (v a) -> v a -> IO ()
- defaultRnf :: (NFData a, Vector v a) => v a -> ()
- convert :: (Vector v a, Vector w a, Dim v ~ Dim w) => v a -> w a
- toList :: Vector v a => v a -> [a]
- fromList :: Vector v a => [a] -> v a
- fromList' :: Vector v a => [a] -> v a
- fromListM :: Vector v a => [a] -> Maybe (v a)
- fromFoldable :: (Vector v a, Foldable f) => f a -> Maybe (v a)
- newtype VecList (n :: Nat) a = VecList (VecPeano (Peano n) a)
- data VecPeano (n :: PeanoNum) a where
- newtype Only a = Only a
- data Empty (a :: k) = Empty
- type Tuple2 a = (a, a)
- type Tuple3 a = (a, a, a)
- type Tuple4 a = (a, a, a, a)
- type Tuple5 a = (a, a, a, a, a)
Vector type class
Vector size
type family Dim (v :: Type -> Type) :: Nat Source #
Size of vector expressed as type-level natural.
Instances
Type class
class Arity (Dim v) => Vector (v :: Type -> Type) a where Source #
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
For example instance for 2D vectors could be written as:
data V2 a = V2 a a type instance V2 = 2 instance Vector V2 a where construct = Fun V2 inspect (V2 a b) (Fun f) = f a b
Methods
construct :: Fun (Peano (Dim v)) a (v a) Source #
N-ary function for creation of vectors.
inspect :: v a -> Fun (Peano (Dim v)) a b -> b Source #
Deconstruction of vector.
basicIndex :: v a -> Int -> a Source #
Optional more efficient implementation of indexing. Shouldn't
be used directly, use !
instead.