src/HOL/TPTP/atp_theory_export.ML
author blanchet
Tue Jul 10 23:36:03 2012 +0200 (2012-07-10)
changeset 48234 06216c789ac9
parent 48233 50e00ee405f8
child 48235 40655464a93b
permissions -rw-r--r--
moved MaSh into own files
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@48234
    11
  type stature = Sledgehammer_Filter.stature
blanchet@45305
    12
blanchet@43494
    13
  val theorems_mentioned_in_proof_term :
blanchet@43494
    14
    string list option -> thm -> string list
blanchet@48234
    15
  val all_facts_of_theory : theory -> (((unit -> string) * stature) * thm) list
blanchet@48234
    16
  val generate_atp_inference_file_for_theory :
blanchet@45305
    17
    Proof.context -> theory -> atp_format -> string -> string -> unit
blanchet@42602
    18
end;
blanchet@42602
    19
blanchet@48234
    20
structure ATP_Theory_Export : ATP_THEORY_EXPORT =
blanchet@42602
    21
struct
blanchet@42602
    22
blanchet@43479
    23
open ATP_Problem
blanchet@43566
    24
open ATP_Proof
blanchet@46320
    25
open ATP_Problem_Generate
blanchet@43566
    26
open ATP_Systems
blanchet@42602
    27
blanchet@48234
    28
val fact_name_of = prefix fact_prefix o ascii_of
blanchet@42602
    29
blanchet@43468
    30
(* FIXME: Similar yet different code in "mirabelle.ML". The code here has a few
blanchet@43468
    31
   fixes that seem to be missing over there; or maybe the two code portions are
blanchet@43468
    32
   not doing the same? *)
blanchet@48213
    33
fun fold_body_thms thm_name f =
blanchet@42602
    34
  let
blanchet@43468
    35
    fun app n (PBody {thms, ...}) =
blanchet@43479
    36
      thms |> fold (fn (_, (name, prop, body)) => fn x =>
blanchet@43479
    37
        let
blanchet@43479
    38
          val body' = Future.join body
blanchet@43479
    39
          val n' =
blanchet@43479
    40
            n + (if name = "" orelse
blanchet@43498
    41
                    (* uncommon case where the proved theorem occurs twice
blanchet@43498
    42
                       (e.g., "Transitive_Closure.trancl_into_trancl") *)
blanchet@48213
    43
                    (n = 1 andalso name = thm_name) then
blanchet@43479
    44
                   0
blanchet@43479
    45
                 else
blanchet@43479
    46
                   1)
blanchet@43479
    47
          val x' = x |> n' <= 1 ? app n' body'
blanchet@43479
    48
        in (x' |> n = 1 ? f (name, prop, body')) end)
blanchet@43479
    49
  in fold (app 0) end
blanchet@42602
    50
blanchet@48213
    51
fun theorems_mentioned_in_proof_term all_names th =
blanchet@42602
    52
  let
blanchet@48213
    53
    val is_name_ok =
blanchet@48213
    54
      case all_names of
blanchet@48213
    55
        SOME names => member (op =) names
blanchet@48213
    56
      | NONE => (fn s => s <> "" andalso not (String.isPrefix "Pure." s))
blanchet@48213
    57
    fun collect (s, _, _) = is_name_ok s ? insert (op =) s
blanchet@42602
    58
    val names =
blanchet@48213
    59
      [] |> fold_body_thms (Thm.get_name_hint th) collect [Thm.proof_body_of th]
blanchet@42602
    60
  in names end
blanchet@42602
    61
blanchet@48234
    62
fun all_facts_of_theory thy =
blanchet@48229
    63
  let val ctxt = Proof_Context.init_global thy in
blanchet@48229
    64
    Sledgehammer_Filter.all_facts ctxt false Symtab.empty true [] []
blanchet@48229
    65
        (Sledgehammer_Filter.clasimpset_rule_table_of ctxt)
blanchet@48229
    66
  end
blanchet@48229
    67
blanchet@42602
    68
fun inference_term [] = NONE
blanchet@42602
    69
  | inference_term ss =
blanchet@48132
    70
    ATerm (("inference", []),
blanchet@48132
    71
           [ATerm (("isabelle", []), []),
blanchet@48132
    72
            ATerm ((tptp_empty_list, []), []),
blanchet@48132
    73
            ATerm ((tptp_empty_list, []),
blanchet@48132
    74
            map (fn s => ATerm ((s, []), [])) ss)])
blanchet@42602
    75
    |> SOME
blanchet@42602
    76
