src/HOL/Tools/Sledgehammer/sledgehammer_prover_smt.ML
author blanchet
Mon, 03 Feb 2014 15:19:07 +0100
changeset 56627 e88ad20035f4
parent 56554 5832470d956e
child 56629 ffa306239316
permissions -rw-r--r--
merged 'reconstructors' and 'proof methods'
blanchet@56547
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_prover_smt.ML
blanchet@56547
     2
    Author:     Fabian Immler, TU Muenchen
blanchet@56547
     3
    Author:     Makarius
blanchet@56547
     4
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@56547
     5
blanchet@56547
     6
SMT solvers as Sledgehammer provers.
blanchet@56547
     7
*)
blanchet@56547
     8
blanchet@56547
     9
signature SLEDGEHAMMER_PROVER_SMT =
blanchet@56547
    10
sig
blanchet@56547
    11
  type stature = ATP_Problem_Generate.stature
blanchet@56547
    12
  type mode = Sledgehammer_Prover.mode
blanchet@56547
    13
  type prover = Sledgehammer_Prover.prover
blanchet@56547
    14
blanchet@56547
    15
  val smt_builtins : bool Config.T
blanchet@56547
    16
  val smt_triggers : bool Config.T
blanchet@56547
    17
  val smt_weights : bool Config.T
blanchet@56547
    18
  val smt_weight_min_facts : int Config.T
blanchet@56547
    19
  val smt_min_weight : int Config.T
blanchet@56547
    20
  val smt_max_weight : int Config.T
blanchet@56547
    21
  val smt_max_weight_index : int Config.T
blanchet@56547
    22
  val smt_weight_curve : (int -> int) Unsynchronized.ref
blanchet@56547
    23
  val smt_max_slices : int Config.T
blanchet@56547
    24
  val smt_slice_fact_frac : real Config.T
blanchet@56547
    25
  val smt_slice_time_frac : real Config.T
blanchet@56547
    26
  val smt_slice_min_secs : int Config.T
blanchet@56547
    27
blanchet@56547
    28
  val is_smt_prover : Proof.context -> string -> bool
blanchet@56547
    29
  val run_smt_solver : mode -> string -> prover
blanchet@56547
    30
end;
blanchet@56547
    31
blanchet@56547
    32
structure Sledgehammer_Prover_SMT : SLEDGEHAMMER_PROVER_SMT =
blanchet@56547
    33
struct
blanchet@56547
    34
blanchet@56554
    35
open ATP_Util
blanchet@56547
    36
open ATP_Proof
blanchet@56547
    37
open ATP_Systems
blanchet@56547
    38
open ATP_Problem_Generate
blanchet@56547
    39
open ATP_Proof_Reconstruct
blanchet@56547
    40
open Sledgehammer_Util
blanchet@56547
    41
open Sledgehammer_Reconstructor
blanchet@56547
    42
open Sledgehammer_Prover
blanchet@56547
    43
blanchet@56547
    44
val smt_builtins = Attrib.setup_config_bool @{binding sledgehammer_smt_builtins} (K true)
blanchet@56547
    45
val smt_triggers = Attrib.setup_config_bool @{binding sledgehammer_smt_triggers} (K true)
blanchet@56547
    46
val smt_weights = Attrib.setup_config_bool @{binding sledgehammer_smt_weights} (K true)
blanchet@56547
    47
val smt_weight_min_facts =
blanchet@56547
    48
  Attrib.setup_config_int @{binding sledgehammer_smt_weight_min_facts} (K 20)
blanchet@56547
    49
blanchet@56547
    50
fun is_smt_prover ctxt = member (op =) (SMT_Solver.available_solvers_of ctxt)
blanchet@56547
    51
blanchet@56547
    52
(* FUDGE *)
blanchet@56554
    53
val smt_min_weight = Attrib.setup_config_int @{binding sledgehammer_smt_min_weight} (K 0)
blanchet@56554
    54
val smt_max_weight = Attrib.setup_config_int @{binding sledgehammer_smt_max_weight} (K 10)
blanchet@56547
    55
val smt_max_weight_index =
blanchet@56547
    56
  Attrib.setup_config_int @{binding sledgehammer_smt_max_weight_index} (K 200)
blanchet@56547
    57
val smt_weight_curve = Unsynchronized.ref (fn x : int => x * x)
blanchet@56547
    58
blanchet@56547
    59
fun smt_fact_weight ctxt j num_facts =
blanchet@56554
    60
  if Config.get ctxt smt_weights andalso num_facts >= Config.get ctxt smt_weight_min_facts then
blanchet@56547
    61
    let
blanchet@56547
    62
      val min = Config.get ctxt smt_min_weight
