newtype-th 0.3.1 → 0.3.2
raw patch · 2 files changed
+28/−17 lines, 2 filesdep ~haskell-src-metadep ~newtypedep ~template-haskell
Dependency ranges changed: haskell-src-meta, newtype, template-haskell
Files
- Control/Newtype/TH.hs +22/−11
- newtype-th.cabal +6/−6
Control/Newtype/TH.hs view
@@ -23,7 +23,7 @@ -- ----------------------------------------------------------------------------- -module Control.Newtype.TH ( mkNewTypes ) where+module Control.Newtype.TH ( mkNewType, mkNewTypes ) where import Control.Newtype ( Newtype(pack, unpack) ) @@ -40,28 +40,35 @@ import Language.Haskell.TH import Language.Haskell.Meta.Utils (conName, conTypes) +-- | Derive a single instance of @Newtype@.+mkNewType :: Name -> Q [Dec]+mkNewType = mkNewTypes . (:[])+ -- | Derive instances of @Newtype@, specified as a list of references -- to newtypes. mkNewTypes :: [Name] -> Q [Dec]-mkNewTypes = mapM (\n -> rewriteFamilies =<< mkInstH n <$> reify n)+mkNewTypes = mapM (\n -> rewriteFamilies =<< mkInst <$> reify n) where+ mkInst (TyConI (NewtypeD a b c d _)) = mkInstFor a b c d+ mkInst (TyConI (DataD a b c [d] _)) = mkInstFor a b c d+ mkInst x+ = error $ show x+ ++ " is not a Newtype or single-field single-constructor datatype."+ --Construct the instance declaration -- "instance Newtype (<newtype> a ...) (<field type> a ...) where"- mkInstH name (TyConI (NewtypeD context _ vs con _))+ mkInstFor context name bnds con = InstanceD context ( foldl1 AppT [ ConT ''Newtype- , bndrsToType (ConT name) vs+ , bndrsToType (ConT name) bnds , head $ conTypes con ] )- [ FunD 'pack [ Clause [] - (NormalB $ ConE cname) [] ]- , FunD 'unpack [ Clause [ConP cname [VarP xname]] - (NormalB $ VarE xname) [] ]+ [ FunD 'pack [Clause [] (NormalB $ ConE cn) []]+ , FunD 'unpack [Clause [ConP cn [VarP xn]] (NormalB $ VarE xn) []] ] where- cname = conName con- xname = mkName "x"- mkInstH name _ = error $ show name ++ " is not a Newtype"+ cn = conName con+ xn = mkName "x" -- Given a root type and a list of type variables, converts for use as -- parameters to the newtype's type in the instance head.@@ -89,7 +96,11 @@ -- refer to it, along with the cannonical reified name and the passed -- type. justFamily :: (Name, Type, Info) -> Maybe (Name, (Name, Type))+#if __GLASGOW_HASKELL__ >= 704+ justFamily (n, t, FamilyI (FamilyD _ n' _ _) _) = Just (n, (n', t))+#else justFamily (n, t, TyConI (FamilyD _ n' _ _)) = Just (n, (n', t))+#endif justFamily _ = Nothing -- Merges all of the identical applications of the family constructor.
newtype-th.cabal view
@@ -1,5 +1,5 @@ Name: newtype-th-Version: 0.3.1+Version: 0.3.2 Synopsis: A template haskell deriver to create Control.Newtype instances. Description: Provides a template haskell based mechanism for deriving instances of djahandarie's Control.Newtype class.@@ -20,11 +20,11 @@ Location: git://github.com/mgsloan/newtype-th Library- Extensions: TemplateHaskell+ Extensions: CPP, TemplateHaskell Exposed-modules: Control.Newtype.TH- Build-depends: base >= 3.0 && < 6, - haskell-src-meta, - newtype, + Build-depends: base >= 3.0 && < 6,+ haskell-src-meta >= 0.0.4 && < 0.6,+ newtype >= 0.1 && < 0.3, syb,- template-haskell+ template-haskell >= 2.4 && < 2.8 Ghc-options: -Wall