src/HOL/ex/atp_export.ML
author blanchet
Mon Jun 20 10:41:02 2011 +0200 (2011-06-20)
changeset 43479 5af1abc13c1f
parent 43468 c768f7adb711
child 43494 13eefebbc4cb
permissions -rw-r--r--
only refer to facts found in TPTP file -- e.g. facts that simplify to true are excluded
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@42602
    11
  val generate_tptp_graph_file_for_theory :
blanchet@42602
    12
    Proof.context -> theory -> string -> unit
blanchet@42602
    13
  val generate_tptp_inference_file_for_theory :
blanchet@43400
    14
    Proof.context -> theory -> string -> string -> unit
blanchet@42602
    15
end;
blanchet@42602
    16
blanchet@43223
    17
structure ATP_Export : ATP_EXPORT =
blanchet@42602
    18
struct
blanchet@42602
    19
blanchet@43479
    20
open ATP_Problem
blanchet@43479
    21
open ATP_Translate
blanchet@42602
    22
blanchet@43479
    23
val fact_name_of = prefix fact_prefix o ascii_of
blanchet@42602
    24
blanchet@42602
    25
fun facts_of thy =
wenzelm@43276
    26
  Sledgehammer_Filter.all_facts (Proof_Context.init_global thy) Symtab.empty
blanchet@43351
    27
                                true [] []
blanchet@42602
    28
blanchet@43468
    29
(* FIXME: Similar yet different code in "mirabelle.ML". The code here has a few
blanchet@43468
    30
   fixes that seem to be missing over there; or maybe the two code portions are
blanchet@43468
    31
   not doing the same? *)
blanchet@43479
    32
fun fold_body_thms all_names f =
blanchet@42602
    33
  let
blanchet@43468
    34
    fun app n (PBody {thms, ...}) =
blanchet@43479
    35
      thms |> fold (fn (_, (name, prop, body)) => fn x =>
blanchet@43479
    36
        let
blanchet@43479
    37
          val body' = Future.join body
blanchet@43479
    38
          val n' =
blanchet@43479
    39
            n + (if name = "" orelse
blanchet@43479
    40
                    (is_some all_names andalso
blanchet@43479
    41
                     not (member (op =) (the all_names) name)) then
blanchet@43479
    42
                   0
blanchet@43479
    43
                 else
blanchet@43479
    44
                   1)
blanchet@43479
    45
          val x' = x |> n' <= 1 ? app n' body'
blanchet@43479
    46
        in (x' |> n = 1 ? f (name, prop, body')) end)
blanchet@43479
    47
  in fold (app 0) end
blanchet@42602
    48
blanchet@43479
    49
fun theorems_mentioned_in_proof_term all_names thm =
blanchet@42602
    50
  let
blanchet@42602
    51
    fun collect (s, _, _) = if s <> "" then insert (op =) s else I
blanchet@42602
    52
    val names =
blanchet@43479
    53
      [] |> fold_body_thms all_names collect [Thm.proof_body_of thm]
blanchet@43479
    54
         |> map fact_name_of
blanchet@42602
    55
  in names end
blanchet@42602
    56
blanchet@42602
    57
fun interesting_const_names ctxt =
wenzelm@43276
    58
  let val thy = Proof_Context.theory_of ctxt in
blanchet@42602
    59
    Sledgehammer_Filter.const_names_in_fact thy
blanchet@42602
    60
        (Sledgehammer_Provers.is_built_in_const_for_prover ctxt ATP_Systems.eN)
blanchet@42602
    61
  end
blanchet@42602
    62
blanchet@42602
    63
fun generate_tptp_graph_file_for_theory ctxt thy file_name =
blanchet@42602
    64
  let
blanchet@42602
    65
    val path = file_name |> Path.explode
blanchet@42602
    66
    val _ = File.write path ""
blanchet@42602
    67
    val axioms = Theory.all_axioms_of thy |> map fst
blanchet@42602
    68
    fun do_thm thm =
blanchet@42602
    69
      let
blanchet@42602
    70
        val name = Thm.get_name_hint thm
blanchet@42602
    71
        val s =
blanchet@42602
    72
          "[" ^ Thm.legacy_get_kind thm ^ "] " ^
