derive-topdown 0.0.0.1 → 0.0.0.2
raw patch · 5 files changed
+54/−52 lines, 5 files
Files
- README.md +1/−1
- derive-topdown.cabal +1/−1
- src/Data/Derive/TopDown/Derive.hs +24/−20
- src/Data/Derive/TopDown/Generic.hs +28/−26
- src/Data/Derive/TopDown/Utils.hs +0/−4
README.md view
@@ -100,7 +100,7 @@ We have to specify makeEq or other Derivation values here and I am not sure how to eleminate it. -Solution is is to use standalone deriving. it will be supposrt in GHC 7.10 standalone deriving will be supported and you do not need to write. Currently unimplemented. +Solution 2 is is to use standalone deriving. it will be supposrt in GHC 7.10 standalone deriving will be supported and you do not need to write. *Currently unimplemented*. deriving (Data, Typeable, Generic, Ord,Eq,Show)
derive-topdown.cabal view
@@ -2,7 +2,7 @@ -- documentation, see http://haskell.org/cabal/users-guide/ name: derive-topdown -version: 0.0.0.1 +version: 0.0.0.2 synopsis: This library will help you generate Haskell empty Generic instances and deriving type instances from the top automatically to the bottom for composited data types. -- description: homepage: https://github.com/HaskellZhangSong/derive-topdown
src/Data/Derive/TopDown/Derive.hs view
@@ -1,28 +1,12 @@-{-# LANGUAGE TemplateHaskell , QuasiQuotes, RankNTypes #-} - -module Data.Derive.TopDown.Derive ( -derivings -) where - -import Data.Derive.TopDown.Utils -import Language.Haskell.TH -import Language.Haskell.TH.Utils - -import Control.Monad (forM) -import Data.List (foldl') -import Control.Monad.State -import Control.Monad.Trans (lift) -import Debug.Trace -import qualified Language.Haskell.TH.Syntax as S -import Data.DeriveTH - --- | deriving from top {-| + > data A a b = A a (B b) deriving (Show) > data B a = B a deriving (Show) > > derivings ''Eq makeEq ''A + If you enable -ddump-splices, you will get: + > > Data\Derive\TopDown\Test.hs:1:1: Splicing declarations > derives ''Eq makeEq ''A @@ -38,6 +22,26 @@ For now, you have to specify both of ''Eq and makeEq, I suppose ''Eq will be enough. To look what typeclasses you can derive, see 'derive' library on hackage. -} + +{-# LANGUAGE TemplateHaskell , QuasiQuotes, RankNTypes #-} + +module Data.Derive.TopDown.Derive ( +derivings +) where + +import Data.Derive.TopDown.Utils +import Language.Haskell.TH +import Language.Haskell.TH.Utils + +import Control.Monad (forM) +import Data.List (foldl') +import Control.Monad.State +import Control.Monad.Trans (lift) +import Debug.Trace +import qualified Language.Haskell.TH.Syntax as S +import Data.DeriveTH + +-- | deriving from top derivings :: Name -> Derivation -> Name -> Q [Dec] derivings className dv typeName = (fmap fst ((runStateT $ gen className typeName dv) [])) @@ -86,7 +90,7 @@ let makeClassName = mkName $ "make" ++ nameBase cla let tpname = nameBase tp dec <- lift $ appExp [(varE (mkName "derive")), (varE makeClassName), (varE tp)] - -- ****** how to splice [Exp] to [Dec] ?! + -- how to splice [Exp] to [Dec] ?! lift [| derive $(varE makeClassName) tp |] modify (instanceType:) let names = concatMap getCompositeType cons
src/Data/Derive/TopDown/Generic.hs view
@@ -1,42 +1,33 @@-{-# LANGUAGE TemplateHaskell #-} -module Data.Derive.TopDown.Generic ((-->),instances, instanceList) where - -import Data.List (foldl') -import Control.Monad.State -import Control.Monad.Trans (lift) -import Language.Haskell.TH -import Data.Derive.TopDown.Utils -import Language.Haskell.TH.Utils {-| An example to generate Out class for Person, Name and Address. Out class has to provide a default implementation for the function it declears. -@ -data Person = Person Names Address - | Student Names Address - deriving (Show, Generic, Eq, Ord , Data,Typeable) -data Names = Names String - deriving (Show, Generic, Eq, Ord, Data, Typeable) -data Address = Address Gate - deriving (Show, Generic, Eq, Ord, Typeable, Data) - -type Gate = PF - -data PF = PF String deriving (Data, Typeable, Generic, Ord,Eq,Show) -@ +> data Person = Person Names Address +> | Student Names Address +> deriving (Show, Generic, Eq, Ord , Data,Typeable) +> data Names = Names String +> deriving (Show, Generic, Eq, Ord, Data, Typeable) +> data Address = Address Gate +> deriving (Show, Generic, Eq, Ord, Typeable, Data) +> +> type Gate = PF +> +> data PF = PF String deriving (Data, Typeable, Generic, Ord,Eq,Show) For generating 4 empty instances + > instance Out Person > instnace Out Nmads > instance Out Address > instance Out Gate you just write: -@ -instances ''Out ''Person -@ + +> instances ''Out ''Person + It will generate all instances that form Person and including Person. If you use :set -ddump-splices, you will get + > instances ''Out ''Person > ======> > ~\Test.hs:13:1-18 @@ -45,10 +36,21 @@ > instance Out Address > instance Out Person > Ok, modules loaded: CompositeDataInstancesGen, Main. + You can also use instnaceList to generate a list of class. -} --- | synatx sugar +{-# LANGUAGE TemplateHaskell #-} +module Data.Derive.TopDown.Generic ((-->),instances, instanceList) where + +import Data.List (foldl') +import Control.Monad.State +import Control.Monad.Trans (lift) +import Language.Haskell.TH +import Data.Derive.TopDown.Utils +import Language.Haskell.TH.Utils + +-- | Synatx sugar instances = deriveInstances (-->) = deriveInstances
src/Data/Derive/TopDown/Utils.hs view
@@ -26,10 +26,6 @@ getCxtTyVarCons' :: Type -> Q (Cxt, [TyVarBndr], [Con]) getCxtTyVarCons'(ConT t) = undefined --return ([],[],[NormalC]) -{-| -applyContext ''Eq [''a, ''b] will give (Eq a, Eq b) class constraint --} - applyContext :: Name -> [Name] -> Q [Pred] applyContext con typeNames = return (map apply typeNames) where apply t = ClassP con [VarT t]