动机。我正在尝试创建一个monad变换器,其中包含一条特殊指令f <||> g
,意思是“重复包含f <||> g
的整个块,一次使用f
,下次使用g
”。这可以用于DSL转换,但您可以想象其他应用程序。
示例用法。 computation
monad表达了不同的可能选择(在这种情况下,是要打印的东西)。 printme
函数说明了如何处理每个不同的结果。在这种情况下,我们在运行之前打印“开始计算”,在之后打印“---”。
computation = do
lift (print "start -- always")
(lift (print "first choice") <||> lift (print "second choice"))
lift (print "intermediate -- always")
(lift (print "third choice") <||> lift (print "fourth choice"))
lift (print "end -- always")
printme x = do
putStrLn "=== start computation"
xv <- x
putStrLn "---\n"
return xv
test = runIndep printme computation
输出如下,
=== start computation
"start -- always"
"first choice"
"intermediate -- always"
"third choice"
"end -- always"
---
=== start computation
"start -- always"
"first choice"
"intermediate -- always"
"fourth choice"
"end -- always"
---
=== start computation
"start -- always"
"second choice"
"intermediate -- always"
"third choice"
"end -- always"
---
=== start computation
"start -- always"
"second choice"
"intermediate -- always"
"fourth choice"
"end -- always"
---
问题。使用某种延续传递方式monad变换器有没有一种干净的方法来实现上述行为?我看过Oleg等人的“Backtracking,Interleaving,Terminating Monad Transformers”论文,但似乎无法完全掌握它们的实现(一旦它们继续实现msplit
实现)。 / p>
当前实施。我目前的实现是传递一个分支决策列表。 monad将返回它实际选择的分支列表,然后下次我们将切换最后一个可能的分支。代码如下(应该在7.0.3中运行),
import Control.Monad.Trans.Class
data IndepModelT α = IndepModelT {
unIndepModelT :: [Bool] -> (α, [Bool]) }
instance Monad => Monad (IndepModelT ) where
return x = IndepModelT $ \choices -> return (x, [])
(IndepModelT x) >>= f = IndepModelT $ \choices -> do
(xv, branches) <- x choices
let choices' = drop (length branches) choices
(fxv, branches') <- unIndepModelT (f xv) choices'
return (fxv, branches ++ branches')
instance MonadTrans IndepModelT where
lift x = IndepModelT $ \c -> liftWithChoice [] x
liftWithChoice cs mx = mx >>= \xv -> return (xv, cs)
(<||>)
:: Monad => IndepModelT α -> IndepModelT α -> IndepModelT α
(IndepModelT f) <||> (IndepModelT g) = IndepModelT go where
go (False:cs) = do
(fv, branches) <- f cs
return (fv, False : branches)
go (True:cs) = do
(fv, branches) <- g cs
return (fv, True : branches)
run_inner next_choices k comp@(IndepModelT comp_inner) = do
(xv, branches) <- k $ comp_inner next_choices
case (get_next_choices branches) of
Nothing -> return ()
Just choices -> run_inner (choices ++ repeat False) k comp
where
get_next_choices [] = Nothing
get_next_choices [True] = Nothing
get_next_choices [False] = Just [True]
get_next_choices (c:cs)
| Just cs' <- get_next_choices cs = Just $ c:cs'
| c Prelude.== False = Just [True]
| otherwise = Nothing
runIndep :: Monad =>
( (α, [Bool]) -> (β, [Bool]))
-> IndepModelT α
-> ()
runIndep = run_inner (repeat False)
runIndepFirst (IndepModelT comp) = comp (repeat False)
答案 0 :(得分:8)
问题在于:这不是一个单子!这种行为甚至没有明确定义。 F.E.在这种情况下应该怎么做:
do
b <- ...randomly True or False...
if b then ...some choices... else ...some other choices...
但是,它是Applicative
。我们需要的类型是[IO a]
,它是2个applicative functor的组合,因此我们可以使用变换器包中的Data.Functor.Compose
。这样也可以免费提供Alternative
<|>
个实例。我们将使用Rebindable Syntax为Applicatives使用do-notation:
{-# LANGUAGE RebindableSyntax #-}
import Prelude hiding ((>>), (>>=))
import Control.Applicative
import Data.Functor.Compose
lift :: Applicative f => g a -> Compose f g a
lift = Compose . pure
(>>) :: Applicative f => f a -> f b -> f b
(>>) = (*>)
computation :: Alternative f => Compose f IO ()
computation = do
lift (print "start -- always")
lift (print "first choice") <|> lift (print "second choice")
lift (print "intermediate -- always")
lift (print "third choice") <|> lift (print "fourth choice")
lift (print "end -- always")
printme x = do
putStrLn "=== start computation"
x
putStrLn "---\n"
test = mapM printme $ getCompose computation
答案 1 :(得分:3)
到目前为止你的建议不起作用。这是怎么回事:
f <||> g = ContT $ \k -> do
xs <- runContT f k
ys <- runContT g k
return $ xs ++ ys
test = runContT computation (return . (:[]))
但是这不会重新启动每个选择的整个计算,结果如下:
"start -- always"
"first choice"
"intermediate -- always"
"third choice"
"end -- always"
"fourth choice"
"end -- always"
"second choice"
"intermediate -- always"
"third choice"
"end -- always"
"fourth choice"
"end -- always"
我还没有找到一个好的解决方案。
答案 2 :(得分:1)
如果您正在寻找专门针对基于延续的方法,那么您将不会比the LogicT
paper中的SFKT
成功/失败延续实施简单得多。
如果msplit
太多(并且它是一个非常微妙的野兽),你可以忽略它为这个应用程序。它的目的是允许公平的连接和分离,如果那些样本输出线要按顺序打印,这不是您的规范的一部分。只需关注第5.1节中的Monad
和MonadPlus
实现,您就可以全部设置。
更新:正如Sjoerd Visscher所指出的那样,这是不正确的,因为重新启动只发生在mplus
而不是整个计算。这比第一次阅读时出现的问题要复杂得多。