packages feed

derive-topdown 0.0.0.9 → 0.0.1.0

raw patch · 9 files changed

+38/−18 lines, 9 filesdep +GenericPrettydep +simple-sql-parserPVP ok

version bump matches the API change (PVP)

Dependencies added: GenericPretty, simple-sql-parser

API changes (from Hackage documentation)

Files

ChangeLog.md view
@@ -1,5 +1,9 @@ derive-topdown ======+derive-topdown-0.0.1.0+--------------+- `0.0.1.0`+    * Imporve error message.Will print the types list when type is not data or newtype defined.  derive-topdown-0.0.0.9 --------------
README.md view
@@ -92,7 +92,7 @@  ### 4. Deriving with strategies in GHC 8.2 If you want to specify the strategy for deriving mechanism then `strategy_deriving`, `strategy_derivings` and `strategy_derivingss` can be used.-The 3 strategies for deriving `StockStrategy`,`AnyclassStrategy`,`NewtypeStrategy` are exposed when you import `TopDown`. They can be write as `stock`, `anyclass` as the default grammar. For `newtype`, you can write it as `newtype_` since there is a clison with `newtype` for data declaration. Please see [DerivingStrategies](https://ghc.haskell.org/trac/ghc/wiki/Commentary/Compiler/DerivingStrategies)+The 3 strategies for deriving `StockStrategy`,`AnyclassStrategy`,`NewtypeStrategy` are exposed when you import `TopDown`. They can be written as `stock`, `anyclass` as the default grammar. For `newtype`, you can write it as `newtype_` since there is a clison with `newtype` for data declaration. Please see [DerivingStrategies](https://ghc.haskell.org/trac/ghc/wiki/Commentary/Compiler/DerivingStrategies)  #### **NOTE**:  About deriving instances of Typeable There is a bug with `isInstance` function when working with Typeable class. See [`ticket #11251`](https://ghc.haskell.org/trac/ghc/ticket/11251). So there might be problems if you really want to derive `Typeable` class. However, this bug should affect you too much here since GHC now has `AutoDeriveTypeable` extension, which means you should never derive `Typeable` manually.
derive-topdown.cabal view
@@ -2,7 +2,7 @@ -- documentation, see http://haskell.org/cabal/users-guide/  name:                derive-topdown-version:             0.0.0.9+version:             0.0.1.0 synopsis:            Help Haskellers derive class instances for composited data types. description:         This package will make it easier to derive class instance for complex composited data types by using Template Haskell. license:             BSD3@@ -59,7 +59,9 @@                 derive>=2.5.0,                 QuickCheck>=2.8.0,                 binary>=0.8.0.0,-                haskell-src>=1.0.2.0+                haskell-src>=1.0.2.0,+                simple-sql-parser>=0.4,+                GenericPretty>=1.2.1                    default-extensions:  TemplateHaskell,CPP   default-language:    Haskell2010
src/Data/Derive/TopDown.hs view
@@ -39,14 +39,16 @@  This will make sense if you have a deep composited data types, nomally an AST of a language. -'instance_' and 'instances' functions will generate empty instances. It is not quite useful with GHC newer 7.10 since they ahve 'DeriveAnyClass' extension. However, with older GHC, it may help. The useage is the same with 'derive' and 'derives'.+'instance_' and 'instances' functions will generate empty instances. It is not quite useful with GHC newer than 7.10 with 'DeriveAnyClass' extension. However, with older GHC, it may help. The useage is the same with 'deriving' and 'derivings'. -For other classes whose instance can only be generated by using a function @Name -> Q [Dec]@ like 'Arbitrary' in 'QuickCheck', the @derive@ package provides @derive makeArbitrary@ function. For doing the top-down derive in these cases, 'deriving_th' and 'deriving_ths' are defined. Other example can the @deriveXXXX@ functions in @Data.Aeson.TH@.+* __NOTE!__ This is not tested with GHC 7.8. +For other classes whose instance can only be generated by using a function with type @Name -> Q [Dec]@ like 'Arbitrary' in 'QuickCheck', the @derive@ package provides @derive makeArbitrary@ function. For doing the top-down derive in these cases, 'deriving_th' and 'deriving_ths' are defined. Other example can the @deriveXXXX@ functions in @Data.Aeson.TH@.+ > deriving_th (''FromJSON, deriveFromJSON defaultOptions) ''Company > deriving_th (''ToJSON, deriveToJSON defaultOptions)     ''Company -However, the poblem could be that the instance context is generated by the template haskell derive function instead mine, so it could be wrong in some circumtances. For example, type with high order type constructor:+However, the poblem could be that the instance context is generated by the template haskell derive function instead mine, so it could be wrong in some circumtances. For example, a type with high order type constructor:  > data T1 k a b = T11 (k a) b | T12 (k (k b)) a b Int @@ -54,7 +56,7 @@  Also, there are some data types which are impossible to be derived as instances of a certain type class. For example, Word cannot be derived as Functor or Generic. Using @derive-topdown@ is the same with hand-written code, so it is your responsiblity to make that right. -* __NOTE!__ @derive-topdown@ works with 'Typeable' type class, but there might be problems. Use @(AutoDeriveTypeable) extension if you want derive @Typeable@@ class. See README and [here](https://ghc.haskell.org/trac/ghc/ticket/11251).+* __NOTE!__ @derive-topdown@ works with 'Typeable' type class, but there might be problems. Use @AutoDeriveTypeable@ extension if you want derive 'Typeable' class. See README and [here](https://ghc.haskell.org/trac/ghc/ticket/11251).  * __NOTE!__ When derive @Generic@ type class, @derive-topdown@ will stop generation on primitive and 'Integer' types. You need to specify the types that deriving @Generic@ class can break with. See the document below. 
src/Data/Derive/TopDown/Instance.hs view
@@ -13,7 +13,7 @@  genEmptyInstanceDecl :: ClassName -> TypeName -> StateT [Type] Q [Dec] genEmptyInstanceDecl cn tn = do-                   (tvbs,cons) <- lift $ getTyVarCons cn tn+                   (tvbs,cons) <- getTyVarCons cn tn                    classContext <- lift $ generateClassContext cn tn                    let typeNames = map getTVBName tvbs                    instanceType <- lift $ foldl' appT (conT tn) $ map varT typeNames
src/Data/Derive/TopDown/Lib.hs view
@@ -12,12 +12,13 @@  ) where  import Language.Haskell.TH-import Language.Haskell.TH.Syntax+import Language.Haskell.TH.Syntax hiding (lift) import Data.Generics (mkT,everywhere,mkQ,everything) import GHC.Exts import Language.Haskell.TH.ExpandSyns (expandSyns) import Data.List (nub,intersect,foldr1)-+import Control.Monad.State+import Control.Monad.Trans #ifdef __GLASGOW_HASKELL__ import Data.Typeable import Data.Data@@ -116,9 +117,9 @@ getContextType name2role (RecGadtC name bangtypes result_type) = fmap concat $ mapM (expandSynsAndGetContextTypes name2role) (map third bangtypes) #endif -getTyVarCons :: ClassName -> TypeName -> Q ([TyVarBndr], [Con])+getTyVarCons :: ClassName -> TypeName -> StateT [Type] Q ([TyVarBndr], [Con]) getTyVarCons cn name = do-            info <- reify name+            info <- lift $ reify name             case info of               TyConI dec -> case dec of #if __GLASGOW_HASKELL__>710@@ -129,7 +130,9 @@                               NewtypeD _ _ tvbs con _-> return (tvbs, [con]) #endif                               TySynD name tvbs t -> error $ show name ++ " is a type synonym and -XTypeSynonymInstances is not supported. If you did not derive it then This is a bug, please report this bug to the author of this package."-                              x -> error $ pprint x ++ " is not a data or newtype definition."+                              x -> do+                                 tys <- get+                                 error $ pprint x ++ " is not a data or newtype definition. " ++ show "Stack: " ++ show tys               _ -> error $ "Cannot generate "++ show cn ++ " instances for " ++ show name  type ClassName = Name@@ -139,7 +142,7 @@ -- See https://ghc.haskell.org/trac/ghc/ticket/13324 generateClassContext :: ClassName -> TypeName -> Q (Maybe Type) generateClassContext classname typename = do-                            (tvbs, cons) <- getTyVarCons classname typename+                            (tvbs, cons) <- (evalStateT $ getTyVarCons classname typename) []                             -- Need to remove phantom types                             roles <- reifyRoles typename                             let varName2Role = zip (map getTVBName tvbs) roles
src/Data/Derive/TopDown/Standalone.hs view
@@ -26,7 +26,7 @@ genStandaloneDerivingDecl :: ClassName -> TypeName -> [TypeName] -> StateT [Type] Q [Dec] genStandaloneDerivingDecl cn tn breaks = do #endif-                   (tvbs,cons) <- lift $ getTyVarCons cn tn+                   (tvbs,cons) <- getTyVarCons cn tn                    classContext <- lift $ generateClassContext cn tn                    let typeNames = map getTVBName tvbs                    instanceType <- lift $ foldl' appT (conT tn) $ map varT typeNames@@ -83,7 +83,7 @@  {- | This is particularly useful with 'Generic' class. -For the types like 'Int', 'Char','Ratio', 'Complex' or other types which are not 'Generic', there must be a way to stop the generation process on those types.+For the types like 'Int', 'Char','Ratio' or other types which are not 'Generic', there must be a way to stop the generation process on those types.  However, the deriving topdown function will only stop generating 'Generic' instances on primitive types and 'Integer' by default, so you do not need to break on them manually. 
src/Data/Derive/TopDown/TH.hs view
@@ -13,7 +13,7 @@  genTH :: (ClassName, Name -> Q [Dec]) -> TypeName -> StateT [Type] Q [Dec] genTH (className,deriveFunction) typeName = do-                       (tvbs, cons) <- lift $ getTyVarCons className typeName+                       (tvbs, cons) <- getTyVarCons className typeName                        compositeNames <- lift $ fmap concat $ mapM getCompositeTypeNames cons                        let typeNames = map getTVBName tvbs                        instanceType <- lift $ foldl' appT (conT typeName) $ map varT typeNames
tests/Test.hs view
@@ -19,7 +19,9 @@ import Language.Haskell.Syntax import Data.Ratio import Text.Show.Functions-+import Language.SQL.SimpleSQL.Syntax+import Data.Word+import Text.PrettyPrint.GenericPretty  -- Test for deriving strategy newtype A = A (Int,B)@@ -159,6 +161,13 @@ #else deriving_ ''Show ''P3 #endif++data T = T Word8++deriving_ ''Typeable ''Word8++derivings [''Out, ''Generic] ''QueryExpr+  main = putStrLn "Test passed"