src/HOL/TPTP/mash_export.ML
author blanchet
Wed, 18 Jul 2012 08:44:03 +0200
changeset 48302 6cf5e58f1185
parent 48300 9910021c80a7
child 48303 f1d135d0ea69
permissions -rw-r--r--
more implementation work on MaSh
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48234
06216c789ac9 moved MaSh into own files
blanchet
parents:
diff changeset
     1
(*  Title:      HOL/TPTP/mash_export.ML
06216c789ac9 moved MaSh into own files
blanchet
parents:
diff changeset
     2
    Author:     Jasmin Blanchette, TU Muenchen
06216c789ac9 moved MaSh into own files
blanchet
parents:
diff changeset
     3
    Copyright   2012
06216c789ac9 moved MaSh into own files
blanchet
parents:
diff changeset
     4
06216c789ac9 moved MaSh into own files
blanchet
parents:
diff changeset
     5
Export Isabelle theory information for MaSh (Machine-learning for Sledgehammer).
06216c789ac9 moved MaSh into own files
blanchet
parents:
diff changeset
     6
*)
06216c789ac9 moved MaSh into own files
blanchet
parents:
diff changeset
     7
06216c789ac9 moved MaSh into own files
blanchet
parents:
diff changeset
     8
signature MASH_EXPORT =
06216c789ac9 moved MaSh into own files
blanchet
parents:
diff changeset
     9
sig
48251
6cdcfbddc077 moved most of MaSh exporter code to Sledgehammer
blanchet
parents: 48250
diff changeset
    10
  type params = Sledgehammer_Provers.params
48235
40655464a93b MaSh evaluation driver
blanchet
parents: 48234
diff changeset
    11
48299
5e5c6616f0fe centrally construct expensive data structures
blanchet
parents: 48296
diff changeset
    12
  val generate_commands : Proof.context -> theory -> string -> unit
48251
6cdcfbddc077 moved most of MaSh exporter code to Sledgehammer
blanchet
parents: 48250
diff changeset
    13
  val generate_iter_suggestions :
6cdcfbddc077 moved most of MaSh exporter code to Sledgehammer
blanchet
parents: 48250
diff changeset
    14
    Proof.context -> params -> theory -> int -> string -> unit
48234
06216c789ac9 moved MaSh into own files
blanchet
parents:
diff changeset
    15
end;
06216c789ac9 moved MaSh into own files
blanchet
parents:
diff changeset
    16
06216c789ac9 moved MaSh into own files
blanchet
parents:
diff changeset
    17
structure MaSh_Export : MASH_EXPORT =
06216c789ac9 moved MaSh into own files
blanchet
parents:
diff changeset
    18
struct
06216c789ac9 moved MaSh into own files
blanchet
parents:
diff changeset
    19
48251
6cdcfbddc077 moved most of MaSh exporter code to Sledgehammer
blanchet
parents: 48250
diff changeset
    20
open Sledgehammer_Filter_MaSh
48245
854a47677335 generate ATP dependencies
blanchet
parents: 48242
diff changeset
    21
48299
5e5c6616f0fe centrally construct expensive data structures
blanchet
parents: 48296
diff changeset
    22
fun generate_commands ctxt thy file_name =
48234
06216c789ac9 moved MaSh into own files
blanchet
parents:
diff changeset
    23
  let
06216c789ac9 moved MaSh into own files
blanchet
parents:
diff changeset
    24
    val path = file_name |> Path.explode
06216c789ac9 moved MaSh into own files
blanchet
parents:
diff changeset
    25
    val _ = File.write path ""
48299
5e5c6616f0fe centrally construct expensive data structures
blanchet
parents: 48296
diff changeset
    26
    val css_table = Sledgehammer_Fact.clasimpset_rule_table_of ctxt
5e5c6616f0fe centrally construct expensive data structures
blanchet
parents: 48296
diff changeset
    27
    val facts = all_non_tautological_facts_of thy css_table
48234
06216c789ac9 moved MaSh into own files
blanchet
parents:
diff changeset
    28
    val (new_facts, old_facts) =
06216c789ac9 moved MaSh into own files
blanchet
parents:
diff changeset
    29
      facts |> List.partition (has_thy thy o snd)
