author | blanchet |
Fri, 03 Aug 2012 17:56:35 +0200 | |
changeset 48666 | 0ba2e9be9f20 |
parent 48665 | 14b0732c72f7 |
child 48667 | ac58317ef11f |
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 |
|
48530
d443166f9520
repaired accessibility chains generated by MaSh exporter + tuned one function out
blanchet
parents:
48529
diff
changeset
|
12 |
val generate_accessibility : Proof.context -> theory -> bool -> string -> unit |
48318 | 13 |
val generate_features : |
14 |
Proof.context -> string -> theory -> bool -> string -> unit |
|
48333 | 15 |
val generate_isar_dependencies : |
48530
d443166f9520
repaired accessibility chains generated by MaSh exporter + tuned one function out
blanchet
parents:
48529
diff
changeset
|
16 |
Proof.context -> theory -> bool -> string -> unit |
48333 | 17 |
val generate_atp_dependencies : |
48304 | 18 |
Proof.context -> params -> theory -> bool -> string -> unit |
48529
716ec3458b1d
generate fact name in queries again + use ATP dependencies when possible
blanchet
parents:
48438
diff
changeset
|
19 |
val generate_commands : Proof.context -> params -> 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 |
|
48531 | 40 |
fun all_non_technical_facts ctxt thy = |
41 |
let val css = Sledgehammer_Fact.clasimpset_rule_table_of ctxt in |
|
42 |
all_facts (Proof_Context.init_global thy) false Symtab.empty [] [] css |
|
43 |
|> filter_out (is_thm_technical o snd) |
|
44 |
end |
|
45 |
||
48316
252f45c04042
drastic overhaul of MaSh data structures + fixed a few performance issues
blanchet
parents:
48315
diff
changeset
|
46 |
fun parent_facts thy thy_map = |
252f45c04042
drastic overhaul of MaSh data structures + fixed a few performance issues
blanchet
parents:
48315
diff
changeset
|
47 |
let |
252f45c04042
drastic overhaul of MaSh data structures + fixed a few performance issues
blanchet
parents:
48315
diff
changeset
|
48 |
fun add_last thy = |
252f45c04042
drastic overhaul of MaSh data structures + fixed a few performance issues
blanchet
parents:
48315
diff
changeset
|
49 |
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
|
50 |
SOME (last_fact :: _) => insert (op =) last_fact |
252f45c04042
drastic overhaul of MaSh data structures + fixed a few performance issues
blanchet
parents:
48315
diff
changeset
|
51 |
| _ => add_parent thy |
252f45c04042
drastic overhaul of MaSh data structures + fixed a few performance issues
blanchet
parents:
48315
diff
changeset
|
52 |
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
|
53 |
in add_parent thy [] end |
252f45c04042
drastic overhaul of MaSh data structures + fixed a few performance issues
blanchet
parents:
48315
diff
changeset
|
54 |
|
252f45c04042
drastic overhaul of MaSh data structures + fixed a few performance issues
blanchet
parents:
48315
diff
changeset
|
55 |
val thy_name_of_fact = hd o Long_Name.explode |
48304 | 56 |
fun thy_of_fact thy = Context.this_theory thy o thy_name_of_fact |
57 |
||
48438
3e45c98fe127
distinguish between recursive and nonrecursive definitions + clean up typedef dependencies in MaSh
blanchet
parents:
48406
diff
changeset
|
58 |
val all_names = 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
|
59 |
|
48530
d443166f9520
repaired accessibility chains generated by MaSh exporter + tuned one function out
blanchet
parents:
48529
diff
changeset
|
60 |
fun generate_accessibility ctxt thy include_thy file_name = |
48304 | 61 |
let |
62 |
val path = file_name |> Path.explode |
|
63 |
val _ = File.write path "" |
|
64 |
fun do_fact fact prevs = |
|
65 |
let |
|
66 |
val s = escape_meta fact ^ ": " ^ escape_metas prevs ^ "\n" |
|
67 |
val _ = File.append path s |
|
68 |
in [fact] end |
|
48315
82d6e46c673f
fixed order of accessibles + other tweaks to MaSh
blanchet
parents:
48304
diff
changeset
|
69 |
val thy_map = |
48531 | 70 |
all_non_technical_facts ctxt thy |
48304 | 71 |
|> 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
|
72 |
|> thy_map_from_facts |
48304 | 73 |
fun do_thy facts = |
74 |
let |
|
75 |
val thy = thy_of_fact thy (hd facts) |
|
48315
82d6e46c673f
fixed order of accessibles + other tweaks to MaSh
blanchet
parents:
48304
diff
changeset
|
76 |
val parents = parent_facts thy thy_map |
48530
d443166f9520
repaired accessibility chains generated by MaSh exporter + tuned one function out
blanchet
parents:
48529
diff
changeset
|
77 |
in fold_rev do_fact facts parents; () end |
48333 | 78 |
in fold_rev (fn (_, facts) => fn () => do_thy facts) thy_map () end |
48304 | 79 |
|
48318 | 80 |
fun generate_features ctxt prover thy include_thy file_name = |
48304 | 81 |
let |
82 |
val path = file_name |> Path.explode |
|
83 |
val _ = File.write path "" |
|
84 |
val facts = |
|
48531 | 85 |
all_non_technical_facts ctxt thy |
48304 | 86 |
|> not include_thy ? filter_out (has_thy thy o snd) |
48385 | 87 |
fun do_fact ((_, stature), th) = |
48304 | 88 |
let |
48378 | 89 |
val name = nickname_of th |
48318 | 90 |
val feats = |
48385 | 91 |
features_of ctxt prover (theory_of_thm th) stature [prop_of th] |
48304 | 92 |
val s = escape_meta name ^ ": " ^ escape_metas feats ^ "\n" |
93 |
in File.append path s end |
|
94 |
in List.app do_fact facts end |
|
95 |
||
48530
d443166f9520
repaired accessibility chains generated by MaSh exporter + tuned one function out
blanchet
parents:
48529
diff
changeset
|
96 |
fun generate_isar_dependencies ctxt thy include_thy file_name = |
48304 | 97 |
let |
98 |
val path = file_name |> Path.explode |
|
99 |
val _ = File.write path "" |
|
100 |
val ths = |
|
48531 | 101 |
all_non_technical_facts ctxt thy |
48304 | 102 |
|> not include_thy ? filter_out (has_thy thy o snd) |
103 |
|> map snd |
|
48324 | 104 |
val all_names = all_names ths |
48304 | 105 |
fun do_thm th = |
106 |
let |
|
48378 | 107 |
val name = nickname_of th |
48404 | 108 |
val deps = isar_dependencies_of all_names th |> these |
48304 | 109 |
val s = escape_meta name ^ ": " ^ escape_metas deps ^ "\n" |
110 |
in File.append path s end |
|
111 |
in List.app do_thm ths end |
|
112 |
||
48392
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48385
diff
changeset
|
113 |
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
|
114 |
file_name = |
48304 | 115 |
let |
116 |
val path = file_name |> Path.explode |
|
117 |
val _ = File.write path "" |
|
48318 | 118 |
val prover = hd provers |
48304 | 119 |
val facts = |
48531 | 120 |
all_non_technical_facts ctxt thy |
48304 | 121 |
|> not include_thy ? filter_out (has_thy thy o snd) |
122 |
val ths = facts |> map snd |
|
48324 | 123 |
val all_names = all_names ths |
48304 | 124 |
fun do_thm th = |
125 |
let |
|
48378 | 126 |
val name = nickname_of th |
48665 | 127 |
val deps = |
128 |
atp_dependencies_of ctxt params prover 0 facts all_names th |
|
129 |
|> snd |> these |
|
48304 | 130 |
val s = escape_meta name ^ ": " ^ escape_metas deps ^ "\n" |
131 |
in File.append path s end |
|
132 |
in List.app do_thm ths end |
|
133 |
||
48529
716ec3458b1d
generate fact name in queries again + use ATP dependencies when possible
blanchet
parents:
48438
diff
changeset
|
134 |
fun generate_commands ctxt (params as {provers, ...}) thy file_name = |
48234 | 135 |
let |
136 |
val path = file_name |> Path.explode |
|
137 |
val _ = File.write path "" |
|
48529
716ec3458b1d
generate fact name in queries again + use ATP dependencies when possible
blanchet
parents:
48438
diff
changeset
|
138 |
val prover = hd provers |
48531 | 139 |
val facts = all_non_technical_facts ctxt thy |
48234 | 140 |
val (new_facts, old_facts) = |
141 |
facts |> List.partition (has_thy thy o snd) |
|
142 |
|>> sort (thm_ord o pairself snd) |
|
48665 | 143 |
val all_names = all_names (map snd facts) |
48385 | 144 |
fun do_fact ((_, stature), th) prevs = |
48234 | 145 |
let |
48378 | 146 |
val name = nickname_of th |
48385 | 147 |
val feats = features_of ctxt prover thy stature [prop_of th] |
48665 | 148 |
val deps = |
149 |
atp_dependencies_of ctxt params prover 0 facts all_names th |
|
150 |
|> snd |> these |
|
48234 | 151 |
val kind = Thm.legacy_get_kind th |
48529
716ec3458b1d
generate fact name in queries again + use ATP dependencies when possible
blanchet
parents:
48438
diff
changeset
|
152 |
val core = |
716ec3458b1d
generate fact name in queries again + use ATP dependencies when possible
blanchet
parents:
48438
diff
changeset
|
153 |
escape_meta name ^ ": " ^ escape_metas prevs ^ "; " ^ |
716ec3458b1d
generate fact name in queries again + use ATP dependencies when possible
blanchet
parents:
48438
diff
changeset
|
154 |
escape_metas feats |
48666 | 155 |
val query = |
156 |
if kind = "" orelse null deps then "" else "? " ^ core ^ "\n" |
|
48529
716ec3458b1d
generate fact name in queries again + use ATP dependencies when possible
blanchet
parents:
48438
diff
changeset
|
157 |
val update = "! " ^ core ^ "; " ^ escape_metas deps ^ "\n" |
48234 | 158 |
val _ = File.append path (query ^ update) |
159 |
in [name] end |
|
48315
82d6e46c673f
fixed order of accessibles + other tweaks to MaSh
blanchet
parents:
48304
diff
changeset
|
160 |
val thy_map = old_facts |> thy_map_from_facts |
82d6e46c673f
fixed order of accessibles + other tweaks to MaSh
blanchet
parents:
48304
diff
changeset
|
161 |
val parents = parent_facts thy thy_map |
48239
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
162 |
in fold do_fact new_facts parents; () end |
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
163 |
|
48379
2b5ad61e2ccc
renamed "iter" fact filter to "MePo" (Meng--Paulson)
blanchet
parents:
48378
diff
changeset
|
164 |
fun generate_mepo_suggestions ctxt (params as {provers, ...}) thy max_relevant |
48292 | 165 |
file_name = |
48239
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
166 |
let |
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
167 |
val path = file_name |> Path.explode |
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
168 |
val _ = File.write path "" |
48318 | 169 |
val prover = hd provers |
48531 | 170 |
val facts = all_non_technical_facts ctxt thy |
48239
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
171 |
val (new_facts, old_facts) = |
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
172 |
facts |> List.partition (has_thy thy o snd) |
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
173 |
|>> sort (thm_ord o pairself snd) |
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
174 |
fun do_fact (fact as (_, th)) old_facts = |
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
175 |
let |
48378 | 176 |
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
|
177 |
val goal = goal_of_thm thy th |
48292 | 178 |
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
|
179 |
val kind = Thm.legacy_get_kind th |
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
180 |
val _ = |
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
181 |
if kind <> "" then |
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
182 |
let |
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
183 |
val suggs = |
48292 | 184 |
old_facts |
48406 | 185 |
|> Sledgehammer_MePo.mepo_suggested_facts ctxt params prover |
48381 | 186 |
max_relevant NONE hyp_ts concl_t |
48303
f1d135d0ea69
improved MaSh string escaping and make more operations string-based
blanchet
parents:
48302
diff
changeset
|
187 |
|> map (fn ((name, _), _) => name ()) |
f1d135d0ea69
improved MaSh string escaping and make more operations string-based
blanchet
parents:
48302
diff
changeset
|
188 |
val s = escape_meta name ^ ": " ^ escape_metas suggs ^ "\n" |
48239
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
189 |
in File.append path s end |
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
190 |
else |
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
191 |
() |
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
192 |
in fact :: old_facts end |
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
193 |
in fold do_fact new_facts old_facts; () end |
48234 | 194 |
|
195 |
end; |