How can I "kill" a pure calculation which is taking too long? I tried
import System.Timeout
fact 0 = 1
fact n = n * (fact $ n - 1)
main = do maybeNum <- timeout (10 ^ 7) $ (return . fact) 99999999
print maybeNum
However, this doesn't work. Replace the (return . fact) 99999999
with a "real" IO function like getLine
and this works as expected.
Yes, it's possible for a pure function to return the time, if it's given that time as a parameter. Different time argument, different time result. Then form other functions of time as well and combine them with a simple vocabulary of function(-of-time)-transforming (higher-order) functions.
A pure function does not have side-effects and its result does not depend on anything other than its inputs. A pure function guarantees that for a given input it will produce the same output no matter how many times it is called.
The point is that
return (fact 999999999)
immediately returns and doesn't trigger the timeout. It returns a thunk that will be evaluated later.
If you force evaluation of the return value,
main = do maybeNum <- timeout (10 ^ 7) $ return $! fact 99999999
print maybeNum
it should trigger the timeout (if you provide a stack large enough so that the timeout happens before the stack overflow).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With