src/HOL/ex/atp_export.ML
author blanchet
Mon, 27 Jun 2011 13:52:47 +0200
changeset 44431 b342cd125533
parent 44428 a818d5a34cca
child 44434 ae612a423dad
permissions -rw-r--r--
removed "full_types" option from Sledgehammer, now that virtually sound encodings are used as the default anyway
blanchet@44064
     1
(*  Title:      HOL/ex/atp_export.ML
blanchet@43473
     2
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@43473
     3
    Copyright   2011
blanchet@43473
     4
blanchet@43473
     5
Export Isabelle theories as first-order TPTP inferences, exploiting
blanchet@43473
     6
Sledgehammer's translation.
blanchet@43473
     7
*)
blanchet@43473
     8
blanchet@44064
     9
signature ATP_EXPORT =
blanchet@43473
    10
sig
blanchet@44365
    11
  val theorems_mentioned_in_proof_term :
blanchet@44365
    12
    string list option -> thm -> string list
blanchet@43473
    13
  val generate_tptp_graph_file_for_theory :
blanchet@43473
    14
    Proof.context -> theory -> string -> unit
blanchet@43473
    15
  val generate_tptp_inference_file_for_theory :
blanchet@44263
    16
    Proof.context -> theory -> string -> string -> unit
blanchet@43473
    17
end;
blanchet@43473
    18
blanchet@44064
    19
structure ATP_Export : ATP_EXPORT =
blanchet@43473
    20
struct
blanchet@43473
    21
blanchet@44350
    22
open ATP_Problem
blanchet@44350
    23
open ATP_Translate
blanchet@44428
    24
open ATP_Proof
blanchet@44428
    25
open ATP_Systems
blanchet@43473
    26
blanchet@44350
    27
val fact_name_of = prefix fact_prefix o ascii_of
blanchet@43473
    28
blanchet@43473
    29
fun facts_of thy =
wenzelm@44156
    30
  Sledgehammer_Filter.all_facts (Proof_Context.init_global thy) Symtab.empty
blanchet@44217
    31
                                true [] []
blanchet@43473
    32
blanchet@44329
    33
(* FIXME: Similar yet different code in "mirabelle.ML". The code here has a few
blanchet@44329
    34
   fixes that seem to be missing over there; or maybe the two code portions are
blanchet@44329
    35
   not doing the same? *)
blanchet@44369
    36
fun fold_body_thms thm_name all_names f =
blanchet@43473
    37
  let
blanchet@44329
    38
    fun app n (PBody {thms, ...}) =
blanchet@44350
    39
      thms |> fold (fn (_, (name, prop, body)) => fn x =>
blanchet@44350
    40
        let
blanchet@44350
    41
          val body' = Future.join body
blanchet@44350
    42
          val n' =
blanchet@44350
    43
            n + (if name = "" orelse
blanchet@44350
    44
                    (is_some all_names andalso
blanchet@44369
    45
                     not (member (op =) (the all_names) name)) orelse
blanchet@44369
    46
                    (* uncommon case where the proved theorem occurs twice
blanchet@44369
    47
                       (e.g., "Transitive_Closure.trancl_into_trancl") *)
blanchet@44369
    48
                    n = 1 andalso name = thm_name then
blanchet@44350
    49
                   0
blanchet@44350
    50
                 else
blanchet@44350
    51
                   1)
blanchet@44350
    52
          val x' = x |> n' <= 1 ? app n' body'
blanchet@44350
    53
        in (x' |> n = 1 ? f (name, prop, body')) end)
blanchet@44350
    54
  in fold (app 0) end
blanchet@43473
    55
blanchet@44350
    56
fun theorems_mentioned_in_proof_term all_names thm =
blanchet@43473
    57
  let
blanchet@43473
    58
    fun collect (s, _, _) = if s <> "" then insert (op =) s else I
blanchet@43473
    59
    val names =
blanchet@44369
    60
      [] |> fold_body_thms (Thm.get_name_hint thm) all_names collect
blanchet@44369
    61
                           [Thm.proof_body_of thm]
blanchet@44350
    62
         |> map fact_name_of
blanchet@43473
    63
  in names end
blanchet@43473
    64
blanchet@43473
    65
fun interesting_const_names ctxt =
wenzelm@44156
    66
  let val thy = Proof_Context.theory_of ctxt in
blanchet@43473
    67
    Sledgehammer_Filter.const_names_in_fact thy
blanchet@44428
    68
        (Sledgehammer_Provers.is_built_in_const_for_prover ctxt eN)
