src/HOL/ex/atp_export.ML
author blanchet
Tue, 21 Jun 2011 17:17:39 +0200
changeset 44365 13eefebbc4cb
parent 44350 5af1abc13c1f
child 44369 75caf7e4302e
permissions -rw-r--r--
make sure that enough type information is generated -- because the exported "lemma"s are also used as "conjecture", we can't optimize type information based on polarity
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@43473
    24
blanchet@44350
    25
val fact_name_of = prefix fact_prefix o ascii_of
blanchet@43473
    26
blanchet@43473
    27
fun facts_of thy =
wenzelm@44156
    28
  Sledgehammer_Filter.all_facts (Proof_Context.init_global thy) Symtab.empty
blanchet@44217
    29
                                true [] []
blanchet@43473
    30
blanchet@44329
    31
(* FIXME: Similar yet different code in "mirabelle.ML". The code here has a few
blanchet@44329
    32
   fixes that seem to be missing over there; or maybe the two code portions are
blanchet@44329
    33
   not doing the same? *)
blanchet@44350
    34
fun fold_body_thms all_names f =
blanchet@43473
    35
  let
blanchet@44329
    36
    fun app n (PBody {thms, ...}) =
blanchet@44350
    37
      thms |> fold (fn (_, (name, prop, body)) => fn x =>
blanchet@44350
    38
        let
blanchet@44350
    39
          val body' = Future.join body
blanchet@44350
    40
          val n' =
blanchet@44350
    41
            n + (if name = "" orelse
blanchet@44350
    42
                    (is_some all_names andalso
blanchet@44350
    43
                     not (member (op =) (the all_names) name)) then
blanchet@44350
    44
                   0
blanchet@44350
    45
                 else
blanchet@44350
    46
                   1)
blanchet@44350
    47
          val x' = x |> n' <= 1 ? app n' body'
blanchet@44350
    48
        in (x' |> n = 1 ? f (name, prop, body')) end)
blanchet@44350
    49
  in fold (app 0) end
blanchet@43473
    50
blanchet@44350
    51
fun theorems_mentioned_in_proof_term all_names thm =
blanchet@43473
    52
  let
blanchet@43473
    53
    fun collect (s, _, _) = if s <> "" then insert (op =) s else I
blanchet@43473
    54
    val names =
blanchet@44350
    55
      [] |> fold_body_thms all_names collect [Thm.proof_body_of thm]
blanchet@44350
    56
         |> map fact_name_of
blanchet@43473
    57
  in names end
blanchet@43473
    58
blanchet@43473
    59
fun interesting_const_names ctxt =
wenzelm@44156
    60
  let val thy = Proof_Context.theory_of ctxt in
blanchet@43473
    61
    Sledgehammer_Filter.const_names_in_fact thy
blanchet@43473
    62
        (Sledgehammer_Provers.is_built_in_const_for_prover ctxt ATP_Systems.eN)
blanchet@43473
    63
  end
blanchet@43473
    64
blanchet@43473
    65
fun generate_tptp_graph_file_for_theory ctxt thy file_name =
blanchet@43473
    66
  let
blanchet@43473
    67
    val path = file_name |> Path.explode
blanchet@43473
    68
    val _ = File.write path ""
blanchet@43473
    69
    val axioms = Theory.all_axioms_of thy |> map fst
blanchet@43473
    70
    fun do_thm thm =
blanchet@43473
    71
      let
blanchet@43473
    72
        val name = Thm.get_name_hint thm
blanchet@43473
    73
        val s =
blanchet@43473
    74
          "[" ^ Thm.legacy_get_kind thm ^ "] " ^
blanchet@43473
    75
          (if member (op =) axioms name then "A" else "T") ^ " " ^
blanchet@44350
    76
          prefix fact_prefix (ascii_of name) ^ ": " ^
blanchet@44350
    77
          commas (theorems_mentioned_in_proof_term NONE thm) ^ "; " ^
