如何解释函数实例的bind />> =? [英] How to interpret bind/>>= of the function instance?
问题描述
我试图通过在Javascript中实现它们的函数实例来提高我对 Applicative
s和 Monad
s的理解。我对Haskell的知识是有限的,我希望我的问题是有道理的。
以下是我的 fmap
,< $>
和>> =
code>, Applicative
和 Monad
Javascript中的类型类:
const fmap = f => g => x => F(G(X)); // B combinator
const apply = f => g => x => f(x)(g(x)); // S combinator
const bind = f => g => x => g(f(x))(x); //?
我不确定绑定
是正确翻译Haskell实现:
(>> =)::(r - > a) - > (a→(r→b))→> r - > b
实例Monad(( - >)r)其中
f>> = k = \ r - >假如绑定
是正确的,它是如何解释的?我知道一个 Applicative
可以排序有效的计算。我还知道, Monad
还允许您根据前一个结果确定下一个效果。
我可以看到这些序列(在Javascript中的渴望评估顺序):
apply
: f(x)
... g(x)
... lambda(的结果)
... lambda
bind code>:
f(x)
... g(f的结果)
... lambda(x)
... lambda
然而, bind
函数看起来很奇怪。为什么 f
和 g
嵌套相反?特定的 Monad
行为(根据前一个效果确定下一个效果)如何体现在此实现中?其实 g(f(x))(x)
看起来像一个带有翻转参数的函数组合,其中 g
是一个二进制函数。
当我应用 apply
/ bind
使用一元函数和二元函数,他们会得到相同的结果。这没什么意义。
函数monad实例中的值的类型为 在您的代码 该函数将被赋予 I'm trying to improve my understanding of Here are my implementations of I am not sure whether Provided that I can see the sequences (eager evaluation order in Javascript): However, the When I apply The values in the monad instance for functions have type In your code The function will be given
这篇关于如何解释函数实例的bind />> =?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! r - >对于某些固定类型
。给予 r
,(>> =)
的函数(a - >(r - > b))
给出当前值的结果(一个函数 r - > a
),您可以选择下一个函数返回。 fr
有类型 a
和 k(fr)
r - > b 这是要应用的下一个函数。
g(f(x))$因此c $ c>是一个函数,它需要
r
类型的单个参数。 bind
的调用者可以根据前一个函数返回的值来选择该函数,例如
var inc = x => x + 1;
var f = bind(inc)(function(i){
if(i< = 5){return x => x * 2;}
else {return x => ; x * 3;}
});
x
作为输入,并可以根据 inc(x)
的结果选择计算中的下一个阶段,例如
f(2)// 4;
f(5)// 15;
Applicative
s and Monad
s by implementing their function instances in Javascript. My knowledge of Haskell is limited and I hope that my question makes sense at all.fmap
, <*>
and >>=
for the Functor
, Applicative
and Monad
typeclasses in Javascript:const fmap = f => g => x => f(g(x)); // B combinator
const apply = f => g => x => f(x) (g(x)); // S combinator
const bind = f => g => x => g(f(x)) (x); // ?
bind
is the correct translation of the Haskell implementation:(>>=) :: (r -> a) -> (a -> (r -> b)) -> r -> b
instance Monad ((->) r) where
f >>= k = \ r -> k (f r) r
bind
is correct, how is it interpreted? I know that an Applicative
can sequence effectful computations. I also know that a Monad
in addition allows you to determine a next effect according to the result of a previous one.
apply
: f(x)
... g(x)
... lambda(result of g)
... result of lambda
bind
: f(x)
... g(result of f)
... lambda(x)
... result of lambda
bind
function looks pretty weird. Why are f
and g
nested the other way around? How is the specific Monad
behavior (determines a next effect according to a previous one) reflected in this implementation? Actually g(f(x)) (x)
looks like a function composition with flipped arguments, where g
is a binary function.apply
/bind
with an unary and a binary function, they yield the same result. This doesn't make much sense.r -> a
for some fixed type r
. The function (a -> (r -> b))
given to (>>=)
allows you to choose the next function to return given the result from the current value (a function r -> a
). f r
has type a
and k (f r)
has type r -> b
which is the next function to apply.g(f(x))
is therefore a function which expects a single argument of type r
. The caller of bind
can choose this function based on the value returned by the previous function e.g.var inc = x => x + 1;
var f = bind(inc)(function(i) {
if(i <= 5) { return x => x * 2; }
else { return x => x * 3; }
});
x
as an input and can choose the next stage in the computation based on the result of inc(x)
e.g.f(2) //4;
f(5) //15;