th-lift 0.4 → 0.5
raw patch · 4 files changed
+43/−22 lines, 4 filesdep ~base
Dependency ranges changed: base
Files
- Changelog +7/−0
- Language/Haskell/TH/Lift.hs +25/−15
- t/Foo.hs +1/−1
- th-lift.cabal +10/−6
Changelog view
@@ -1,3 +1,10 @@+2010-08-05 Mathieu Boespflug <mboes@tweag.net>++ * Support for contexts in datatypes, thanks to Ben Millwood.+ * deriveLiftWith becomes deriveLift' and takes an Info structure+ rather than a custom reification function.+ * Add deriveLiftMany to derive many Lift instances in one go.+ 2010-08-02 Mathieu Boespflug <mboes@tweag.net> * Add support for newtypes and records syntax, thanks to a patch
Language/Haskell/TH/Lift.hs view
@@ -1,33 +1,43 @@-module Language.Haskell.TH.Lift (deriveLift, deriveLiftWith, Lift(..)) where+module Language.Haskell.TH.Lift (deriveLift, deriveLiftMany, deriveLift', deriveLiftMany', Lift(..)) where import GHC.Exts import Language.Haskell.TH import Language.Haskell.TH.Syntax+import Control.Monad ((<=<)) modName :: String modName = "Language.Haskell.TH.Lift" -- | Derive Lift instances for the given datatype. deriveLift :: Name -> Q [Dec]-deriveLift = deriveLiftWith reify+deriveLift = deriveLift' <=< reify +-- | Derive Lift instances for many datatypes.+deriveLiftMany :: [Name] -> Q [Dec]+deriveLiftMany = deriveLiftMany' <=< mapM reify+ -- | Obtain Info values through a custom reification function. This is useful -- when generating instances for datatypes that have not yet been declared.-deriveLiftWith :: (Name -> Q Info) -> Name -> Q [Dec]-deriveLiftWith n = do- i <- reify n+deriveLift' :: Info -> Q [Dec]+deriveLift' = fmap (:[]) . deriveLiftOne++deriveLiftMany' :: [Info] -> Q [Dec]+deriveLiftMany' = mapM deriveLiftOne++deriveLiftOne :: Info -> Q Dec+deriveLiftOne i = case i of- TyConI (DataD _ _ vsk cons _) ->- liftInstance (map unTyVarBndr vsk) (map doCons cons)- TyConI (NewtypeD _ _ vsk con _) ->- liftInstance (map unTyVarBndr vsk) [doCons con]+ TyConI (DataD dcx n vsk cons _) ->+ liftInstance dcx n (map unTyVarBndr vsk) (map doCons cons)+ TyConI (NewtypeD dcx n vsk con _) ->+ liftInstance dcx n (map unTyVarBndr vsk) [doCons con] _ -> error (modName ++ ".deriveLift: unhandled: " ++ pprint i)- where liftInstance vs cases =- fmap (:[]) $ instanceD (ctxt vs) (conT ''Lift `appT` typ vs) [funD 'lift cases]- unTyVarBndr (PlainTV v) = v- unTyVarBndr (KindedTV v _) = v- ctxt = cxt . map (\n -> classP ''Lift [varT n])- typ = foldl appT (conT n) . map varT+ where liftInstance dcx n vs cases =+ instanceD (ctxt dcx vs) (conT ''Lift `appT` typ n vs) [funD 'lift cases]+ unTyVarBndr (PlainTV v) = v+ unTyVarBndr (KindedTV v _) = v+ ctxt dcx = fmap (dcx ++) . cxt . map (\n -> classP ''Lift [varT n])+ typ n = foldl appT (conT n) . map varT doCons :: Con -> Q Clause doCons (NormalC c sts) = do
t/Foo.hs view
@@ -3,7 +3,7 @@ import Language.Haskell.TH.Lift -data Foo a = Foo a Char | Bar a+data (Eq a) => Foo a = Foo a Char | Bar a deriving Show newtype Rec a = Rec { field :: a }
th-lift.cabal view
@@ -1,6 +1,6 @@ Name: th-lift-Version: 0.4-Cabal-Version: >= 1.3+Version: 0.5+Cabal-Version: >= 1.6 License: OtherLicense License-File: COPYING Copyright: Ian Lynagh, 2006@@ -11,10 +11,14 @@ Derive Template Haskell's Lift class for datatypes. Category: Language Tested-With: GHC==6.12-Build-Depends: base >= 4 && < 5, template-haskell >= 2.4-Extensions: TemplateHaskell, MagicHash, TypeSynonymInstances build-type: Simple Extra-source-files: BSD3, GPL-2, Changelog, t/Foo.hs, t/Test.hs-Exposed-modules:- Language.Haskell.TH.Lift +Library+ Build-Depends: base >= 4 && < 5, template-haskell >= 2.4 && < 2.5+ Exposed-modules: Language.Haskell.TH.Lift+ Extensions: TemplateHaskell, MagicHash, TypeSynonymInstances++source-repository head+ type: git+ location: git://github.com/mboes/th-lift