src/HOL/ex/atp_export.ML
author blanchet
Tue Jun 21 17:17:39 2011 +0200 (2011-06-21)
changeset 43499 9ca694caa61b
parent 43498 75caf7e4302e
child 43566 a818d5a34cca
permissions -rw-r--r--
order generated facts topologically
blanchet@43223
     1
(*  Title:      HOL/ex/atp_export.ML
blanchet@42602
     2
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@42602
     3
    Copyright   2011
blanchet@42602
     4
blanchet@42602
     5
Export Isabelle theories as first-order TPTP inferences, exploiting
blanchet@42602
     6
Sledgehammer's translation.
blanchet@42602
     7
*)
blanchet@42602
     8
blanchet@43223
     9
signature ATP_EXPORT =
blanchet@42602
    10
sig
blanchet@43494
    11
  val theorems_mentioned_in_proof_term :
blanchet@43494
    12
    string list option -> thm -> string list
blanchet@42602
    13
  val generate_tptp_graph_file_for_theory :
blanchet@42602
    14
    Proof.context -> theory -> string -> unit
blanchet@42602
    15
  val generate_tptp_inference_file_for_theory :
blanchet@43400
    16
    Proof.context -> theory -> string -> string -> unit
blanchet@42602
    17
end;
blanchet@42602
    18
blanchet@43223
    19
structure ATP_Export : ATP_EXPORT =
blanchet@42602
    20
struct
blanchet@42602
    21
blanchet@43479
    22
open ATP_Problem
blanchet@43479
    23
open ATP_Translate
blanchet@42602
    24
blanchet@43479
    25
val fact_name_of = prefix fact_prefix o ascii_of
blanchet@42602
    26
blanchet@42602
    27
fun facts_of thy =
wenzelm@43276
    28
  Sledgehammer_Filter.all_facts (Proof_Context.init_global thy) Symtab.empty
blanchet@43351
    29
                                true [] []
blanchet@42602
    30
blanchet@43468
    31
(* FIXME: Similar yet different code in "mirabelle.ML". The code here has a few
blanchet@43468
    32
   fixes that seem to be missing over there; or maybe the two code portions are
blanchet@43468
    33
   not doing the same? *)
blanchet@43498
    34
fun fold_body_thms thm_name all_names f =
blanchet@42602
    35
  let
blanchet@43468
    36
    fun app n (PBody {thms, ...}) =
blanchet@43479
    37
      thms |> fold (fn (_, (name, prop, body)) => fn x =>
blanchet@43479
    38
        let
blanchet@43479
    39
          val body' = Future.join body
blanchet@43479
    40
          val n' =
blanchet@43479
    41
            n + (if name = "" orelse
blanchet@43479
    42
                    (is_some all_names andalso
blanchet@43498
    43
                     not (member (op =) (the all_names) name)) orelse
blanchet@43498
    44
                    (* uncommon case where the proved theorem occurs twice
blanchet@43498
    45
                       (e.g., "Transitive_Closure.trancl_into_trancl") *)
blanchet@43498
    46
                    n = 1 andalso name = thm_name then
blanchet@43479
    47
                   0
blanchet@43479
    48
                 else
blanchet@43479
    49
                   1)
blanchet@43479
    50
          val x' = x |> n' <= 1 ? app n' body'
blanchet@43479
    51
        in (x' |> n = 1 ? f (name, prop, body')) end)
blanchet@43479
    52
  in fold (app 0) end
blanchet@42602
    53
blanchet@43479
    54
fun theorems_mentioned_in_proof_term all_names thm =
blanchet@42602
    55
  let
blanchet@42602
    56
    fun collect (s, _, _) = if s <> "" then insert (op =) s else I
blanchet@42602
    57
    val names =
blanchet@43498
    58
      [] |> fold_body_thms (Thm.get_name_hint thm) all_names collect
blanchet@43498
    59
                           [Thm.proof_body_of thm]
blanchet@43479
    60
         |> map fact_name_of
blanchet@42602
    61
  in names end
blanchet@42602
    62
blanchet@42602
    63
fun interesting_const_names ctxt =
wenzelm@43276
    64
  let val thy = Proof_Context.theory_of ctxt in
blanchet@42602
    65
    Sledgehammer_Filter.const_names_in_fact thy
blanchet@42602
    66
        (Sledgehammer_Provers.is_built_in_const_for_prover ctxt ATP_Systems.eN)
blanchet@42602
    67
  end
blanchet@42602
    68
blanchet@42602
    69
fun generate_tptp_graph_file_for_theory ctxt thy file_name =
blanchet@42602
    70
  let
blanchet@42602
    71
    val path = file_name |> Path.explode
blanchet@42602
    72
    val _ = File.write path ""
blanchet@42602
    73
    val axioms = Theory.all_axioms_of thy |> map fst
blanchet@42602
    74
    fun do_thm thm =
blanchet@42602
    75
      let
blanchet@42602
    76
        val name = Thm.get_name_hint thm
blanchet@42602
    77
        val s =
blanchet@42602
    78
          "[" ^ Thm.legacy_get_kind thm ^ "] " ^
blanchet@42602
    79
          (if member (op =) axioms name then "A" else "T") ^ " " ^
blanchet@43479
    80
          prefix fact_prefix (ascii_of name) ^ ": " ^
blanchet@43479
    81
          commas (theorems_mentioned_in_proof_term NONE thm) ^ "; " ^
