@@ -38,16 +38,22 @@ import Language.Haskell.Generate.Expression
3838--------------------------------------------------------------------------------
3939-- Generate expressions
4040
41+-- | This monad keeps track of a counter for generating unique names and the set of modules
42+-- that are needed for the expression.
4143newtype Generate a = Generate { unGenerate :: StateT Integer (Writer (S.Set ModuleName)) a } deriving (Functor, Applicative, Monad)
4244
45+-- | Extract the set of modules and the value from a Generate action.
4346runGenerate :: Generate a -> (a, S.Set ModuleName)
4447runGenerate (Generate a) = runWriter $ evalStateT a 0
4548
49+-- | This is a type alias for a Generate action that returns an expression of type 't'.
4650type ExpG t = Generate (Expression t)
4751
52+-- | Use a haskell-src-exts Exp as the result of a Generate action.
4853returnE :: Exp -> ExpG t
4954returnE = return . Expression
5055
56+-- | Pretty print the expression generated by a given action.
5157generateExp :: ExpG t -> String
5258generateExp = prettyPrint . runExpression . fst . runGenerate
5359
@@ -88,7 +94,16 @@ newName pref = Generate $ do
8894 i <- get <* modify succ
8995 return $ Ident $ pref ++ show i
9096
91−-- | Generate a expression from a haskell value.
97+-- | Generate a expression from a haskell value. This can for example be used to create lambdas:
98+--
99+-- >>> putStrLn $ generateExp $ expr (\x f -> f <>$ x)
100+-- \ pvar_0 -> \ pvar_1 -> pvar_1 pvar_0
101+--
102+-- Or string literals:
103+--
104+-- >>> putStrLn $ generateExp $ expr "I'm a string!"
105+-- ['I', '\'', 'm', ' ', 'a', ' ', 's', 't', 'r', 'i', 'n', 'g', '!']
106+--
92107class GenExp t where
93108 type GenExpType t :: *
94109
@@ -129,7 +144,6 @@ instance GenExp x => GenExp (ExpG a -> x) where
129144--------------------------------------------------------------------------------
130145-- Apply functions
131146
132−
133147-- | Apply a function in a haskell expression to a value.
134148applyE :: ExpG (a -> b) -> ExpG a -> ExpG b
135149applyE a b = wrap $ liftM (foldl1 App) $ sequence [unwrap a, unwrap b]