61
|
1 |
theory TimeLimit
|
346
|
2 |
imports "../Appendix"
|
61
|
3 |
begin
|
|
4 |
|
565
|
5 |
section \<open>Restricting the Runtime of a Function\label{rec:timeout}\<close>
|
|
6 |
text \<open>
|
61
|
7 |
{\bf Problem:}
|
63
|
8 |
Your tool should run only a specified amount of time.\smallskip
|
61
|
9 |
|
562
|
10 |
{\bf Solution:} In Isabelle 2016-1 and later, this can be achieved
|
|
11 |
using the function @{ML apply in Timeout}.\smallskip
|
61
|
12 |
|
168
|
13 |
Assume you defined the Ackermann function on the ML-level.
|
565
|
14 |
\<close>
|
61
|
15 |
|
565
|
16 |
ML %grayML\<open>fun ackermann (0, n) = n + 1
|
61
|
17 |
| ackermann (m, 0) = ackermann (m - 1, 1)
|
565
|
18 |
| ackermann (m, n) = ackermann (m - 1, ackermann (m, n - 1))\<close>
|
61
|
19 |
|
565
|
20 |
text \<open>
|
63
|
21 |
|
|
22 |
Now the call
|
|
23 |
|
569
|
24 |
@{ML_matchresult_fake_both [display,gray] \<open>ackermann (4, 12)\<close> \<open>\<dots>\<close>}
|
61
|
25 |
|
68
|
26 |
takes a bit of time before it finishes. To avoid this, the call can be encapsulated
|
168
|
27 |
in a time limit of five seconds. For this you have to write
|
63
|
28 |
|
567
|
29 |
@{ML_matchresult_fake_both [display,gray]
|
569
|
30 |
\<open>Timeout.apply (Time.fromSeconds 5) ackermann (4, 12)
|
|
31 |
handle TIMEOUT => ~1\<close>
|
|
32 |
\<open>~1\<close>}
|
61
|
33 |
|
565
|
34 |
where \<open>TimeOut\<close> is the exception raised when the time limit
|
63
|
35 |
is reached.
|
|
36 |
|
569
|
37 |
Note that @{ML \<open>apply\<close> in Timeout} is only meaningful when you use PolyML 5.2.1
|
186
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
38 |
or later, because this version of PolyML has the infrastructure for multithreaded
|
569
|
39 |
programming on which @{ML \<open>apply\<close> in Timeout} relies.
|
61
|
40 |
|
63
|
41 |
\begin{readmore}
|
569
|
42 |
The function @{ML \<open>apply\<close> in Timeout} is defined in the structure
|
567
|
43 |
@{ML_structure Timeout} which can be found in the file
|
562
|
44 |
@{ML_file "Pure/concurrent/timeout.ML"}.
|
63
|
45 |
\end{readmore}
|
|
46 |
|
|
47 |
|
565
|
48 |
\<close>
|
517
d8c376662bb4
removed special ML-setup and replaced it by explicit markups (i.e., %grayML)
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
49 |
end
|