src/HOL/TPTP/atp_theory_export.ML
author blanchet
Thu, 26 Jul 2012 10:48:03 +0200
changeset 49545 d443166f9520
parent 49453 3e45c98fe127
child 49731 1d2a12bb0640
permissions -rw-r--r--
repaired accessibility chains generated by MaSh exporter + tuned one function out
blanchet@47149
     1
(*  Title:      HOL/TPTP/atp_theory_export.ML
blanchet@43473
     2
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@43473
     3
    Copyright   2011
blanchet@43473
     4
blanchet@49249
     5
Export Isabelle theories as first-order TPTP inferences.
blanchet@43473
     6
*)
blanchet@43473
     7
blanchet@47149
     8
signature ATP_THEORY_EXPORT =
blanchet@43473
     9
sig
blanchet@46176
    10
  type atp_format = ATP_Problem.atp_format
blanchet@46176
    11
blanchet@49249
    12
  val generate_atp_inference_file_for_theory :
blanchet@46176
    13
    Proof.context -> theory -> atp_format -> string -> string -> unit
blanchet@43473
    14
end;
blanchet@43473
    15
blanchet@49249
    16
structure ATP_Theory_Export : ATP_THEORY_EXPORT =
blanchet@43473
    17
struct
blanchet@43473
    18
blanchet@44350
    19
open ATP_Problem
blanchet@44428
    20
open ATP_Proof
blanchet@47148
    21
open ATP_Problem_Generate
blanchet@44428
    22
open ATP_Systems
blanchet@43473
    23
blanchet@49249
    24
val fact_name_of = prefix fact_prefix o ascii_of
blanchet@43473
    25
blanchet@43473
    26
fun inference_term [] = NONE
blanchet@43473
    27
  | inference_term ss =
blanchet@49147
    28
    ATerm (("inference", []),
blanchet@49147
    29
           [ATerm (("isabelle", []), []),
blanchet@49147
    30
            ATerm ((tptp_empty_list, []), []),
blanchet@49147
    31
            ATerm ((tptp_empty_list, []),
blanchet@49147
    32
            map (fn s => ATerm ((s, []), [])) ss)])
blanchet@43473
    33
    |> SOME
blanchet@43473
    34
fun inference infers ident =
blanchet@43473
    35
  these (AList.lookup (op =) infers ident) |> inference_term
blanchet@43473
    36
fun add_inferences_to_problem_line infers
blanchet@47234
    37
                                   (Formula (ident, Axiom, phi, NONE, tms)) =
blanchet@47234
    38
    Formula (ident, Lemma, phi, inference infers ident, tms)
blanchet@43473
    39
  | add_inferences_to_problem_line _ line = line
blanchet@44867
    40
fun add_inferences_to_problem infers =
blanchet@44867
    41
  map (apsnd (map (add_inferences_to_problem_line infers)))
blanchet@43473
    42
blanchet@49157
    43
fun ident_of_problem_line (Class_Decl (ident, _, _)) = ident
blanchet@49157
    44
  | ident_of_problem_line (Type_Decl (ident, _, _)) = ident
blanchet@49152
    45
  | ident_of_problem_line (Sym_Decl (ident, _, _)) = ident
blanchet@49157
    46
  | ident_of_problem_line (Class_Memb (ident, _, _, _)) = ident
blanchet@44350
    47
  | ident_of_problem_line (Formula (ident, _, _, _, _)) = ident
blanchet@44350
    48
blanchet@49333
    49
fun atp_for_format (THF (Polymorphic, _, _, _)) = dummy_thfN
blanchet@49333
    50
  | atp_for_format (THF (Monomorphic, _, _, _)) = satallaxN
blanchet@49333
    51
  | atp_for_format (DFG Polymorphic) = spass_polyN
blanchet@49333
    52
  | atp_for_format (DFG Monomorphic) = spassN
blanchet@49333
    53
  | atp_for_format (TFF (Polymorphic, _)) = alt_ergoN
blanchet@49333
    54
  | atp_for_format (TFF (Monomorphic, _)) = vampireN
blanchet@49333
    55
  | atp_for_format FOF = eN
blanchet@49333
    56
  | atp_for_format CNF_UEQ = waldmeisterN
