CookBook/Recipes/TimeLimit.thy
author boehmes
Wed, 07 Jan 2009 16:29:49 +0100
changeset 61 64c9540f2f84
child 63 83cea5dc6bac
permissions -rw-r--r--
Added four recipes.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
61
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
     1
theory TimeLimit
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
     2
imports Base
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
     3
begin
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
     4
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
     5
section {* Restricting the Runtime of a Function *} 
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
     6
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
     7
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
     8
text {*
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
     9
  {\bf Problem:}
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    10
  Your tool should run only a specified amount of seconds.\smallskip
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    11
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    12
  {\bf Solution:} This can be achieved using time limits.\smallskip
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    13
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    14
  Assume the following function should run only five seconds:
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    15
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    16
  *}
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    17
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    18
ML {*
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    19
fun ackermann (0, n) = n + 1
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    20
  | ackermann (m, 0) = ackermann (m - 1, 1)
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    21
  | ackermann (m, n) = ackermann (m - 1, ackermann (m, n - 1))
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    22
*}
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    23
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    24
ML {* ackermann (3, 12) *}
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    25
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    26
(* takes more than 10 seconds *)
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    27
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    28
text {*
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    29
  The call can be encapsulated in a time limit of five seconds as follows:
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    30
  *}
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    31
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    32
ML {* 
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    33
TimeLimit.timeLimit (Time.fromSeconds 5) ackermann (3, 12) 
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    34
  handle TimeOut => ~1
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    35
*}
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    36
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    37
text {*
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    38
  The function "TimeLimit.timeLimit" has type "???" and is defined in
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    39
  TODO: refer to code *}
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    40
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    41
end