如何解释函数实例的bind />> =? [英] How to interpret bind/>>= of the function instance?

查看:103
本文介绍了如何解释函数实例的bind />> =?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过在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实例中的值的类型为 r - >对于某些固定类型 r 。给予(>> =)的函数(a - >(r - > b))给出当前值的结果(一个函数 r - > a ),您可以选择下一个函数返回。 fr 有类型 a k(fr) r - > b 这是要应用的下一个函数。

在您的代码 g(f(x))是一个函数,它需要 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;


I'm trying to improve my understanding of Applicatives and Monads by implementing their function instances in Javascript. My knowledge of Haskell is limited and I hope that my question makes sense at all.

Here are my implementations of 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); // ?

I am not sure whether 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

Provided that 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.

I can see the sequences (eager evaluation order in Javascript):

  • apply: f(x) ... g(x) ... lambda(result of g) ... result of lambda
  • bind: f(x) ... g(result of f) ... lambda(x) ... result of lambda

However, the 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.

When I apply apply/bind with an unary and a binary function, they yield the same result. This doesn't make much sense.

解决方案

The values in the monad instance for functions have type 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.

In your code 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; }
});

The function will be given 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;

这篇关于如何解释函数实例的bind /&gt;&gt; =?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