Safe Haskell | None |
---|---|
Language | Haskell2010 |
- newtype Generate a = Generate {
- unGenerate :: StateT Integer (Writer (Set ModuleName)) a
- type ExpG t = Generate (Expression t)
- runGenerate :: Generate a -> (a, Set ModuleName)
- newName :: String -> Generate Name
- returnE :: Exp -> ExpG t
- useValue :: String -> Name -> ExpG a
- useCon :: String -> Name -> Generate QName
- useVar :: Name -> ExpG t
- caseE :: ExpG x -> [(Pat, ExpG t)] -> ExpG t
- applyE :: ExpG (a -> b) -> ExpG a -> ExpG b
- applyE2 :: ExpG (a -> b -> c) -> ExpG a -> ExpG b -> ExpG c
- applyE3 :: ExpG (a -> b -> c -> d) -> ExpG a -> ExpG b -> ExpG c -> ExpG d
- applyE4 :: ExpG (a -> b -> c -> d -> e) -> ExpG a -> ExpG b -> ExpG c -> ExpG d -> ExpG e
- applyE5 :: ExpG (a -> b -> c -> d -> e -> f) -> ExpG a -> ExpG b -> ExpG c -> ExpG d -> ExpG e -> ExpG f
- applyE6 :: ExpG (a -> b -> c -> d -> e -> f -> g) -> ExpG a -> ExpG b -> ExpG c -> ExpG d -> ExpG e -> ExpG f -> ExpG g
- (<>$) :: ExpG (a -> b) -> ExpG a -> ExpG b
- class GenExp t where
- type GenExpType t :: *
- expr :: t -> ExpG (GenExpType t)
- newtype ModuleM a = ModuleM (Writer (Set ModuleName, [Decl]) a)
- type ModuleG = ModuleM (Maybe [ExportSpec])
- data FunRef t = FunRef Name
- data Name :: *
- exportFun :: FunRef t -> ExportSpec
- addDecl :: Name -> ExpG t -> ModuleM (FunRef t)
- runModuleM :: ModuleG -> String -> Module
- generateModule :: ModuleG -> String -> String
- generateExp :: ExpG t -> String
Documentation
This monad keeps track of a counter for generating unique names and the set of modules that are needed for the expression.
Generate | |
|
Monad Generate | |
Functor Generate | |
Applicative Generate | |
Num t => Num (ExpG t) | |
GenExp (ExpG a) | |
GenExp x => GenExp (ExpG a -> x) | |
type GenExpType (ExpG a) = a | |
type GenExpType (ExpG a -> x) = a -> GenExpType x |
type ExpG t = Generate (Expression t) Source
This is a type alias for a Generate action that returns an expression of type t
.
runGenerate :: Generate a -> (a, Set ModuleName) Source
Extract the set of modules and the value from a Generate action.
newName :: String -> Generate Name Source
Generate a new unique variable name with the given prefix. Note that this new variable name is only unique relative to other variable names generated by this function.
useValue :: String -> Name -> ExpG a Source
Import a function from a module. This function is polymorphic in the type of the resulting expression, you should probably only use this function to define type-restricted specializations.
Example:
addInt :: ExpG (Int -> Int -> Int) -- Here we restricted the type to something sensible addInt = useValue "Prelude" $ Symbol "+"
useCon :: String -> Name -> Generate QName Source
Import a value constructor from a module. Returns the qualified name of the constructor.
applyE :: ExpG (a -> b) -> ExpG a -> ExpG b Source
Apply a function in a haskell expression to a value.
applyE3 :: ExpG (a -> b -> c -> d) -> ExpG a -> ExpG b -> ExpG c -> ExpG d Source
Apply a function to 3 arguments
applyE4 :: ExpG (a -> b -> c -> d -> e) -> ExpG a -> ExpG b -> ExpG c -> ExpG d -> ExpG e Source
Apply a function to 4 arguments
applyE5 :: ExpG (a -> b -> c -> d -> e -> f) -> ExpG a -> ExpG b -> ExpG c -> ExpG d -> ExpG e -> ExpG f Source
Apply a function to 5 arguments
applyE6 :: ExpG (a -> b -> c -> d -> e -> f -> g) -> ExpG a -> ExpG b -> ExpG c -> ExpG d -> ExpG e -> ExpG f -> ExpG g Source
Apply a function to 6 arguments
Generate a expression from a haskell value. This can for example be used to create lambdas:
>>>
putStrLn $ generateExp $ expr (\x f -> f <>$ x)
\ pvar_0 -> \ pvar_1 -> pvar_1 pvar_0
Or string literals:
>>>
putStrLn $ generateExp $ expr "I'm a string!"
['I', '\'', 'm', ' ', 'a', ' ', 's', 't', 'r', 'i', 'n', 'g', '!']
type GenExpType t :: * Source
expr :: t -> ExpG (GenExpType t) Source
This function generates the haskell expression from the given haskell value.
A module keeps track of the needed imports, but also has a list of declarations in it.
ModuleM (Writer (Set ModuleName, [Decl]) a) |
type ModuleG = ModuleM (Maybe [ExportSpec]) Source
This is the resulting type of a function generating a module. It is a ModuleM action returning the export list.
A reference to a function. With a reference to a function, you can apply it (by lifting it into ExprT using expr
) to some value
or export it using exportFun
.
GenExp (FunRef t) | |
type GenExpType (FunRef t) = t |
data Name :: *
This type is used to represent variables, and also constructors.
exportFun :: FunRef t -> ExportSpec Source
Generate a ExportSpec for a given function item.
addDecl :: Name -> ExpG t -> ModuleM (FunRef t) Source
Add a declaration to the module. Return a reference to it that can be used to either apply the function to some values or export it.
runModuleM :: ModuleG -> String -> Module Source
Extract the Module from a module generator.
generateModule :: ModuleG -> String -> String Source
Generate the source code for a module.
generateExp :: ExpG t -> String Source
Pretty print the expression generated by a given action.