I'm having some trouble defining the type signature when using the generic vector interface. I want to construct a method that can work on both boxed and unboxed vectors.
This works, but is constrained to boxed vectors:
import Control.Monad.ST
import Data.Vector as V
import Data.Vector.Generic as VG
import Data.Vector.Generic.Mutable as VGM
import Data.Vector.Mutable as VM
import Data.Vector.Unboxed as VU
import Data.Vector.Unboxed.Mutable as VUM
mySwap :: V.Vector a -> V.Vector a
mySwap vec = runST $ do
vec_mut <- V.thaw vec
VM.swap vec_mut 0 1
V.unsafeFreeze vec_mut
If I change V to VG in order to use the generic interface, then two arguments are needed for the vector in the type signature, but I'm not sure how to constrain the first argument:
mySwap2 :: VG.Vector v a -> VG.Vector v a
mySwap2 vec = runST $ do
vec_mut <- VG.thaw vec
VGM.swap vec_mut 0 1
VG.unsafeFreeze vec_mut
Expected a type, but
VG.Vector v ahas kindghc-prim-.4.0.0:GHC.Prim.Constraint