src/HOL/Mirabelle/Tools/mirabelle.ML
author wenzelm
Thu, 09 Sep 2010 17:20:27 +0200
changeset 39232 69c6d3e87660
parent 36787 f60e4dd6d76f
child 39377 9e544eb396dc
permissions -rw-r--r--
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32564
378528d2f7eb standard headers and text sections;
wenzelm
parents: 32551
diff changeset
     1
(*  Title:      HOL/Mirabelle/Tools/mirabelle.ML
378528d2f7eb standard headers and text sections;
wenzelm
parents: 32551
diff changeset
     2
    Author:     Jasmin Blanchette and Sascha Boehme, TU Munich
32381
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
     3
*)
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
     4
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
     5
signature MIRABELLE =
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
     6
sig
32564
378528d2f7eb standard headers and text sections;
wenzelm
parents: 32551
diff changeset
     7
  (*configuration*)
32396
e756600502cc keep the modified (tested) theory,
boehmes
parents: 32385
diff changeset
     8
  val logfile : string Config.T
32381
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
     9
  val timeout : int Config.T
32382
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents: 32381
diff changeset
    10
  val start_line : int Config.T
98674ac811c4 Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents: 32381
diff changeset
    11
  val end_line : int Config.T
32495
6decc1ffdbed removed unused signature
boehmes
parents: 32489
diff changeset
    12
  val setup : theory -> theory
32381
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
    13
32564
378528d2f7eb standard headers and text sections;
wenzelm
parents: 32551
diff changeset
    14
  (*core*)
32567
de411627a985 explicitly export type abbreviations (as usual in SML97);
wenzelm
parents: 32564
diff changeset
    15
  type init_action = int -> theory -> theory
de411627a985 explicitly export type abbreviations (as usual in SML97);
wenzelm
parents: 32564
diff changeset
    16
  type done_args = {last: Toplevel.state, log: string -> unit}
de411627a985 explicitly export type abbreviations (as usual in SML97);
wenzelm
parents: 32564
diff changeset
    17
  type done_action = int -> done_args -> unit
de411627a985 explicitly export type abbreviations (as usual in SML97);
wenzelm
parents: 32564
diff changeset
    18
  type run_args = {pre: Proof.state, post: Toplevel.state option,
32676
b1c85a117dec record how many "proof"s are solved by s/h
nipkow
parents: 32567
diff changeset
    19
    timeout: Time.time, log: string -> unit, pos: Position.T, name: string}
32567
de411627a985 explicitly export type abbreviations (as usual in SML97);
wenzelm
parents: 32564
diff changeset
    20
  type run_action = int -> run_args -> unit
de411627a985 explicitly export type abbreviations (as usual in SML97);
wenzelm
parents: 32564
diff changeset
    21
  type action = init_action * run_action * done_action
32521
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32515
diff changeset
    22
  val catch : (int -> string) -> run_action -> run_action
34052
b2e6245fb3da only invoke metisFT if metis failed
boehmes
parents: 34035
diff changeset
    23
  val catch_result : (int -> string) -> 'a -> (int -> run_args -> 'a) ->
b2e6245fb3da only invoke metisFT if metis failed
boehmes
parents: 34035
diff changeset
    24
    int -> run_args -> 'a
32515
e7c0d3c0494a Mirabelle: actions are responsible for catching exceptions and producing suitable log messages (makes log message uniform),
boehmes
parents: 32504
diff changeset
    25
  val register : action -> theory -> theory
32495
6decc1ffdbed removed unused signature
boehmes
parents: 32489
diff changeset
    26
  val step_hook : Toplevel.transition -> Toplevel.state -> Toplevel.state ->
6decc1ffdbed removed unused signature
boehmes
parents: 32489
diff changeset
    27
    unit
6decc1ffdbed removed unused signature
boehmes
parents: 32489
diff changeset
    28
32564
378528d2f7eb standard headers and text sections;
wenzelm
parents: 32551
diff changeset
    29
  (*utility functions*)
32469
1ad7d4fc0954 Mirabelle: added preliminary documentation,
boehmes
parents: 32468
diff changeset
    30
  val can_apply : Time.time -> (Proof.context -> int -> tactic) ->
1ad7d4fc0954 Mirabelle: added preliminary documentation,
boehmes
parents: 32468
diff changeset
    31
    Proof.state -> bool
