src/HOL/Tools/Sledgehammer/sledgehammer.ML
author blanchet
Tue, 15 Jul 2014 00:21:32 +0200
changeset 57557 242ce8d3d16b
parent 57464 3e94eb1124b0
child 57734 18bb3e1ff6f6
permissions -rw-r--r--
record MaSh algorithm in spying data
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55202
824c48a539c9 renamed many Sledgehammer ML files to clarify structure
blanchet
parents: 55201
diff changeset
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer.ML
28477
9339d4dcec8b version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff changeset
     2
    Author:     Fabian Immler, TU Muenchen
32996
d2e48879e65a removed disjunctive group cancellation -- provers run independently;
wenzelm
parents: 32995
diff changeset
     3
    Author:     Makarius
35969
c9565298df9e added support for Sledgehammer parameters;
blanchet
parents: 35867
diff changeset
     4
    Author:     Jasmin Blanchette, TU Muenchen
28477
9339d4dcec8b version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff changeset
     5
38021
e024504943d1 rename "ATP_Manager" ML module to "Sledgehammer";
blanchet
parents: 38020
diff changeset
     6
Sledgehammer's heart.
28477
9339d4dcec8b version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff changeset
     7
*)
9339d4dcec8b version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff changeset
     8
55202
824c48a539c9 renamed many Sledgehammer ML files to clarify structure
blanchet
parents: 55201
diff changeset
     9
signature SLEDGEHAMMER =
28477
9339d4dcec8b version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff changeset
    10
sig
51008
e096c0dc538b more precise output of selected facts
blanchet
parents: 51007
diff changeset
    11
  type fact = Sledgehammer_Fact.fact
48292
7fcee834c7f5 more code rationalization in relevance filter
blanchet
parents: 48289
diff changeset
    12
  type fact_override = Sledgehammer_Fact.fact_override
55287
ffa306239316 renamed ML file
blanchet
parents: 55286
diff changeset
    13
  type minimize_command = Sledgehammer_Proof_Methods.minimize_command
55201
1ee776da8da7 renamed ML file
blanchet
parents: 54816
diff changeset
    14
  type mode = Sledgehammer_Prover.mode
1ee776da8da7 renamed ML file
blanchet
parents: 54816
diff changeset
    15
  type params = Sledgehammer_Prover.params
40061
71cc5aac8b76 generalization of the Sledgehammer minimizer, to make it possible to handle SMT solvers as well
blanchet
parents: 40060
diff changeset
    16
43020
abb5d1f907e4 added "try" command, to launch Solve Direct, Quickcheck, Nitpick, Sledgehammer, and Try Methods
blanchet
parents: 43006
diff changeset
    17
  val someN : string
abb5d1f907e4 added "try" command, to launch Solve Direct, Quickcheck, Nitpick, Sledgehammer, and Try Methods
blanchet
parents: 43006
diff changeset
    18
  val noneN : string
abb5d1f907e4 added "try" command, to launch Solve Direct, Quickcheck, Nitpick, Sledgehammer, and Try Methods
blanchet
parents: 43006
diff changeset
    19
  val timeoutN : string
abb5d1f907e4 added "try" command, to launch Solve Direct, Quickcheck, Nitpick, Sledgehammer, and Try Methods
blanchet
parents: 43006
diff changeset
    20
  val unknownN : string
55212
blanchet
parents: 55202
diff changeset
    21
51010
afd0213a3dab tuned data structure
blanchet
parents: 51009
diff changeset
    22
  val string_of_factss : (string * fact list) list -> string
54816
10d48c2a3e32 made timeouts in Sledgehammer not be 'option's -- simplified lots of code
blanchet
parents: 54799
diff changeset
    23
  val run_sledgehammer : params -> mode -> (string -> unit) option -> int -> fact_override ->
10d48c2a3e32 made timeouts in Sledgehammer not be 'option's -- simplified lots of code
blanchet
parents: 54799
diff changeset
    24
    ((string * string list) list -> string -> minimize_command) -> Proof.state ->
10d48c2a3e32 made timeouts in Sledgehammer not be 'option's -- simplified lots of code
blanchet
parents: 54799
diff changeset
    25
    bool * (string * Proof.state)
28477
9339d4dcec8b version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff changeset
    26
end;
9339d4dcec8b version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff changeset
    27
55202
824c48a539c9 renamed many Sledgehammer ML files to clarify structure
blanchet
parents: 55201
diff changeset
    28
structure Sledgehammer : SLEDGEHAMMER =
28477
9339d4dcec8b version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff changeset
    29
struct
9339d4dcec8b version of sledgehammer using threads instead of processes, misc cleanup;
wenzelm
parents:
diff changeset
    30
43085
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents: 43064
diff changeset
    31
open ATP_Util
55212
blanchet
parents: 55202
diff changeset
    32
open ATP_Proof
46320
0b8b73b49848 renamed two files to make room for a new file
blanchet
parents: 46301
diff changeset
    33
open ATP_Problem_Generate
38023
962b0a7f544b more refactoring
blanchet
parents: 38021
diff changeset
    34
open Sledgehammer_Util
48250
1065c307fafe further ML structure split to permit finer-grained loading/reordering (problem to solve: MaSh needs most of Sledgehammer)
blanchet
parents: 47904
diff changeset
    35
