src/HOL/TPTP/atp_theory_export.ML
author blanchet
Wed Jul 18 08:44:04 2012 +0200 (2012-07-18)
changeset 48315 82d6e46c673f
parent 48301 e5c5037a3104
child 48316 252f45c04042
permissions -rw-r--r--
fixed order of accessibles + other tweaks to MaSh
blanchet@46321
     1
(*  Title:      HOL/TPTP/atp_theory_export.ML
blanchet@42602
     2
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@42602
     3
    Copyright   2011
blanchet@42602
     4
blanchet@48234
     5
Export Isabelle theories as first-order TPTP inferences.
blanchet@42602
     6
*)
blanchet@42602
     7
blanchet@46321
     8
signature ATP_THEORY_EXPORT =
blanchet@42602
     9
sig
blanchet@45305
    10
  type atp_format = ATP_Problem.atp_format
blanchet@45305
    11
blanchet@48234
    12
  val generate_atp_inference_file_for_theory :
blanchet@45305
    13
    Proof.context -> theory -> atp_format -> string -> string -> unit
blanchet@42602
    14
end;
blanchet@42602
    15
blanchet@48234
    16
structure ATP_Theory_Export : ATP_THEORY_EXPORT =
blanchet@42602
    17
struct
blanchet@42602
    18
blanchet@43479
    19
open ATP_Problem
blanchet@43566
    20
open ATP_Proof
blanchet@46320
    21
open ATP_Problem_Generate
blanchet@43566
    22
open ATP_Systems
blanchet@42602
    23
blanchet@48234
    24
val fact_name_of = prefix fact_prefix o ascii_of
blanchet@42602
    25
blanchet@42602
    26
fun inference_term [] = NONE
blanchet@42602
    27
  | inference_term ss =
blanchet@48132
    28
    ATerm (("inference", []),
blanchet@48132
    29
           [ATerm (("isabelle", []), []),
blanchet@48132
    30
            ATerm ((tptp_empty_list, []), []),
blanchet@48132
    31
            ATerm ((tptp_empty_list, []),
blanchet@48132
    32
            map (fn s => ATerm ((s, []), [])) ss)])
blanchet@42602
    33
    |> SOME
blanchet@42602
    34
fun inference infers ident =
blanchet@42602
    35
  these (AList.lookup (op =) infers ident) |> inference_term
blanchet@42602
    36
fun add_inferences_to_problem_line infers
blanchet@46406
    37
                                   (Formula (ident, Axiom, phi, NONE, tms)) =
blanchet@46406
    38
    Formula (ident, Lemma, phi, inference infers ident, tms)
blanchet@42602
    39
  | add_inferences_to_problem_line _ line = line
blanchet@43996
    40
fun add_inferences_to_problem infers =
blanchet@43996
    41
  map (apsnd (map (add_inferences_to_problem_line infers)))
blanchet@42602
    42
blanchet@48142
    43
fun ident_of_problem_line (Class_Decl (ident, _, _)) = ident
blanchet@48142
    44
  | ident_of_problem_line (Type_Decl (ident, _, _)) = ident
blanchet@48137
    45
  | ident_of_problem_line (Sym_Decl (ident, _, _)) = ident
blanchet@48142
    46
  | ident_of_problem_line (Class_Memb (ident, _, _, _)) = ident
blanchet@43479
    47
  | ident_of_problem_line (Formula (ident, _, _, _, _)) = ident
blanchet@43479
    48
blanchet@45305
    49
fun run_some_atp ctxt format problem =
blanchet@43566
    50
  let
blanchet@43566
    51
    val thy = Proof_Context.theory_of ctxt
blanchet@47055
    52
    val prob_file = File.tmp_path (Path.explode "prob")
blanchet@48301
    53
    val atp =
blanchet@48301
    54
      case format of
blanchet@48301
    55
        DFG Polymorphic => spass_polyN
blanchet@48301
    56
      | DFG Monomorphic => spassN
blanchet@48301
    57
      | _ => eN
blanchet@47606
    58
    val {exec, arguments, proof_delims, known_failures, ...} =
