src/HOL/Tools/Sledgehammer/sledgehammer_run.ML
author blanchet
Tue, 08 Feb 2011 16:10:10 +0100
changeset 42591 ab3f6d76fb23
parent 41680 3214c39777ab
child 42612 839d1488045f
permissions -rw-r--r--
available_provers ~> supported_provers (for clarity)
blanchet@41335
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_run.ML
wenzelm@28477
     2
    Author:     Fabian Immler, TU Muenchen
wenzelm@32996
     3
    Author:     Makarius
blanchet@35967
     4
    Author:     Jasmin Blanchette, TU Muenchen
wenzelm@28477
     5
blanchet@38255
     6
Sledgehammer's heart.
wenzelm@28477
     7
*)
wenzelm@28477
     8
blanchet@41335
     9
signature SLEDGEHAMMER_RUN =
wenzelm@28477
    10
sig
blanchet@39232
    11
  type relevance_override = Sledgehammer_Filter.relevance_override
blanchet@40249
    12
  type minimize_command = Sledgehammer_ATP_Reconstruct.minimize_command
blanchet@41335
    13
  type params = Sledgehammer_Provers.params
blanchet@41511
    14
  type prover = Sledgehammer_Provers.prover
blanchet@39733
    15
blanchet@41583
    16
  val auto_minimize_min_facts : int Unsynchronized.ref
blanchet@41511
    17
  val get_minimizing_prover : Proof.context -> bool -> string -> prover
blanchet@38290
    18
  val run_sledgehammer :
blanchet@39564
    19
    params -> bool -> int -> relevance_override -> (string -> minimize_command)
blanchet@39564
    20
    -> Proof.state -> bool * Proof.state
wenzelm@28477
    21
end;
wenzelm@28477
    22
blanchet@41335
    23
structure Sledgehammer_Run : SLEDGEHAMMER_RUN =
wenzelm@28477
    24
struct
wenzelm@28477
    25
blanchet@38257
    26
open Sledgehammer_Util
blanchet@39232
    27
open Sledgehammer_Filter
blanchet@40249
    28
open Sledgehammer_ATP_Translate
blanchet@41335
    29
open Sledgehammer_Provers
blanchet@41339
    30
open Sledgehammer_Minimize
blanchet@40253
    31
blanchet@41456
    32
fun prover_description ctxt ({verbose, blocking, ...} : params) name num_facts i
blanchet@41337
    33
                       n goal =
blanchet@41337
    34
  quote name ^
blanchet@41337
    35
  (if verbose then
blanchet@41337
    36
     " with " ^ string_of_int num_facts ^ " fact" ^ plural_s num_facts
blanchet@41337
    37
   else
blanchet@41337
    38
     "") ^
blanchet@41337
    39
  " on " ^ (if n = 1 then "goal" else "subgoal " ^ string_of_int i) ^ ":" ^
blanchet@41337
    40
  (if blocking then
blanchet@41337
    41
     ""
blanchet@41337
    42
   else
blanchet@41337
    43
     "\n" ^ Syntax.string_of_term ctxt (Thm.term_of (Thm.cprem_of goal i)))
blanchet@41337
    44
blanchet@41583
    45
val auto_minimize_min_facts = Unsynchronized.ref (!binary_min_facts)
blanchet@41339
    46
blanchet@41511
    47
fun get_minimizing_prover ctxt auto name (params as {debug, verbose, ...})
blanchet@41511
    48
        minimize_command
blanchet@41511
    49
        (problem as {state, subgoal, subgoal_count, facts, ...}) =
blanchet@41511
    50
  get_prover ctxt auto name params minimize_command problem
