{-# LANGUAGE CPP                   #-}
{-# LANGUAGE DataKinds             #-}
{-# LANGUAGE DeriveDataTypeable    #-}
{-# LANGUAGE FlexibleContexts      #-}
{-# LANGUAGE FlexibleInstances     #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE PolyKinds             #-}
{-# LANGUAGE ScopedTypeVariables   #-}
{-# LANGUAGE StandaloneDeriving    #-}
{-# LANGUAGE TypeFamilies          #-}
{-# LANGUAGE UndecidableInstances  #-}
-- |
-- Unboxed vectors with fixed length.
module Data.Vector.Fixed.Unboxed(
    -- * Immutable
    Vec
  , Vec1
  , Vec2
  , Vec3
  , Vec4
  , Vec5
    -- * Mutable
  , MVec
    -- * Type classes
  , Unbox
  ) where

import Control.Applicative   (Const(..))
import Control.Monad
import Control.DeepSeq       (NFData(..))
import Data.Complex
import Data.Data
import Data.Functor.Identity (Identity(..))
import Data.Int              (Int8, Int16, Int32, Int64 )
import Data.Monoid           (Monoid(..),Dual(..),Sum(..),Product(..),All(..),Any(..))
import Data.Semigroup        (Semigroup(..))
import Data.Ord              (Down(..))
import Data.Word             (Word,Word8,Word16,Word32,Word64)
import Foreign.Storable      (Storable(..))
import GHC.TypeLits
import Prelude               ( Show(..),Eq(..),Ord(..),Int,Double,Float,Char,Bool(..)
                             , ($),(.),seq)

import Data.Vector.Fixed (Dim,Vector(..),VectorN,eq,ord,replicate,zipWith,foldl,
                          defaultSizeOf,defaultAlignemnt,defaultPeek,defaultPoke
                         )
import Data.Vector.Fixed.Mutable (Mutable, MVector(..), IVector(..), DimM, constructVec, inspectVec, Arity, index)
import qualified Data.Vector.Fixed.Cont      as C
import qualified Data.Vector.Fixed.Primitive as P
import qualified Data.Vector.Fixed.Internal  as I



----------------------------------------------------------------
-- Data type
----------------------------------------------------------------

data family Vec  (n :: Nat) a
data family MVec (n :: Nat) s a

deriving instance Typeable Vec
deriving instance Typeable MVec

type Vec1 = Vec 1
type Vec2 = Vec 2
type Vec3 = Vec 3
type Vec4 = Vec 4
type Vec5 = Vec 5

class (Arity n, IVector (Vec n) a, MVector (MVec n) a) => Unbox n a


----------------------------------------------------------------
-- Generic instances
----------------------------------------------------------------

instance (Arity n, Show a, Unbox n a) => Show (Vec n a) where
  showsPrec :: Int -> Vec n a -> ShowS
showsPrec = Int -> Vec n a -> ShowS
forall (v :: * -> *) a. (Vector v a, Show a) => Int -> v a -> ShowS
I.showsPrec

instance (Arity n, Unbox n a, NFData a) => NFData (Vec n a) where
  rnf :: Vec n a -> ()
rnf = (() -> a -> ()) -> () -> Vec n a -> ()
forall (v :: * -> *) a b.
Vector v a =>
(b -> a -> b) -> b -> v a -> b
foldl (\()
r a
a -> ()
r () -> () -> ()
`seq` a -> ()
forall a. NFData a => a -> ()
rnf a
a) ()
  {-# INLINE rnf #-}

type instance Mutable (Vec n) = MVec n

type instance Dim  (Vec  n) = n
type instance DimM (MVec n) = n

instance (Unbox n a) => Vector (Vec n) a where
  construct :: Fun (Peano (Dim (Vec n))) a (Vec n a)
construct  = Fun (Peano (Dim (Vec n))) a (Vec n a)
forall (v :: * -> *) a.
(Arity (Dim v), IVector v a) =>
Fun (Peano (Dim v)) a (v a)
constructVec
  inspect :: Vec n a -> Fun (Peano (Dim (Vec n))) a b -> b
inspect    = Vec n a -> Fun (Peano (Dim (Vec n))) a b -> b
forall (v :: * -> *) a b.
(Arity (Dim v), IVector v a) =>
v a -> Fun (Peano (Dim v)) a b -> b
inspectVec
  basicIndex :: Vec n a -> Int -> a
basicIndex = Vec n a -> Int -> a
forall (v :: * -> *) a. IVector v a => v a -> Int -> a
index
  {-# INLINE construct  #-}
  {-# INLINE inspect    #-}
  {-# INLINE basicIndex #-}


instance (Unbox n a) => VectorN Vec n a

instance (Unbox n a, Eq a) => Eq (Vec n a) where
  == :: Vec n a -> Vec n a -> Bool
(==) = Vec n a -> Vec n a -> Bool
forall (v :: * -> *) a. (Vector v a, Eq a) => v a -> v a -> Bool
eq
  {-# INLINE (==) #-}
instance (Unbox n a, Ord a) => Ord (Vec n a) where
  compare :: Vec n a -> Vec n a -> Ordering
compare = Vec n a -> Vec n a -> Ordering
forall (v :: * -> *) a.
(Vector v a, Ord a) =>
v a -> v a -> Ordering
ord
  {-# INLINE compare #-}

instance (Unbox n a, Monoid a) => Monoid (Vec n a) where
  mempty :: Vec n a
mempty  = a -> Vec n a
forall (v :: * -> *) a. Vector v a => a -> v a
replicate a
forall a. Monoid a => a
mempty
  mappend :: Vec n a -> Vec n a -> Vec n a
mappend = (a -> a -> a) -> Vec n a -> Vec n a -> Vec n a
forall (v :: * -> *) a b c.
(Vector v a, Vector v b, Vector v c) =>
(a -> b -> c) -> v a -> v b -> v c
zipWith a -> a -> a
forall a. Monoid a => a -> a -> a
mappend
  {-# INLINE mempty  #-}
  {-# INLINE mappend #-}

instance (Unbox n a, Semigroup a) => Semigroup (Vec n a) where
  <> :: Vec n a -> Vec n a -> Vec n a
(<>) = (a -> a -> a) -> Vec n a -> Vec n a -> Vec n a
forall (v :: * -> *) a b c.
(Vector v a, Vector v b, Vector v c) =>
(a -> b -> c) -> v a -> v b -> v c
zipWith a -> a -> a
forall a. Semigroup a => a -> a -> a
(<>)
  {-# INLINE (<>) #-}

instance (Typeable n, Unbox n a, Data a) => Data (Vec n a) where
  gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Vec n a -> c (Vec n a)
gfoldl       = (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Vec n a -> c (Vec n a)
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)
C.gfoldl
  gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Vec n a)
gunfold      = (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Vec n a)
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)
C.gunfold
  toConstr :: Vec n a -> Constr
toConstr   Vec n a
_ = Constr
con_Vec
  dataTypeOf :: Vec n a -> DataType
dataTypeOf Vec n a
_ = DataType
ty_Vec

ty_Vec :: DataType
ty_Vec :: DataType
ty_Vec  = String -> [Constr] -> DataType
mkDataType String
"Data.Vector.Fixed.Unboxed.Vec" [Constr
con_Vec]

con_Vec :: Constr
con_Vec :: Constr
con_Vec = DataType -> String -> [String] -> Fixity -> Constr
mkConstr DataType
ty_Vec String
"Vec" [] Fixity
Prefix

instance (Storable a, Unbox n a) => Storable (Vec n a) where
  alignment :: Vec n a -> Int
alignment = Vec n a -> Int
forall a (v :: * -> *). Storable a => v a -> Int
defaultAlignemnt
  sizeOf :: Vec n a -> Int
sizeOf    = Vec n a -> Int
forall a (v :: * -> *). (Storable a, Vector v a) => v a -> Int
defaultSizeOf
  peek :: Ptr (Vec n a) -> IO (Vec n a)
peek      = Ptr (Vec n a) -> IO (Vec n a)
forall a (v :: * -> *).
(Storable a, Vector v a) =>
Ptr (v a) -> IO (v a)
defaultPeek
  poke :: Ptr (Vec n a) -> Vec n a -> IO ()
poke      = Ptr (Vec n a) -> Vec n a -> IO ()
forall a (v :: * -> *).
(Storable a, Vector v a) =>
Ptr (v a) -> v a -> IO ()
defaultPoke
  {-# INLINE alignment #-}
  {-# INLINE sizeOf    #-}
  {-# INLINE peek      #-}
  {-# INLINE poke      #-}



----------------------------------------------------------------
-- Data instances
----------------------------------------------------------------

-- Unit type
data instance MVec n s () = MV_Unit
data instance Vec  n   () = V_Unit

instance Arity n => Unbox n ()

instance Arity n => MVector (MVec n) () where
  new :: m (MVec n (PrimState m) ())
new          = MVec n (PrimState m) () -> m (MVec n (PrimState m) ())
forall (m :: * -> *) a. Monad m => a -> m a
return MVec n (PrimState m) ()
forall (n :: Nat) s. MVec n s ()
MV_Unit
  {-# INLINE new         #-}
  copy :: MVec n (PrimState m) () -> MVec n (PrimState m) () -> m ()
copy MVec n (PrimState m) ()
_ MVec n (PrimState m) ()
_     = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
  {-# INLINE move        #-}
  move :: MVec n (PrimState m) () -> MVec n (PrimState m) () -> m ()
move MVec n (PrimState m) ()
_ MVec n (PrimState m) ()
_     = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
  {-# INLINE copy        #-}
  unsafeRead :: MVec n (PrimState m) () -> Int -> m ()
unsafeRead  MVec n (PrimState m) ()
_ Int
_   = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
  {-# INLINE unsafeRead  #-}
  unsafeWrite :: MVec n (PrimState m) () -> Int -> () -> m ()
unsafeWrite MVec n (PrimState m) ()
_ Int
_ ()
_ = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
  {-# INLINE unsafeWrite #-}

instance Arity n => IVector (Vec n) () where
  unsafeFreeze :: Mutable (Vec n) (PrimState m) () -> m (Vec n ())
unsafeFreeze Mutable (Vec n) (PrimState m) ()
_   = Vec n () -> m (Vec n ())
forall (m :: * -> *) a. Monad m => a -> m a
return Vec n ()
forall (n :: Nat). Vec n ()
V_Unit
  unsafeThaw :: Vec n () -> m (Mutable (Vec n) (PrimState m) ())
unsafeThaw   Vec n ()
_   = MVec n (PrimState m) () -> m (MVec n (PrimState m) ())
forall (m :: * -> *) a. Monad m => a -> m a
return MVec n (PrimState m) ()
forall (n :: Nat) s. MVec n s ()
MV_Unit
  unsafeIndex :: Vec n () -> Int -> ()
unsafeIndex  Vec n ()
_ Int
_ = ()
  {-# INLINE unsafeFreeze #-}
  {-# INLINE unsafeThaw   #-}
  {-# INLINE unsafeIndex  #-}



----------------------------------------------------------------
-- Boolean

newtype instance MVec n s Bool = MV_Bool (P.MVec n s Word8)
newtype instance Vec  n   Bool = V_Bool  (P.Vec  n   Word8)

instance Arity n => Unbox n Bool

instance Arity n => MVector (MVec n) Bool where
  new :: m (MVec n (PrimState m) Bool)
new          = MVec n (PrimState m) Word8 -> MVec n (PrimState m) Bool
forall (n :: Nat) s. MVec n s Word8 -> MVec n s Bool
MV_Bool (MVec n (PrimState m) Word8 -> MVec n (PrimState m) Bool)
-> m (MVec n (PrimState m) Word8) -> m (MVec n (PrimState m) Bool)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` m (MVec n (PrimState m) Word8)
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
m (v (PrimState m) a)
new
  {-# INLINE new         #-}
  copy :: MVec n (PrimState m) Bool -> MVec n (PrimState m) Bool -> m ()
copy (MV_Bool v) (MV_Bool w) = MVec n (PrimState m) Word8 -> MVec n (PrimState m) Word8 -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> v (PrimState m) a -> m ()
copy MVec n (PrimState m) Word8
v MVec n (PrimState m) Word8
w
  {-# INLINE copy        #-}
  move :: MVec n (PrimState m) Bool -> MVec n (PrimState m) Bool -> m ()
move (MV_Bool v) (MV_Bool w) = MVec n (PrimState m) Word8 -> MVec n (PrimState m) Word8 -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> v (PrimState m) a -> m ()
move MVec n (PrimState m) Word8
v MVec n (PrimState m) Word8
w
  {-# INLINE move        #-}
  unsafeRead :: MVec n (PrimState m) Bool -> Int -> m Bool
unsafeRead  (MV_Bool v) Int
i   = Word8 -> Bool
toBool (Word8 -> Bool) -> m Word8 -> m Bool
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` MVec n (PrimState m) Word8 -> Int -> m Word8
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> Int -> m a
unsafeRead MVec n (PrimState m) Word8
v Int
i
  {-# INLINE unsafeRead  #-}
  unsafeWrite :: MVec n (PrimState m) Bool -> Int -> Bool -> m ()
unsafeWrite (MV_Bool v) Int
i Bool
b = MVec n (PrimState m) Word8 -> Int -> Word8 -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> Int -> a -> m ()
unsafeWrite MVec n (PrimState m) Word8
v Int
i (Bool -> Word8
fromBool Bool
b)
  {-# INLINE unsafeWrite #-}

instance Arity n => IVector (Vec n) Bool where
  unsafeFreeze :: Mutable (Vec n) (PrimState m) Bool -> m (Vec n Bool)
unsafeFreeze (MV_Bool v) = Vec n Word8 -> Vec n Bool
forall (n :: Nat). Vec n Word8 -> Vec n Bool
V_Bool  (Vec n Word8 -> Vec n Bool) -> m (Vec n Word8) -> m (Vec n Bool)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` Mutable (Vec n) (PrimState m) Word8 -> m (Vec n Word8)
forall (v :: * -> *) a (m :: * -> *).
(IVector v a, PrimMonad m) =>
Mutable v (PrimState m) a -> m (v a)
unsafeFreeze Mutable (Vec n) (PrimState m) Word8
MVec n (PrimState m) Word8
v
  unsafeThaw :: Vec n Bool -> m (Mutable (Vec n) (PrimState m) Bool)
unsafeThaw   (V_Bool  v) = MVec n (PrimState m) Word8 -> MVec n (PrimState m) Bool
forall (n :: Nat) s. MVec n s Word8 -> MVec n s Bool
MV_Bool (MVec n (PrimState m) Word8 -> MVec n (PrimState m) Bool)
-> m (MVec n (PrimState m) Word8) -> m (MVec n (PrimState m) Bool)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` Vec n Word8 -> m (Mutable (Vec n) (PrimState m) Word8)
forall (v :: * -> *) a (m :: * -> *).
(IVector v a, PrimMonad m) =>
v a -> m (Mutable v (PrimState m) a)
unsafeThaw   Vec n Word8
v
  unsafeIndex :: Vec n Bool -> Int -> Bool
unsafeIndex  (V_Bool  v) = Word8 -> Bool
toBool (Word8 -> Bool) -> (Int -> Word8) -> Int -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vec n Word8 -> Int -> Word8
forall (v :: * -> *) a. IVector v a => v a -> Int -> a
unsafeIndex Vec n Word8
v
  {-# INLINE unsafeFreeze #-}
  {-# INLINE unsafeThaw   #-}
  {-# INLINE unsafeIndex  #-}


fromBool :: Bool -> Word8
{-# INLINE fromBool #-}
fromBool :: Bool -> Word8
fromBool Bool
True = Word8
1
fromBool Bool
False = Word8
0

toBool :: Word8 -> Bool
{-# INLINE toBool #-}
toBool :: Word8 -> Bool
toBool Word8
0 = Bool
False
toBool Word8
_ = Bool
True


----------------------------------------------------------------
-- Primitive wrappers
#define primMV(ty,con)                              \
instance Arity n => MVector (MVec n) ty where {     \
; new = con `liftM` new                             \
; copy (con v) (con w) = copy v w                   \
; move (con v) (con w) = move v w                   \
; unsafeRead  (con v) i = unsafeRead v i            \
; unsafeWrite (con v) i x = unsafeWrite v i x       \
; {-# INLINE new         #-}                        \
; {-# INLINE move        #-}                        \
; {-# INLINE copy        #-}                        \
; {-# INLINE unsafeRead  #-}                        \
; {-# INLINE unsafeWrite #-}                        \
}

#define primIV(ty,con,mcon)                             \
instance Arity n => IVector (Vec n) ty where {          \
; unsafeFreeze (mcon v)   = con  `liftM` unsafeFreeze v \
; unsafeThaw   (con  v)   = mcon `liftM` unsafeThaw   v \
; unsafeIndex  (con  v) i = unsafeIndex v i             \
; {-# INLINE unsafeFreeze #-}                           \
; {-# INLINE unsafeThaw   #-}                           \
; {-# INLINE unsafeIndex  #-}                           \
}

#define primWrap(ty,con,mcon) \
newtype instance MVec n s ty = mcon (P.MVec n s ty) ; \
newtype instance Vec  n   ty = con  (P.Vec  n   ty) ; \
instance Arity n => Unbox n ty ; \
primMV(ty, mcon     )          ; \
primIV(ty, con, mcon)



primWrap(Int,   V_Int,   MV_Int  )
primWrap(Int8,  V_Int8,  MV_Int8 )
primWrap(Int16, V_Int16, MV_Int16)
primWrap(Int32, V_Int32, MV_Int32)
primWrap(Int64, V_Int64, MV_Int64)

primWrap(Word,   V_Word,   MV_Word  )
primWrap(Word8,  V_Word8,  MV_Word8 )
primWrap(Word16, V_Word16, MV_Word16)
primWrap(Word32, V_Word32, MV_Word32)
primWrap(Word64, V_Word64, MV_Word64)

primWrap(Char,   V_Char,   MV_Char  )
primWrap(Float,  V_Float,  MV_Float )
primWrap(Double, V_Double, MV_Double)



----------------------------------------------------------------
-- Complex
newtype instance MVec n s (Complex a) = MV_Complex (MVec n s (a,a))
newtype instance Vec  n   (Complex a) = V_Complex  (Vec  n   (a,a))

instance (Unbox n a) => Unbox n (Complex a)

instance (Arity n, MVector (MVec n) a) => MVector (MVec n) (Complex a) where
  new :: m (MVec n (PrimState m) (Complex a))
new = MVec n (PrimState m) (a, a) -> MVec n (PrimState m) (Complex a)
forall (n :: Nat) s a. MVec n s (a, a) -> MVec n s (Complex a)
MV_Complex (MVec n (PrimState m) (a, a) -> MVec n (PrimState m) (Complex a))
-> m (MVec n (PrimState m) (a, a))
-> m (MVec n (PrimState m) (Complex a))
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` m (MVec n (PrimState m) (a, a))
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
m (v (PrimState m) a)
new
  {-# INLINE new #-}
  copy :: MVec n (PrimState m) (Complex a)
-> MVec n (PrimState m) (Complex a) -> m ()
copy (MV_Complex v) (MV_Complex w) = MVec n (PrimState m) (a, a) -> MVec n (PrimState m) (a, a) -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> v (PrimState m) a -> m ()
copy MVec n (PrimState m) (a, a)
v MVec n (PrimState m) (a, a)
w
  {-# INLINE copy        #-}
  move :: MVec n (PrimState m) (Complex a)
-> MVec n (PrimState m) (Complex a) -> m ()
move (MV_Complex v) (MV_Complex w) = MVec n (PrimState m) (a, a) -> MVec n (PrimState m) (a, a) -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> v (PrimState m) a -> m ()
move MVec n (PrimState m) (a, a)
v MVec n (PrimState m) (a, a)
w
  {-# INLINE move        #-}
  unsafeRead :: MVec n (PrimState m) (Complex a) -> Int -> m (Complex a)
unsafeRead (MV_Complex v) Int
i = do (a
a,a
b) <- MVec n (PrimState m) (a, a) -> Int -> m (a, a)
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> Int -> m a
unsafeRead MVec n (PrimState m) (a, a)
v Int
i
                                   Complex a -> m (Complex a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a
b)
  {-# INLINE unsafeRead  #-}
  unsafeWrite :: MVec n (PrimState m) (Complex a) -> Int -> Complex a -> m ()
unsafeWrite (MV_Complex v) Int
i (a
a :+ a
b) = MVec n (PrimState m) (a, a) -> Int -> (a, a) -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> Int -> a -> m ()
unsafeWrite MVec n (PrimState m) (a, a)
v Int
i (a
a,a
b)
  {-# INLINE unsafeWrite #-}

instance (Arity n, IVector (Vec n) a) => IVector (Vec n) (Complex a) where
  unsafeFreeze :: Mutable (Vec n) (PrimState m) (Complex a) -> m (Vec n (Complex a))
unsafeFreeze (MV_Complex v) = Vec n (a, a) -> Vec n (Complex a)
forall (n :: Nat) a. Vec n (a, a) -> Vec n (Complex a)
V_Complex (Vec n (a, a) -> Vec n (Complex a))
-> m (Vec n (a, a)) -> m (Vec n (Complex a))
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` Mutable (Vec n) (PrimState m) (a, a) -> m (Vec n (a, a))
forall (v :: * -> *) a (m :: * -> *).
(IVector v a, PrimMonad m) =>
Mutable v (PrimState m) a -> m (v a)
unsafeFreeze Mutable (Vec n) (PrimState m) (a, a)
MVec n (PrimState m) (a, a)
v
  {-# INLINE unsafeFreeze #-}
  unsafeThaw :: Vec n (Complex a) -> m (Mutable (Vec n) (PrimState m) (Complex a))
unsafeThaw   (V_Complex  v) = MVec n (PrimState m) (a, a) -> MVec n (PrimState m) (Complex a)
forall (n :: Nat) s a. MVec n s (a, a) -> MVec n s (Complex a)
MV_Complex (MVec n (PrimState m) (a, a) -> MVec n (PrimState m) (Complex a))
-> m (MVec n (PrimState m) (a, a))
-> m (MVec n (PrimState m) (Complex a))
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` Vec n (a, a) -> m (Mutable (Vec n) (PrimState m) (a, a))
forall (v :: * -> *) a (m :: * -> *).
(IVector v a, PrimMonad m) =>
v a -> m (Mutable v (PrimState m) a)
unsafeThaw Vec n (a, a)
v
  {-# INLINE unsafeThaw   #-}
  unsafeIndex :: Vec n (Complex a) -> Int -> Complex a
unsafeIndex (V_Complex v) Int
i =
    case Vec n (a, a) -> Int -> (a, a)
forall (v :: * -> *) a. IVector v a => v a -> Int -> a
unsafeIndex Vec n (a, a)
v Int
i of (a
a,a
b) -> a
a a -> a -> Complex a
forall a. a -> a -> Complex a
:+ a
b
  {-# INLINE unsafeIndex  #-}



----------------------------------------------------------------
-- Tuples
data instance MVec n s (a,b) = MV_2 !(MVec n s a) !(MVec n s b)
data instance Vec  n   (a,b) = V_2  !(Vec  n   a) !(Vec  n   b)

instance (Unbox n a, Unbox n b) => Unbox n (a,b)

instance (Arity n, MVector (MVec n) a, MVector (MVec n) b) => MVector (MVec n) (a,b) where
  new :: m (MVec n (PrimState m) (a, b))
new = do MVec n (PrimState m) a
as <- m (MVec n (PrimState m) a)
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
m (v (PrimState m) a)
new
           MVec n (PrimState m) b
bs <- m (MVec n (PrimState m) b)
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
m (v (PrimState m) a)
new
           MVec n (PrimState m) (a, b) -> m (MVec n (PrimState m) (a, b))
forall (m :: * -> *) a. Monad m => a -> m a
return (MVec n (PrimState m) (a, b) -> m (MVec n (PrimState m) (a, b)))
-> MVec n (PrimState m) (a, b) -> m (MVec n (PrimState m) (a, b))
forall a b. (a -> b) -> a -> b
$ MVec n (PrimState m) a
-> MVec n (PrimState m) b -> MVec n (PrimState m) (a, b)
forall (n :: Nat) s a b.
MVec n s a -> MVec n s b -> MVec n s (a, b)
MV_2 MVec n (PrimState m) a
as MVec n (PrimState m) b
bs
  {-# INLINE new #-}
  copy :: MVec n (PrimState m) (a, b) -> MVec n (PrimState m) (a, b) -> m ()
copy (MV_2 va vb) (MV_2 wa wb) = MVec n (PrimState m) a -> MVec n (PrimState m) a -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> v (PrimState m) a -> m ()
copy MVec n (PrimState m) a
va MVec n (PrimState m) a
wa m () -> m () -> m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> MVec n (PrimState m) b -> MVec n (PrimState m) b -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> v (PrimState m) a -> m ()
copy MVec n (PrimState m) b
vb MVec n (PrimState m) b
wb
  {-# INLINE copy        #-}
  move :: MVec n (PrimState m) (a, b) -> MVec n (PrimState m) (a, b) -> m ()
move (MV_2 va vb) (MV_2 wa wb) = MVec n (PrimState m) a -> MVec n (PrimState m) a -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> v (PrimState m) a -> m ()
move MVec n (PrimState m) a
va MVec n (PrimState m) a
wa m () -> m () -> m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> MVec n (PrimState m) b -> MVec n (PrimState m) b -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> v (PrimState m) a -> m ()
move MVec n (PrimState m) b
vb MVec n (PrimState m) b
wb
  {-# INLINE move        #-}
  unsafeRead :: MVec n (PrimState m) (a, b) -> Int -> m (a, b)
unsafeRead  (MV_2 v w) Int
i = do a
a <- MVec n (PrimState m) a -> Int -> m a
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> Int -> m a
unsafeRead MVec n (PrimState m) a
v Int
i
                                b
b <- MVec n (PrimState m) b -> Int -> m b
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> Int -> m a
unsafeRead MVec n (PrimState m) b
w Int
i
                                (a, b) -> m (a, b)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a,b
b)
  {-# INLINE unsafeRead  #-}
  unsafeWrite :: MVec n (PrimState m) (a, b) -> Int -> (a, b) -> m ()
unsafeWrite (MV_2 v w) Int
i (a
a,b
b) = MVec n (PrimState m) a -> Int -> a -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> Int -> a -> m ()
unsafeWrite MVec n (PrimState m) a
v Int
i a
a m () -> m () -> m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> MVec n (PrimState m) b -> Int -> b -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> Int -> a -> m ()
unsafeWrite MVec n (PrimState m) b
w Int
i b
b
  {-# INLINE unsafeWrite #-}


instance ( Arity n
         , IVector (Vec n) a, IVector (Vec n) b
         ) => IVector (Vec n) (a,b) where
  unsafeFreeze :: Mutable (Vec n) (PrimState m) (a, b) -> m (Vec n (a, b))
unsafeFreeze (MV_2 v w)   = do Vec n a
as <- Mutable (Vec n) (PrimState m) a -> m (Vec n a)
forall (v :: * -> *) a (m :: * -> *).
(IVector v a, PrimMonad m) =>
Mutable v (PrimState m) a -> m (v a)
unsafeFreeze Mutable (Vec n) (PrimState m) a
MVec n (PrimState m) a
v
                                 Vec n b
bs <- Mutable (Vec n) (PrimState m) b -> m (Vec n b)
forall (v :: * -> *) a (m :: * -> *).
(IVector v a, PrimMonad m) =>
Mutable v (PrimState m) a -> m (v a)
unsafeFreeze Mutable (Vec n) (PrimState m) b
MVec n (PrimState m) b
w
                                 Vec n (a, b) -> m (Vec n (a, b))
forall (m :: * -> *) a. Monad m => a -> m a
return (Vec n (a, b) -> m (Vec n (a, b)))
-> Vec n (a, b) -> m (Vec n (a, b))
forall a b. (a -> b) -> a -> b
$ Vec n a -> Vec n b -> Vec n (a, b)
forall (n :: Nat) a b. Vec n a -> Vec n b -> Vec n (a, b)
V_2 Vec n a
as Vec n b
bs
  {-# INLINE unsafeFreeze #-}
  unsafeThaw :: Vec n (a, b) -> m (Mutable (Vec n) (PrimState m) (a, b))
unsafeThaw   (V_2  v w)   = do MVec n (PrimState m) a
as <- Vec n a -> m (Mutable (Vec n) (PrimState m) a)
forall (v :: * -> *) a (m :: * -> *).
(IVector v a, PrimMonad m) =>
v a -> m (Mutable v (PrimState m) a)
unsafeThaw Vec n a
v
                                 MVec n (PrimState m) b
bs <- Vec n b -> m (Mutable (Vec n) (PrimState m) b)
forall (v :: * -> *) a (m :: * -> *).
(IVector v a, PrimMonad m) =>
v a -> m (Mutable v (PrimState m) a)
unsafeThaw Vec n b
w
                                 MVec n (PrimState m) (a, b) -> m (MVec n (PrimState m) (a, b))
forall (m :: * -> *) a. Monad m => a -> m a
return (MVec n (PrimState m) (a, b) -> m (MVec n (PrimState m) (a, b)))
-> MVec n (PrimState m) (a, b) -> m (MVec n (PrimState m) (a, b))
forall a b. (a -> b) -> a -> b
$ MVec n (PrimState m) a
-> MVec n (PrimState m) b -> MVec n (PrimState m) (a, b)
forall (n :: Nat) s a b.
MVec n s a -> MVec n s b -> MVec n s (a, b)
MV_2 MVec n (PrimState m) a
as MVec n (PrimState m) b
bs
  {-# INLINE unsafeThaw   #-}
  unsafeIndex :: Vec n (a, b) -> Int -> (a, b)
unsafeIndex  (V_2  v w) Int
i = (Vec n a -> Int -> a
forall (v :: * -> *) a. IVector v a => v a -> Int -> a
unsafeIndex Vec n a
v Int
i, Vec n b -> Int -> b
forall (v :: * -> *) a. IVector v a => v a -> Int -> a
unsafeIndex Vec n b
w Int
i)
  {-# INLINE unsafeIndex  #-}




data instance MVec n s (a,b,c) = MV_3 !(MVec n s a) !(MVec n s b) !(MVec n s c)
data instance Vec  n   (a,b,c) = V_3  !(Vec  n   a) !(Vec  n   b) !(Vec  n   c)

instance (Unbox n a, Unbox n b, Unbox n c) => Unbox n (a,b,c)

instance (Arity n, MVector (MVec n) a, MVector (MVec n) b, MVector (MVec n) c
         ) => MVector (MVec n) (a,b,c) where
  new :: m (MVec n (PrimState m) (a, b, c))
new = do MVec n (PrimState m) a
as <- m (MVec n (PrimState m) a)
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
m (v (PrimState m) a)
new
           MVec n (PrimState m) b
bs <- m (MVec n (PrimState m) b)
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
m (v (PrimState m) a)
new
           MVec n (PrimState m) c
cs <- m (MVec n (PrimState m) c)
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
m (v (PrimState m) a)
new
           MVec n (PrimState m) (a, b, c)
-> m (MVec n (PrimState m) (a, b, c))
forall (m :: * -> *) a. Monad m => a -> m a
return (MVec n (PrimState m) (a, b, c)
 -> m (MVec n (PrimState m) (a, b, c)))
-> MVec n (PrimState m) (a, b, c)
-> m (MVec n (PrimState m) (a, b, c))
forall a b. (a -> b) -> a -> b
$ MVec n (PrimState m) a
-> MVec n (PrimState m) b
-> MVec n (PrimState m) c
-> MVec n (PrimState m) (a, b, c)
forall (n :: Nat) s a b c.
MVec n s a -> MVec n s b -> MVec n s c -> MVec n s (a, b, c)
MV_3 MVec n (PrimState m) a
as MVec n (PrimState m) b
bs MVec n (PrimState m) c
cs
  {-# INLINE new #-}
  copy :: MVec n (PrimState m) (a, b, c)
-> MVec n (PrimState m) (a, b, c) -> m ()
copy (MV_3 va vb vc) (MV_3 wa wb wc)
    = MVec n (PrimState m) a -> MVec n (PrimState m) a -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> v (PrimState m) a -> m ()
copy MVec n (PrimState m) a
va MVec n (PrimState m) a
wa m () -> m () -> m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> MVec n (PrimState m) b -> MVec n (PrimState m) b -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> v (PrimState m) a -> m ()
copy MVec n (PrimState m) b
vb MVec n (PrimState m) b
wb m () -> m () -> m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> MVec n (PrimState m) c -> MVec n (PrimState m) c -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> v (PrimState m) a -> m ()
copy MVec n (PrimState m) c
vc MVec n (PrimState m) c
wc
  {-# INLINE copy        #-}
  move :: MVec n (PrimState m) (a, b, c)
-> MVec n (PrimState m) (a, b, c) -> m ()
move (MV_3 va vb vc) (MV_3 wa wb wc)
    = MVec n (PrimState m) a -> MVec n (PrimState m) a -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> v (PrimState m) a -> m ()
move MVec n (PrimState m) a
va MVec n (PrimState m) a
wa m () -> m () -> m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> MVec n (PrimState m) b -> MVec n (PrimState m) b -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> v (PrimState m) a -> m ()
move MVec n (PrimState m) b
vb MVec n (PrimState m) b
wb m () -> m () -> m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> MVec n (PrimState m) c -> MVec n (PrimState m) c -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> v (PrimState m) a -> m ()
move MVec n (PrimState m) c
vc MVec n (PrimState m) c
wc
  {-# INLINE move        #-}
  unsafeRead :: MVec n (PrimState m) (a, b, c) -> Int -> m (a, b, c)
unsafeRead  (MV_3 v w u) Int
i = do a
a <- MVec n (PrimState m) a -> Int -> m a
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> Int -> m a
unsafeRead MVec n (PrimState m) a
v Int
i
                                  b
b <- MVec n (PrimState m) b -> Int -> m b
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> Int -> m a
unsafeRead MVec n (PrimState m) b
w Int
i
                                  c
c <- MVec n (PrimState m) c -> Int -> m c
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> Int -> m a
unsafeRead MVec n (PrimState m) c
u Int
i
                                  (a, b, c) -> m (a, b, c)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a,b
b,c
c)
  {-# INLINE unsafeRead  #-}
  unsafeWrite :: MVec n (PrimState m) (a, b, c) -> Int -> (a, b, c) -> m ()
unsafeWrite (MV_3 v w u) Int
i (a
a,b
b,c
c)
    = MVec n (PrimState m) a -> Int -> a -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> Int -> a -> m ()
unsafeWrite MVec n (PrimState m) a
v Int
i a
a m () -> m () -> m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> MVec n (PrimState m) b -> Int -> b -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> Int -> a -> m ()
unsafeWrite MVec n (PrimState m) b
w Int
i b
b m () -> m () -> m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> MVec n (PrimState m) c -> Int -> c -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> Int -> a -> m ()
unsafeWrite MVec n (PrimState m) c
u Int
i c
c
  {-# INLINE unsafeWrite #-}

instance ( Arity n
         , Vector  (Vec n) a, Vector  (Vec n) b, Vector  (Vec n) c
         , IVector (Vec n) a, IVector (Vec n) b, IVector (Vec n) c
         ) => IVector (Vec n) (a,b,c) where
  unsafeFreeze :: Mutable (Vec n) (PrimState m) (a, b, c) -> m (Vec n (a, b, c))
unsafeFreeze (MV_3 v w u) = do Vec n a
as <- Mutable (Vec n) (PrimState m) a -> m (Vec n a)
forall (v :: * -> *) a (m :: * -> *).
(IVector v a, PrimMonad m) =>
Mutable v (PrimState m) a -> m (v a)
unsafeFreeze Mutable (Vec n) (PrimState m) a
MVec n (PrimState m) a
v
                                 Vec n b
bs <- Mutable (Vec n) (PrimState m) b -> m (Vec n b)
forall (v :: * -> *) a (m :: * -> *).
(IVector v a, PrimMonad m) =>
Mutable v (PrimState m) a -> m (v a)
unsafeFreeze Mutable (Vec n) (PrimState m) b
MVec n (PrimState m) b
w
                                 Vec n c
cs <- Mutable (Vec n) (PrimState m) c -> m (Vec n c)
forall (v :: * -> *) a (m :: * -> *).
(IVector v a, PrimMonad m) =>
Mutable v (PrimState m) a -> m (v a)
unsafeFreeze Mutable (Vec n) (PrimState m) c
MVec n (PrimState m) c
u
                                 Vec n (a, b, c) -> m (Vec n (a, b, c))
forall (m :: * -> *) a. Monad m => a -> m a
return (Vec n (a, b, c) -> m (Vec n (a, b, c)))
-> Vec n (a, b, c) -> m (Vec n (a, b, c))
forall a b. (a -> b) -> a -> b
$ Vec n a -> Vec n b -> Vec n c -> Vec n (a, b, c)
forall (n :: Nat) a b c.
Vec n a -> Vec n b -> Vec n c -> Vec n (a, b, c)
V_3 Vec n a
as Vec n b
bs Vec n c
cs
  {-# INLINE unsafeFreeze #-}
  unsafeThaw :: Vec n (a, b, c) -> m (Mutable (Vec n) (PrimState m) (a, b, c))
unsafeThaw   (V_3  v w u) = do MVec n (PrimState m) a
as <- Vec n a -> m (Mutable (Vec n) (PrimState m) a)
forall (v :: * -> *) a (m :: * -> *).
(IVector v a, PrimMonad m) =>
v a -> m (Mutable v (PrimState m) a)
unsafeThaw Vec n a
v
                                 MVec n (PrimState m) b
bs <- Vec n b -> m (Mutable (Vec n) (PrimState m) b)
forall (v :: * -> *) a (m :: * -> *).
(IVector v a, PrimMonad m) =>
v a -> m (Mutable v (PrimState m) a)
unsafeThaw Vec n b
w
                                 MVec n (PrimState m) c
cs <- Vec n c -> m (Mutable (Vec n) (PrimState m) c)
forall (v :: * -> *) a (m :: * -> *).
(IVector v a, PrimMonad m) =>
v a -> m (Mutable v (PrimState m) a)
unsafeThaw Vec n c
u
                                 MVec n (PrimState m) (a, b, c)
-> m (MVec n (PrimState m) (a, b, c))
forall (m :: * -> *) a. Monad m => a -> m a
return (MVec n (PrimState m) (a, b, c)
 -> m (MVec n (PrimState m) (a, b, c)))
-> MVec n (PrimState m) (a, b, c)
-> m (MVec n (PrimState m) (a, b, c))
forall a b. (a -> b) -> a -> b
$ MVec n (PrimState m) a
-> MVec n (PrimState m) b
-> MVec n (PrimState m) c
-> MVec n (PrimState m) (a, b, c)
forall (n :: Nat) s a b c.
MVec n s a -> MVec n s b -> MVec n s c -> MVec n s (a, b, c)
MV_3 MVec n (PrimState m) a
as MVec n (PrimState m) b
bs MVec n (PrimState m) c
cs
  {-# INLINE unsafeThaw   #-}
  unsafeIndex :: Vec n (a, b, c) -> Int -> (a, b, c)
unsafeIndex  (V_3 v w u) Int
i
    = (Vec n a -> Int -> a
forall (v :: * -> *) a. IVector v a => v a -> Int -> a
unsafeIndex Vec n a
v Int
i, Vec n b -> Int -> b
forall (v :: * -> *) a. IVector v a => v a -> Int -> a
unsafeIndex Vec n b
w Int
i, Vec n c -> Int -> c
forall (v :: * -> *) a. IVector v a => v a -> Int -> a
unsafeIndex Vec n c
u Int
i)
  {-# INLINE unsafeIndex  #-}


----------------------------------------------------------------
-- Newtype wrappers

newtype instance MVec n s (Const a b) = MV_Const (MVec n s a)
newtype instance Vec  n   (Const a b) = V_Const  (Vec  n   a)
instance Unbox n a => Unbox n (Const a b)

instance (Unbox n a) => MVector (MVec n) (Const a b) where
  new :: m (MVec n (PrimState m) (Const a b))
new                                  = MVec n (PrimState m) a -> MVec n (PrimState m) (Const a b)
forall k (n :: Nat) s a (b :: k).
MVec n s a -> MVec n s (Const a b)
MV_Const (MVec n (PrimState m) a -> MVec n (PrimState m) (Const a b))
-> m (MVec n (PrimState m) a)
-> m (MVec n (PrimState m) (Const a b))
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` m (MVec n (PrimState m) a)
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
m (v (PrimState m) a)
new
  copy :: MVec n (PrimState m) (Const a b)
-> MVec n (PrimState m) (Const a b) -> m ()
copy (MV_Const v) (MV_Const w)       = MVec n (PrimState m) a -> MVec n (PrimState m) a -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> v (PrimState m) a -> m ()
copy MVec n (PrimState m) a
v MVec n (PrimState m) a
w
  move :: MVec n (PrimState m) (Const a b)
-> MVec n (PrimState m) (Const a b) -> m ()
move (MV_Const v) (MV_Const w)       = MVec n (PrimState m) a -> MVec n (PrimState m) a -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> v (PrimState m) a -> m ()
move MVec n (PrimState m) a
v MVec n (PrimState m) a
w
  unsafeRead :: MVec n (PrimState m) (Const a b) -> Int -> m (Const a b)
unsafeRead  (MV_Const v) Int
i           = a -> Const a b
forall k a (b :: k). a -> Const a b
Const (a -> Const a b) -> m a -> m (Const a b)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` MVec n (PrimState m) a -> Int -> m a
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> Int -> m a
unsafeRead MVec n (PrimState m) a
v Int
i
  unsafeWrite :: MVec n (PrimState m) (Const a b) -> Int -> Const a b -> m ()
unsafeWrite (MV_Const v) Int
i (Const a
x) = MVec n (PrimState m) a -> Int -> a -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> Int -> a -> m ()
unsafeWrite MVec n (PrimState m) a
v Int
i a
x
  {-# INLINE new         #-}
  {-# INLINE move        #-}
  {-# INLINE copy        #-}
  {-# INLINE unsafeRead  #-}
  {-# INLINE unsafeWrite #-}

instance (Unbox n a) => IVector (Vec n) (Const a b) where
  unsafeFreeze :: Mutable (Vec n) (PrimState m) (Const a b) -> m (Vec n (Const a b))
unsafeFreeze (MV_Const v)   = Vec n a -> Vec n (Const a b)
forall k (n :: Nat) a (b :: k). Vec n a -> Vec n (Const a b)
V_Const  (Vec n a -> Vec n (Const a b))
-> m (Vec n a) -> m (Vec n (Const a b))
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` Mutable (Vec n) (PrimState m) a -> m (Vec n a)
forall (v :: * -> *) a (m :: * -> *).
(IVector v a, PrimMonad m) =>
Mutable v (PrimState m) a -> m (v a)
unsafeFreeze Mutable (Vec n) (PrimState m) a
MVec n (PrimState m) a
v
  unsafeThaw :: Vec n (Const a b) -> m (Mutable (Vec n) (PrimState m) (Const a b))
unsafeThaw   (V_Const  v)   = MVec n (PrimState m) a -> MVec n (PrimState m) (Const a b)
forall k (n :: Nat) s a (b :: k).
MVec n s a -> MVec n s (Const a b)
MV_Const (MVec n (PrimState m) a -> MVec n (PrimState m) (Const a b))
-> m (MVec n (PrimState m) a)
-> m (MVec n (PrimState m) (Const a b))
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` Vec n a -> m (Mutable (Vec n) (PrimState m) a)
forall (v :: * -> *) a (m :: * -> *).
(IVector v a, PrimMonad m) =>
v a -> m (Mutable v (PrimState m) a)
unsafeThaw   Vec n a
v
  unsafeIndex :: Vec n (Const a b) -> Int -> Const a b
unsafeIndex  (V_Const  v) Int
i = a -> Const a b
forall k a (b :: k). a -> Const a b
Const (Vec n a -> Int -> a
forall (v :: * -> *) a. IVector v a => v a -> Int -> a
unsafeIndex Vec n a
v Int
i)
  {-# INLINE unsafeFreeze #-}
  {-# INLINE unsafeThaw   #-}
  {-# INLINE unsafeIndex  #-}


----------------------------------------------------------------
-- Newtype wrappers with kind * -> *

#define primNewMV(ty,con)                         \
instance Unbox n a => MVector (MVec n) (ty a) where {     \
; new = con `liftM` new                             \
; copy (con v) (con w) = copy v w                   \
; move (con v) (con w) = move v w                   \
; unsafeRead  (con v) i = ty `liftM` unsafeRead v i            \
; unsafeWrite (con v) i (ty x) = unsafeWrite v i x       \
; {-# INLINE new         #-}                        \
; {-# INLINE move        #-}                        \
; {-# INLINE copy        #-}                        \
; {-# INLINE unsafeRead  #-}                        \
; {-# INLINE unsafeWrite #-}                        \
}

#define primNewIV(ty,con,mcon)                             \
instance Unbox n a => IVector (Vec n) (ty a)  where {          \
; unsafeFreeze (mcon v)   = con  `liftM` unsafeFreeze v \
; unsafeThaw   (con  v)   = mcon `liftM` unsafeThaw   v \
; unsafeIndex  (con  v) i = ty (unsafeIndex v i)             \
; {-# INLINE unsafeFreeze #-}                           \
; {-# INLINE unsafeThaw   #-}                           \
; {-# INLINE unsafeIndex  #-}                           \
}

#define primNewWrap(ty,con,mcon) \
newtype instance MVec n s (ty a) = mcon (MVec n s a) ; \
newtype instance Vec  n   (ty a) = con  (Vec  n   a) ; \
instance Unbox n a => Unbox n (ty a) ; \
primNewMV(ty, mcon     )          ; \
primNewIV(ty, con, mcon)


primNewWrap(Identity, V_Identity, MV_Identity)
primNewWrap(Down, V_Down, MV_Down)
primNewWrap(Dual, V_Dual, MV_Dual)
primNewWrap(Sum, V_Sum, MV_Sum)
primNewWrap(Product, V_Product, MV_Product)


----------------------------------------------------------------
-- Monomorphic newtype wrappers

#define primNewMonoMV(ty,con)                         \
instance Arity n => MVector (MVec n) ty where {     \
; new = con `liftM` new                             \
; copy (con v) (con w) = copy v w                   \
; move (con v) (con w) = move v w                   \
; unsafeRead  (con v) i = ty `liftM` unsafeRead v i            \
; unsafeWrite (con v) i (ty x) = unsafeWrite v i x       \
; {-# INLINE new         #-}                        \
; {-# INLINE move        #-}                        \
; {-# INLINE copy        #-}                        \
; {-# INLINE unsafeRead  #-}                        \
; {-# INLINE unsafeWrite #-}                        \
}

#define primNewMonoIV(ty,con,mcon)                             \
instance Arity n => IVector (Vec n) ty where {          \
; unsafeFreeze (mcon v)   = con  `liftM` unsafeFreeze v \
; unsafeThaw   (con  v)   = mcon `liftM` unsafeThaw   v \
; unsafeIndex  (con  v) i = ty (unsafeIndex v i)             \
; {-# INLINE unsafeFreeze #-}                           \
; {-# INLINE unsafeThaw   #-}                           \
; {-# INLINE unsafeIndex  #-}                           \
}

#define primNewMonoWrap(ty,repr,con,mcon) \
newtype instance MVec n s ty = mcon (MVec n s repr) ; \
newtype instance Vec  n   ty = con  (Vec  n   repr) ; \
instance Arity n => Unbox n ty ; \
primNewMonoMV(ty, mcon     )          ; \
primNewMonoIV(ty, con, mcon)

primNewMonoWrap(Any, Bool, V_Any, MV_Any)
primNewMonoWrap(All, Bool, V_All, MV_All)