haskell - 代表无限链 Action 的 Monad 转换器?

标签 haskell while-loop monads monad-transformers

我试图实现一个代表无限链 Action 的 monad 转换器,如下所示:

import Control.Arrow
import Control.Monad
import Data.Functor.Classes
import Data.Functor.Identity
import Data.Monoid
import Data.Semigroup
import Text.Read

import qualified Control.Monad.Trans.Class as T

newtype WhileT m a = WhileT {uncons :: m (a, WhileT m a)}

instance T.MonadTrans WhileT where
    lift m = WhileT $ do
        x <- m
        pure (x, T.lift m)

headW :: Functor m => WhileT m a -> m a
headW (WhileT m) = fmap fst m

tailW :: Functor m => WhileT m a -> m (WhileT m a)
tailW (WhileT m) = fmap snd m

drop1 :: Monad m => WhileT m a -> WhileT m a
drop1 m = WhileT (tailW m >>= uncons)

dropN :: Monad m => Int -> WhileT m a -> WhileT m a
dropN n = appEndo (stimes n (Endo drop1))

instance Functor m => Functor (WhileT m) where
    fmap f (WhileT m) = WhileT (fmap (f *** fmap f) m)

instance Monad m => Applicative (WhileT m) where
    pure x = WhileT (pure (x, pure x))
    (<*>) = ap

instance Monad m => Monad (WhileT m) where
    WhileT m >>= f = WhileT $ do
        (x, xs) <- m
        y <- headW (f x)
        let ys = xs >>= drop1 . f
        pure (y, ys)
姓名WhileT在 C 的 while 之后.很容易看出WhileT Identity是一个单子(monad)。
但是 m 的其他选择呢? ?特别是,通过一些实验判断,WhileT Maybe似乎相当于 ZipList .但是已经知道没有instance Monad ZipList .这是否意味着 WhileT真的不是单子(monad)变压器吗?
编辑
看来这是正确的实现方式instance Monad (WhileT m) :
import Control.Applicative

instance MonadPlus m => Applicative (WhileT m) where
    pure x = WhileT (pure (x, empty))
    liftA2 = liftM2
    (<*>) = ap

instance MonadPlus m => Alternative (WhileT m) where
    empty = WhileT empty
    WhileT xs <|> WhileT ys = WhileT (xs <|> ys)

instance MonadPlus m => Monad (WhileT m) where
    WhileT xs >>= f = WhileT $ do
        (y, ys) <- xs
        uncons (f y) <|> uncons (ys >>= f)

instance MonadPlus m => MonadPlus (WhileT m)
现在WhileT Identity甚至不是一个单子(monad),但是嘿,它只是 (->) Natural .
现在WhileT Maybe相当于[] .现在呢? m 任意选择在哪里 WhileT m违反单子(monad)定律?

最佳答案

this link 的反例为您的 WhileT Maybe 工作, 也。

-- to make these things a bit more convenient to type/read
fromListW :: [a] -> WhileT Maybe a
fromListW [] = WhileT Nothing
fromListW (x:xs) = WhileT (Just (x, fromListW xs))

xs = fromListW [1,2]

f 1 = fromListW [11]
f 2 = fromListW [21, 22]

g 11 = fromListW [111]
g 21 = fromListW []
g 22 = fromListW [221, 222]
然后,在 ghci 中:
> (xs >>= f) >>= g
WhileT {uncons = Just (111,WhileT {uncons = Just (222,WhileT {uncons = Nothing})})}
> xs >>= (\x -> f x >>= g)
WhileT {uncons = Just (111,WhileT {uncons = Nothing})}

关于haskell - 代表无限链 Action 的 Monad 转换器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67067754/

相关文章:

haskell - 显式导入是否能够减少编译文件的大小?

javascript - 组合多个 ||在做同时

java - 获取2个随机数的平均值 "30"

haskell - 使用 State Monad 插入树

haskell - 如何解释函数实例的bind/>>=?

haskell - 将 ghc-options 传递给 Stack 脚本解释器

email - Heroku 上 Yesod 应用程序的邮件配置

Haskell Wikibook - 广义代数数据类型练习 - Maybe 和 Either

php - MySQL CONCAT_WS 三列,在explode()之后在foreach循环中回显结果

haskell - 绑定(bind)多个参数