src/HOL/Tools/Sledgehammer/sledgehammer_run.ML
author blanchet
Tue, 24 May 2011 00:01:33 +0200
changeset 43793 96f62b77748f
parent 43787 ddff373cf3ad
child 43807 4e2d6c1e5392
permissions -rw-r--r--
tuning -- the "appropriate" terminology is inspired from TPTP
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@43517
    16
  val auto_minimize_min_facts : int Config.T
blanchet@43315
    17
  val get_minimizing_prover : Proof.context -> bool -> string -> prover
blanchet@38290
    18
  val run_sledgehammer :
blanchet@43315
    19
    params -> bool -> int -> relevance_override -> (string -> minimize_command)
blanchet@43315
    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@42614
    39
  " on " ^ (if n = 1 then "goal" else "subgoal " ^ string_of_int i) ^
blanchet@41337
    40
  (if blocking then
blanchet@42614
    41
     "."
blanchet@41337
    42
   else
blanchet@42614
    43
     ":\n" ^ Syntax.string_of_term ctxt (Thm.term_of (Thm.cprem_of goal i)))
blanchet@41337
    44
blanchet@43517
    45
val auto_minimize_min_facts =
blanchet@43517
    46
  Attrib.setup_config_int @{binding sledgehammer_auto_minimize_min_facts}
blanchet@43517
    47
      (fn generic => Config.get_generic generic binary_min_facts)
blanchet@41339
    48
blanchet@43315
    49
fun get_minimizing_prover ctxt auto name
blanchet@42613
    50
        (params as {debug, verbose, explicit_apply, ...}) minimize_command
blanchet@41511
    51
        (problem as {state, subgoal, subgoal_count, facts, ...}) =
blanchet@43315
    52
  get_prover ctxt auto name params minimize_command problem
blanchet@41511
    53
  |> (fn result as {outcome, used_facts, run_time_in_msecs, message} =>
blanchet@41511
    54
         if is_some outcome then
blanchet@41511
    55
           result
blanchet@41511
    56
         else
blanchet@41511
    57
           let
blanchet@41511
    58
             val (used_facts, message) =
blanchet@43517
    59
               if length used_facts
blanchet@43517
    60
                  >= Config.get ctxt auto_minimize_min_facts then
blanchet@42613
    61
                 minimize_facts name params (SOME explicit_apply) (not verbose)
blanchet@42613
    62
                     subgoal subgoal_count state
blanchet@41511
    63
                     (filter_used_facts used_facts
blanchet@41511
    64
                          (map (apsnd single o untranslated_fact) facts))
blanchet@41511
    65
                 |>> Option.map (map fst)
blanchet@41511
    66
               else
blanchet@41511
    67
                 (SOME used_facts, message)
blanchet@41511
    68
           in
blanchet@41511
    69
             case used_facts of
blanchet@41511
    70
               SOME used_facts =>
blanchet@41511
    71
               (if debug andalso not (null used_facts) then
blanchet@41511
    72
                  facts ~~ (0 upto length facts - 1)
blanchet@41511
    73
                  |> map (fn (fact, j) =>
blanchet@41511
    74
                             fact |> untranslated_fact |> apsnd (K j))
blanchet@41511
    75
                  |> filter_used_facts used_facts
blanchet@41511
    76
                  |> map (fn ((name, _), j) => name ^ "@" ^ string_of_int j)
blanchet@41511
    77
                  |> commas
blanchet@41511
    78
                  |> enclose ("Fact" ^ plural_s (length facts) ^ " in " ^
blanchet@41511
    79
                              quote name ^ " proof (of " ^
blanchet@41511
    80
                              string_of_int (length facts) ^ "): ") "."
blanchet@41511
    81
                  |> Output.urgent_message
blanchet@41511
    82
                else
blanchet@41511
    83
                  ();
blanchet@41511
    84
                {outcome = NONE, used_facts = used_facts,
blanchet@41511
    85
                 run_time_in_msecs = run_time_in_msecs, message = message})
blanchet@41511
    86
             | NONE => result
blanchet@41511
    87
           end)
blanchet@41510
    88
blanchet@43314
    89
fun launch_prover (params as {debug, blocking, max_relevant, slicing, timeout,
blanchet@43314
    90
                              expect, ...})
blanchet@43315
    91
        auto minimize_command only
blanchet@42612
    92
        {state, goal, subgoal, subgoal_count, facts, smt_filter} name =
blanchet@41337
    93
  let
blanchet@41337
    94
    val ctxt = Proof.context_of state