fun inference infers ident =
blanchet@42602
    77
  these (AList.lookup (op =) infers ident) |> inference_term
blanchet@42602
    78
fun add_inferences_to_problem_line infers
blanchet@46406
    79
                                   (Formula (ident, Axiom, phi, NONE, tms)) =
blanchet@46406
    80
    Formula (ident, Lemma, phi, inference infers ident, tms)
blanchet@42602
    81
  | add_inferences_to_problem_line _ line = line
blanchet@43996
    82
fun add_inferences_to_problem infers =
blanchet@43996
    83
  map (apsnd (map (add_inferences_to_problem_line infers)))
blanchet@42602
    84
blanchet@48142
    85
fun ident_of_problem_line (Class_Decl (ident, _, _)) = ident
blanchet@48142
    86
  | ident_of_problem_line (Type_Decl (ident, _, _)) = ident
blanchet@48137
    87
  | ident_of_problem_line (Sym_Decl (ident, _, _)) = ident
blanchet@48142
    88
  | ident_of_problem_line (Class_Memb (ident, _, _, _)) = ident
blanchet@43479
    89
  | ident_of_problem_line (Formula (ident, _, _, _, _)) = ident
blanchet@43479
    90
blanchet@45305
    91
fun run_some_atp ctxt format problem =
blanchet@43566
    92
  let
blanchet@43566
    93
    val thy = Proof_Context.theory_of ctxt
blanchet@47055
    94
    val prob_file = File.tmp_path (Path.explode "prob")
blanchet@48131
    95
    val atp = case format of DFG _ => spassN | _ => eN
blanchet@47606
    96
    val {exec, arguments, proof_delims, known_failures, ...} =
blanchet@47606
    97
      get_atp thy atp ()
blanchet@47038
    98
    val ord = effective_term_order ctxt atp
blanchet@47038
    99
    val _ = problem |> lines_for_atp_problem format ord (K [])
blanchet@46442
   100
                    |> File.write_list prob_file
blanchet@48213
   101
    val path = getenv (List.last (fst exec)) ^ "/" ^ snd exec
blanchet@43566
   102
    val command =
blanchet@48213
   103
      File.shell_path (Path.explode path) ^
blanchet@47038
   104
      " " ^ arguments ctxt false "" (seconds 1.0) (ord, K [], K []) ^ " " ^
blanchet@43566
   105
      File.shell_path prob_file
blanchet@43566
   106
  in
wenzelm@43850
   107
    TimeLimit.timeLimit (seconds 0.3) Isabelle_System.bash_output command
blanchet@43566
   108
    |> fst
blanchet@47038
   109
    |> extract_tstplike_proof_and_outcome false true proof_delims known_failures
blanchet@43566
   110
    |> snd
blanchet@43566
   111
  end
blanchet@43566
   112
  handle TimeLimit.TimeOut => SOME TimedOut
blanchet@43566
   113
blanchet@48217
   114
val tautology_prefixes =
blanchet@43569
   115
  [@{theory HOL}, @{theory Meson}, @{theory ATP}, @{theory Metis}]
blanchet@43566
   116
  |> map (fact_name_of o Context.theory_name)
blanchet@43566
   117
blanchet@45305
   118
fun is_problem_line_tautology ctxt format (Formula (ident, _, phi, _, _)) =
blanchet@43566
   119
    exists (fn prefix => String.isPrefix prefix ident)
blanchet@48217
   120
           tautology_prefixes andalso
blanchet@45305
   121
    is_none (run_some_atp ctxt format
blanchet@46406
   122
                 [(factsN, [Formula (ident, Conjecture, phi, NONE, [])])])
blanchet@45305
   123
  | is_problem_line_tautology _ _ _ = false
blanchet@43566
   124
blanchet@43499
   125
fun order_facts ord = sort (ord o pairself ident_of_problem_line)
blanchet@43499
   126
fun order_problem_facts _ [] = []
blanchet@43499
   127
  | order_problem_facts ord ((heading, lines) :: problem) =
blanchet@43499
   128
    if heading = factsN then (heading, order_facts ord lines) :: problem
blanchet@43499
   129
    else (heading, lines) :: order_problem_facts ord problem
blanchet@43499
   130
blanchet@45305
   131
(* A fairly random selection of types used for monomorphizing. *)
blanchet@45305
   132
val ground_types =
blanchet@45305
   133
  [@{typ nat}, HOLogic.intT, HOLogic.realT, @{typ "nat => bool"}, @{typ bool},
blanchet@45305
   134
   @{typ unit}]
blanchet@45305
   135
blanchet@45305
   136
