Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create an unbox instance of an ADT?

I'm having trouble finding good resources that work for how to make my data types unboxed, for use in an unboxed vector. How would I make the data type

data Color = Yellow | Red | Green | Blue | Empty deriving (Show, Eq)

be an instance of Unbox?

Edit: after poking around a bit more, it seems that by forcing paramaters in some functions to be strict, I can convince GHC to unbox them automatically. If this applicable in my case? How do I know which paramaters to make strict?

like image 218
Drew Avatar asked Nov 28 '12 07:11

Drew


1 Answers

You can use the vector-th-unbox package to derive the instance for you. You just need to provide conversion functions to and from some existing Unbox type:

colorToWord8 :: Color -> Word8
colorToWord8 = ...

word8ToColor :: Word8 -> Color
word8ToColor = ...

derivingUnbox "Color"
  [t| Color -> Word8 |]
  colorToWord8
  word8ToColor
like image 68
hammar Avatar answered Sep 28 '22 17:09

hammar