本文介绍了Control.Parallel.Strategies中的Eval的绑定运算符如何严格评估其参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 Control.Parallel.Strategies的源代码( http://hackage.haskell.org/packages/archive/parallel/3.1.0.1/doc/html/src/Control-Parallel-Strategies.html#Eval )包含一个类型为Eval的定义如下: data Eval a =完成a 有以下Monad实例: 实例Monad Eval return x =完成x 完成x>> = k = kx - 注意:pattern'Done x'使'>> ='strict 请注意bind定义中的注释。为什么这个评论是真实的?我对严格性的理解是,如果一个功能必须对它的论点知道某些事情,它才是严格的。在这里,绑定只需将k应用于x,因此它不会出现(对我而言)需要了解x的任何信息。此外,评论表明严格性在模式匹配中是诱导的,甚至在函数被定义之前。有人可以帮我理解为什么绑定是严格的吗? 另外,它看起来像Eval只是Monad的身份,而且, bind,bind的定义几乎适用于任何Monad。是这样吗?解决方案在 m>>与Identity Monad不同,评估 m : Prelude Control.Parallel.Strategies Control.Monad.Identity> runIdentity(undefined>> returnend)end Prelude Control.Parallel.Strategies Control.Monad.Identity> runEval(undefined>> returnend)***例外:Prelude.undefined m 产生的价值并不严格,w就是你指出的: end The source code for Control.Parallel.Strategies ( http://hackage.haskell.org/packages/archive/parallel/3.1.0.1/doc/html/src/Control-Parallel-Strategies.html#Eval ) contains a type Eval defined as:data Eval a = Done awhich has the following Monad instance:instance Monad Eval where return x = Done x Done x >>= k = k x -- Note: pattern 'Done x' makes '>>=' strictNote the comment in the definition of bind. Why is this comment true? My understanding of strictness is that a function is only strict if it must "know something" about its argument. Here, bind just applies k to x, thus it doesn't appear (to me) to need to know anything about x. Further, the comment suggests that strictness is "induced" in the pattern match, before the function is even defined. Can someone help me understand why bind is strict?Also, it looks like Eval is just the identity Monad and that, given the comment in the definition of bind, bind would be strict for almost any Monad. Is this the case? 解决方案 It is strict in that m >> n evaluates m, unlike the Identity Monad:Prelude Control.Parallel.Strategies Control.Monad.Identity> runIdentity (undefined >> return "end") "end"Prelude Control.Parallel.Strategies Control.Monad.Identity> runEval (undefined >> return "end") "*** Exception: Prelude.undefinedIt is not strict in the value that m produces,w which is what you are pointing out:Prelude Control.Parallel.Strategies Control.Monad.Identity> runEval (return undefined >> return "end") "end" 这篇关于Control.Parallel.Strategies中的Eval的绑定运算符如何严格评估其参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-29 10:46