author | blanchet |
Wed, 18 Jul 2012 08:44:04 +0200 | |
changeset 48315 | 82d6e46c673f |
parent 48304 | 50e64af9c829 |
child 48316 | 252f45c04042 |
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 |
13 |
val generate_features : Proof.context -> theory -> bool -> string -> unit |
|
14 |
val generate_isa_dependencies : theory -> bool -> string -> unit |
|
15 |
val generate_atp_dependencies : |
|
16 |
Proof.context -> params -> theory -> bool -> string -> unit |
|
48299 | 17 |
val generate_commands : Proof.context -> theory -> string -> unit |
48251
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48250
diff
changeset
|
18 |
val generate_iter_suggestions : |
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48250
diff
changeset
|
19 |
Proof.context -> params -> theory -> int -> string -> unit |
48234 | 20 |
end; |
21 |
||
22 |
structure MaSh_Export : MASH_EXPORT = |
|
23 |
struct |
|
24 |
||
48304 | 25 |
open Sledgehammer_Fact |
26 |
open Sledgehammer_Filter_Iter |
|
48251
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48250
diff
changeset
|
27 |
open Sledgehammer_Filter_MaSh |
48245 | 28 |
|
48304 | 29 |
fun thy_of_fact thy = Context.this_theory thy o thy_name_of_fact |
30 |
||
31 |
fun generate_accessibility thy include_thy file_name = |
|
32 |
let |
|
33 |
val path = file_name |> Path.explode |
|
34 |
val _ = File.write path "" |
|
35 |
fun do_fact fact prevs = |
|
36 |
let |
|
37 |
val s = escape_meta fact ^ ": " ^ escape_metas prevs ^ "\n" |
|
38 |
val _ = File.append path s |
|
39 |
in [fact] end |
|
48315
82d6e46c673f
fixed order of accessibles + other tweaks to MaSh
blanchet
parents:
48304
diff
changeset
|
40 |
val thy_map = |
82d6e46c673f
fixed order of accessibles + other tweaks to MaSh
blanchet
parents:
48304
diff
changeset
|
41 |
all_facts_of thy Termtab.empty |
48304 | 42 |
|> 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
|
43 |
|> thy_map_from_facts |
48304 | 44 |
fun do_thy facts = |
45 |
let |
|
46 |
val thy = thy_of_fact thy (hd facts) |
|
48315
82d6e46c673f
fixed order of accessibles + other tweaks to MaSh
blanchet
parents:
48304
diff
changeset
|
47 |
val parents = parent_facts thy thy_map |
48304 | 48 |
in fold do_fact facts parents; () end |
48315
82d6e46c673f
fixed order of accessibles + other tweaks to MaSh
blanchet
parents:
48304
diff
changeset
|
49 |
in fold_rev (fn (_, facts) => K (do_thy facts)) thy_map () end |
48304 | 50 |
|
51 |
fun generate_features ctxt thy include_thy file_name = |
|
52 |
let |
|
53 |
val path = file_name |> Path.explode |
|
54 |
val _ = File.write path "" |
|
55 |
val css_table = clasimpset_rule_table_of ctxt |
|
56 |
val facts = |
|
48315
82d6e46c673f
fixed order of accessibles + other tweaks to MaSh
blanchet
parents:
48304
diff
changeset
|
57 |
all_facts_of thy css_table |
48304 | 58 |
|> not include_thy ? filter_out (has_thy thy o snd) |
59 |
fun do_fact ((_, (_, status)), th) = |
|
60 |
let |
|
61 |
val name = Thm.get_name_hint th |
|
62 |
val feats = features_of (theory_of_thm th) status [prop_of th] |
|
63 |
val s = escape_meta name ^ ": " ^ escape_metas feats ^ "\n" |
|
64 |
in File.append path s end |
|
65 |
in List.app do_fact facts end |
|
66 |
||
67 |
fun generate_isa_dependencies thy include_thy file_name = |
|
68 |
let |
|
69 |
val path = file_name |> Path.explode |
|
70 |
val _ = File.write path "" |
|
71 |
val ths = |
|
48315
82d6e46c673f
fixed order of accessibles + other tweaks to MaSh
blanchet
parents:
48304
diff
changeset
|
72 |
all_facts_of thy Termtab.empty |
48304 | 73 |
|> not include_thy ? filter_out (has_thy thy o snd) |
74 |
|> map snd |
|
48315
82d6e46c673f
fixed order of accessibles + other tweaks to MaSh
blanchet
parents:
48304
diff
changeset
|
75 |
val all_names = |
82d6e46c673f
fixed order of accessibles + other tweaks to MaSh
blanchet
parents:
48304
diff
changeset
|
76 |
ths |> filter_out (is_likely_tautology orf is_too_meta) |
82d6e46c673f
fixed order of accessibles + other tweaks to MaSh
blanchet
parents:
48304
diff
changeset
|
77 |
|> map Thm.get_name_hint |
48304 | 78 |
fun do_thm th = |
79 |
let |
|
80 |
val name = Thm.get_name_hint th |
|
81 |
val deps = isabelle_dependencies_of all_names th |
|
82 |
val s = escape_meta name ^ ": " ^ escape_metas deps ^ "\n" |
|
83 |
in File.append path s end |
|
84 |
in List.app do_thm ths end |
|
85 |
||
86 |
fun generate_atp_dependencies ctxt (params as {provers, max_facts, ...}) thy |
|
87 |
include_thy file_name = |
|
88 |
let |
|
89 |
val path = file_name |> Path.explode |
|
90 |
val _ = File.write path "" |
|
91 |
val facts = |
|
48315
82d6e46c673f
fixed order of accessibles + other tweaks to MaSh
blanchet
parents:
48304
diff
changeset
|
92 |
all_facts_of thy Termtab.empty |
48304 | 93 |
|> not include_thy ? filter_out (has_thy thy o snd) |
94 |
val ths = facts |> map snd |
|
48315
82d6e46c673f
fixed order of accessibles + other tweaks to MaSh
blanchet
parents:
48304
diff
changeset
|
95 |
val all_names = |
82d6e46c673f
fixed order of accessibles + other tweaks to MaSh
blanchet
parents:
48304
diff
changeset
|
96 |
ths |> filter_out (is_likely_tautology orf is_too_meta) |
82d6e46c673f
fixed order of accessibles + other tweaks to MaSh
blanchet
parents:
48304
diff
changeset
|
97 |
|> map Thm.get_name_hint |
48304 | 98 |
fun is_dep dep (_, th) = Thm.get_name_hint th = dep |
99 |
fun add_isa_dep facts dep accum = |
|
100 |
if exists (is_dep dep) accum then |
|
101 |
accum |
|
102 |
else case find_first (is_dep dep) facts of |
|
103 |
SOME ((name, status), th) => accum @ [((name, status), th)] |
|
104 |
| NONE => accum (* shouldn't happen *) |
|
105 |
fun fix_name ((_, stature), th) = |
|
106 |
((fn () => th |> Thm.get_name_hint, stature), th) |
|
107 |
fun do_thm th = |
|
108 |
let |
|
109 |
val name = Thm.get_name_hint th |
|
110 |
val goal = goal_of_thm thy th |
|
111 |
val (_, hyp_ts, concl_t) = ATP_Util.strip_subgoal ctxt goal 1 |
|
112 |
val deps = |
|
113 |
case isabelle_dependencies_of all_names th of |
|
114 |
[] => [] |
|
115 |
| isa_dep as [_] => isa_dep (* can hardly beat that *) |
|
116 |
| isa_deps => |
|
117 |
let |
|
118 |
val facts = |
|
119 |
facts |> filter (fn (_, th') => thm_ord (th', th) = LESS) |
|
120 |
val facts = |
|
121 |
facts |> iterative_relevant_facts ctxt params (hd provers) |
|
122 |
(the max_facts) NONE hyp_ts concl_t |
|
123 |
|> fold (add_isa_dep facts) isa_deps |
|
124 |
|> map fix_name |
|
125 |
in |
|
126 |
case run_prover ctxt params facts goal of |
|
127 |
{outcome = NONE, used_facts, ...} => |
|
128 |
used_facts |> map fst |> sort string_ord |
|
129 |
| _ => isa_deps |
|
130 |
end |
|
131 |
val s = escape_meta name ^ ": " ^ escape_metas deps ^ "\n" |
|
132 |
in File.append path s end |
|
133 |
in List.app do_thm ths end |
|
134 |
||
48299 | 135 |
fun generate_commands ctxt thy file_name = |
48234 | 136 |
let |
137 |
val path = file_name |> Path.explode |
|
138 |
val _ = File.write path "" |
|
48299 | 139 |
val css_table = Sledgehammer_Fact.clasimpset_rule_table_of ctxt |
48315
82d6e46c673f
fixed order of accessibles + other tweaks to MaSh
blanchet
parents:
48304
diff
changeset
|
140 |
val facts = all_facts_of thy css_table |
48234 | 141 |
val (new_facts, old_facts) = |
142 |
facts |> List.partition (has_thy thy o snd) |
|
143 |
|>> sort (thm_ord o pairself snd) |
|
144 |
val ths = facts |> map snd |
|
48315
82d6e46c673f
fixed order of accessibles + other tweaks to MaSh
blanchet
parents:
48304
diff
changeset
|
145 |
val all_names = |
82d6e46c673f
fixed order of accessibles + other tweaks to MaSh
blanchet
parents:
48304
diff
changeset
|
146 |
ths |> filter_out (is_likely_tautology orf is_too_meta) |
82d6e46c673f
fixed order of accessibles + other tweaks to MaSh
blanchet
parents:
48304
diff
changeset
|
147 |
|> map Thm.get_name_hint |
48234 | 148 |
fun do_fact ((_, (_, status)), th) prevs = |
149 |
let |
|
150 |
val name = Thm.get_name_hint th |
|
48302 | 151 |
val feats = features_of thy status [prop_of th] |
48251
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48250
diff
changeset
|
152 |
val deps = isabelle_dependencies_of all_names th |
48234 | 153 |
val kind = Thm.legacy_get_kind th |
48304 | 154 |
val core = escape_metas prevs ^ "; " ^ escape_metas feats |
48234 | 155 |
val query = if kind <> "" then "? " ^ core ^ "\n" else "" |
48300 | 156 |
val update = |
48303
f1d135d0ea69
improved MaSh string escaping and make more operations string-based
blanchet
parents:
48302
diff
changeset
|
157 |
"! " ^ escape_meta name ^ ": " ^ core ^ "; " ^ |
48304 | 158 |
escape_metas deps ^ "\n" |
48234 | 159 |
val _ = File.append path (query ^ update) |
160 |
in [name] end |
|
48315
82d6e46c673f
fixed order of accessibles + other tweaks to MaSh
blanchet
parents:
48304
diff
changeset
|
161 |
val thy_map = old_facts |> thy_map_from_facts |
82d6e46c673f
fixed order of accessibles + other tweaks to MaSh
blanchet
parents:
48304
diff
changeset
|
162 |
val parents = parent_facts thy thy_map |
48239
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
163 |
in fold do_fact new_facts parents; () end |
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
164 |
|
48292 | 165 |
fun generate_iter_suggestions ctxt (params as {provers, ...}) thy max_relevant |
166 |
file_name = |
|
48239
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
167 |
let |
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
168 |
val path = file_name |> Path.explode |
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
169 |
val _ = File.write path "" |
48299 | 170 |
val css_table = Sledgehammer_Fact.clasimpset_rule_table_of ctxt |
48315
82d6e46c673f
fixed order of accessibles + other tweaks to MaSh
blanchet
parents:
48304
diff
changeset
|
171 |
val facts = all_facts_of thy css_table |
48239
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
172 |
val (new_facts, old_facts) = |
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
173 |
facts |> List.partition (has_thy thy o snd) |
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
174 |
|>> sort (thm_ord o pairself snd) |
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
175 |
fun do_fact (fact as (_, th)) old_facts = |
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
176 |
let |
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
177 |
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
|
178 |
val goal = goal_of_thm thy th |
48292 | 179 |
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
|
180 |
val kind = Thm.legacy_get_kind th |
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
181 |
val _ = |
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
182 |
if kind <> "" then |
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
183 |
let |
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
184 |
val suggs = |
48292 | 185 |
old_facts |
186 |
|> Sledgehammer_Filter_Iter.iterative_relevant_facts ctxt params |
|
187 |
(hd provers) max_relevant NONE hyp_ts concl_t |
|
48303
f1d135d0ea69
improved MaSh string escaping and make more operations string-based
blanchet
parents:
48302
diff
changeset
|
188 |
|> map (fn ((name, _), _) => name ()) |
48292 | 189 |
|> sort string_ord |
48303
f1d135d0ea69
improved MaSh string escaping and make more operations string-based
blanchet
parents:
48302
diff
changeset
|
190 |
val s = escape_meta name ^ ": " ^ escape_metas suggs ^ "\n" |
48239
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
191 |
in File.append path s end |
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
192 |
else |
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
193 |
() |
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
194 |
in fact :: old_facts end |
0016290f904c
generate Meng--Paulson facts for evaluation purposes
blanchet
parents:
48235
diff
changeset
|
195 |
in fold do_fact new_facts old_facts; () end |
48234 | 196 |
|
197 |
end; |