src/HOL/Mirabelle/Tools/mirabelle.ML
changeset 32498 1132c7c13f36
parent 32497 922718ac81e4
child 32503 14efbc20b708
equal deleted inserted replaced
32497:922718ac81e4 32498:1132c7c13f36
    23     Proof.state -> bool
    23     Proof.state -> bool
    24   val theorems_in_proof_term : Thm.thm -> Thm.thm list
    24   val theorems_in_proof_term : Thm.thm -> Thm.thm list
    25   val theorems_of_sucessful_proof : Toplevel.state option -> Thm.thm list
    25   val theorems_of_sucessful_proof : Toplevel.state option -> Thm.thm list
    26   val get_setting : (string * string) list -> string * string -> string
    26   val get_setting : (string * string) list -> string * string -> string
    27   val get_int_setting : (string * string) list -> string * int -> int
    27   val get_int_setting : (string * string) list -> string * int -> int
       
    28   val cpu_time : ('a -> 'b) -> 'a -> 'b * int
    28 end
    29 end
    29 
    30 
    30 
    31 
    31 
    32 
    32 structure Mirabelle : MIRABELLE =
    33 structure Mirabelle : MIRABELLE =
    65   let fun append_to n = if n = "" then K () else File.append (Path.explode n)
    66   let fun append_to n = if n = "" then K () else File.append (Path.explode n)
    66   in append_to (Config.get_thy thy logfile) (s ^ "\n") end
    67   in append_to (Config.get_thy thy logfile) (s ^ "\n") end
    67   (* FIXME: with multithreading and parallel proofs enabled, we might need to
    68   (* FIXME: with multithreading and parallel proofs enabled, we might need to
    68      encapsulate this inside a critical section *)
    69      encapsulate this inside a critical section *)
    69 
    70 
    70 fun log_block thy msg = log thy (msg ^ "\n------------------")
    71 fun log_sep thy = log thy "------------------"
    71 fun log_action thy name msg = log_block thy (name ^ ": " ^ msg)
       
    72 
    72 
    73 fun capture_exns logf f x =
    73 fun try_with f NONE = SOME ()
    74   let
    74   | try_with f (SOME e) = (f e; try (fn () => reraise e) ())
    75     fun f' x = f x
    75 
    76       handle TimeLimit.TimeOut => logf "time out"
    76 fun capture_exns thy name f x =
    77            | ERROR msg => logf ("error: " ^ msg)
    77   (case try_with I (Exn.get_exn (Exn.capture f x)) of
    78   in (case try f' x of NONE => logf "exception" | _ => ()) end
    78     NONE => (log thy ("Unhandled exception in " ^ quote name); log_sep thy)
       
    79   | SOME _ => log_sep thy)
    79 
    80 
    80 fun apply_actions thy info (pre, post, time) actions =
    81 fun apply_actions thy info (pre, post, time) actions =
    81   let
    82   let
    82     val _ = log_block thy info
       
    83     fun apply (name, action) =
    83     fun apply (name, action) =
    84       let val st = {pre=pre, post=post, timeout=time, log=log_action thy name}
    84       {pre=pre, post=post, timeout=time, log=log thy}
    85       in capture_exns (log_action thy name) action st end
    85       |> capture_exns thy name action
    86   in List.app apply actions end
    86   in (log thy info; log_sep thy; List.app apply actions) end
    87 
    87 
    88 fun in_range _ _ NONE = true
    88 fun in_range _ _ NONE = true
    89   | in_range l r (SOME i) = (l <= i andalso (r < 0 orelse i <= r))
    89   | in_range l r (SOME i) = (l <= i andalso (r < 0 orelse i <= r))
    90 
    90 
    91 fun only_within_range thy pos f x =
    91 fun only_within_range thy pos f x =
   180   (case Option.map Int.fromString (AList.lookup (op =) settings key) of
   180   (case Option.map Int.fromString (AList.lookup (op =) settings key) of
   181     SOME (SOME i) => i
   181     SOME (SOME i) => i
   182   | SOME NONE => error ("bad option: " ^ key)
   182   | SOME NONE => error ("bad option: " ^ key)
   183   | NONE => default)
   183   | NONE => default)
   184 
   184 
       
   185 fun cpu_time f x =
       
   186   let
       
   187     val start = start_timing ()
       
   188     val result = Exn.capture (fn () => f x) ()
       
   189     val time = Time.toMilliseconds (#cpu (end_timing start))
       
   190   in (Exn.release result, time) end
       
   191 
   185 end
   192 end