author | boehmes |
Fri, 30 Jan 2009 16:58:31 +0100 | |
changeset 94 | 531e453c9d67 |
parent 72 | 7b8c4fe235aa |
child 102 | 5e309df58557 |
permissions | -rw-r--r-- |
61 | 1 |
theory TimeLimit |
63 | 2 |
imports "../Base" |
61 | 3 |
begin |
4 |
||
94 | 5 |
section {* Restricting the Runtime of a Function\label{rec:timeout} *} |
61 | 6 |
|
7 |
text {* |
|
8 |
{\bf Problem:} |
|
63 | 9 |
Your tool should run only a specified amount of time.\smallskip |
61 | 10 |
|
63 | 11 |
{\bf Solution:} This can be achieved using the function |
12 |
@{ML timeLimit in TimeLimit}.\smallskip |
|
61 | 13 |
|
68
e7519207c2b7
added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents:
67
diff
changeset
|
14 |
Assume you defined the Ackermann function: |
61 | 15 |
|
16 |
*} |
|
17 |
||
69
19106a9975c1
highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents:
68
diff
changeset
|
18 |
ML{*fun ackermann (0, n) = n + 1 |
61 | 19 |
| ackermann (m, 0) = ackermann (m - 1, 1) |
69
19106a9975c1
highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents:
68
diff
changeset
|
20 |
| ackermann (m, n) = ackermann (m - 1, ackermann (m, n - 1)) *} |
61 | 21 |
|
63 | 22 |
text {* |
23 |
||
24 |
Now the call |
|
25 |
||
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents:
69
diff
changeset
|
26 |
@{ML_response_fake [display,gray] "ackermann (4, 12)" "\<dots>"} |
61 | 27 |
|
68
e7519207c2b7
added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents:
67
diff
changeset
|
28 |
takes a bit of time before it finishes. To avoid this, the call can be encapsulated |
63 | 29 |
in a time limit of five seconds. For this you have to write: |
30 |
||
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents:
69
diff
changeset
|
31 |
@{ML_response [display,gray] |
63 | 32 |
"TimeLimit.timeLimit (Time.fromSeconds 5) ackermann (4, 12) |
94 | 33 |
handle TimeLimit.TimeOut => ~1" |
63 | 34 |
"~1"} |
61 | 35 |
|
63 | 36 |
where @{ML_text TimeOut} is the exception raised when the time limit |
37 |
is reached. |
|
38 |
||
39 |
Note that @{ML "timeLimit" in TimeLimit} is only meaningful when you use PolyML, |
|
40 |
because PolyML has a rich infrastructure for multithreading programming on |
|
41 |
which @{ML "timeLimit" in TimeLimit} relies. |
|
61 | 42 |
|
63 | 43 |
\begin{readmore} |
44 |
The function @{ML "timeLimit" in TimeLimit} is defined in the structure |
|
45 |
@{ML_struct TimeLimit} which can be found in the file |
|
46 |
@{ML_file "Pure/ML-Systems/multithreading_polyml.ML"}. |
|
47 |
\end{readmore} |
|
48 |
||
49 |
||
61 | 50 |
*} |
51 |
||
52 |
end |