blanchet@47606
    59
      get_atp thy atp ()
blanchet@47038
    60
    val ord = effective_term_order ctxt atp
blanchet@47038
    61
    val _ = problem |> lines_for_atp_problem format ord (K [])
blanchet@46442
    62
                    |> File.write_list prob_file
blanchet@48213
    63
    val path = getenv (List.last (fst exec)) ^ "/" ^ snd exec
blanchet@43566
    64
    val command =
blanchet@48213
    65
      File.shell_path (Path.explode path) ^
blanchet@47038
    66
      " " ^ arguments ctxt false "" (seconds 1.0) (ord, K [], K []) ^ " " ^
blanchet@43566
    67
      File.shell_path prob_file
blanchet@43566
    68
  in
wenzelm@43850
    69
    TimeLimit.timeLimit (seconds 0.3) Isabelle_System.bash_output command
blanchet@43566
    70
    |> fst
blanchet@47038
    71
    |> extract_tstplike_proof_and_outcome false true proof_delims known_failures
blanchet@43566
    72
    |> snd
blanchet@43566
    73
  end
blanchet@43566
    74
  handle TimeLimit.TimeOut => SOME TimedOut
blanchet@43566
    75
blanchet@48217
    76
val tautology_prefixes =
blanchet@43569
    77
  [@{theory HOL}, @{theory Meson}, @{theory ATP}, @{theory Metis}]
blanchet@43566
    78
  |> map (fact_name_of o Context.theory_name)
blanchet@43566
    79
blanchet@45305
    80
fun is_problem_line_tautology ctxt format (Formula (ident, _, phi, _, _)) =
blanchet@43566
    81
    exists (fn prefix => String.isPrefix prefix ident)
blanchet@48217
    82
           tautology_prefixes andalso
blanchet@45305
    83
    is_none (run_some_atp ctxt format
blanchet@46406
    84
                 [(factsN, [Formula (ident, Conjecture, phi, NONE, [])])])
blanchet@45305
    85
  | is_problem_line_tautology _ _ _ = false
blanchet@43566
    86
blanchet@43499
    87
fun order_facts ord = sort (ord o pairself ident_of_problem_line)
blanchet@43499
    88
fun order_problem_facts _ [] = []
blanchet@43499
    89
  | order_problem_facts ord ((heading, lines) :: problem) =
blanchet@43499
    90
    if heading = factsN then (heading, order_facts ord lines) :: problem
blanchet@43499
    91
    else (heading, lines) :: order_problem_facts ord problem
blanchet@43499
    92
blanchet@45305
    93
(* A fairly random selection of types used for monomorphizing. *)
blanchet@45305
    94
val ground_types =
blanchet@45305
    95
  [@{typ nat}, HOLogic.intT, HOLogic.realT, @{typ "nat => bool"}, @{typ bool},
blanchet@45305
    96
   @{typ unit}]
blanchet@45305
    97
blanchet@45305
    98
fun ground_type_for_tvar _ [] tvar =
blanchet@45305
    99
    raise TYPE ("ground_type_for_sorts", [TVar tvar], [])
blanchet@45305
   100
  | ground_type_for_tvar thy (T :: Ts) tvar =
blanchet@45305
   101
    if can (Sign.typ_match thy (TVar tvar, T)) Vartab.empty then T
blanchet@45305
   102
    else ground_type_for_tvar thy Ts tvar
blanchet@45305
   103
blanchet@45305
   104
fun monomorphize_term ctxt t =
blanchet@45305
   105
  let val thy = Proof_Context.theory_of ctxt in
blanchet@45305
   106
    t |> map_types (map_type_tvar (ground_type_for_tvar thy ground_types))
blanchet@45305
   107
    handle TYPE _ => @{prop True}
blanchet@45305
   108
  end
blanchet@45305
   109
blanchet@48315
   110
fun all_non_tautological_facts_of thy css_table =
blanchet@48315
   111
  Sledgehammer_Fact.all_facts_of thy css_table
