src/HOL/Tools/ATP/atp_systems.ML
author blanchet
Wed Feb 09 17:18:58 2011 +0100 (2011-02-09)
changeset 41740 4b09f8b9e012
parent 41738 eb98c60a6cf0
child 41741 839d1488045f
permissions -rw-r--r--
added "Z3 as an ATP" support to Sledgehammer locally
blanchet@38047
     1
(*  Title:      HOL/Tools/ATP/atp_systems.ML
wenzelm@28592
     2
    Author:     Fabian Immler, TU Muenchen
blanchet@36371
     3
    Author:     Jasmin Blanchette, TU Muenchen
wenzelm@28592
     4
blanchet@36376
     5
Setup for supported ATPs.
wenzelm@28592
     6
*)
wenzelm@28592
     7
blanchet@36376
     8
signature ATP_SYSTEMS =
wenzelm@28592
     9
sig
blanchet@39491
    10
  type failure = ATP_Proof.failure
blanchet@38023
    11
blanchet@40059
    12
  type atp_config =
blanchet@38092
    13
    {exec: string * string,
blanchet@38092
    14
     required_execs: (string * string) list,
blanchet@41313
    15
     arguments: bool -> Time.time -> (unit -> (string * real) list) -> string,
blanchet@38645
    16
     has_incomplete_mode: bool,
blanchet@38023
    17
     proof_delims: (string * string) list,
blanchet@38023
    18
     known_failures: (failure * string) list,
blanchet@38744
    19
     default_max_relevant: int,
blanchet@38631
    20
     explicit_forall: bool,
blanchet@38631
    21
     use_conjecture_for_hypotheses: bool}
blanchet@38023
    22
blanchet@41725
    23
  datatype e_weight_method = E_Auto | E_Fun_Weight | E_Sym_Offset_Weight
blanchet@41725
    24
blanchet@41313
    25
  (* for experimentation purposes -- do not use in production code *)
blanchet@41725
    26
  val e_weight_method : e_weight_method Unsynchronized.ref
blanchet@41725
    27
  val e_default_fun_weight : real Unsynchronized.ref
blanchet@41725
    28
  val e_fun_weight_base : real Unsynchronized.ref
blanchet@41725
    29
  val e_fun_weight_factor : real Unsynchronized.ref
blanchet@41725
    30
  val e_default_sym_offs_weight : real Unsynchronized.ref
blanchet@41725
    31
  val e_sym_offs_weight_base : real Unsynchronized.ref
blanchet@41725
    32
  val e_sym_offs_weight_factor : real Unsynchronized.ref
blanchet@41313
    33
blanchet@40059
    34
  val eN : string
blanchet@40059
    35
  val spassN : string
blanchet@40059
    36
  val vampireN : string
blanchet@40059
    37
  val sine_eN : string
blanchet@40059
    38
  val snarkN : string
blanchet@41738
    39
  val z3_atpN : string
blanchet@40060
    40
  val remote_prefix : string
blanchet@41738
    41
  val remote_atp :
blanchet@41738
    42
    string -> string -> string list -> (string * string) list
blanchet@41738
    43
    -> (failure * string) list -> int -> bool -> string * atp_config
blanchet@40059
    44
  val add_atp : string * atp_config -> theory -> theory
blanchet@40059
    45
  val get_atp : theory -> string -> atp_config
blanchet@41727
    46
  val supported_atps : theory -> string list
blanchet@40059
    47
  val is_atp_installed : theory -> string -> bool
blanchet@35867
    48
  val refresh_systems_on_tptp : unit -> unit
blanchet@35867
    49
  val setup : theory -> theory
wenzelm@28592
    50
end;
wenzelm@28592
    51
blanchet@36376
    52
structure ATP_Systems : ATP_SYSTEMS =
wenzelm@28592
    53
struct
wenzelm@28596
    54
blanchet@39491
    55
open ATP_Proof
boehmes@32864
    56
blanchet@40059
    57
(* ATP configuration *)
boehmes@32864
    58
blanchet@40059
    59
