| Portability | non-portable |
|---|---|
| Stability | experimental |
| Maintainer | vector-th-unbox@liyang.hu |
| Safe Haskell | Trustworthy |
Data.Vector.Unboxed.Deriving
Description
Writing Unbox instances for new data types is tedious and formulaic. More
often than not, there is a straightforward mapping of the new type onto some
existing one already imbued with an Unbox instance. The example from the
vector package represents Complex a as pairs (a, a). (See
http://hackage.haskell.org/packages/archive/vector/latest/doc/html/Data-Vector-Unboxed.html.)
Using derivingUnbox, we can define the same instances much more
succinctly:
derivingUnbox "Complex"
[t| (Unbox a) ⇒ Complex a → (a, a) |]
[| \ (r :+ i) → (r, i) |]
[| \ (r, i) → r :+ i |]
Requires the MultiParamTypeClasses, TemplateHaskell, TypeFamilies and
probably the FlexibleInstances LANGUAGE extensions. Older versions of
GHC needs the Vector and MVector class method names in scope:
import qualified Data.Vector.Generic import qualified Data.Vector.Generic.Mutable
Documentation
Arguments
| :: String | Unique constructor suffix for the MVector and Vector data families |
| -> TypeQ | Quotation of the form |
| -> ExpQ | Quotation of an expression of type |
| -> ExpQ | Quotation of an expression of type |
| -> DecsQ | Declarations to be spliced for the derived Unbox instance |
Let's consider a more complex example: suppose we want an Unbox
instance for Maybe a. We can encode this using the pair (Bool, a), with
the boolean indicating whether we have Nothing or Just something. This
encoding requires a dummy value in the Nothing case, necessitating an
additional Default (see the data-default package) constraint. Thus:
derivingUnbox "Maybe"
[t| (Default a, Unbox a) ⇒ Maybe a → (Bool, a) |]
[| maybe (False, def) (\ x → (True, x)) |]
[| \ (b, x) → if b then Just x else Nothing |]