src/HOL/Mirabelle/Tools/mirabelle_sledgehammer.ML
author boehmes
Sat, 05 Sep 2009 11:45:57 +0200
changeset 32521 f20cc66b2c74
parent 32518 e3c4e337196c
child 32523 ba397aa0ff5d
permissions -rw-r--r--
added initialization and cleanup of actions, added option to suppress Isabelle output, sledgehammer action produces its own report (no need for additional perl script)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32385
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents:
diff changeset
     1
(* Title:  mirabelle_sledgehammer.ML
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents:
diff changeset
     2
   Author: Jasmin Blanchette and Sascha Boehme
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents:
diff changeset
     3
*)
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents:
diff changeset
     4
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents:
diff changeset
     5
structure Mirabelle_Sledgehammer : MIRABELLE_ACTION =
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents:
diff changeset
     6
struct
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents:
diff changeset
     7
32521
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
     8
val proverK = "prover"
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
     9
val keepK = "keep"
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    10
val metisK = "metis"
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    11
val full_typesK = "full_types"
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    12
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    13
fun sh_tag id = "#" ^ string_of_int id ^ " sledgehammer: "
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    14
fun metis_tag id = "#" ^ string_of_int id ^ " metis (sledgehammer): "
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    15
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    16
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    17
datatype data = Data of {
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    18
  sh_calls: int,
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    19
  sh_success: int,
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    20
  sh_time: int,
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    21
  metis_calls: int,
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    22
  metis_success: int,
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    23
  metis_time: int,
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    24
  metis_timeout: int }
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    25
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    26
fun make_data (sh_calls, sh_success, sh_time, metis_calls, metis_success,
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    27
    metis_time, metis_timeout) =
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    28
  Data {sh_calls=sh_calls, sh_success=sh_success, sh_time=sh_time,
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    29
    metis_calls=metis_calls, metis_success=metis_success,
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    30
    metis_time=metis_time, metis_timeout=metis_timeout}
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    31
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    32
fun map_data f (Data {sh_calls, sh_success, sh_time, metis_calls,
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    33
    metis_success, metis_time, metis_timeout}) =
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    34
  make_data (f (sh_calls, sh_success, sh_time, metis_calls, metis_success,
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    35
    metis_time, metis_timeout))
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    36
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    37
val empty_data = make_data (0, 0, 0, 0, 0, 0, 0)
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    38
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    39
val inc_sh_calls = map_data (fn (sh_calls, sh_success, sh_time, metis_calls,
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    40
  metis_success, metis_time, metis_timeout) => (sh_calls + 1, sh_success,
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    41
  sh_time, metis_calls, metis_success, metis_time, metis_timeout))
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    42
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    43
val inc_sh_success = map_data (fn (sh_calls, sh_success, sh_time, metis_calls,
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    44
  metis_success, metis_time, metis_timeout) => (sh_calls, sh_success + 1,
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    45
  sh_time, metis_calls, metis_success, metis_time, metis_timeout))
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    46
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    47
fun inc_sh_time t = map_data (fn (sh_calls, sh_success, sh_time, metis_calls,
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    48
  metis_success, metis_time, metis_timeout) => (sh_calls, sh_success,
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    49
  sh_time + t, metis_calls, metis_success, metis_time, metis_timeout))
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    50
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    51
val inc_metis_calls = map_data (fn (sh_calls, sh_success, sh_time, metis_calls,
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    52
  metis_success, metis_time, metis_timeout) => (sh_calls, sh_success,
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    53
  sh_time, metis_calls + 1, metis_success, metis_time, metis_timeout))
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    54
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    55
val inc_metis_success = map_data (fn (sh_calls, sh_success, sh_time,
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    56
  metis_calls, metis_success, metis_time, metis_timeout) => (sh_calls,
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    57
  sh_success, sh_time, metis_calls, metis_success + 1, metis_time,
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    58
  metis_timeout))
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    59
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    60
fun inc_metis_time t = map_data (fn (sh_calls, sh_success, sh_time,
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    61
  metis_calls, metis_success, metis_time, metis_timeout) => (sh_calls,
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    62
  sh_success, sh_time, metis_calls, metis_success, metis_time + t,
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    63
  metis_timeout))
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    64
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    65
val inc_metis_timeout = map_data (fn (sh_calls, sh_success, sh_time,
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    66
  metis_calls, metis_success, metis_time, metis_timeout) => (sh_calls,
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    67
  sh_success, sh_time, metis_calls, metis_success, metis_time,
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    68
  metis_timeout + 1))
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    69
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    70
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    71
local
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    72
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    73
val str = string_of_int
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    74
val str3 = Real.fmt (StringCvt.FIX (SOME 3))
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    75
fun percentage a b = string_of_int (a * 100 div b)
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    76
fun time t = Real.fromInt t / 1000.0
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    77
fun avg_time t n =
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    78
  if n > 0 then (Real.fromInt t / 1000.0) / Real.fromInt n else 0.0
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    79
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    80
fun log_sh_data log sh_calls sh_success sh_time =
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    81
 (log ("Total number of sledgehammer calls: " ^ str sh_calls);
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    82
  log ("Number of successful sledgehammer calls: " ^ str sh_success);
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    83
  log ("Success rate: " ^ percentage sh_success sh_calls ^ "%");
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    84
  log ("Total time for successful sledgehammer calls: " ^ str3 (time sh_time));
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    85
  log ("Average time for successful sledgehammer calls: " ^
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    86
    str3 (avg_time sh_time sh_success)))
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    87
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    88
fun log_metis_data log sh_success metis_calls metis_success metis_time
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    89
    metis_timeout =
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    90
 (log ("Total number of metis calls: " ^ str metis_calls);
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    91
  log ("Number of successful metis calls: " ^ str metis_success);
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    92
  log ("Number of metis timeouts: " ^ str metis_timeout);
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    93
  log ("Number of metis exceptions: " ^
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    94
    str (sh_success - metis_success - metis_timeout));
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    95
  log ("Success rate: " ^ percentage metis_success metis_calls ^ "%");
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    96
  log ("Total time for successful metis calls: " ^ str3 (time metis_time));
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    97
  log ("Average time for successful metis calls: " ^
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    98
    str3 (avg_time metis_time metis_success)))
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
    99
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   100
in
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   101
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   102
fun log_data id log (Data {sh_calls, sh_success, sh_time, metis_calls,
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   103
    metis_success, metis_time, metis_timeout}) =
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   104
  if sh_calls > 0
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   105
  then
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   106
   (log ("\n\n\nReport #" ^ string_of_int id ^ ":\n");
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   107
    log_sh_data log sh_calls sh_success sh_time;
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   108
    log "";
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   109
    if metis_calls > 0 then log_metis_data log sh_success metis_calls
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   110
      metis_success metis_time metis_timeout else ())
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   111
  else ()
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   112
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   113
end
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   114
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   115
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   116
(* Warning: we implicitly assume single-threaded execution here! *)
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   117
val data = ref ([] : (int * data) list)
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   118
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   119
fun init id thy = (change data (cons (id, empty_data)); thy)
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   120
fun done id {log, ...} =
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   121
  AList.lookup (op =) (!data) id
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   122
  |> Option.map (log_data id log)
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   123
  |> K ()
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   124
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   125
fun change_data id f = (change data (AList.map_entry (op =) id f); ())
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   126
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   127
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   128
local
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   129
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   130
fun safe init done f x =
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   131
  let
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   132
    val y = init x
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   133
    val z = Exn.capture f y
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   134
    val _ = done y
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   135
  in Exn.release z end
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   136
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   137
fun init_sh NONE = !AtpWrapper.destdir
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   138
  | init_sh (SOME path) =
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   139
      let
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   140
        (* Warning: we implicitly assume single-threaded execution here! *)
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   141
        val old = !AtpWrapper.destdir
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   142
        val _ = AtpWrapper.destdir := path
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   143
      in old end
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   144
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   145
fun done_sh path = AtpWrapper.destdir := path
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   146
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   147
fun run_sh prover_name timeout st _ =
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   148
  let
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   149
    val prover = the (AtpManager.get_prover prover_name (Proof.theory_of st))
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   150
    val atp_timeout = AtpManager.get_timeout () 
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   151
    val atp = prover atp_timeout NONE NONE prover_name 1
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   152
    val ((success, (message, thm_names), time, _, _, _), time') =
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   153
      TimeLimit.timeLimit timeout (Mirabelle.cpu_time atp) (Proof.get_goal st)
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   154
  in
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   155
    if success then (message, SOME (time + time', thm_names))
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   156
    else (message, NONE)
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   157
  end
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   158
  handle ResHolClause.TOO_TRIVIAL => ("trivial", SOME (0, []))
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   159
       | TimeLimit.TimeOut => ("timeout", NONE)
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   160
       | ERROR msg => ("error: " ^ msg, NONE)
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   161
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   162
in
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   163
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   164
fun run_sledgehammer args thm_names id {pre=st, timeout, log, ...} =
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   165
  let
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   166
    val _ = change_data id inc_sh_calls 
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   167
    val prover_name =
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   168
      AList.lookup (op =) args proverK
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   169
      |> the_default (hd (space_explode " " (AtpManager.get_atps ())))
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   170
    val dir = AList.lookup (op =) args keepK
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   171
    val (msg, result) =
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   172
      safe init_sh done_sh (run_sh prover_name timeout st) dir
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   173
    val _ =
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   174
      if is_some result
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   175
      then
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   176
        let
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   177
          val time = fst (the result)
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   178
          val _ = change_data id inc_sh_success
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   179
          val _ = change_data id (inc_sh_time time)
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   180
        in
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   181
          log (sh_tag id ^ "succeeded (" ^ string_of_int time ^ ") [" ^
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   182
            prover_name ^ "]:\n" ^ msg)
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   183
        end
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   184
      else log (sh_tag id ^ "failed: " ^ msg)
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   185
  in change thm_names (K (Option.map snd result)) end
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   186
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   187
end
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   188
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   189
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   190
local
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   191
32454
a1a5589207ad Mirabelle: proper parsing of theorem names found by sledgehammer, respecting test intervals given along with file names
boehmes
parents: 32452
diff changeset
   192
fun thms_of_name ctxt name =
a1a5589207ad Mirabelle: proper parsing of theorem names found by sledgehammer, respecting test intervals given along with file names
boehmes
parents: 32452
diff changeset
   193
  let
a1a5589207ad Mirabelle: proper parsing of theorem names found by sledgehammer, respecting test intervals given along with file names
boehmes
parents: 32452
diff changeset
   194
    val lex = OuterKeyword.get_lexicons
a1a5589207ad Mirabelle: proper parsing of theorem names found by sledgehammer, respecting test intervals given along with file names
boehmes
parents: 32452
diff changeset
   195
    val get = maps (ProofContext.get_fact ctxt o fst)
a1a5589207ad Mirabelle: proper parsing of theorem names found by sledgehammer, respecting test intervals given along with file names
boehmes
parents: 32452
diff changeset
   196
  in
a1a5589207ad Mirabelle: proper parsing of theorem names found by sledgehammer, respecting test intervals given along with file names
boehmes
parents: 32452
diff changeset
   197
    Source.of_string name
a1a5589207ad Mirabelle: proper parsing of theorem names found by sledgehammer, respecting test intervals given along with file names
boehmes
parents: 32452
diff changeset
   198
    |> Symbol.source {do_recover=false}
a1a5589207ad Mirabelle: proper parsing of theorem names found by sledgehammer, respecting test intervals given along with file names
boehmes
parents: 32452
diff changeset
   199
    |> OuterLex.source {do_recover=SOME false} lex Position.start
a1a5589207ad Mirabelle: proper parsing of theorem names found by sledgehammer, respecting test intervals given along with file names
boehmes
parents: 32452
diff changeset
   200
    |> OuterLex.source_proper
a1a5589207ad Mirabelle: proper parsing of theorem names found by sledgehammer, respecting test intervals given along with file names
boehmes
parents: 32452
diff changeset
   201
    |> Source.source OuterLex.stopper (SpecParse.xthms1 >> get) NONE
a1a5589207ad Mirabelle: proper parsing of theorem names found by sledgehammer, respecting test intervals given along with file names
boehmes
parents: 32452
diff changeset
   202
    |> Source.exhaust
a1a5589207ad Mirabelle: proper parsing of theorem names found by sledgehammer, respecting test intervals given along with file names
boehmes
parents: 32452
diff changeset
   203
  end
32452
d84edd022efe apply metis with found theorems in case sledgehammer was successful
boehmes
parents: 32434
diff changeset
   204
32498
1132c7c13f36 Mirabelle: actions are responsible for handling exceptions,
boehmes
parents: 32496
diff changeset
   205
in
1132c7c13f36 Mirabelle: actions are responsible for handling exceptions,
boehmes
parents: 32496
diff changeset
   206
32521
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   207
fun run_metis args thm_names id {pre=st, timeout, log, ...} =
32385
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents:
diff changeset
   208
  let
32521
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   209
    fun get_thms ctxt = maps (thms_of_name ctxt)
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   210
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   211
    fun metis thms ctxt = MetisTools.metis_tac ctxt thms
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   212
    fun apply_metis thms = Mirabelle.can_apply timeout (metis thms) st
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   213
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   214
    fun with_time (false, t) = "failed (" ^ string_of_int t ^ ")"
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   215
      | with_time (true, t) = (change_data id inc_metis_success;
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   216
          change_data id (inc_metis_time t);
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   217
          "succeeded (" ^ string_of_int t ^ ")")
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   218
    fun timed_metis thms = with_time (Mirabelle.cpu_time apply_metis thms)
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   219
      handle TimeLimit.TimeOut => (change_data id inc_metis_timeout; "timeout")
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   220
           | ERROR msg => "error: " ^ msg
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   221
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   222
    val _ = log "-----"
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   223
    val _ = change_data id inc_metis_calls
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   224
  in
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   225
    thm_names
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   226
    |> get_thms (Proof.context_of st)
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   227
    |> timed_metis
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   228
    |> log o prefix (metis_tag id) 
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   229
  end
32385
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents:
diff changeset
   230
32498
1132c7c13f36 Mirabelle: actions are responsible for handling exceptions,
boehmes
parents: 32496
diff changeset
   231
end
1132c7c13f36 Mirabelle: actions are responsible for handling exceptions,
boehmes
parents: 32496
diff changeset
   232
32452
d84edd022efe apply metis with found theorems in case sledgehammer was successful
boehmes
parents: 32434
diff changeset
   233
32521
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   234
fun sledgehammer_action args id (st as {log, ...}) =
32515
e7c0d3c0494a Mirabelle: actions are responsible for catching exceptions and producing suitable log messages (makes log message uniform),
boehmes
parents: 32511
diff changeset
   235
  let
e7c0d3c0494a Mirabelle: actions are responsible for catching exceptions and producing suitable log messages (makes log message uniform),
boehmes
parents: 32511
diff changeset
   236
    val thm_names = ref (NONE : string list option)
32521
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   237
    val _ = Mirabelle.catch sh_tag (run_sledgehammer args thm_names) id st
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   238
  in
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   239
    if AList.defined (op =) args metisK andalso is_some (!thm_names)
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   240
    then Mirabelle.catch metis_tag (run_metis args (these (!thm_names))) id st
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   241
    else ()
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   242
  end
32385
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents:
diff changeset
   243
32511
43d2ac4aa2de added option full_typed for sledgehammer action
boehmes
parents: 32510
diff changeset
   244
fun invoke args =
32515
e7c0d3c0494a Mirabelle: actions are responsible for catching exceptions and producing suitable log messages (makes log message uniform),
boehmes
parents: 32511
diff changeset
   245
  let
e7c0d3c0494a Mirabelle: actions are responsible for catching exceptions and producing suitable log messages (makes log message uniform),
boehmes
parents: 32511
diff changeset
   246
    val _ = AtpManager.set_full_types (AList.defined (op =) args full_typesK)
32521
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32518
diff changeset
   247
  in Mirabelle.register (init, sledgehammer_action args, done) end
32385
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents:
diff changeset
   248
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents:
diff changeset
   249
end