Haskell 使用 >=> 编写 >>=单子

发布于 2025-01-10 10:04:26 字数 568 浏览 2 评论 0原文

(>=>) :: Monad m => (a -> m b) -> (b -> m c) -> (a -> m c)

bind :: Monad m => m a -> (a -> m b) -> m b

-- 如何使用>=>来编写bind

我非常困惑,我明白这两者是如何工作的,但我什至无法开始。

我尝试了以下所有替代方案:

bind1 a b = (b a)
bind2 a b = (a >=> b)
bind3 a b = (b >=> a)
bind4 a b = (a b)

但每次尝试都会引发错误。我可以从哪里开始回答这个问题?

由于 >>= 有 2 个参数,而 >=> 有 3 个参数,我如何使用 >=> 来少1个参数时要写>>=吗?

(>=>) :: Monad m => (a -> m b) -> (b -> m c) -> (a -> m c)

bind :: Monad m => m a -> (a -> m b) -> m b

-- how do you use >=> to write bind?

I am absolutely so confused, I understand how both of these works but I cant even get started.

I tried all the following alternatives:

bind1 a b = (b a)
bind2 a b = (a >=> b)
bind3 a b = (b >=> a)
bind4 a b = (a b)

but every single attempt raises an error. Where can i possibly start on this question?

Since >>= has 2 parameters, and >=> has 3, how can I use >=> to write >>= when there is 1 less parameter?

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

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

发布评论

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

评论(2

你是我的挚爱i 2025-01-17 10:04:26

首先,我建议重命名类型参数,以便最终结果匹配:

(>=>) :: Monad m => (a -> m b) -> (b -> m c) -> (a -> m c)
bind :: Monad m => m a -> (a -> m c)               -> m c

现在我们有一个 b ->;第一个签名中的 m ca -> m c 在第二个;建议将 a 重命名为 b

(>=>) :: Monad m => (a -> m b) -> (b -> m c) -> (a -> m c)
bind :: Monad m =>     m b     -> (b -> m c)       -> m c

主要区别在于有一个额外的参数 a。但我们显然不需要它,所以我们不妨将其专门化为无信息类型 () (或任何其他单值类型):

(>=>) :: Monad m => (() -> m b) -> (b -> m c) -> (() -> m c)
bind :: Monad m =>         m b  -> (b -> m c) ->        m c

() -> m x 实际上与单独的 m x 同构:

addDummyArg :: a -> (() -> a)
addDummyArg c () = c
rmDummyArg :: (() -> a) -> a
rmDummyArg f = f ()

这只是使用这些转换函数来使函数相互适应的问题。

First I'd suggest renaming the type parameters so the final result matches:

(>=>) :: Monad m => (a -> m b) -> (b -> m c) -> (a -> m c)
bind :: Monad m => m a -> (a -> m c)               -> m c

Now we have a b -> m c in the first signature and a -> m c in the second; that suggests renaming also a to b:

(>=>) :: Monad m => (a -> m b) -> (b -> m c) -> (a -> m c)
bind :: Monad m =>     m b     -> (b -> m c)       -> m c

That leaves the main difference as having an extra argument a. But we evidently don't need that, so we might as well specialize it to the information-less type () (or any other single-value type):

(>=>) :: Monad m => (() -> m b) -> (b -> m c) -> (() -> m c)
bind :: Monad m =>         m b  -> (b -> m c) ->        m c

() -> m x is actually isomorphic to m x alone:

addDummyArg :: a -> (() -> a)
addDummyArg c () = c
rmDummyArg :: (() -> a) -> a
rmDummyArg f = f ()

And it's just a matter of using those conversion functions to adapt the functions to each other.

迟月 2025-01-17 10:04:26

由于>>=有2个参数,而>=>有3个参数,我该如何使用>=> 当参数少 1 个时要写 >>= 吗?


Haskell 中的每个函数都有一个参数。您可以将 >=> 解释为第一个参数为 a ->; 类型的函数。 m b 和 b -> 的第二个内容m c,a -> m c 是“结果”函数。

您在这里可以做的是将 bind 的第一个参数操作为接受参数的函数,然后提供该参数,例如:

bind :: Monad m => m a -> (a -> m b) -> m b
bind ma f = (const ma >=> f) undefined

这里使用 const ma,因此我们创建一个带有我们不关心的参数的函数,然后构造一个 a ->; m c 通过使用 >=> 并使用 undefined (或其他值)调用它来生成 m c 值。

Since >>= has 2 parameters, and >=> has 3, how can I use >=> to write >>= when there is 1 less parameter?

Each function in Haskell has one parameter. You can interpret >=> , as a function that has as first parameter something of type a -> m b and the second something of b -> m c, the a -> m c is the "result" function.

What you here can do is manipulate the first parameter of bind into a function that accepts a parameter, and then provide that parameter, something like:

bind :: Monad m => m a -> (a -> m b) -> m b
bind ma f = (const ma >=> f) undefined

Here with const ma, we thus create a function that takes a parameter we do not care about, then we construct an a -> m c by using >=> and we call it with undefined (or some other value) to produce the m c value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文