src/HOL/Tools/ATP_Manager/atp_systems.ML
author blanchet
Tue, 27 Jul 2010 17:43:11 +0200
changeset 38019 e207a64e1e0b
parent 38017 3ad3e3ca2451
child 38021 e024504943d1
permissions -rw-r--r--
complete renaming of "Sledgehammer_TPTP_Format" to "ATP_Problem"
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36376
e83d52a52449 renamed module "ATP_Wrapper" to "ATP_Systems"
blanchet
parents: 36371
diff changeset
     1
(*  Title:      HOL/Tools/ATP_Manager/atp_systems.ML
28592
824f8390aaa2 renamed AtpThread to AtpWrapper;
wenzelm
parents:
diff changeset
     2
    Author:     Fabian Immler, TU Muenchen
36371
8c83ea1a7740 move the Sledgehammer menu options to "sledgehammer_isar.ML"
blanchet
parents: 36370
diff changeset
     3
    Author:     Jasmin Blanchette, TU Muenchen
28592
824f8390aaa2 renamed AtpThread to AtpWrapper;
wenzelm
parents:
diff changeset
     4
36376
e83d52a52449 renamed module "ATP_Wrapper" to "ATP_Systems"
blanchet
parents: 36371
diff changeset
     5
Setup for supported ATPs.
28592
824f8390aaa2 renamed AtpThread to AtpWrapper;
wenzelm
parents:
diff changeset
     6
*)
824f8390aaa2 renamed AtpThread to AtpWrapper;
wenzelm
parents:
diff changeset
     7
36376
e83d52a52449 renamed module "ATP_Wrapper" to "ATP_Systems"
blanchet
parents: 36371
diff changeset
     8
signature ATP_SYSTEMS =
28592
824f8390aaa2 renamed AtpThread to AtpWrapper;
wenzelm
parents:
diff changeset
     9
sig
37623
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
    10
  val trace : bool Unsynchronized.ref
36376
e83d52a52449 renamed module "ATP_Wrapper" to "ATP_Systems"
blanchet
parents: 36371
diff changeset
    11
  val dest_dir : string Config.T
35867
16279c4c7a33 move all ATP setup code into ATP_Wrapper
blanchet
parents: 35865
diff changeset
    12
  val problem_prefix : string Config.T
16279c4c7a33 move all ATP setup code into ATP_Wrapper
blanchet
parents: 35865
diff changeset
    13
  val measure_runtime : bool Config.T
16279c4c7a33 move all ATP setup code into ATP_Wrapper
blanchet
parents: 35865
diff changeset
    14
  val refresh_systems_on_tptp : unit -> unit
36371
8c83ea1a7740 move the Sledgehammer menu options to "sledgehammer_isar.ML"
blanchet
parents: 36370
diff changeset
    15
  val default_atps_param_value : unit -> string
35867
16279c4c7a33 move all ATP setup code into ATP_Wrapper
blanchet
parents: 35865
diff changeset
    16
  val setup : theory -> theory
28592
824f8390aaa2 renamed AtpThread to AtpWrapper;
wenzelm
parents:
diff changeset
    17
end;
824f8390aaa2 renamed AtpThread to AtpWrapper;
wenzelm
parents:
diff changeset
    18
36376
e83d52a52449 renamed module "ATP_Wrapper" to "ATP_Systems"
blanchet
parents: 36371
diff changeset
    19
structure ATP_Systems : ATP_SYSTEMS =
28592
824f8390aaa2 renamed AtpThread to AtpWrapper;
wenzelm
parents:
diff changeset
    20
struct
28596
fcd463a6b6de tuned interfaces -- plain prover function, without thread;
wenzelm
parents: 28592
diff changeset
    21
37578
9367cb36b1c4 renamed "Sledgehammer_FOL_Clauses" to "Metis_Clauses", so that Metis doesn't depend on Sledgehammer
blanchet
parents: 37577
diff changeset
    22
open Metis_Clauses
36187
4deef08608ee added timestamp to proof
blanchet
parents: 36169
diff changeset
    23
open Sledgehammer_Util
35865
2f8fb5242799 more Sledgehammer refactoring
blanchet
parents: 35826
diff changeset
    24
open Sledgehammer_Fact_Filter
38019
e207a64e1e0b complete renaming of "Sledgehammer_TPTP_Format" to "ATP_Problem"
blanchet
parents: 38017
diff changeset
    25
open ATP_Problem
35865
2f8fb5242799 more Sledgehammer refactoring
blanchet
parents: 35826
diff changeset
    26
open Sledgehammer_Proof_Reconstruct
35867
16279c4c7a33 move all ATP setup code into ATP_Wrapper
blanchet
parents: 35865
diff changeset
    27
open ATP_Manager
35826
1590abc3d42a renamed Sledgehammer structures
blanchet
parents: 35570
diff changeset
    28
37623
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
    29
val trace = Unsynchronized.ref false
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
    30
fun trace_msg msg = if !trace then tracing (msg ()) else ()
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
    31
36376
e83d52a52449 renamed module "ATP_Wrapper" to "ATP_Systems"
blanchet
parents: 36371
diff changeset
    32
(** generic ATP **)
28596
fcd463a6b6de tuned interfaces -- plain prover function, without thread;
wenzelm
parents: 28592
diff changeset
    33
32944
ecc0705174c2 clarified File.platform_path vs. File.shell_path;
wenzelm
parents: 32942
diff changeset
    34
(* external problem files *)
32864
a226f29d4bdc re-organized signature of AtpWrapper structure: records instead of unnamed parameters and return values,
boehmes
parents: 32740
diff changeset
    35
36376
e83d52a52449 renamed module "ATP_Wrapper" to "ATP_Systems"
blanchet
parents: 36371
diff changeset
    36
val (dest_dir, dest_dir_setup) = Attrib.config_string "atp_dest_dir" (K "");
32864
a226f29d4bdc re-organized signature of AtpWrapper structure: records instead of unnamed parameters and return values,
boehmes
parents: 32740
diff changeset
    37
  (*Empty string means create files in Isabelle's temporary files directory.*)
a226f29d4bdc re-organized signature of AtpWrapper structure: records instead of unnamed parameters and return values,
boehmes
parents: 32740
diff changeset
    38
a226f29d4bdc re-organized signature of AtpWrapper structure: records instead of unnamed parameters and return values,
boehmes
parents: 32740
diff changeset
    39
val (problem_prefix, problem_prefix_setup) =
36001
992839c4be90 static defaults for configuration options;
wenzelm
parents: 35969
diff changeset
    40
  Attrib.config_string "atp_problem_prefix" (K "prob");
32864
a226f29d4bdc re-organized signature of AtpWrapper structure: records instead of unnamed parameters and return values,
boehmes
parents: 32740
diff changeset
    41
33247
ed1681284f62 measure runtime of ATPs only if requested
boehmes
parents: 33082
diff changeset
    42
val (measure_runtime, measure_runtime_setup) =
36001
992839c4be90 static defaults for configuration options;
wenzelm
parents: 35969
diff changeset
    43
  Attrib.config_bool "atp_measure_runtime" (K false);
33247
ed1681284f62 measure runtime of ATPs only if requested
boehmes
parents: 33082
diff changeset
    44
32864
a226f29d4bdc re-organized signature of AtpWrapper structure: records instead of unnamed parameters and return values,
boehmes
parents: 32740
diff changeset
    45
35867
16279c4c7a33 move all ATP setup code into ATP_Wrapper
blanchet
parents: 35865
diff changeset
    46
(* prover configuration *)
32864
a226f29d4bdc re-organized signature of AtpWrapper structure: records instead of unnamed parameters and return values,
boehmes
parents: 32740
diff changeset
    47
32941
72d48e333b77 eliminated extraneous wrapping of public records;
wenzelm
parents: 32936
diff changeset
    48
type prover_config =
36917
8674cdb0b8cc query _HOME environment variables at run-time, not at build-time
blanchet
parents: 36910
diff changeset
    49
  {home_var: string,
36370
a4f601daa175 centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents: 36369
diff changeset
    50
   executable: string,
37514
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
    51
   arguments: bool -> Time.time -> string,
36370
a4f601daa175 centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents: 36369
diff changeset
    52
   proof_delims: (string * string) list,
a4f601daa175 centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents: 36369
diff changeset
    53
   known_failures: (failure * string) list,
38009
34e1ac9cb71d shrink the "max_new_relevant_facts_per_iter" fudge factors, now that we count formulas and not clauses
blanchet
parents: 38005
diff changeset
    54
   max_new_relevant_facts_per_iter: int,
37994
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37989
diff changeset
    55
   prefers_theory_relevant: bool,
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37989
diff changeset
    56
   explicit_forall: bool}
28596
fcd463a6b6de tuned interfaces -- plain prover function, without thread;
wenzelm
parents: 28592
diff changeset
    57
fcd463a6b6de tuned interfaces -- plain prover function, without thread;
wenzelm
parents: 28592
diff changeset
    58
fcd463a6b6de tuned interfaces -- plain prover function, without thread;
wenzelm
parents: 28592
diff changeset
    59
(* basic template *)
fcd463a6b6de tuned interfaces -- plain prover function, without thread;
wenzelm
parents: 28592
diff changeset
    60
36370
a4f601daa175 centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents: 36369
diff changeset
    61
val remotify = prefix "remote_"
a4f601daa175 centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents: 36369
diff changeset
    62
32458
de6834b20e9e sledgehammer's temporary files are removed properly (even in case of an exception occurs)
boehmes
parents: 32451
diff changeset
    63
fun with_path cleanup after f path =
de6834b20e9e sledgehammer's temporary files are removed properly (even in case of an exception occurs)
boehmes
parents: 32451
diff changeset
    64
  Exn.capture f path
de6834b20e9e sledgehammer's temporary files are removed properly (even in case of an exception occurs)
boehmes
parents: 32451
diff changeset
    65
  |> tap (fn _ => cleanup path)
de6834b20e9e sledgehammer's temporary files are removed properly (even in case of an exception occurs)
boehmes
parents: 32451
diff changeset
    66
  |> Exn.release
36369
d2cd0d04b8e6 handle ATP proof delimiters in a cleaner, more extensible fashion
blanchet
parents: 36289
diff changeset
    67
  |> tap (after path)
d2cd0d04b8e6 handle ATP proof delimiters in a cleaner, more extensible fashion
blanchet
parents: 36289
diff changeset
    68
d2cd0d04b8e6 handle ATP proof delimiters in a cleaner, more extensible fashion
blanchet
parents: 36289
diff changeset
    69
(* Splits by the first possible of a list of delimiters. *)
d2cd0d04b8e6 handle ATP proof delimiters in a cleaner, more extensible fashion
blanchet
parents: 36289
diff changeset
    70
fun extract_proof delims output =
d2cd0d04b8e6 handle ATP proof delimiters in a cleaner, more extensible fashion
blanchet
parents: 36289
diff changeset
    71
  case pairself (find_first (fn s => String.isSubstring s output))
d2cd0d04b8e6 handle ATP proof delimiters in a cleaner, more extensible fashion
blanchet
parents: 36289
diff changeset
    72
                (ListPair.unzip delims) of
d2cd0d04b8e6 handle ATP proof delimiters in a cleaner, more extensible fashion
blanchet
parents: 36289
diff changeset
    73
    (SOME begin_delim, SOME end_delim) =>
36549
d29617bcc1fb properly extract SPASS proof
blanchet
parents: 36548
diff changeset
    74
    (output |> first_field begin_delim |> the |> snd
d29617bcc1fb properly extract SPASS proof
blanchet
parents: 36548
diff changeset
    75
            |> first_field end_delim |> the |> fst
d29617bcc1fb properly extract SPASS proof
blanchet
parents: 36548
diff changeset
    76
            |> first_field "\n" |> the |> snd
d29617bcc1fb properly extract SPASS proof
blanchet
parents: 36548
diff changeset
    77
     handle Option.Option => "")
36369
d2cd0d04b8e6 handle ATP proof delimiters in a cleaner, more extensible fashion
blanchet
parents: 36289
diff changeset
    78
  | _ => ""
32458
de6834b20e9e sledgehammer's temporary files are removed properly (even in case of an exception occurs)
boehmes
parents: 32451
diff changeset
    79
37514
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
    80
fun extract_proof_and_outcome complete res_code proof_delims known_failures
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
    81
                              output =
36370
a4f601daa175 centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents: 36369
diff changeset
    82
  case map_filter (fn (failure, pattern) =>
a4f601daa175 centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents: 36369
diff changeset
    83
                      if String.isSubstring pattern output then SOME failure
a4f601daa175 centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents: 36369
diff changeset
    84
                      else NONE) known_failures of
36369
d2cd0d04b8e6 handle ATP proof delimiters in a cleaner, more extensible fashion
blanchet
parents: 36289
diff changeset
    85
    [] => (case extract_proof proof_delims output of
36370
a4f601daa175 centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents: 36369
diff changeset
    86
             "" => ("", SOME UnknownError)
a4f601daa175 centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents: 36369
diff changeset
    87
           | proof => if res_code = 0 then (proof, NONE)
a4f601daa175 centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents: 36369
diff changeset
    88
                      else ("", SOME UnknownError))
37514
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
    89
  | (failure :: _) =>
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
    90
    ("", SOME (if failure = IncompleteUnprovable andalso complete then
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
    91
                 Unprovable
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
    92
               else
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
    93
                 failure))
36370
a4f601daa175 centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents: 36369
diff changeset
    94
a4f601daa175 centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents: 36369
diff changeset
    95
fun string_for_failure Unprovable = "The ATP problem is unprovable."
37413
e856582fe9c4 improve ATP-specific error messages
blanchet
parents: 37347
diff changeset
    96
  | string_for_failure IncompleteUnprovable =
e856582fe9c4 improve ATP-specific error messages
blanchet
parents: 37347
diff changeset
    97
    "The ATP cannot prove the problem."
37627
1d1754ccd233 more precise error message for remote ATPs
blanchet
parents: 37625
diff changeset
    98
  | string_for_failure CantConnect = "Can't connect to remote ATP."
36370
a4f601daa175 centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents: 36369
diff changeset
    99
  | string_for_failure TimedOut = "Timed out."
a4f601daa175 centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents: 36369
diff changeset
   100
  | string_for_failure OutOfResources = "The ATP ran out of resources."
a4f601daa175 centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents: 36369
diff changeset
   101
  | string_for_failure OldSpass =
36393
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36382
diff changeset
   102
    (* FIXME: Change the error message below to point to the Isabelle download
37997
abf8a79853c9 kill Skolem handling in Sledgehammer
blanchet
parents: 37996
diff changeset
   103
       page once the package is there. *)
36370
a4f601daa175 centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents: 36369
diff changeset
   104
    "Warning: Sledgehammer requires a more recent version of SPASS with \
a4f601daa175 centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents: 36369
diff changeset
   105
    \support for the TPTP syntax. To install it, download and untar the \
a4f601daa175 centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents: 36369
diff changeset
   106
    \package \"http://isabelle.in.tum.de/~blanchet/spass-3.7.tgz\" and add the \
a4f601daa175 centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents: 36369
diff changeset
   107
    \\"spass-3.7\" directory's full path to \"" ^
a4f601daa175 centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents: 36369
diff changeset
   108
    Path.implode (Path.expand (Path.appends
a4f601daa175 centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents: 36369
diff changeset
   109
        (Path.variable "ISABELLE_HOME_USER" ::
a4f601daa175 centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents: 36369
diff changeset
   110
         map Path.basic ["etc", "components"]))) ^
a4f601daa175 centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents: 36369
diff changeset
   111
    "\" on a line of its own."
36965
67ae217c6b5c identify common SPASS error more clearly
blanchet
parents: 36924
diff changeset
   112
  | string_for_failure MalformedInput =
67ae217c6b5c identify common SPASS error more clearly
blanchet
parents: 36924
diff changeset
   113
    "Internal Sledgehammer error: The ATP problem is malformed. Please report \
67ae217c6b5c identify common SPASS error more clearly
blanchet
parents: 36924
diff changeset
   114
    \this to the Isabelle developers."
36370
a4f601daa175 centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents: 36369
diff changeset
   115
  | string_for_failure MalformedOutput = "Error: The ATP output is malformed."
a4f601daa175 centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents: 36369
diff changeset
   116
  | string_for_failure UnknownError = "Error: An unknown ATP error occurred."
35865
2f8fb5242799 more Sledgehammer refactoring
blanchet
parents: 35826
diff changeset
   117
37623
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
   118
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
   119
(* Clause preparation *)
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
   120
38017
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   121
datatype fol_formula =
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   122
  FOLFormula of {formula_name: string,
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   123
                 kind: kind,
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   124
                 combformula: (name, combterm) formula,
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   125
                 ctypes_sorts: typ list}
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   126
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   127
fun mk_anot phi = AConn (ANot, [phi])
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   128
fun mk_aconn c phi1 phi2 = AConn (c, [phi1, phi2])
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   129
fun mk_ahorn [] phi = phi
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   130
  | mk_ahorn (phi :: phis) psi =
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   131
    AConn (AImplies, [fold (mk_aconn AAnd) phis phi, psi])
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   132
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   133
(* ### FIXME: reintroduce
37623
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
   134
fun make_clause_table xs =
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
   135
  fold (Termtab.update o `(prop_of o snd)) xs Termtab.empty
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
   136
(* Remove existing axiom clauses from the conjecture clauses, as this can
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
   137
   dramatically boost an ATP's performance (for some reason). *)
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
   138
fun subtract_cls ax_clauses =
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
   139
  filter_out (Termtab.defined (make_clause_table ax_clauses) o prop_of)
38017
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   140
*)
37623
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
   141
37995
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37994
diff changeset
   142
fun combformula_for_prop thy =
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37994
diff changeset
   143
  let
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37994
diff changeset
   144
    val do_term = combterm_from_term thy
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37994
diff changeset
   145
    fun do_quant bs q s T t' =
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37994
diff changeset
   146
      do_formula ((s, T) :: bs) t'
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37994
diff changeset
   147
      #>> (fn phi => AQuant (q, [`make_bound_var s], phi))
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37994
diff changeset
   148
    and do_conn bs c t1 t2 =
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37994
diff changeset
   149
      do_formula bs t1 ##>> do_formula bs t2
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37994
diff changeset
   150
      #>> (fn (phi1, phi2) => AConn (c, [phi1, phi2]))
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37994
diff changeset
   151
    and do_formula bs t =
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37994
diff changeset
   152
      case t of