open Sledgehammer_Fact
55287
ffa306239316 renamed ML file
blanchet
parents: 55286
diff changeset
    36
open Sledgehammer_Proof_Methods
55201
1ee776da8da7 renamed ML file
blanchet
parents: 54816
diff changeset
    37
open Sledgehammer_Prover
55212
blanchet
parents: 55202
diff changeset
    38
open Sledgehammer_Prover_ATP
55202
824c48a539c9 renamed many Sledgehammer ML files to clarify structure
blanchet
parents: 55201
diff changeset
    39
open Sledgehammer_Prover_Minimize
48381
1b7d798460bb renamed ML structures
blanchet
parents: 48321
diff changeset
    40
open Sledgehammer_MaSh
40072
27f2a45b0aab more robust handling of "remote_" vs. non-"remote_" provers
blanchet
parents: 40071
diff changeset
    41
43020
abb5d1f907e4 added "try" command, to launch Solve Direct, Quickcheck, Nitpick, Sledgehammer, and Try Methods
blanchet
parents: 43006
diff changeset
    42
val someN = "some"
abb5d1f907e4 added "try" command, to launch Solve Direct, Quickcheck, Nitpick, Sledgehammer, and Try Methods
blanchet
parents: 43006
diff changeset
    43
val noneN = "none"
abb5d1f907e4 added "try" command, to launch Solve Direct, Quickcheck, Nitpick, Sledgehammer, and Try Methods
blanchet
parents: 43006
diff changeset
    44
val timeoutN = "timeout"
abb5d1f907e4 added "try" command, to launch Solve Direct, Quickcheck, Nitpick, Sledgehammer, and Try Methods
blanchet
parents: 43006
diff changeset
    45
val unknownN = "unknown"
abb5d1f907e4 added "try" command, to launch Solve Direct, Quickcheck, Nitpick, Sledgehammer, and Try Methods
blanchet
parents: 43006
diff changeset
    46
abb5d1f907e4 added "try" command, to launch Solve Direct, Quickcheck, Nitpick, Sledgehammer, and Try Methods
blanchet
parents: 43006
diff changeset
    47
val ordered_outcome_codes = [someN, unknownN, timeoutN, noneN]
abb5d1f907e4 added "try" command, to launch Solve Direct, Quickcheck, Nitpick, Sledgehammer, and Try Methods
blanchet
parents: 43006
diff changeset
    48
abb5d1f907e4 added "try" command, to launch Solve Direct, Quickcheck, Nitpick, Sledgehammer, and Try Methods
blanchet
parents: 43006
diff changeset
    49
fun max_outcome_code codes =
abb5d1f907e4 added "try" command, to launch Solve Direct, Quickcheck, Nitpick, Sledgehammer, and Try Methods
blanchet
parents: 43006
diff changeset
    50
  NONE
abb5d1f907e4 added "try" command, to launch Solve Direct, Quickcheck, Nitpick, Sledgehammer, and Try Methods
blanchet
parents: 43006
diff changeset
    51
  |> fold (fn candidate =>
abb5d1f907e4 added "try" command, to launch Solve Direct, Quickcheck, Nitpick, Sledgehammer, and Try Methods
blanchet
parents: 43006
diff changeset
    52
              fn accum as SOME _ => accum
55286
blanchet
parents: 55212
diff changeset
    53
               | NONE => if member (op =) codes candidate then SOME candidate else NONE)
blanchet
parents: 55212
diff changeset
    54
       ordered_outcome_codes
43020
abb5d1f907e4 added "try" command, to launch Solve Direct, Quickcheck, Nitpick, Sledgehammer, and Try Methods
blanchet
parents: 43006
diff changeset
    55
  |> the_default unknownN
abb5d1f907e4 added "try" command, to launch Solve Direct, Quickcheck, Nitpick, Sledgehammer, and Try Methods
blanchet
parents: 43006
diff changeset
    56
57053
46000c075d07 shorten Sledgehammer output, as suggested by Andrei Popescu
blanchet
parents: 57037
diff changeset
    57
fun prover_description verbose name num_facts =
48319
340187063d84 use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents: 48299
diff changeset
    58
  (quote name,
57053
46000c075d07 shorten Sledgehammer output, as suggested by Andrei Popescu
blanchet
parents: 57037
diff changeset
    59
   if verbose then " with " ^ string_of_int num_facts ^ " fact" ^ plural_s num_facts else "")
41089
2e69fb6331cb moved function to later module
blanchet
parents: 41088
diff changeset
    60
54816
10d48c2a3e32 made timeouts in Sledgehammer not be 'option's -- simplified lots of code
blanchet
parents: 54799
diff changeset
    61
fun launch_prover (params as {debug, verbose, spy, blocking, max_facts, timeout, expect, ...}) mode
10d48c2a3e32 made timeouts in Sledgehammer not be 'option's -- simplified lots of code
blanchet
parents: 54799
diff changeset
    62
    output_result minimize_command only learn
10d48c2a3e32 made timeouts in Sledgehammer not be 'option's -- simplified lots of code
blanchet
parents: 54799
diff changeset
    63
    {comment, state, goal, subgoal, subgoal_count, factss as (_, facts) :: _} name =