type atp_config =
blanchet@38092
    60
  {exec: string * string,
blanchet@38092
    61
   required_execs: (string * string) list,
blanchet@41313
    62
   arguments: bool -> Time.time -> (unit -> (string * real) list) -> string,
blanchet@38645
    63
   has_incomplete_mode: bool,
blanchet@36370
    64
   proof_delims: (string * string) list,
blanchet@36370
    65
   known_failures: (failure * string) list,
blanchet@38744
    66
   default_max_relevant: int,
blanchet@38631
    67
   explicit_forall: bool,
blanchet@38631
    68
   use_conjecture_for_hypotheses: bool}
wenzelm@28596
    69
blanchet@38061
    70
val known_perl_failures =
blanchet@38094
    71
  [(CantConnect, "HTTP error"),
blanchet@38094
    72
   (NoPerl, "env: perl"),
blanchet@38065
    73
   (NoLibwwwPerl, "Can't locate HTTP")]
wenzelm@28596
    74
blanchet@40059
    75
(* named ATPs *)
blanchet@40059
    76
blanchet@40059
    77
val eN = "e"
blanchet@40059
    78
val spassN = "spass"
blanchet@40059
    79
val vampireN = "vampire"
blanchet@41740
    80
val z3_atpN = "z3_atp"
blanchet@40059
    81
val sine_eN = "sine_e"
blanchet@40059
    82
val snarkN = "snark"
blanchet@40060
    83
val remote_prefix = "remote_"
blanchet@38001
    84
blanchet@38023
    85
structure Data = Theory_Data
blanchet@38023
    86
(
blanchet@40059
    87
  type T = (atp_config * stamp) Symtab.table
blanchet@38023
    88
  val empty = Symtab.empty
blanchet@38023
    89
  val extend = I
blanchet@38023
    90
  fun merge data : T = Symtab.merge (eq_snd op =) data
blanchet@38023
    91
    handle Symtab.DUP name => error ("Duplicate ATP: " ^ quote name ^ ".")
blanchet@38023
    92
)
blanchet@38017
    93
blanchet@38737
    94
fun to_secs bonus time = (Time.toMilliseconds time + bonus + 999) div 1000
blanchet@36142
    95
blanchet@39491
    96
blanchet@40059
    97
(* E *)
wenzelm@28596
    98
blanchet@40344
    99
(* Give E an extra second to reconstruct the proof. Older versions even get two
blanchet@40344
   100
   seconds, because the "eproof" script wrongly subtracted an entire second to
blanchet@40344
   101
   account for the overhead of the script itself, which is in fact much
blanchet@40344
   102
   lower. *)
blanchet@38737
   103
fun e_bonus () =
blanchet@41334
   104
  if string_ord (getenv "E_VERSION", "1.1") = LESS then 2000 else 1000
blanchet@38737
   105
blanchet@36369
   106
val tstp_proof_delims =
blanchet@36369
   107
  ("# SZS output start CNFRefutation.", "# SZS output end CNFRefutation")
blanchet@36369
   108
blanchet@41725
   109
datatype e_weight_method = E_Auto | E_Fun_Weight | E_Sym_Offset_Weight
blanchet@41725
   110
blanchet@41725
   111
val e_weight_method = Unsynchronized.ref E_Fun_Weight
blanchet@41725
   112
val e_default_fun_weight = Unsynchronized.ref 0.5
blanchet@41725
   113
val e_fun_weight_base = Unsynchronized.ref 0.0
blanchet@41725
   114
val e_fun_weight_factor = Unsynchronized.ref 40.0
blanchet@41725
   115
val e_default_sym_offs_weight = Unsynchronized.ref 0.0
blanchet@41725
   116
val e_sym_offs_weight_base = Unsynchronized.ref ~30.0
blanchet@41725
   117
val e_sym_offs_weight_factor = Unsynchronized.ref ~30.0
blanchet@41725
   118
blanchet@41725
   119
fun e_weight_method_case fw sow =
blanchet@41725
   120
  case !e_weight_method of
blanchet@41725
   121
    E_Auto => raise Fail "Unexpected \"E_Auto\""
blanchet@41725
   122
  | E_Fun_Weight => fw
blanchet@41725
   123
  | E_Sym_Offset_Weight => sow
blanchet@41725
   124
blanchet@41725
   125