blanchet@41511
    51
  |> (fn result as {outcome, used_facts, run_time_in_msecs, message} =>
blanchet@41511
    52
         if is_some outcome then
blanchet@41511
    53
           result
blanchet@41511
    54
         else
blanchet@41511
    55
           let
blanchet@41511
    56
             val (used_facts, message) =
blanchet@41583
    57
               if length used_facts >= !auto_minimize_min_facts then
blanchet@41513
    58
                 minimize_facts name params (not verbose) subgoal subgoal_count
blanchet@41511
    59
                     state
blanchet@41511
    60
                     (filter_used_facts used_facts
blanchet@41511
    61
                          (map (apsnd single o untranslated_fact) facts))
blanchet@41511
    62
                 |>> Option.map (map fst)
blanchet@41511
    63
               else
blanchet@41511
    64
                 (SOME used_facts, message)
blanchet@41511
    65
           in
blanchet@41511
    66
             case used_facts of
blanchet@41511
    67
               SOME used_facts =>
blanchet@41511
    68
               (if debug andalso not (null used_facts) then
blanchet@41511
    69
                  facts ~~ (0 upto length facts - 1)
blanchet@41511
    70
                  |> map (fn (fact, j) =>
blanchet@41511
    71
                             fact |> untranslated_fact |> apsnd (K j))
blanchet@41511
    72
                  |> filter_used_facts used_facts
blanchet@41511
    73
                  |> map (fn ((name, _), j) => name ^ "@" ^ string_of_int j)
blanchet@41511
    74
                  |> commas
blanchet@41511
    75
                  |> enclose ("Fact" ^ plural_s (length facts) ^ " in " ^
blanchet@41511
    76
                              quote name ^ " proof (of " ^
blanchet@41511
    77
                              string_of_int (length facts) ^ "): ") "."
blanchet@41511
    78
                  |> Output.urgent_message
blanchet@41511
    79
                else
blanchet@41511
    80
                  ();
blanchet@41511
    81
                {outcome = NONE, used_facts = used_facts,
blanchet@41511
    82
                 run_time_in_msecs = run_time_in_msecs, message = message})
blanchet@41511
    83
             | NONE => result
blanchet@41511
    84
           end)
blanchet@41510
    85
blanchet@41510
    86
fun launch_prover
blanchet@41510
    87
        (params as {debug, blocking, max_relevant, timeout, expect, ...})
blanchet@41510
    88
        auto minimize_command only
blanchet@41510
    89
        {state, goal, subgoal, subgoal_count, facts, smt_head} name =
blanchet@41337
    90
  let
blanchet@41337
    91
    val ctxt = Proof.context_of state
blanchet@41337
    92
    val birth_time = Time.now ()
blanchet@41337
    93
    val death_time = Time.+ (birth_time, timeout)
blanchet@41337
    94
    val max_relevant =
blanchet@41337
    95
      the_default (default_max_relevant_for_prover ctxt name) max_relevant
blanchet@41337
    96
    val num_facts = length facts |> not only ? Integer.min max_relevant
blanchet@41337
    97
    val desc =
blanchet@41337
    98
      prover_description ctxt params name num_facts subgoal subgoal_count goal
blanchet@41337
    99
    val problem =
blanchet@41337
   100
      {state = state, goal = goal, subgoal = subgoal,
blanchet@41483
   101
       subgoal_count = subgoal_count, facts = take num_facts facts,
blanchet@41483
   102
       smt_head = smt_head}
blanchet@41501
   103
    fun really_go () =
blanchet@41511
   104
      problem
blanchet@41511
   105
      |> get_minimizing_prover ctxt auto name params (minimize_command name)
blanchet@41510
   106
      |> (fn {outcome, message, ...} =>
blanchet@41510
   107
             (if is_some outcome then "none" else "some" (* sic *), message))
blanchet@41337
   108
    fun go () =
blanchet@41337
   109
      let
blanchet@41337
   110
        val (outcome_code, message) =
blanchet@41337
   111
          if debug then
blanchet@41337
   112
            really_go ()
blanchet@41337
   113
          else
blanchet@41337
   114
            (really_go ()
blanchet@41337
   115
             handle ERROR message => ("unknown", "Error: " ^ message ^ "\n")
blanchet@41337
   116
                  | exn =>
blanchet@41337
   117
                    if Exn.is_interrupt exn then
blanchet@41337
   118
                      reraise exn
blanchet@41337
   119
                    else
blanchet@41337
   120
                      ("unknown", "Internal error:\n" ^
blanchet@41337
   121
                                  ML_Compiler.exn_message exn ^ "\n"))
blanchet@41337
   122
        val _ =
blanchet@41390
   123
          (* The "expect" argument is deliberately ignored if the prover is
blanchet@41390
   124
             missing so that the "Metis_Examples" can be processed on any
blanchet@41390
   125
             machine. *)
blanchet@41390
   126
          if expect = "" orelse outcome_code = expect orelse
blanchet@41390
   127
             not (is_prover_installed ctxt name) then
blanchet@41337
   128
            ()
blanchet@41337
   129
          else if blocking then
blanchet@41337
   130
            error ("Unexpected outcome: " ^ quote outcome_code ^ ".")
blanchet@41337
   131
          else
blanchet@41337
   132
            warning ("Unexpected outcome: " ^ quote outcome_code ^ ".");
blanchet@41337
   133
      in (outcome_code = "some", message) end
blanchet@41337
   134
  in
blanchet@41337
   135
    if auto then
blanchet@41337
   136
      let val (success, message) = TimeLimit.timeLimit timeout go () in
