{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
module Data.Vector.Fixed.Internal where
import Control.DeepSeq (NFData(..))
import Data.Typeable (Proxy(..))
import Data.Functor.Identity (Identity(..))
import qualified Data.Foldable as T
import qualified Data.Traversable as T
import Foreign.Storable (Storable(..))
import Foreign.Ptr (Ptr,castPtr)
import GHC.TypeLits
import Data.Vector.Fixed.Cont (Vector(..),Dim,Arity,vector,Add)
import qualified Data.Vector.Fixed.Cont as C
import Prelude hiding ( replicate,map,zipWith,maximum,minimum,and,or,all,any
, foldl,foldr,foldl1,length,sum,reverse,scanl,scanl1
, head,tail,mapM,mapM_,sequence,sequence_,concat
)
mk0 :: (Vector v a, Dim v ~ 0) => v a
mk0 = vector C.empty
{-# INLINE mk0 #-}
mk1 :: (Vector v a, Dim v ~ 1) => a -> v a
mk1 a1 = vector $ C.mk1 a1
{-# INLINE mk1 #-}
mk2 :: (Vector v a, Dim v ~ 2) => a -> a -> v a
mk2 a1 a2 = vector $ C.mk2 a1 a2
{-# INLINE mk2 #-}
mk3 :: (Vector v a, Dim v ~ 3) => a -> a -> a -> v a
mk3 a1 a2 a3 = vector $ C.mk3 a1 a2 a3
{-# INLINE mk3 #-}
mk4 :: (Vector v a, Dim v ~ 4) => a -> a -> a -> a -> v a
mk4 a1 a2 a3 a4 = vector $ C.mk4 a1 a2 a3 a4
{-# INLINE mk4 #-}
mk5 :: (Vector v a, Dim v ~ 5) => a -> a -> a -> a -> a -> v a
mk5 a1 a2 a3 a4 a5 = vector $ C.mk5 a1 a2 a3 a4 a5
{-# INLINE mk5 #-}
mkN :: forall proxy v a. (Vector v a)
=> proxy (v a) -> C.Fn (C.Peano (Dim v)) a (v a)
mkN _ = C.unFun (construct :: C.Fun (C.Peano (Dim v)) a (v a))
replicate :: Vector v a => a -> v a
{-# INLINE replicate #-}
replicate
= vector . C.replicate
replicateM :: (Vector v a, Applicative f) => f a -> f (v a)
{-# INLINE replicateM #-}
replicateM
= fmap vector . C.replicateM
basis :: (Vector v a, Num a) => Int -> v a
{-# INLINE basis #-}
basis = vector . C.basis
unfoldr :: (Vector v a) => (b -> (a,b)) -> b -> v a
{-# INLINE unfoldr #-}
unfoldr f = vector . C.unfoldr f
generate :: (Vector v a) => (Int -> a) -> v a
{-# INLINE generate #-}
generate = vector . C.generate
generateM :: (Applicative f, Vector v a) => (Int -> f a) -> f (v a)
{-# INLINE generateM #-}
generateM = fmap vector . C.generateM
head :: (Vector v a, 1 <= Dim v) => v a -> a
{-# INLINE head #-}
head = C.head . C.cvec
tail :: (Vector v a, Vector w a, Dim v ~ (Dim w + 1))
=> v a -> w a
{-# INLINE tail #-}
tail = vector . C.tail . C.cvec
cons :: (Vector v a, Vector w a, Dim w ~ (Dim v + 1))
=> a -> v a -> w a
{-# INLINE cons #-}
cons a = vector . C.cons a . C.cvec
snoc :: (Vector v a, Vector w a, Dim w ~ (Dim v + 1))
=> a -> v a -> w a
{-# INLINE snoc #-}
snoc a = vector . C.snoc a . C.cvec
concat :: ( Vector v a, Vector u a, Vector w a
, (Dim v + Dim u) ~ Dim w
, C.Peano (Dim v + Dim u) ~ Add (C.Peano (Dim v)) (C.Peano (Dim u))
)
=> v a -> u a -> w a
{-# INLINE concat #-}
concat v u = vector $ C.concat (C.cvec v) (C.cvec u)
reverse :: Vector v a => v a -> v a
reverse = vector . C.reverse . C.cvec
{-# INLINE reverse #-}
(!) :: (Vector v a) => v a -> Int -> a
{-# INLINE (!) #-}
v ! n = runIndex n (C.cvec v)
runIndex :: Arity n => Int -> C.ContVec n r -> r
runIndex = C.index
{-# INLINE[0] runIndex #-}
{-# RULES
"fixed-vector:index/basicIndex"[1] forall vv i.
runIndex i (C.cvec vv) = C.basicIndex vv i
#-}
index :: (Vector v a, KnownNat k, k + 1 <= Dim v)
=> v a -> proxy k -> a
{-# INLINE index #-}
index v k = v ! fromIntegral (natVal k)
set :: (Vector v a, KnownNat k, k + 1 <= Dim v) => proxy k -> a -> v a -> v a
{-# INLINE set #-}
set k a = runIdentity . element (fromIntegral (natVal k))
(const (Identity a))
element :: (Vector v a, Functor f) => Int -> (a -> f a) -> (v a -> f (v a))
{-# INLINE element #-}
element i f v = vector `fmap` C.element i f (C.cvec v)
elementTy :: (Vector v a, KnownNat k, k + 1 <= Dim v, Functor f)
=> proxy k -> (a -> f a) -> (v a -> f (v a))
{-# INLINE elementTy #-}
elementTy k = element (fromIntegral (natVal k))
foldl :: Vector v a => (b -> a -> b) -> b -> v a -> b
{-# INLINE foldl #-}
foldl f x = C.foldl f x
. C.cvec
foldr :: Vector v a => (a -> b -> b) -> b -> v a -> b
{-# INLINE foldr #-}
foldr f x = C.foldr f x
. C.cvec
foldl1 :: (Vector v a, 1 <= Dim v) => (a -> a -> a) -> v a -> a
{-# INLINE foldl1 #-}
foldl1 f = C.foldl1 f
. C.cvec
fold :: (Vector v m, Monoid m) => v m -> m
{-# INLINE fold #-}
fold = T.fold
. C.cvec
foldMap :: (Vector v a, Monoid m) => (a -> m) -> v a -> m
{-# INLINE foldMap #-}
foldMap f = T.foldMap f
. C.cvec
ifoldr :: Vector v a => (Int -> a -> b -> b) -> b -> v a -> b
{-# INLINE ifoldr #-}
ifoldr f x = C.ifoldr f x
. C.cvec
ifoldl :: Vector v a => (b -> Int -> a -> b) -> b -> v a -> b
{-# INLINE ifoldl #-}
ifoldl f z = C.ifoldl f z
. C.cvec
foldM :: (Vector v a, Monad m) => (b -> a -> m b) -> b -> v a -> m b
{-# INLINE foldM #-}
foldM f x = C.foldM f x . C.cvec
ifoldM :: (Vector v a, Monad m) => (b -> Int -> a -> m b) -> b -> v a -> m b
{-# INLINE ifoldM #-}
ifoldM f x = C.ifoldM f x . C.cvec
sum :: (Vector v a, Num a) => v a -> a
sum = C.sum . C.cvec
{-# INLINE sum #-}
maximum :: (Vector v a, 1 <= Dim v, Ord a) => v a -> a
maximum = C.maximum . C.cvec
{-# INLINE maximum #-}
minimum :: (Vector v a, 1 <= Dim v, Ord a) => v a -> a
minimum = C.minimum . C.cvec
{-# INLINE minimum #-}
and :: (Vector v Bool) => v Bool -> Bool
and = C.and . C.cvec
{-# INLINE and #-}
or :: (Vector v Bool) => v Bool -> Bool
or = C.or . C.cvec
{-# INLINE or #-}
all :: (Vector v a) => (a -> Bool) -> v a -> Bool
all f = (C.all f) . C.cvec
{-# INLINE all #-}
any :: (Vector v a) => (a -> Bool) -> v a -> Bool
any f = (C.any f) . C.cvec
{-# INLINE any #-}
find :: (Vector v a) => (a -> Bool) -> v a -> Maybe a
find f = (C.find f) . C.cvec
{-# INLINE find #-}
eq :: (Vector v a, Eq a) => v a -> v a -> Bool
{-# INLINE eq #-}
eq v w = C.and
$ C.zipWith (==) (C.cvec v) (C.cvec w)
ord :: (Vector v a, Ord a) => v a -> v a -> Ordering
{-# INLINE ord #-}
ord v w = C.foldl mappend mempty
$ C.zipWith compare (C.cvec v) (C.cvec w)
map :: (Vector v a, Vector v b) => (a -> b) -> v a -> v b
{-# INLINE map #-}
map f = vector
. C.map f
. C.cvec
sequence :: (Vector v a, Vector v (f a), Applicative f) => v (f a) -> f (v a)
{-# INLINE sequence #-}
sequence = mapM id
sequence_ :: (Vector v (f a), Applicative f) => v (f a) -> f ()
{-# INLINE sequence_ #-}
sequence_ = mapM_ id
mapM :: (Vector v a, Vector v b, Applicative f) => (a -> f b) -> v a -> f (v b)
{-# INLINE mapM #-}
mapM f = fmap vector
. C.mapM f
. C.cvec
mapM_ :: (Vector v a, Applicative f) => (a -> f b) -> v a -> f ()
{-# INLINE mapM_ #-}
mapM_ f = C.mapM_ f
. C.cvec
imap :: (Vector v a, Vector v b) =>
(Int -> a -> b) -> v a -> v b
{-# INLINE imap #-}
imap f = vector
. C.imap f
. C.cvec
imapM :: (Vector v a, Vector v b, Applicative f)
=> (Int -> a -> f b) -> v a -> f (v b)
{-# INLINE imapM #-}
imapM f = fmap vector
. C.imapM f
. C.cvec
imapM_ :: (Vector v a, Applicative f) => (Int -> a -> f b) -> v a -> f ()
{-# INLINE imapM_ #-}
imapM_ f = C.imapM_ f
. C.cvec
scanl :: (Vector v a, Vector w b, Dim w ~ (Dim v + 1))
=> (b -> a -> b) -> b -> v a -> w b
{-# INLINE scanl #-}
scanl f x0 = vector . C.scanl f x0 . C.cvec
scanl1 :: (Vector v a)
=> (a -> a -> a) -> v a -> v a
{-# INLINE scanl1 #-}
scanl1 f = vector . C.scanl1 f . C.cvec
sequenceA :: (Vector v a, Vector v (f a), Applicative f)
=> v (f a) -> f (v a)
{-# INLINE sequenceA #-}
sequenceA = fmap vector . T.sequenceA . C.cvec
traverse :: (Vector v a, Vector v b, Applicative f)
=> (a -> f b) -> v a -> f (v b)
{-# INLINE traverse #-}
traverse f = fmap vector . T.traverse f . C.cvec
distribute :: (Vector v a, Vector v (f a), Functor f)
=> f (v a) -> v (f a)
{-# INLINE distribute #-}
distribute = vector . C.distribute . fmap C.cvec
collect :: (Vector v a, Vector v b, Vector v (f b), Functor f)
=> (a -> v b) -> f a -> v (f b)
{-# INLINE collect #-}
collect f = vector . C.collect (C.cvec . f)
zipWith :: (Vector v a, Vector v b, Vector v c)
=> (a -> b -> c) -> v a -> v b -> v c
{-# INLINE zipWith #-}
zipWith f v u = vector
$ C.zipWith f (C.cvec v) (C.cvec u)
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
{-# INLINE zipWith3 #-}
zipWith3 f v1 v2 v3
= vector
$ C.zipWith3 f (C.cvec v1) (C.cvec v2) (C.cvec v3)
zipWithM :: (Vector v a, Vector v b, Vector v c, Applicative f)
=> (a -> b -> f c) -> v a -> v b -> f (v c)
{-# INLINE zipWithM #-}
zipWithM f v u = fmap vector
$ C.zipWithM f (C.cvec v) (C.cvec u)
zipWithM_
:: (Vector v a, Vector v b, Applicative f)
=> (a -> b -> f c) -> v a -> v b -> f ()
{-# INLINE zipWithM_ #-}
zipWithM_ f xs ys = C.zipWithM_ f (C.cvec xs) (C.cvec ys)
izipWith :: (Vector v a, Vector v b, Vector v c)
=> (Int -> a -> b -> c) -> v a -> v b -> v c
{-# INLINE izipWith #-}
izipWith f v u = vector
$ C.izipWith f (C.cvec v) (C.cvec u)
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
{-# INLINE izipWith3 #-}
izipWith3 f v1 v2 v3
= vector
$ C.izipWith3 f (C.cvec v1) (C.cvec v2) (C.cvec v3)
izipWithM :: (Vector v a, Vector v b, Vector v c, Applicative f)
=> (Int -> a -> b -> f c) -> v a -> v b -> f (v c)
{-# INLINE izipWithM #-}
izipWithM f v u = fmap vector
$ C.izipWithM f (C.cvec v) (C.cvec u)
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 ()
{-# INLINE izipWithM_ #-}
izipWithM_ f xs ys = C.izipWithM_ f (C.cvec xs) (C.cvec ys)
defaultAlignemnt :: forall a v. Storable a => v a -> Int
defaultAlignemnt _ = alignment (undefined :: a)
{-# INLINE defaultAlignemnt #-}
defaultSizeOf
:: forall a v. (Storable a, Vector v a)
=> v a -> Int
defaultSizeOf _ = sizeOf (undefined :: a) * C.arity (Proxy :: Proxy (Dim v))
{-# INLINE defaultSizeOf #-}
defaultPeek :: (Storable a, Vector v a) => Ptr (v a) -> IO (v a)
{-# INLINE defaultPeek #-}
defaultPeek ptr
= generateM (peekElemOff (castPtr ptr))
defaultPoke :: (Storable a, Vector v a) => Ptr (v a) -> v a -> IO ()
{-# INLINE defaultPoke #-}
defaultPoke ptr
= imapM_ (pokeElemOff (castPtr ptr))
defaultRnf :: (NFData a, Vector v a) => v a -> ()
defaultRnf = foldl (\() a -> rnf a) ()
convert :: (Vector v a, Vector w a, Dim v ~ Dim w) => v a -> w a
{-# INLINE convert #-}
convert = vector . C.cvec
toList :: (Vector v a) => v a -> [a]
toList = foldr (:) []
{-# INLINE toList #-}
fromList :: (Vector v a) => [a] -> v a
{-# INLINE fromList #-}
fromList = vector . C.fromList
fromList' :: (Vector v a) => [a] -> v a
{-# INLINE fromList' #-}
fromList' = vector . C.fromList'
fromListM :: (Vector v a) => [a] -> Maybe (v a)
{-# INLINE fromListM #-}
fromListM = fmap vector . C.fromListM
fromFoldable :: (Vector v a, T.Foldable f) => f a -> Maybe (v a)
{-# INLINE fromFoldable #-}
fromFoldable = fromListM . T.toList
showsPrec :: (Vector v a, Show a) => Int -> v a -> ShowS
showsPrec d v = showParen (d > 10) $ showString "fromList " . Prelude.showsPrec 11 (toList v)
{-# INLINE showsPrec #-}