📜 ⬆️ ⬇️

Calculation in a Haskell heap

The Beginning of the Haskell Series

The spirit of Christmas gifts

Today in the article we will briefly review what happens when you open a gift with the spirit inside in the Haskell pile. Almost in everything that is in the heap, except for constants and what has already been calculated, the spirit sits. The whole question is what will make the spirit in the gift.

In the simplest case, almost nothing!

Unlike real gift certificates, you must take a gift (Haskell will not allow you to calculate the thunk, and then do not care about the indication that is inside)

In most cases, it turns out that the spirit is a complete bummer . When he is woken up in order to familiarize himself with the contents of the gift, he has not done anything yet and must work with other gifts first!

To perform a primitive operation, you need to open all the gifts involved in it.


However, the spirit suddenly for no apparent reason can open an extraordinary gift ...

or perform a dangerous IO action with a side effect ...

Consider that, opening gifts, the spirit wakes up other spirits:

As a result, the spirits arrange a real Sabbath, and all this, to open a single gift!

The fact that opening a single gift (thunk) can lead to a chain reaction is the reason why lazy calculations surprise people who previously kept all their objects in a heap of unpacked (calculated). In order for a surprise to pass, you need to understand when the spirit decides to unpack a gift (strictness analysis / strictness analysis) or your gift can already be unpacked (amortized analysis).
')
Last time: Haskell Heap
Next time: IO works with a bunch of Haskell

Source: https://habr.com/ru/post/118831/


All Articles