What are Haskell's strictness points?

前端 未结 8 1069
伪装坚强ぢ
伪装坚强ぢ 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:41

    Lazy doesn't mean do nothing. Whenever your program pattern matches a case expression, it evaluates something -- just enough anyway. Otherwise it can't figure out which RHS to use. Don't see any case expressions in your code? Don't worry, the compiler is translating your code to a stripped down form of Haskell where they are hard to avoid using.

    For a beginner, a basic rule of thumb is let is lazy, case is less lazy.

    0 讨论(0)
  • 2021-01-29 22:41

    The Glasgow Haskell compiler translates your code into a Lambda-calculus-like language called core. In this language, something is going to be evaluated, whenever you pattern match it by a case-statement. Thus if a function is called, the outermost constructor and only it (if there are no forced fields) is going to be evaluated. Anything else is canned in a thunk. (Thunks are introduced by let bindings).

    Of course this is not exactly what happens in the real language. The compiler convert Haskell into Core in a very sophisticated way, making as many things as possibly lazy and anything that is always needed lazy. Additionally, there are unboxed values and tuples that are always strict.

    If you try to evaluate a function by hand, you can basically think:

    • Try to evaluate the outermost constructor of the return.
    • If anything else is needed to get the result (but only if it's really needed) is also going to be evaluated. The order doesn't matters.
    • In case of IO you have to evaluate the results of all statements from the first to the last in that. This is a bit more complicated, since the IO monad does some tricks to force evaluation in a specific order.
    0 讨论(0)
提交回复
热议问题