fun scaled_e_weight w =
blanchet@41725
   126
  e_weight_method_case (!e_fun_weight_base) (!e_sym_offs_weight_base)
blanchet@41725
   127
  + w * e_weight_method_case (!e_fun_weight_factor) (!e_sym_offs_weight_factor)
blanchet@41725
   128
  |> Real.ceil |> signed_string_of_int
blanchet@41313
   129
blanchet@41335
   130
fun e_weight_arguments weights =
blanchet@41725
   131
  if !e_weight_method = E_Auto orelse
blanchet@41725
   132
     string_ord (getenv "E_VERSION", "1.2w") = LESS then
blanchet@41725
   133
    "-xAutoDev"
blanchet@41725
   134
  else
blanchet@41314
   135
    "--split-clauses=4 --split-reuse-defs --simul-paramod --forward-context-sr \
blanchet@41314
   136
    \--destructive-er-aggressive --destructive-er --presat-simplify \
blanchet@41314
   137
    \--prefer-initial-clauses -tKBO6 -winvfreqrank -c1 -Ginvfreqconjmax -F1 \
blanchet@41314
   138
    \--delete-bad-limit=150000000 -WSelectMaxLComplexAvoidPosPred \
blanchet@41725
   139
    \-H'(4*" ^ e_weight_method_case "FunWeight" "SymOffsetWeight" ^
blanchet@41725
   140
    "(SimulateSOS, " ^
blanchet@41725
   141
    scaled_e_weight (e_weight_method_case (!e_default_fun_weight)
blanchet@41725
   142
                                          (!e_default_sym_offs_weight)) ^
blanchet@41314
   143
    ",20,1.5,1.5,1" ^
blanchet@41725
   144
    (weights () |> map (fn (s, w) => "," ^ s ^ ":" ^ scaled_e_weight w)
blanchet@41725
   145
                |> implode) ^
blanchet@41314
   146
    "),3*ConjectureGeneralSymbolWeight(PreferNonGoals,200,100,200,50,50,1,100,\
blanchet@41314
   147
    \1.5,1.5,1),1*Clauseweight(PreferProcessed,1,1,1),1*\
blanchet@41314
   148
    \FIFOWeight(PreferProcessed))'"
blanchet@41313
   149
blanchet@40059
   150
val e_config : atp_config =
blanchet@38092
   151
  {exec = ("E_HOME", "eproof"),
blanchet@38092
   152
   required_execs = [],
blanchet@41313
   153
   arguments = fn _ => fn timeout => fn weights =>
blanchet@41335
   154
     "--tstp-in --tstp-out -l5 " ^ e_weight_arguments weights ^ " -tAutoDev \
blanchet@41313
   155
     \--silent --cpu-limit=" ^ string_of_int (to_secs (e_bonus ()) timeout),
blanchet@38645
   156
   has_incomplete_mode = false,
blanchet@36369
   157
   proof_delims = [tstp_proof_delims],
blanchet@36265
   158
   known_failures =
blanchet@37995
   159
     [(Unprovable, "SZS status: CounterSatisfiable"),
blanchet@37995
   160
      (Unprovable, "SZS status CounterSatisfiable"),
blanchet@36370
   161
      (TimedOut, "Failure: Resource limit exceeded (time)"),
blanchet@36370
   162
      (TimedOut, "time limit exceeded"),
blanchet@36370
   163
      (OutOfResources,
blanchet@36370
   164
       "# Cannot determine problem status within resource limit"),
blanchet@36370
   165
      (OutOfResources, "SZS status: ResourceOut"),
blanchet@36370
   166
      (OutOfResources, "SZS status ResourceOut")],
blanchet@38744
   167
   default_max_relevant = 500 (* FUDGE *),
blanchet@38631
   168
   explicit_forall = false,
blanchet@38631
   169
   use_conjecture_for_hypotheses = true}
blanchet@38454
   170
blanchet@40059
   171
val e = (eN, e_config)
wenzelm@28596
   172
wenzelm@28596
   173
blanchet@39491
   174
(* SPASS *)
blanchet@39491
   175
blanchet@36219
   176
(* The "-VarWeight=3" option helps the higher-order problems, probably by
blanchet@36219
   177
   counteracting the presence of "hAPP". *)
