vector-th-unbox 0.2.1.0 → 0.2.1.1
raw patch · 3 files changed
+44/−34 lines, 3 files
Files
- Data/Vector/Unboxed/Deriving.hs +40/−31
- tests/sanity.hs +2/−1
- vector-th-unbox.cabal +2/−2
Data/Vector/Unboxed/Deriving.hs view
@@ -16,46 +16,22 @@ Maintainer: vector-th-unbox@liyang.hu Stability: experimental Portability: non-portable--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-<http://hackage.haskell.org/package/vector/docs/Data-Vector-Unboxed.html example>-from the @vector@ package represents @Complex a@ as pairs @(a, a)@. 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. Note that GHC 7.4-(but not earlier nor later) needs the 'G.Vector' and 'M.MVector' class-method names to be in scope in order to define the appropriate instances:-->#if __GLASGOW_HASKELL__ == 704->import qualified Data.Vector.Generic->import qualified Data.Vector.Generic.Mutable->#endif--Consult the <https://github.com/liyang/vector-th-unbox/blob/master/tests/sanity.hs sanity test>-for a working example. -} -module Data.Vector.Unboxed.Deriving (derivingUnbox) where+module Data.Vector.Unboxed.Deriving+ ( -- $usage+ derivingUnbox+ ) where import Control.Arrow import Control.Applicative import Control.Monad+import Data.Char (isAlphaNum) import qualified Data.Vector.Generic as G import qualified Data.Vector.Generic.Mutable as M import Data.Vector.Unboxed.Base (MVector (..), Vector (..), Unbox) import Language.Haskell.TH -consUnbox :: String -> (Name, Name)-consUnbox name = (mkName $ "MV_" ++ name, mkName $ "V_" ++ name)- -- Create a @Pat@ bound to the given name and an @Exp@ for said binding. newPatExp :: String -> Q (Pat, Exp) newPatExp = fmap (VarP &&& VarE) . newName@@ -66,7 +42,10 @@ common :: String -> Q Common common name = do- let (mvName, vName) = consUnbox name+ -- A bit looser than “Haskell 2010: §2.4 Identifiers and Operators”…+ unless (all (\ c -> c == '\'' || c == '#' || isAlphaNum c) name) $ do+ fail (show name ++ " is not a valid constructor suffix!")+ let (mvName, vName) = (mkName $ "MV_" ++ name, mkName $ "V_" ++ name) i <- newPatExp "idx" n <- newPatExp "len" mv <- first (ConP mvName . (:[])) <$> newPatExp "mvec"@@ -108,7 +87,7 @@ constraint. Thus: >derivingUnbox "Maybe"-> [t| (Default a, Unbox a) ⇒ Maybe a → (Bool, a) |]+> [t| ∀ a. (Default a, Unbox a) ⇒ Maybe a → (Bool, a) |] > [| maybe (False, def) (\ x → (True, x)) |] > [| \ (b, x) → if b then Just x else Nothing |] -}@@ -164,4 +143,34 @@ return [ InstanceD cxts (ConT ''Unbox `AppT` typ) [] , newtypeMVector, instanceMVector , newtypeVector, instanceVector ]++#undef __GLASGOW_HASKELL__+{-$usage++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+<http://hackage.haskell.org/package/vector/docs/Data-Vector-Unboxed.html example>+from the @vector@ package represents @Complex a@ as pairs @(a, a)@. Using+'derivingUnbox', we can define the same instances much more succinctly:++>derivingUnbox "Complex"+> [t| ∀ a. (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. Note that GHC 7.4+(but not earlier nor later) needs the 'G.Vector' and 'M.MVector' class+method names to be in scope in order to define the appropriate instances:++>#if __GLASGOW_HASKELL__ == 704+>import qualified Data.Vector.Generic+>import qualified Data.Vector.Generic.Mutable+>#endif++Consult the <https://github.com/liyang/vector-th-unbox/blob/master/tests/sanity.hs sanity test>+for a working example.++-}
tests/sanity.hs view
@@ -1,5 +1,6 @@ {-# LANGUAGE CPP #-} {-# LANGUAGE MultiParamTypeClasses #-}+{-# LANGUAGE RankNTypes #-} {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE UnicodeSyntax #-}@@ -17,7 +18,7 @@ import Data.Vector.Unboxed.Deriving derivingUnbox "Maybe"- [t| (Default a, Unbox a) ⇒ Maybe a → (Bool, a) |]+ [t| ∀ a. (Default a, Unbox a) ⇒ Maybe a → (Bool, a) |] [| maybe (False, def) (\ x → (True, x)) |] [| \ (b, x) → if b then Just x else Nothing |]
vector-th-unbox.cabal view
@@ -1,5 +1,5 @@ name: vector-th-unbox-version: 0.2.1.0+version: 0.2.1.1 synopsis: Deriver for Data.Vector.Unboxed using Template Haskell description: A Template Haskell deriver for unboxed vectors, given a pair of coercion@@ -26,7 +26,7 @@ Data.Vector.Unboxed.Deriving build-depends:- base >= 4.3 && < 5,+ base >= 4.5 && < 5, template-haskell >= 2.5, vector >= 0.7