Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
The Thunk
API provides a way to defer potentially recursive computations:
thunk
is lazy in its argument, and does not run it directly- the first
force
triggers execution of the action passed to thunk - that action is run at most once, and returuns a list of other thunks
force
forces these thunks as well, and does not return before all of them have executed- Cycles are allowed: The action passed to
thunk
may return a thunk whose action returns the first thunk.
The implementation is hopefully thread safe: Even if multiple threads force or kick related thunks, all actions are still run at most once, and all calls to force terminate (no deadlock).
>>>
:set -XRecursiveDo
>>>
:{
mdo t1 <- thunk $ putStrLn "Hello" >> pure [t1, t2] t2 <- thunk $ putStrLn "World" >> pure [t1, t2] putStrLn "Nothing happened so far, but now:" force t1 putStrLn "No more will happen now:" force t1 putStrLn "That's it" :} Nothing happened so far, but now: Hello World No more will happen now: That's it