What are Haskell's strictness points?

前端 未结 8 1085
伪装坚强ぢ
伪装坚强ぢ 2021-01-29 22:00

We all know (or should know) that Haskell is lazy by default. Nothing is evaluated until it must be evaluated. So when must something be evaluated? There are points where Haskel

8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-29 22:31

    Haskell is AFAIK not a pure lazy language, but rather a non-strict language. This means that it does not necessarily evaluate terms at the last possible moment.

    A good source for haskell's model of "lazyness" can be found here: http://en.wikibooks.org/wiki/Haskell/Laziness

    Basically, it is important to understand the difference between a thunk and the weak header normal form WHNF.

    My understanding is that haskell pulls computations through backwards as compared to imperative languages. What this means is that in the absence of "seq" and bang patterns, it will ultimately be some kind of side effect that forces the evaluation of a thunk, which may cause prior evaluations in turn (true lazyness).

    As this would lead to a horrible space leak, the compiler then figures out how and when to evaluate thunks ahead of time to save space. The programmer can then support this process by giving strictness annotations (en.wikibooks.org/wiki/Haskell/Strictness , www.haskell.org/haskellwiki/Performance/Strictness) to further reduce space usage in form of nested thunks.

    I am not an expert in the operational semantics of haskell, so I will just leave the link as a resource.

    Some more resources:

    http://www.haskell.org/haskellwiki/Performance/Laziness

    http://www.haskell.org/haskellwiki/Haskell/Lazy_Evaluation

提交回复
热议问题