C# 中的 Monad——为什么 Bind 实现需要传递函数来返回 monad?
我在 C# 中看到的大多数 monad 示例都有点像这样编写:
public static Identity<B> Bind<A, B>(this Identity<A> a, Func<A, Identity<B>> func) {
return func(a.Value);
}
例如,请参阅 http://mikehadlow.blogspot.com/2011/01/monads-in-c-3-creating-our-first-monad.html。
问题是,要求 func
返回 Identity
有何意义?如果我使用以下定义:
public interface IValue<A> {
public IValue<B> Bind<B>(Func<A, B> func)
}
那么我实际上可以使用相同的 func
for Lazy
, Task
, 也许
等实际上并不依赖于实现IValue
的实际类型。
我在这里缺少什么重要的东西吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,考虑组合的概念。我们可以轻松地将组合表达为对委托的操作:
因此,如果我有一个函数 g ,它是
(int x) =>; x.ToString()
和一个函数 f,即(string s) => s.Length
然后我可以创建一个组合函数 h ,它是(int x) => x.ToString().Length 通过调用
f.Compose(g)
来实现。这应该很清楚。
现在假设我有一个从
T
到Monad
的函数 g 和一个从U
到Monad
F 的函数 f /代码>。我希望编写一个方法,将这两个返回 monad 的函数组合成一个接受
T
并返回Monad
的函数。所以我尝试这样写:行不通。
g
返回一个Monad
,但f
接受一个U
。我有一种方法可以将U
“包装”到Monad
中,但我没有办法“解开”它。但是,如果我有一个方法
,那么我可以编写一个由两个都返回 monad 的方法组成的方法:
这就是 Bind 从
T
到Monad
Monad
获取 func 的原因。 U>
——因为事情的重点是能够将函数 g 从T
获取到Monad
以及函数 fU
到Monad
并将它们组合成从T
到Monad
的函数 h。如果您想将函数 g 从
T
转换为U
并将函数 f 从U
转换为Monad
> 那么你一开始就不需要绑定。只需正常组合函数即可获得从T
到Monad
的方法! Bind的全部目的就是为了解决这个问题;如果您消除了该问题,那么您一开始就不需要 Bind。更新:
我假设您想将其组合成一个从
T
到V
的函数。但你不能保证这样的操作被定义了!例如,将“Maybe monad”作为 monad,在 C# 中表示为T?
。假设 g 为(int x)=>(double?)null
并且函数 f 为(double y)=>(decimal)y
。您应该如何将 f 和 g 组合成一个接受 int 并返回不可为 null 的decimal
类型的方法?没有“解包”将可为空的双精度值解包为 f 可以采用的双精度值!您可以使用 Bind 将 f 和 g 组合成一个接受 int 并返回可为 null 的小数的方法:
其中 Unit 是接受 的函数。代码>.
V
并返回Monad
Monad但是,如果 g 返回一个 monad 并且 f 不返回该 monad,那么 f 和 g 根本就没有组合——不能保证有一种方法可以从 monad 的实例返回到“未包装”类型。也许在某些 monad 的情况下总是存在的——比如 Lazy。或者有时可能存在,就像“也许”单子一样。通常有一种方法可以做到这一点,但没有要求您可以这样做。
顺便说一句,请注意我们如何使用“Bind”作为瑞士军刀来制作一种新的构图。绑定后可以进行任何操作!例如,假设我们对序列 monad 进行 Bind 操作,我们在 C# 中的 IEnumerable 类型上将其称为“SelectMany”:
您可能还对序列有一个运算符:
您真的需要吗将该代码写入
Where
中?不!您可以完全用“Bind/SelectMany”来构建它:高效吗?不,但是没有什么是 Bind/SelectMany 做不到的。如果您确实愿意,您可以只用 SelectMany 构建所有 LINQ 序列运算符。
First off, consider the notion of composition. We can express composition as an operation on delegates easily:
So if I have a function g which is
(int x) => x.ToString()
and a function f which is(string s) => s.Length
then I can make a composed function h which is(int x) => x.ToString().Length
by callingf.Compose(g)
.That should be clear.
Now suppose I have a function g from
T
toMonad<U>
and a function f fromU
toMonad<V>
. I wish to write a method that composes these two functions that return monads into a function that takes aT
and returns aMonad<V>
. So I try to write that:Doesn't work.
g
returns aMonad<U>
butf
takes aU
. I have a way to "wrap" aU
into aMonad<U>
but I don't have a way to "unwrap" one.However, if I have a method
then I can write a method that composes two methods that both return monads:
That's why Bind takes a func from
T
toMonad<U>
-- because the whole point of the thing is to be able to take a function g fromT
toMonad<U>
and a function f fromU
toMonad<V>
and compose them into a function h fromT
toMonad<V>
.If you want to take a function g from
T
toU
and a function f fromU
toMonad<V>
then you don't need Bind in the first place. Just compose the functions normally to get a method fromT
toMonad<V>
! The whole purpose of Bind is to solve this problem; if you wave that problem away then you don't need Bind in the first place.UPDATE:
And I presume you then want to compose that into a function from
T
toV
. But you can't guarantee that such an operation is defined! For example, take the "Maybe monad" as the monad, which is expressed in C# asT?
. Suppose you have g as(int x)=>(double?)null
and you have a function f that is(double y)=>(decimal)y
. How are you supposed to compose f and g into a method that takes an int and returns the non-nullabledecimal
type? There is no "unwrapping" that unwraps the nullable double into a double value that f can take!You can use Bind to compose f and g into a method that takes an int and returns a nullable decimal:
where Unit is a function that takes a
V
and returns aMonad<V>
.But there simply is no composition of f and g if g returns a monad and f doesn't return the monad -- there is no guarantee that there is a way to go back from the instance of the monad to an "unwrapped" type. Maybe in the case of some monads there always is -- like
Lazy<T>
. Or maybe there sometimes is, like with the "maybe" monad. There often is a way to do it, but there is not a requirement that you can do so.Incidentally, notice how we just used "Bind" as a Swiss Army Knife to make a new kind of composition. Bind can make any operation! For example, suppose we have the Bind operation on the sequence monad, which we call "SelectMany" on the
IEnumerable<T>
type in C#:You might also have an operator on sequences:
Do you really need to write that code inside
Where
? No! You can instead build it entirely out of "Bind/SelectMany":Efficient? No. But there is nothing that Bind/SelectMany cannot do. If you really wanted to you could build all of the LINQ sequence operators out of nothing but SelectMany.