38001
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   153
        @{const Not} $ t1 =>
37995
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37994
diff changeset
   154
        do_formula bs t1 #>> (fn phi => AConn (ANot, [phi]))
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37994
diff changeset
   155
      | Const (@{const_name All}, _) $ Abs (s, T, t') =>
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37994
diff changeset
   156
        do_quant bs AForall s T t'
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37994
diff changeset
   157
      | Const (@{const_name Ex}, _) $ Abs (s, T, t') =>
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37994
diff changeset
   158
        do_quant bs AExists s T t'
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37994
diff changeset
   159
      | @{const "op &"} $ t1 $ t2 => do_conn bs AAnd t1 t2
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37994
diff changeset
   160
      | @{const "op |"} $ t1 $ t2 => do_conn bs AOr t1 t2
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37994
diff changeset
   161
      | @{const "op -->"} $ t1 $ t2 => do_conn bs AImplies t1 t2
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37994
diff changeset
   162
      | Const (@{const_name "op ="}, Type (_, [@{typ bool}, _])) $ t1 $ t2 =>
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37994
diff changeset
   163
        do_conn bs AIff t1 t2
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37994
diff changeset
   164
      | _ => (fn ts => do_term bs (Envir.eta_contract t)
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37994
diff changeset
   165
                       |>> APred ||> union (op =) ts)
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37994
diff changeset
   166
  in do_formula [] end
37623
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
   167
38001
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   168
(* Converts an elim-rule into an equivalent theorem that does not have the
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   169
   predicate variable. Leaves other theorems unchanged. We simply instantiate
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   170
   the conclusion variable to False. (Cf. "transform_elim_term" in
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   171
   "ATP_Systems".) *)
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   172
(* FIXME: test! *)
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   173
fun transform_elim_term t =
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   174
  case Logic.strip_imp_concl t of
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   175
    @{const Trueprop} $ Var (z, @{typ bool}) =>
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   176
    subst_Vars [(z, @{const True})] t
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   177
  | Var (z, @{typ prop}) => subst_Vars [(z, @{prop True})] t
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   178
  | _ => t
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   179
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   180
(* Removes the lambdas from an equation of the form "t = (%x. u)".
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   181
   (Cf. "extensionalize_theorem" in "Clausifier".) *)
38000
c0b9efa8bfca added extensionalization to Sledgehammer, mimicking what the Clausifier used to do
blanchet
parents: 37999
diff changeset
   182
fun extensionalize_term t =
c0b9efa8bfca added extensionalization to Sledgehammer, mimicking what the Clausifier used to do
blanchet
parents: 37999
diff changeset
   183
  let
38001
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   184
    fun aux j (Const (@{const_name "op ="}, Type (_, [Type (_, [_, T']), _]))
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   185
               $ t2 $ Abs (s, var_T, t')) =
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   186
        let val var_t = Var (("x", j), var_T) in
38000
c0b9efa8bfca added extensionalization to Sledgehammer, mimicking what the Clausifier used to do
blanchet
parents: 37999
diff changeset
   187
          Const (@{const_name "op ="}, T' --> T' --> HOLogic.boolT)
c0b9efa8bfca added extensionalization to Sledgehammer, mimicking what the Clausifier used to do
blanchet
parents: 37999
diff changeset
   188
            $ betapply (t2, var_t) $ subst_bound (var_t, t')
c0b9efa8bfca added extensionalization to Sledgehammer, mimicking what the Clausifier used to do
blanchet
parents: 37999
diff changeset
   189
          |> aux (j + 1)
c0b9efa8bfca added extensionalization to Sledgehammer, mimicking what the Clausifier used to do
blanchet
parents: 37999
diff changeset
   190
        end
c0b9efa8bfca added extensionalization to Sledgehammer, mimicking what the Clausifier used to do
blanchet
parents: 37999
diff changeset
   191
      | aux _ t = t
c0b9efa8bfca added extensionalization to Sledgehammer, mimicking what the Clausifier used to do
blanchet
parents: 37999
diff changeset
   192
  in aux (maxidx_of_term t + 1) t end
c0b9efa8bfca added extensionalization to Sledgehammer, mimicking what the Clausifier used to do
blanchet
parents: 37999
diff changeset
   193
38001
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   194
(* FIXME: Guarantee freshness *)
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   195
fun concealed_bound_name j = "Sledgehammer" ^ Int.toString j
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   196
fun conceal_bounds Ts t =
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   197
  subst_bounds (map (Free o apfst concealed_bound_name)
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   198
                    (length Ts - 1 downto 0 ~~ rev Ts), t)
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   199
fun reveal_bounds Ts =
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   200
  subst_atomic (map (fn (j, T) => (Free (concealed_bound_name j, T), Bound j))
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   201
                    (0 upto length Ts - 1 ~~ Ts))
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   202
38005
b6555e9c5de4 prevent schematic variable clash in combinator-introduction code, when invoked from Sledgehammer (another consequence of the CNF -> FOF transition)
blanchet
parents: 38004
diff changeset
   203
fun introduce_combinators_in_term ctxt kind t =
38001
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   204
  let
38005
b6555e9c5de4 prevent schematic variable clash in combinator-introduction code, when invoked from Sledgehammer (another consequence of the CNF -> FOF transition)
blanchet
parents: 38004
diff changeset
   205
    val thy = ProofContext.theory_of ctxt
38001
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   206
    fun aux Ts t =
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   207
      case t of
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   208
        @{const Not} $ t1 => @{const Not} $ aux Ts t1
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   209
      | (t0 as Const (@{const_name All}, _)) $ Abs (s, T, t') =>
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   210
        t0 $ Abs (s, T, aux (T :: Ts) t')
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   211
      | (t0 as Const (@{const_name Ex}, _)) $ Abs (s, T, t') =>
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   212
        t0 $ Abs (s, T, aux (T :: Ts) t')
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   213
      | (t0 as @{const "op &"}) $ t1 $ t2 => t0 $ aux Ts t1 $ aux Ts t2
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   214
      | (t0 as @{const "op |"}) $ t1 $ t2 => t0 $ aux Ts t1 $ aux Ts t2
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   215
      | (t0 as @{const "op -->"}) $ t1 $ t2 => t0 $ aux Ts t1 $ aux Ts t2
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   216
      | (t0 as Const (@{const_name "op ="}, Type (_, [@{typ bool}, _])))
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   217
          $ t1 $ t2 =>
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   218
        t0 $ aux Ts t1 $ aux Ts t2
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   219
      | _ => if not (exists_subterm (fn Abs _ => true | _ => false) t) then
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   220
               t
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   221
             else
38005
b6555e9c5de4 prevent schematic variable clash in combinator-introduction code, when invoked from Sledgehammer (another consequence of the CNF -> FOF transition)
blanchet
parents: 38004
diff changeset
   222
               let
b6555e9c5de4 prevent schematic variable clash in combinator-introduction code, when invoked from Sledgehammer (another consequence of the CNF -> FOF transition)
blanchet
parents: 38004
diff changeset
   223
                 val t = t |> conceal_bounds Ts
b6555e9c5de4 prevent schematic variable clash in combinator-introduction code, when invoked from Sledgehammer (another consequence of the CNF -> FOF transition)
blanchet
parents: 38004
diff changeset
   224
                           |> Envir.eta_contract
b6555e9c5de4 prevent schematic variable clash in combinator-introduction code, when invoked from Sledgehammer (another consequence of the CNF -> FOF transition)
blanchet
parents: 38004
diff changeset
   225
                 val ([t], ctxt') = Variable.import_terms true [t] ctxt
b6555e9c5de4 prevent schematic variable clash in combinator-introduction code, when invoked from Sledgehammer (another consequence of the CNF -> FOF transition)
blanchet
parents: 38004
diff changeset
   226
               in
b6555e9c5de4 prevent schematic variable clash in combinator-introduction code, when invoked from Sledgehammer (another consequence of the CNF -> FOF transition)
blanchet
parents: 38004
diff changeset
   227
                 t |> cterm_of thy
b6555e9c5de4 prevent schematic variable clash in combinator-introduction code, when invoked from Sledgehammer (another consequence of the CNF -> FOF transition)
blanchet
parents: 38004
diff changeset
   228
                   |> Clausifier.introduce_combinators_in_cterm
b6555e9c5de4 prevent schematic variable clash in combinator-introduction code, when invoked from Sledgehammer (another consequence of the CNF -> FOF transition)
blanchet
parents: 38004
diff changeset
   229
                   |> singleton (Variable.export ctxt' ctxt)
b6555e9c5de4 prevent schematic variable clash in combinator-introduction code, when invoked from Sledgehammer (another consequence of the CNF -> FOF transition)
blanchet
parents: 38004
diff changeset
   230
                   |> prop_of |> Logic.dest_equals |> snd
b6555e9c5de4 prevent schematic variable clash in combinator-introduction code, when invoked from Sledgehammer (another consequence of the CNF -> FOF transition)
blanchet
parents: 38004
diff changeset
   231
                   |> reveal_bounds Ts
b6555e9c5de4 prevent schematic variable clash in combinator-introduction code, when invoked from Sledgehammer (another consequence of the CNF -> FOF transition)
blanchet
parents: 38004
diff changeset
   232
               end
38001
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   233
  in t |> not (Meson.is_fol_term thy t) ? aux [] end
38005
b6555e9c5de4 prevent schematic variable clash in combinator-introduction code, when invoked from Sledgehammer (another consequence of the CNF -> FOF transition)
blanchet
parents: 38004
diff changeset
   234
  handle THM _ =>
b6555e9c5de4 prevent schematic variable clash in combinator-introduction code, when invoked from Sledgehammer (another consequence of the CNF -> FOF transition)
blanchet
parents: 38004
diff changeset
   235
         (* A type variable of sort "{}" will make abstraction fail. *)
b6555e9c5de4 prevent schematic variable clash in combinator-introduction code, when invoked from Sledgehammer (another consequence of the CNF -> FOF transition)
blanchet
parents: 38004
diff changeset
   236
         case kind of
b6555e9c5de4 prevent schematic variable clash in combinator-introduction code, when invoked from Sledgehammer (another consequence of the CNF -> FOF transition)
blanchet
parents: 38004
diff changeset
   237
           Axiom => HOLogic.true_const
b6555e9c5de4 prevent schematic variable clash in combinator-introduction code, when invoked from Sledgehammer (another consequence of the CNF -> FOF transition)
blanchet
parents: 38004
diff changeset
   238
         | Conjecture => HOLogic.false_const
38001
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   239
37623
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
   240
(* making axiom and conjecture clauses *)
38005
b6555e9c5de4 prevent schematic variable clash in combinator-introduction code, when invoked from Sledgehammer (another consequence of the CNF -> FOF transition)
blanchet
parents: 38004
diff changeset
   241
fun make_clause ctxt (formula_name, kind, t) =
37623
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
   242
  let
38005
b6555e9c5de4 prevent schematic variable clash in combinator-introduction code, when invoked from Sledgehammer (another consequence of the CNF -> FOF transition)
blanchet
parents: 38004
diff changeset
   243
    val thy = ProofContext.theory_of ctxt
38002
31705eccee23 get rid of obsolete "axiom ID" component, since it's now always 0
blanchet
parents: 38001
diff changeset
   244
    (* ### FIXME: perform other transformations previously done by
31705eccee23 get rid of obsolete "axiom ID" component, since it's now always 0
blanchet
parents: 38001
diff changeset
   245
       "Clausifier.to_nnf", e.g. "HOL.If" *)
38001
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   246
    val t = t |> transform_elim_term
a9b47b85ca24 reintroduced more preprocessing steps to Sledgehammer, adapted to the new FOF setting
blanchet
parents: 38000
diff changeset
   247
              |> Object_Logic.atomize_term thy
38000
c0b9efa8bfca added extensionalization to Sledgehammer, mimicking what the Clausifier used to do
blanchet
parents: 37999
diff changeset
   248
              |> extensionalize_term
38005
b6555e9c5de4 prevent schematic variable clash in combinator-introduction code, when invoked from Sledgehammer (another consequence of the CNF -> FOF transition)
blanchet
parents: 38004
diff changeset
   249
              |> introduce_combinators_in_term ctxt kind
37995
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37994
diff changeset
   250
    val (combformula, ctypes_sorts) = combformula_for_prop thy t []
37623
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
   251
  in
38004
43fdc7c259ea remove obsolete field in record
blanchet
parents: 38003
diff changeset
   252
    FOLFormula {formula_name = formula_name, combformula = combformula,
43fdc7c259ea remove obsolete field in record
blanchet
parents: 38003
diff changeset
   253
                kind = kind, ctypes_sorts = ctypes_sorts}
37623
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
   254
  end
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
   255
38005
b6555e9c5de4 prevent schematic variable clash in combinator-introduction code, when invoked from Sledgehammer (another consequence of the CNF -> FOF transition)
blanchet
parents: 38004
diff changeset
   256
fun make_axiom_clause ctxt (name, th) =
b6555e9c5de4 prevent schematic variable clash in combinator-introduction code, when invoked from Sledgehammer (another consequence of the CNF -> FOF transition)
blanchet
parents: 38004
diff changeset
   257
  (name, make_clause ctxt (name, Axiom, prop_of th))
b6555e9c5de4 prevent schematic variable clash in combinator-introduction code, when invoked from Sledgehammer (another consequence of the CNF -> FOF transition)
blanchet
parents: 38004
diff changeset
   258
fun make_conjecture_clauses ctxt ts =
b6555e9c5de4 prevent schematic variable clash in combinator-introduction code, when invoked from Sledgehammer (another consequence of the CNF -> FOF transition)
blanchet
parents: 38004
diff changeset
   259
  map2 (fn j => fn t => make_clause ctxt (Int.toString j, Conjecture, t))
37999
12a559b5c550 simplify code
blanchet
parents: 37997
diff changeset
   260
       (0 upto length ts - 1) ts
37623
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
   261
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
   262
(** Helper clauses **)
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
   263
37994
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37989
diff changeset
   264
fun count_combterm (CombConst ((s, _), _, _)) =
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37989
diff changeset
   265
    Symtab.map_entry s (Integer.add 1)
37623
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
   266
  | count_combterm (CombVar _) = I
37994
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37989
diff changeset
   267
  | count_combterm (CombApp (t1, t2)) = fold count_combterm [t1, t2]
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37989
diff changeset
   268
fun count_combformula (AQuant (_, _, phi)) = count_combformula phi
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37989
diff changeset
   269
  | count_combformula (AConn (_, phis)) = fold count_combformula phis
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37989
diff changeset
   270
  | count_combformula (APred tm) = count_combterm tm
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37989
diff changeset
   271
fun count_fol_formula (FOLFormula {combformula, ...}) =
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37989
diff changeset
   272
  count_combformula combformula
37623
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
   273
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
   274
val optional_helpers =
38002
31705eccee23 get rid of obsolete "axiom ID" component, since it's now always 0
blanchet
parents: 38001
diff changeset
   275
  [(["c_COMBI", "c_COMBK"], @{thms COMBI_def COMBK_def}),
31705eccee23 get rid of obsolete "axiom ID" component, since it's now always 0
blanchet
parents: 38001
diff changeset
   276
   (["c_COMBB", "c_COMBC"], @{thms COMBB_def COMBC_def}),
31705eccee23 get rid of obsolete "axiom ID" component, since it's now always 0
blanchet
parents: 38001
diff changeset
   277
   (["c_COMBS"], @{thms COMBS_def})]
37623
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
   278
val optional_typed_helpers =
38002
31705eccee23 get rid of obsolete "axiom ID" component, since it's now always 0
blanchet
parents: 38001
diff changeset
   279
  [(["c_True", "c_False"], @{thms True_or_False}),
31705eccee23 get rid of obsolete "axiom ID" component, since it's now always 0
blanchet
parents: 38001
diff changeset
   280
   (["c_If"], @{thms if_True if_False True_or_False})]
37623
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
   281
val mandatory_helpers = @{thms fequal_imp_equal equal_imp_fequal}
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
   282
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
   283
val init_counters =
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
   284
  Symtab.make (maps (maps (map (rpair 0) o fst))
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
   285
                    [optional_helpers, optional_typed_helpers])
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
   286
38005
b6555e9c5de4 prevent schematic variable clash in combinator-introduction code, when invoked from Sledgehammer (another consequence of the CNF -> FOF transition)
blanchet
parents: 38004
diff changeset
   287
fun get_helper_clauses ctxt is_FO full_types conjectures axclauses =
37623
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
   288
  let
37994
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37989
diff changeset
   289
    val ct = fold (fold count_fol_formula) [conjectures, axclauses]
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37989
diff changeset
   290
                  init_counters
37623
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
   291
    fun is_needed c = the (Symtab.lookup ct c) > 0
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
   292
    val cnfs =
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
   293
      (optional_helpers
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
   294
       |> full_types ? append optional_typed_helpers
38002
31705eccee23 get rid of obsolete "axiom ID" component, since it's now always 0
blanchet
parents: 38001
diff changeset
   295
       |> maps (fn (ss, ths) =>
31705eccee23 get rid of obsolete "axiom ID" component, since it's now always 0
blanchet
parents: 38001
diff changeset
   296
                   if exists is_needed ss then map (`Thm.get_name_hint) ths
31705eccee23 get rid of obsolete "axiom ID" component, since it's now always 0
blanchet
parents: 38001
diff changeset
   297
                   else [])) @
31705eccee23 get rid of obsolete "axiom ID" component, since it's now always 0
blanchet
parents: 38001
diff changeset
   298
      (if is_FO then [] else map (`Thm.get_name_hint) mandatory_helpers)
38005
b6555e9c5de4 prevent schematic variable clash in combinator-introduction code, when invoked from Sledgehammer (another consequence of the CNF -> FOF transition)
blanchet
parents: 38004
diff changeset
   299
  in map (snd o make_axiom_clause ctxt) cnfs end
37623
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
   300
38002
31705eccee23 get rid of obsolete "axiom ID" component, since it's now always 0
blanchet
parents: 38001
diff changeset
   301
fun s_not (@{const Not} $ t) = t
31705eccee23 get rid of obsolete "axiom ID" component, since it's now always 0
blanchet
parents: 38001
diff changeset
   302
  | s_not t = @{const Not} $ t
37995
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37994
diff changeset
   303
37623
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
   304
(* prepare for passing to writer,
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
   305
   create additional clauses based on the information from extra_cls *)
38005
b6555e9c5de4 prevent schematic variable clash in combinator-introduction code, when invoked from Sledgehammer (another consequence of the CNF -> FOF transition)
blanchet
parents: 38004
diff changeset
   306
fun prepare_clauses ctxt full_types hyp_ts concl_t axcls extra_cls =
37623
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
   307
  let
38005
b6555e9c5de4 prevent schematic variable clash in combinator-introduction code, when invoked from Sledgehammer (another consequence of the CNF -> FOF transition)
blanchet
parents: 38004
diff changeset
   308
    val thy = ProofContext.theory_of ctxt
37995
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37994
diff changeset
   309
    val goal_t = Logic.list_implies (hyp_ts, concl_t)
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37994
diff changeset
   310
    val is_FO = Meson.is_fol_term thy goal_t
38005
b6555e9c5de4 prevent schematic variable clash in combinator-introduction code, when invoked from Sledgehammer (another consequence of the CNF -> FOF transition)
blanchet
parents: 38004
diff changeset
   311
    val _ = trace_msg (fn _ => Syntax.string_of_term ctxt goal_t)
37995
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37994
diff changeset
   312
    val axtms = map (prop_of o snd) extra_cls
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37994
diff changeset
   313
    val subs = tfree_classes_of_terms [goal_t]
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37994
diff changeset
   314
    val supers = tvar_classes_of_terms axtms
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37994
diff changeset
   315
    val tycons = type_consts_of_terms thy (goal_t :: axtms)
38002
31705eccee23 get rid of obsolete "axiom ID" component, since it's now always 0
blanchet
parents: 38001
diff changeset
   316
    (* TFrees in conjecture clauses; TVars in axiom clauses *)
38005
b6555e9c5de4 prevent schematic variable clash in combinator-introduction code, when invoked from Sledgehammer (another consequence of the CNF -> FOF transition)
blanchet
parents: 38004
diff changeset
   317
    val conjectures =
b6555e9c5de4 prevent schematic variable clash in combinator-introduction code, when invoked from Sledgehammer (another consequence of the CNF -> FOF transition)
blanchet
parents: 38004
diff changeset
   318
      map (s_not o HOLogic.dest_Trueprop) hyp_ts @
b6555e9c5de4 prevent schematic variable clash in combinator-introduction code, when invoked from Sledgehammer (another consequence of the CNF -> FOF transition)
blanchet
parents: 38004
diff changeset
   319
        [HOLogic.dest_Trueprop concl_t]
b6555e9c5de4 prevent schematic variable clash in combinator-introduction code, when invoked from Sledgehammer (another consequence of the CNF -> FOF transition)
blanchet
parents: 38004
diff changeset
   320
      |> make_conjecture_clauses ctxt
b6555e9c5de4 prevent schematic variable clash in combinator-introduction code, when invoked from Sledgehammer (another consequence of the CNF -> FOF transition)
blanchet
parents: 38004
diff changeset
   321
    val extra_clauses = map (snd o make_axiom_clause ctxt) extra_cls
38003
523dc7ad6f9f simplify code
blanchet
parents: 38002
diff changeset
   322
    val (clnames, axiom_clauses) =
38005
b6555e9c5de4 prevent schematic variable clash in combinator-introduction code, when invoked from Sledgehammer (another consequence of the CNF -> FOF transition)
blanchet
parents: 38004
diff changeset
   323
      ListPair.unzip (map (make_axiom_clause ctxt) axcls)
38003
523dc7ad6f9f simplify code
blanchet
parents: 38002
diff changeset
   324
    (* FIXME: Should it read "extra_clauses" or "axiom_clauses" in the
523dc7ad6f9f simplify code
blanchet
parents: 38002
diff changeset
   325
       "get_helper_clauses" call? *)
37623
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
   326
    val helper_clauses =
38005
b6555e9c5de4 prevent schematic variable clash in combinator-introduction code, when invoked from Sledgehammer (another consequence of the CNF -> FOF transition)
blanchet
parents: 38004
diff changeset
   327
      get_helper_clauses ctxt is_FO full_types conjectures extra_clauses
37623
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
   328
    val (supers', arity_clauses) = make_arity_clauses thy tycons supers
37925
1188e6bff48d rename "classrel" to "class_rel"
blanchet
parents: 37923
diff changeset
   329
    val class_rel_clauses = make_class_rel_clauses thy subs supers'
37623
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
   330
  in
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
   331
    (Vector.fromList clnames,
37925
1188e6bff48d rename "classrel" to "class_rel"
blanchet
parents: 37923
diff changeset
   332
      (conjectures, axiom_clauses, extra_clauses, helper_clauses,
1188e6bff48d rename "classrel" to "class_rel"
blanchet
parents: 37923
diff changeset
   333
       class_rel_clauses, arity_clauses))
37623
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
   334
  end
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
   335
38017
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   336
val axiom_prefix = "ax_"
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   337
val conjecture_prefix = "conj_"
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   338
val arity_clause_prefix = "clsarity_"
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   339
val tfrees_name = "tfrees"
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   340
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   341
fun wrap_type ty t = ATerm ((type_wrapper_name, type_wrapper_name), [ty, t])
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   342
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   343
fun fo_term_for_combtyp (CombTVar name) = ATerm (name, [])
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   344
  | fo_term_for_combtyp (CombTFree name) = ATerm (name, [])
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   345
  | fo_term_for_combtyp (CombType (name, tys)) =
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   346
    ATerm (name, map fo_term_for_combtyp tys)
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   347
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   348
fun fo_literal_for_type_literal (TyLitVar (class, name)) =
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   349
    (true, ATerm (class, [ATerm (name, [])]))
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   350
  | fo_literal_for_type_literal (TyLitFree (class, name)) =
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   351
    (true, ATerm (class, [ATerm (name, [])]))
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   352
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   353
fun formula_for_fo_literal (pos, t) = APred t |> not pos ? mk_anot
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   354
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   355
fun fo_term_for_combterm full_types =
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   356
  let
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   357
    fun aux top_level u =
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   358
      let
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   359
        val (head, args) = strip_combterm_comb u
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   360
        val (x, ty_args) =
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   361
          case head of
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   362
            CombConst (name, _, ty_args) =>
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   363
            if fst name = "equal" then
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   364
              (if top_level andalso length args = 2 then name
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   365
               else ("c_fequal", @{const_name fequal}), [])
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   366
            else
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   367
              (name, if full_types then [] else ty_args)
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   368
          | CombVar (name, _) => (name, [])
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   369
          | CombApp _ => raise Fail "impossible \"CombApp\""
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   370
        val t = ATerm (x, map fo_term_for_combtyp ty_args @
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   371
                          map (aux false) args)
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   372
    in
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   373
      if full_types then wrap_type (fo_term_for_combtyp (combtyp_of u)) t else t
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   374
    end
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   375
  in aux true end
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   376
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   377
fun formula_for_combformula full_types =
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   378
  let
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   379
    fun aux (AQuant (q, xs, phi)) = AQuant (q, xs, aux phi)
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   380
      | aux (AConn (c, phis)) = AConn (c, map aux phis)
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   381
      | aux (APred tm) = APred (fo_term_for_combterm full_types tm)
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   382
  in aux end
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   383
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   384
fun formula_for_axiom full_types (FOLFormula {combformula, ctypes_sorts, ...}) =
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   385
  mk_ahorn (map (formula_for_fo_literal o fo_literal_for_type_literal)
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   386
                (type_literals_for_types ctypes_sorts))
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   387
           (formula_for_combformula full_types combformula)
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   388
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   389
fun problem_line_for_axiom full_types
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   390
        (formula as FOLFormula {formula_name, kind, ...}) =
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   391
  Fof (axiom_prefix ^ ascii_of formula_name, kind,
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   392
       formula_for_axiom full_types formula)
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   393
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   394
fun problem_line_for_class_rel_clause
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   395
        (ClassRelClause {axiom_name, subclass, superclass, ...}) =
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   396
  let val ty_arg = ATerm (("T", "T"), []) in
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   397
    Fof (ascii_of axiom_name, Axiom,
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   398
         AConn (AImplies, [APred (ATerm (subclass, [ty_arg])),
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   399
                           APred (ATerm (superclass, [ty_arg]))]))
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   400
  end
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   401
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   402
fun fo_literal_for_arity_literal (TConsLit (c, t, args)) =
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   403
    (true, ATerm (c, [ATerm (t, map (fn arg => ATerm (arg, [])) args)]))
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   404
  | fo_literal_for_arity_literal (TVarLit (c, sort)) =
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   405
    (false, ATerm (c, [ATerm (sort, [])]))
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   406
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   407
fun problem_line_for_arity_clause
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   408
        (ArityClause {axiom_name, conclLit, premLits, ...}) =
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   409
  Fof (arity_clause_prefix ^ ascii_of axiom_name, Axiom,
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   410
       mk_ahorn (map (formula_for_fo_literal o apfst not
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   411
                      o fo_literal_for_arity_literal) premLits)
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   412
                (formula_for_fo_literal
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   413
                     (fo_literal_for_arity_literal conclLit)))
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   414
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   415
fun problem_line_for_conjecture full_types
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   416
        (FOLFormula {formula_name, kind, combformula, ...}) =
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   417
  Fof (conjecture_prefix ^ formula_name, kind,
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   418
       formula_for_combformula full_types combformula)
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   419
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   420
fun free_type_literals_for_conjecture (FOLFormula {ctypes_sorts, ...}) =
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   421
  map fo_literal_for_type_literal (type_literals_for_types ctypes_sorts)
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   422
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   423
fun problem_line_for_free_type lit =
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   424
  Fof (tfrees_name, Conjecture, mk_anot (formula_for_fo_literal lit))
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   425
fun problem_lines_for_free_types conjectures =
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   426
  let
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   427
    val litss = map free_type_literals_for_conjecture conjectures
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   428
    val lits = fold (union (op =)) litss []
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   429
  in map problem_line_for_free_type lits end
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   430
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   431
(** "hBOOL" and "hAPP" **)
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   432
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   433
type const_info = {min_arity: int, max_arity: int, sub_level: bool}
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   434
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   435
fun consider_term top_level (ATerm ((s, _), ts)) =
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   436
  (if is_tptp_variable s then
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   437
     I
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   438
   else
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   439
     let val n = length ts in
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   440
       Symtab.map_default
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   441
           (s, {min_arity = n, max_arity = 0, sub_level = false})
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   442
           (fn {min_arity, max_arity, sub_level} =>
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   443
               {min_arity = Int.min (n, min_arity),
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   444
                max_arity = Int.max (n, max_arity),
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   445
                sub_level = sub_level orelse not top_level})
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   446
     end)
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   447
  #> fold (consider_term (top_level andalso s = type_wrapper_name)) ts
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   448
fun consider_formula (AQuant (_, _, phi)) = consider_formula phi
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   449
  | consider_formula (AConn (_, phis)) = fold consider_formula phis
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   450
  | consider_formula (APred tm) = consider_term true tm
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   451
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   452
fun consider_problem_line (Fof (_, _, phi)) = consider_formula phi
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   453
fun consider_problem problem = fold (fold consider_problem_line o snd) problem
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   454
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   455
fun const_table_for_problem explicit_apply problem =
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   456
  if explicit_apply then NONE
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   457
  else SOME (Symtab.empty |> consider_problem problem)
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   458
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   459
val tc_fun = make_fixed_type_const @{type_name fun}
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   460
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   461
fun min_arity_of thy full_types NONE s =
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   462
    (if s = "equal" orelse s = type_wrapper_name orelse
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   463
        String.isPrefix type_const_prefix s orelse
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   464
        String.isPrefix class_prefix s then
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   465
       16383 (* large number *)
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   466
     else if full_types then
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   467
       0
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   468
     else case strip_prefix_and_undo_ascii const_prefix s of
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   469
       SOME s' => num_type_args thy (invert_const s')
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   470
     | NONE => 0)
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   471
  | min_arity_of _ _ (SOME the_const_tab) s =
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   472
    case Symtab.lookup the_const_tab s of
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   473
      SOME ({min_arity, ...} : const_info) => min_arity
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   474
    | NONE => 0
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   475
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   476
fun full_type_of (ATerm ((s, _), [ty, _])) =
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   477
    if s = type_wrapper_name then ty else raise Fail "expected type wrapper"
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   478
  | full_type_of _ = raise Fail "expected type wrapper"
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   479
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   480
fun list_hAPP_rev _ t1 [] = t1
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   481
  | list_hAPP_rev NONE t1 (t2 :: ts2) =
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   482
    ATerm (`I "hAPP", [list_hAPP_rev NONE t1 ts2, t2])
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   483
  | list_hAPP_rev (SOME ty) t1 (t2 :: ts2) =
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   484
    let val ty' = ATerm (`make_fixed_type_const @{type_name fun},
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   485
                         [full_type_of t2, ty]) in
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   486
      ATerm (`I "hAPP", [wrap_type ty' (list_hAPP_rev (SOME ty') t1 ts2), t2])
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   487
    end
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   488
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   489
fun repair_applications_in_term thy full_types const_tab =
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   490
  let
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   491
    fun aux opt_ty (ATerm (name as (s, _), ts)) =
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   492
      if s = type_wrapper_name then
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   493
        case ts of
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   494
          [t1, t2] => ATerm (name, [aux NONE t1, aux (SOME t1) t2])
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   495
        | _ => raise Fail "malformed type wrapper"
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   496
      else
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   497
        let
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   498
          val ts = map (aux NONE) ts
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   499
          val (ts1, ts2) = chop (min_arity_of thy full_types const_tab s) ts
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   500
        in list_hAPP_rev opt_ty (ATerm (name, ts1)) (rev ts2) end
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   501
  in aux NONE end
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   502
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   503
fun boolify t = ATerm (`I "hBOOL", [t])
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   504
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   505
(* True if the constant ever appears outside of the top-level position in
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   506
   literals, or if it appears with different arities (e.g., because of different
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   507
   type instantiations). If false, the constant always receives all of its
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   508
   arguments and is used as a predicate. *)
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   509
fun is_predicate NONE s =
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   510
    s = "equal" orelse String.isPrefix type_const_prefix s orelse
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   511
    String.isPrefix class_prefix s
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   512
  | is_predicate (SOME the_const_tab) s =
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   513
    case Symtab.lookup the_const_tab s of
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   514
      SOME {min_arity, max_arity, sub_level} =>
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   515
      not sub_level andalso min_arity = max_arity
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   516
    | NONE => false
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   517
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   518
fun repair_predicates_in_term const_tab (t as ATerm ((s, _), ts)) =
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   519
  if s = type_wrapper_name then
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   520
    case ts of
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   521
      [_, t' as ATerm ((s', _), _)] =>
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   522
      if is_predicate const_tab s' then t' else boolify t
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   523
    | _ => raise Fail "malformed type wrapper"
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   524
  else
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   525
    t |> not (is_predicate const_tab s) ? boolify
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   526
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   527
fun close_universally phi =
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   528
  let
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   529
    fun term_vars bounds (ATerm (name as (s, _), tms)) =
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   530
        (is_tptp_variable s andalso not (member (op =) bounds name))
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   531
          ? insert (op =) name
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   532
        #> fold (term_vars bounds) tms
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   533
    fun formula_vars bounds (AQuant (q, xs, phi)) =
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   534
        formula_vars (xs @ bounds) phi
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   535
      | formula_vars bounds (AConn (_, phis)) = fold (formula_vars bounds) phis
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   536
      | formula_vars bounds (APred tm) = term_vars bounds tm
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   537
  in
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   538
    case formula_vars [] phi [] of [] => phi | xs => AQuant (AForall, xs, phi)
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   539
  end
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   540
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   541
fun repair_formula thy explicit_forall full_types const_tab =
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   542
  let
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   543
    fun aux (AQuant (q, xs, phi)) = AQuant (q, xs, aux phi)
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   544
      | aux (AConn (c, phis)) = AConn (c, map aux phis)
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   545
      | aux (APred tm) =
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   546
        APred (tm |> repair_applications_in_term thy full_types const_tab
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   547
                  |> repair_predicates_in_term const_tab)
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   548
  in aux #> explicit_forall ? close_universally end
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   549
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   550
fun repair_problem_line thy explicit_forall full_types const_tab
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   551
                        (Fof (ident, kind, phi)) =
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   552
  Fof (ident, kind, repair_formula thy explicit_forall full_types const_tab phi)
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   553
fun repair_problem_with_const_table thy =
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   554
  map o apsnd o map ooo repair_problem_line thy
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   555
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   556
fun repair_problem thy explicit_forall full_types explicit_apply problem =
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   557
  repair_problem_with_const_table thy explicit_forall full_types
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   558
      (const_table_for_problem explicit_apply problem) problem
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   559
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   560
fun write_tptp_file thy readable_names explicit_forall full_types explicit_apply
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   561
                    file (conjectures, axiom_clauses, extra_clauses,
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   562
                          helper_clauses, class_rel_clauses, arity_clauses) =
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   563
  let
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   564
    val axiom_lines = map (problem_line_for_axiom full_types) axiom_clauses
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   565
    val class_rel_lines =
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   566
      map problem_line_for_class_rel_clause class_rel_clauses
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   567
    val arity_lines = map problem_line_for_arity_clause arity_clauses
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   568
    val helper_lines = map (problem_line_for_axiom full_types) helper_clauses
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   569
    val conjecture_lines =
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   570
      map (problem_line_for_conjecture full_types) conjectures
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   571
    val tfree_lines = problem_lines_for_free_types conjectures
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   572
    (* Reordering these might or might not confuse the proof reconstruction
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   573
       code or the SPASS Flotter hack. *)
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   574
    val problem =
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   575
      [("Relevant facts", axiom_lines),
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   576
       ("Class relationships", class_rel_lines),
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   577
       ("Arity declarations", arity_lines),
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   578
       ("Helper facts", helper_lines),
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   579
       ("Conjectures", conjecture_lines),
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   580
       ("Type variables", tfree_lines)]
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   581
      |> repair_problem thy explicit_forall full_types explicit_apply
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   582
    val (problem, pool) = nice_tptp_problem readable_names problem
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   583
    val conjecture_offset =
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   584
      length axiom_lines + length class_rel_lines + length arity_lines
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   585
      + length helper_lines
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   586
    val _ = File.write_list file (strings_for_tptp_problem problem)
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   587
  in
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   588
    (case pool of SOME the_pool => snd the_pool | NONE => Symtab.empty,
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   589
     conjecture_offset)
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   590
  end
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38015
diff changeset
   591
37962
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37926
diff changeset
   592
fun extract_clause_sequence output =
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37926
diff changeset
   593
  let
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37926
diff changeset
   594
    val tokens_of = String.tokens (not o Char.isAlphaNum)
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37926
diff changeset
   595
    fun extract_num ("clause" :: (ss as _ :: _)) =
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37926
diff changeset
   596
    Int.fromString (List.last ss)
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37926
diff changeset
   597
      | extract_num _ = NONE
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37926
diff changeset
   598
  in output |> split_lines |> map_filter (extract_num o tokens_of) end
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37926
diff changeset
   599
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37926
diff changeset
   600
val set_ClauseFormulaRelationN = "set_ClauseFormulaRelation"
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37926
diff changeset
   601
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37926
diff changeset
   602
val parse_clause_formula_pair =
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37926
diff changeset
   603
  $$ "(" |-- scan_integer --| $$ "," -- Symbol.scan_id --| $$ ")"
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37926
diff changeset
   604
  --| Scan.option ($$ ",")
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37926
diff changeset
   605
val parse_clause_formula_relation =
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37926
diff changeset
   606
  Scan.this_string set_ClauseFormulaRelationN |-- $$ "("
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37926
diff changeset
   607
  |-- Scan.repeat parse_clause_formula_pair
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37926
diff changeset
   608
val extract_clause_formula_relation =
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37926
diff changeset
   609
  Substring.full
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37926
diff changeset
   610
  #> Substring.position set_ClauseFormulaRelationN
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37926
diff changeset
   611
  #> snd #> Substring.string #> strip_spaces #> explode
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37926
diff changeset
   612
  #> parse_clause_formula_relation #> fst
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37926
diff changeset
   613
37989
ca3041b0f445 reorder SPASS conjectures correctly, based on Flotter output
blanchet
parents: 37962
diff changeset
   614
fun repair_conjecture_shape_and_theorem_names output conjecture_shape
ca3041b0f445 reorder SPASS conjectures correctly, based on Flotter output
blanchet
parents: 37962
diff changeset
   615
                                              thm_names =
37962
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37926
diff changeset
   616
  if String.isSubstring set_ClauseFormulaRelationN output then
38009
34e1ac9cb71d shrink the "max_new_relevant_facts_per_iter" fudge factors, now that we count formulas and not clauses
blanchet
parents: 38005
diff changeset
   617
    (* This is a hack required for keeping track of axioms after they have been
34e1ac9cb71d shrink the "max_new_relevant_facts_per_iter" fudge factors, now that we count formulas and not clauses
blanchet
parents: 38005
diff changeset
   618
       clausified by SPASS's Flotter tool. The "SPASS_TPTP" script is also part
34e1ac9cb71d shrink the "max_new_relevant_facts_per_iter" fudge factors, now that we count formulas and not clauses
blanchet
parents: 38005
diff changeset
   619
       of this hack. *)
37962
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37926
diff changeset
   620
    let
37996
11c076ea92e9 simplify "conjecture_shape" business, as a result of using FOF instead of CNF
blanchet
parents: 37995
diff changeset
   621
      val j0 = hd conjecture_shape
37962
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37926
diff changeset
   622
      val seq = extract_clause_sequence output
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37926
diff changeset
   623
      val name_map = extract_clause_formula_relation output
37989
ca3041b0f445 reorder SPASS conjectures correctly, based on Flotter output
blanchet
parents: 37962
diff changeset
   624
      fun renumber_conjecture j =
ca3041b0f445 reorder SPASS conjectures correctly, based on Flotter output
blanchet
parents: 37962
diff changeset
   625
        AList.find (op =) name_map (conjecture_prefix ^ Int.toString (j - j0))
ca3041b0f445 reorder SPASS conjectures correctly, based on Flotter output
blanchet
parents: 37962
diff changeset
   626
        |> the_single
ca3041b0f445 reorder SPASS conjectures correctly, based on Flotter output
blanchet
parents: 37962
diff changeset
   627
        |> (fn s => find_index (curry (op =) s) seq + 1)
37962
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37926
diff changeset
   628
    in
37996
11c076ea92e9 simplify "conjecture_shape" business, as a result of using FOF instead of CNF
blanchet
parents: 37995
diff changeset
   629
      (conjecture_shape |> map renumber_conjecture,
37989
ca3041b0f445 reorder SPASS conjectures correctly, based on Flotter output
blanchet
parents: 37962
diff changeset
   630
       seq |> map (the o AList.lookup (op =) name_map)
ca3041b0f445 reorder SPASS conjectures correctly, based on Flotter output
blanchet
parents: 37962
diff changeset
   631
           |> map (fn s => case try (unprefix axiom_prefix) s of
ca3041b0f445 reorder SPASS conjectures correctly, based on Flotter output
blanchet
parents: 37962
diff changeset
   632
                             SOME s' => undo_ascii_of s'
ca3041b0f445 reorder SPASS conjectures correctly, based on Flotter output
blanchet
parents: 37962
diff changeset
   633
                           | NONE => "")
ca3041b0f445 reorder SPASS conjectures correctly, based on Flotter output
blanchet
parents: 37962
diff changeset
   634
           |> Vector.fromList)
37962
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37926
diff changeset
   635
    end
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37926
diff changeset
   636
  else
37989
ca3041b0f445 reorder SPASS conjectures correctly, based on Flotter output
blanchet
parents: 37962
diff changeset
   637
    (conjecture_shape, thm_names)
37962
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37926
diff changeset
   638
37623
295f3a9b44b6 move functions not needed by Metis out of "Metis_Clauses"
blanchet
parents: 37621
diff changeset
   639
37499
5ff37037fbec merge "generic_prover" and "generic_tptp_prover"
blanchet
parents: 37498
diff changeset
   640
(* generic TPTP-based provers *)
5ff37037fbec merge "generic_prover" and "generic_tptp_prover"
blanchet
parents: 37498
diff changeset
   641
5ff37037fbec merge "generic_prover" and "generic_tptp_prover"
blanchet
parents: 37498
diff changeset
   642
fun generic_tptp_prover
5ff37037fbec merge "generic_prover" and "generic_tptp_prover"
blanchet
parents: 37498
diff changeset
   643
        (name, {home_var, executable, arguments, proof_delims, known_failures,
38009
34e1ac9cb71d shrink the "max_new_relevant_facts_per_iter" fudge factors, now that we count formulas and not clauses
blanchet
parents: 38005
diff changeset
   644
                max_new_relevant_facts_per_iter, prefers_theory_relevant,
34e1ac9cb71d shrink the "max_new_relevant_facts_per_iter" fudge factors, now that we count formulas and not clauses
blanchet
parents: 38005
diff changeset
   645
                explicit_forall})
37580
c2c1caff5dea got rid of "respect_no_atp" option, which even I don't use
blanchet
parents: 37578
diff changeset
   646
        ({debug, overlord, full_types, explicit_apply, relevance_threshold,
c2c1caff5dea got rid of "respect_no_atp" option, which even I don't use
blanchet
parents: 37578
diff changeset
   647
          relevance_convergence, theory_relevant, defs_relevant, isar_proof,
c2c1caff5dea got rid of "respect_no_atp" option, which even I don't use
blanchet
parents: 37578
diff changeset
   648
          isar_shrink_factor, ...} : params)
37499
5ff37037fbec merge "generic_prover" and "generic_tptp_prover"
blanchet
parents: 37498
diff changeset
   649
        minimize_command timeout
35969
c9565298df9e added support for Sledgehammer parameters;
blanchet
parents: 35869
diff changeset
   650
        ({subgoal, goal, relevance_override, axiom_clauses, filtered_clauses}
c9565298df9e added support for Sledgehammer parameters;
blanchet
parents: 35869
diff changeset
   651
         : problem) =
28596
fcd463a6b6de tuned interfaces -- plain prover function, without thread;
wenzelm
parents: 28592
diff changeset
   652
  let
31750
f28b7365fabf restructured external_prover
immler@in.tum.de
parents: 31411
diff changeset
   653
    (* get clauses and prepare them for writing *)
37498
b426cbdb5a23 removed Sledgehammer's support for the DFG syntax;
blanchet
parents: 37480
diff changeset
   654
    val (ctxt, (_, th)) = goal;
38005
b6555e9c5de4 prevent schematic variable clash in combinator-introduction code, when invoked from Sledgehammer (another consequence of the CNF -> FOF transition)
blanchet
parents: 38004
diff changeset
   655
    val thy = ProofContext.theory_of ctxt
37995
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37994
diff changeset
   656
    (* ### FIXME: (1) preprocessing for "if" etc. *)
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37994
diff changeset
   657
    val (params, hyp_ts, concl_t) = strip_subgoal th subgoal
31752
19a5f1c8a844 use results of relevance-filter to determine additional clauses;
immler@in.tum.de
parents: 31751
diff changeset
   658
    val the_filtered_clauses =
37621
3e78dbf7a382 adapt call
blanchet
parents: 37580
diff changeset
   659
      case filtered_clauses of
3e78dbf7a382 adapt call
blanchet
parents: 37580
diff changeset
   660
        SOME fcls => fcls
3e78dbf7a382 adapt call
blanchet
parents: 37580
diff changeset
   661
      | NONE => relevant_facts full_types relevance_threshold
38009
34e1ac9cb71d shrink the "max_new_relevant_facts_per_iter" fudge factors, now that we count formulas and not clauses
blanchet
parents: 38005
diff changeset
   662
                    relevance_convergence defs_relevant
34e1ac9cb71d shrink the "max_new_relevant_facts_per_iter" fudge factors, now that we count formulas and not clauses
blanchet
parents: 38005
diff changeset
   663
                    max_new_relevant_facts_per_iter
37499
5ff37037fbec merge "generic_prover" and "generic_tptp_prover"
blanchet
parents: 37498
diff changeset
   664
                    (the_default prefers_theory_relevant theory_relevant)
37995
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37994
diff changeset
   665
                    relevance_override goal hyp_ts concl_t
37506
32a1ee39c49b missing "Unsynchronized" + make exception take a unit
blanchet
parents: 37499
diff changeset
   666
    val the_axiom_clauses = axiom_clauses |> the_default the_filtered_clauses
35969
c9565298df9e added support for Sledgehammer parameters;
blanchet
parents: 35869
diff changeset
   667
    val (internal_thm_names, clauses) =
38005
b6555e9c5de4 prevent schematic variable clash in combinator-introduction code, when invoked from Sledgehammer (another consequence of the CNF -> FOF transition)
blanchet
parents: 38004
diff changeset
   668
      prepare_clauses ctxt full_types hyp_ts concl_t the_axiom_clauses
b6555e9c5de4 prevent schematic variable clash in combinator-introduction code, when invoked from Sledgehammer (another consequence of the CNF -> FOF transition)
blanchet
parents: 38004
diff changeset
   669
                      the_filtered_clauses
31750
f28b7365fabf restructured external_prover
immler@in.tum.de
parents: 31411
diff changeset
   670
32864
a226f29d4bdc re-organized signature of AtpWrapper structure: records instead of unnamed parameters and return values,
boehmes
parents: 32740
diff changeset
   671
    (* path to unique problem file *)
36376
e83d52a52449 renamed module "ATP_Wrapper" to "ATP_Systems"
blanchet
parents: 36371
diff changeset
   672
    val the_dest_dir = if overlord then getenv "ISABELLE_HOME_USER"
e83d52a52449 renamed module "ATP_Wrapper" to "ATP_Systems"
blanchet
parents: 36371
diff changeset
   673
                       else Config.get ctxt dest_dir;
e83d52a52449 renamed module "ATP_Wrapper" to "ATP_Systems"
blanchet
parents: 36371
diff changeset
   674
    val the_problem_prefix = Config.get ctxt problem_prefix;
32864
a226f29d4bdc re-organized signature of AtpWrapper structure: records instead of unnamed parameters and return values,
boehmes
parents: 32740
diff changeset
   675
    fun prob_pathname nr =
36143
6490319b1703 added "overlord" option (to get easy access to output files for debugging) + systematically use "raw_goal" rather than an inconsistent mixture
blanchet
parents: 36142
diff changeset
   676
      let
6490319b1703 added "overlord" option (to get easy access to output files for debugging) + systematically use "raw_goal" rather than an inconsistent mixture
blanchet
parents: 36142
diff changeset
   677
        val probfile =
36568
d495d2e1f0a6 in "overlord" mode: ignore problem prefix specified in the .thy file
blanchet
parents: 36552
diff changeset
   678
          Path.basic ((if overlord then "prob_" ^ name
d495d2e1f0a6 in "overlord" mode: ignore problem prefix specified in the .thy file
blanchet
parents: 36552
diff changeset
   679
                       else the_problem_prefix ^ serial_string ())
36143
6490319b1703 added "overlord" option (to get easy access to output files for debugging) + systematically use "raw_goal" rather than an inconsistent mixture
blanchet
parents: 36142
diff changeset
   680
                      ^ "_" ^ string_of_int nr)
32942
b6711ec9de26 misc tuning and recovery of Isabelle coding style;
wenzelm
parents: 32941
diff changeset
   681
      in
36376
e83d52a52449 renamed module "ATP_Wrapper" to "ATP_Systems"
blanchet
parents: 36371
diff changeset
   682
        if the_dest_dir = "" then File.tmp_path probfile
e83d52a52449 renamed module "ATP_Wrapper" to "ATP_Systems"
blanchet
parents: 36371
diff changeset
   683
        else if File.exists (Path.explode the_dest_dir)
e83d52a52449 renamed module "ATP_Wrapper" to "ATP_Systems"
blanchet
parents: 36371
diff changeset
   684
        then Path.append (Path.explode the_dest_dir) probfile
e83d52a52449 renamed module "ATP_Wrapper" to "ATP_Systems"
blanchet
parents: 36371
diff changeset
   685
        else error ("No such directory: " ^ the_dest_dir ^ ".")
32942
b6711ec9de26 misc tuning and recovery of Isabelle coding style;
wenzelm
parents: 32941
diff changeset
   686
      end;
32864
a226f29d4bdc re-organized signature of AtpWrapper structure: records instead of unnamed parameters and return values,
boehmes
parents: 32740
diff changeset
   687
36917
8674cdb0b8cc query _HOME environment variables at run-time, not at build-time
blanchet
parents: 36910
diff changeset
   688
    val home = getenv home_var
36289
f75b6a3e1450 set "atps" reference's default value to "(remote_)e (remote_)spass (remote_)vampire", based on what is installed
blanchet
parents: 36287
diff changeset
   689
    val command = Path.explode (home ^ "/" ^ executable)
31750
f28b7365fabf restructured external_prover
immler@in.tum.de
parents: 31411
diff changeset
   690
    (* write out problem file and call prover *)
37514
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
   691
    fun command_line complete probfile =
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
   692
      let
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
   693
        val core = File.shell_path command ^ " " ^ arguments complete timeout ^
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
   694
                   " " ^ File.shell_path probfile
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
   695
      in
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
   696
        (if Config.get ctxt measure_runtime then
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
   697
           "TIMEFORMAT='%3U'; { time " ^ core ^ " ; }"
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
   698
         else
37962
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37926
diff changeset
   699
           "exec " ^ core) ^ " 2>&1"
37514
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
   700
      end
32510
1b56f8b1e5cc added runtime information to sledgehammer
boehmes
parents: 32458
diff changeset
   701
    fun split_time s =
1b56f8b1e5cc added runtime information to sledgehammer
boehmes
parents: 32458
diff changeset
   702
      let
32942
b6711ec9de26 misc tuning and recovery of Isabelle coding style;
wenzelm
parents: 32941
diff changeset
   703
        val split = String.tokens (fn c => str c = "\n");
36369
d2cd0d04b8e6 handle ATP proof delimiters in a cleaner, more extensible fashion
blanchet
parents: 36289
diff changeset
   704
        val (output, t) = s |> split |> split_last |> apfst cat_lines;
32942
b6711ec9de26 misc tuning and recovery of Isabelle coding style;
wenzelm
parents: 32941
diff changeset
   705
        fun as_num f = f >> (fst o read_int);
b6711ec9de26 misc tuning and recovery of Isabelle coding style;
wenzelm
parents: 32941
diff changeset
   706
        val num = as_num (Scan.many1 Symbol.is_ascii_digit);
b6711ec9de26 misc tuning and recovery of Isabelle coding style;
wenzelm
parents: 32941
diff changeset
   707
        val digit = Scan.one Symbol.is_ascii_digit;
b6711ec9de26 misc tuning and recovery of Isabelle coding style;
wenzelm
parents: 32941
diff changeset
   708
        val num3 = as_num (digit ::: digit ::: (digit >> single));
b6711ec9de26 misc tuning and recovery of Isabelle coding style;
wenzelm
parents: 32941
diff changeset
   709
        val time = num --| Scan.$$ "." -- num3 >> (fn (a, b) => a * 1000 + b);
b6711ec9de26 misc tuning and recovery of Isabelle coding style;
wenzelm
parents: 32941
diff changeset
   710
        val as_time = the_default 0 o Scan.read Symbol.stopper time o explode;
36369
d2cd0d04b8e6 handle ATP proof delimiters in a cleaner, more extensible fashion
blanchet
parents: 36289
diff changeset
   711
      in (output, as_time t) end;
32458
de6834b20e9e sledgehammer's temporary files are removed properly (even in case of an exception occurs)
boehmes
parents: 32451
diff changeset
   712
    fun run_on probfile =
37962
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37926
diff changeset
   713
      if home = "" then
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37926
diff changeset
   714
        error ("The environment variable " ^ quote home_var ^ " is not set.")
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37926
diff changeset
   715
      else if File.exists command then
37514
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
   716
        let
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
   717
          fun do_run complete =
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
   718
            let
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
   719
              val command = command_line complete probfile
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
   720
              val ((output, msecs), res_code) =
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
   721
                bash_output command
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
   722
                |>> (if overlord then
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
   723
                       prefix ("% " ^ command ^ "\n% " ^ timestamp () ^ "\n")
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
   724
                     else
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
   725
                       I)
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
   726
                |>> (if Config.get ctxt measure_runtime then split_time
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
   727
                     else rpair 0)
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
   728
              val (proof, outcome) =
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
   729
                extract_proof_and_outcome complete res_code proof_delims
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
   730
                                          known_failures output
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
   731
            in (output, msecs, proof, outcome) end
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37628
diff changeset
   732
          val readable_names = debug andalso overlord
37514
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
   733
          val (pool, conjecture_offset) =
37994
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37989
diff changeset
   734
            write_tptp_file thy readable_names explicit_forall full_types
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37989
diff changeset
   735
                            explicit_apply probfile clauses
37995
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37994
diff changeset
   736
          val conjecture_shape =
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37994
diff changeset
   737
            conjecture_offset + 1 upto conjecture_offset + length hyp_ts + 1
37514
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
   738
          val result =
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
   739
            do_run false
37550
fc2f979b9a08 split SPASS time slot between SOS and non-SOS, in case SOS times out
blanchet
parents: 37514
diff changeset
   740
            |> (fn (_, msecs0, _, SOME _) =>
37514
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
   741
                   do_run true
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
   742
                   |> (fn (output, msecs, proof, outcome) =>
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
   743
                          (output, msecs0 + msecs, proof, outcome))
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
   744
                 | result => result)
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
   745
        in ((pool, conjecture_shape), result) end
36382
b90fc0d75bca cosmetics
blanchet
parents: 36377
diff changeset
   746
      else
b90fc0d75bca cosmetics
blanchet
parents: 36377
diff changeset
   747
        error ("Bad executable: " ^ Path.implode command ^ ".");
28592
824f8390aaa2 renamed AtpThread to AtpWrapper;
wenzelm
parents:
diff changeset
   748
36167
c1a35be8e476 make Sledgehammer's output more debugging friendly
blanchet
parents: 36143
diff changeset
   749
    (* If the problem file has not been exported, remove it; otherwise, export
c1a35be8e476 make Sledgehammer's output more debugging friendly
blanchet
parents: 36143
diff changeset
   750
       the proof file too. *)
36376
e83d52a52449 renamed module "ATP_Wrapper" to "ATP_Systems"
blanchet
parents: 36371
diff changeset
   751
    fun cleanup probfile =
e83d52a52449 renamed module "ATP_Wrapper" to "ATP_Systems"
blanchet
parents: 36371
diff changeset
   752
      if the_dest_dir = "" then try File.rm probfile else NONE
37514
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
   753
    fun export probfile (_, (output, _, _, _)) =
36376
e83d52a52449 renamed module "ATP_Wrapper" to "ATP_Systems"
blanchet
parents: 36371
diff changeset
   754
      if the_dest_dir = "" then
36187
4deef08608ee added timestamp to proof
blanchet
parents: 36169
diff changeset
   755
        ()
4deef08608ee added timestamp to proof
blanchet
parents: 36169
diff changeset
   756
      else
37514
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
   757
        File.write (Path.explode (Path.implode probfile ^ "_proof")) output
32257
bad5a99c16d8 neg_conjecture_clauses, neg_clausify_tac: proper context, eliminated METAHYPS;
wenzelm
parents: 32091
diff changeset
   758
37514
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
   759
    val ((pool, conjecture_shape), (output, msecs, proof, outcome)) =
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
   760
      with_path cleanup export run_on (prob_pathname subgoal)
37989
ca3041b0f445 reorder SPASS conjectures correctly, based on Flotter output
blanchet
parents: 37962
diff changeset
   761
    val (conjecture_shape, internal_thm_names) =
ca3041b0f445 reorder SPASS conjectures correctly, based on Flotter output
blanchet
parents: 37962
diff changeset
   762
      repair_conjecture_shape_and_theorem_names output conjecture_shape
ca3041b0f445 reorder SPASS conjectures correctly, based on Flotter output
blanchet
parents: 37962
diff changeset
   763
                                                internal_thm_names
37514
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
   764
38015
b30c3c2e1030 implemented "sublinear" minimization algorithm
blanchet
parents: 38009
diff changeset
   765
    val (message, used_thm_names) =
36370
a4f601daa175 centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents: 36369
diff changeset
   766
      case outcome of
36400
c5bae529f967 rename options and keep track of conjecture shape (to facilitate proof reconstruction)
blanchet
parents: 36393
diff changeset
   767
        NONE =>
c5bae529f967 rename options and keep track of conjecture shape (to facilitate proof reconstruction)
blanchet
parents: 36393
diff changeset
   768
        proof_text isar_proof
37480
d5a85d35ef62 thread "full_types"
blanchet
parents: 37415
diff changeset
   769
            (pool, debug, isar_shrink_factor, ctxt, conjecture_shape)
d5a85d35ef62 thread "full_types"
blanchet
parents: 37415
diff changeset
   770
            (full_types, minimize_command, proof, internal_thm_names, th,
d5a85d35ef62 thread "full_types"
blanchet
parents: 37415
diff changeset
   771
             subgoal)
36370
a4f601daa175 centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents: 36369
diff changeset
   772
      | SOME failure => (string_for_failure failure ^ "\n", [])
32864
a226f29d4bdc re-organized signature of AtpWrapper structure: records instead of unnamed parameters and return values,
boehmes
parents: 32740
diff changeset
   773
  in
36393
be73a2b2443b support readable names even when Isar proof reconstruction is enabled -- useful for debugging
blanchet
parents: 36382
diff changeset
   774
    {outcome = outcome, message = message, pool = pool,
38015
b30c3c2e1030 implemented "sublinear" minimization algorithm
blanchet
parents: 38009
diff changeset
   775
     used_thm_names = used_thm_names, atp_run_time_in_msecs = msecs,
37514
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
   776
     output = output, proof = proof, internal_thm_names = internal_thm_names,
36400
c5bae529f967 rename options and keep track of conjecture shape (to facilitate proof reconstruction)
blanchet
parents: 36393
diff changeset
   777
     conjecture_shape = conjecture_shape,
35969
c9565298df9e added support for Sledgehammer parameters;
blanchet
parents: 35869
diff changeset
   778
     filtered_clauses = the_filtered_clauses}
37499
5ff37037fbec merge "generic_prover" and "generic_tptp_prover"
blanchet
parents: 37498
diff changeset
   779
  end
28596
fcd463a6b6de tuned interfaces -- plain prover function, without thread;
wenzelm
parents: 28592
diff changeset
   780
35969
c9565298df9e added support for Sledgehammer parameters;
blanchet
parents: 35869
diff changeset
   781
fun tptp_prover name p = (name, generic_tptp_prover (name, p));
28596
fcd463a6b6de tuned interfaces -- plain prover function, without thread;
wenzelm
parents: 28592
diff changeset
   782
36382
b90fc0d75bca cosmetics
blanchet
parents: 36377
diff changeset
   783
fun to_generous_secs time = (Time.toMilliseconds time + 999) div 1000
36142
f5e15e9aae10 make Sledgehammer "minimize" output less confusing + round up (not down) time limits to nearest second
blanchet
parents: 36064
diff changeset
   784
28596
fcd463a6b6de tuned interfaces -- plain prover function, without thread;
wenzelm
parents: 28592
diff changeset
   785
(* E prover *)
fcd463a6b6de tuned interfaces -- plain prover function, without thread;
wenzelm
parents: 28592
diff changeset
   786
36369
d2cd0d04b8e6 handle ATP proof delimiters in a cleaner, more extensible fashion
blanchet
parents: 36289
diff changeset
   787
val tstp_proof_delims =
d2cd0d04b8e6 handle ATP proof delimiters in a cleaner, more extensible fashion
blanchet
parents: 36289
diff changeset
   788
  ("# SZS output start CNFRefutation.", "# SZS output end CNFRefutation")
d2cd0d04b8e6 handle ATP proof delimiters in a cleaner, more extensible fashion
blanchet
parents: 36289
diff changeset
   789
35969
c9565298df9e added support for Sledgehammer parameters;
blanchet
parents: 35869
diff changeset
   790
val e_config : prover_config =
36917
8674cdb0b8cc query _HOME environment variables at run-time, not at build-time
blanchet
parents: 36910
diff changeset
   791
  {home_var = "E_HOME",
36289
f75b6a3e1450 set "atps" reference's default value to "(remote_)e (remote_)spass (remote_)vampire", based on what is installed
blanchet
parents: 36287
diff changeset
   792
   executable = "eproof",
37514
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
   793
   arguments = fn _ => fn timeout =>
36382
b90fc0d75bca cosmetics
blanchet
parents: 36377
diff changeset
   794
     "--tstp-in --tstp-out -l5 -xAutoDev -tAutoDev --silent --cpu-limit=" ^
b90fc0d75bca cosmetics
blanchet
parents: 36377
diff changeset
   795
     string_of_int (to_generous_secs timeout),
36369
d2cd0d04b8e6 handle ATP proof delimiters in a cleaner, more extensible fashion
blanchet
parents: 36289
diff changeset
   796
   proof_delims = [tstp_proof_delims],
36265
41c9e755e552 distinguish between the different ATP errors in the user interface;
blanchet
parents: 36264
diff changeset
   797
   known_failures =
37995
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37994
diff changeset
   798
     [(Unprovable, "SZS status: CounterSatisfiable"),
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37994
diff changeset
   799
      (Unprovable, "SZS status CounterSatisfiable"),
36370
a4f601daa175 centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents: 36369
diff changeset
   800
      (TimedOut, "Failure: Resource limit exceeded (time)"),
a4f601daa175 centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents: 36369
diff changeset
   801
      (TimedOut, "time limit exceeded"),
a4f601daa175 centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents: 36369
diff changeset
   802
      (OutOfResources,
a4f601daa175 centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents: 36369
diff changeset
   803
       "# Cannot determine problem status within resource limit"),
a4f601daa175 centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents: 36369
diff changeset
   804
      (OutOfResources, "SZS status: ResourceOut"),
a4f601daa175 centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents: 36369
diff changeset
   805
      (OutOfResources, "SZS status ResourceOut")],
38009
34e1ac9cb71d shrink the "max_new_relevant_facts_per_iter" fudge factors, now that we count formulas and not clauses
blanchet
parents: 38005
diff changeset
   806
   max_new_relevant_facts_per_iter = 80 (* FIXME *),
37994
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37989
diff changeset
   807
   prefers_theory_relevant = false,
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37989
diff changeset
   808
   explicit_forall = false}
35969
c9565298df9e added support for Sledgehammer parameters;
blanchet
parents: 35869
diff changeset
   809
val e = tptp_prover "e" e_config
28596
fcd463a6b6de tuned interfaces -- plain prover function, without thread;
wenzelm
parents: 28592
diff changeset
   810
fcd463a6b6de tuned interfaces -- plain prover function, without thread;
wenzelm
parents: 28592
diff changeset
   811
36219
16670b4f0baa set SPASS option on the command-line, so that it doesn't vanish when moving to TPTP format
blanchet
parents: 36190
diff changeset
   812
(* The "-VarWeight=3" option helps the higher-order problems, probably by
16670b4f0baa set SPASS option on the command-line, so that it doesn't vanish when moving to TPTP format
blanchet
parents: 36190
diff changeset
   813
   counteracting the presence of "hAPP". *)
37498
b426cbdb5a23 removed Sledgehammer's support for the DFG syntax;
blanchet
parents: 37480
diff changeset
   814
val spass_config : prover_config =
37962
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37926
diff changeset
   815
  {home_var = "ISABELLE_ATP_MANAGER",
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37926
diff changeset
   816
   executable = "SPASS_TPTP",
37550
fc2f979b9a08 split SPASS time slot between SOS and non-SOS, in case SOS times out
blanchet
parents: 37514
diff changeset
   817
   (* "div 2" accounts for the fact that SPASS is often run twice. *)
37514
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
   818
   arguments = fn complete => fn timeout =>
37962
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37926
diff changeset
   819
     ("-Auto -PGiven=0 -PProblem=0 -Splits=0 -FullRed=0 -DocProof \
37550
fc2f979b9a08 split SPASS time slot between SOS and non-SOS, in case SOS times out
blanchet
parents: 37514
diff changeset
   820
      \-VarWeight=3 -TimeLimit=" ^
fc2f979b9a08 split SPASS time slot between SOS and non-SOS, in case SOS times out
blanchet
parents: 37514
diff changeset
   821
      string_of_int (to_generous_secs timeout div 2))
37514
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
   822
     |> not complete ? prefix "-SOS=1 ",
36369
d2cd0d04b8e6 handle ATP proof delimiters in a cleaner, more extensible fashion
blanchet
parents: 36289
diff changeset
   823
   proof_delims = [("Here is a proof", "Formulae used in the proof")],
36289
f75b6a3e1450 set "atps" reference's default value to "(remote_)e (remote_)spass (remote_)vampire", based on what is installed
blanchet
parents: 36287
diff changeset
   824
   known_failures =
37413
e856582fe9c4 improve ATP-specific error messages
blanchet
parents: 37347
diff changeset
   825
     [(IncompleteUnprovable, "SPASS beiseite: Completion found"),
36370
a4f601daa175 centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents: 36369
diff changeset
   826
      (TimedOut, "SPASS beiseite: Ran out of time"),
36965
67ae217c6b5c identify common SPASS error more clearly
blanchet
parents: 36924
diff changeset
   827
      (OutOfResources, "SPASS beiseite: Maximal number of loops exceeded"),
37413
e856582fe9c4 improve ATP-specific error messages
blanchet
parents: 37347
diff changeset
   828
      (MalformedInput, "Undefined symbol"),
37414
d0cea0796295 expect SPASS 3.7, and give a friendly warning if an older version is used
blanchet
parents: 37413
diff changeset
   829
      (MalformedInput, "Free Variable"),
37962
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37926
diff changeset
   830
      (OldSpass, "tptp2dfg")],
38009
34e1ac9cb71d shrink the "max_new_relevant_facts_per_iter" fudge factors, now that we count formulas and not clauses
blanchet
parents: 38005
diff changeset
   831
   max_new_relevant_facts_per_iter = 26 (* FIXME *),
37994
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37989
diff changeset
   832
   prefers_theory_relevant = true,
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37989
diff changeset
   833
   explicit_forall = true}
37414
d0cea0796295 expect SPASS 3.7, and give a friendly warning if an older version is used
blanchet
parents: 37413
diff changeset
   834
val spass = tptp_prover "spass" spass_config
28596
fcd463a6b6de tuned interfaces -- plain prover function, without thread;
wenzelm
parents: 28592
diff changeset
   835
37509
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents: 37506
diff changeset
   836
(* Vampire *)
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents: 37506
diff changeset
   837
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents: 37506
diff changeset
   838
val vampire_config : prover_config =
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents: 37506
diff changeset
   839
  {home_var = "VAMPIRE_HOME",
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents: 37506
diff changeset
   840
   executable = "vampire",
37514
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
   841
   arguments = fn _ => fn timeout =>
37509
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents: 37506
diff changeset
   842
     "--output_syntax tptp --mode casc -t " ^
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents: 37506
diff changeset
   843
     string_of_int (to_generous_secs timeout),
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents: 37506
diff changeset
   844
   proof_delims =
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents: 37506
diff changeset
   845
     [("=========== Refutation ==========",
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents: 37506
diff changeset
   846
       "======= End of refutation ======="),
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents: 37506
diff changeset
   847
      ("% SZS output start Refutation", "% SZS output end Refutation")],
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents: 37506
diff changeset
   848
   known_failures =
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents: 37506
diff changeset
   849
     [(Unprovable, "UNPROVABLE"),
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents: 37506
diff changeset
   850
      (IncompleteUnprovable, "CANNOT PROVE"),
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents: 37506
diff changeset
   851
      (Unprovable, "Satisfiability detected"),
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents: 37506
diff changeset
   852
      (OutOfResources, "Refutation not found")],
38009
34e1ac9cb71d shrink the "max_new_relevant_facts_per_iter" fudge factors, now that we count formulas and not clauses
blanchet
parents: 38005
diff changeset
   853
   max_new_relevant_facts_per_iter = 40 (* FIXME *),
37994
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37989
diff changeset
   854
   prefers_theory_relevant = false,
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37989
diff changeset
   855
   explicit_forall = false}
37509
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents: 37506
diff changeset
   856
val vampire = tptp_prover "vampire" vampire_config
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents: 37506
diff changeset
   857
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents: 37506
diff changeset
   858
(* Remote prover invocation via SystemOnTPTP *)
28596
fcd463a6b6de tuned interfaces -- plain prover function, without thread;
wenzelm
parents: 28592
diff changeset
   859
36376
e83d52a52449 renamed module "ATP_Wrapper" to "ATP_Systems"
blanchet
parents: 36371
diff changeset
   860
val systems = Synchronized.var "atp_systems" ([]: string list);
31835
b686d4df54c2 check for current versions on server
immler@in.tum.de
parents: 31832
diff changeset
   861
b686d4df54c2 check for current versions on server
immler@in.tum.de
parents: 31832
diff changeset
   862
fun get_systems () =
36370
a4f601daa175 centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents: 36369
diff changeset
   863
  case bash_output "\"$ISABELLE_ATP_MANAGER/SystemOnTPTP\" -w" of
a4f601daa175 centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents: 36369
diff changeset
   864
    (answer, 0) => split_lines answer
a4f601daa175 centralized ATP-specific error handling in "atp_wrapper.ML"
blanchet
parents: 36369
diff changeset
   865
  | (answer, _) =>
37627
1d1754ccd233 more precise error message for remote ATPs
blanchet
parents: 37625
diff changeset
   866
    error ("Failed to get available systems at SystemOnTPTP:\n" ^
1d1754ccd233 more precise error message for remote ATPs
blanchet
parents: 37625
diff changeset
   867
           perhaps (try (unsuffix "\n")) answer)
31835
b686d4df54c2 check for current versions on server
immler@in.tum.de
parents: 31832
diff changeset
   868
35867
16279c4c7a33 move all ATP setup code into ATP_Wrapper
blanchet
parents: 35865
diff changeset
   869
fun refresh_systems_on_tptp () =
37509
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents: 37506
diff changeset
   870
  Synchronized.change systems (fn _ => get_systems ())
31835
b686d4df54c2 check for current versions on server
immler@in.tum.de
parents: 31832
diff changeset
   871
b686d4df54c2 check for current versions on server
immler@in.tum.de
parents: 31832
diff changeset
   872
fun get_system prefix = Synchronized.change_result systems (fn systems =>
32864
a226f29d4bdc re-organized signature of AtpWrapper structure: records instead of unnamed parameters and return values,
boehmes
parents: 32740
diff changeset
   873
  (if null systems then get_systems () else systems)
32942
b6711ec9de26 misc tuning and recovery of Isabelle coding style;
wenzelm
parents: 32941
diff changeset
   874
  |> `(find_first (String.isPrefix prefix)));
32864
a226f29d4bdc re-organized signature of AtpWrapper structure: records instead of unnamed parameters and return values,
boehmes
parents: 32740
diff changeset
   875
32948
e95a4be101a8 natural argument order for prover;
wenzelm
parents: 32944
diff changeset
   876
fun the_system prefix =
32864
a226f29d4bdc re-organized signature of AtpWrapper structure: records instead of unnamed parameters and return values,
boehmes
parents: 32740
diff changeset
   877
  (case get_system prefix of
37509
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents: 37506
diff changeset
   878
    NONE => error ("System " ^ quote prefix ^ " not available at SystemOnTPTP.")
32942
b6711ec9de26 misc tuning and recovery of Isabelle coding style;
wenzelm
parents: 32941
diff changeset
   879
  | SOME sys => sys);
31835
b686d4df54c2 check for current versions on server
immler@in.tum.de
parents: 31832
diff changeset
   880
36265
41c9e755e552 distinguish between the different ATP errors in the user interface;
blanchet
parents: 36264
diff changeset
   881
val remote_known_failures =
37627
1d1754ccd233 more precise error message for remote ATPs
blanchet
parents: 37625
diff changeset
   882
  [(CantConnect, "HTTP-Error"),
1d1754ccd233 more precise error message for remote ATPs
blanchet
parents: 37625
diff changeset
   883
   (TimedOut, "says Timeout"),
36377
b3dce4c715d0 now rename the file "atp_wrapper.ML" to "atp_systems.ML" + fix typo in "SystemOnTPTP" script
blanchet
parents: 36376
diff changeset
   884
   (MalformedOutput, "Remote script could not extract proof")]
35865
2f8fb5242799 more Sledgehammer refactoring
blanchet
parents: 35826
diff changeset
   885
37509
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents: 37506
diff changeset
   886
fun remote_config atp_prefix args
38009
34e1ac9cb71d shrink the "max_new_relevant_facts_per_iter" fudge factors, now that we count formulas and not clauses
blanchet
parents: 38005
diff changeset
   887
        ({proof_delims, known_failures, max_new_relevant_facts_per_iter,
37994
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37989
diff changeset
   888
          prefers_theory_relevant, explicit_forall, ...} : prover_config)
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37989
diff changeset
   889
        : prover_config =
36917
8674cdb0b8cc query _HOME environment variables at run-time, not at build-time
blanchet
parents: 36910
diff changeset
   890
  {home_var = "ISABELLE_ATP_MANAGER",
36289
f75b6a3e1450 set "atps" reference's default value to "(remote_)e (remote_)spass (remote_)vampire", based on what is installed
blanchet
parents: 36287
diff changeset
   891
   executable = "SystemOnTPTP",
37514
b147d01b8ebc if SPASS fails at finding a proof with the SOS option turned on, turn it off and try again
blanchet
parents: 37509
diff changeset
   892
   arguments = fn _ => fn timeout =>
36382
b90fc0d75bca cosmetics
blanchet
parents: 36377
diff changeset
   893
     args ^ " -t " ^ string_of_int (to_generous_secs timeout) ^ " -s " ^
b90fc0d75bca cosmetics
blanchet
parents: 36377
diff changeset
   894
     the_system atp_prefix,
36369
d2cd0d04b8e6 handle ATP proof delimiters in a cleaner, more extensible fashion
blanchet
parents: 36289
diff changeset
   895
   proof_delims = insert (op =) tstp_proof_delims proof_delims,
36265
41c9e755e552 distinguish between the different ATP errors in the user interface;
blanchet
parents: 36264
diff changeset
   896
   known_failures = remote_known_failures @ known_failures,
38009
34e1ac9cb71d shrink the "max_new_relevant_facts_per_iter" fudge factors, now that we count formulas and not clauses
blanchet
parents: 38005
diff changeset
   897
   max_new_relevant_facts_per_iter = max_new_relevant_facts_per_iter,
37994
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37989
diff changeset
   898
   prefers_theory_relevant = prefers_theory_relevant,
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37989
diff changeset
   899
   explicit_forall = explicit_forall}
28596
fcd463a6b6de tuned interfaces -- plain prover function, without thread;
wenzelm
parents: 28592
diff changeset
   900
37509
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents: 37506
diff changeset
   901
fun remote_tptp_prover prover atp_prefix args config =
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents: 37506
diff changeset
   902
  tptp_prover (remotify (fst prover)) (remote_config atp_prefix args config)
32864
a226f29d4bdc re-organized signature of AtpWrapper structure: records instead of unnamed parameters and return values,
boehmes
parents: 32740
diff changeset
   903
37509
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents: 37506
diff changeset
   904
val remote_e = remote_tptp_prover e "EP---" "" e_config
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents: 37506
diff changeset
   905
val remote_spass = remote_tptp_prover spass "SPASS---" "-x" spass_config
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents: 37506
diff changeset
   906
val remote_vampire = remote_tptp_prover vampire "Vampire---9" "" vampire_config
28592
824f8390aaa2 renamed AtpThread to AtpWrapper;
wenzelm
parents:
diff changeset
   907
36917
8674cdb0b8cc query _HOME environment variables at run-time, not at build-time
blanchet
parents: 36910
diff changeset
   908
fun maybe_remote (name, _) ({home_var, ...} : prover_config) =
8674cdb0b8cc query _HOME environment variables at run-time, not at build-time
blanchet
parents: 36910
diff changeset
   909
  name |> getenv home_var = "" ? remotify
36371
8c83ea1a7740 move the Sledgehammer menu options to "sledgehammer_isar.ML"
blanchet
parents: 36370
diff changeset
   910
8c83ea1a7740 move the Sledgehammer menu options to "sledgehammer_isar.ML"
blanchet
parents: 36370
diff changeset
   911
fun default_atps_param_value () =
8c83ea1a7740 move the Sledgehammer menu options to "sledgehammer_isar.ML"
blanchet
parents: 36370
diff changeset
   912
  space_implode " " [maybe_remote e e_config, maybe_remote spass spass_config,
8c83ea1a7740 move the Sledgehammer menu options to "sledgehammer_isar.ML"
blanchet
parents: 36370
diff changeset
   913
                     remotify (fst vampire)]
8c83ea1a7740 move the Sledgehammer menu options to "sledgehammer_isar.ML"
blanchet
parents: 36370
diff changeset
   914
37509
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents: 37506
diff changeset
   915
val provers = [e, spass, vampire, remote_e, remote_spass, remote_vampire]
35867
16279c4c7a33 move all ATP setup code into ATP_Wrapper
blanchet
parents: 35865
diff changeset
   916
val prover_setup = fold add_prover provers
16279c4c7a33 move all ATP setup code into ATP_Wrapper
blanchet
parents: 35865
diff changeset
   917
16279c4c7a33 move all ATP setup code into ATP_Wrapper
blanchet
parents: 35865
diff changeset
   918
val setup =
36376
e83d52a52449 renamed module "ATP_Wrapper" to "ATP_Systems"
blanchet
parents: 36371
diff changeset
   919
  dest_dir_setup
35867
16279c4c7a33 move all ATP setup code into ATP_Wrapper
blanchet
parents: 35865
diff changeset
   920
  #> problem_prefix_setup
16279c4c7a33 move all ATP setup code into ATP_Wrapper
blanchet
parents: 35865
diff changeset
   921
  #> measure_runtime_setup
36371
8c83ea1a7740 move the Sledgehammer menu options to "sledgehammer_isar.ML"
blanchet
parents: 36370
diff changeset
   922
  #> prover_setup
35867
16279c4c7a33 move all ATP setup code into ATP_Wrapper
blanchet
parents: 35865
diff changeset
   923
28592
824f8390aaa2 renamed AtpThread to AtpWrapper;
wenzelm
parents:
diff changeset
   924
end;