可以将一个空闲的monad转换为任何其他的monad,但是给定一个Free f x类型的值,我想要打印整个树,而不是将生成的AST的每个节点映射到另一个monad中的其他节点。
加布里埃尔冈萨雷斯直接uses值
showProgram :: (Show a, Show r) => Free (Toy a) r -> String
showProgram (Free (Output a x)) =
"output " ++ show a ++ "\n" ++ showProgram x
showProgram (Free (Bell x)) =
"bell\n" ++ showProgram x
showProgram (Free Done) =
"done\n"
showProgram (Pure r) =
"return " ++ show r ++ "\n"它可以抽象为
showF :: (x -> b) -> ((Free f x -> b) -> f (Free f x) -> b) -> Free f x -> b
showF backLiftValue backLiftF = fix (showFU backLiftValue backLiftF)
where
showFU :: (x -> b) -> ((Free f x -> b) -> f (Free f x) -> b) -> (Free f x -> b) -> Free f x -> b
showFU backLiftValue backLiftF next = go . runIdentity . runFreeT where
go (FreeF c ) = backLiftF next c
go (Pure x) = backLiftValue x 如果我们有一个类似于多态函数(使用Choice x = Choice x x作为函子),就很容易调用它。
showChoice :: forall x. (x -> String) -> Choice x -> String
showChoice show (Choice a b) = "Choice (" ++ show a ++ "," ++ show b ++ ")"但对于一个简单的手术来说这似乎很复杂..。还有哪些其他方法可以从f x -> b到Free f x -> b呢?
发布于 2015-12-15 11:05:34
使用iter和fmap
{-# LANGUAGE DeriveFunctor #-}
import Control.Monad.Free
data Choice x = Choice x x deriving (Functor)
-- iter :: Functor f => (f a -> a) -> Free f a -> a
-- iter _ (Pure a) = a
-- iter phi (Free m) = phi (iter phi <$> m)
showFreeChoice :: Show a => Free Choice a -> String
showFreeChoice =
iter (\(Choice l r) -> "(Choice " ++ l ++ " " ++ r ++ ")")
. fmap (\a -> "(Pure " ++ show a ++ ")")fmap从Free f a转换为Free f b,其余的由iter完成。你可以考虑一下这一点,或许还能得到更好的表现:
iter' :: Functor f => (f b -> b) -> (a -> b) -> Free f a -> b
iter' f g = go where
go (Pure a) = g a
go (Free fa) = f (go <$> fa)https://stackoverflow.com/questions/34287089
复制相似问题