src/HOL/TPTP/atp_theory_export.ML
author blanchet
Tue, 28 Feb 2012 15:54:51 +0100
changeset 47601 6256e064f8fa
parent 47544 318b669fe660
child 47909 2409b484e1cc
permissions -rw-r--r--
speed up Sledgehammer's clasimpset lookup a bit
     1 (*  Title:      HOL/TPTP/atp_theory_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_THEORY_EXPORT =
    10 sig
    11   type atp_format = ATP_Problem.atp_format
    12 
    13   val theorems_mentioned_in_proof_term :
    14     string list option -> thm -> string list
    15   val generate_tptp_graph_file_for_theory :
    16     Proof.context -> theory -> string -> unit
    17   val generate_tptp_inference_file_for_theory :
    18     Proof.context -> theory -> atp_format -> string -> string -> unit
    19 end;
    20 
    21 structure ATP_Theory_Export : ATP_THEORY_EXPORT =
    22 struct
    23 
    24 open ATP_Problem
    25 open ATP_Proof
    26 open ATP_Problem_Generate
    27 open ATP_Systems
    28 
    29 val fact_name_of = prefix fact_prefix o ascii_of
    30 
    31 fun facts_of thy =
    32   let val ctxt = Proof_Context.init_global thy in
    33     Sledgehammer_Filter.all_facts ctxt false
    34         Symtab.empty true [] []
    35         (Sledgehammer_Filter.clasimpset_rule_table_of ctxt)
    36     |> filter (curry (op =) @{typ bool} o fastype_of
    37                o Object_Logic.atomize_term thy o prop_of o snd)
    38   end
    39 
    40 (* FIXME: Similar yet different code in "mirabelle.ML". The code here has a few
    41    fixes that seem to be missing over there; or maybe the two code portions are
    42    not doing the same? *)
    43 fun fold_body_thms thm_name all_names f =
    44   let
    45     fun app n (PBody {thms, ...}) =
    46       thms |> fold (fn (_, (name, prop, body)) => fn x =>
    47         let
    48           val body' = Future.join body
    49           val n' =
    50             n + (if name = "" orelse
    51                     (is_some all_names andalso
    52                      not (member (op =) (the all_names) name)) orelse
    53                     (* uncommon case where the proved theorem occurs twice
    54                        (e.g., "Transitive_Closure.trancl_into_trancl") *)
    55                     n = 1 andalso name = thm_name then
    56                    0
    57                  else
    58                    1)
    59           val x' = x |> n' <= 1 ? app n' body'
    60         in (x' |> n = 1 ? f (name, prop, body')) end)
    61   in fold (app 0) end
    62 
    63 fun theorems_mentioned_in_proof_term all_names thm =
    64   let
    65     fun collect (s, _, _) = if s <> "" then insert (op =) s else I
    66     val names =
    67       [] |> fold_body_thms (Thm.get_name_hint thm) all_names collect
    68                            [Thm.proof_body_of thm]
    69          |> map fact_name_of
    70   in names end
    71 
    72 fun interesting_const_names ctxt =
    73   let val thy = Proof_Context.theory_of ctxt in
    74     Sledgehammer_Filter.const_names_in_fact thy
    75         (Sledgehammer_Provers.is_built_in_const_for_prover ctxt eN)
    76   end
    77 
    78 fun generate_tptp_graph_file_for_theory ctxt thy file_name =
    79   let
    80     val path = file_name |> Path.explode
    81     val _ = File.write path ""
    82     val axioms = Theory.all_axioms_of thy |> map fst
    83     fun do_thm thm =
    84       let
    85         val name = Thm.get_name_hint thm
    86         val s =
    87           "[" ^ Thm.legacy_get_kind thm ^ "] " ^
    88           (if member (op =) axioms name then "A" else "T") ^ " " ^
    89           prefix fact_prefix (ascii_of name) ^ ": " ^
    90           commas (theorems_mentioned_in_proof_term NONE thm) ^ "; " ^
    91           commas (map (prefix const_prefix o ascii_of)
    92                       (interesting_const_names ctxt (Thm.prop_of thm))) ^ " \n"
    93       in File.append path s end
    94     val thms = facts_of thy |> map snd
    95     val _ = map do_thm thms
    96   in () end
    97 
    98 fun inference_term [] = NONE
    99   | inference_term ss =
   100     ATerm ("inference",
   101            [ATerm ("isabelle", []),
   102             ATerm (tptp_empty_list, []),
   103             ATerm (tptp_empty_list, map (fn s => ATerm (s, [])) ss)])
   104     |> SOME
   105 fun inference infers ident =
   106   these (AList.lookup (op =) infers ident) |> inference_term
   107 fun add_inferences_to_problem_line infers
   108                                    (Formula (ident, Axiom, phi, NONE, tms)) =
   109     Formula (ident, Lemma, phi, inference infers ident, tms)
   110   | add_inferences_to_problem_line _ line = line
   111 fun add_inferences_to_problem infers =
   112   map (apsnd (map (add_inferences_to_problem_line infers)))
   113 
   114 fun ident_of_problem_line (Decl (ident, _, _)) = ident
   115   | ident_of_problem_line (Formula (ident, _, _, _, _)) = ident
   116 
   117 fun run_some_atp ctxt format problem =
   118   let
   119     val thy = Proof_Context.theory_of ctxt
   120     val prob_file = File.tmp_path (Path.explode "prob.tptp")
   121     val {exec, arguments, proof_delims, known_failures, ...} =
   122       get_atp thy (case format of DFG _ => spassN | _ => eN)
   123     val _ = problem |> lines_for_atp_problem format (K [])
   124                     |> File.write_list prob_file
   125     val command =
   126       File.shell_path (Path.explode (getenv (fst exec) ^ "/" ^ snd exec)) ^
   127       " " ^ arguments ctxt false "" (seconds 1.0) (K []) ^ " " ^
   128       File.shell_path prob_file
   129   in
   130     TimeLimit.timeLimit (seconds 0.3) Isabelle_System.bash_output command
   131     |> fst
   132     |> extract_tstplike_proof_and_outcome false true proof_delims
   133                                           known_failures
   134     |> snd
   135   end
   136   handle TimeLimit.TimeOut => SOME TimedOut
   137 
   138 val likely_tautology_prefixes =
   139   [@{theory HOL}, @{theory Meson}, @{theory ATP}, @{theory Metis}]
   140   |> map (fact_name_of o Context.theory_name)
   141 
   142 fun is_problem_line_tautology ctxt format (Formula (ident, _, phi, _, _)) =
   143     exists (fn prefix => String.isPrefix prefix ident)
   144            likely_tautology_prefixes andalso
   145     is_none (run_some_atp ctxt format
   146                  [(factsN, [Formula (ident, Conjecture, phi, NONE, [])])])
   147   | is_problem_line_tautology _ _ _ = false
   148 
   149 fun order_facts ord = sort (ord o pairself ident_of_problem_line)
   150 fun order_problem_facts _ [] = []
   151   | order_problem_facts ord ((heading, lines) :: problem) =
   152     if heading = factsN then (heading, order_facts ord lines) :: problem
   153     else (heading, lines) :: order_problem_facts ord problem
   154 
   155 (* A fairly random selection of types used for monomorphizing. *)
   156 val ground_types =
   157   [@{typ nat}, HOLogic.intT, HOLogic.realT, @{typ "nat => bool"}, @{typ bool},
   158    @{typ unit}]
   159 
   160 fun ground_type_for_tvar _ [] tvar =
   161     raise TYPE ("ground_type_for_sorts", [TVar tvar], [])
   162   | ground_type_for_tvar thy (T :: Ts) tvar =
   163     if can (Sign.typ_match thy (TVar tvar, T)) Vartab.empty then T
   164     else ground_type_for_tvar thy Ts tvar
   165 
   166 fun monomorphize_term ctxt t =
   167   let val thy = Proof_Context.theory_of ctxt in
   168     t |> map_types (map_type_tvar (ground_type_for_tvar thy ground_types))
   169     handle TYPE _ => @{prop True}
   170   end
   171 
   172 fun generate_tptp_inference_file_for_theory ctxt thy format type_enc file_name =
   173   let
   174     val type_enc = type_enc |> type_enc_from_string Strict
   175                             |> adjust_type_enc format
   176     val mono = polymorphism_of_type_enc type_enc <> Polymorphic
   177     val path = file_name |> Path.explode
   178     val _ = File.write path ""
   179     val facts = facts_of thy
   180     val atp_problem =
   181       facts
   182       |> map (fn ((_, loc), th) =>
   183                  ((Thm.get_name_hint th, loc),
   184                    th |> prop_of |> mono ? monomorphize_term ctxt))
   185       |> prepare_atp_problem ctxt format Axiom Axiom type_enc true combsN false
   186                              false true [] @{prop False}
   187       |> #1
   188     val atp_problem =
   189       atp_problem
   190       |> map (apsnd (filter_out (is_problem_line_tautology ctxt format)))
   191     val all_names = facts |> map (Thm.get_name_hint o snd)
   192     val infers =
   193       facts |> map (fn (_, th) =>
   194                        (fact_name_of (Thm.get_name_hint th),
   195                         theorems_mentioned_in_proof_term (SOME all_names) th))
   196     val all_atp_problem_names =
   197       atp_problem |> maps (map ident_of_problem_line o snd)
   198     val infers =
   199       infers |> filter (member (op =) all_atp_problem_names o fst)
   200              |> map (apsnd (filter (member (op =) all_atp_problem_names)))
   201     val ordered_names =
   202       String_Graph.empty
   203       |> fold (String_Graph.new_node o rpair ()) all_atp_problem_names
   204       |> fold (fn (to, froms) =>
   205                   fold (fn from => String_Graph.add_edge (from, to)) froms)
   206               infers
   207       |> String_Graph.topological_order
   208     val order_tab =
   209       Symtab.empty
   210       |> fold (Symtab.insert (op =))
   211               (ordered_names ~~ (1 upto length ordered_names))
   212     val name_ord = int_ord o pairself (the o Symtab.lookup order_tab)
   213     val atp_problem =
   214       atp_problem
   215       |> (case format of DFG _ => I | _ => add_inferences_to_problem infers)
   216       |> order_problem_facts name_ord
   217     val ss = lines_for_atp_problem format (K []) atp_problem
   218     val _ = app (File.append path) ss
   219   in () end
   220 
   221 end;