33243
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 32676
diff changeset
    32
  val theorems_in_proof_term : thm -> thm list
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 32676
diff changeset
    33
  val theorems_of_sucessful_proof : Toplevel.state option -> thm list
32385
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents: 32383
diff changeset
    34
  val get_setting : (string * string) list -> string * string -> string
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents: 32383
diff changeset
    35
  val get_int_setting : (string * string) list -> string * int -> int
32498
1132c7c13f36 Mirabelle: actions are responsible for handling exceptions,
boehmes
parents: 32497
diff changeset
    36
  val cpu_time : ('a -> 'b) -> 'a -> 'b * int
32381
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
    37
end
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
    38
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
    39
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
    40
32497
922718ac81e4 removed errors overseen in previous changes
boehmes
parents: 32496
diff changeset
    41
structure Mirabelle : MIRABELLE =
32495
6decc1ffdbed removed unused signature
boehmes
parents: 32489
diff changeset
    42
struct
6decc1ffdbed removed unused signature
boehmes
parents: 32489
diff changeset
    43
6decc1ffdbed removed unused signature
boehmes
parents: 32489
diff changeset
    44
(* Mirabelle configuration *)
6decc1ffdbed removed unused signature
boehmes
parents: 32489
diff changeset
    45
36001
992839c4be90 static defaults for configuration options;
wenzelm
parents: 35592
diff changeset
    46
val (logfile, setup1) = Attrib.config_string "mirabelle_logfile" (K "")
992839c4be90 static defaults for configuration options;
wenzelm
parents: 35592
diff changeset
    47
val (timeout, setup2) = Attrib.config_int "mirabelle_timeout" (K 30)
992839c4be90 static defaults for configuration options;
wenzelm
parents: 35592
diff changeset
    48
val (start_line, setup3) = Attrib.config_int "mirabelle_start_line" (K 0)
992839c4be90 static defaults for configuration options;
wenzelm
parents: 35592
diff changeset
    49
val (end_line, setup4) = Attrib.config_int "mirabelle_end_line" (K ~1)
32495
6decc1ffdbed removed unused signature
boehmes
parents: 32489
diff changeset
    50
6decc1ffdbed removed unused signature
boehmes
parents: 32489
diff changeset
    51
val setup = setup1 #> setup2 #> setup3 #> setup4
32385
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents: 32383
diff changeset
    52
594890623c46 split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents: 32383
diff changeset
    53
32381
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
    54
(* Mirabelle core *)
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
    55
32521
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32515
diff changeset
    56
type init_action = int -> theory -> theory
32567
de411627a985 explicitly export type abbreviations (as usual in SML97);
wenzelm
parents: 32564
diff changeset
    57
type done_args = {last: Toplevel.state, log: string -> unit}
de411627a985 explicitly export type abbreviations (as usual in SML97);
wenzelm
parents: 32564
diff changeset
    58
type done_action = int -> done_args -> unit
de411627a985 explicitly export type abbreviations (as usual in SML97);
wenzelm
parents: 32564
diff changeset
    59
type run_args = {pre: Proof.state, post: Toplevel.state option,
32676
b1c85a117dec record how many "proof"s are solved by s/h
nipkow
parents: 32567
diff changeset
    60
  timeout: Time.time, log: string -> unit, pos: Position.T, name: string}
32567
de411627a985 explicitly export type abbreviations (as usual in SML97);
wenzelm
parents: 32564
diff changeset
    61
type run_action = int -> run_args -> unit
32521
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32515
diff changeset
    62
type action = init_action * run_action * done_action
32381
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
    63
33522
737589bb9bb8 adapted Theory_Data;
wenzelm
parents: 33292
diff changeset
    64
structure Actions = Theory_Data
32381
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
    65
(
32521
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32515
diff changeset
    66
  type T = (int * run_action * done_action) list
32515
e7c0d3c0494a Mirabelle: actions are responsible for catching exceptions and producing suitable log messages (makes log message uniform),
boehmes
parents: 32504
diff changeset
    67
  val empty = []
32381
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
    68
  val extend = I
33522
737589bb9bb8 adapted Theory_Data;
wenzelm
parents: 33292
diff changeset
    69
  fun merge data = Library.merge (K true) data  (* FIXME ?!? *)
32381
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
    70
)
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
    71