fun ground_type_for_tvar _ [] tvar =
blanchet@45305
   137
    raise TYPE ("ground_type_for_sorts", [TVar tvar], [])
blanchet@45305
   138
  | ground_type_for_tvar thy (T :: Ts) tvar =
blanchet@45305
   139
    if can (Sign.typ_match thy (TVar tvar, T)) Vartab.empty then T
blanchet@45305
   140
    else ground_type_for_tvar thy Ts tvar
blanchet@45305
   141
blanchet@45305
   142
fun monomorphize_term ctxt t =
blanchet@45305
   143
  let val thy = Proof_Context.theory_of ctxt in
blanchet@45305
   144
    t |> map_types (map_type_tvar (ground_type_for_tvar thy ground_types))
blanchet@45305
   145
    handle TYPE _ => @{prop True}
blanchet@45305
   146
  end
blanchet@45305
   147
blanchet@48234
   148
fun generate_atp_inference_file_for_theory ctxt thy format type_enc file_name =
blanchet@42602
   149
  let
blanchet@46301
   150
    val type_enc = type_enc |> type_enc_from_string Strict
blanchet@45305
   151
                            |> adjust_type_enc format
blanchet@48131
   152
    val mono = not (is_type_enc_polymorphic type_enc)
blanchet@42602
   153
    val path = file_name |> Path.explode
blanchet@42602
   154
    val _ = File.write path ""
blanchet@48234
   155
    val facts = all_facts_of_theory thy
blanchet@45551
   156
    val atp_problem =
blanchet@43576
   157
      facts
blanchet@45305
   158
      |> map (fn ((_, loc), th) =>
blanchet@45305
   159
                 ((Thm.get_name_hint th, loc),
blanchet@45305
   160
                   th |> prop_of |> mono ? monomorphize_term ctxt))
blanchet@47946
   161
      |> prepare_atp_problem ctxt format Axiom type_enc Exporter combsN false
blanchet@47946
   162
                             false true [] @{prop False}
blanchet@45551
   163
      |> #1
blanchet@43566
   164
    val atp_problem =
blanchet@43566
   165
      atp_problem
blanchet@45305
   166
      |> map (apsnd (filter_out (is_problem_line_tautology ctxt format)))
blanchet@48217
   167
    val ths = facts |> map snd
blanchet@48229
   168
    val all_names = ths |> map Thm.get_name_hint
blanchet@42602
   169
    val infers =
blanchet@43576
   170
      facts |> map (fn (_, th) =>
blanchet@43576
   171
                       (fact_name_of (Thm.get_name_hint th),
blanchet@48234
   172
                        th |> theorems_mentioned_in_proof_term (SOME all_names)
blanchet@48234
   173
                           |> map fact_name_of))
blanchet@43479
   174
    val all_atp_problem_names =
blanchet@43479
   175
      atp_problem |> maps (map ident_of_problem_line o snd)
blanchet@42602
   176
    val infers =
blanchet@43499
   177
      infers |> filter (member (op =) all_atp_problem_names o fst)
blanchet@43499
   178
             |> map (apsnd (filter (member (op =) all_atp_problem_names)))
blanchet@43499
   179
    val ordered_names =
blanchet@43499
   180
      String_Graph.empty
blanchet@43499
   181
      |> fold (String_Graph.new_node o rpair ()) all_atp_problem_names
blanchet@43499
   182
      |> fold (fn (to, froms) =>
blanchet@43566
   183
                  fold (fn from => String_Graph.add_edge (from, to)) froms)
blanchet@43566
   184
              infers
blanchet@43499
   185
      |> String_Graph.topological_order
blanchet@43499
   186
    val order_tab =
blanchet@43499
   187
      Symtab.empty
blanchet@43499
   188
      |> fold (Symtab.insert (op =))
blanchet@43499
   189
              (ordered_names ~~ (1 upto length ordered_names))
blanchet@43499
   190
    val name_ord = int_ord o pairself (the o Symtab.lookup order_tab)
blanchet@43499
   191
    val atp_problem =
blanchet@45305
   192
      atp_problem
blanchet@48131
   193
      |> (case format of DFG _ => I | _ => add_inferences_to_problem infers)
blanchet@45305
   194
      |> order_problem_facts name_ord
blanchet@47038
   195
    val ord = effective_term_order ctxt eN (* dummy *)
blanchet@47038
   196
    val ss = lines_for_atp_problem format ord (K []) atp_problem
blanchet@42602
   197
    val _ = app (File.append path) ss
blanchet@42602
   198
  in () end
blanchet@42602
   199
blanchet@42602
   200
end;