Pergunta

In the Parallel & Concurrent Haskell book, at http://chimera.labs.oreilly.com/books/1230000000929/ch09.html#sec_timeout , why fmap Just m doesn't just instantly return a thunk (defeating the purpose of the time limit)?

Or for IO something is special and fmap always forces a result? That would surprise me, as the monad-par package uses NFData and forcing to avoid that kind of behaviour AFAIK.

I surely miss something. Please enlighten me.

Foi útil?

Solução

In this answer, I'm using the word 'computation' to describe the effectful, interacting with the environment, part of executing an IO action. Terminology varies.

Given an IO computation act :: IO a the computation fmap Just act is a computation which (when run) runs the whole of computation act and then applies Just to the result.

It doesn't instantly return a thunk. To do that it would have to defer the computation act into the thunk, and computations can't be put into thunks, only evaluations. Thunks contain pure evaluations only and that's precisely why they work - we know it's OK to defer the evaluation, because we know that it doesn't matter when the evaluation is performed, the answer will still the be same. That's not true of the execution of computations, which can interact with the outside world and may return different answers depending on the environment when it is run.

The primitives (arguably "language-breaking" primitives) unsafePerformIO and unsafeInterleaveIO allow you to break the rules in the preceding paragraph and bury an IO computation into a thunk, which is occasionally what you really want and often a terrible, terrible idea.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top