2
(begin-book t :ttags ((:acl2s-timeout)))
5
;Author: Harsh Raju Chamarthi
6
;Acknowledgements: Matt Kaufmann provided significant help.
10
(include-book "xdoc/top" :dir :system)
13
:short "Evaluate form with a timeout (in seconds)"
15
"<p>Evaluate form with a timeout in seconds. </p>
17
<p>The general form is:
18
@({with-timeout duration body timeout-form})
22
@('duration') can be any rational value. A duration of 0 seconds disables
23
the timeout mechanism, i.e its a no-op. Suppose it is not, and @('duration')
24
seconds elapse during evaluation of <tt>body</tt> then the evaluation is aborted
25
and the value of @('timeout-form') is returned; in the normal case the value
26
of <tt>body</tt> is returned.
28
<p> The signature of <tt>body</tt> and <tt>timeout-form</tt> should be the same. </p>
30
<h3>Advanced Notes:</h3>
32
This form should be called either at the top-level or in
33
an environment where state is available and <tt>body</tt> has
34
no free variables other than state.
35
If the timeout-form is a long running computation,
36
then the purpose of with-timeout is defeated.
41
(with-timeout 5 (fibonacci 40) :timed-out)
47
(defttag :acl2s-timeout)
52
(load (concatenate 'string (cbd) "with-timeout-raw.lsp")))
55
(defmacro-last with-timeout-aux)
59
(defmacro with-timeout (duration form timeout-form)
60
"can only be called at top-level, that too only forms that are allowed
61
to be evaluated inside a function body. To eval defthm, use
62
with-timeout-ev instead"
63
`(if (equal 0 ,duration) ;if 0 then timeout is disabled
65
(top-level (with-timeout1 ,duration ,form ,timeout-form))))
68
;the following is for internal use only. I use it in timing out
69
;top-level-test? form, where i manually make a function body
70
;corresponding to the top-level-test?-fn, this way I dont have to
71
;worry about capturing free variables
73
(defmacro with-timeout1 (duration form timeout-form)
74
"can only be used inside a function body, and if form has
75
free variables other than state, then manually make a function
76
which takes those free variables as arguments and at the calling
77
context, pass the arguments, binding the free variables.
78
See top-level-test? macro for an example"
79
`(if (equal 0 ,duration) ;if 0 then timeout is disabled
81
(with-timeout-aux '(,duration ,timeout-form) ,form)))
83
(defttag nil) ; optional (books end with this implicitly)