author | blanchet |
Fri, 20 Jul 2012 22:19:46 +0200 | |
changeset 48406 | b002cc16aa99 |
parent 48404 | 0a261b4aa093 |
child 48438 | 3e45c98fe127 |
permissions | -rw-r--r-- |
48234 | 1 |
(* Title: HOL/TPTP/mash_export.ML |
2 |
Author: Jasmin Blanchette, TU Muenchen |
|
3 |
Copyright 2012 |
|
4 |
||
5 |
Export Isabelle theory information for MaSh (Machine-learning for Sledgehammer). |
|
6 |
*) |
|
7 |
||
8 |
signature MASH_EXPORT = |
|
9 |
sig |
|
48251
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48250
diff
changeset
|
10 |
type params = Sledgehammer_Provers.params |
48235 | 11 |
|
48304 | 12 |
val generate_accessibility : theory -> bool -> string -> unit |
48318 | 13 |
val generate_features : |
14 |
Proof.context -> string -> theory -> bool -> string -> unit |
|
48333 | 15 |
val generate_isar_dependencies : |
48324 | 16 |
theory -> bool -> string -> unit |
48333 | 17 |
val generate_atp_dependencies : |
48304 | 18 |
Proof.context -> params -> theory -> bool -> string -> unit |
48318 | 19 |
val generate_commands : Proof.context -> string -> theory -> string -> unit |
48379
2b5ad61e2ccc
renamed "iter" fact filter to "MePo" (Meng--Paulson)
blanchet
parents:
48378
diff
changeset
|
20 |
val generate_mepo_suggestions : |
48251
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48250
diff
changeset
|
21 |
Proof.context -> params -> theory -> int -> string -> unit |
48234 | 22 |
end; |
23 |
||
24 |
structure MaSh_Export : MASH_EXPORT = |
|
25 |
struct |
|
26 |
||
48304 | 27 |
open Sledgehammer_Fact |
48381 | 28 |
open Sledgehammer_MePo |
29 |
open Sledgehammer_MaSh |
|
48245 | 30 |
|
48316
252f45c04042
drastic overhaul of MaSh data structures + fixed a few performance issues
blanchet
parents:
48315
diff
changeset
|
31 |
fun thy_map_from_facts ths = |
252f45c04042
drastic overhaul of MaSh data structures + fixed a few performance issues
blanchet
parents:
48315
diff
changeset
|
32 |
ths |> sort (thm_ord o swap o pairself snd) |
252f45c04042
drastic overhaul of MaSh data structures + fixed a few performance issues
blanchet
parents:
48315
diff
changeset
|
33 |
|> map (snd #> `(theory_of_thm #> Context.theory_name)) |
252f45c04042
drastic overhaul of MaSh data structures + fixed a few performance issues
blanchet
parents:
48315
diff
changeset
|
34 |
|> AList.coalesce (op =) |
48378 | 35 |
|> map (apsnd (map nickname_of)) |
48316
252f45c04042
drastic overhaul of MaSh data structures + fixed a few performance issues
blanchet
parents:
48315
diff
changeset
|
36 |
|
252f45c04042
drastic overhaul of MaSh data structures + fixed a few performance issues
blanchet
parents:
48315
diff
changeset
|
37 |
fun has_thy thy th = |
252f45c04042
drastic overhaul of MaSh data structures + fixed a few performance issues
blanchet
parents:
48315
diff
changeset
|
38 |
Context.theory_name thy = Context.theory_name (theory_of_thm th) |
252f45c04042
drastic overhaul of MaSh data structures + fixed a few performance issues
blanchet
parents:
48315
diff
changeset
|
39 |
|
252f45c04042
drastic overhaul of MaSh data structures + fixed a few performance issues
blanchet
parents:
48315
diff
changeset
|
40 |
fun parent_facts thy thy_map = |
252f45c04042
drastic overhaul of MaSh data structures + fixed a few performance issues
blanchet
parents:
48315
diff
changeset
|
41 |
let |
252f45c04042
drastic overhaul of MaSh data structures + fixed a few performance issues
blanchet
parents:
48315
diff
changeset
|
42 |
fun add_last thy = |
252f45c04042
drastic overhaul of MaSh data structures + fixed a few performance issues
blanchet
parents:
48315
diff
changeset
|
43 |
case AList.lookup (op =) thy_map (Context.theory_name thy) of |
252f45c04042
drastic overhaul of MaSh data structures + fixed a few performance issues
blanchet
parents:
48315
diff
changeset
|
44 |
SOME (last_fact :: _) => insert (op =) last_fact |
252f45c04042
drastic overhaul of MaSh data structures + fixed a few performance issues
blanchet
parents:
48315
diff
changeset
|
45 |
| _ => add_parent thy |
252f45c04042
drastic overhaul of MaSh data structures + fixed a few performance issues
blanchet
parents:
48315
diff
changeset
|
46 |
and add_parent thy = fold add_last (Theory.parents_of thy) |
252f45c04042
drastic overhaul of MaSh data structures + fixed a few performance issues
blanchet
parents:
48315
diff
changeset
|
47 |
in add_parent thy [] end |
252f45c04042
drastic overhaul of MaSh data structures + fixed a few performance issues
blanchet
parents:
48315
diff
changeset
|
48 |
|
252f45c04042
drastic overhaul of MaSh data structures + fixed a few performance issues
blanchet
parents:
48315
diff
changeset
|
49 |
val thy_name_of_fact = hd o Long_Name.explode |
48304 | 50 |
fun thy_of_fact thy = Context.this_theory thy o thy_name_of_fact |
51 |
||
48324 | 52 |
val all_names = |
48333 | 53 |
filter_out is_likely_tautology_or_too_meta |
48378 | 54 |
#> map (rpair () o nickname_of) #> Symtab.make |
48316
252f45c04042
drastic overhaul of MaSh data structures + fixed a few performance issues
blanchet
parents:
48315
diff
changeset
|
55 |
|
48304 | 56 |
fun generate_accessibility thy include_thy file_name = |
57 |
let |
|
58 |
val path = file_name |> Path.explode |
|
59 |
val _ = File.write path "" |
|
60 |
fun do_fact fact prevs = |
|
61 |
let |
|
62 |
val s = escape_meta fact ^ ": " ^ escape_metas prevs ^ "\n" |
|
63 |
val _ = File.append path s |
|
64 |
in [fact] end |
|
48315
82d6e46c673f
fixed order of accessibles + other tweaks to MaSh
blanchet
parents:
48304
diff
changeset
|
65 |
val thy_map = |
48394
82fc8c956cdc
fixed various issues with MaSh's file handling + tune output + generate local facts again + handle nameless facts gracefully
blanchet
parents:
48392
diff
changeset
|
66 |
all_facts_of (Proof_Context.init_global thy) Termtab.empty |
48304 | 67 |
|> not include_thy ? filter_out (has_thy thy o snd) |
48315
82d6e46c673f
fixed order of accessibles + other tweaks to MaSh
blanchet
parents:
48304
diff
changeset
|
68 |
|> thy_map_from_facts |
48304 | 69 |
fun do_thy facts = |
70 |
let |
|
71 |
val thy = thy_of_fact thy (hd facts) |
|
48315
82d6e46c673f
fixed order of accessibles + other tweaks to MaSh
blanchet
parents:
48304
diff
changeset
|
72 |
val parents = parent_facts thy thy_map |
48304 | 73 |
in fold do_fact facts parents; () end |
48333 | 74 |
in fold_rev (fn (_, facts) => fn () => do_thy facts) thy_map () end |
48304 | 75 |
|
48318 | 76 |
fun generate_features ctxt prover thy include_thy file_name = |
48304 | 77 |
let |
78 |
val path = file_name |> Path.explode |
|
79 |
val _ = File.write path "" |
|
80 |
val css_table = clasimpset_rule_table_of ctxt |
|
81 |
val facts = |
|
48394
82fc8c956cdc
fixed various issues with MaSh's file handling + tune output + generate local facts again + handle nameless facts gracefully
blanchet
parents:
48392
diff
changeset
|
82 |
all_facts_of (Proof_Context.init_global thy) css_table |
48304 | 83 |
|> not include_thy ? filter_out (has_thy thy o snd) |
48385 | 84 |
fun do_fact ((_, stature), th) = |
48304 | 85 |
let |
48378 | 86 |
val name = nickname_of th |
48318 | 87 |
val feats = |
48385 | 88 |
features_of ctxt prover (theory_of_thm th) stature [prop_of th] |
48304 | 89 |
val s = escape_meta name ^ ": " ^ escape_metas feats ^ "\n" |
90 |
in File.append path s end |
|
91 |
in List.app do_fact facts end |
|
92 |
||
48333 | 93 |
fun generate_isar_dependencies thy include_thy file_name = |
48304 | 94 |
let |
95 |
val path = file_name |> Path.explode |
|
96 |
val _ = File.write path "" |
|
97 |
val ths = |
|
48394
82fc8c956cdc
fixed various issues with MaSh's file handling + tune output + generate local facts again + handle nameless facts gracefully
blanchet
parents:
48392
diff
changeset
|
98 |
all_facts_of (Proof_Context.init_global thy) Termtab.empty |
48304 | 99 |
|> not include_thy ? filter_out (has_thy thy o snd) |
100 |
|> map snd |
|
48324 | 101 |
val all_names = all_names ths |
48304 | 102 |
fun do_thm th = |
103 |
let |
|
48378 | 104 |
val name = nickname_of th |
48404 | 105 |
val deps = isar_dependencies_of all_names th |> these |
48304 | 106 |
val s = escape_meta name ^ ": " ^ escape_metas deps ^ "\n" |
107 |
in File.append path s end |
|
108 |
in List.app do_thm ths end |
|
109 |
||
48392
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48385
diff
changeset
|
110 |
fun generate_atp_dependencies ctxt (params as {provers, ...}) thy include_thy |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48385
diff
changeset
|
111 |
file_name = |
48304 | 112 |
let |
113 |
val path = file_name |> Path.explode |
|
114 |
val _ = File.write path "" |
|
48318 | 115 |
val prover = hd provers |
48304 | 116 |
val facts = |
48394
82fc8c956cdc
fixed various issues with MaSh's file handling + tune output + generate local facts again + handle nameless facts gracefully
blanchet
parents:
48392
diff
changeset
|
117 |
all_facts_of (Proof_Context.init_global thy) Termtab.empty |
48304 | 118 |
|> not include_thy ? filter_out (has_thy thy o snd) |
119 |
val ths = facts |> map snd |
|
48324 | 120 |
val all_names = all_names ths |
48304 | 121 |
fun do_thm th = |
122 |
let |
|
48378 | 123 |
val name = nickname_of th |
48304 | 124 |
val deps = |
48404 | 125 |
case atp_dependencies_of ctxt params prover 0 facts all_names th of |
126 |
SOME deps => deps |
|
127 |
| NONE => isar_dependencies_of all_names th |> these |
|
48304 | 128 |
val s = escape_meta name ^ ": " ^ escape_metas deps ^ "\n" |
129 |
in File.append path s end |
|
130 |
in List.app do_thm ths end |
|
131 |
||
48318 | 132 |
fun generate_commands ctxt prover thy file_name = |
48234 | 133 |
let |
134 |
val path = file_name |> Path.explode |
|
135 |
val _ = File.write path "" |
|
48299 | 136 |
val css_table = Sledgehammer_Fact.clasimpset_rule_table_of ctxt |
48394
82fc8c956cdc
fixed various issues with MaSh's file handling + tune output + generate local facts again + handle nameless facts gracefully
blanchet
parents:
48392
diff
changeset
|
137 |
val facts = all_facts_of (Proof_Context.init_global thy) css_table |
48234 | 138 |
val (new_facts, old_facts) = |
139 |
facts |> List.partition (has_thy thy o snd) |
|
140 |
|>> sort (thm_ord o pairself snd) |
|
141 |
val ths = facts |> map snd |
|
48324 | 142 |
val all_names = all_names ths |
48385 | 143 |
fun do_fact ((_, stature), th) prevs = |
48234 | 144 |
let |
48378 | 145 |
val name = nickname_of th |
48385 | 146 |
val feats = features_of ctxt prover thy stature [prop_of th] |
48404 | 147 |
val deps = isar_dependencies_of all_names th |> these |
48234 | 148 |
val kind = Thm.legacy_get_kind th |
48304 | 149 |
val core = escape_metas prevs ^ "; " ^ escape_metas feats |
48234 | 150 |
val query = if kind <> "" then "? " ^ core ^ "\n" else "" |
48300 | 151 |
val update = |
48303
f1d135d0ea69
improved MaSh string escaping and make more operations string-based
blanchet
parents:
48302
diff
changeset
|
152 |
"! " ^ escape_meta name ^ ": " ^ core ^ "; " ^ |
48304 | 153 |
escape_metas deps ^ "\n" |
48234 | 154 |
val _ = File.append path (query ^ update) |
155 |
in [name] end |
|
48315
82d6e46c673f
fixed order of accessibles + other tweaks to MaSh
blanchet
parents:
48304
diff
changeset
|
156 |
val thy_map = old_facts |> thy_map_from_facts |
82d6e46c673f
fixed order of accessibles + other tweaks to MaSh
blanchet
parents:
48304
diff
changeset
|
157 |
val parents = parent_facts thy thy_map |
48239
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
158 |
in fold do_fact new_facts parents; () end |
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
159 |
|
48379
2b5ad61e2ccc
renamed "iter" fact filter to "MePo" (Meng--Paulson)
blanchet
parents:
48378
diff
changeset
|
160 |
fun generate_mepo_suggestions ctxt (params as {provers, ...}) thy max_relevant |
48292 | 161 |
file_name = |
48239
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
162 |
let |
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
163 |
val path = file_name |> Path.explode |
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
164 |
val _ = File.write path "" |
48318 | 165 |
val prover = hd provers |
48299 | 166 |
val css_table = Sledgehammer_Fact.clasimpset_rule_table_of ctxt |
48394
82fc8c956cdc
fixed various issues with MaSh's file handling + tune output + generate local facts again + handle nameless facts gracefully
blanchet
parents:
48392
diff
changeset
|
167 |
val facts = all_facts_of (Proof_Context.init_global thy) css_table |
48239
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
168 |
val (new_facts, old_facts) = |
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
169 |
facts |> List.partition (has_thy thy o snd) |
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
170 |
|>> sort (thm_ord o pairself snd) |
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
171 |
fun do_fact (fact as (_, th)) old_facts = |
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
172 |
let |
48378 | 173 |
val name = nickname_of 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
|
174 |
val goal = goal_of_thm thy th |
48292 | 175 |
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
|
176 |
val kind = Thm.legacy_get_kind th |
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
177 |
val _ = |
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
178 |
if kind <> "" then |
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
179 |
let |
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
180 |
val suggs = |
48292 | 181 |
old_facts |
48406 | 182 |
|> Sledgehammer_MePo.mepo_suggested_facts ctxt params prover |
48381 | 183 |
max_relevant NONE hyp_ts concl_t |
48303
f1d135d0ea69
improved MaSh string escaping and make more operations string-based
blanchet
parents:
48302
diff
changeset
|
184 |
|> map (fn ((name, _), _) => name ()) |
f1d135d0ea69
improved MaSh string escaping and make more operations string-based
blanchet
parents:
48302
diff
changeset
|
185 |
val s = escape_meta name ^ ": " ^ escape_metas suggs ^ "\n" |
48239
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
186 |
in File.append path s end |
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
187 |
else |
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
188 |
() |
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
189 |
in fact :: old_facts end |
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
190 |
in fold do_fact new_facts old_facts; () end |
48234 | 191 |
|
192 |
end; |