blanchet@56547
    63
      val max = Config.get ctxt smt_max_weight
blanchet@56547
    64
      val max_index = Config.get ctxt smt_max_weight_index
blanchet@56547
    65
      val curve = !smt_weight_curve
blanchet@56547
    66
    in
blanchet@56554
    67
      SOME (max - (max - min + 1) * curve (Int.max (0, max_index - j - 1)) div curve max_index)
blanchet@56547
    68
    end
blanchet@56547
    69
  else
blanchet@56547
    70
    NONE
blanchet@56547
    71
blanchet@56547
    72
fun weight_smt_fact ctxt num_facts ((info, th), j) =
blanchet@56547
    73
  let val thy = Proof_Context.theory_of ctxt in
blanchet@56547
    74
    (info, (smt_fact_weight ctxt j num_facts, th |> Thm.transfer thy))
blanchet@56547
    75
  end
blanchet@56547
    76
blanchet@56547
    77
(* "SMT_Failure.Abnormal_Termination" carries the solver's return code. Until these are sorted out
blanchet@56547
    78
   properly in the SMT module, we must interpret these here. *)
blanchet@56547
    79
val z3_failures =
blanchet@56547
    80
  [(101, OutOfResources),
blanchet@56547
    81
   (103, MalformedInput),
blanchet@56547
    82
   (110, MalformedInput),
blanchet@56547
    83
   (112, TimedOut)]
blanchet@56547
    84
val unix_failures =
blanchet@56547
    85
  [(138, Crashed),
blanchet@56547
    86
   (139, Crashed)]
blanchet@56547
    87
val smt_failures = z3_failures @ unix_failures
blanchet@56547
    88
blanchet@56547
    89
fun failure_of_smt_failure (SMT_Failure.Counterexample {is_real_cex, ...}) =
blanchet@56547
    90
    if is_real_cex then Unprovable else GaveUp
blanchet@56547
    91
  | failure_of_smt_failure SMT_Failure.Time_Out = TimedOut
blanchet@56547
    92
  | failure_of_smt_failure (SMT_Failure.Abnormal_Termination code) =
blanchet@56547
    93
    (case AList.lookup (op =) smt_failures code of
blanchet@56547
    94
      SOME failure => failure
blanchet@56547
    95
    | NONE => UnknownError ("Abnormal termination with exit code " ^ string_of_int code ^ "."))
blanchet@56547
    96
  | failure_of_smt_failure SMT_Failure.Out_Of_Memory = OutOfResources
blanchet@56547
    97
  | failure_of_smt_failure (SMT_Failure.Other_Failure s) = UnknownError s
blanchet@56547
    98
blanchet@56547
    99
(* FUDGE *)
blanchet@56547
   100
val smt_max_slices = Attrib.setup_config_int @{binding sledgehammer_smt_max_slices} (K 8)
blanchet@56547
   101
val smt_slice_fact_frac =
blanchet@56547
   102
  Attrib.setup_config_real @{binding sledgehammer_smt_slice_fact_frac} (K 0.667)
blanchet@56547
   103
val smt_slice_time_frac =
blanchet@56547
   104
  Attrib.setup_config_real @{binding sledgehammer_smt_slice_time_frac} (K 0.333)
blanchet@56547
   105
val smt_slice_min_secs = Attrib.setup_config_int @{binding sledgehammer_smt_slice_min_secs} (K 3)
blanchet@56547
   106
blanchet@56547
   107
val is_boring_builtin_typ =
blanchet@56547
   108
  not o exists_subtype (member (op =) [@{typ nat}, @{typ int}, HOLogic.realT])
blanchet@56547
   109
blanchet@56547
   110
fun smt_filter_loop name ({debug, overlord, max_mono_iters, max_new_mono_instances, timeout, slice,
blanchet@56547
   111
      ...} : params) state goal i =
blanchet@56547
   112
  let
blanchet@56547
   113
    fun repair_context ctxt =
blanchet@56554
   114
      ctxt |> Context.proof_map (SMT_Config.select_solver name)
blanchet@56547
   115
           |> Config.put SMT_Config.verbose debug
blanchet@56547
   116
           |> (if overlord then
blanchet@56547
   117
                 Config.put SMT_Config.debug_files
blanchet@56547
   118
                   (overlord_file_location_of_prover name |> (fn (path, name) => path ^ "/" ^ name))
blanchet@56547
   119
               else
blanchet@56547
   120
                 I)
blanchet@56547
   121
           |> Config.put SMT_Config.infer_triggers (Config.get ctxt smt_triggers)
blanchet@56547
   122
           |> not (Config.get ctxt smt_builtins)
