一次,我以为我理解了单元。但是,当我试图将对代码的理解与理论联系起来时,我发现自己仍然不清楚。因此,它是:
第1部分。这是流行的教育,因此链接 a-monad-is-just-just-a-just-a-konoid-in-the-eNdofunctors-whats-whats-whats-the-problem 。
我的问题:
-
得分最高的答案说,蒙达德是个单型。单体由一个集合,一个操作 *和e组成。它解释了monoid定义之后的monad是什么。我要确认的第一件事是集合s的元素是单functor t:x-> x在单月定义中提到的x,正确?
-
这个问题标题引用了著名的短语,上面写着“内臂类别中的单体”。在这里,我对“内臂类别”有些困惑。我们知道的一个类别由物体和某些形态组成。因此,在上一个问题中提到的原始函数是对象是正确的,是正确的吗?我不太了解“ 中的”(中的一个单体类别...)在数学中的平均值。这是否意味着“属于”,“是”或“ include”?
部分。什么是单月?,我最初发现它很有启发性。但是最近我发现我无法将其示例中的代码清楚地映射到正式定义中。我对Haskell的经验有限,因此我专注于其C ++样本代码。这是作者声称这是一个单子的那个。
#include <iostream>
#include <string>
using namespace std;
string ret(string x) {
return "[" + x + "]";
}
string bind(string x, string (*func) (string)) {
return "[" + func(x.substr(1, x.length() - 2)) + "]";
}
string step1(string x) {
return "Hello " + x;
}
string step2(string x) {
return x + ", monads aren't that complicated.";
}
string step3(string x) {
return "***" + x + "***";
}
template<typename T> T run(T (*ret) (string),
T (*bind) (T, string (*) (string))) {
T x = "friend";
// first, wrap up the value of x
x = ret(x);
// now use wrapCall to run each step
x = bind(x, step1);
x = bind(x, step2);
x = bind(x, step3);
return x;
}
int main() {
cout << run<string>(ret, bind) << "\n";
}
然后作者说:
信不信由你,我们只是写了一个单子(特别是单调是绑定和ret)!
!
我的问题:
-
函数字符串ret(String X)
在这里与MONOID定义中的E相对应吗?但是E应该适用于集合的元素。
-
在此代码中,t(集合s的元素)是哪个函数?它应该是函子。我猜 step1,step2,step3
是。但是,它们看起来不像函子,而是类别。因为它们只是链接对象的箭头(类型为字符串)。函子应链接两个类别。
-
是绑定
操作 *(自然变换μ:t×t→t)?
希望我很好地说明了我的困惑,任何帮助都是感谢。
At one time, I thought I understood Monad. However, when I try to connect my understanding of code to the piece of theory, I found myself still not clear. So here it is:
part 1. Here is the popular educational SO link a-monad-is-just-a-monoid-in-the-category-of-endofunctors-whats-the-problem.
My questions:
-
The highest scored answer says monad is a monoid. And the monoid is consisted of a set S, an operation * and e. The it explains what monad is right after the monoid definition. The first thing that I want to confirm is that the element of the set S is the endofunctor T:X->X mentioned in Monad definition, correct?
-
The question title quotes the famous phrase saying "a monoid in the category of endofunctors". Here, I am a bit confused by "category of endofunctors". A category as we know consisted of objects and morphisms. So here is it correct to say endofunctors are the objects and the operation * mentioned in the previous question is the morphism? I don't quite understand what does the "in" (a monoid in the category...) mean in mathematics. Does it mean "belongs to", "is" or "include"?
part 2. Here is another link No, really, what's a monad?, which I initially found very instructive. However recently I found I could not clearly map the code in its example to the formal definition. My experience with Haskell is limited, so I focus on its c++ sample code. Here is the one the author claims it's a monad.
#include <iostream>
#include <string>
using namespace std;
string ret(string x) {
return "[" + x + "]";
}
string bind(string x, string (*func) (string)) {
return "[" + func(x.substr(1, x.length() - 2)) + "]";
}
string step1(string x) {
return "Hello " + x;
}
string step2(string x) {
return x + ", monads aren't that complicated.";
}
string step3(string x) {
return "***" + x + "***";
}
template<typename T> T run(T (*ret) (string),
T (*bind) (T, string (*) (string))) {
T x = "friend";
// first, wrap up the value of x
x = ret(x);
// now use wrapCall to run each step
x = bind(x, step1);
x = bind(x, step2);
x = bind(x, step3);
return x;
}
int main() {
cout << run<string>(ret, bind) << "\n";
}
Then the author says:
Believe it or not, we’ve just written a monad (specifically, the monad is the pair of bind and ret)!
My questions:
-
Is the function string ret(string x)
here corresponding to the e in monoid definition? But the e should apply to the set S elements.
-
In this code, which function is the T (the element of the set S)? It's supposed to be a functor. I guess step1, step2, step3
are. However, they don't look like functors but categories. Since they just act like arrows linking objects (type string to type string). A functor should link two categories.
-
Is bind
the operation * (natural transformation μ : T × T → T)?
Hopefully I state my confusion well, any help is appreciate.
发布评论
评论(1)
我想我已经弄清楚了。让我自己回答这个问题。在我发布的C ++代码中,
[]
是类型构造函数,bind> bind
函数,与Haskell中的fmap
相对应,因此它们两种形式函数,是t
此处提到的 a-monad-is-just-just-a-a-a-inonoid-in-in-the-eNdofunctors-whats-whats-whats-the-problem 。ret
函数是自然变换,η。运行
实际上表示自然变换μ。I think I've figured it out. Let me answer this question by myself. In the C++ code that I posted, the
[]
is the type constructor,bind
function corresponding to thefmap
in Haskell, hence they two form a functor, which is theT
mentioned here a-monad-is-just-a-monoid-in-the-category-of-endofunctors-whats-the-problem.The
ret
function is the natural transformation, η. Therun
actually represent the natural transformation, μ.