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