blanchet@42602
    73
          (if member (op =) axioms name then "A" else "T") ^ " " ^
blanchet@43479
    74
          prefix fact_prefix (ascii_of name) ^ ": " ^
blanchet@43479
    75
          commas (theorems_mentioned_in_proof_term NONE thm) ^ "; " ^
blanchet@43479
    76
          commas (map (prefix const_prefix o ascii_of)
blanchet@42602
    77
                      (interesting_const_names ctxt (Thm.prop_of thm))) ^ " \n"
blanchet@42602
    78
      in File.append path s end
blanchet@43245
    79
    val thms = facts_of thy |> map snd
blanchet@42602
    80
    val _ = map do_thm thms
blanchet@42602
    81
  in () end
blanchet@42602
    82
blanchet@42602
    83
fun inference_term [] = NONE
blanchet@42602
    84
  | inference_term ss =
blanchet@43479
    85
    ATerm ("inference",
blanchet@43479
    86
           [ATerm ("isabelle", []),
blanchet@43479
    87
            ATerm (tptp_empty_list, []),
blanchet@43479
    88
            ATerm (tptp_empty_list, map (fn s => ATerm (s, [])) ss)])
blanchet@42602
    89
    |> SOME
blanchet@42602
    90
fun inference infers ident =
blanchet@42602
    91
  these (AList.lookup (op =) infers ident) |> inference_term
blanchet@42602
    92
fun add_inferences_to_problem_line infers
blanchet@43479
    93
                                   (Formula (ident, Axiom, phi, NONE, NONE)) =
blanchet@43479
    94
    Formula (ident, Lemma, phi, inference infers ident, NONE)
blanchet@42602
    95
  | add_inferences_to_problem_line _ line = line
blanchet@42602
    96
val add_inferences_to_problem =
blanchet@42602
    97
  map o apsnd o map o add_inferences_to_problem_line
blanchet@42602
    98
blanchet@43479
    99
fun ident_of_problem_line (Decl (ident, _, _)) = ident
blanchet@43479
   100
  | ident_of_problem_line (Formula (ident, _, _, _, _)) = ident
blanchet@43479
   101
blanchet@43400
   102
fun generate_tptp_inference_file_for_theory ctxt thy type_sys file_name =
blanchet@42602
   103
  let
blanchet@43479
   104
    val format = FOF
blanchet@43479
   105
    val type_sys = type_sys |> type_sys_from_string
blanchet@42602
   106
    val path = file_name |> Path.explode
blanchet@42602
   107
    val _ = File.write path ""
blanchet@42602
   108
    val facts0 = facts_of thy
blanchet@42602
   109
    val facts =
blanchet@43245
   110
      facts0 |> map (fn ((_, loc), th) =>
blanchet@43222
   111
                        ((Thm.get_name_hint th, loc), prop_of th))
blanchet@42881
   112
    val (atp_problem, _, _, _, _, _, _) =
blanchet@43479
   113
      prepare_atp_problem ctxt format Axiom Axiom type_sys false false true []
blanchet@43479
   114
                          @{prop False} facts
blanchet@43479
   115
    val all_names = facts0 |> map (Thm.get_name_hint o snd)
blanchet@42602
   116
    val infers =
blanchet@43245
   117
      facts0 |> map (fn (_, th) =>
blanchet@42602
   118
                        (fact_name_of (Thm.get_name_hint th),
blanchet@43479
   119
                         theorems_mentioned_in_proof_term (SOME all_names) th))
blanchet@43479
   120
    val all_atp_problem_names =
blanchet@43479
   121
      atp_problem |> maps (map ident_of_problem_line o snd)
blanchet@42602
   122
    val infers =
blanchet@43479
   123
      infers |> map (apsnd (filter (member (op =) all_atp_problem_names)))
blanchet@42602
   124
    val atp_problem = atp_problem |> add_inferences_to_problem infers
blanchet@43479
   125
    val ss = tptp_lines_for_atp_problem FOF atp_problem
blanchet@42602
   126
    val _ = app (File.append path) ss
blanchet@42602
   127
  in () end
blanchet@42602
   128
blanchet@42602
   129
end;