gpt4 book ai didi

haskell - `Monad` 中是否需要 `<$!>` 约束

转载 作者:行者123 更新时间:2023-12-03 21:13:37 24 4
gpt4 key购买 nike

documentation 中所述<$!><$> 的严格版本, 但令人惊讶的是

<$!> :: Monad m => (a -> b) -> m a -> m b 
f <$!> m = do
x <- m
let z = f x
z `seq` return z

而不是更自然的(在我看来;因为它保持较弱的约束并模仿 $! )

<$!> :: Functor f => (a -> b) -> f a -> f b
f <$!> x = x `seq` (f <$> x)

我想应用 seq绑定(bind)后与“自然”方法不同,但我不知道它有何不同。我的问题是:是否有任何原因使“自然”方法变得无用,这就是为什么实现受限于 Monad 的原因?

最佳答案

GHC 的 commit message包括以下两个链接,可以更清楚地了解此功能:

这是 Johan Tibell 提到的原因(引用自链接的邮件列表):

It works on Monads instead of Functors as required by us inspecting the argument.

This version is highly convenient if you want to work with functors/applicatives in e.g. parser and avoid spurious thunks at the same time. I realized that it was needed while fixing large space usage (but not space-leak) issues in cassava.

关于haskell - `Monad` 中是否需要 `<$!>` 约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62042036/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com