blanchet@43719
    95
    val hard_timeout = Time.+ (timeout, timeout)
blanchet@41337
    96
    val birth_time = Time.now ()
blanchet@43719
    97
    val death_time = Time.+ (birth_time, hard_timeout)
blanchet@41337
    98
    val max_relevant =
blanchet@43314
    99
      max_relevant
blanchet@43314
   100
      |> the_default (default_max_relevant_for_prover ctxt slicing name)
blanchet@41337
   101
    val num_facts = length facts |> not only ? Integer.min max_relevant
blanchet@41337
   102
    val desc =
blanchet@41337
   103
      prover_description ctxt params name num_facts subgoal subgoal_count goal
blanchet@41337
   104
    val problem =
blanchet@41337
   105
      {state = state, goal = goal, subgoal = subgoal,
blanchet@43509
   106
       subgoal_count = subgoal_count, facts = facts |> take num_facts,
blanchet@42612
   107
       smt_filter = smt_filter}
blanchet@41501
   108
    fun really_go () =
blanchet@41511
   109
      problem
blanchet@43315
   110
      |> get_minimizing_prover ctxt auto name params (minimize_command name)
blanchet@41510
   111
      |> (fn {outcome, message, ...} =>
blanchet@41510
   112
             (if is_some outcome then "none" else "some" (* sic *), message))
blanchet@41337
   113
    fun go () =
blanchet@41337
   114
      let
blanchet@41337
   115
        val (outcome_code, message) =
blanchet@41337
   116
          if debug then
blanchet@41337
   117
            really_go ()
blanchet@41337
   118
          else
blanchet@41337
   119
            (really_go ()
blanchet@41337
   120
             handle ERROR message => ("unknown", "Error: " ^ message ^ "\n")
blanchet@41337
   121
                  | exn =>
blanchet@41337
   122
                    if Exn.is_interrupt exn then
blanchet@41337
   123
                      reraise exn
blanchet@41337
   124
                    else
blanchet@41337
   125
                      ("unknown", "Internal error:\n" ^
blanchet@41337
   126
                                  ML_Compiler.exn_message exn ^ "\n"))
blanchet@41337
   127
        val _ =
blanchet@41390
   128
          (* The "expect" argument is deliberately ignored if the prover is
blanchet@41390
   129
             missing so that the "Metis_Examples" can be processed on any
blanchet@41390
   130
             machine. *)
blanchet@41390
   131
          if expect = "" orelse outcome_code = expect orelse
blanchet@41390
   132
             not (is_prover_installed ctxt name) then
blanchet@41337
   133
            ()
blanchet@41337
   134
          else if blocking then
blanchet@41337
   135
            error ("Unexpected outcome: " ^ quote outcome_code ^ ".")
blanchet@41337
   136
          else
blanchet@41337
   137
            warning ("Unexpected outcome: " ^ quote outcome_code ^ ".");
blanchet@41337
   138
      in (outcome_code = "some", message) end
blanchet@41337
   139
  in
blanchet@41337
   140
    if auto then
blanchet@41337
   141
      let val (success, message) = TimeLimit.timeLimit timeout go () in
blanchet@41337
   142
        (success, state |> success ? Proof.goal_message (fn () =>
blanchet@41339
   143
             Pretty.chunks [Pretty.str "",
blanchet@41339
   144
                            Pretty.mark Markup.hilite (Pretty.str message)]))
blanchet@41337
   145
      end
blanchet@41337
   146
    else if blocking then
blanchet@43719
   147
      let val (success, message) = TimeLimit.timeLimit hard_timeout go () in
blanchet@41337
   148
        List.app Output.urgent_message
blanchet@41337
   149
                 (Async_Manager.break_into_chunks [desc ^ "\n" ^ message]);
blanchet@41337
   150
        (success, state)
blanchet@41337
   151
      end
blanchet@41337
   152
    else
blanchet@41337
   153
      (Async_Manager.launch das_Tool birth_time death_time desc (snd o go);
blanchet@41337
   154
       (false, state))
blanchet@41337
   155
  end
blanchet@41337
   156
blanchet@41483
   157
fun class_of_smt_solver ctxt name =
blanchet@41483
   158
  ctxt |> select_smt_solver name
blanchet@41483
   159
       |> SMT_Config.solver_class_of |> SMT_Utils.string_of_class
blanchet@41483
   160
blanchet@41483
   161
(* Makes backtraces more transparent and might be more efficient as well. *)
blanchet@41483
   162
fun smart_par_list_map _ [] = []
blanchet@41483
   163
  | smart_par_list_map f [x] = [f x]
