需要 MonadPlus (ST a) 实例

发布于 12-08 20:16 字数 1456 浏览 1 评论 0原文

我正在阅读论文 Haskell 中的类型化逻辑变量< /em>,但我无法理解最终实现的细节。特别是第 4 节中介绍的回溯状态转换器。出于某种我不知道的原因,GHC 认为我需要函数 (ST a)MonadPlus 实例。 code>unify,如下:

newtype BackT m a = BT { run :: forall b . (a -> m [b]) -> m [b] }

instance (Monad m) => Monad (BackT m) where
 return a   = BT (\k -> k a)
 BT m >>= f = BT (\k -> m (\a -> run (f a) k))

instance (MonadPlus m) => MonadPlus (BackT m) where 
 mzero             = BT (\s -> mzero)
 f `mplus` g = BT (\s -> (run f) s `mplus` (run g) s)

type LP a = BackT (ST a)
type LR   = STRef

type Var s a = LR s (Maybe a)   
data Atom s = VarA (Var s (Atom s)) | Atom String

class Unify b a | a -> b where
 var   :: a -> Maybe (Var b a)
 unify :: a -> a -> LP s ()

instance Unify s (Atom s) where
 var (VarA a) = Just a
 var _        = Nothing

 unify (Atom a) (Atom b) | a == b = return () -- type checks
 unify _        _                 = mzero     -- requires MonadPlus (ST a) instance

我不确定问题是什么以及如何解决它。到目前为止,我的印象是我理解了前面的讨论和代码,但显然我错了。如果有人可以指出出了什么问题 - 我是否需要 MonadPlus (ST a) 实例? - 这会很有帮助。

[编辑:澄清]我应该指出作者似乎声称mzero,或者mzero的一些变体, 是适当的函数。我只是不知道合适的功能是什么。我想知道我是否应该创建一个 MonadPlus (ST a) 实例,或者我没有使用正确的函数,并且误读了一些内容。

I'm reading the paper Typed Logical Variables in Haskell, but I'm failing to understand the details of the ultimate implementation. In particular, the backtracking state transformer introduced in section 4. For some reason, unknown to me, GHC believes I require a MonadPlus instance for (ST a) in the function unify, below:

newtype BackT m a = BT { run :: forall b . (a -> m [b]) -> m [b] }

instance (Monad m) => Monad (BackT m) where
 return a   = BT (\k -> k a)
 BT m >>= f = BT (\k -> m (\a -> run (f a) k))

instance (MonadPlus m) => MonadPlus (BackT m) where 
 mzero             = BT (\s -> mzero)
 f `mplus` g = BT (\s -> (run f) s `mplus` (run g) s)

type LP a = BackT (ST a)
type LR   = STRef

type Var s a = LR s (Maybe a)   
data Atom s = VarA (Var s (Atom s)) | Atom String

class Unify b a | a -> b where
 var   :: a -> Maybe (Var b a)
 unify :: a -> a -> LP s ()

instance Unify s (Atom s) where
 var (VarA a) = Just a
 var _        = Nothing

 unify (Atom a) (Atom b) | a == b = return () -- type checks
 unify _        _                 = mzero     -- requires MonadPlus (ST a) instance

I'm unsure what the problem is, and how to fix it. I was under the impression that I understood the preceding discussion and code until this point, but apparently I was mistaken. If someone could point out what's going awry - do I need a MonadPlus (ST a) instance or not? - it would be very helpful.

[EDIT: Clarification] I should have pointed out that the authors appear to claim that mzero, or some variation on mzero, is the appropriate function. I just don't know what the appropriate function is. What I'm wondering is whether I am supposed to make a MonadPlus (ST a) instance or I'm not using the correct function, and have misread something.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风向决定发型2024-12-15 20:16:13

mzero 是类型类 MonadPlus 的成员。特别是,

mzero :: MonadPlus m => m a

用于您的函数 unify 的 monad 是 LP,它实际上是用 ST 实例化 BackT 的。您还可以为 BackT 定义一个 MonadPlus 实例,它依赖于底层 monad 的此类实例。由于 ST 没有这样的实例,GHC 会嘲笑你。

这是重要的部分:

instance (MonadPlus m) => MonadPlus (BackT m) where 
  mzero             = BT (\s -> mzero)
  f `mplus` g = BT (\s -> (run f) s `mplus` (run g) s)

用简单的英语来说:这是 BackT mMonadPlus 实例,前提是 m 也是 MonadPlus 的实例。代码>MonadPlus。由于 m 是使用 ST 实例化的,因此您需要该 ST 实例。我想知道如何在没有委托的情况下定义一个合理的 MonadPlus 实例。我有一个想法:

instance MonadPlus (BackT m) where
  mzero = BT (const $ return [])
  mplus (BT f) (BT g) = BT $ \a -> do
    fs <- f a
    gs <- g a
    return $ fs ++ gs

这个实例基本上连接了两个输出列表。我希望它适合您的需求。

mzero is a member of the typeclass MonadPlus. In particular

mzero :: MonadPlus m => m a

The monad that is used for your function unify is LP, which is actually BackT instantiated with ST. You furthermore define an instance of MonadPlus for BackT, that depends on such an instance for the underlying monad. Since ST has no such instance, GHC mocks you.

This is the important part:

instance (MonadPlus m) => MonadPlus (BackT m) where 
  mzero             = BT (\s -> mzero)
  f `mplus` g = BT (\s -> (run f) s `mplus` (run g) s)

In plain english: This is an instance of MonadPlus for BackT m, provided that m is also an instance of MonadPlus. Since m is instanciated with ST, you need that instance for ST. I wonder how you could define a sensible instance of MonadPlus without delegation. I have an idea:

instance MonadPlus (BackT m) where
  mzero = BT (const $ return [])
  mplus (BT f) (BT g) = BT $ \a -> do
    fs <- f a
    gs <- g a
    return $ fs ++ gs

This instance basically concatenates the two output lists. I hope it suits your needs.

挖鼻大婶2024-12-15 20:16:13

BackT MonadPlus 实例可能应该使用 []MonadPlus 实例,而不是 m >,像这样:

instance (Monad m) => MonadPlus (BackT m) where 
  mzero       = BT (\_ -> return mzero)
  f `mplus` g = BT (\s -> liftM2 mplus (run f s) (run g s))

The BackT MonadPlus instance probably should use the MonadPlus instance of [] instead of m, like this:

instance (Monad m) => MonadPlus (BackT m) where 
  mzero       = BT (\_ -> return mzero)
  f `mplus` g = BT (\s -> liftM2 mplus (run f s) (run g s))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文