06216c789ac9 moved MaSh into own files
blanchet
parents:
diff changeset
    30
            |>> sort (thm_ord o pairself snd)
06216c789ac9 moved MaSh into own files
blanchet
parents:
diff changeset
    31
    val ths = facts |> map snd
06216c789ac9 moved MaSh into own files
blanchet
parents:
diff changeset
    32
    val all_names = ths |> map Thm.get_name_hint
06216c789ac9 moved MaSh into own files
blanchet
parents:
diff changeset
    33
    fun do_fact ((_, (_, status)), th) prevs =
06216c789ac9 moved MaSh into own files
blanchet
parents:
diff changeset
    34
      let
06216c789ac9 moved MaSh into own files
blanchet
parents:
diff changeset
    35
        val name = Thm.get_name_hint th
48302
6cf5e58f1185 more implementation work on MaSh
blanchet
parents: 48300
diff changeset
    36
        val feats = features_of thy status [prop_of th]
48251
6cdcfbddc077 moved most of MaSh exporter code to Sledgehammer
blanchet
parents: 48250
diff changeset
    37
        val deps = isabelle_dependencies_of all_names th
48234
06216c789ac9 moved MaSh into own files
blanchet
parents:
diff changeset
    38
        val kind = Thm.legacy_get_kind th
06216c789ac9 moved MaSh into own files
blanchet
parents:
diff changeset
    39
        val name = fact_name_of name
06216c789ac9 moved MaSh into own files
blanchet
parents:
diff changeset
    40
        val core =
48300
9910021c80a7 tweak output
blanchet
parents: 48299
diff changeset
    41
          space_implode " " prevs ^ "; " ^ space_implode " " feats
48234
06216c789ac9 moved MaSh into own files
blanchet
parents:
diff changeset
    42
        val query = if kind <> "" then "? " ^ core ^ "\n" else ""
48300
9910021c80a7 tweak output
blanchet
parents: 48299
diff changeset
    43
        val update =
9910021c80a7 tweak output
blanchet
parents: 48299
diff changeset
    44
          "! " ^ name ^ ": " ^ core ^ "; " ^ space_implode " " deps ^ "\n"
48234
06216c789ac9 moved MaSh into own files
blanchet
parents:
diff changeset
    45
        val _ = File.append path (query ^ update)
06216c789ac9 moved MaSh into own files
blanchet
parents:
diff changeset
    46
      in [name] end
06216c789ac9 moved MaSh into own files
blanchet
parents:
diff changeset
    47
    val thy_ths = old_facts |> thms_by_thy
48296
e7f01b7e244e gracefully handle the case of empty theories when going up the accessibility chain
blanchet
parents: 48292
diff changeset
    48
    val parents = parent_facts thy_ths thy
48239
0016290f904c generate Meng--Paulson facts for evaluation purposes
blanchet
parents: 48235
diff changeset
    49
  in fold do_fact new_facts parents; () end
0016290f904c generate Meng--Paulson facts for evaluation purposes
blanchet
parents: 48235
diff changeset
    50
48292
7fcee834c7f5 more code rationalization in relevance filter
blanchet
parents: 48289
diff changeset
    51
fun generate_iter_suggestions ctxt (params as {provers, ...}) thy max_relevant
7fcee834c7f5 more code rationalization in relevance filter
blanchet
parents: 48289
diff changeset
    52
                              file_name =
48239
0016290f904c generate Meng--Paulson facts for evaluation purposes
blanchet
parents: 48235
diff changeset
    53
  let
0016290f904c generate Meng--Paulson facts for evaluation purposes
blanchet
parents: 48235
diff changeset
    54
    val path = file_name |> Path.explode
0016290f904c generate Meng--Paulson facts for evaluation purposes
blanchet
parents: 48235
diff changeset
    55
    val _ = File.write path ""
48299
5e5c6616f0fe centrally construct expensive data structures
blanchet
parents: 48296
diff changeset
    56
    val css_table = Sledgehammer_Fact.clasimpset_rule_table_of ctxt
5e5c6616f0fe centrally construct expensive data structures
blanchet
parents: 48296
diff changeset
    57
    val facts = all_non_tautological_facts_of thy css_table