blanchet@43479
    82
          commas (map (prefix const_prefix o ascii_of)
blanchet@42602
    83
                      (interesting_const_names ctxt (Thm.prop_of thm))) ^ " \n"
blanchet@42602
    84
      in File.append path s end
blanchet@43245
    85
    val thms = facts_of thy |> map snd
blanchet@42602
    86
    val _ = map do_thm thms
blanchet@42602
    87
  in () end
blanchet@42602
    88
blanchet@42602
    89
fun inference_term [] = NONE
blanchet@42602
    90
  | inference_term ss =
blanchet@43479
    91
    ATerm ("inference",
blanchet@43479
    92
           [ATerm ("isabelle", []),
blanchet@43479
    93
            ATerm (tptp_empty_list, []),
blanchet@43479
    94
            ATerm (tptp_empty_list, map (fn s => ATerm (s, [])) ss)])
blanchet@42602
    95
    |> SOME
blanchet@42602
    96
fun inference infers ident =
blanchet@42602
    97
  these (AList.lookup (op =) infers ident) |> inference_term
blanchet@42602
    98
fun add_inferences_to_problem_line infers
blanchet@43479
    99
                                   (Formula (ident, Axiom, phi, NONE, NONE)) =
blanchet@43479
   100
    Formula (ident, Lemma, phi, inference infers ident, NONE)
blanchet@42602
   101
  | add_inferences_to_problem_line _ line = line
blanchet@42602
   102
val add_inferences_to_problem =
blanchet@42602
   103
  map o apsnd o map o add_inferences_to_problem_line
blanchet@42602
   104
blanchet@43479
   105
fun ident_of_problem_line (Decl (ident, _, _)) = ident
blanchet@43479
   106
  | ident_of_problem_line (Formula (ident, _, _, _, _)) = ident
blanchet@43479
   107
blanchet@43499
   108
structure String_Graph = Graph(type key = string val ord = string_ord);
blanchet@43499
   109
blanchet@43499
   110
fun order_facts ord = sort (ord o pairself ident_of_problem_line)
blanchet@43499
   111
fun order_problem_facts _ [] = []
blanchet@43499
   112
  | order_problem_facts ord ((heading, lines) :: problem) =
blanchet@43499
   113
    if heading = factsN then (heading, order_facts ord lines) :: problem
blanchet@43499
   114
    else (heading, lines) :: order_problem_facts ord problem
blanchet@43499
   115
blanchet@43400
   116
fun generate_tptp_inference_file_for_theory ctxt thy type_sys file_name =
blanchet@42602
   117
  let
blanchet@43479
   118
    val format = FOF
blanchet@43479
   119
    val type_sys = type_sys |> type_sys_from_string
blanchet@42602
   120
    val path = file_name |> Path.explode
blanchet@42602
   121
    val _ = File.write path ""
blanchet@42602
   122
    val facts0 = facts_of thy
blanchet@42602
   123
    val facts =
blanchet@43245
   124
      facts0 |> map (fn ((_, loc), th) =>
blanchet@43222
   125
                        ((Thm.get_name_hint th, loc), prop_of th))
blanchet@42881
   126
    val (atp_problem, _, _, _, _, _, _) =
blanchet@43494
   127
      prepare_atp_problem ctxt format Axiom Axiom type_sys true false true []
blanchet@43479
   128
                          @{prop False} facts
blanchet@43479
   129
    val all_names = facts0 |> map (Thm.get_name_hint o snd)
blanchet@42602
   130
    val infers =
blanchet@43245
   131
      facts0 |> map (fn (_, th) =>
blanchet@42602
   132
                        (fact_name_of (Thm.get_name_hint th),
blanchet@43479
   133
                         theorems_mentioned_in_proof_term (SOME all_names) th))
blanchet@43479
   134
    val all_atp_problem_names =
blanchet@43479
   135
      atp_problem |> maps (map ident_of_problem_line o snd)
blanchet@42602
   136
    val infers =
blanchet@43499
   137
      infers |> filter (member (op =) all_atp_problem_names o fst)
blanchet@43499
   138
             |> map (apsnd (filter (member (op =) all_atp_problem_names)))
blanchet@43499
   139
    val ordered_names =
blanchet@43499
   140
      String_Graph.empty
blanchet@43499
   141
      |> fold (String_Graph.new_node o rpair ()) all_atp_problem_names
blanchet@43499
   142
      |> fold (fn (to, froms) =>
blanchet@43499
   143
                  fold (fn from => String_Graph.add_edge (from, to)) froms) infers
blanchet@43499
   144
      |> String_Graph.topological_order
blanchet@43499
   145
    val order_tab =
blanchet@43499
   146
      Symtab.empty
blanchet@43499
   147
      |> fold (Symtab.insert (op =))
blanchet@43499
   148
              (ordered_names ~~ (1 upto length ordered_names))
blanchet@43499
   149
    val name_ord = int_ord o pairself (the o Symtab.lookup order_tab)
blanchet@43499
   150
    val atp_problem =
blanchet@43499
   151
      atp_problem |> add_inferences_to_problem infers
blanchet@43499
   152
                  |> order_problem_facts name_ord
blanchet@43479
   153
    val ss = tptp_lines_for_atp_problem FOF atp_problem
blanchet@42602
   154
    val _ = app (File.append path) ss
blanchet@42602
   155
  in () end
blanchet@42602
   156
blanchet@42602
   157
end;