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