48239
0016290f904c generate Meng--Paulson facts for evaluation purposes
blanchet
parents: 48235
diff changeset
    58
    val (new_facts, old_facts) =
0016290f904c generate Meng--Paulson facts for evaluation purposes
blanchet
parents: 48235
diff changeset
    59
      facts |> List.partition (has_thy thy o snd)
0016290f904c generate Meng--Paulson facts for evaluation purposes
blanchet
parents: 48235
diff changeset
    60
            |>> sort (thm_ord o pairself snd)
0016290f904c generate Meng--Paulson facts for evaluation purposes
blanchet
parents: 48235
diff changeset
    61
    fun do_fact (fact as (_, th)) old_facts =
0016290f904c generate Meng--Paulson facts for evaluation purposes
blanchet
parents: 48235
diff changeset
    62
      let
0016290f904c generate Meng--Paulson facts for evaluation purposes
blanchet
parents: 48235
diff changeset
    63
        val name = Thm.get_name_hint th
48250
1065c307fafe further ML structure split to permit finer-grained loading/reordering (problem to solve: MaSh needs most of Sledgehammer)
blanchet
parents: 48246
diff changeset
    64
        val goal = goal_of_thm thy th
48292
7fcee834c7f5 more code rationalization in relevance filter
blanchet
parents: 48289
diff changeset
    65
        val (_, hyp_ts, concl_t) = ATP_Util.strip_subgoal ctxt goal 1
48239
0016290f904c generate Meng--Paulson facts for evaluation purposes
blanchet
parents: 48235
diff changeset
    66
        val kind = Thm.legacy_get_kind th
0016290f904c generate Meng--Paulson facts for evaluation purposes
blanchet
parents: 48235
diff changeset
    67
        val _ =
0016290f904c generate Meng--Paulson facts for evaluation purposes
blanchet
parents: 48235
diff changeset
    68
          if kind <> "" then
0016290f904c generate Meng--Paulson facts for evaluation purposes
blanchet
parents: 48235
diff changeset
    69
            let
0016290f904c generate Meng--Paulson facts for evaluation purposes
blanchet
parents: 48235
diff changeset
    70
              val suggs =
48292
7fcee834c7f5 more code rationalization in relevance filter
blanchet
parents: 48289
diff changeset
    71
                old_facts
7fcee834c7f5 more code rationalization in relevance filter
blanchet
parents: 48289
diff changeset
    72
                |> Sledgehammer_Filter_Iter.iterative_relevant_facts ctxt params
7fcee834c7f5 more code rationalization in relevance filter
blanchet
parents: 48289
diff changeset
    73
                                (hd provers) max_relevant NONE hyp_ts concl_t
7fcee834c7f5 more code rationalization in relevance filter
blanchet
parents: 48289
diff changeset
    74
                |> map (fn ((name, _), _) => fact_name_of (name ()))
7fcee834c7f5 more code rationalization in relevance filter
blanchet
parents: 48289
diff changeset
    75
                |> sort string_ord
48239
0016290f904c generate Meng--Paulson facts for evaluation purposes
blanchet
parents: 48235
diff changeset
    76
              val s = fact_name_of name ^ ": " ^ space_implode " " suggs ^ "\n"
0016290f904c generate Meng--Paulson facts for evaluation purposes
blanchet
parents: 48235
diff changeset
    77
            in File.append path s end
0016290f904c generate Meng--Paulson facts for evaluation purposes
blanchet
parents: 48235
diff changeset
    78
          else
0016290f904c generate Meng--Paulson facts for evaluation purposes
blanchet
parents: 48235
diff changeset
    79
            ()
0016290f904c generate Meng--Paulson facts for evaluation purposes
blanchet
parents: 48235
diff changeset
    80
      in fact :: old_facts end
0016290f904c generate Meng--Paulson facts for evaluation purposes
blanchet
parents: 48235
diff changeset
    81
  in fold do_fact new_facts old_facts; () end
48234
06216c789ac9 moved MaSh into own files
blanchet
parents:
diff changeset
    82
06216c789ac9 moved MaSh into own files
blanchet
parents:
diff changeset
    83
end;