src/HOL/TPTP/mash_export.ML
author blanchet
Thu, 26 Jul 2012 10:48:03 +0200
changeset 49544 716ec3458b1d
parent 49453 3e45c98fe127
child 49545 d443166f9520
permissions -rw-r--r--
generate fact name in queries again + use ATP dependencies when possible
blanchet@49249
     1
(*  Title:      HOL/TPTP/mash_export.ML
blanchet@49249
     2
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@49249
     3
    Copyright   2012
blanchet@49249
     4
blanchet@49249
     5
Export Isabelle theory information for MaSh (Machine-learning for Sledgehammer).
blanchet@49249
     6
*)
blanchet@49249
     7
blanchet@49249
     8
signature MASH_EXPORT =
blanchet@49249
     9
sig
blanchet@49266
    10
  type params = Sledgehammer_Provers.params
blanchet@49250
    11
blanchet@49319
    12
  val generate_accessibility : theory -> bool -> string -> unit
blanchet@49333
    13
  val generate_features :
blanchet@49333
    14
    Proof.context -> string -> theory -> bool -> string -> unit
blanchet@49348
    15
  val generate_isar_dependencies :
blanchet@49339
    16
    theory -> bool -> string -> unit
blanchet@49348
    17
  val generate_atp_dependencies :
blanchet@49319
    18
    Proof.context -> params -> theory -> bool -> string -> unit
blanchet@49544
    19
  val generate_commands : Proof.context -> params -> theory -> string -> unit
blanchet@49394
    20
  val generate_mepo_suggestions :
blanchet@49266
    21
    Proof.context -> params -> theory -> int -> string -> unit
blanchet@49249
    22
end;
blanchet@49249
    23
blanchet@49249
    24
structure MaSh_Export : MASH_EXPORT =
blanchet@49249
    25
struct
blanchet@49249
    26
blanchet@49319
    27
open Sledgehammer_Fact
blanchet@49396
    28
open Sledgehammer_MePo
blanchet@49396
    29
open Sledgehammer_MaSh
blanchet@49260
    30
blanchet@49331
    31
fun thy_map_from_facts ths =
blanchet@49331
    32
  ths |> sort (thm_ord o swap o pairself snd)