blanchet@41483
   164
  | smart_par_list_map f xs = Par_List.map f xs
blanchet@41483
   165
blanchet@41502
   166
fun dest_SMT_Weighted_Fact (SMT_Weighted_Fact p) = p
blanchet@41502
   167
  | dest_SMT_Weighted_Fact _ = raise Fail "dest_SMT_Weighted_Fact"
blanchet@41502
   168
blanchet@43450
   169
val auto_max_relevant_divisor = 2 (* FUDGE *)
blanchet@40241
   170
blanchet@43787
   171
fun run_sledgehammer (params as {debug, verbose, blocking, provers,
blanchet@43787
   172
                                 relevance_thresholds, max_relevant, slicing,
blanchet@43787
   173
                                 timeout, ...})
blanchet@43315
   174
        auto i (relevance_override as {only, ...}) minimize_command state =
blanchet@40240
   175
  if null provers then
blanchet@40240
   176
    error "No prover is set."
blanchet@39564
   177
  else case subgoal_count state of
wenzelm@40392
   178
    0 => (Output.urgent_message "No subgoal!"; (false, state))
blanchet@39564
   179
  | n =>
blanchet@39564
   180
    let
blanchet@39610
   181
      val _ = Proof.assert_backward state
blanchet@42644
   182
      val print = if auto then K () else Output.urgent_message
blanchet@41483
   183
      val state =
blanchet@41483
   184
        state |> Proof.map_context (Config.put SMT_Config.verbose debug)
blanchet@40441
   185
      val ctxt = Proof.context_of state
blanchet@40441
   186
      val {facts = chained_ths, goal, ...} = Proof.goal state
blanchet@40241
   187
      val (_, hyp_ts, concl_t) = strip_subgoal goal i
blanchet@40240
   188
      val _ = () |> not blocking ? kill_provers
blanchet@42591
   189
      val _ = case find_first (not o is_prover_supported ctxt) provers of
blanchet@41189
   190
                SOME name => error ("No such prover: " ^ name ^ ".")
blanchet@41189
   191
              | NONE => ()
blanchet@42644
   192
      val _ = print "Sledgehammering..."
blanchet@43785
   193
      val (smts, (ueq_atps, full_atps)) =
blanchet@43785
   194
        provers |> List.partition (is_smt_prover ctxt)
blanchet@43785
   195
                ||> List.partition (is_unit_equational_atp ctxt)
blanchet@42612
   196
      fun launch_provers state get_facts translate maybe_smt_filter provers =
blanchet@41502
   197
        let
blanchet@41502
   198
          val facts = get_facts ()
blanchet@41502
   199
          val num_facts = length facts
blanchet@41502
   200
          val facts = facts ~~ (0 upto num_facts - 1)
blanchet@41502
   201
                      |> map (translate num_facts)
blanchet@41502
   202
          val problem =
blanchet@41502
   203
            {state = state, goal = goal, subgoal = i, subgoal_count = n,
blanchet@41502
   204
             facts = facts,
blanchet@42612
   205
             smt_filter = maybe_smt_filter
blanchet@41502
   206
                  (fn () => map_filter (try dest_SMT_Weighted_Fact) facts) i}
blanchet@43315
   207
          val launch = launch_prover params auto minimize_command only
blanchet@41502
   208
        in
blanchet@41502
   209
          if auto then
blanchet@41502
   210
            fold (fn prover => fn (true, state) => (true, state)
blanchet@41510
   211
                                | (false, _) => launch problem prover)
blanchet@41502
   212
                 provers (false, state)
blanchet@41502
   213
          else
blanchet@41502
   214
            provers
blanchet@41510
   215
            |> (if blocking then smart_par_list_map else map) (launch problem)
blanchet@41502
   216
            |> exists fst |> rpair state
blanchet@41502
   217
        end
blanchet@43793
   218
      fun get_facts label is_appropriate_prop relevance_fudge provers =
blanchet@41483
   219
        let
blanchet@41483
   220
          val max_max_relevant =
blanchet@41483
   221
            case max_relevant of
blanchet@41483
   222
              SOME n => n
blanchet@41483
   223
            | NONE =>
blanchet@43314
   224
              0 |> fold (Integer.max
blanchet@43314
   225
                         o default_max_relevant_for_prover ctxt slicing)
blanchet@41483
   226
                        provers
blanchet@41483
   227
                |> auto ? (fn n => n div auto_max_relevant_divisor)
blanchet@41483
   228
          val is_built_in_const =