32515
e7c0d3c0494a Mirabelle: actions are responsible for catching exceptions and producing suitable log messages (makes log message uniform),
boehmes
parents: 32504
diff changeset
    72
34035
08d34921b7dd also consider the fully-typed version of metis for Mirabelle measurements
boehmes
parents: 33522
diff changeset
    73
fun log_exn log tag id e = log (tag id ^ "exception:\n" ^ General.exnMessage e)
08d34921b7dd also consider the fully-typed version of metis for Mirabelle measurements
boehmes
parents: 33522
diff changeset
    74
34052
b2e6245fb3da only invoke metisFT if metis failed
boehmes
parents: 34035
diff changeset
    75
fun catch tag f id (st as {log, ...}: run_args) = (f id st; ())
39232
69c6d3e87660 more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents: 36787
diff changeset
    76
  handle exn =>
69c6d3e87660 more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents: 36787
diff changeset
    77
    if Exn.is_interrupt exn then reraise exn else (log_exn log tag id exn; ())
34035
08d34921b7dd also consider the fully-typed version of metis for Mirabelle measurements
boehmes
parents: 33522
diff changeset
    78
34052
b2e6245fb3da only invoke metisFT if metis failed
boehmes
parents: 34035
diff changeset
    79
fun catch_result tag d f id (st as {log, ...}: run_args) = f id st
39232
69c6d3e87660 more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents: 36787
diff changeset
    80
  handle exn =>
69c6d3e87660 more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents: 36787
diff changeset
    81
    if Exn.is_interrupt exn then reraise exn else (log_exn log tag id exn; d)
32515
e7c0d3c0494a Mirabelle: actions are responsible for catching exceptions and producing suitable log messages (makes log message uniform),
boehmes
parents: 32504
diff changeset
    82
32521
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32515
diff changeset
    83
fun register (init, run, done) thy =
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32515
diff changeset
    84
  let val id = length (Actions.get thy) + 1
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32515
diff changeset
    85
  in
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32515
diff changeset
    86
    thy
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32515
diff changeset
    87
    |> init id
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32515
diff changeset
    88
    |> Actions.map (cons (id, run, done))
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32515
diff changeset
    89
  end
32381
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
    90
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
    91
local
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
    92
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
    93
fun log thy s =
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
    94
  let fun append_to n = if n = "" then K () else File.append (Path.explode n)
36787
f60e4dd6d76f renamed Config.get_thy to Config.get_global etc. to indicate that this is not the real thing;
wenzelm
parents: 36001
diff changeset
    95
  in append_to (Config.get_global thy logfile) (s ^ "\n") end
32381
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
    96
  (* FIXME: with multithreading and parallel proofs enabled, we might need to
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
    97
     encapsulate this inside a critical section *)
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
    98
32498
1132c7c13f36 Mirabelle: actions are responsible for handling exceptions,
boehmes
parents: 32497
diff changeset
    99
fun log_sep thy = log thy "------------------"
1132c7c13f36 Mirabelle: actions are responsible for handling exceptions,
boehmes
parents: 32497
diff changeset
   100
32676
b1c85a117dec record how many "proof"s are solved by s/h
nipkow
parents: 32567
diff changeset
   101
fun apply_actions thy pos name info (pre, post, time) actions =
32472
7b92a8b8daaf Mirabelle: actions are responsible for their log messages, output is better readable
boehmes
parents: 32469
diff changeset
   102
  let
32676
b1c85a117dec record how many "proof"s are solved by s/h
nipkow
parents: 32567
diff changeset
   103
    fun apply f = f {pre=pre, post=post, timeout=time, log=log thy, pos=pos, name=name}
32521
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32515
diff changeset
   104
    fun run (id, run, _) = (apply (run id); log_sep thy)
32515
e7c0d3c0494a Mirabelle: actions are responsible for catching exceptions and producing suitable log messages (makes log message uniform),
boehmes
parents: 32504
diff changeset
   105
  in (log thy info; log_sep thy; List.app run actions) end
