The Quipper System

Safe HaskellNone

Libraries.Template.Lifting

Contents

Description

This module describes stripped-down Template Haskell abstract syntax trees (ASTs) for a subset of Haskell.

Synopsis

Abstract syntax trees of a simplified language

type Body = ExpSource

There are no "guarded bodies". One net effect is to make the "where" construct equivalent to a simple "let".

data Lit Source

Literals.

Constructors

CharL Char

Characters.

IntegerL Integer

Integers.

RationalL Rational

Reals.

Instances

data Pat Source

Patterns.

Constructors

LitP Lit

Literal.

VarP Name

Variable name.

TupP [Pat]

Tuple.

WildP

Wildchar.

ListP [Pat]

List as [...].

ConP Name [Pat]

Cons: h:t.

Instances

data Match Source

Match term construct.

Constructors

Match Pat Body 

Instances

data Dec Source

First-level declaration.

Constructors

ValD Name Body 

Instances

data Exp Source

Expression

Constructors

VarE Name

Variable name.

ConE Name

Constant name.

LitE Lit

Literal.

AppE Exp Exp

Application.

LamE Name Exp

Lambda abstraction.

TupE [Exp]

Tuple.

CondE Exp Exp Exp

If-then-else.

LetE [Dec] Exp

Let-construct.

CaseE Exp [Match]

Case distinction

ListE [Exp]

List: [...].

ReturnE

hardcoded constant for return.

MAppE

hardcoded constant for >>=.

Instances

Syntactic sugar to recover do-notation.

data BindS Source

Datatype to encode the notation x <- expr.

Constructors

BindS Name Exp 

doE :: [BindS] -> Exp -> ExpSource

A simple do: list of monadic let followed by a computation.

Variable substitution

getVarNames :: Pat -> Set NameSource

Get the set of variable names in a pattern.

substMatch :: Name -> Exp -> Match -> MatchSource

Substitution in a Match.

substDec :: Name -> Exp -> Dec -> DecSource

Substitution in a Dec.

substExp :: Name -> Exp -> Exp -> ExpSource

Substitution in an Exp.

mapSubstExp :: Map Name Exp -> Exp -> ExpSource

Substitution of several variables in one go.

Downgrading Template Haskell to AST

litTHtoExpAST :: Lit -> LiftQ ExpSource

Downgrading TH literals to Exp.

litTHtoPatAST :: Lit -> LiftQ PatSource

Downgrading TH literals to Pat.

normalizePatInExp :: [Pat] -> LiftQ ([Name], Map Name Exp)Source

Take a list of patterns coming from a where section and output a list of fresh names for normalized lets. Also gives a mapping for substituting inside the expressions. Assume all names in the list of patterns are distinct.

whereToLet :: Exp -> [(Pat, Exp)] -> LiftQ ExpSource

Build a let-expression out of pieces.

clauseToMatch :: Clause -> LiftQ MatchSource

Build a Match out of a TH clause

clausesToLambda :: [Clause] -> LiftQ ExpSource

From a list of TH clauses, make a case-distinction wrapped in a lambda abstraction.

expTHtoAST :: Exp -> LiftQ ExpSource

Downgrade expressions.

matchTHtoAST :: Match -> LiftQ MatchSource

Downgrade match-constructs.

bodyTHtoAST :: Body -> LiftQ ExpSource

Downgrade bodies into expressions.

patTHtoAST :: Pat -> LiftQ PatSource

Downgrade patterns.

firstLevelDecTHtoAST :: Dec -> Maybe (LiftQ Dec)Source

Downgrade first-level declarations.

decTHtoAST :: Dec -> LiftQ (Pat, Exp)Source

Downgrade any declarations (including the ones in where-constructs).

Upgrade AST to Template Haskell

typReturnE :: LiftQ TypeSource

Abstract syntax tree of the type of the function return.

typMAppE :: LiftQ TypeSource

Abstract syntax tree of the type of the function >>=.

litASTtoTH :: Lit -> LitSource

Upgrade literals

patASTtoTH :: Pat -> PatSource

Upgrade patterns.

matchASTtoTH :: Match -> LiftQ MatchSource

Upgrade match-constructs.

decASTtoTH :: Dec -> LiftQ DecSource

Upgrade declarations.

expASTtoTH :: Exp -> LiftQ ExpSource

Upgrade expressions.

Lifting AST terms (into AST terms)

liftIntegerL :: ExpSource

Variable referring to the lifting function for integers.

liftRationalL :: ExpSource

Variable referring to the lifting function for reals.

liftLitAST :: Lit -> LiftQ ExpSource

Lifting literals.

liftPatAST :: Pat -> LiftQ PatSource

Lifting patterns.

liftMatchAST :: Match -> LiftQ MatchSource

Lifting match-constructs.

liftDecAST :: Dec -> LiftQ DecSource

Lifting declarations.

liftFirstLevelDecAST :: Dec -> LiftQ DecSource

Lifting first-level declarations.

liftExpAST :: Exp -> LiftQ ExpSource

Lifting expressions.

makeDecTemplate :: Dec -> LiftQ DecSource

make a declaration into a template-declaration (by renaming with the template-prefix).

Various pretty printing functions

prettyPrintAST :: Q [Dec] -> IO ()Source

pretty-printing Template Haskell declarations.

prettyPrintLiftExpTH :: Q Exp -> IO ()Source

Pretty-printing Template Haskell expressions.

prettyPrintLiftExpAST :: LiftQ Exp -> IO ()Source

Pretty-printing expressions.

The main 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 ExpSource

Lift an expression. The first argument is the name of the monad to lift into.