blanchet@41483
   229
            is_built_in_const_for_prover ctxt (hd provers)
blanchet@41483
   230
        in
blanchet@43793
   231
          relevant_facts ctxt relevance_thresholds max_max_relevant
blanchet@43793
   232
                         is_appropriate_prop is_built_in_const relevance_fudge
blanchet@43793
   233
                         relevance_override chained_ths hyp_ts concl_t
blanchet@41483
   234
          |> tap (fn facts =>
blanchet@41483
   235
                     if debug then
blanchet@41483
   236
                       label ^ plural_s (length provers) ^ ": " ^
blanchet@41483
   237
                       (if null facts then
blanchet@41483
   238
                          "Found no relevant facts."
blanchet@41483
   239
                        else
blanchet@41483
   240
                          "Including (up to) " ^ string_of_int (length facts) ^
blanchet@41483
   241
                          " relevant fact" ^ plural_s (length facts) ^ ":\n" ^
blanchet@41483
   242
                          (facts |> map (fst o fst) |> space_implode " ") ^ ".")
blanchet@42644
   243
                       |> print
blanchet@41483
   244
                     else
blanchet@41483
   245
                       ())
blanchet@41483
   246
        end
blanchet@43793
   247
      fun launch_atps label is_appropriate_prop atps accum =
blanchet@43787
   248
        if null atps then
blanchet@41502
   249
          accum
blanchet@43793
   250
        else if not (is_appropriate_prop concl_t) then
blanchet@43787
   251
          (if verbose orelse length atps = length provers then
blanchet@43787
   252
             "Goal outside the scope of " ^
blanchet@43787
   253
             space_implode " " (serial_commas "and" (map quote atps)) ^ "."
blanchet@43787
   254
             |> Output.urgent_message
blanchet@43787
   255
           else
blanchet@43787
   256
             ();
blanchet@43787
   257
           accum)
blanchet@41502
   258
        else
blanchet@43785
   259
          launch_provers state
blanchet@43793
   260
              (get_facts label is_appropriate_prop atp_relevance_fudge o K atps)
blanchet@43785
   261
              (K (Untranslated_Fact o fst)) (K (K NONE)) atps
blanchet@42617
   262
      fun launch_smts accum =
blanchet@42617
   263
        if null smts then
blanchet@41483
   264
          accum
blanchet@41483
   265
        else
blanchet@41483
   266
          let
blanchet@43785
   267
            val facts = get_facts "SMT solver" (K true) smt_relevance_fudge smts
blanchet@43517
   268
            val weight = SMT_Weighted_Fact oo weight_smt_fact ctxt
blanchet@42612
   269
            fun smt_filter facts =
blanchet@42659
   270
              (if debug then curry (op o) SOME
blanchet@42659
   271
               else TimeLimit.timeLimit timeout o try)
boehmes@41680
   272
                  (SMT_Solver.smt_filter_preprocess state (facts ()))
blanchet@41483
   273
          in
blanchet@41483
   274
            smts |> map (`(class_of_smt_solver ctxt))
blanchet@41483
   275
                 |> AList.group (op =)
blanchet@42612
   276
                 |> map (launch_provers state (K facts) weight smt_filter o snd)
blanchet@41483
   277
                 |> exists fst |> rpair state
blanchet@41483
   278
          end
blanchet@43785
   279
      val launch_full_atps = launch_atps "ATP" (K true) full_atps
blanchet@43785
   280
      val launch_ueq_atps =
blanchet@43785
   281
        launch_atps "Unit equational provers" is_unit_equality ueq_atps
blanchet@41510
   282
      fun launch_atps_and_smt_solvers () =
blanchet@43785
   283
        [launch_full_atps, launch_ueq_atps, launch_smts]
blanchet@41483
   284
        |> smart_par_list_map (fn f => f (false, state) |> K ())
blanchet@42644
   285
        handle ERROR msg => (print ("Error: " ^ msg); error msg)
blanchet@40241
   286
    in
blanchet@40246
   287
      (false, state)
blanchet@43785
   288
      |> (if blocking then
blanchet@43785
   289
            launch_full_atps #> not auto ? (launch_ueq_atps #> launch_smts)
blanchet@43785
   290
          else
blanchet@43785
   291
            (fn p => Future.fork (tap launch_atps_and_smt_solvers) |> K p))
blanchet@42644
   292
      handle TimeLimit.TimeOut =>
blanchet@42644
   293
             (print "Sledgehammer ran out of time."; (false, state))
blanchet@40241
   294
    end
blanchet@38290
   295
wenzelm@28582
   296
end;