32381
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   106
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   107
fun in_range _ _ NONE = true
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   108
  | in_range l r (SOME i) = (l <= i andalso (r < 0 orelse i <= r))
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   109
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   110
fun only_within_range thy pos f x =
36787
f60e4dd6d76f renamed Config.get_thy to Config.get_global etc. to indicate that this is not the real thing;
wenzelm
parents: 36001
diff changeset
   111
  let val l = Config.get_global thy start_line and r = Config.get_global thy end_line
32472
7b92a8b8daaf Mirabelle: actions are responsible for their log messages, output is better readable
boehmes
parents: 32469
diff changeset
   112
  in if in_range l r (Position.line_of pos) then f x else () end
32381
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   113
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   114
in
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   115
32521
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32515
diff changeset
   116
fun run_actions tr pre post =
32381
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   117
  let
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   118
    val thy = Proof.theory_of pre
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   119
    val pos = Toplevel.pos_of tr
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   120
    val name = Toplevel.name_of tr
36787
f60e4dd6d76f renamed Config.get_thy to Config.get_global etc. to indicate that this is not the real thing;
wenzelm
parents: 36001
diff changeset
   121
    val st = (pre, post, Time.fromSeconds (Config.get_global thy timeout))
32472
7b92a8b8daaf Mirabelle: actions are responsible for their log messages, output is better readable
boehmes
parents: 32469
diff changeset
   122
7b92a8b8daaf Mirabelle: actions are responsible for their log messages, output is better readable
boehmes
parents: 32469
diff changeset
   123
    val str0 = string_of_int o the_default 0
7b92a8b8daaf Mirabelle: actions are responsible for their log messages, output is better readable
boehmes
parents: 32469
diff changeset
   124
    val loc = str0 (Position.line_of pos) ^ ":" ^ str0 (Position.column_of pos)
7b92a8b8daaf Mirabelle: actions are responsible for their log messages, output is better readable
boehmes
parents: 32469
diff changeset
   125
    val info = "\n\nat " ^ loc ^ " (" ^ name ^ "):"
32381
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   126
  in
32676
b1c85a117dec record how many "proof"s are solved by s/h
nipkow
parents: 32567
diff changeset
   127
    only_within_range thy pos (apply_actions thy pos name info st) (Actions.get thy)
32381
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   128
  end
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   129
32521
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32515
diff changeset
   130
fun done_actions st =
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32515
diff changeset
   131
  let
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32515
diff changeset
   132
    val thy = Toplevel.theory_of st
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32515
diff changeset
   133
    val _ = log thy "\n\n";
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32515
diff changeset
   134
  in
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32515
diff changeset
   135
    thy
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32515
diff changeset
   136
    |> Actions.get
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32515
diff changeset
   137
    |> List.app (fn (id, _, done) => done id {last=st, log=log thy})
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32515
diff changeset
   138
  end
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32515
diff changeset
   139
32381
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   140
end
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   141
32504
7a06bf89c038 replaced backlist by whitelist
boehmes
parents: 32503
diff changeset
   142
val whitelist = ["apply", "by", "proof"]
32468
3e6f5365971e Mirabelle: explicit command blacklist, preliminary documentation
boehmes
parents: 32396
diff changeset
   143
32381
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   144
fun step_hook tr pre post =
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   145
 (* FIXME: might require wrapping into "interruptible" *)
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   146
  if can (Proof.assert_backward o Toplevel.proof_of) pre andalso
32504
7a06bf89c038 replaced backlist by whitelist
boehmes
parents: 32503
diff changeset
   147
     member (op =) whitelist (Toplevel.name_of tr)
32521
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32515
diff changeset
   148
  then run_actions tr (Toplevel.proof_of pre) (SOME post)
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32515
diff changeset
   149
  else if not (Toplevel.is_toplevel pre) andalso Toplevel.is_toplevel post
f20cc66b2c74 added initialization and cleanup of actions,
boehmes
parents: 32515
diff changeset
   150
  then done_actions pre
32381
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   151
  else ()   (* FIXME: add theory_hook here *)
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   152
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   153
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   154
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   155
(* Mirabelle utility functions *)
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   156
32472
7b92a8b8daaf Mirabelle: actions are responsible for their log messages, output is better readable
boehmes
parents: 32469
diff changeset
   157
fun can_apply time tac st =
32469
1ad7d4fc0954 Mirabelle: added preliminary documentation,
boehmes
parents: 32468
diff changeset
   158
  let