41089
2e69fb6331cb moved function to later module
blanchet
parents: 41088
diff changeset
    64
  let
2e69fb6331cb moved function to later module
blanchet
parents: 41088
diff changeset
    65
    val ctxt = Proof.context_of state
53800
ac1ec5065316 added "spy" option to Sledgehammer
blanchet
parents: 53549
diff changeset
    66
54816
10d48c2a3e32 made timeouts in Sledgehammer not be 'option's -- simplified lots of code
blanchet
parents: 54799
diff changeset
    67
    val hard_timeout = time_mult 3.0 timeout
54062
427380d5d1d7 more Sledgehammer spying -- record fact indices
blanchet
parents: 54057
diff changeset
    68
    val _ = spying spy (fn () => (state, subgoal, name, "Launched"));
41089
2e69fb6331cb moved function to later module
blanchet
parents: 41088
diff changeset
    69
    val birth_time = Time.now ()
42850
c8709be8a40f distinguish between a soft timeout (30 s by defalt) and a hard timeout (60 s), to let minimization-based provers (such as CVC3, Yices, and occasionally the other provers) do their job
blanchet
parents: 42646
diff changeset
    70
    val death_time = Time.+ (birth_time, hard_timeout)
54126
6675cdc0d1ae if slicing is disabled, pick the maximum number of facts, not the number of facts in the last slice
blanchet
parents: 54090
diff changeset
    71
    val max_facts = max_facts |> the_default (default_max_facts_of_prover ctxt name)
48293
914ca0827804 renamed Sledgehammer options
blanchet
parents: 48292
diff changeset
    72
    val num_facts = length facts |> not only ? Integer.min max_facts
53800
ac1ec5065316 added "spy" option to Sledgehammer
blanchet
parents: 53549
diff changeset
    73
41089
2e69fb6331cb moved function to later module
blanchet
parents: 41088
diff changeset
    74
    val problem =
