author | Norbert Schirmer <norbert.schirmer@web.de> |
Tue, 14 May 2019 16:59:53 +0200 | |
changeset 564 | 6e2479089226 |
parent 562 | daf404920ab9 |
child 565 | cecd7a941885 |
permissions | -rw-r--r-- |
61 | 1 |
theory TimeLimit |
346
0fea8b7a14a1
tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents:
191
diff
changeset
|
2 |
imports "../Appendix" |
61 | 3 |
begin |
4 |
||
94 | 5 |
section {* Restricting the Runtime of a Function\label{rec:timeout} *} |
61 | 6 |
text {* |
7 |
{\bf Problem:} |
|
63 | 8 |
Your tool should run only a specified amount of time.\smallskip |
61 | 9 |
|
562
daf404920ab9
Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents:
557
diff
changeset
|
10 |
{\bf Solution:} In Isabelle 2016-1 and later, this can be achieved |
daf404920ab9
Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents:
557
diff
changeset
|
11 |
using the function @{ML apply in Timeout}.\smallskip |
61 | 12 |
|
168
009ca4807baa
polished somewhat the recipes and solutions
Christian Urban <urbanc@in.tum.de>
parents:
155
diff
changeset
|
13 |
Assume you defined the Ackermann function on the ML-level. |
009ca4807baa
polished somewhat the recipes and solutions
Christian Urban <urbanc@in.tum.de>
parents:
155
diff
changeset
|
14 |
*} |
61 | 15 |
|
517
d8c376662bb4
removed special ML-setup and replaced it by explicit markups (i.e., %grayML)
Christian Urban <urbanc@in.tum.de>
parents:
346
diff
changeset
|
16 |
ML %grayML{*fun ackermann (0, n) = n + 1 |
61 | 17 |
| 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
|
18 |
| ackermann (m, n) = ackermann (m - 1, ackermann (m, n - 1)) *} |
61 | 19 |
|
63 | 20 |
text {* |
21 |
||
22 |
Now the call |
|
23 |
||
557
77ea2de0ca62
updated for Isabelle 2014
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
517
diff
changeset
|
24 |
@{ML_response_fake_both [display,gray] "ackermann (4, 12)" "\<dots>"} |
61 | 25 |
|
68
e7519207c2b7
added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents:
67
diff
changeset
|
26 |
takes a bit of time before it finishes. To avoid this, the call can be encapsulated |
168
009ca4807baa
polished somewhat the recipes and solutions
Christian Urban <urbanc@in.tum.de>
parents:
155
diff
changeset
|
27 |
in a time limit of five seconds. For this you have to write |
63 | 28 |
|
186
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
parents:
175
diff
changeset
|
29 |
@{ML_response_fake_both [display,gray] |
562
daf404920ab9
Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents:
557
diff
changeset
|
30 |
"Timeout.apply (Time.fromSeconds 5) ackermann (4, 12) |
daf404920ab9
Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents:
557
diff
changeset
|
31 |
handle TIMEOUT => ~1" |
63 | 32 |
"~1"} |
61 | 33 |
|
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
94
diff
changeset
|
34 |
where @{text TimeOut} is the exception raised when the time limit |
63 | 35 |
is reached. |
36 |
||
562
daf404920ab9
Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents:
557
diff
changeset
|
37 |
Note that @{ML "apply" 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>
parents:
175
diff
changeset
|
38 |
or later, because this version of PolyML has the infrastructure for multithreaded |
562
daf404920ab9
Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents:
557
diff
changeset
|
39 |
programming on which @{ML "apply" in Timeout} relies. |
61 | 40 |
|
63 | 41 |
\begin{readmore} |
562
daf404920ab9
Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents:
557
diff
changeset
|
42 |
The function @{ML "apply" in Timeout} is defined in the structure |
daf404920ab9
Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents:
557
diff
changeset
|
43 |
@{ML_struct Timeout} which can be found in the file |
daf404920ab9
Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents:
557
diff
changeset
|
44 |
@{ML_file "Pure/concurrent/timeout.ML"}. |
63 | 45 |
\end{readmore} |
46 |
||
47 |
||
61 | 48 |
*} |
517
d8c376662bb4
removed special ML-setup and replaced it by explicit markups (i.e., %grayML)
Christian Urban <urbanc@in.tum.de>
parents:
346
diff
changeset
|
49 |
end |