mirror of
https://github.com/ilyakooo0/haskell-generate.git
synced 2024-09-11 08:25:30 +03:00
Add more comments
This commit is contained in:
parent
c129f0625c
commit
76614f7476
@ -38,16 +38,22 @@ import Language.Haskell.Generate.Expression
|
|||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
-- Generate expressions
|
-- Generate expressions
|
||||||
|
|
||||||
|
-- | This monad keeps track of a counter for generating unique names and the set of modules
|
||||||
|
-- that are needed for the expression.
|
||||||
newtype Generate a = Generate { unGenerate :: StateT Integer (Writer (S.Set ModuleName)) a } deriving (Functor, Applicative, Monad)
|
newtype Generate a = Generate { unGenerate :: StateT Integer (Writer (S.Set ModuleName)) a } deriving (Functor, Applicative, Monad)
|
||||||
|
|
||||||
|
-- | Extract the set of modules and the value from a Generate action.
|
||||||
runGenerate :: Generate a -> (a, S.Set ModuleName)
|
runGenerate :: Generate a -> (a, S.Set ModuleName)
|
||||||
runGenerate (Generate a) = runWriter $ evalStateT a 0
|
runGenerate (Generate a) = runWriter $ evalStateT a 0
|
||||||
|
|
||||||
|
-- | This is a type alias for a Generate action that returns an expression of type 't'.
|
||||||
type ExpG t = Generate (Expression t)
|
type ExpG t = Generate (Expression t)
|
||||||
|
|
||||||
|
-- | Use a haskell-src-exts Exp as the result of a Generate action.
|
||||||
returnE :: Exp -> ExpG t
|
returnE :: Exp -> ExpG t
|
||||||
returnE = return . Expression
|
returnE = return . Expression
|
||||||
|
|
||||||
|
-- | Pretty print the expression generated by a given action.
|
||||||
generateExp :: ExpG t -> String
|
generateExp :: ExpG t -> String
|
||||||
generateExp = prettyPrint . runExpression . fst . runGenerate
|
generateExp = prettyPrint . runExpression . fst . runGenerate
|
||||||
|
|
||||||
@ -88,7 +94,16 @@ newName pref = Generate $ do
|
|||||||
i <- get <* modify succ
|
i <- get <* modify succ
|
||||||
return $ Ident $ pref ++ show i
|
return $ Ident $ pref ++ show i
|
||||||
|
|
||||||
-- | Generate a expression from a haskell value.
|
-- | 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', '!']
|
||||||
|
--
|
||||||
class GenExp t where
|
class GenExp t where
|
||||||
type GenExpType t :: *
|
type GenExpType t :: *
|
||||||
|
|
||||||
@ -129,7 +144,6 @@ instance GenExp x => GenExp (ExpG a -> x) where
|
|||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
-- Apply functions
|
-- Apply functions
|
||||||
|
|
||||||
|
|
||||||
-- | Apply a function in a haskell expression to a value.
|
-- | Apply a function in a haskell expression to a value.
|
||||||
applyE :: ExpG (a -> b) -> ExpG a -> ExpG b
|
applyE :: ExpG (a -> b) -> ExpG a -> ExpG b
|
||||||
applyE a b = wrap $ liftM (foldl1 App) $ sequence [unwrap a, unwrap b]
|
applyE a b = wrap $ liftM (foldl1 App) $ sequence [unwrap a, unwrap b]
|
||||||
|
Loading…
Reference in New Issue
Block a user