blanchet@49331
    33
      |> map (snd #> `(theory_of_thm #> Context.theory_name))
blanchet@49331
    34
      |> AList.coalesce (op =)
blanchet@49393
    35
      |> map (apsnd (map nickname_of))
blanchet@49331
    36
blanchet@49331
    37
fun has_thy thy th =
blanchet@49331
    38
  Context.theory_name thy = Context.theory_name (theory_of_thm th)
blanchet@49331
    39
blanchet@49331
    40
fun parent_facts thy thy_map =
blanchet@49331
    41
  let
blanchet@49331
    42
    fun add_last thy =
blanchet@49331
    43
      case AList.lookup (op =) thy_map (Context.theory_name thy) of
blanchet@49331
    44
        SOME (last_fact :: _) => insert (op =) last_fact
blanchet@49331
    45
      | _ => add_parent thy
blanchet@49331
    46
    and add_parent thy = fold add_last (Theory.parents_of thy)
blanchet@49331
    47
  in add_parent thy [] end
blanchet@49331
    48
blanchet@49331
    49
val thy_name_of_fact = hd o Long_Name.explode
blanchet@49319
    50
fun thy_of_fact thy = Context.this_theory thy o thy_name_of_fact
blanchet@49319
    51
blanchet@49453
    52
val all_names = map (rpair () o nickname_of) #> Symtab.make
blanchet@49331
    53
blanchet@49544
    54
fun smart_dependencies_of ctxt params prover facts all_names th =
blanchet@49544
    55
  case atp_dependencies_of ctxt params prover 0 facts all_names th of
blanchet@49544
    56
    SOME deps => deps
blanchet@49544
    57
  | NONE => isar_dependencies_of all_names th |> these
blanchet@49544
    58
blanchet@49319
    59
fun generate_accessibility thy include_thy file_name =
blanchet@49319
    60
  let
blanchet@49319
    61
    val path = file_name |> Path.explode
blanchet@49319
    62
    val _ = File.write path ""
blanchet@49319
    63
    fun do_fact fact prevs =
blanchet@49319
    64
      let
blanchet@49319
    65
        val s = escape_meta fact ^ ": " ^ escape_metas prevs ^ "\n"
blanchet@49319
    66
        val _ = File.append path s
blanchet@49319
    67
      in [fact] end
blanchet@49330
    68
    val thy_map =
blanchet@49409
    69
      all_facts_of (Proof_Context.init_global thy) Termtab.empty
blanchet@49319
    70
      |> not include_thy ? filter_out (has_thy thy o snd)
blanchet@49330
    71
      |> thy_map_from_facts
blanchet@49319
    72
    fun do_thy facts =
blanchet@49319
    73
      let
blanchet@49319
    74
        val thy = thy_of_fact thy (hd facts)
blanchet@49330
    75
        val parents = parent_facts thy thy_map
blanchet@49319
    76
      in fold do_fact facts parents; () end
blanchet@49348
    77
  in fold_rev (fn (_, facts) => fn () => do_thy facts) thy_map () end
blanchet@49319
    78
blanchet@49333
    79
fun generate_features ctxt prover thy include_thy file_name =
blanchet@49319
    80
  let
blanchet@49319
    81
    val path = file_name |> Path.explode
blanchet@49319
    82
    val _ = File.write path ""
blanchet@49319
    83
    val css_table = clasimpset_rule_table_of ctxt
blanchet@49319
    84
    val facts =
blanchet@49409
    85
      all_facts_of (Proof_Context.init_global thy) css_table
blanchet@49319
    86
      |> not include_thy ? filter_out (has_thy thy o snd)
blanchet@49400
    87
    fun do_fact ((_, stature), th) =
blanchet@49319
    88
      let
blanchet@49393
    89
        val name = nickname_of th
blanchet@49333
    90
        val feats =
blanchet@49400
    91
          features_of ctxt prover (theory_of_thm th) stature [prop_of th]
blanchet@49319
    92
        val s = escape_meta name ^ ": " ^ escape_metas feats ^ "\n"
blanchet@49319
    93
      in File.append path s end
blanchet@49319
    94
  in List.app do_fact facts end
blanchet@49319
    95
blanchet@49348
    96
fun generate_isar_dependencies thy include_thy file_name =
blanchet@49319
    97
  let
blanchet@49319
    98
    val path = file_name |> Path.explode
blanchet@49319
    99
    val _ = File.write path ""
blanchet@49319
   100
    val ths =
blanchet@49409
   101
      all_facts_of (Proof_Context.init_global thy) Termtab.empty
blanchet@49319
   102
      |> not include_thy ? filter_out (has_thy thy o snd)
blanchet@49319
   103
      |> map snd
blanchet@49339
   104
    val all_names = all_names ths
blanchet@49319
   105
    fun do_thm th =
blanchet@49319
   106
      let
blanchet@49393
   107
        val name = nickname_of th
blanchet@49419
   108
        val deps = isar_dependencies_of all_names th |> these
blanchet@49319
   109
        val s = escape_meta name ^ ": " ^ escape_metas deps ^ "\n"
blanchet@49319
   110
      in File.append path s end
blanchet@49319
   111
  in List.app do_thm ths end
blanchet@49319
   112
blanchet@49407
   113
fun generate_atp_dependencies ctxt (params as {provers, ...}) thy include_thy
blanchet@49407
   114
                              file_name =
blanchet@49319
   115
  let
blanchet@49319
   116
    val path = file_name |> Path.explode
blanchet@49319
   117
    val _ = File.write path ""
blanchet@49333
   118
    val prover = hd provers
blanchet@49319
   119
    val facts =
blanchet@49409
   120
      all_facts_of (Proof_Context.init_global thy) Termtab.empty
blanchet@49319
   121
      |> not include_thy ? filter_out (has_thy thy o snd)
blanchet@49319
   122
    val ths = facts |> map snd
blanchet@49339
   123
    val all_names = all_names ths
blanchet@49319
   124
    fun do_thm th =
blanchet@49319
   125
      let
blanchet@49393
   126
        val name = nickname_of th
blanchet@49544
   127
        val deps = smart_dependencies_of ctxt params prover facts all_names th
blanchet@49319
   128
        val s = escape_meta name ^ ": " ^ escape_metas deps ^ "\n"
blanchet@49319
   129
      in File.append path s end
blanchet@49319
   130
  in List.app do_thm ths end
blanchet@49319
   131
blanchet@49544
   132
fun generate_commands ctxt (params as {provers, ...}) thy file_name =
blanchet@49249
   133
  let
blanchet@49249
   134
    val path = file_name |> Path.explode
blanchet@49249
   135
    val _ = File.write path ""
blanchet@49544
   136
    val prover = hd provers
blanchet@49314
   137
    val css_table = Sledgehammer_Fact.clasimpset_rule_table_of ctxt
blanchet@49409
   138
    val facts = all_facts_of (Proof_Context.init_global thy) css_table
blanchet@49249
   139
    val (new_facts, old_facts) =
blanchet@49249
   140
      facts |> List.partition (has_thy thy o snd)
blanchet@49249
   141
            |>> sort (thm_ord o pairself snd)
blanchet@49249
   142
    val ths = facts |> map snd
blanchet@49339
   143
    val all_names = all_names ths
blanchet@49400
   144
    fun do_fact ((_, stature), th) prevs =
blanchet@49249
   145
      let
blanchet@49393
   146
        val name = nickname_of th
blanchet@49400
   147
        val feats = features_of ctxt prover thy stature [prop_of th]
blanchet@49544
   148
        val deps = smart_dependencies_of ctxt params prover facts all_names th
blanchet@49249
   149
        val kind = Thm.legacy_get_kind th
blanchet@49544
   150
        val core =
blanchet@49544
   151
          escape_meta name ^ ": " ^ escape_metas prevs ^ "; " ^
blanchet@49544
   152
          escape_metas feats
blanchet@49249
   153
        val query = if kind <> "" then "? " ^ core ^ "\n" else ""
blanchet@49544
   154
        val update = "! " ^ core ^ "; " ^ escape_metas deps ^ "\n"
blanchet@49249
   155
        val _ = File.append path (query ^ update)
blanchet@49249
   156
      in [name] end
blanchet@49330
   157
    val thy_map = old_facts |> thy_map_from_facts
blanchet@49330
   158
    val parents = parent_facts thy thy_map
blanchet@49254
   159
  in fold do_fact new_facts parents; () end
blanchet@49254
   160
blanchet@49394
   161
fun generate_mepo_suggestions ctxt (params as {provers, ...}) thy max_relevant
blanchet@49307
   162
                              file_name =
blanchet@49254
   163
  let
blanchet@49254
   164
    val path = file_name |> Path.explode
blanchet@49254
   165
    val _ = File.write path ""
blanchet@49333
   166
    val prover = hd provers
blanchet@49314
   167
    val css_table = Sledgehammer_Fact.clasimpset_rule_table_of ctxt
blanchet@49409
   168
    val facts = all_facts_of (Proof_Context.init_global thy) css_table
blanchet@49254
   169
    val (new_facts, old_facts) =
blanchet@49254
   170
      facts |> List.partition (has_thy thy o snd)
blanchet@49254
   171
            |>> sort (thm_ord o pairself snd)
blanchet@49254
   172
    fun do_fact (fact as (_, th)) old_facts =
blanchet@49254
   173
      let
blanchet@49393
   174
        val name = nickname_of th
blanchet@49265
   175
        val goal = goal_of_thm thy th
blanchet@49307
   176
        val (_, hyp_ts, concl_t) = ATP_Util.strip_subgoal ctxt goal 1
blanchet@49254
   177
        val kind = Thm.legacy_get_kind th
blanchet@49254
   178
        val _ =
blanchet@49254
   179
          if kind <> "" then
blanchet@49254
   180
            let
blanchet@49254
   181
              val suggs =
blanchet@49307
   182
                old_facts
blanchet@49421
   183
                |> Sledgehammer_MePo.mepo_suggested_facts ctxt params prover
blanchet@49396
   184
                       max_relevant NONE hyp_ts concl_t
blanchet@49318
   185
                |> map (fn ((name, _), _) => name ())
blanchet@49318
   186
              val s = escape_meta name ^ ": " ^ escape_metas suggs ^ "\n"
blanchet@49254
   187
            in File.append path s end
blanchet@49254
   188
          else
blanchet@49254
   189
            ()
blanchet@49254
   190
      in fact :: old_facts end
blanchet@49254
   191
  in fold do_fact new_facts old_facts; () end
blanchet@49249
   192
blanchet@49249
   193
end;