blanchet@56547
   123
              ? (SMT_Builtin.filter_builtins is_boring_builtin_typ
blanchet@56547
   124
                 #> Config.put SMT_Config.datatypes false)
blanchet@56547
   125
           |> repair_monomorph_context max_mono_iters default_max_mono_iters max_new_mono_instances
blanchet@56547
   126
                default_max_new_mono_instances
blanchet@56547
   127
blanchet@56547
   128
    val state = Proof.map_context (repair_context) state
blanchet@56547
   129
    val ctxt = Proof.context_of state
blanchet@56547
   130
    val max_slices = if slice then Config.get ctxt smt_max_slices else 1
blanchet@56547
   131
blanchet@56547
   132
    fun do_slice timeout slice outcome0 time_so_far
blanchet@56554
   133
        (weighted_factss as (fact_filter, weighted_facts) :: _) =
blanchet@56547
   134
      let
blanchet@56547
   135
        val timer = Timer.startRealTimer ()
blanchet@56547
   136
        val slice_timeout =
blanchet@56547
   137
          if slice < max_slices then
blanchet@56547
   138
            let val ms = Time.toMilliseconds timeout in
blanchet@56547
   139
              Int.min (ms, Int.max (1000 * Config.get ctxt smt_slice_min_secs,
blanchet@56547
   140
                Real.ceil (Config.get ctxt smt_slice_time_frac * Real.fromInt ms)))
blanchet@56547
   141
              |> Time.fromMilliseconds
blanchet@56547
   142
            end
blanchet@56547
   143
          else
blanchet@56547
   144
            timeout
blanchet@56547
   145
        val num_facts = length weighted_facts
blanchet@56547
   146
        val _ =
blanchet@56547
   147
          if debug then
blanchet@56547
   148
            quote name ^ " slice " ^ string_of_int slice ^ " with " ^ string_of_int num_facts ^
blanchet@56547
   149
            " fact" ^ plural_s num_facts ^ " for " ^ string_of_time slice_timeout
blanchet@56547
   150
            |> Output.urgent_message
blanchet@56547
   151
          else
blanchet@56547
   152
            ()
blanchet@56547
   153
        val birth = Timer.checkRealTimer timer
blanchet@56547
   154
        val _ = if debug then Output.urgent_message "Invoking SMT solver..." else ()
blanchet@56547
   155
blanchet@56547
   156
        val (outcome, used_facts) =
blanchet@56547
   157
          SMT_Solver.smt_filter_preprocess ctxt [] goal weighted_facts i
blanchet@56547
   158
          |> SMT_Solver.smt_filter_apply slice_timeout
blanchet@56547
   159
          |> (fn {outcome, used_facts} => (outcome, used_facts))
blanchet@56547
   160
          handle exn =>
blanchet@56547
   161
            if Exn.is_interrupt exn then reraise exn
blanchet@56547
   162
            else (ML_Compiler.exn_message exn |> SMT_Failure.Other_Failure |> SOME, [])
blanchet@56547
   163
blanchet@56547
   164
        val death = Timer.checkRealTimer timer
blanchet@56547
   165
        val outcome0 = if is_none outcome0 then SOME outcome else outcome0
blanchet@56547
   166
        val time_so_far = Time.+ (time_so_far, Time.- (death, birth))
blanchet@56547
   167
blanchet@56547
   168
        val too_many_facts_perhaps =
blanchet@56547
   169
          (case outcome of
blanchet@56547
   170
            NONE => false
blanchet@56547
   171
          | SOME (SMT_Failure.Counterexample _) => false
blanchet@56547
   172
          | SOME SMT_Failure.Time_Out => slice_timeout <> timeout
blanchet@56547
   173
          | SOME (SMT_Failure.Abnormal_Termination _) => true (* kind of *)
blanchet@56547
   174
          | SOME SMT_Failure.Out_Of_Memory => true
blanchet@56547
   175
          | SOME (SMT_Failure.Other_Failure _) => true)
blanchet@56547
   176
blanchet@56547
   177
        val timeout = Time.- (timeout, Timer.checkRealTimer timer)
blanchet@56547
   178
      in
blanchet@56547
   179
        if too_many_facts_perhaps andalso slice < max_slices andalso num_facts > 0 andalso
blanchet@56547
   180
           Time.> (timeout, Time.zeroTime) then
blanchet@56547
   181
          let
blanchet@56547
   182
            val new_num_facts =
blanchet@56547
   183
              Real.ceil (Config.get ctxt smt_slice_fact_frac * Real.fromInt num_facts)
blanchet@56547
   184
            val weighted_factss as (new_fact_filter, _) :: _ =
blanchet@56547
   185
              weighted_factss
blanchet@56547
   186
              |> (fn (x :: xs) => xs @ [x])
blanchet@56547
   187
              |> app_hd (apsnd (take new_num_facts))
blanchet@56547
   188
            val show_filter = fact_filter <> new_fact_filter
blanchet@56547
   189
blanchet@56547
   190
            fun num_of_facts fact_filter num_facts =
blanchet@56547
   191
              string_of_int num_facts ^ (if show_filter then " " ^ quote fact_filter else "") ^
blanchet@56547
   192
              " fact" ^ plural_s num_facts
blanchet@56547
   193
blanchet@56547
   194
            val _ =
blanchet@56547
   195
              if debug then
blanchet@56547
   196
                quote name ^ " invoked with " ^
blanchet@56547
   197
                num_of_facts fact_filter num_facts ^ ": " ^
blanchet@56547
   198
                string_of_atp_failure (failure_of_smt_failure (the outcome)) ^
blanchet@56547
   199
                " Retrying with " ^ num_of_facts new_fact_filter new_num_facts ^
blanchet@56547
   200
                "..."
blanchet@56547
   201
                |> Output.urgent_message
blanchet@56547
   202
              else
blanchet@56547
   203
                ()
blanchet@56547
   204
          in
blanchet@56547
   205
            do_slice timeout (slice + 1) outcome0 time_so_far weighted_factss
blanchet@56547
   206
          end
blanchet@56547
   207
        else
blanchet@56547
   208
          {outcome = if is_none outcome then NONE else the outcome0, used_facts = used_facts,
blanchet@56547
   209
           used_from = map (apsnd snd) weighted_facts, run_time = time_so_far}
blanchet@56547
   210
      end
blanchet@56547
   211
  in
blanchet@56547
   212
    do_slice timeout 1 NONE Time.zeroTime
blanchet@56547
   213
  end
blanchet@56547
   214
blanchet@56547
   215
fun run_smt_solver mode name (params as {debug, verbose, preplay_timeout, ...}) minimize_command
blanchet@56547
   216
    ({state, goal, subgoal, subgoal_count, factss, ...} : prover_problem) =
blanchet@56547
   217
  let
blanchet@56547
   218
    val thy = Proof.theory_of state
blanchet@56547
   219
    val ctxt = Proof.context_of state
blanchet@56547
   220
blanchet@56547
   221
    fun weight_facts facts =
blanchet@56547
   222
      let val num_facts = length facts in
blanchet@56547
   223
        map (weight_smt_fact ctxt num_facts) (facts ~~ (0 upto num_facts - 1))
blanchet@56547
   224
      end
blanchet@56547
   225
blanchet@56547
   226
    val weighted_factss = factss |> map (apsnd weight_facts)
blanchet@56547
   227
    val {outcome, used_facts = used_pairs, used_from, run_time} =
blanchet@56547
   228
      smt_filter_loop name params state goal subgoal weighted_factss
blanchet@56547
   229
    val used_facts = used_pairs |> map fst
blanchet@56547
   230
    val outcome = outcome |> Option.map failure_of_smt_failure
blanchet@56547
   231
blanchet@56547
   232
    val (preplay, message, message_tail) =
blanchet@56547
   233
      (case outcome of
blanchet@56547
   234
        NONE =>
blanchet@56547
   235
        (Lazy.lazy (fn () =>
blanchet@56627
   236
           play_one_line_proof mode debug verbose preplay_timeout used_pairs state subgoal
blanchet@56627
   237
             SMT_Method (bunch_of_proof_methods false liftingN)),
blanchet@56547
   238
         fn preplay =>
blanchet@56547
   239
            let
blanchet@56547
   240
              val one_line_params =
blanchet@56547
   241
                (preplay, proof_banner mode name, used_facts,
blanchet@56547
   242
                 choose_minimize_command thy params minimize_command name preplay, subgoal,
blanchet@56547
   243
                 subgoal_count)
blanchet@56547
   244
              val num_chained = length (#facts (Proof.goal state))
blanchet@56547
   245
            in
blanchet@56547
   246
              one_line_proof_text num_chained one_line_params
blanchet@56547
   247
            end,
blanchet@56547
   248
         if verbose then "\nSMT solver real CPU time: " ^ string_of_time run_time ^ "." else "")
blanchet@56547
   249
      | SOME failure =>
blanchet@56627
   250
        (Lazy.value (Metis_Method (NONE, NONE), Play_Failed),
blanchet@56627
   251
         fn _ => string_of_atp_failure failure, ""))
blanchet@56547
   252
  in
blanchet@56554
   253
    {outcome = outcome, used_facts = used_facts, used_from = used_from, run_time = run_time,
blanchet@56554
   254
     preplay = preplay, message = message, message_tail = message_tail}
blanchet@56547
   255
  end
blanchet@56547
   256
blanchet@56547
   257
end;