54141
f57f8e7a879f generate a comment storing the goal nickname in "learn_prover"
blanchet
parents: 54126
diff changeset
    75
      {comment = comment, state = state, goal = goal, subgoal = subgoal,
47904
67663c968d70 distinguish between instantiated and uninstantiated inductions -- the latter are OK for first-order provers
blanchet
parents: 47531
diff changeset
    76
       subgoal_count = subgoal_count,
53800
ac1ec5065316 added "spy" option to Sledgehammer
blanchet
parents: 53549
diff changeset
    77
       factss = factss
51010
afd0213a3dab tuned data structure
blanchet
parents: 51009
diff changeset
    78
         |> map (apsnd ((not (is_ho_atp ctxt name)
afd0213a3dab tuned data structure
blanchet
parents: 51009
diff changeset
    79
                         ? filter_out (fn ((_, (_, Induction)), _) => true
afd0213a3dab tuned data structure
blanchet
parents: 51009
diff changeset
    80
                                        | _ => false))
afd0213a3dab tuned data structure
blanchet
parents: 51009
diff changeset
    81
                        #> take num_facts))}
53800
ac1ec5065316 added "spy" option to Sledgehammer
blanchet
parents: 53549
diff changeset
    82
51009
e8ff34a1fa9a thread through fact triple component from which used facts come, for accurate index output
blanchet
parents: 51008
diff changeset
    83
    fun print_used_facts used_facts used_from =
e8ff34a1fa9a thread through fact triple component from which used facts come, for accurate index output
blanchet
parents: 51008
diff changeset
    84
      tag_list 1 used_from
51005
ce4290c33d73 eliminated needless speed optimization -- and simplified code quite a bit
blanchet
parents: 51004
diff changeset
    85
      |> map (fn (j, fact) => fact |> apsnd (K j))
48798
9152e66f98da be less aggressive at kicking out chained facts
blanchet
parents: 48407
diff changeset
    86
      |> filter_used_facts false used_facts
48394
82fc8c956cdc fixed various issues with MaSh's file handling + tune output + generate local facts again + handle nameless facts gracefully
blanchet
parents: 48384
diff changeset
    87
      |> map (fn ((name, _), j) => name ^ "@" ^ string_of_int j)
82fc8c956cdc fixed various issues with MaSh's file handling + tune output + generate local facts again + handle nameless facts gracefully
blanchet
parents: 48384
diff changeset
    88
      |> commas
82fc8c956cdc fixed various issues with MaSh's file handling + tune output + generate local facts again + handle nameless facts gracefully
blanchet
parents: 48384
diff changeset
    89
      |> enclose ("Fact" ^ plural_s (length facts) ^ " in " ^ quote name ^
82fc8c956cdc fixed various issues with MaSh's file handling + tune output + generate local facts again + handle nameless facts gracefully
blanchet
parents: 48384
diff changeset
    90
                  " proof (of " ^ string_of_int (length facts) ^ "): ") "."
82fc8c956cdc fixed various issues with MaSh's file handling + tune output + generate local facts again + handle nameless facts gracefully
blanchet
parents: 48384
diff changeset
    91
      |> Output.urgent_message
53800
ac1ec5065316 added "spy" option to Sledgehammer
blanchet
parents: 53549
diff changeset
    92
54062
427380d5d1d7 more Sledgehammer spying -- record fact indices
blanchet
parents: 54057
diff changeset
    93
    fun spying_str_of_res ({outcome = NONE, used_facts, used_from, ...} : prover_result) =
427380d5d1d7 more Sledgehammer spying -- record fact indices
blanchet
parents: 54057
diff changeset
    94
        let
427380d5d1d7 more Sledgehammer spying -- record fact indices
blanchet
parents: 54057
diff changeset
    95
          val num_used_facts = length used_facts
54063
c0658286aa08 more thorough spying
blanchet
parents: 54062
diff changeset
    96
c0658286aa08 more thorough spying
blanchet
parents: 54062
diff changeset
    97
          fun find_indices facts =
c0658286aa08 more thorough spying
blanchet
parents: 54062
diff changeset
    98
            tag_list 1 facts
54062
427380d5d1d7 more Sledgehammer spying -- record fact indices
blanchet
parents: 54057
diff changeset
    99
            |> map (fn (j, fact) => fact |> apsnd (K j))
427380d5d1d7 more Sledgehammer spying -- record fact indices
blanchet
parents: 54057
diff changeset
   100
            |> filter_used_facts false used_facts
54773
79f66cd15d57 fixed source of 'Subscript' exception
blanchet
parents: 54503
diff changeset
   101
            |> distinct (eq_fst (op =))
54062
427380d5d1d7 more Sledgehammer spying -- record fact indices
blanchet
parents: 54057
diff changeset
   102
            |> map (prefix "@" o string_of_int o snd)
54063
c0658286aa08 more thorough spying
blanchet
parents: 54062
diff changeset
   103
c0658286aa08 more thorough spying
blanchet
parents: 54062
diff changeset
   104
          fun filter_info (fact_filter, facts) =
c0658286aa08 more thorough spying
blanchet
parents: 54062
diff changeset
   105
            let
c0658286aa08 more thorough spying
blanchet
parents: 54062
diff changeset
   106
              val indices = find_indices facts
54773
79f66cd15d57 fixed source of 'Subscript' exception
blanchet
parents: 54503
diff changeset
   107
              (* "Int.max" is there for robustness -- it shouldn't be necessary *)
79f66cd15d57 fixed source of 'Subscript' exception
blanchet
parents: 54503
diff changeset
   108
              val unknowns = replicate (Int.max (0, num_used_facts - length indices)) "?"
79f66cd15d57 fixed source of 'Subscript' exception
blanchet
parents: 54503
diff changeset
   109
            in
79f66cd15d57 fixed source of 'Subscript' exception
blanchet
parents: 54503
diff changeset
   110
              (commas (indices @ unknowns), fact_filter)
79f66cd15d57 fixed source of 'Subscript' exception
blanchet
parents: 54503
diff changeset
   111
            end
54063
c0658286aa08 more thorough spying
blanchet
parents: 54062
diff changeset
   112
c0658286aa08 more thorough spying
blanchet
parents: 54062
diff changeset
   113
          val filter_infos =
c0658286aa08 more thorough spying
blanchet
parents: 54062
diff changeset
   114
            map filter_info (("actual", used_from) :: factss)
c0658286aa08 more thorough spying
blanchet
parents: 54062
diff changeset
   115
            |> AList.group (op =)
c0658286aa08 more thorough spying
blanchet
parents: 54062
diff changeset
   116
            |> map (fn (indices, fact_filters) => commas fact_filters ^ ": " ^ indices)
54062
427380d5d1d7 more Sledgehammer spying -- record fact indices
blanchet
parents: 54057
diff changeset
   117
        in
54063
c0658286aa08 more thorough spying
blanchet
parents: 54062
diff changeset
   118
          "Success: Found proof with " ^ string_of_int num_used_facts ^
c0658286aa08 more thorough spying
blanchet
parents: 54062
diff changeset
   119
          " of " ^ string_of_int num_facts ^ " fact" ^ plural_s num_facts ^
c0658286aa08 more thorough spying
blanchet
parents: 54062
diff changeset
   120
          (if num_used_facts = 0 then "" else ": " ^ commas filter_infos)
53800
ac1ec5065316 added "spy" option to Sledgehammer
blanchet
parents: 53549
diff changeset
   121
        end
ac1ec5065316 added "spy" option to Sledgehammer
blanchet
parents: 53549
diff changeset
   122
      | spying_str_of_res {outcome = SOME failure, ...} =
54062
427380d5d1d7 more Sledgehammer spying -- record fact indices
blanchet
parents: 54057
diff changeset
   123
        "Failure: " ^ string_of_atp_failure failure
53800
ac1ec5065316 added "spy" option to Sledgehammer
blanchet
parents: 53549
diff changeset
   124
41255
a80024d7b71b added debugging option to find out how good the relevance filter was at identifying relevant facts
blanchet
parents: 41245
diff changeset
   125
    fun really_go () =
41263
4cac389c005f renamed function to run prover with minimizer and changed signature to clarify its semantics and make it a drop in replacement for "get_prover"
blanchet
parents: 41262
diff changeset
   126
      problem
51187
c344cf148e8f avoid using "smt" for minimization -- better use the prover itself, since then Sledgehammer gets to try metis again and gives the opportunity to output an Isar proof -- and show Isar proof as fallback for SMT proofs
blanchet
parents: 51010
diff changeset
   127
      |> get_minimizing_prover ctxt mode learn name params minimize_command
51009
e8ff34a1fa9a thread through fact triple component from which used facts come, for accurate index output
blanchet
parents: 51008
diff changeset
   128
      |> verbose
e8ff34a1fa9a thread through fact triple component from which used facts come, for accurate index output
blanchet
parents: 51008
diff changeset
   129
         ? tap (fn {outcome = NONE, used_facts as _ :: _, used_from, ...} =>
e8ff34a1fa9a thread through fact triple component from which used facts come, for accurate index output
blanchet
parents: 51008
diff changeset
   130
                   print_used_facts used_facts used_from
e8ff34a1fa9a thread through fact triple component from which used facts come, for accurate index output
blanchet
parents: 51008
diff changeset
   131
                 | _ => ())
57056
8b2283566f6e properly reconstruct helpers in Z3 proofs
blanchet
parents: 57053
diff changeset
   132
      |> spy ? tap (fn res => spying spy (fn () => (state, subgoal, name, spying_str_of_res res)))
43261
a4aeb26a6362 make sure that the message tail (timing + TPTP important message) is preserved upon automatic minimization
blanchet
parents: 43233
diff changeset
   133
      |> (fn {outcome, preplay, message, message_tail, ...} =>
43005
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 43004
diff changeset
   134
             (if outcome = SOME ATP_Proof.TimedOut then timeoutN
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 43004
diff changeset
   135
              else if is_some outcome then noneN
50669
84c7cf36b2e0 use "Lazy" to simplify control flow a bit and guarantee single evaluation (at most)
blanchet
parents: 50668
diff changeset
   136
              else someN, fn () => message (Lazy.force preplay) ^ message_tail))
53800
ac1ec5065316 added "spy" option to Sledgehammer
blanchet
parents: 53549
diff changeset
   137
41089
2e69fb6331cb moved function to later module
blanchet
parents: 41088
diff changeset
   138
    fun go () =
2e69fb6331cb moved function to later module
blanchet
parents: 41088
diff changeset
   139
      let
2e69fb6331cb moved function to later module
blanchet
parents: 41088
diff changeset
   140
        val (outcome_code, message) =
2e69fb6331cb moved function to later module
blanchet
parents: 41088
diff changeset
   141
          if debug then
2e69fb6331cb moved function to later module
blanchet
parents: 41088
diff changeset
   142
            really_go ()
2e69fb6331cb moved function to later module
blanchet
parents: 41088
diff changeset
   143
          else
2e69fb6331cb moved function to later module
blanchet
parents: 41088
diff changeset
   144
            (really_go ()
57056
8b2283566f6e properly reconstruct helpers in Z3 proofs
blanchet
parents: 57053
diff changeset
   145
             handle
8b2283566f6e properly reconstruct helpers in Z3 proofs
blanchet
parents: 57053
diff changeset
   146
               ERROR msg => (unknownN, fn () => "Error: " ^ msg ^ "\n")
8b2283566f6e properly reconstruct helpers in Z3 proofs
blanchet
parents: 57053
diff changeset
   147
             | exn =>
8b2283566f6e properly reconstruct helpers in Z3 proofs
blanchet
parents: 57053
diff changeset
   148
               if Exn.is_interrupt exn then reraise exn
8b2283566f6e properly reconstruct helpers in Z3 proofs
blanchet
parents: 57053
diff changeset
   149
               else (unknownN, fn () => "Internal error:\n" ^ Runtime.exn_message exn ^ "\n"))
8b2283566f6e properly reconstruct helpers in Z3 proofs
blanchet
parents: 57053
diff changeset
   150
41089
2e69fb6331cb moved function to later module
blanchet
parents: 41088
diff changeset
   151
        val _ =
41142
43e2b051339c weaken the "expect" flag so that it doesn't trigger errors if a prover is not installed
blanchet
parents: 41138
diff changeset
   152
          (* The "expect" argument is deliberately ignored if the prover is
43e2b051339c weaken the "expect" flag so that it doesn't trigger errors if a prover is not installed
blanchet
parents: 41138
diff changeset
   153
             missing so that the "Metis_Examples" can be processed on any
43e2b051339c weaken the "expect" flag so that it doesn't trigger errors if a prover is not installed
blanchet
parents: 41138
diff changeset
   154
             machine. *)
43e2b051339c weaken the "expect" flag so that it doesn't trigger errors if a prover is not installed
blanchet
parents: 41138
diff changeset
   155
          if expect = "" orelse outcome_code = expect orelse
43e2b051339c weaken the "expect" flag so that it doesn't trigger errors if a prover is not installed
blanchet
parents: 41138
diff changeset
   156
             not (is_prover_installed ctxt name) then
41089
2e69fb6331cb moved function to later module
blanchet
parents: 41088
diff changeset
   157
            ()
2e69fb6331cb moved function to later module
blanchet
parents: 41088
diff changeset
   158
          else if blocking then
2e69fb6331cb moved function to later module
blanchet
parents: 41088
diff changeset
   159
            error ("Unexpected outcome: " ^ quote outcome_code ^ ".")
2e69fb6331cb moved function to later module
blanchet
parents: 41088
diff changeset
   160
          else
2e69fb6331cb moved function to later module
blanchet
parents: 41088
diff changeset
   161
            warning ("Unexpected outcome: " ^ quote outcome_code ^ ".");
43005
c96f06bffd90 merge timeout messages from several ATPs into one message to avoid clutter
blanchet
parents: 43004
diff changeset
   162
      in (outcome_code, message) end
41089
2e69fb6331cb moved function to later module
blanchet
parents: 41088
diff changeset
   163
  in
43021
5910dd009d0e handle non-auto try case of Sledgehammer better
blanchet
parents: 43020
diff changeset
   164
    if mode = Auto_Try then
54816
10d48c2a3e32 made timeouts in Sledgehammer not be 'option's -- simplified lots of code
blanchet
parents: 54799
diff changeset
   165
      let val (outcome_code, message) = TimeLimit.timeLimit timeout go () in
43006
ff631c45797e make output more concise
blanchet
parents: 43005
diff changeset
   166
        (outcome_code,
ff631c45797e make output more concise
blanchet
parents: 43005
diff changeset
   167
         state
ff631c45797e make output more concise
blanchet
parents: 43005
diff changeset
   168
         |> outcome_code = someN
ff631c45797e make output more concise
blanchet
parents: 43005
diff changeset
   169
            ? Proof.goal_message (fn () =>
52643
34c29356930e more explicit Markup.information for messages produced by "auto" tools;
wenzelm
parents: 52555
diff changeset
   170
                  Pretty.mark Markup.information (Pretty.str (message ()))))
41089
2e69fb6331cb moved function to later module
blanchet
parents: 41088
diff changeset
   171
      end
2e69fb6331cb moved function to later module
blanchet
parents: 41088
diff changeset
   172
    else if blocking then
43006
ff631c45797e make output more concise
blanchet
parents: 43005
diff changeset
   173
      let
ff631c45797e make output more concise
blanchet
parents: 43005
diff changeset
   174
        val (outcome_code, message) = TimeLimit.timeLimit hard_timeout go ()
53048
0f76e620561f more direct sledgehammer configuration via mode = Normal_Result and output_result;
wenzelm
parents: 52997
diff changeset
   175
        val outcome =
57056
8b2283566f6e properly reconstruct helpers in Z3 proofs
blanchet
parents: 57053
diff changeset
   176
          if outcome_code = someN orelse mode = Normal then quote name ^ ": " ^ message () else ""
53048
0f76e620561f more direct sledgehammer configuration via mode = Normal_Result and output_result;
wenzelm
parents: 52997
diff changeset
   177
        val _ =
53052
a0db255af8c5 sledgehammer sendback always uses Markup.padding_command: sensible default for most practical applications -- old-style in-line replacement is superseded by auto mode or panel;
wenzelm
parents: 53048
diff changeset
   178
          if outcome <> "" andalso is_some output_result then
53048
0f76e620561f more direct sledgehammer configuration via mode = Normal_Result and output_result;
wenzelm
parents: 52997
diff changeset
   179
            the output_result outcome
52908
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents: 52643
diff changeset
   180
          else
53048
0f76e620561f more direct sledgehammer configuration via mode = Normal_Result and output_result;
wenzelm
parents: 52997
diff changeset
   181
            outcome
0f76e620561f more direct sledgehammer configuration via mode = Normal_Result and output_result;
wenzelm
parents: 52997
diff changeset
   182
            |> Async_Manager.break_into_chunks
0f76e620561f more direct sledgehammer configuration via mode = Normal_Result and output_result;
wenzelm
parents: 52997
diff changeset
   183
            |> List.app Output.urgent_message
0f76e620561f more direct sledgehammer configuration via mode = Normal_Result and output_result;
wenzelm
parents: 52997
diff changeset
   184
      in (outcome_code, state) end
41089
2e69fb6331cb moved function to later module
blanchet
parents: 41088
diff changeset
   185
    else
57053
46000c075d07 shorten Sledgehammer output, as suggested by Andrei Popescu
blanchet
parents: 57037
diff changeset
   186
      (Async_Manager.thread SledgehammerN birth_time death_time
46000c075d07 shorten Sledgehammer output, as suggested by Andrei Popescu
blanchet
parents: 57037
diff changeset
   187
         (prover_description verbose name num_facts)
55212
blanchet
parents: 55202
diff changeset
   188
         ((fn (outcome_code, message) => (verbose orelse outcome_code = someN, message ())) o go);
43006
ff631c45797e make output more concise
blanchet
parents: 43005
diff changeset
   189
       (unknownN, state))
41089
2e69fb6331cb moved function to later module
blanchet
parents: 41088
diff changeset
   190
  end
2e69fb6331cb moved function to later module
blanchet
parents: 41088
diff changeset
   191
48293
914ca0827804 renamed Sledgehammer options
blanchet
parents: 48292
diff changeset
   192
val auto_try_max_facts_divisor = 2 (* FUDGE *)
40060
5ef6747aa619 first step in adding support for an SMT backend to Sledgehammer
blanchet
parents: 40059
diff changeset
   193
51008
e096c0dc538b more precise output of selected facts
blanchet
parents: 51007
diff changeset
   194
fun string_of_facts facts =
57384
085e85cc6eea tuned output
blanchet
parents: 57368
diff changeset
   195
  "Including " ^ string_of_int (length facts) ^ " relevant fact" ^ plural_s (length facts) ^ ": " ^
51008
e096c0dc538b more precise output of selected facts
blanchet
parents: 51007
diff changeset
   196
  (facts |> map (fst o fst) |> space_implode " ") ^ "."
e096c0dc538b more precise output of selected facts
blanchet
parents: 51007
diff changeset
   197
51010
afd0213a3dab tuned data structure
blanchet
parents: 51009
diff changeset
   198
fun string_of_factss factss =
57464
3e94eb1124b0 tuned message
blanchet
parents: 57387
diff changeset
   199
  if forall (null o snd) factss then
3e94eb1124b0 tuned message
blanchet
parents: 57387
diff changeset
   200
    "Found no relevant facts."
3e94eb1124b0 tuned message
blanchet
parents: 57387
diff changeset
   201
  else
3e94eb1124b0 tuned message
blanchet
parents: 57387
diff changeset
   202
    cat_lines (map (fn (filter, facts) =>
3e94eb1124b0 tuned message
blanchet
parents: 57387
diff changeset
   203
      (if filter = "" then "" else quote filter ^ ": ") ^ string_of_facts facts) factss)
51008
e096c0dc538b more precise output of selected facts
blanchet
parents: 51007
diff changeset
   204
57368
blanchet
parents: 57262
diff changeset
   205
fun run_sledgehammer (params as {verbose, spy, blocking, provers, max_facts, ...}) mode
54799
blanchet
parents: 54773
diff changeset
   206
    output_result i (fact_override as {only, ...}) minimize_command state =
40059
6ad9081665db use consistent terminology in Sledgehammer: "prover = ATP or SMT solver or ..."
blanchet
parents: 39494
diff changeset
   207
  if null provers then
6ad9081665db use consistent terminology in Sledgehammer: "prover = ATP or SMT solver or ..."
blanchet
parents: 39494
diff changeset
   208
    error "No prover is set."
55286
blanchet
parents: 55212
diff changeset
   209
  else
blanchet
parents: 55212
diff changeset
   210
    (case subgoal_count state of
blanchet
parents: 55212
diff changeset
   211
      0 =>
52908
3461985dcbc3 dockable window for Sledgehammer, based on asynchronous/parallel query operation;
wenzelm
parents: 52643
diff changeset
   212
      ((if blocking then error else Output.urgent_message) "No subgoal!"; (false, (noneN, state)))
55286
blanchet
parents: 55212
diff changeset
   213
    | n =>
blanchet
parents: 55212
diff changeset
   214
      let
blanchet
parents: 55212
diff changeset
   215
        val _ = Proof.assert_backward state
blanchet
parents: 55212
diff changeset
   216
        val print =
blanchet
parents: 55212
diff changeset
   217
          if mode = Normal andalso is_none output_result then Output.urgent_message else K ()
blanchet
parents: 55212
diff changeset
   218
        val ctxt = Proof.context_of state
blanchet
parents: 55212
diff changeset
   219
        val {facts = chained, goal, ...} = Proof.goal state
blanchet
parents: 55212
diff changeset
   220
        val (_, hyp_ts, concl_t) = strip_subgoal goal i ctxt
blanchet
parents: 55212
diff changeset
   221
        val ho_atp = exists (is_ho_atp ctxt) provers
blanchet
parents: 55212
diff changeset
   222
        val reserved = reserved_isar_keyword_table ()
blanchet
parents: 55212
diff changeset
   223
        val css = clasimpset_rule_table_of ctxt
blanchet
parents: 55212
diff changeset
   224
        val all_facts =
57262
b2c629647a14 moved code around
blanchet
parents: 57101
diff changeset
   225
          nearly_all_facts ctxt ho_atp fact_override reserved css chained hyp_ts concl_t
55286
blanchet
parents: 55212
diff changeset
   226
        val _ = () |> not blocking ? kill_provers
blanchet
parents: 55212
diff changeset
   227
        val _ =
blanchet
parents: 55212
diff changeset
   228
          (case find_first (not o is_prover_supported ctxt) provers of
blanchet
parents: 55212
diff changeset
   229
            SOME name => error ("No such prover: " ^ name ^ ".")
blanchet
parents: 55212
diff changeset
   230
          | NONE => ())
blanchet
parents: 55212
diff changeset
   231
        val _ = print "Sledgehammering..."
57037
c51132be8e16 avoid markup-generating @{make_string}
blanchet
parents: 56303
diff changeset
   232
        val _ = spying spy (fn () => (state, i, "***", "Starting " ^ str_of_mode mode ^ " mode"))
54090
a28992e35032 run relevance filter only once for ATPs and SMT solvers, since it should now yield the same results anyway
blanchet
parents: 54063
diff changeset
   233
55286
blanchet
parents: 55212
diff changeset
   234
        val spying_str_of_factss =
blanchet
parents: 55212
diff changeset
   235
          commas o map (fn (filter, facts) => filter ^ ": " ^ string_of_int (length facts))
53800
ac1ec5065316 added "spy" option to Sledgehammer
blanchet
parents: 53549
diff changeset
   236
55286
blanchet
parents: 55212
diff changeset
   237
        fun get_factss provers =
blanchet
parents: 55212
diff changeset
   238
          let
blanchet
parents: 55212
diff changeset
   239
            val max_max_facts =
blanchet
parents: 55212
diff changeset
   240
              (case max_facts of
blanchet
parents: 55212
diff changeset
   241
                SOME n => n
blanchet
parents: 55212
diff changeset
   242
              | NONE =>
blanchet
parents: 55212
diff changeset
   243
                0 |> fold (Integer.max o default_max_facts_of_prover ctxt) provers
blanchet
parents: 55212
diff changeset
   244
                  |> mode = Auto_Try ? (fn n => n div auto_try_max_facts_divisor))
blanchet
parents: 55212
diff changeset
   245
            val _ = spying spy (fn () => (state, i, "All",
57557
242ce8d3d16b record MaSh algorithm in spying data
blanchet
parents: 57464
diff changeset
   246
              "Filtering " ^ string_of_int (length all_facts) ^ " facts (MaSh algorithm: " ^
242ce8d3d16b record MaSh algorithm in spying data
blanchet
parents: 57464
diff changeset
   247
              str_of_mash_algorithm (the_mash_algorithm ()) ^ ")"));
55286
blanchet
parents: 55212
diff changeset
   248
          in
blanchet
parents: 55212
diff changeset
   249
            all_facts
blanchet
parents: 55212
diff changeset
   250
            |> relevant_facts ctxt params (hd provers) max_max_facts fact_override hyp_ts concl_t
blanchet
parents: 55212
diff changeset
   251
            |> tap (fn factss => if verbose then print (string_of_factss factss) else ())
blanchet
parents: 55212
diff changeset
   252
            |> spy ? tap (fn factss => spying spy (fn () =>
blanchet
parents: 55212
diff changeset
   253
              (state, i, "All", "Selected facts: " ^ spying_str_of_factss factss)))
blanchet
parents: 55212
diff changeset
   254
          end
53800
ac1ec5065316 added "spy" option to Sledgehammer
blanchet
parents: 53549
diff changeset
   255
55286
blanchet
parents: 55212
diff changeset
   256
        fun launch_provers state =
blanchet
parents: 55212
diff changeset
   257
          let
blanchet
parents: 55212
diff changeset
   258
            val factss = get_factss provers
blanchet
parents: 55212
diff changeset
   259
            val problem =
blanchet
parents: 55212
diff changeset
   260
              {comment = "", state = state, goal = goal, subgoal = i, subgoal_count = n,
blanchet
parents: 55212
diff changeset
   261
               factss = factss}
blanchet
parents: 55212
diff changeset
   262
            val learn = mash_learn_proof ctxt params (prop_of goal) all_facts
blanchet
parents: 55212
diff changeset
   263
            val launch = launch_prover params mode output_result minimize_command only learn
blanchet
parents: 55212
diff changeset
   264
          in
blanchet
parents: 55212
diff changeset
   265
            if mode = Auto_Try then
blanchet
parents: 55212
diff changeset
   266
              (unknownN, state)
blanchet
parents: 55212
diff changeset
   267
              |> fold (fn prover => fn accum as (outcome_code, _) =>
57056
8b2283566f6e properly reconstruct helpers in Z3 proofs
blanchet
parents: 57053
diff changeset
   268
                  if outcome_code = someN then accum else launch problem prover)
8b2283566f6e properly reconstruct helpers in Z3 proofs
blanchet
parents: 57053
diff changeset
   269
                provers
55286
blanchet
parents: 55212
diff changeset
   270
            else
57387
2b6fe2a48352 reintroduced MaSh hints, this time as persistent creatures
blanchet
parents: 57384
diff changeset
   271
              (learn chained;
2b6fe2a48352 reintroduced MaSh hints, this time as persistent creatures
blanchet
parents: 57384
diff changeset
   272
               provers
2b6fe2a48352 reintroduced MaSh hints, this time as persistent creatures
blanchet
parents: 57384
diff changeset
   273
               |> (if blocking then Par_List.map else map) (launch problem #> fst)
2b6fe2a48352 reintroduced MaSh hints, this time as persistent creatures
blanchet
parents: 57384
diff changeset
   274
               |> max_outcome_code |> rpair state)
55286
blanchet
parents: 55212
diff changeset
   275
          end
blanchet
parents: 55212
diff changeset
   276
      in
blanchet
parents: 55212
diff changeset
   277
        (if blocking then launch_provers state
blanchet
parents: 55212
diff changeset
   278
         else (Future.fork (tap (fn () => launch_provers state)); (unknownN, state)))
blanchet
parents: 55212
diff changeset
   279
        handle TimeLimit.TimeOut => (print "Sledgehammer ran out of time."; (unknownN, state))
blanchet
parents: 55212
diff changeset
   280
      end
blanchet
parents: 55212
diff changeset
   281
      |> `(fn (outcome_code, _) => outcome_code = someN))
38044
463177795c49 minor refactoring
blanchet
parents: 38040
diff changeset
   282
28582
c269a3045fdf info: back to plain printing;
wenzelm
parents: 28571
diff changeset
   283
end;