我试图根据我在网上阅读的一些有用的文献,使用Free monad构建AST.
我有一些关于在实践中使用这些AST的问题,我已经归结为以下示例.
假设我的语言允许以下命令:
{-# LANGUAGE DeriveFunctor #-}
data Command next
= DisplayChar Char next
| DisplayString String next
| Repeat Int (Free Command ()) next
| Done
deriving (Eq, Show, Functor)
Run Code Online (Sandbox Code Playgroud)
我手动定义了Free monad样板:
displayChar :: Char -> Free Command ()
displayChar ch = liftF (DisplayChar ch ())
displayString :: String -> Free Command ()
displayString str = liftF (DisplayString str ())
repeat :: Int -> Free Command () -> Free Command ()
repeat times block = liftF (Repeat times …Run Code Online (Sandbox Code Playgroud)