blanchet@41337
   137
        (success, state |> success ? Proof.goal_message (fn () =>
blanchet@41339
   138
             Pretty.chunks [Pretty.str "",
blanchet@41339
   139
                            Pretty.mark Markup.hilite (Pretty.str message)]))
blanchet@41337
   140
      end
blanchet@41337
   141
    else if blocking then
blanchet@41337
   142
      let val (success, message) = TimeLimit.timeLimit timeout go () in
blanchet@41337
   143
        List.app Output.urgent_message
blanchet@41337
   144
                 (Async_Manager.break_into_chunks [desc ^ "\n" ^ message]);
blanchet@41337
   145
        (success, state)
blanchet@41337
   146
      end
blanchet@41337
   147
    else
blanchet@41337
   148
      (Async_Manager.launch das_Tool birth_time death_time desc (snd o go);
blanchet@41337
   149
       (false, state))
blanchet@41337
   150
  end
blanchet@41337
   151
blanchet@41483
   152
fun class_of_smt_solver ctxt name =
blanchet@41483
   153
  ctxt |> select_smt_solver name
blanchet@41483
   154
       |> SMT_Config.solver_class_of |> SMT_Utils.string_of_class
blanchet@41483
   155
blanchet@41483
   156
(* Makes backtraces more transparent and might be more efficient as well. *)
blanchet@41483
   157
fun smart_par_list_map _ [] = []
blanchet@41483
   158
  | smart_par_list_map f [x] = [f x]
blanchet@41483
   159
  | smart_par_list_map f xs = Par_List.map f xs
blanchet@41483
   160
blanchet@41502
   161
fun dest_SMT_Weighted_Fact (SMT_Weighted_Fact p) = p
blanchet@41502
   162
  | dest_SMT_Weighted_Fact _ = raise Fail "dest_SMT_Weighted_Fact"
blanchet@41502
   163
blanchet@40946
   164
(* FUDGE *)
blanchet@40946
   165
val auto_max_relevant_divisor = 2
blanchet@40241
   166
blanchet@41456
   167
fun run_sledgehammer (params as {debug, blocking, provers, type_sys,
blanchet@40250
   168
                                 relevance_thresholds, max_relevant, ...})
blanchet@39612
   169
                     auto i (relevance_override as {only, ...}) minimize_command
blanchet@39612
   170
                     state =
blanchet@40240
   171
  if null provers then
blanchet@40240
   172
    error "No prover is set."
blanchet@39564
   173
  else case subgoal_count state of
wenzelm@40392
   174
    0 => (Output.urgent_message "No subgoal!"; (false, state))
blanchet@39564
   175
  | n =>
blanchet@39564
   176
    let
blanchet@39610
   177
      val _ = Proof.assert_backward state
blanchet@41483
   178
      val state =
blanchet@41483
   179
        state |> Proof.map_context (Config.put SMT_Config.verbose debug)
blanchet@40441
   180
      val ctxt = Proof.context_of state
blanchet@41483
   181
      val thy = ProofContext.theory_of ctxt
blanchet@40441
   182
      val {facts = chained_ths, goal, ...} = Proof.goal state
blanchet@40241
   183
      val (_, hyp_ts, concl_t) = strip_subgoal goal i
blanchet@41386
   184
      val no_dangerous_types = types_dangerous_types type_sys
blanchet@40240
   185
      val _ = () |> not blocking ? kill_provers
blanchet@42591
   186
      val _ = case find_first (not o is_prover_supported ctxt) provers of
blanchet@41189
   187
                SOME name => error ("No such prover: " ^ name ^ ".")
blanchet@41189
   188
              | NONE => ()
wenzelm@40392
   189
      val _ = if auto then () else Output.urgent_message "Sledgehammering..."
blanchet@41189
   190
      val (smts, atps) = provers |> List.partition (is_smt_prover ctxt)
blanchet@41510
   191
      fun launch_provers state get_facts translate maybe_smt_head provers =
blanchet@41502
   192
        let
blanchet@41502
   193
          val facts = get_facts ()
blanchet@41502
   194
          val num_facts = length facts
blanchet@41502
   195
          val facts = facts ~~ (0 upto num_facts - 1)
blanchet@41502
   196
                      |> map (translate num_facts)
blanchet@41502
   197
          val problem =
blanchet@41502
   198
            {state = state, goal = goal, subgoal = i, subgoal_count = n,
blanchet@41502
   199
             facts = facts,
blanchet@41502
   200
             smt_head = maybe_smt_head
blanchet@41502
   201
                  (fn () => map_filter (try dest_SMT_Weighted_Fact) facts) i}
blanchet@41510
   202
          val launch = launch_prover params auto minimize_command only
blanchet@41502
   203
        in
blanchet@41502
   204
          if auto then