35592
768d17f54125 use regular Proof.goal (which is what methods usually see) and prevent sledgehammer from crashing;
wenzelm
parents: 34052
diff changeset
   159
    val {context = ctxt, facts, goal} = Proof.goal st
32469
1ad7d4fc0954 Mirabelle: added preliminary documentation,
boehmes
parents: 32468
diff changeset
   160
    val full_tac = HEADGOAL (Method.insert_tac facts THEN' tac ctxt)
32381
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   161
  in
32472
7b92a8b8daaf Mirabelle: actions are responsible for their log messages, output is better readable
boehmes
parents: 32469
diff changeset
   162
    (case TimeLimit.timeLimit time (Seq.pull o full_tac) goal of
32381
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   163
      SOME (thm, _) => true
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   164
    | NONE => false)
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   165
  end
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   166
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   167
local
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   168
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   169
fun fold_body_thms f =
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   170
  let
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   171
    fun app n (PBody {thms, ...}) = thms |> fold (fn (i, (name, prop, body)) =>
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   172
      fn (x, seen) =>
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   173
        if Inttab.defined seen i then (x, seen)
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   174
        else
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   175
          let
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   176
            val body' = Future.join body
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   177
            val (x', seen') = app (n + (if name = "" then 0 else 1)) body'
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   178
              (x, Inttab.update (i, ()) seen)
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   179
        in (x' |> n = 0 ? f (name, prop, body'), seen') end)
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   180
  in fn bodies => fn x => #1 (fold (app 0) bodies (x, Inttab.empty)) end
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   181
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   182
in
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   183
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   184
fun theorems_in_proof_term thm =
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   185
  let
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   186
    val all_thms = PureThy.all_thms_of (Thm.theory_of_thm thm)
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   187
    fun collect (s, _, _) = if s <> "" then insert (op =) s else I
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   188
    fun member_of xs (x, y) = if member (op =) xs x then SOME y else NONE
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   189
    fun resolve_thms names = map_filter (member_of names) all_thms
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   190
  in
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   191
    resolve_thms (fold_body_thms collect [Thm.proof_body_of thm] [])
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   192
  end
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   193
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   194
end
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   195
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   196
fun theorems_of_sucessful_proof state =
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   197
  (case state of
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   198
    NONE => []
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   199
  | SOME st =>
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   200
      if not (Toplevel.is_proof st) then []
35592
768d17f54125 use regular Proof.goal (which is what methods usually see) and prevent sledgehammer from crashing;
wenzelm
parents: 34052
diff changeset
   201
      else theorems_in_proof_term (#goal (Proof.goal (Toplevel.proof_of st))))
32381
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   202
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   203
fun get_setting settings (key, default) =
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   204
  the_default default (AList.lookup (op =) settings key)
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   205
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   206
fun get_int_setting settings (key, default) =
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   207
  (case Option.map Int.fromString (AList.lookup (op =) settings key) of
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   208
    SOME (SOME i) => i
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   209
  | SOME NONE => error ("bad option: " ^ key)
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   210
  | NONE => default)
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   211
32498
1132c7c13f36 Mirabelle: actions are responsible for handling exceptions,
boehmes
parents: 32497
diff changeset
   212
fun cpu_time f x =
1132c7c13f36 Mirabelle: actions are responsible for handling exceptions,
boehmes
parents: 32497
diff changeset
   213
  let
1132c7c13f36 Mirabelle: actions are responsible for handling exceptions,
boehmes
parents: 32497
diff changeset
   214
    val start = start_timing ()
1132c7c13f36 Mirabelle: actions are responsible for handling exceptions,
boehmes
parents: 32497
diff changeset
   215
    val result = Exn.capture (fn () => f x) ()
1132c7c13f36 Mirabelle: actions are responsible for handling exceptions,
boehmes
parents: 32497
diff changeset
   216
    val time = Time.toMilliseconds (#cpu (end_timing start))
1132c7c13f36 Mirabelle: actions are responsible for handling exceptions,
boehmes
parents: 32497
diff changeset
   217
  in (Exn.release result, time) end
1132c7c13f36 Mirabelle: actions are responsible for handling exceptions,
boehmes
parents: 32497
diff changeset
   218
32381
11542bebe4d4 made Mirabelle a component
boehmes
parents:
diff changeset
   219
end