blanchet@40059
   178
val spass_config : atp_config =
blanchet@38092
   179
  {exec = ("ISABELLE_ATP", "scripts/spass"),
blanchet@39002
   180
   required_execs = [("SPASS_HOME", "SPASS"), ("SPASS_HOME", "tptp2dfg")],
blanchet@41313
   181
   arguments = fn complete => fn timeout => fn _ =>
blanchet@37962
   182
     ("-Auto -PGiven=0 -PProblem=0 -Splits=0 -FullRed=0 -DocProof \
blanchet@38737
   183
      \-VarWeight=3 -TimeLimit=" ^ string_of_int (to_secs 0 timeout))
blanchet@37514
   184
     |> not complete ? prefix "-SOS=1 ",
blanchet@38645
   185
   has_incomplete_mode = true,
blanchet@36369
   186
   proof_delims = [("Here is a proof", "Formulae used in the proof")],
blanchet@36289
   187
   known_failures =
blanchet@38061
   188
     known_perl_failures @
blanchet@37413
   189
     [(IncompleteUnprovable, "SPASS beiseite: Completion found"),
blanchet@36370
   190
      (TimedOut, "SPASS beiseite: Ran out of time"),
blanchet@36965
   191
      (OutOfResources, "SPASS beiseite: Maximal number of loops exceeded"),
blanchet@37413
   192
      (MalformedInput, "Undefined symbol"),
blanchet@37414
   193
      (MalformedInput, "Free Variable"),
blanchet@39263
   194
      (SpassTooOld, "tptp2dfg"),
blanchet@39263
   195
      (InternalError, "Please report this error")],
blanchet@38744
   196
   default_max_relevant = 350 (* FUDGE *),
blanchet@38631
   197
   explicit_forall = true,
blanchet@38631
   198
   use_conjecture_for_hypotheses = true}
blanchet@38454
   199
blanchet@40059
   200
val spass = (spassN, spass_config)
wenzelm@28596
   201
blanchet@38454
   202
blanchet@37509
   203
(* Vampire *)
blanchet@37509
   204
blanchet@40059
   205
val vampire_config : atp_config =
blanchet@38092
   206
  {exec = ("VAMPIRE_HOME", "vampire"),
blanchet@38092
   207
   required_execs = [],
blanchet@41313
   208
   arguments = fn complete => fn timeout => fn _ =>
blanchet@41203
   209
     (* This would work too but it's less tested: "--proof tptp " ^ *)
blanchet@41203
   210
     "--mode casc -t " ^ string_of_int (to_secs 0 timeout) ^
blanchet@41203
   211
     " --thanks \"Andrei and Krystof\" --input_file"
blanchet@38744
   212
     |> not complete ? prefix "--sos on ",
blanchet@38744
   213
   has_incomplete_mode = true,
blanchet@37509
   214
   proof_delims =
blanchet@37509
   215
     [("=========== Refutation ==========",
blanchet@37509
   216
       "======= End of refutation ======="),
blanchet@38033
   217
      ("% SZS output start Refutation", "% SZS output end Refutation"),
blanchet@38033
   218
      ("% SZS output start Proof", "% SZS output end Proof")],
blanchet@37509
   219
   known_failures =
blanchet@37509
   220
     [(Unprovable, "UNPROVABLE"),
blanchet@37509
   221
      (IncompleteUnprovable, "CANNOT PROVE"),
blanchet@38092
   222
      (TimedOut, "SZS status Timeout"),
blanchet@37509
   223
      (Unprovable, "Satisfiability detected"),
blanchet@38647
   224
      (Unprovable, "Termination reason: Satisfiable"),
blanchet@39263
   225
      (VampireTooOld, "not a valid option"),
blanchet@39263
   226
      (Interrupted, "Aborted by signal SIGINT")],
blanchet@38744
   227
   default_max_relevant = 400 (* FUDGE *),
blanchet@38631
   228
   explicit_forall = false,
blanchet@38680
   229
   use_conjecture_for_hypotheses = true}
blanchet@38454
   230
blanchet@40059
   231
val vampire = (vampireN, vampire_config)
blanchet@37509
   232
blanchet@38454
   233
blanchet@41740
   234
(* Z3 with TPTP syntax *)
blanchet@41740
   235
blanchet@41740
   236
val z3_atp_config : atp_config =
blanchet@41740
   237
  {exec = ("Z3_HOME", "z3"),
blanchet@41740
   238
   required_execs = [],
blanchet@41740
   239
   arguments = fn _ => fn timeout => fn _ =>
blanchet@41740
   240
     "MBQI=true /p /t:" ^
blanchet@41740
   241
     string_of_int (to_secs 0 timeout),
blanchet@41740
   242
   has_incomplete_mode = false,
blanchet@41740
   243
   proof_delims = [],
blanchet@41740
   244
   known_failures =
blanchet@41740
   245
     [(IncompleteUnprovable, "\nsat"),
blanchet@41740
   246
      (ProofMissing, "\nunsat")],
blanchet@41740
   247
   default_max_relevant = 250 (* FUDGE *),
blanchet@41740
   248
   explicit_forall = true,
blanchet@41740
   249
   use_conjecture_for_hypotheses = false}
blanchet@41740
   250
blanchet@41740
   251
val z3_atp = (z3_atpN, z3_atp_config)
blanchet@41740
   252
blanchet@41740
   253
blanchet@40059
   254
(* Remote ATP invocation via SystemOnTPTP *)
wenzelm@28596
   255
blanchet@38061
   256
val systems = Synchronized.var "atp_systems" ([] : string list)
immler@31835
   257
immler@31835
   258
fun get_systems () =
blanchet@38061
   259
  case bash_output "\"$ISABELLE_ATP/scripts/remote_atp\" -w 2>&1" of
blanchet@39491
   260
    (output, 0) => split_lines output
blanchet@39491
   261
  | (output, _) =>
blanchet@39491
   262
    error (case extract_known_failure known_perl_failures output of
blanchet@40669
   263
             SOME failure => string_for_failure "ATP" failure
blanchet@39491
   264
           | NONE => perhaps (try (unsuffix "\n")) output ^ ".")
immler@31835
   265
blanchet@38690
   266
fun find_system name [] systems = find_first (String.isPrefix name) systems
blanchet@38690
   267
  | find_system name (version :: versions) systems =
blanchet@38690
   268
    case find_first (String.isPrefix (name ^ "---" ^ version)) systems of
blanchet@38690
   269
      NONE => find_system name versions systems
blanchet@38690
   270
    | res => res
blanchet@38690
   271
blanchet@38690
   272
fun get_system name versions =
blanchet@38589
   273
  Synchronized.change_result systems
blanchet@38589
   274
      (fn systems => (if null systems then get_systems () else systems)
blanchet@38690
   275
                     |> `(find_system name versions))
boehmes@32864
   276
blanchet@38690
   277
fun the_system name versions =
blanchet@38690
   278
  case get_system name versions of
blanchet@39010
   279
    SOME sys => sys
blanchet@41269
   280
  | NONE => error ("System " ^ quote name ^
blanchet@41269
   281
                   " is not available at SystemOnTPTP.")
immler@31835
   282
blanchet@41148
   283
val max_remote_secs = 240 (* give Geoff Sutcliffe's servers a break *)
blanchet@41148
   284
blanchet@38690
   285
fun remote_config system_name system_versions proof_delims known_failures
blanchet@38997
   286
                  default_max_relevant use_conjecture_for_hypotheses
blanchet@40059
   287
                  : atp_config =
blanchet@38092
   288
  {exec = ("ISABELLE_ATP", "scripts/remote_atp"),
blanchet@38092
   289
   required_execs = [],
blanchet@41313
   290
   arguments = fn _ => fn timeout => fn _ =>
blanchet@41148
   291
     " -t " ^ string_of_int (Int.min (max_remote_secs, (to_secs 0 timeout)))
blanchet@41148
   292
     ^ " -s " ^ the_system system_name system_versions,
blanchet@38645
   293
   has_incomplete_mode = false,
blanchet@36369
   294
   proof_delims = insert (op =) tstp_proof_delims proof_delims,
blanchet@38061
   295
   known_failures =
blanchet@38061
   296
     known_failures @ known_perl_failures @
blanchet@38094
   297
     [(TimedOut, "says Timeout")],
blanchet@38744
   298
   default_max_relevant = default_max_relevant,
blanchet@38631
   299
   explicit_forall = true,
blanchet@38631
   300
   use_conjecture_for_hypotheses = use_conjecture_for_hypotheses}
wenzelm@28596
   301
blanchet@38690
   302
fun remotify_config system_name system_versions
blanchet@38744
   303
        ({proof_delims, known_failures, default_max_relevant,
blanchet@40059
   304
          use_conjecture_for_hypotheses, ...} : atp_config) : atp_config =
blanchet@38690
   305
  remote_config system_name system_versions proof_delims known_failures
blanchet@38997
   306
                default_max_relevant use_conjecture_for_hypotheses
blanchet@38023
   307
blanchet@40059
   308
fun remote_atp name system_name system_versions proof_delims known_failures
blanchet@40059
   309
               default_max_relevant use_conjecture_for_hypotheses =
blanchet@40060
   310
  (remote_prefix ^ name,
blanchet@38690
   311
   remote_config system_name system_versions proof_delims known_failures
blanchet@38997
   312
                 default_max_relevant use_conjecture_for_hypotheses)
blanchet@40059
   313
fun remotify_atp (name, config) system_name system_versions =
blanchet@40060
   314
  (remote_prefix ^ name, remotify_config system_name system_versions config)
wenzelm@28592
   315
blanchet@40059
   316
val remote_e = remotify_atp e "EP" ["1.0", "1.1", "1.2"]
blanchet@40059
   317
val remote_vampire = remotify_atp vampire "Vampire" ["0.6", "9.0", "1.0"]
blanchet@41738
   318
val remote_z3_atp =
blanchet@41738
   319
  remote_atp z3_atpN "Z3---" ["2.18"] []
blanchet@41738
   320
             [(IncompleteUnprovable, "SZS status Satisfiable"),
blanchet@41738
   321
              (ProofMissing, "SZS status Unsatisfiable")]
blanchet@41738
   322
             250 (* FUDGE *) false
blanchet@41740
   323
val remote_sine_e =
blanchet@41740
   324
  remote_atp sine_eN "SInE" [] [] [(IncompleteUnprovable, "says Unknown")]
blanchet@41740
   325
             600 (* FUDGE *) true
blanchet@41740
   326
val remote_snark =
blanchet@41740
   327
  remote_atp snarkN "SNARK---" [] [("refutation.", "end_refutation.")] []
blanchet@41740
   328
             250 (* FUDGE *) true
blanchet@38454
   329
blanchet@38454
   330
(* Setup *)
blanchet@38454
   331
blanchet@40059
   332
fun add_atp (name, config) thy =
blanchet@40059
   333
  Data.map (Symtab.update_new (name, (config, stamp ()))) thy
blanchet@40059
   334
  handle Symtab.DUP name => error ("Duplicate ATP: " ^ quote name ^ ".")
blanchet@40059
   335
blanchet@40059
   336
fun get_atp thy name =
blanchet@40059
   337
  the (Symtab.lookup (Data.get thy) name) |> fst
blanchet@40059
   338
  handle Option.Option => error ("Unknown ATP: " ^ name ^ ".")
blanchet@40059
   339
blanchet@41727
   340
val supported_atps = Symtab.keys o Data.get
blanchet@36371
   341
blanchet@40059
   342
fun is_atp_installed thy name =
blanchet@40059
   343
  let val {exec, required_execs, ...} = get_atp thy name in
blanchet@40059
   344
    forall (curry (op <>) "" o getenv o fst) (exec :: required_execs)
blanchet@40059
   345
  end
blanchet@36371
   346
blanchet@40059
   347
fun refresh_systems_on_tptp () =
blanchet@40059
   348
  Synchronized.change systems (fn _ => get_systems ())
blanchet@40059
   349
blanchet@41740
   350
val atps = [e, spass, vampire, z3_atp, remote_e, remote_vampire, remote_z3_atp,
blanchet@41740
   351
            remote_sine_e, remote_snark]
blanchet@40059
   352
val setup = fold add_atp atps
blanchet@35867
   353
wenzelm@28592
   354
end;