Why is the $! operator right-associative?

会有一股神秘感。 提交于 2019-12-12 12:12:15

问题


I'm just learning Haskell and I'm still not entirely clear on when and how strict evaluation is forced

When I want a function to evaluate its arguments strictly I find myself writing

((f $! x) $! y ) $! z

which seems weird. Shouldn't $! be left-associative so I could write

f $! x $! y $! z

and have it do what I want?

Am I completely misunderstanding the $! operator?


回答1:


Argument against

I found a proposal from 2008 in haskell-prime to make the $ and $! operators left-associative:

https://ghc.haskell.org/trac/haskell-prime/wiki/ChangeDollarAssociativity

There is only one argument against the proposal: "This would break a lot of code".

Arguments in favour

Instead, there are given four arguments in favour of left-associative ($), the last one being the same as yours, and considered the most important. They are, in short:

  • 0) given the expression f x y, with two applications, we would be able to write f $ x $ y

  • 1) now, with right associative ($), we can write f . g . h $ x as f $ g $ h $ x,

    however: \x -> f $ g $ h $ x ==> f $ g $ h is invalid,

    so that writing such pipelines with composition is better, as it allows easier cleanup of code

  • 2) Left associative ($) allows you to eliminate more parentheses, in addition to the ones eliminated with (.), for instance:

    f (g x) (h y) ==> f $ g x $ h y

  • 3) your argument: the right associative version of $! is inconvenient because of giving rise to things like: ((f $! x) $! y) $! z instead of f $! x $! y $! z

Conclusion

I give support to use the better left-associative version of application operators redefining them at the beginning of our code, like this:

import Prelude hiding (($), ($!))

infixl 0  $, $!
($), ($!) :: (a -> b) -> a -> b  
f $  x =         f x
f $! x = x `seq` f x



回答2:


It's to mirror the fixity of $. You could make a very good case for both $ and $! having the wrong fixity.



来源:https://stackoverflow.com/questions/23570501/why-is-the-operator-right-associative

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!