Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timing out pure functions

Tags:

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.

like image 489
amindfv Avatar asked Apr 09 '12 09:04

amindfv


People also ask

Is time a pure function?

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.

Can pure functions have side effects?

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.


1 Answers

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).

like image 128
Daniel Fischer Avatar answered Dec 24 '22 22:12

Daniel Fischer