blanchet@41502
   205
            fold (fn prover => fn (true, state) => (true, state)
blanchet@41510
   206
                                | (false, _) => launch problem prover)
blanchet@41502
   207
                 provers (false, state)
blanchet@41502
   208
          else
blanchet@41502
   209
            provers
blanchet@41510
   210
            |> (if blocking then smart_par_list_map else map) (launch problem)
blanchet@41502
   211
            |> exists fst |> rpair state
blanchet@41502
   212
        end
blanchet@41483
   213
      fun get_facts label no_dangerous_types relevance_fudge provers =
blanchet@41483
   214
        let
blanchet@41483
   215
          val max_max_relevant =
blanchet@41483
   216
            case max_relevant of
blanchet@41483
   217
              SOME n => n
blanchet@41483
   218
            | NONE =>
blanchet@41483
   219
              0 |> fold (Integer.max o default_max_relevant_for_prover ctxt)
blanchet@41483
   220
                        provers
blanchet@41483
   221
                |> auto ? (fn n => n div auto_max_relevant_divisor)
blanchet@41483
   222
          val is_built_in_const =
blanchet@41483
   223
            is_built_in_const_for_prover ctxt (hd provers)
blanchet@41483
   224
        in
blanchet@41483
   225
          relevant_facts ctxt no_dangerous_types relevance_thresholds
blanchet@41483
   226
                         max_max_relevant is_built_in_const relevance_fudge
blanchet@41483
   227
                         relevance_override chained_ths hyp_ts concl_t
blanchet@41483
   228
          |> tap (fn facts =>
blanchet@41483
   229
                     if debug then
blanchet@41483
   230
                       label ^ plural_s (length provers) ^ ": " ^
blanchet@41483
   231
                       (if null facts then
blanchet@41483
   232
                          "Found no relevant facts."
blanchet@41483
   233
                        else
blanchet@41483
   234
                          "Including (up to) " ^ string_of_int (length facts) ^
blanchet@41483
   235
                          " relevant fact" ^ plural_s (length facts) ^ ":\n" ^
blanchet@41483
   236
                          (facts |> map (fst o fst) |> space_implode " ") ^ ".")
blanchet@41483
   237
                       |> Output.urgent_message
blanchet@41483
   238
                     else
blanchet@41483
   239
                       ())
blanchet@41483
   240
        end
blanchet@41510
   241
      fun launch_atps (accum as (success, _)) =
blanchet@41502
   242
        if success orelse null atps then
blanchet@41502
   243
          accum
blanchet@41502
   244
        else
blanchet@41510
   245
          launch_provers state
blanchet@41502
   246
              (get_facts "ATP" no_dangerous_types atp_relevance_fudge o K atps)
blanchet@41502
   247
              (ATP_Translated_Fact oo K (translate_atp_fact ctxt o fst))
blanchet@41502
   248
              (K (K NONE)) atps
blanchet@41510
   249
      fun launch_smts (accum as (success, _)) =
blanchet@41483
   250
        if success orelse null smts then
blanchet@41483
   251
          accum
blanchet@41483
   252
        else
blanchet@41483
   253
          let
blanchet@41483
   254
            val facts = get_facts "SMT solver" true smt_relevance_fudge smts
blanchet@41502
   255
            val weight = SMT_Weighted_Fact oo weight_smt_fact thy
blanchet@41502
   256
            fun smt_head facts =
blanchet@41564
   257
              (if debug then curry (op o) SOME else try)
boehmes@41680
   258
                  (SMT_Solver.smt_filter_preprocess state (facts ()))
blanchet@41483
   259
          in
blanchet@41483
   260
            smts |> map (`(class_of_smt_solver ctxt))
blanchet@41483
   261
                 |> AList.group (op =)
blanchet@41510
   262
                 |> map (launch_provers state (K facts) weight smt_head o snd)
blanchet@41483
   263
                 |> exists fst |> rpair state
blanchet@41483
   264
          end
blanchet@41510
   265
      fun launch_atps_and_smt_solvers () =
blanchet@41510
   266
        [launch_atps, launch_smts]
blanchet@41483
   267
        |> smart_par_list_map (fn f => f (false, state) |> K ())
blanchet@41428
   268
        handle ERROR msg => (Output.urgent_message ("Error: " ^ msg); error msg)
blanchet@40241
   269
    in
blanchet@40246
   270
      (false, state)
blanchet@41510
   271
      |> (if blocking then launch_atps #> not auto ? launch_smts
blanchet@41510
   272
          else (fn p => Future.fork (tap launch_atps_and_smt_solvers) |> K p))
blanchet@40241
   273
    end
blanchet@38290
   274
wenzelm@28582
   275
end;