blanchet@49333
    57
  | atp_for_format CNF = eN
blanchet@49333
    58
blanchet@46176
    59
fun run_some_atp ctxt format problem =
blanchet@44428
    60
  let
blanchet@44428
    61
    val thy = Proof_Context.theory_of ctxt
blanchet@47925
    62
    val prob_file = File.tmp_path (Path.explode "prob")
blanchet@49333
    63
    val atp = atp_for_format format
blanchet@48469
    64
    val {exec, arguments, proof_delims, known_failures, ...} =
blanchet@48469
    65
      get_atp thy atp ()
blanchet@47909
    66
    val ord = effective_term_order ctxt atp
blanchet@47909
    67
    val _ = problem |> lines_for_atp_problem format ord (K [])
blanchet@47270
    68
                    |> File.write_list prob_file
blanchet@49391
    69
    val path = getenv (List.last (fst exec)) ^ "/" ^ List.last (snd exec)
blanchet@44428
    70
    val command =
blanchet@49228
    71
      File.shell_path (Path.explode path) ^
blanchet@47909
    72
      " " ^ arguments ctxt false "" (seconds 1.0) (ord, K [], K []) ^ " " ^
blanchet@44428
    73
      File.shell_path prob_file
blanchet@44428
    74
  in
wenzelm@44721
    75
    TimeLimit.timeLimit (seconds 0.3) Isabelle_System.bash_output command
blanchet@44428
    76
    |> fst
blanchet@47909
    77
    |> extract_tstplike_proof_and_outcome false true proof_delims known_failures
blanchet@44428
    78
    |> snd
blanchet@44428
    79
  end
blanchet@44428
    80
  handle TimeLimit.TimeOut => SOME TimedOut
blanchet@44428
    81
blanchet@49232
    82
val tautology_prefixes =
blanchet@44431
    83
  [@{theory HOL}, @{theory Meson}, @{theory ATP}, @{theory Metis}]
blanchet@44428
    84
  |> map (fact_name_of o Context.theory_name)
blanchet@44428
    85
blanchet@46176
    86
fun is_problem_line_tautology ctxt format (Formula (ident, _, phi, _, _)) =
blanchet@44428
    87
    exists (fn prefix => String.isPrefix prefix ident)
blanchet@49232
    88
           tautology_prefixes andalso
blanchet@46176
    89
    is_none (run_some_atp ctxt format
blanchet@47234
    90
                 [(factsN, [Formula (ident, Conjecture, phi, NONE, [])])])
blanchet@46176
    91
  | is_problem_line_tautology _ _ _ = false
blanchet@44428
    92
blanchet@44370
    93
fun order_facts ord = sort (ord o pairself ident_of_problem_line)
blanchet@44370
    94
fun order_problem_facts _ [] = []
blanchet@44370
    95
  | order_problem_facts ord ((heading, lines) :: problem) =
blanchet@44370
    96
    if heading = factsN then (heading, order_facts ord lines) :: problem
blanchet@44370
    97
    else (heading, lines) :: order_problem_facts ord problem
blanchet@44370
    98
blanchet@46176
    99
(* A fairly random selection of types used for monomorphizing. *)
blanchet@46176
   100
val ground_types =
blanchet@46176
   101
  [@{typ nat}, HOLogic.intT, HOLogic.realT, @{typ "nat => bool"}, @{typ bool},
blanchet@46176
   102
   @{typ unit}]
blanchet@46176
   103
blanchet@46176
   104
fun ground_type_for_tvar _ [] tvar =
blanchet@46176
   105
    raise TYPE ("ground_type_for_sorts", [TVar tvar], [])
blanchet@46176
   106
  | ground_type_for_tvar thy (T :: Ts) tvar =
blanchet@46176
   107
    if can (Sign.typ_match thy (TVar tvar, T)) Vartab.empty then T
blanchet@46176
   108
    else ground_type_for_tvar thy Ts tvar
blanchet@46176
   109
blanchet@46176
   110
fun monomorphize_term ctxt t =
blanchet@46176
   111
  let val thy = Proof_Context.theory_of ctxt in
blanchet@46176
   112
    t |> map_types (map_type_tvar (ground_type_for_tvar thy ground_types))