blanchet@44350
    78
          commas (map (prefix const_prefix o ascii_of)
blanchet@43473
    79
                      (interesting_const_names ctxt (Thm.prop_of thm))) ^ " \n"
blanchet@43473
    80
      in File.append path s end
blanchet@44086
    81
    val thms = facts_of thy |> map snd
blanchet@43473
    82
    val _ = map do_thm thms
blanchet@43473
    83
  in () end
blanchet@43473
    84
blanchet@43473
    85
fun inference_term [] = NONE
blanchet@43473
    86
  | inference_term ss =
blanchet@44350
    87
    ATerm ("inference",
blanchet@44350
    88
           [ATerm ("isabelle", []),
blanchet@44350
    89
            ATerm (tptp_empty_list, []),
blanchet@44350
    90
            ATerm (tptp_empty_list, map (fn s => ATerm (s, [])) ss)])
blanchet@43473
    91
    |> SOME
blanchet@43473
    92
fun inference infers ident =
blanchet@43473
    93
  these (AList.lookup (op =) infers ident) |> inference_term
blanchet@43473
    94
fun add_inferences_to_problem_line infers
blanchet@44350
    95
                                   (Formula (ident, Axiom, phi, NONE, NONE)) =
blanchet@44350
    96
    Formula (ident, Lemma, phi, inference infers ident, NONE)
blanchet@43473
    97
  | add_inferences_to_problem_line _ line = line
blanchet@43473
    98
val add_inferences_to_problem =
blanchet@43473
    99
  map o apsnd o map o add_inferences_to_problem_line
blanchet@43473
   100
blanchet@44350
   101
fun ident_of_problem_line (Decl (ident, _, _)) = ident
blanchet@44350
   102
  | ident_of_problem_line (Formula (ident, _, _, _, _)) = ident
blanchet@44350
   103
blanchet@44263
   104
fun generate_tptp_inference_file_for_theory ctxt thy type_sys file_name =
blanchet@43473
   105
  let
blanchet@44350
   106
    val format = FOF
blanchet@44350
   107
    val type_sys = type_sys |> type_sys_from_string
blanchet@43473
   108
    val path = file_name |> Path.explode
blanchet@43473
   109
    val _ = File.write path ""
blanchet@43473
   110
    val facts0 = facts_of thy
blanchet@43473
   111
    val facts =
blanchet@44086
   112
      facts0 |> map (fn ((_, loc), th) =>
blanchet@44063
   113
                        ((Thm.get_name_hint th, loc), prop_of th))
blanchet@43750
   114
    val (atp_problem, _, _, _, _, _, _) =
blanchet@44365
   115
      prepare_atp_problem ctxt format Axiom Axiom type_sys true false true []
blanchet@44350
   116
                          @{prop False} facts
blanchet@44350
   117
    val all_names = facts0 |> map (Thm.get_name_hint o snd)
blanchet@43473
   118
    val infers =
blanchet@44086
   119
      facts0 |> map (fn (_, th) =>
blanchet@43473
   120
                        (fact_name_of (Thm.get_name_hint th),
blanchet@44350
   121
                         theorems_mentioned_in_proof_term (SOME all_names) th))
blanchet@44350
   122
    val all_atp_problem_names =
blanchet@44350
   123
      atp_problem |> maps (map ident_of_problem_line o snd)
blanchet@43473
   124
    val infers =
blanchet@44350
   125
      infers |> map (apsnd (filter (member (op =) all_atp_problem_names)))
blanchet@43473
   126
    val atp_problem = atp_problem |> add_inferences_to_problem infers
blanchet@44350
   127
    val ss = tptp_lines_for_atp_problem FOF atp_problem
blanchet@43473
   128
    val _ = app (File.append path) ss
blanchet@43473
   129
  in () end
blanchet@43473
   130
blanchet@43473
   131
end;