blanchet@43473
    69
  end
blanchet@43473
    70
blanchet@43473
    71
fun generate_tptp_graph_file_for_theory ctxt thy file_name =
blanchet@43473
    72
  let
blanchet@43473
    73
    val path = file_name |> Path.explode
blanchet@43473
    74
    val _ = File.write path ""
blanchet@43473
    75
    val axioms = Theory.all_axioms_of thy |> map fst
blanchet@43473
    76
    fun do_thm thm =
blanchet@43473
    77
      let
blanchet@43473
    78
        val name = Thm.get_name_hint thm
blanchet@43473
    79
        val s =
blanchet@43473
    80
          "[" ^ Thm.legacy_get_kind thm ^ "] " ^
blanchet@43473
    81
          (if member (op =) axioms name then "A" else "T") ^ " " ^
blanchet@44350
    82
          prefix fact_prefix (ascii_of name) ^ ": " ^
blanchet@44350
    83
          commas (theorems_mentioned_in_proof_term NONE thm) ^ "; " ^
blanchet@44350
    84
          commas (map (prefix const_prefix o ascii_of)
blanchet@43473
    85
                      (interesting_const_names ctxt (Thm.prop_of thm))) ^ " \n"
blanchet@43473
    86
      in File.append path s end
blanchet@44086
    87
    val thms = facts_of thy |> map snd
blanchet@43473
    88
    val _ = map do_thm thms
blanchet@43473
    89
  in () end
blanchet@43473
    90
blanchet@43473
    91
fun inference_term [] = NONE
blanchet@43473
    92
  | inference_term ss =
blanchet@44350
    93
    ATerm ("inference",
blanchet@44350
    94
           [ATerm ("isabelle", []),
blanchet@44350
    95
            ATerm (tptp_empty_list, []),
blanchet@44350
    96
            ATerm (tptp_empty_list, map (fn s => ATerm (s, [])) ss)])
blanchet@43473
    97
    |> SOME
blanchet@43473
    98
fun inference infers ident =
blanchet@43473
    99
  these (AList.lookup (op =) infers ident) |> inference_term
blanchet@43473
   100
fun add_inferences_to_problem_line infers
blanchet@44350
   101
                                   (Formula (ident, Axiom, phi, NONE, NONE)) =
blanchet@44350
   102
    Formula (ident, Lemma, phi, inference infers ident, NONE)
blanchet@43473
   103
  | add_inferences_to_problem_line _ line = line
blanchet@43473
   104
val add_inferences_to_problem =
blanchet@43473
   105
  map o apsnd o map o add_inferences_to_problem_line
blanchet@43473
   106
blanchet@44350
   107
fun ident_of_problem_line (Decl (ident, _, _)) = ident
blanchet@44350
   108
  | ident_of_problem_line (Formula (ident, _, _, _, _)) = ident
blanchet@44350
   109
blanchet@44428
   110
fun run_some_atp ctxt problem =
blanchet@44428
   111
  let
blanchet@44428
   112
    val thy = Proof_Context.theory_of ctxt
blanchet@44428
   113
    val prob_file = Path.explode "/tmp/prob.tptp"
blanchet@44428
   114
    val {exec, arguments, proof_delims, known_failures, ...} =
blanchet@44428
   115
      get_atp thy spassN
blanchet@44428
   116
    val _ = problem |> tptp_lines_for_atp_problem FOF
blanchet@44428
   117
                    |> File.write_list prob_file
blanchet@44428
   118
    val command =
blanchet@44428
   119
      File.shell_path (Path.explode (getenv (fst exec) ^ "/" ^ snd exec)) ^
blanchet@44428
   120
      " " ^ arguments ctxt false "" (seconds 1.0) (K []) ^ " " ^
blanchet@44428
   121
      File.shell_path prob_file
blanchet@44428
   122
  in
blanchet@44431
   123
    TimeLimit.timeLimit (seconds 0.3) bash_output command
blanchet@44428
   124
    |> fst
blanchet@44428
   125
    |> extract_tstplike_proof_and_outcome false true proof_delims
blanchet@44428
   126
                                          known_failures
blanchet@44428
   127
    |> snd
blanchet@44428
   128
  end
blanchet@44428
   129
  handle TimeLimit.TimeOut => SOME TimedOut
blanchet@44428
   130
blanchet@44428
   131
val likely_tautology_prefixes =
blanchet@44431
   132
  [@{theory HOL}, @{theory Meson}, @{theory ATP}, @{theory Metis}]