blanchet@46176
   113
    handle TYPE _ => @{prop True}
blanchet@46176
   114
  end
blanchet@46176
   115
blanchet@49249
   116
fun generate_atp_inference_file_for_theory ctxt thy format type_enc file_name =
blanchet@43473
   117
  let
blanchet@49314
   118
    val css_table = Sledgehammer_Fact.clasimpset_rule_table_of ctxt
blanchet@49330
   119
    val type_enc =
blanchet@49330
   120
      type_enc |> type_enc_from_string Strict
blanchet@49330
   121
               |> adjust_type_enc format
blanchet@49146
   122
    val mono = not (is_type_enc_polymorphic type_enc)
blanchet@43473
   123
    val path = file_name |> Path.explode
blanchet@43473
   124
    val _ = File.write path ""
blanchet@49453
   125
    val facts =
blanchet@49545
   126
      Sledgehammer_Fact.all_facts (Proof_Context.init_global thy) false
blanchet@49545
   127
                                  Symtab.empty [] [] css_table
blanchet@46422
   128
    val atp_problem =
blanchet@44438
   129
      facts
blanchet@46176
   130
      |> map (fn ((_, loc), th) =>
blanchet@46176
   131
                 ((Thm.get_name_hint th, loc),
blanchet@46176
   132
                   th |> prop_of |> mono ? monomorphize_term ctxt))
blanchet@48961
   133
      |> prepare_atp_problem ctxt format Axiom type_enc Exporter combsN false
blanchet@48961
   134
                             false true [] @{prop False}
blanchet@46422
   135
      |> #1
blanchet@44428
   136
    val atp_problem =
blanchet@44428
   137
      atp_problem
blanchet@46176
   138
      |> map (apsnd (filter_out (is_problem_line_tautology ctxt format)))
blanchet@49232
   139
    val ths = facts |> map snd
blanchet@49331
   140
    val all_names = ths |> map (rpair () o Thm.get_name_hint) |> Symtab.make
blanchet@43473
   141
    val infers =
blanchet@44438
   142
      facts |> map (fn (_, th) =>
blanchet@44438
   143
                       (fact_name_of (Thm.get_name_hint th),
blanchet@49266
   144
                        th |> Sledgehammer_Util.thms_in_proof (SOME all_names)
blanchet@49249
   145
                           |> map fact_name_of))
blanchet@44350
   146
    val all_atp_problem_names =
blanchet@44350
   147
      atp_problem |> maps (map ident_of_problem_line o snd)
blanchet@43473
   148
    val infers =
blanchet@44370
   149
      infers |> filter (member (op =) all_atp_problem_names o fst)
blanchet@44370
   150
             |> map (apsnd (filter (member (op =) all_atp_problem_names)))
blanchet@44370
   151
    val ordered_names =
blanchet@44370
   152
      String_Graph.empty
blanchet@44370
   153
      |> fold (String_Graph.new_node o rpair ()) all_atp_problem_names
blanchet@44370
   154
      |> fold (fn (to, froms) =>
blanchet@44428
   155
                  fold (fn from => String_Graph.add_edge (from, to)) froms)
blanchet@44428
   156
              infers
blanchet@44370
   157
      |> String_Graph.topological_order
blanchet@44370
   158
    val order_tab =
blanchet@44370
   159
      Symtab.empty
blanchet@44370
   160
      |> fold (Symtab.insert (op =))
blanchet@44370
   161
              (ordered_names ~~ (1 upto length ordered_names))
blanchet@44370
   162
    val name_ord = int_ord o pairself (the o Symtab.lookup order_tab)
blanchet@44370
   163
    val atp_problem =
blanchet@46176
   164
      atp_problem
blanchet@49146
   165
      |> (case format of DFG _ => I | _ => add_inferences_to_problem infers)
blanchet@46176
   166
      |> order_problem_facts name_ord
blanchet@47909
   167
    val ord = effective_term_order ctxt eN (* dummy *)
blanchet@47909
   168
    val ss = lines_for_atp_problem format ord (K []) atp_problem
blanchet@43473
   169
    val _ = app (File.append path) ss
blanchet@43473
   170
  in () end
blanchet@43473
   171
blanchet@43473
   172
end;