blanchet@48315
   112
  |> filter_out ((Sledgehammer_Filter_MaSh.is_likely_tautology orf
blanchet@48315
   113
                  Sledgehammer_Filter_MaSh.is_too_meta) o snd)
blanchet@48315
   114
blanchet@48234
   115
fun generate_atp_inference_file_for_theory ctxt thy format type_enc file_name =
blanchet@42602
   116
  let
blanchet@48299
   117
    val css_table = Sledgehammer_Fact.clasimpset_rule_table_of ctxt
blanchet@48315
   118
    val type_enc =
blanchet@48315
   119
      type_enc |> type_enc_from_string Strict
blanchet@48315
   120
               |> adjust_type_enc format
blanchet@48131
   121
    val mono = not (is_type_enc_polymorphic type_enc)
blanchet@42602
   122
    val path = file_name |> Path.explode
blanchet@42602
   123
    val _ = File.write path ""
blanchet@48315
   124
    val facts = all_non_tautological_facts_of thy css_table
blanchet@45551
   125
    val atp_problem =
blanchet@43576
   126
      facts
blanchet@45305
   127
      |> map (fn ((_, loc), th) =>
blanchet@45305
   128
                 ((Thm.get_name_hint th, loc),
blanchet@45305
   129
                   th |> prop_of |> mono ? monomorphize_term ctxt))
blanchet@47946
   130
      |> prepare_atp_problem ctxt format Axiom type_enc Exporter combsN false
blanchet@47946
   131
                             false true [] @{prop False}
blanchet@45551
   132
      |> #1
blanchet@43566
   133
    val atp_problem =
blanchet@43566
   134
      atp_problem
blanchet@45305
   135
      |> map (apsnd (filter_out (is_problem_line_tautology ctxt format)))
blanchet@48217
   136
    val ths = facts |> map snd
blanchet@48229
   137
    val all_names = ths |> map Thm.get_name_hint
blanchet@42602
   138
    val infers =
blanchet@43576
   139
      facts |> map (fn (_, th) =>
blanchet@43576
   140
                       (fact_name_of (Thm.get_name_hint th),
blanchet@48251
   141
                        th |> Sledgehammer_Util.thms_in_proof (SOME all_names)
blanchet@48234
   142
                           |> map fact_name_of))
blanchet@43479
   143
    val all_atp_problem_names =
blanchet@43479
   144
      atp_problem |> maps (map ident_of_problem_line o snd)
blanchet@42602
   145
    val infers =
blanchet@43499
   146
      infers |> filter (member (op =) all_atp_problem_names o fst)
blanchet@43499
   147
             |> map (apsnd (filter (member (op =) all_atp_problem_names)))
blanchet@43499
   148
    val ordered_names =
blanchet@43499
   149
      String_Graph.empty
blanchet@43499
   150
      |> fold (String_Graph.new_node o rpair ()) all_atp_problem_names
blanchet@43499
   151
      |> fold (fn (to, froms) =>
blanchet@43566
   152
                  fold (fn from => String_Graph.add_edge (from, to)) froms)
blanchet@43566
   153
              infers
blanchet@43499
   154
      |> String_Graph.topological_order
blanchet@43499
   155
    val order_tab =
blanchet@43499
   156
      Symtab.empty
blanchet@43499
   157
      |> fold (Symtab.insert (op =))
blanchet@43499
   158
              (ordered_names ~~ (1 upto length ordered_names))
blanchet@43499
   159
    val name_ord = int_ord o pairself (the o Symtab.lookup order_tab)
blanchet@43499
   160
    val atp_problem =
blanchet@45305
   161
      atp_problem
blanchet@48131
   162
      |> (case format of DFG _ => I | _ => add_inferences_to_problem infers)
blanchet@45305
   163
      |> order_problem_facts name_ord
blanchet@47038
   164
    val ord = effective_term_order ctxt eN (* dummy *)
blanchet@47038
   165
    val ss = lines_for_atp_problem format ord (K []) atp_problem
blanchet@42602
   166
    val _ = app (File.append path) ss
blanchet@42602
   167
  in () end
blanchet@42602
   168
blanchet@42602
   169
end;