blanchet@44428
   133
  |> map (fact_name_of o Context.theory_name)
blanchet@44428
   134
blanchet@44428
   135
fun is_problem_line_tautology ctxt (Formula (ident, _, phi, _, _)) =
blanchet@44428
   136
    exists (fn prefix => String.isPrefix prefix ident)
blanchet@44428
   137
           likely_tautology_prefixes andalso
blanchet@44428
   138
    is_none (run_some_atp ctxt
blanchet@44428
   139
                 [(factsN, [Formula (ident, Conjecture, phi, NONE, NONE)])])
blanchet@44428
   140
  | is_problem_line_tautology _ _ = false
blanchet@44428
   141
blanchet@44370
   142
structure String_Graph = Graph(type key = string val ord = string_ord);
blanchet@44370
   143
blanchet@44370
   144
fun order_facts ord = sort (ord o pairself ident_of_problem_line)
blanchet@44370
   145
fun order_problem_facts _ [] = []
blanchet@44370
   146
  | order_problem_facts ord ((heading, lines) :: problem) =
blanchet@44370
   147
    if heading = factsN then (heading, order_facts ord lines) :: problem
blanchet@44370
   148
    else (heading, lines) :: order_problem_facts ord problem
blanchet@44370
   149
blanchet@44263
   150
fun generate_tptp_inference_file_for_theory ctxt thy type_sys file_name =
blanchet@43473
   151
  let
blanchet@44350
   152
    val format = FOF
blanchet@44350
   153
    val type_sys = type_sys |> type_sys_from_string
blanchet@43473
   154
    val path = file_name |> Path.explode
blanchet@43473
   155
    val _ = File.write path ""
blanchet@43473
   156
    val facts0 = facts_of thy
blanchet@43473
   157
    val facts =
blanchet@44086
   158
      facts0 |> map (fn ((_, loc), th) =>
blanchet@44063
   159
                        ((Thm.get_name_hint th, loc), prop_of th))
blanchet@43750
   160
    val (atp_problem, _, _, _, _, _, _) =
blanchet@44365
   161
      prepare_atp_problem ctxt format Axiom Axiom type_sys true false true []
blanchet@44350
   162
                          @{prop False} facts
blanchet@44428
   163
    val atp_problem =
blanchet@44428
   164
      atp_problem
blanchet@44428
   165
      |> map (apsnd (filter_out (is_problem_line_tautology ctxt)))
blanchet@44350
   166
    val all_names = facts0 |> map (Thm.get_name_hint o snd)
blanchet@43473
   167
    val infers =
blanchet@44086
   168
      facts0 |> map (fn (_, th) =>
blanchet@43473
   169
                        (fact_name_of (Thm.get_name_hint th),
blanchet@44350
   170
                         theorems_mentioned_in_proof_term (SOME all_names) th))
blanchet@44428
   171
blanchet@44350
   172
    val all_atp_problem_names =
blanchet@44350
   173
      atp_problem |> maps (map ident_of_problem_line o snd)
blanchet@43473
   174
    val infers =
blanchet@44370
   175
      infers |> filter (member (op =) all_atp_problem_names o fst)
blanchet@44370
   176
             |> map (apsnd (filter (member (op =) all_atp_problem_names)))
blanchet@44370
   177
    val ordered_names =
blanchet@44370
   178
      String_Graph.empty
blanchet@44370
   179
      |> fold (String_Graph.new_node o rpair ()) all_atp_problem_names
blanchet@44370
   180
      |> fold (fn (to, froms) =>
blanchet@44428
   181
                  fold (fn from => String_Graph.add_edge (from, to)) froms)
blanchet@44428
   182
              infers
blanchet@44370
   183
      |> String_Graph.topological_order
blanchet@44370
   184
    val order_tab =
blanchet@44370
   185
      Symtab.empty
blanchet@44370
   186
      |> fold (Symtab.insert (op =))
blanchet@44370
   187
              (ordered_names ~~ (1 upto length ordered_names))
blanchet@44370
   188
    val name_ord = int_ord o pairself (the o Symtab.lookup order_tab)
blanchet@44370
   189
    val atp_problem =
blanchet@44370
   190
      atp_problem |> add_inferences_to_problem infers
blanchet@44370
   191
                  |> order_problem_facts name_ord
blanchet@44350
   192
    val ss = tptp_lines_for_atp_problem FOF atp_problem
blanchet@43473
   193
    val _ = app (File.append path) ss
blanchet@43473
   194
  in () end
blanchet@43473
   195
blanchet@43473
   196
end;