I want to use unboxed vectors on a simple newtype, but it's not clear to me how to enable this. What I have so far:
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeFamilies #-}
module UnboxedTest where
import Data.Vector.Unboxed
import Data.Word
newtype X = X Word64 deriving (Unbox)
I get:
src/UnboxedTest.hs:11:32: error:
• No instance for (Data.Vector.Generic.Base.Vector Vector X)
arising from the 'deriving' clause of a data type declaration
Possible fix:
use a standalone 'deriving instance' declaration,
so you can specify the instance context yourself
• When deriving the instance for (Unbox X)
|
11 | newtype X = X Word64 deriving (Unbox)
How do I enable this to be put inside an unboxed vector?
It is pretty simple if there is an isomorphic type that can already be stored inside unboxed vectors. For
Pointthat is(Word64, Word64).You have to do as that documentation page says and write:
These declarations specify how your
Pointcan be converted to a(Word64, Word64)to be stored in the unboxed vector. It becomes slightly more obvious that these are not completely redundant if you look at for example thebasicUnsafeWriteandbasicUnsafeIndexMfunction where you can see that you actually do need to write some code to convertPointto(Word64, Word64)and back.