src/HOL/ex/atp_export.ML
author blanchet
Tue, 07 Jun 2011 07:04:53 +0200
changeset 44064 c9e87dc92d9e
parent 44063 src/HOL/ex/tptp_export.ML@d90151a666cc
child 44065 97906dfd39b7
permissions -rw-r--r--
renamed example theory to "ATP_Export", for consistency with its underlying "ATP_" modules
     1 (*  Title:      HOL/ex/atp_export.ML
     2     Author:     Jasmin Blanchette, TU Muenchen
     3     Copyright   2011
     4 
     5 Export Isabelle theories as first-order TPTP inferences, exploiting
     6 Sledgehammer's translation.
     7 *)
     8 
     9 signature ATP_EXPORT =
    10 sig
    11   val generate_tptp_graph_file_for_theory :
    12     Proof.context -> theory -> string -> unit
    13   val generate_tptp_inference_file_for_theory :
    14     Proof.context -> theory -> bool -> string -> unit
    15 end;
    16 
    17 structure ATP_Export : ATP_EXPORT =
    18 struct
    19 
    20 val ascii_of = ATP_Translate.ascii_of
    21 
    22 val fact_name_of = prefix ATP_Translate.fact_prefix o ascii_of
    23 
    24 fun facts_of thy =
    25   Sledgehammer_Filter.all_facts (ProofContext.init_global thy) Symtab.empty
    26                                 true (K true) [] []
    27 
    28 fun fold_body_thms f =
    29   let
    30     fun app n (PBody {thms, ...}) = thms |> fold (fn (i, (name, prop, body)) => fn (x, seen) =>
    31       if Inttab.defined seen i then (x, seen)
    32       else
    33         let
    34           val body' = Future.join body;
    35           val (x', seen') = app (n + (if name = "" then 0 else 1)) body' (x, Inttab.update (i, ()) seen);
    36         in (x' |> n = 1 ? f (name, prop, body'), seen') end);
    37   in fn bodies => fn x => #1 (fold (app 0) bodies (x, Inttab.empty)) end;
    38 
    39 fun theorems_mentioned_in_proof_term thm =
    40   let
    41     fun collect (s, _, _) = if s <> "" then insert (op =) s else I
    42     val names =
    43       [] |> fold_body_thms collect [Thm.proof_body_of thm] |> map fact_name_of
    44   in names end
    45 
    46 fun interesting_const_names ctxt =
    47   let val thy = ProofContext.theory_of ctxt in
    48     Sledgehammer_Filter.const_names_in_fact thy
    49         (Sledgehammer_Provers.is_built_in_const_for_prover ctxt ATP_Systems.eN)
    50   end
    51 
    52 fun generate_tptp_graph_file_for_theory ctxt thy file_name =
    53   let
    54     val path = file_name |> Path.explode
    55     val _ = File.write path ""
    56     val axioms = Theory.all_axioms_of thy |> map fst
    57     fun do_thm thm =
    58       let
    59         val name = Thm.get_name_hint thm
    60         val s =
    61           "[" ^ Thm.legacy_get_kind thm ^ "] " ^
    62           (if member (op =) axioms name then "A" else "T") ^ " " ^
    63           prefix ATP_Translate.fact_prefix (ascii_of name) ^ ": " ^
    64           commas (theorems_mentioned_in_proof_term thm) ^ "; " ^
    65           commas (map (prefix ATP_Translate.const_prefix o ascii_of)
    66                       (interesting_const_names ctxt (Thm.prop_of thm))) ^ " \n"
    67       in File.append path s end
    68     val thms = facts_of thy |> map (snd o snd)
    69     val _ = map do_thm thms
    70   in () end
    71 
    72 fun inference_term [] = NONE
    73   | inference_term ss =
    74     ATP_Problem.ATerm ("inference",
    75            [ATP_Problem.ATerm ("isabelle", []),
    76             ATP_Problem.ATerm ("[]", []),
    77             ATP_Problem.ATerm ("[]",
    78                 map (fn s => ATP_Problem.ATerm (s, [])) ss)])
    79     |> SOME
    80 fun inference infers ident =
    81   these (AList.lookup (op =) infers ident) |> inference_term
    82 fun add_inferences_to_problem_line infers
    83         (ATP_Problem.Formula (ident, ATP_Problem.Axiom, phi, NONE, NONE)) =
    84     ATP_Problem.Formula (ident, ATP_Problem.Lemma, phi, inference infers ident,
    85                          NONE)
    86   | add_inferences_to_problem_line _ line = line
    87 val add_inferences_to_problem =
    88   map o apsnd o map o add_inferences_to_problem_line
    89 
    90 fun generate_tptp_inference_file_for_theory ctxt thy full_types file_name =
    91   let
    92     val format = ATP_Problem.FOF
    93     val type_sys =
    94       ATP_Translate.Preds
    95           (ATP_Translate.Polymorphic,
    96            if full_types then ATP_Translate.All_Types
    97            else ATP_Translate.Const_Arg_Types,
    98            ATP_Translate.Heavyweight)
    99     val path = file_name |> Path.explode
   100     val _ = File.write path ""
   101     val facts0 = facts_of thy
   102     val facts =
   103       facts0 |> map (fn ((_, loc), (_, th)) =>
   104                         ((Thm.get_name_hint th, loc), prop_of th))
   105     val explicit_apply = NONE
   106     val (atp_problem, _, _, _, _, _, _) =
   107       ATP_Translate.prepare_atp_problem ctxt format
   108           ATP_Problem.Axiom ATP_Problem.Axiom type_sys explicit_apply false true
   109           [] @{prop False} facts
   110     val infers =
   111       facts0 |> map (fn (_, (_, th)) =>
   112                         (fact_name_of (Thm.get_name_hint th),
   113                          theorems_mentioned_in_proof_term th))
   114     val infers =
   115       infers |> map (apsnd (filter (member (op = o apsnd fst) infers)))
   116     val atp_problem = atp_problem |> add_inferences_to_problem infers
   117     val ss =
   118       ATP_Problem.tptp_strings_for_atp_problem ATP_Problem.FOF atp_problem
   119     val _ = app (File.append path) ss
   120   in () end
   121 
   122 end;