DTC-1.1.0: Data To Class transformation.

Language.Haskell.DTC.Class

Contents

Description

Class definition from a data declaration.

Synopsis

Functions

dataToClassWith :: String -> Decl -> DeclSource

Transform a data declaration to a class definition. The String argument will be the name of the type variable of the class definition.

dataToClass :: Decl -> DeclSource

Transform a data declaration to a class definition. Equivalent to dataToClassWith "t".

Example

Let's see an example of how you can transform all data declarations in a module to class definitions.

Let the following module (Maybe.hs):

 module MaybeExample where

 data Maybe a = Just a | Nothing

It contains the data declaration of the Maybe type. Now, if we write:

 import Language.Haskell.DTC

 main = do -- Parse the code.
           m <- parseModuleWithSrc "Maybe.hs" defaultParseMode
           -- Modify declarations with dataToClassWith.
           let m' = modifyHsDecls (map (dataToClassWith "m")) m
           -- Write the pretty-printed output.
           writeFile "MaybeC.hs" (prettyPrint m')

It produces the following output (in MaybeC.hs):

 module MaybeExample where

 class Maybe m where

         just :: a -> m a

         fromJust :: m a -> a

         nothing :: m a