Safe Haskell | None |
---|
This module provides the public interface to the template lifting library. It provides functions that input a Haskell declaration or expression (in the form of a Haskell abstract syntax tree), and lift this to another declaration or expression, with all functions lifted into a specified monad.
This can be combined with Template Haskell to convert source code to Haskell abstract syntax trees and vice versa.
Synopsis
- decToMonad :: String -> Q [Dec] -> Q [Dec]
- expToMonad :: String -> Q Exp -> Q Exp
- module Quipper.Utils.Template.Auxiliary
- data Q a
- data Dec
Example
We give an example to illustrate what is meant by "lifting" a term to a monad. Consider the expression
f = \g x -> g x x,
which has type
f :: (a -> a -> b) -> (a -> b).
We can lift this to the IO
monad by
- converting the expression to an abstract syntax tree, using the
special Template Haskell notation
;[| ... |]
- applying the
expToMonad
function; - converting the resulting abstract syntax tree back to a term,
using the special Template Haskell notation
.$( ... )
This allows us to write the following:
f' = $( expToMonad "IO" [| \\g x -> g x x |] ),
which has type
f' :: IO ((a -> IO (a -> IO b)) -> IO (a -> IO b)),
and is in fact equivalent to
f'' = return $ \g -> return $ \x -> do h <- g x y <- h x return y
General lifting functions
decToMonad :: String -> Q [Dec] -> Q [Dec] Source #
Lift a list of declarations. The first argument is the name of the monad to lift into.
expToMonad :: String -> Q Exp -> Q Exp Source #
Lift an expression. The first argument is the name of the monad to lift into.
Liftings of specific constants
Re-exports from Language.Haskell.TH.
Instances
Monad Q | |
Functor Q | |
MonadFail Q | |
Defined in Language.Haskell.TH.Syntax | |
Applicative Q | |
MonadIO Q | |
Defined in Language.Haskell.TH.Syntax | |
Quasi Q | |
Defined in Language.Haskell.TH.Syntax qNewName :: String -> Q Name # qReport :: Bool -> String -> Q () # qRecover :: Q a -> Q a -> Q a # qLookupName :: Bool -> String -> Q (Maybe Name) # qReifyFixity :: Name -> Q (Maybe Fixity) # qReifyInstances :: Name -> [Type] -> Q [Dec] # qReifyRoles :: Name -> Q [Role] # qReifyAnnotations :: Data a => AnnLookup -> Q [a] # qReifyModule :: Module -> Q ModuleInfo # qReifyConStrictness :: Name -> Q [DecidedStrictness] # qAddDependentFile :: FilePath -> Q () # qAddTempFile :: String -> Q FilePath # qAddTopDecls :: [Dec] -> Q () # qAddForeignFilePath :: ForeignSrcLang -> String -> Q () # qAddModFinalizer :: Q () -> Q () # qAddCorePlugin :: String -> Q () # qGetQ :: Typeable a => Q (Maybe a) # qPutQ :: Typeable a => a -> Q () # qIsExtEnabled :: Extension -> Q Bool # qExtsEnabled :: Q [Extension] # |