author | blanchet |
Wed, 05 Dec 2012 13:25:06 +0100 | |
changeset 50382 | cb564ff43c28 |
parent 50359 | da395f0e7dea |
child 50383 | 4274b25ff4e7 |
permissions | -rw-r--r-- |
48380 | 1 |
(* Title: HOL/Tools/Sledgehammer/sledgehammer_mash.ML |
48248 | 2 |
Author: Jasmin Blanchette, TU Muenchen |
3 |
||
4 |
Sledgehammer's machine-learning-based relevance filter (MaSh). |
|
5 |
*) |
|
6 |
||
48381 | 7 |
signature SLEDGEHAMMER_MASH = |
48248 | 8 |
sig |
48251
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
9 |
type stature = ATP_Problem_Generate.stature |
48296
e7f01b7e244e
gracefully handle the case of empty theories when going up the accessibility chain
blanchet
parents:
48293
diff
changeset
|
10 |
type fact = Sledgehammer_Fact.fact |
e7f01b7e244e
gracefully handle the case of empty theories when going up the accessibility chain
blanchet
parents:
48293
diff
changeset
|
11 |
type fact_override = Sledgehammer_Fact.fact_override |
48251
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
12 |
type params = Sledgehammer_Provers.params |
48288
255c6e1fd505
rationalize relevance filter, slowing moving code from Iter to MaSh
blanchet
parents:
48251
diff
changeset
|
13 |
type relevance_fudge = Sledgehammer_Provers.relevance_fudge |
48251
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
14 |
type prover_result = Sledgehammer_Provers.prover_result |
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
15 |
|
48308 | 16 |
val trace : bool Config.T |
48319
340187063d84
use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents:
48318
diff
changeset
|
17 |
val MaShN : string |
48379
2b5ad61e2ccc
renamed "iter" fact filter to "MePo" (Meng--Paulson)
blanchet
parents:
48378
diff
changeset
|
18 |
val mepoN : string |
2b5ad61e2ccc
renamed "iter" fact filter to "MePo" (Meng--Paulson)
blanchet
parents:
48378
diff
changeset
|
19 |
val mashN : string |
48314
ee33ba3c0e05
added option to control which fact filter is used
blanchet
parents:
48313
diff
changeset
|
20 |
val meshN : string |
48392
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
21 |
val unlearnN : string |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
22 |
val learn_isarN : string |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
23 |
val learn_atpN : string |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
24 |
val relearn_isarN : string |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
25 |
val relearn_atpN : string |
48314
ee33ba3c0e05
added option to control which fact filter is used
blanchet
parents:
48313
diff
changeset
|
26 |
val fact_filters : string list |
48303
f1d135d0ea69
improved MaSh string escaping and make more operations string-based
blanchet
parents:
48302
diff
changeset
|
27 |
val escape_meta : string -> string |
f1d135d0ea69
improved MaSh string escaping and make more operations string-based
blanchet
parents:
48302
diff
changeset
|
28 |
val escape_metas : string list -> string |
48308 | 29 |
val unescape_meta : string -> string |
30 |
val unescape_metas : string -> string list |
|
50356 | 31 |
val encode_features : (string * real) list -> string |
48406 | 32 |
val extract_query : string -> string * (string * real) list |
50311 | 33 |
val mash_CLEAR : Proof.context -> unit |
34 |
val mash_ADD : |
|
35 |
Proof.context -> bool |
|
50356 | 36 |
-> (string * string list * (string * real) list * string list) list -> unit |
50311 | 37 |
val mash_REPROVE : |
38 |
Proof.context -> bool -> (string * string list) list -> unit |
|
39 |
val mash_QUERY : |
|
50356 | 40 |
Proof.context -> bool -> int -> string list * (string * real) list |
50311 | 41 |
-> (string * real) list |
42 |
val mash_unlearn : Proof.context -> unit |
|
48378 | 43 |
val nickname_of : thm -> string |
48406 | 44 |
val suggested_facts : |
45 |
(string * 'a) list -> ('b * thm) list -> (('b * thm) * 'a) list |
|
48321 | 46 |
val mesh_facts : |
48406 | 47 |
int -> ((('a * thm) * real) list * ('a * thm) list) list -> ('a * thm) list |
48251
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
48 |
val theory_ord : theory * theory -> order |
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
49 |
val thm_ord : thm * thm -> order |
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
50 |
val goal_of_thm : theory -> thm -> thm |
48321 | 51 |
val run_prover_for_mash : |
48318 | 52 |
Proof.context -> params -> string -> fact list -> thm -> prover_result |
48392
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
53 |
val features_of : |
50356 | 54 |
Proof.context -> string -> theory -> stature -> term list |
55 |
-> (string * real) list |
|
48404 | 56 |
val isar_dependencies_of : unit Symtab.table -> thm -> string list option |
48392
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
57 |
val atp_dependencies_of : |
48404 | 58 |
Proof.context -> params -> string -> int -> fact list -> unit Symtab.table |
48665 | 59 |
-> thm -> bool * string list option |
50382 | 60 |
val weight_mash_facts : ('a * thm) list -> (('a * thm) * real) list |
48406 | 61 |
val mash_suggested_facts : |
48321 | 62 |
Proof.context -> params -> string -> int -> term list -> term |
50382 | 63 |
-> fact list -> fact list * fact list |
48383 | 64 |
val mash_learn_proof : |
48384
83dc102041e6
learn on explicit "min" command but do the learning in a thread, since it may take a couple of seconds
blanchet
parents:
48383
diff
changeset
|
65 |
Proof.context -> params -> string -> term -> ('a * thm) list -> thm list |
83dc102041e6
learn on explicit "min" command but do the learning in a thread, since it may take a couple of seconds
blanchet
parents:
48383
diff
changeset
|
66 |
-> unit |
48395
85a7fb65507a
learning should honor the fact override and the chained facts
blanchet
parents:
48394
diff
changeset
|
67 |
val mash_learn : |
85a7fb65507a
learning should honor the fact override and the chained facts
blanchet
parents:
48394
diff
changeset
|
68 |
Proof.context -> params -> fact_override -> thm list -> bool -> unit |
50311 | 69 |
val is_mash_enabled : unit -> bool |
70 |
val mash_can_suggest_facts : Proof.context -> bool |
|
48288
255c6e1fd505
rationalize relevance filter, slowing moving code from Iter to MaSh
blanchet
parents:
48251
diff
changeset
|
71 |
val relevant_facts : |
48292 | 72 |
Proof.context -> params -> string -> int -> fact_override -> term list |
48296
e7f01b7e244e
gracefully handle the case of empty theories when going up the accessibility chain
blanchet
parents:
48293
diff
changeset
|
73 |
-> term -> fact list -> fact list |
48319
340187063d84
use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents:
48318
diff
changeset
|
74 |
val kill_learners : unit -> unit |
340187063d84
use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents:
48318
diff
changeset
|
75 |
val running_learners : unit -> unit |
48248 | 76 |
end; |
77 |
||
48381 | 78 |
structure Sledgehammer_MaSh : SLEDGEHAMMER_MASH = |
48248 | 79 |
struct |
48249 | 80 |
|
48251
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
81 |
open ATP_Util |
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
82 |
open ATP_Problem_Generate |
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
83 |
open Sledgehammer_Util |
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
84 |
open Sledgehammer_Fact |
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
85 |
open Sledgehammer_Provers |
48318 | 86 |
open Sledgehammer_Minimize |
48381 | 87 |
open Sledgehammer_MePo |
48251
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
88 |
|
48308 | 89 |
val trace = |
48380 | 90 |
Attrib.setup_config_bool @{binding sledgehammer_mash_trace} (K false) |
48308 | 91 |
fun trace_msg ctxt msg = if Config.get ctxt trace then tracing (msg ()) else () |
92 |
||
48319
340187063d84
use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents:
48318
diff
changeset
|
93 |
val MaShN = "MaSh" |
340187063d84
use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents:
48318
diff
changeset
|
94 |
|
48379
2b5ad61e2ccc
renamed "iter" fact filter to "MePo" (Meng--Paulson)
blanchet
parents:
48378
diff
changeset
|
95 |
val mepoN = "mepo" |
48314
ee33ba3c0e05
added option to control which fact filter is used
blanchet
parents:
48313
diff
changeset
|
96 |
val mashN = "mash" |
48379
2b5ad61e2ccc
renamed "iter" fact filter to "MePo" (Meng--Paulson)
blanchet
parents:
48378
diff
changeset
|
97 |
val meshN = "mesh" |
48314
ee33ba3c0e05
added option to control which fact filter is used
blanchet
parents:
48313
diff
changeset
|
98 |
|
48379
2b5ad61e2ccc
renamed "iter" fact filter to "MePo" (Meng--Paulson)
blanchet
parents:
48378
diff
changeset
|
99 |
val fact_filters = [meshN, mepoN, mashN] |
48314
ee33ba3c0e05
added option to control which fact filter is used
blanchet
parents:
48313
diff
changeset
|
100 |
|
48392
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
101 |
val unlearnN = "unlearn" |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
102 |
val learn_isarN = "learn_isar" |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
103 |
val learn_atpN = "learn_atp" |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
104 |
val relearn_isarN = "relearn_isar" |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
105 |
val relearn_atpN = "relearn_atp" |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
106 |
|
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
|
107 |
fun mash_model_dir () = |
50340
72519bf5f135
simplify MaSh term patterns + add missing global facts if there aren't too many
blanchet
parents:
50339
diff
changeset
|
108 |
Path.explode "$ISABELLE_HOME_USER/mash" |> tap Isabelle_System.mkdir |
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
|
109 |
val mash_state_dir = mash_model_dir |
50310 | 110 |
fun mash_state_file () = Path.append (mash_state_dir ()) (Path.explode "state") |
48251
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
111 |
|
48330 | 112 |
|
50311 | 113 |
(*** Low-level communication with MaSh ***) |
114 |
||
115 |
fun wipe_out_file file = (try (File.rm o Path.explode) file; ()) |
|
116 |
||
50335
b17b05c8d4a4
proper quoting of paths in MaSh shell script, take 2 (cf. b00eeb8e352e)
blanchet
parents:
50319
diff
changeset
|
117 |
fun write_file banner (xs, f) path = |
b17b05c8d4a4
proper quoting of paths in MaSh shell script, take 2 (cf. b00eeb8e352e)
blanchet
parents:
50319
diff
changeset
|
118 |
(case banner of SOME s => File.write path s | NONE => (); |
b17b05c8d4a4
proper quoting of paths in MaSh shell script, take 2 (cf. b00eeb8e352e)
blanchet
parents:
50319
diff
changeset
|
119 |
xs |> chunk_list 500 |
b17b05c8d4a4
proper quoting of paths in MaSh shell script, take 2 (cf. b00eeb8e352e)
blanchet
parents:
50319
diff
changeset
|
120 |
|> List.app (File.append path o space_implode "" o map f)) |
50319
dddcaeb92e11
robust writing of MaSh state -- better drop learning data than cause other problems in Sledgehammer
blanchet
parents:
50311
diff
changeset
|
121 |
handle IO.Io _ => () |
50311 | 122 |
|
123 |
fun run_mash_tool ctxt overlord save max_suggs write_cmds read_suggs = |
|
124 |
let |
|
125 |
val (temp_dir, serial) = |
|
126 |
if overlord then (getenv "ISABELLE_HOME_USER", "") |
|
127 |
else (getenv "ISABELLE_TMP", serial_string ()) |
|
128 |
val log_file = if overlord then temp_dir ^ "/mash_log" else "/dev/null" |
|
129 |
val err_file = temp_dir ^ "/mash_err" ^ serial |
|
130 |
val sugg_file = temp_dir ^ "/mash_suggs" ^ serial |
|
50335
b17b05c8d4a4
proper quoting of paths in MaSh shell script, take 2 (cf. b00eeb8e352e)
blanchet
parents:
50319
diff
changeset
|
131 |
val sugg_path = Path.explode sugg_file |
50311 | 132 |
val cmd_file = temp_dir ^ "/mash_commands" ^ serial |
50335
b17b05c8d4a4
proper quoting of paths in MaSh shell script, take 2 (cf. b00eeb8e352e)
blanchet
parents:
50319
diff
changeset
|
133 |
val cmd_path = Path.explode cmd_file |
50311 | 134 |
val core = |
135 |
"--inputFile " ^ cmd_file ^ " --predictions " ^ sugg_file ^ |
|
136 |
" --numberOfPredictions " ^ string_of_int max_suggs ^ |
|
137 |
(if save then " --saveModel" else "") |
|
138 |
val command = |
|
50335
b17b05c8d4a4
proper quoting of paths in MaSh shell script, take 2 (cf. b00eeb8e352e)
blanchet
parents:
50319
diff
changeset
|
139 |
"\"$ISABELLE_SLEDGEHAMMER_MASH/src/mash.py\" --quiet --outputDir " ^ |
b17b05c8d4a4
proper quoting of paths in MaSh shell script, take 2 (cf. b00eeb8e352e)
blanchet
parents:
50319
diff
changeset
|
140 |
File.shell_path (mash_model_dir ()) ^ " --log " ^ log_file ^ " " ^ core ^ |
50311 | 141 |
" >& " ^ err_file |
142 |
|> tap (fn _ => trace_msg ctxt (fn () => |
|
143 |
case try File.read (Path.explode err_file) of |
|
144 |
NONE => "Done" |
|
145 |
| SOME "" => "Done" |
|
146 |
| SOME s => "Error: " ^ elide_string 1000 s)) |
|
147 |
fun run_on () = |
|
148 |
(Isabelle_System.bash command; |
|
50335
b17b05c8d4a4
proper quoting of paths in MaSh shell script, take 2 (cf. b00eeb8e352e)
blanchet
parents:
50319
diff
changeset
|
149 |
read_suggs (fn () => try File.read_lines sugg_path |> these)) |
50311 | 150 |
fun clean_up () = |
151 |
if overlord then () |
|
152 |
else List.app wipe_out_file [err_file, sugg_file, cmd_file] |
|
153 |
in |
|
50335
b17b05c8d4a4
proper quoting of paths in MaSh shell script, take 2 (cf. b00eeb8e352e)
blanchet
parents:
50319
diff
changeset
|
154 |
write_file (SOME "") ([], K "") sugg_path; |
b17b05c8d4a4
proper quoting of paths in MaSh shell script, take 2 (cf. b00eeb8e352e)
blanchet
parents:
50319
diff
changeset
|
155 |
write_file (SOME "") write_cmds cmd_path; |
50311 | 156 |
trace_msg ctxt (fn () => "Running " ^ command); |
157 |
with_cleanup clean_up run_on () |
|
158 |
end |
|
48251
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
159 |
|
48308 | 160 |
fun meta_char c = |
48251
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
161 |
if Char.isAlphaNum c orelse c = #"_" orelse c = #"." orelse c = #"(" orelse |
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
162 |
c = #")" orelse c = #"," then |
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
163 |
String.str c |
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
164 |
else |
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
165 |
(* fixed width, in case more digits follow *) |
48395
85a7fb65507a
learning should honor the fact override and the chained facts
blanchet
parents:
48394
diff
changeset
|
166 |
"%" ^ stringN_of_int 3 (Char.ord c) |
48251
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
167 |
|
48308 | 168 |
fun unmeta_chars accum [] = String.implode (rev accum) |
48395
85a7fb65507a
learning should honor the fact override and the chained facts
blanchet
parents:
48394
diff
changeset
|
169 |
| unmeta_chars accum (#"%" :: d1 :: d2 :: d3 :: cs) = |
48308 | 170 |
(case Int.fromString (String.implode [d1, d2, d3]) of |
171 |
SOME n => unmeta_chars (Char.chr n :: accum) cs |
|
172 |
| NONE => "" (* error *)) |
|
48395
85a7fb65507a
learning should honor the fact override and the chained facts
blanchet
parents:
48394
diff
changeset
|
173 |
| unmeta_chars _ (#"%" :: _) = "" (* error *) |
48308 | 174 |
| unmeta_chars accum (c :: cs) = unmeta_chars (c :: accum) cs |
175 |
||
176 |
val escape_meta = String.translate meta_char |
|
48303
f1d135d0ea69
improved MaSh string escaping and make more operations string-based
blanchet
parents:
48302
diff
changeset
|
177 |
val escape_metas = map escape_meta #> space_implode " " |
48315
82d6e46c673f
fixed order of accessibles + other tweaks to MaSh
blanchet
parents:
48314
diff
changeset
|
178 |
val unescape_meta = String.explode #> unmeta_chars [] |
82d6e46c673f
fixed order of accessibles + other tweaks to MaSh
blanchet
parents:
48314
diff
changeset
|
179 |
val unescape_metas = |
82d6e46c673f
fixed order of accessibles + other tweaks to MaSh
blanchet
parents:
48314
diff
changeset
|
180 |
space_explode " " #> filter_out (curry (op =) "") #> map unescape_meta |
48251
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
181 |
|
50356 | 182 |
val supports_weights = false |
183 |
||
184 |
fun encode_feature (name, weight) = |
|
185 |
let val s = escape_meta name in |
|
186 |
if Real.== (weight, 1.0) then |
|
187 |
s |
|
188 |
else if supports_weights then |
|
189 |
s ^ "=" ^ Real.toString weight |
|
190 |
else |
|
191 |
map (prefix (s ^ ".") o string_of_int) (1 upto Real.ceil weight) |
|
192 |
|> space_implode " " |
|
193 |
end |
|
194 |
||
195 |
val encode_features = map encode_feature #> space_implode " " |
|
196 |
||
50311 | 197 |
fun str_of_add (name, parents, feats, deps) = |
198 |
"! " ^ escape_meta name ^ ": " ^ escape_metas parents ^ "; " ^ |
|
50356 | 199 |
encode_features feats ^ "; " ^ escape_metas deps ^ "\n" |
48669
cdcdb0547f29
remember ATP flops to avoid repeating them too quickly
blanchet
parents:
48668
diff
changeset
|
200 |
|
50311 | 201 |
fun str_of_reprove (name, deps) = |
202 |
"p " ^ escape_meta name ^ ": " ^ escape_metas deps ^ "\n" |
|
203 |
||
204 |
fun str_of_query (parents, feats) = |
|
50356 | 205 |
"? " ^ escape_metas parents ^ "; " ^ encode_features feats ^ "\n" |
48406 | 206 |
|
207 |
fun extract_suggestion sugg = |
|
208 |
case space_explode "=" sugg of |
|
209 |
[name, weight] => |
|
210 |
SOME (unescape_meta name, Real.fromString weight |> the_default 0.0) |
|
211 |
| _ => NONE |
|
212 |
||
48311 | 213 |
fun extract_query line = |
214 |
case space_explode ":" line of |
|
48406 | 215 |
[goal, suggs] => |
216 |
(unescape_meta goal, |
|
217 |
map_filter extract_suggestion (space_explode " " suggs)) |
|
48312 | 218 |
| _ => ("", []) |
48311 | 219 |
|
50311 | 220 |
fun mash_CLEAR ctxt = |
221 |
let val path = mash_model_dir () in |
|
222 |
trace_msg ctxt (K "MaSh CLEAR"); |
|
50319
dddcaeb92e11
robust writing of MaSh state -- better drop learning data than cause other problems in Sledgehammer
blanchet
parents:
50311
diff
changeset
|
223 |
try (File.fold_dir (fn file => fn _ => |
dddcaeb92e11
robust writing of MaSh state -- better drop learning data than cause other problems in Sledgehammer
blanchet
parents:
50311
diff
changeset
|
224 |
try File.rm (Path.append path (Path.basic file))) |
dddcaeb92e11
robust writing of MaSh state -- better drop learning data than cause other problems in Sledgehammer
blanchet
parents:
50311
diff
changeset
|
225 |
path) NONE; |
50311 | 226 |
() |
227 |
end |
|
228 |
||
229 |
fun mash_ADD _ _ [] = () |
|
230 |
| mash_ADD ctxt overlord adds = |
|
231 |
(trace_msg ctxt (fn () => "MaSh ADD " ^ |
|
232 |
elide_string 1000 (space_implode " " (map #1 adds))); |
|
233 |
run_mash_tool ctxt overlord true 0 (adds, str_of_add) (K ())) |
|
234 |
||
235 |
fun mash_REPROVE _ _ [] = () |
|
236 |
| mash_REPROVE ctxt overlord reps = |
|
237 |
(trace_msg ctxt (fn () => "MaSh REPROVE " ^ |
|
238 |
elide_string 1000 (space_implode " " (map #1 reps))); |
|
239 |
run_mash_tool ctxt overlord true 0 (reps, str_of_reprove) (K ())) |
|
240 |
||
241 |
fun mash_QUERY ctxt overlord max_suggs (query as (_, feats)) = |
|
50356 | 242 |
(trace_msg ctxt (fn () => "MaSh QUERY " ^ encode_features feats); |
50311 | 243 |
run_mash_tool ctxt overlord false max_suggs |
244 |
([query], str_of_query) |
|
245 |
(fn suggs => |
|
246 |
case suggs () of |
|
247 |
[] => [] |
|
248 |
| suggs => snd (extract_query (List.last suggs))) |
|
249 |
handle List.Empty => []) |
|
250 |
||
251 |
||
252 |
(*** Middle-level communication with MaSh ***) |
|
253 |
||
254 |
datatype proof_kind = Isar_Proof | ATP_Proof | Isar_Proof_wegen_ATP_Flop |
|
255 |
||
256 |
fun str_of_proof_kind Isar_Proof = "i" |
|
257 |
| str_of_proof_kind ATP_Proof = "a" |
|
258 |
| str_of_proof_kind Isar_Proof_wegen_ATP_Flop = "x" |
|
259 |
||
260 |
fun proof_kind_of_str "i" = Isar_Proof |
|
261 |
| proof_kind_of_str "a" = ATP_Proof |
|
262 |
| proof_kind_of_str "x" = Isar_Proof_wegen_ATP_Flop |
|
263 |
||
264 |
(* FIXME: Here a "Graph.update_node" function would be useful *) |
|
265 |
fun update_fact_graph_node (name, kind) = |
|
266 |
Graph.default_node (name, Isar_Proof) |
|
267 |
#> kind <> Isar_Proof ? Graph.map_node name (K kind) |
|
268 |
||
269 |
fun try_graph ctxt when def f = |
|
270 |
f () |
|
271 |
handle Graph.CYCLES (cycle :: _) => |
|
272 |
(trace_msg ctxt (fn () => |
|
273 |
"Cycle involving " ^ commas cycle ^ " when " ^ when); def) |
|
274 |
| Graph.DUP name => |
|
275 |
(trace_msg ctxt (fn () => |
|
276 |
"Duplicate fact " ^ quote name ^ " when " ^ when); def) |
|
277 |
| Graph.UNDEF name => |
|
278 |
(trace_msg ctxt (fn () => |
|
279 |
"Unknown fact " ^ quote name ^ " when " ^ when); def) |
|
280 |
| exn => |
|
281 |
if Exn.is_interrupt exn then |
|
282 |
reraise exn |
|
283 |
else |
|
284 |
(trace_msg ctxt (fn () => |
|
285 |
"Internal error when " ^ when ^ ":\n" ^ |
|
286 |
ML_Compiler.exn_message exn); def) |
|
287 |
||
288 |
fun graph_info G = |
|
289 |
string_of_int (length (Graph.keys G)) ^ " node(s), " ^ |
|
290 |
string_of_int (fold (Integer.add o length o snd) (Graph.dest G) 0) ^ |
|
291 |
" edge(s), " ^ |
|
292 |
string_of_int (length (Graph.minimals G)) ^ " minimal, " ^ |
|
293 |
string_of_int (length (Graph.maximals G)) ^ " maximal" |
|
294 |
||
295 |
type mash_state = {fact_G : unit Graph.T, dirty : string list option} |
|
296 |
||
297 |
val empty_state = {fact_G = Graph.empty, dirty = SOME []} |
|
298 |
||
299 |
local |
|
300 |
||
50357
187ae76a1757
more robustness in the face of MaSh format changes -- don't overwrite new versions with old versions
blanchet
parents:
50356
diff
changeset
|
301 |
val version = "*** MaSh version 20121204a ***" |
187ae76a1757
more robustness in the face of MaSh format changes -- don't overwrite new versions with old versions
blanchet
parents:
50356
diff
changeset
|
302 |
|
187ae76a1757
more robustness in the face of MaSh format changes -- don't overwrite new versions with old versions
blanchet
parents:
50356
diff
changeset
|
303 |
exception Too_New of unit |
50311 | 304 |
|
305 |
fun extract_node line = |
|
306 |
case space_explode ":" line of |
|
307 |
[head, parents] => |
|
308 |
(case space_explode " " head of |
|
309 |
[kind, name] => |
|
310 |
SOME (unescape_meta name, unescape_metas parents, |
|
311 |
try proof_kind_of_str kind |> the_default Isar_Proof) |
|
312 |
| _ => NONE) |
|
313 |
| _ => NONE |
|
314 |
||
315 |
fun load _ (state as (true, _)) = state |
|
316 |
| load ctxt _ = |
|
317 |
let val path = mash_state_file () in |
|
318 |
(true, |
|
319 |
case try File.read_lines path of |
|
320 |
SOME (version' :: node_lines) => |
|
321 |
let |
|
322 |
fun add_edge_to name parent = |
|
323 |
Graph.default_node (parent, Isar_Proof) |
|
324 |
#> Graph.add_edge (parent, name) |
|
325 |
fun add_node line = |
|
326 |
case extract_node line of |
|
327 |
NONE => I (* shouldn't happen *) |
|
328 |
| SOME (name, parents, kind) => |
|
329 |
update_fact_graph_node (name, kind) |
|
330 |
#> fold (add_edge_to name) parents |
|
331 |
val fact_G = |
|
50357
187ae76a1757
more robustness in the face of MaSh format changes -- don't overwrite new versions with old versions
blanchet
parents:
50356
diff
changeset
|
332 |
case string_ord (version', version) of |
187ae76a1757
more robustness in the face of MaSh format changes -- don't overwrite new versions with old versions
blanchet
parents:
50356
diff
changeset
|
333 |
EQUAL => |
187ae76a1757
more robustness in the face of MaSh format changes -- don't overwrite new versions with old versions
blanchet
parents:
50356
diff
changeset
|
334 |
try_graph ctxt "loading state" Graph.empty (fn () => |
187ae76a1757
more robustness in the face of MaSh format changes -- don't overwrite new versions with old versions
blanchet
parents:
50356
diff
changeset
|
335 |
fold add_node node_lines Graph.empty) |
187ae76a1757
more robustness in the face of MaSh format changes -- don't overwrite new versions with old versions
blanchet
parents:
50356
diff
changeset
|
336 |
| LESS => Graph.empty (* can't parse old file *) |
187ae76a1757
more robustness in the face of MaSh format changes -- don't overwrite new versions with old versions
blanchet
parents:
50356
diff
changeset
|
337 |
| GREATER => raise Too_New () |
50311 | 338 |
in |
339 |
trace_msg ctxt (fn () => |
|
340 |
"Loaded fact graph (" ^ graph_info fact_G ^ ")"); |
|
341 |
{fact_G = fact_G, dirty = SOME []} |
|
342 |
end |
|
343 |
| _ => empty_state) |
|
344 |
end |
|
345 |
||
346 |
fun save _ (state as {dirty = SOME [], ...}) = state |
|
347 |
| save ctxt {fact_G, dirty} = |
|
348 |
let |
|
349 |
fun str_of_entry (name, parents, kind) = |
|
350 |
str_of_proof_kind kind ^ " " ^ escape_meta name ^ ": " ^ |
|
351 |
escape_metas parents ^ "\n" |
|
352 |
fun append_entry (name, (kind, (parents, _))) = |
|
353 |
cons (name, Graph.Keys.dest parents, kind) |
|
354 |
val (banner, entries) = |
|
355 |
case dirty of |
|
356 |
SOME names => |
|
357 |
(NONE, fold (append_entry o Graph.get_entry fact_G) names []) |
|
358 |
| NONE => (SOME (version ^ "\n"), Graph.fold append_entry fact_G []) |
|
359 |
in |
|
50335
b17b05c8d4a4
proper quoting of paths in MaSh shell script, take 2 (cf. b00eeb8e352e)
blanchet
parents:
50319
diff
changeset
|
360 |
write_file banner (entries, str_of_entry) (mash_state_file ()); |
50311 | 361 |
trace_msg ctxt (fn () => |
362 |
"Saved fact graph (" ^ graph_info fact_G ^ |
|
363 |
(case dirty of |
|
364 |
SOME dirty => |
|
365 |
"; " ^ string_of_int (length dirty) ^ " dirty fact(s)" |
|
366 |
| _ => "") ^ ")"); |
|
367 |
{fact_G = fact_G, dirty = SOME []} |
|
368 |
end |
|
369 |
||
370 |
val global_state = |
|
371 |
Synchronized.var "Sledgehammer_MaSh.global_state" (false, empty_state) |
|
372 |
||
373 |
in |
|
374 |
||
375 |
fun mash_map ctxt f = |
|
376 |
Synchronized.change global_state (load ctxt ##> (f #> save ctxt)) |
|
50357
187ae76a1757
more robustness in the face of MaSh format changes -- don't overwrite new versions with old versions
blanchet
parents:
50356
diff
changeset
|
377 |
handle Too_New () => () |
50311 | 378 |
|
379 |
fun mash_peek ctxt f = |
|
50357
187ae76a1757
more robustness in the face of MaSh format changes -- don't overwrite new versions with old versions
blanchet
parents:
50356
diff
changeset
|
380 |
Synchronized.change_result global_state |
187ae76a1757
more robustness in the face of MaSh format changes -- don't overwrite new versions with old versions
blanchet
parents:
50356
diff
changeset
|
381 |
(perhaps (try (load ctxt)) #> `snd #>> f) |
50311 | 382 |
|
383 |
fun mash_get ctxt = |
|
50357
187ae76a1757
more robustness in the face of MaSh format changes -- don't overwrite new versions with old versions
blanchet
parents:
50356
diff
changeset
|
384 |
Synchronized.change_result global_state (perhaps (try (load ctxt)) #> `snd) |
50311 | 385 |
|
386 |
fun mash_unlearn ctxt = |
|
387 |
Synchronized.change global_state (fn _ => |
|
388 |
(mash_CLEAR ctxt; (* also removes the state file *) |
|
389 |
(true, empty_state))) |
|
390 |
||
391 |
end |
|
392 |
||
393 |
||
394 |
(*** Isabelle helpers ***) |
|
395 |
||
48378 | 396 |
fun parent_of_local_thm th = |
397 |
let |
|
398 |
val thy = th |> Thm.theory_of_thm |
|
399 |
val facts = thy |> Global_Theory.facts_of |
|
400 |
val space = facts |> Facts.space_of |
|
401 |
fun id_of s = #id (Name_Space.the_entry space s) |
|
402 |
fun max_id (s', _) (s, id) = |
|
403 |
let val id' = id_of s' in if id > id' then (s, id) else (s', id') end |
|
404 |
in ("", ~1) |> Facts.fold_static max_id facts |> fst end |
|
405 |
||
406 |
val local_prefix = "local" ^ Long_Name.separator |
|
407 |
||
408 |
fun nickname_of th = |
|
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
|
409 |
if Thm.has_name_hint th then |
82fc8c956cdc
fixed various issues with MaSh's file handling + tune output + generate local facts again + handle nameless facts gracefully
blanchet
parents:
48392
diff
changeset
|
410 |
let val hint = Thm.get_name_hint th in |
82fc8c956cdc
fixed various issues with MaSh's file handling + tune output + generate local facts again + handle nameless facts gracefully
blanchet
parents:
48392
diff
changeset
|
411 |
(* FIXME: There must be a better way to detect local facts. *) |
82fc8c956cdc
fixed various issues with MaSh's file handling + tune output + generate local facts again + handle nameless facts gracefully
blanchet
parents:
48392
diff
changeset
|
412 |
case try (unprefix local_prefix) hint of |
82fc8c956cdc
fixed various issues with MaSh's file handling + tune output + generate local facts again + handle nameless facts gracefully
blanchet
parents:
48392
diff
changeset
|
413 |
SOME suf => |
82fc8c956cdc
fixed various issues with MaSh's file handling + tune output + generate local facts again + handle nameless facts gracefully
blanchet
parents:
48392
diff
changeset
|
414 |
parent_of_local_thm th ^ Long_Name.separator ^ Long_Name.separator ^ suf |
82fc8c956cdc
fixed various issues with MaSh's file handling + tune output + generate local facts again + handle nameless facts gracefully
blanchet
parents:
48392
diff
changeset
|
415 |
| NONE => hint |
82fc8c956cdc
fixed various issues with MaSh's file handling + tune output + generate local facts again + handle nameless facts gracefully
blanchet
parents:
48392
diff
changeset
|
416 |
end |
82fc8c956cdc
fixed various issues with MaSh's file handling + tune output + generate local facts again + handle nameless facts gracefully
blanchet
parents:
48392
diff
changeset
|
417 |
else |
50047
45684acf0b94
thread context correctly when printing backquoted facts
blanchet
parents:
49997
diff
changeset
|
418 |
backquote_thm (Proof_Context.init_global (Thm.theory_of_thm th)) th |
48378 | 419 |
|
48330 | 420 |
fun suggested_facts suggs facts = |
421 |
let |
|
48378 | 422 |
fun add_fact (fact as (_, th)) = Symtab.default (nickname_of th, fact) |
48330 | 423 |
val tab = Symtab.empty |> fold add_fact facts |
48406 | 424 |
fun find_sugg (name, weight) = |
425 |
Symtab.lookup tab name |> Option.map (rpair weight) |
|
426 |
in map_filter find_sugg suggs end |
|
48311 | 427 |
|
48406 | 428 |
fun sum_avg [] = 0 |
48407 | 429 |
| sum_avg xs = |
430 |
Real.ceil (100000000.0 * fold (curry (op +)) xs 0.0) div length xs |
|
48328
ca0b7d19dd62
attempt at meshing according to more meaningful factors
blanchet
parents:
48327
diff
changeset
|
431 |
|
48406 | 432 |
fun normalize_scores [] = [] |
433 |
| normalize_scores ((fact, score) :: tail) = |
|
434 |
(fact, 1.0) :: map (apsnd (curry Real.* (1.0 / score))) tail |
|
48313
0faafdffa662
mesh facts by taking into consideration whether a fact is known to MeSh
blanchet
parents:
48312
diff
changeset
|
435 |
|
48406 | 436 |
fun mesh_facts max_facts [(sels, unks)] = |
437 |
map fst (take max_facts sels) @ take (max_facts - length sels) unks |
|
48314
ee33ba3c0e05
added option to control which fact filter is used
blanchet
parents:
48313
diff
changeset
|
438 |
| mesh_facts max_facts mess = |
ee33ba3c0e05
added option to control which fact filter is used
blanchet
parents:
48313
diff
changeset
|
439 |
let |
48406 | 440 |
val mess = mess |> map (apfst (normalize_scores #> `length)) |
48314
ee33ba3c0e05
added option to control which fact filter is used
blanchet
parents:
48313
diff
changeset
|
441 |
val fact_eq = Thm.eq_thm o pairself snd |
48406 | 442 |
fun score_at sels = try (nth sels) #> Option.map snd |
48320
891a24a48155
improved meshing of MaSh and Meng--Paulson if some MaSh suggestions are cut-off (the common case)
blanchet
parents:
48319
diff
changeset
|
443 |
fun score_in fact ((sel_len, sels), unks) = |
48406 | 444 |
case find_index (curry fact_eq fact o fst) sels of |
48320
891a24a48155
improved meshing of MaSh and Meng--Paulson if some MaSh suggestions are cut-off (the common case)
blanchet
parents:
48319
diff
changeset
|
445 |
~1 => (case find_index (curry fact_eq fact) unks of |
48406 | 446 |
~1 => score_at sels sel_len |
48320
891a24a48155
improved meshing of MaSh and Meng--Paulson if some MaSh suggestions are cut-off (the common case)
blanchet
parents:
48319
diff
changeset
|
447 |
| _ => NONE) |
48406 | 448 |
| rank => score_at sels rank |
449 |
fun weight_of fact = mess |> map_filter (score_in fact) |> sum_avg |
|
450 |
val facts = |
|
451 |
fold (union fact_eq o map fst o take max_facts o snd o fst) mess [] |
|
48314
ee33ba3c0e05
added option to control which fact filter is used
blanchet
parents:
48313
diff
changeset
|
452 |
in |
48406 | 453 |
facts |> map (`weight_of) |> sort (int_ord o swap o pairself fst) |
48328
ca0b7d19dd62
attempt at meshing according to more meaningful factors
blanchet
parents:
48327
diff
changeset
|
454 |
|> map snd |> take max_facts |
48314
ee33ba3c0e05
added option to control which fact filter is used
blanchet
parents:
48313
diff
changeset
|
455 |
end |
48312 | 456 |
|
50356 | 457 |
fun thy_feature_of s = ("y" ^ s, 1.0 (* FUDGE *)) |
458 |
fun term_feature_of s = ("c" ^ s, 1.0 (* FUDGE *)) |
|
459 |
fun type_feature_of s = ("t" ^ s, 1.0 (* FUDGE *)) |
|
460 |
fun class_feature_of s = ("s" ^ s, 1.0 (* FUDGE *)) |
|
461 |
fun status_feature_of status = (string_of_status status, 1.0 (* FUDGE *)) |
|
462 |
val local_feature = ("local", 20.0 (* FUDGE *)) |
|
463 |
val lams_feature = ("lams", 1.0 (* FUDGE *)) |
|
464 |
val skos_feature = ("skos", 1.0 (* FUDGE *)) |
|
48251
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
465 |
|
48324 | 466 |
fun theory_ord p = |
467 |
if Theory.eq_thy p then |
|
468 |
EQUAL |
|
469 |
else if Theory.subthy p then |
|
470 |
LESS |
|
471 |
else if Theory.subthy (swap p) then |
|
472 |
GREATER |
|
473 |
else case int_ord (pairself (length o Theory.ancestors_of) p) of |
|
474 |
EQUAL => string_ord (pairself Context.theory_name p) |
|
475 |
| order => order |
|
476 |
||
50382 | 477 |
fun thm_ord p = |
478 |
case theory_ord (pairself theory_of_thm p) of |
|
50359
da395f0e7dea
tweaked order of theorems to avoid forward dependencies (MaSh)
blanchet
parents:
50357
diff
changeset
|
479 |
EQUAL => |
da395f0e7dea
tweaked order of theorems to avoid forward dependencies (MaSh)
blanchet
parents:
50357
diff
changeset
|
480 |
(* Hack to put "xxx_def" before "xxxI" and "xxxE" *) |
50382 | 481 |
string_ord (pairself nickname_of (swap p)) |
50359
da395f0e7dea
tweaked order of theorems to avoid forward dependencies (MaSh)
blanchet
parents:
50357
diff
changeset
|
482 |
| ord => ord |
48324 | 483 |
|
48392
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
484 |
val freezeT = Type.legacy_freeze_type |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
485 |
|
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
486 |
fun freeze (t $ u) = freeze t $ freeze u |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
487 |
| freeze (Abs (s, T, t)) = Abs (s, freezeT T, freeze t) |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
488 |
| freeze (Var ((s, _), T)) = Free (s, freezeT T) |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
489 |
| freeze (Const (s, T)) = Const (s, freezeT T) |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
490 |
| freeze (Free (s, T)) = Free (s, freezeT T) |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
491 |
| freeze t = t |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
492 |
|
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
493 |
fun goal_of_thm thy = prop_of #> freeze #> cterm_of thy #> Goal.init |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
494 |
|
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
495 |
fun run_prover_for_mash ctxt params prover facts goal = |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
496 |
let |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
497 |
val problem = |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
498 |
{state = Proof.init ctxt, goal = goal, subgoal = 1, subgoal_count = 1, |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
499 |
facts = facts |> map (apfst (apfst (fn name => name ()))) |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
500 |
|> map Untranslated_Fact} |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
501 |
in |
48399
4bacc8983b3d
learn from SMT proofs when they can be minimized by Metis
blanchet
parents:
48398
diff
changeset
|
502 |
get_minimizing_prover ctxt MaSh (K (K ())) prover params (K (K (K ""))) |
48392
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
503 |
problem |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
504 |
end |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
505 |
|
48326 | 506 |
val bad_types = [@{type_name prop}, @{type_name bool}, @{type_name fun}] |
507 |
||
48398 | 508 |
val logical_consts = |
509 |
[@{const_name prop}, @{const_name Pure.conjunction}] @ atp_logical_consts |
|
510 |
||
48318 | 511 |
fun interesting_terms_types_and_classes ctxt prover term_max_depth |
512 |
type_max_depth ts = |
|
48251
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
513 |
let |
48318 | 514 |
fun is_bad_const (x as (s, _)) args = |
48398 | 515 |
member (op =) logical_consts s orelse |
48318 | 516 |
fst (is_built_in_const_for_prover ctxt prover x args) |
48304 | 517 |
fun add_classes @{sort type} = I |
50356 | 518 |
| add_classes S = union (op = o pairself fst) (map class_feature_of S) |
48251
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
519 |
fun do_add_type (Type (s, Ts)) = |
50356 | 520 |
(not (member (op =) bad_types s) |
521 |
? insert (op = o pairself fst) (type_feature_of s)) |
|
48251
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
522 |
#> fold do_add_type Ts |
48304 | 523 |
| do_add_type (TFree (_, S)) = add_classes S |
524 |
| do_add_type (TVar (_, S)) = add_classes S |
|
48251
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
525 |
fun add_type T = type_max_depth >= 0 ? do_add_type T |
50339
d8dae91f3107
MaSh improvements: deeper patterns + more respect for chained facts
blanchet
parents:
50338
diff
changeset
|
526 |
fun patternify_term _ ~1 _ = [] |
d8dae91f3107
MaSh improvements: deeper patterns + more respect for chained facts
blanchet
parents:
50338
diff
changeset
|
527 |
| patternify_term args _ (Const (x as (s, _))) = |
50356 | 528 |
if is_bad_const x args then [] else [s] |
50339
d8dae91f3107
MaSh improvements: deeper patterns + more respect for chained facts
blanchet
parents:
50338
diff
changeset
|
529 |
| patternify_term _ 0 _ = [] |
d8dae91f3107
MaSh improvements: deeper patterns + more respect for chained facts
blanchet
parents:
50338
diff
changeset
|
530 |
| patternify_term args depth (t $ u) = |
d8dae91f3107
MaSh improvements: deeper patterns + more respect for chained facts
blanchet
parents:
50338
diff
changeset
|
531 |
let |
d8dae91f3107
MaSh improvements: deeper patterns + more respect for chained facts
blanchet
parents:
50338
diff
changeset
|
532 |
val ps = patternify_term (u :: args) depth t |
d8dae91f3107
MaSh improvements: deeper patterns + more respect for chained facts
blanchet
parents:
50338
diff
changeset
|
533 |
val qs = "" :: patternify_term [] (depth - 1) u |
50356 | 534 |
in map_product (fn p => fn "" => p | q => "(" ^ q ^ ")") ps qs end |
50339
d8dae91f3107
MaSh improvements: deeper patterns + more respect for chained facts
blanchet
parents:
50338
diff
changeset
|
535 |
| patternify_term _ _ _ = [] |
50356 | 536 |
val add_term_pattern = |
537 |
union (op = o pairself fst) o map term_feature_of oo patternify_term [] |
|
48251
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
538 |
fun add_term_patterns ~1 _ = I |
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
539 |
| add_term_patterns depth t = |
50338 | 540 |
add_term_pattern depth t #> add_term_patterns (depth - 1) t |
48251
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
541 |
val add_term = add_term_patterns term_max_depth |
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
542 |
fun add_patterns t = |
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
543 |
let val (head, args) = strip_comb t in |
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
544 |
(case head of |
48398 | 545 |
Const (_, T) => add_term t #> add_type T |
48251
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
546 |
| Free (_, T) => add_type T |
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
547 |
| Var (_, T) => add_type T |
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
548 |
| Abs (_, T, body) => add_type T #> add_patterns body |
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
549 |
| _ => I) |
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
550 |
#> fold add_patterns args |
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
551 |
end |
48326 | 552 |
in [] |> fold add_patterns ts end |
48251
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
553 |
|
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
554 |
fun is_exists (s, _) = (s = @{const_name Ex} orelse s = @{const_name Ex1}) |
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
555 |
|
50339
d8dae91f3107
MaSh improvements: deeper patterns + more respect for chained facts
blanchet
parents:
50338
diff
changeset
|
556 |
val term_max_depth = 2 |
d8dae91f3107
MaSh improvements: deeper patterns + more respect for chained facts
blanchet
parents:
50338
diff
changeset
|
557 |
val type_max_depth = 2 |
48251
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
558 |
|
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
559 |
(* TODO: Generate type classes for types? *) |
48385 | 560 |
fun features_of ctxt prover thy (scope, status) ts = |
50356 | 561 |
thy_feature_of (Context.theory_name thy) :: |
48318 | 562 |
interesting_terms_types_and_classes ctxt prover term_max_depth type_max_depth |
563 |
ts |
|
50356 | 564 |
|> status <> General ? cons (status_feature_of status) |
565 |
|> scope <> Global ? cons local_feature |
|
566 |
|> exists (not o is_lambda_free) ts ? cons lams_feature |
|
567 |
|> exists (exists_Const is_exists) ts ? cons skos_feature |
|
48251
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
568 |
|
48665 | 569 |
(* Too many dependencies is a sign that a crazy decision procedure is at work. |
570 |
There isn't much to learn from such proofs. *) |
|
571 |
val max_dependencies = 100 |
|
572 |
val atp_dependency_default_max_facts = 50 |
|
48251
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
573 |
|
48438
3e45c98fe127
distinguish between recursive and nonrecursive definitions + clean up typedef dependencies in MaSh
blanchet
parents:
48436
diff
changeset
|
574 |
(* "type_definition_xxx" facts are characterized by their use of "CollectI". *) |
48441 | 575 |
val typedef_deps = [@{thm CollectI} |> nickname_of] |
48438
3e45c98fe127
distinguish between recursive and nonrecursive definitions + clean up typedef dependencies in MaSh
blanchet
parents:
48436
diff
changeset
|
576 |
|
3e45c98fe127
distinguish between recursive and nonrecursive definitions + clean up typedef dependencies in MaSh
blanchet
parents:
48436
diff
changeset
|
577 |
(* "Rep_xxx_inject", "Abs_xxx_inverse", etc., are derived using these facts. *) |
3e45c98fe127
distinguish between recursive and nonrecursive definitions + clean up typedef dependencies in MaSh
blanchet
parents:
48436
diff
changeset
|
578 |
val typedef_ths = |
3e45c98fe127
distinguish between recursive and nonrecursive definitions + clean up typedef dependencies in MaSh
blanchet
parents:
48436
diff
changeset
|
579 |
@{thms type_definition.Abs_inverse type_definition.Rep_inverse |
3e45c98fe127
distinguish between recursive and nonrecursive definitions + clean up typedef dependencies in MaSh
blanchet
parents:
48436
diff
changeset
|
580 |
type_definition.Rep type_definition.Rep_inject |
3e45c98fe127
distinguish between recursive and nonrecursive definitions + clean up typedef dependencies in MaSh
blanchet
parents:
48436
diff
changeset
|
581 |
type_definition.Abs_inject type_definition.Rep_cases |
3e45c98fe127
distinguish between recursive and nonrecursive definitions + clean up typedef dependencies in MaSh
blanchet
parents:
48436
diff
changeset
|
582 |
type_definition.Abs_cases type_definition.Rep_induct |
3e45c98fe127
distinguish between recursive and nonrecursive definitions + clean up typedef dependencies in MaSh
blanchet
parents:
48436
diff
changeset
|
583 |
type_definition.Abs_induct type_definition.Rep_range |
3e45c98fe127
distinguish between recursive and nonrecursive definitions + clean up typedef dependencies in MaSh
blanchet
parents:
48436
diff
changeset
|
584 |
type_definition.Abs_image} |
3e45c98fe127
distinguish between recursive and nonrecursive definitions + clean up typedef dependencies in MaSh
blanchet
parents:
48436
diff
changeset
|
585 |
|> map nickname_of |
3e45c98fe127
distinguish between recursive and nonrecursive definitions + clean up typedef dependencies in MaSh
blanchet
parents:
48436
diff
changeset
|
586 |
|
48441 | 587 |
fun is_size_def [dep] th = |
588 |
(case first_field ".recs" dep of |
|
589 |
SOME (pref, _) => |
|
590 |
(case first_field ".size" (nickname_of th) of |
|
591 |
SOME (pref', _) => pref = pref' |
|
592 |
| NONE => false) |
|
593 |
| NONE => false) |
|
594 |
| is_size_def _ _ = false |
|
595 |
||
596 |
fun trim_dependencies th deps = |
|
48668
5d63c23b4042
remember which MaSh proofs were found using ATPs
blanchet
parents:
48667
diff
changeset
|
597 |
if length deps > max_dependencies then |
48438
3e45c98fe127
distinguish between recursive and nonrecursive definitions + clean up typedef dependencies in MaSh
blanchet
parents:
48436
diff
changeset
|
598 |
NONE |
3e45c98fe127
distinguish between recursive and nonrecursive definitions + clean up typedef dependencies in MaSh
blanchet
parents:
48436
diff
changeset
|
599 |
else |
48668
5d63c23b4042
remember which MaSh proofs were found using ATPs
blanchet
parents:
48667
diff
changeset
|
600 |
SOME (if deps = typedef_deps orelse |
5d63c23b4042
remember which MaSh proofs were found using ATPs
blanchet
parents:
48667
diff
changeset
|
601 |
exists (member (op =) typedef_ths) deps orelse |
5d63c23b4042
remember which MaSh proofs were found using ATPs
blanchet
parents:
48667
diff
changeset
|
602 |
is_size_def deps th then |
5d63c23b4042
remember which MaSh proofs were found using ATPs
blanchet
parents:
48667
diff
changeset
|
603 |
[] |
5d63c23b4042
remember which MaSh proofs were found using ATPs
blanchet
parents:
48667
diff
changeset
|
604 |
else |
5d63c23b4042
remember which MaSh proofs were found using ATPs
blanchet
parents:
48667
diff
changeset
|
605 |
deps) |
48251
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
606 |
|
48441 | 607 |
fun isar_dependencies_of all_names th = |
608 |
th |> thms_in_proof (SOME all_names) |> trim_dependencies th |
|
48404 | 609 |
|
610 |
fun atp_dependencies_of ctxt (params as {verbose, max_facts, ...}) prover |
|
611 |
auto_level facts all_names th = |
|
48392
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
612 |
case isar_dependencies_of all_names th of |
48665 | 613 |
SOME [] => (false, SOME []) |
48392
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
614 |
| isar_deps => |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
615 |
let |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
616 |
val thy = Proof_Context.theory_of ctxt |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
617 |
val goal = goal_of_thm thy th |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
618 |
val (_, hyp_ts, concl_t) = ATP_Util.strip_subgoal ctxt goal 1 |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
619 |
val facts = facts |> filter (fn (_, th') => thm_ord (th', th) = LESS) |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
620 |
fun fix_name ((_, stature), th) = ((fn () => nickname_of th, stature), th) |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
621 |
fun is_dep dep (_, th) = nickname_of th = dep |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
622 |
fun add_isar_dep facts dep accum = |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
623 |
if exists (is_dep dep) accum then |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
624 |
accum |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
625 |
else case find_first (is_dep dep) facts of |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
626 |
SOME ((name, status), th) => accum @ [((name, status), th)] |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
627 |
| NONE => accum (* shouldn't happen *) |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
628 |
val facts = |
48406 | 629 |
facts |> mepo_suggested_facts ctxt params prover |
48665 | 630 |
(max_facts |> the_default atp_dependency_default_max_facts) |
48404 | 631 |
NONE hyp_ts concl_t |
632 |
|> fold (add_isar_dep facts) (these isar_deps) |
|
48392
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
633 |
|> map fix_name |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
634 |
in |
48404 | 635 |
if verbose andalso auto_level = 0 then |
48392
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
636 |
let val num_facts = length facts in |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
637 |
"MaSh: " ^ quote prover ^ " on " ^ quote (nickname_of th) ^ |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
638 |
" with " ^ string_of_int num_facts ^ " fact" ^ plural_s num_facts ^ |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
639 |
"." |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
640 |
|> Output.urgent_message |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
641 |
end |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
642 |
else |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
643 |
(); |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
644 |
case run_prover_for_mash ctxt params prover facts goal of |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
645 |
{outcome = NONE, used_facts, ...} => |
48404 | 646 |
(if verbose andalso auto_level = 0 then |
48392
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
647 |
let val num_facts = length used_facts in |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
648 |
"Found proof with " ^ string_of_int num_facts ^ " fact" ^ |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
649 |
plural_s num_facts ^ "." |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
650 |
|> Output.urgent_message |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
651 |
end |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
652 |
else |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
653 |
(); |
48665 | 654 |
case used_facts |> map fst |> trim_dependencies th of |
655 |
NONE => (false, isar_deps) |
|
656 |
| atp_deps => (true, atp_deps)) |
|
657 |
| _ => (false, isar_deps) |
|
48392
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
658 |
end |
48251
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
659 |
|
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
660 |
|
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
661 |
(*** High-level communication with MaSh ***) |
6cdcfbddc077
moved most of MaSh exporter code to Sledgehammer
blanchet
parents:
48249
diff
changeset
|
662 |
|
48407 | 663 |
fun num_keys keys = Graph.Keys.fold (K (Integer.add 1)) keys 0 |
48400 | 664 |
|
48407 | 665 |
fun maximal_in_graph fact_G facts = |
48316
252f45c04042
drastic overhaul of MaSh data structures + fixed a few performance issues
blanchet
parents:
48315
diff
changeset
|
666 |
let |
48378 | 667 |
val facts = [] |> fold (cons o nickname_of o snd) facts |
48407 | 668 |
val tab = Symtab.empty |> fold (fn name => Symtab.default (name, ())) facts |
669 |
fun insert_new seen name = |
|
670 |
not (Symtab.defined seen name) ? insert (op =) name |
|
671 |
fun find_maxes _ (maxs, []) = map snd maxs |
|
672 |
| find_maxes seen (maxs, new :: news) = |
|
673 |
find_maxes |
|
674 |
(seen |> num_keys (Graph.imm_succs fact_G new) > 1 |
|
675 |
? Symtab.default (new, ())) |
|
676 |
(if Symtab.defined tab new then |
|
677 |
let |
|
678 |
val newp = Graph.all_preds fact_G [new] |
|
679 |
fun is_ancestor x yp = member (op =) yp x |
|
680 |
val maxs = |
|
681 |
maxs |> filter (fn (_, max) => not (is_ancestor max newp)) |
|
682 |
in |
|
683 |
if exists (is_ancestor new o fst) maxs then |
|
684 |
(maxs, news) |
|
685 |
else |
|
686 |
((newp, new) |
|
687 |
:: filter_out (fn (_, max) => is_ancestor max newp) maxs, |
|
688 |
news) |
|
689 |
end |
|
690 |
else |
|
691 |
(maxs, Graph.Keys.fold (insert_new seen) |
|
692 |
(Graph.imm_preds fact_G new) news)) |
|
693 |
in find_maxes Symtab.empty ([], Graph.maximals fact_G) end |
|
48316
252f45c04042
drastic overhaul of MaSh data structures + fixed a few performance issues
blanchet
parents:
48315
diff
changeset
|
694 |
|
48318 | 695 |
(* Generate more suggestions than requested, because some might be thrown out |
696 |
later for various reasons and "meshing" gives better results with some |
|
697 |
slack. *) |
|
48434
aaaec69db3db
ensure all calls to "mash" program are synchronous
blanchet
parents:
48433
diff
changeset
|
698 |
fun max_suggs_of max_facts = max_facts + Int.min (50, max_facts) |
48318 | 699 |
|
48400 | 700 |
fun is_fact_in_graph fact_G (_, th) = |
701 |
can (Graph.get_node fact_G) (nickname_of th) |
|
48320
891a24a48155
improved meshing of MaSh and Meng--Paulson if some MaSh suggestions are cut-off (the common case)
blanchet
parents:
48319
diff
changeset
|
702 |
|
48442 | 703 |
fun interleave 0 _ _ = [] |
704 |
| interleave n [] ys = take n ys |
|
705 |
| interleave n xs [] = take n xs |
|
706 |
| interleave 1 (x :: _) _ = [x] |
|
707 |
| interleave n (x :: xs) (y :: ys) = x :: y :: interleave (n - 2) xs ys |
|
48435 | 708 |
|
50340
72519bf5f135
simplify MaSh term patterns + add missing global facts if there aren't too many
blanchet
parents:
50339
diff
changeset
|
709 |
(* factor that controls whether unknown global facts should be included *) |
72519bf5f135
simplify MaSh term patterns + add missing global facts if there aren't too many
blanchet
parents:
50339
diff
changeset
|
710 |
val include_unk_global_factor = 15 |
72519bf5f135
simplify MaSh term patterns + add missing global facts if there aren't too many
blanchet
parents:
50339
diff
changeset
|
711 |
|
50382 | 712 |
val weight_mash_facts = weight_mepo_facts (* use MePo weights for now *) |
713 |
||
48406 | 714 |
fun mash_suggested_facts ctxt ({overlord, ...} : params) prover max_facts hyp_ts |
715 |
concl_t facts = |
|
48301 | 716 |
let |
48302 | 717 |
val thy = Proof_Context.theory_of ctxt |
48434
aaaec69db3db
ensure all calls to "mash" program are synchronous
blanchet
parents:
48433
diff
changeset
|
718 |
val (fact_G, suggs) = |
48699 | 719 |
mash_peek ctxt (fn {fact_G, ...} => |
48434
aaaec69db3db
ensure all calls to "mash" program are synchronous
blanchet
parents:
48433
diff
changeset
|
720 |
if Graph.is_empty fact_G then |
aaaec69db3db
ensure all calls to "mash" program are synchronous
blanchet
parents:
48433
diff
changeset
|
721 |
(fact_G, []) |
aaaec69db3db
ensure all calls to "mash" program are synchronous
blanchet
parents:
48433
diff
changeset
|
722 |
else |
aaaec69db3db
ensure all calls to "mash" program are synchronous
blanchet
parents:
48433
diff
changeset
|
723 |
let |
aaaec69db3db
ensure all calls to "mash" program are synchronous
blanchet
parents:
48433
diff
changeset
|
724 |
val parents = maximal_in_graph fact_G facts |
aaaec69db3db
ensure all calls to "mash" program are synchronous
blanchet
parents:
48433
diff
changeset
|
725 |
val feats = |
aaaec69db3db
ensure all calls to "mash" program are synchronous
blanchet
parents:
48433
diff
changeset
|
726 |
features_of ctxt prover thy (Local, General) (concl_t :: hyp_ts) |
aaaec69db3db
ensure all calls to "mash" program are synchronous
blanchet
parents:
48433
diff
changeset
|
727 |
in |
aaaec69db3db
ensure all calls to "mash" program are synchronous
blanchet
parents:
48433
diff
changeset
|
728 |
(fact_G, mash_QUERY ctxt overlord (max_suggs_of max_facts) |
aaaec69db3db
ensure all calls to "mash" program are synchronous
blanchet
parents:
48433
diff
changeset
|
729 |
(parents, feats)) |
aaaec69db3db
ensure all calls to "mash" program are synchronous
blanchet
parents:
48433
diff
changeset
|
730 |
end) |
50339
d8dae91f3107
MaSh improvements: deeper patterns + more respect for chained facts
blanchet
parents:
50338
diff
changeset
|
731 |
val (chained, unchained) = |
d8dae91f3107
MaSh improvements: deeper patterns + more respect for chained facts
blanchet
parents:
50338
diff
changeset
|
732 |
List.partition (fn ((_, (scope, _)), _) => scope = Chained) facts |
48435 | 733 |
val sels = |
48408 | 734 |
facts |> suggested_facts suggs |
50339
d8dae91f3107
MaSh improvements: deeper patterns + more respect for chained facts
blanchet
parents:
50338
diff
changeset
|
735 |
(* The weights currently returned by "mash.py" are too spaced out to |
48408 | 736 |
make any sense. *) |
48435 | 737 |
|> map fst |
50339
d8dae91f3107
MaSh improvements: deeper patterns + more respect for chained facts
blanchet
parents:
50338
diff
changeset
|
738 |
|> filter_out (member (Thm.eq_thm_prop o pairself snd) chained) |
48435 | 739 |
val (unk_global, unk_local) = |
50339
d8dae91f3107
MaSh improvements: deeper patterns + more respect for chained facts
blanchet
parents:
50338
diff
changeset
|
740 |
unchained |> filter_out (is_fact_in_graph fact_G) |
d8dae91f3107
MaSh improvements: deeper patterns + more respect for chained facts
blanchet
parents:
50338
diff
changeset
|
741 |
|> List.partition (fn ((_, (scope, _)), _) => scope = Global) |
50340
72519bf5f135
simplify MaSh term patterns + add missing global facts if there aren't too many
blanchet
parents:
50339
diff
changeset
|
742 |
val (small_unk_global, big_unk_global) = |
72519bf5f135
simplify MaSh term patterns + add missing global facts if there aren't too many
blanchet
parents:
50339
diff
changeset
|
743 |
([], unk_global) |
72519bf5f135
simplify MaSh term patterns + add missing global facts if there aren't too many
blanchet
parents:
50339
diff
changeset
|
744 |
|> include_unk_global_factor * length unk_global <= max_facts ? swap |
50339
d8dae91f3107
MaSh improvements: deeper patterns + more respect for chained facts
blanchet
parents:
50338
diff
changeset
|
745 |
in |
50382 | 746 |
(interleave max_facts (chained @ unk_local @ small_unk_global) sels, |
50340
72519bf5f135
simplify MaSh term patterns + add missing global facts if there aren't too many
blanchet
parents:
50339
diff
changeset
|
747 |
big_unk_global) |
50339
d8dae91f3107
MaSh improvements: deeper patterns + more respect for chained facts
blanchet
parents:
50338
diff
changeset
|
748 |
end |
48249 | 749 |
|
48668
5d63c23b4042
remember which MaSh proofs were found using ATPs
blanchet
parents:
48667
diff
changeset
|
750 |
fun add_wrt_fact_graph ctxt (name, parents, feats, deps) (adds, graph) = |
48316
252f45c04042
drastic overhaul of MaSh data structures + fixed a few performance issues
blanchet
parents:
48315
diff
changeset
|
751 |
let |
252f45c04042
drastic overhaul of MaSh data structures + fixed a few performance issues
blanchet
parents:
48315
diff
changeset
|
752 |
fun maybe_add_from from (accum as (parents, graph)) = |
48321 | 753 |
try_graph ctxt "updating graph" accum (fn () => |
754 |
(from :: parents, Graph.add_edge_acyclic (from, name) graph)) |
|
48669
cdcdb0547f29
remember ATP flops to avoid repeating them too quickly
blanchet
parents:
48668
diff
changeset
|
755 |
val graph = graph |> Graph.default_node (name, Isar_Proof) |
48316
252f45c04042
drastic overhaul of MaSh data structures + fixed a few performance issues
blanchet
parents:
48315
diff
changeset
|
756 |
val (parents, graph) = ([], graph) |> fold maybe_add_from parents |
48407 | 757 |
val (deps, _) = ([], graph) |> fold maybe_add_from deps |
48404 | 758 |
in ((name, parents, feats, deps) :: adds, graph) end |
48306 | 759 |
|
48668
5d63c23b4042
remember which MaSh proofs were found using ATPs
blanchet
parents:
48667
diff
changeset
|
760 |
fun reprove_wrt_fact_graph ctxt (name, deps) (reps, graph) = |
5d63c23b4042
remember which MaSh proofs were found using ATPs
blanchet
parents:
48667
diff
changeset
|
761 |
let |
5d63c23b4042
remember which MaSh proofs were found using ATPs
blanchet
parents:
48667
diff
changeset
|
762 |
fun maybe_rep_from from (accum as (parents, graph)) = |
5d63c23b4042
remember which MaSh proofs were found using ATPs
blanchet
parents:
48667
diff
changeset
|
763 |
try_graph ctxt "updating graph" accum (fn () => |
5d63c23b4042
remember which MaSh proofs were found using ATPs
blanchet
parents:
48667
diff
changeset
|
764 |
(from :: parents, Graph.add_edge_acyclic (from, name) graph)) |
48669
cdcdb0547f29
remember ATP flops to avoid repeating them too quickly
blanchet
parents:
48668
diff
changeset
|
765 |
val graph = graph |> update_fact_graph_node (name, ATP_Proof) |
48668
5d63c23b4042
remember which MaSh proofs were found using ATPs
blanchet
parents:
48667
diff
changeset
|
766 |
val (deps, _) = ([], graph) |> fold maybe_rep_from deps |
5d63c23b4042
remember which MaSh proofs were found using ATPs
blanchet
parents:
48667
diff
changeset
|
767 |
in ((name, deps) :: reps, graph) end |
5d63c23b4042
remember which MaSh proofs were found using ATPs
blanchet
parents:
48667
diff
changeset
|
768 |
|
48669
cdcdb0547f29
remember ATP flops to avoid repeating them too quickly
blanchet
parents:
48668
diff
changeset
|
769 |
fun flop_wrt_fact_graph name = |
cdcdb0547f29
remember ATP flops to avoid repeating them too quickly
blanchet
parents:
48668
diff
changeset
|
770 |
update_fact_graph_node (name, Isar_Proof_wegen_ATP_Flop) |
cdcdb0547f29
remember ATP flops to avoid repeating them too quickly
blanchet
parents:
48668
diff
changeset
|
771 |
|
48384
83dc102041e6
learn on explicit "min" command but do the learning in a thread, since it may take a couple of seconds
blanchet
parents:
48383
diff
changeset
|
772 |
val learn_timeout_slack = 2.0 |
48318 | 773 |
|
48384
83dc102041e6
learn on explicit "min" command but do the learning in a thread, since it may take a couple of seconds
blanchet
parents:
48383
diff
changeset
|
774 |
fun launch_thread timeout task = |
48383 | 775 |
let |
48384
83dc102041e6
learn on explicit "min" command but do the learning in a thread, since it may take a couple of seconds
blanchet
parents:
48383
diff
changeset
|
776 |
val hard_timeout = time_mult learn_timeout_slack timeout |
83dc102041e6
learn on explicit "min" command but do the learning in a thread, since it may take a couple of seconds
blanchet
parents:
48383
diff
changeset
|
777 |
val birth_time = Time.now () |
83dc102041e6
learn on explicit "min" command but do the learning in a thread, since it may take a couple of seconds
blanchet
parents:
48383
diff
changeset
|
778 |
val death_time = Time.+ (birth_time, hard_timeout) |
48442 | 779 |
val desc = ("Machine learner for Sledgehammer", "") |
48384
83dc102041e6
learn on explicit "min" command but do the learning in a thread, since it may take a couple of seconds
blanchet
parents:
48383
diff
changeset
|
780 |
in Async_Manager.launch MaShN birth_time death_time desc task end |
83dc102041e6
learn on explicit "min" command but do the learning in a thread, since it may take a couple of seconds
blanchet
parents:
48383
diff
changeset
|
781 |
|
48400 | 782 |
fun freshish_name () = |
783 |
Date.fmt ".%Y_%m_%d_%H_%M_%S__" (Date.fromTimeLocal (Time.now ())) ^ |
|
784 |
serial_string () |
|
785 |
||
48384
83dc102041e6
learn on explicit "min" command but do the learning in a thread, since it may take a couple of seconds
blanchet
parents:
48383
diff
changeset
|
786 |
fun mash_learn_proof ctxt ({overlord, timeout, ...} : params) prover t facts |
83dc102041e6
learn on explicit "min" command but do the learning in a thread, since it may take a couple of seconds
blanchet
parents:
48383
diff
changeset
|
787 |
used_ths = |
83dc102041e6
learn on explicit "min" command but do the learning in a thread, since it may take a couple of seconds
blanchet
parents:
48383
diff
changeset
|
788 |
if is_smt_prover ctxt prover then |
83dc102041e6
learn on explicit "min" command but do the learning in a thread, since it may take a couple of seconds
blanchet
parents:
48383
diff
changeset
|
789 |
() |
83dc102041e6
learn on explicit "min" command but do the learning in a thread, since it may take a couple of seconds
blanchet
parents:
48383
diff
changeset
|
790 |
else |
48403
1f214c653c80
don't store fresh names in fact graph, since these cannot be the parents of any other facts
blanchet
parents:
48401
diff
changeset
|
791 |
launch_thread timeout (fn () => |
1f214c653c80
don't store fresh names in fact graph, since these cannot be the parents of any other facts
blanchet
parents:
48401
diff
changeset
|
792 |
let |
1f214c653c80
don't store fresh names in fact graph, since these cannot be the parents of any other facts
blanchet
parents:
48401
diff
changeset
|
793 |
val thy = Proof_Context.theory_of ctxt |
1f214c653c80
don't store fresh names in fact graph, since these cannot be the parents of any other facts
blanchet
parents:
48401
diff
changeset
|
794 |
val name = freshish_name () |
1f214c653c80
don't store fresh names in fact graph, since these cannot be the parents of any other facts
blanchet
parents:
48401
diff
changeset
|
795 |
val feats = features_of ctxt prover thy (Local, General) [t] |
1f214c653c80
don't store fresh names in fact graph, since these cannot be the parents of any other facts
blanchet
parents:
48401
diff
changeset
|
796 |
val deps = used_ths |> map nickname_of |
1f214c653c80
don't store fresh names in fact graph, since these cannot be the parents of any other facts
blanchet
parents:
48401
diff
changeset
|
797 |
in |
48699 | 798 |
mash_peek ctxt (fn {fact_G, ...} => |
48434
aaaec69db3db
ensure all calls to "mash" program are synchronous
blanchet
parents:
48433
diff
changeset
|
799 |
let val parents = maximal_in_graph fact_G facts in |
aaaec69db3db
ensure all calls to "mash" program are synchronous
blanchet
parents:
48433
diff
changeset
|
800 |
mash_ADD ctxt overlord [(name, parents, feats, deps)] |
aaaec69db3db
ensure all calls to "mash" program are synchronous
blanchet
parents:
48433
diff
changeset
|
801 |
end); |
aaaec69db3db
ensure all calls to "mash" program are synchronous
blanchet
parents:
48433
diff
changeset
|
802 |
(true, "") |
48403
1f214c653c80
don't store fresh names in fact graph, since these cannot be the parents of any other facts
blanchet
parents:
48401
diff
changeset
|
803 |
end) |
48383 | 804 |
|
50163
c62ce309dc26
more abstract Sendback operations, with explicit id/exec_id properties;
wenzelm
parents:
50047
diff
changeset
|
805 |
fun sendback sub = Sendback.markup (sledgehammerN ^ " " ^ sub) |
48392
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
806 |
|
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
807 |
val commit_timeout = seconds 30.0 |
48332
271a4a6af734
optimize parent computation in MaSh + remove temporary files
blanchet
parents:
48330
diff
changeset
|
808 |
|
48318 | 809 |
(* The timeout is understood in a very slack fashion. *) |
48404 | 810 |
fun mash_learn_facts ctxt (params as {debug, verbose, overlord, ...}) prover |
811 |
auto_level atp learn_timeout facts = |
|
48304 | 812 |
let |
48318 | 813 |
val timer = Timer.startRealTimer () |
48392
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
814 |
fun next_commit_time () = |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
815 |
Time.+ (Timer.checkRealTimer timer, commit_timeout) |
48699 | 816 |
val {fact_G, ...} = mash_get ctxt |
48400 | 817 |
val (old_facts, new_facts) = |
48667
ac58317ef11f
rule out same "technical" theories for MePo as for MaSh
blanchet
parents:
48665
diff
changeset
|
818 |
facts |> List.partition (is_fact_in_graph fact_G) |
48400 | 819 |
||> sort (thm_ord o pairself snd) |
48308 | 820 |
in |
48404 | 821 |
if null new_facts andalso (not atp orelse null old_facts) then |
822 |
if auto_level < 2 then |
|
823 |
"No new " ^ (if atp then "ATP" else "Isar") ^ " proofs to learn." ^ |
|
824 |
(if auto_level = 0 andalso not atp then |
|
825 |
"\n\nHint: Try " ^ sendback learn_atpN ^ " to learn from ATP proofs." |
|
826 |
else |
|
827 |
"") |
|
48392
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
828 |
else |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
829 |
"" |
48308 | 830 |
else |
48304 | 831 |
let |
48315
82d6e46c673f
fixed order of accessibles + other tweaks to MaSh
blanchet
parents:
48314
diff
changeset
|
832 |
val all_names = |
49997
dbbf9578e712
made MaSh more robust in the face of duplicate "nicknames" (which can happen e.g. if you have a lemma called foo(1) and another called foo_1 in the same theory)
blanchet
parents:
48699
diff
changeset
|
833 |
Symtab.empty |
dbbf9578e712
made MaSh more robust in the face of duplicate "nicknames" (which can happen e.g. if you have a lemma called foo(1) and another called foo_1 in the same theory)
blanchet
parents:
48699
diff
changeset
|
834 |
|> fold Symtab.update (facts |> map (rpair () o nickname_of o snd)) |
48439
67a6bcbd3587
removed MaSh junk arising from primrec definitions
blanchet
parents:
48438
diff
changeset
|
835 |
fun deps_of status th = |
67a6bcbd3587
removed MaSh junk arising from primrec definitions
blanchet
parents:
48438
diff
changeset
|
836 |
if status = Non_Rec_Def orelse status = Rec_Def then |
67a6bcbd3587
removed MaSh junk arising from primrec definitions
blanchet
parents:
48438
diff
changeset
|
837 |
SOME [] |
67a6bcbd3587
removed MaSh junk arising from primrec definitions
blanchet
parents:
48438
diff
changeset
|
838 |
else if atp then |
67a6bcbd3587
removed MaSh junk arising from primrec definitions
blanchet
parents:
48438
diff
changeset
|
839 |
atp_dependencies_of ctxt params prover auto_level facts all_names th |
48665 | 840 |
|> (fn (false, _) => NONE | (true, deps) => deps) |
48404 | 841 |
else |
48439
67a6bcbd3587
removed MaSh junk arising from primrec definitions
blanchet
parents:
48438
diff
changeset
|
842 |
isar_dependencies_of all_names th |
48669
cdcdb0547f29
remember ATP flops to avoid repeating them too quickly
blanchet
parents:
48668
diff
changeset
|
843 |
fun do_commit [] [] [] state = state |
48699 | 844 |
| do_commit adds reps flops {fact_G, dirty} = |
48392
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
845 |
let |
48699 | 846 |
val was_empty = Graph.is_empty fact_G |
48404 | 847 |
val (adds, fact_G) = |
48668
5d63c23b4042
remember which MaSh proofs were found using ATPs
blanchet
parents:
48667
diff
changeset
|
848 |
([], fact_G) |> fold (add_wrt_fact_graph ctxt) adds |
5d63c23b4042
remember which MaSh proofs were found using ATPs
blanchet
parents:
48667
diff
changeset
|
849 |
val (reps, fact_G) = |
5d63c23b4042
remember which MaSh proofs were found using ATPs
blanchet
parents:
48667
diff
changeset
|
850 |
([], fact_G) |> fold (reprove_wrt_fact_graph ctxt) reps |
48669
cdcdb0547f29
remember ATP flops to avoid repeating them too quickly
blanchet
parents:
48668
diff
changeset
|
851 |
val fact_G = fact_G |> fold flop_wrt_fact_graph flops |
48699 | 852 |
val dirty = |
853 |
case (was_empty, dirty, reps) of |
|
854 |
(false, SOME names, []) => SOME (map #1 adds @ names) |
|
855 |
| _ => NONE |
|
48404 | 856 |
in |
857 |
mash_ADD ctxt overlord (rev adds); |
|
858 |
mash_REPROVE ctxt overlord reps; |
|
48699 | 859 |
{fact_G = fact_G, dirty = dirty} |
48404 | 860 |
end |
48669
cdcdb0547f29
remember ATP flops to avoid repeating them too quickly
blanchet
parents:
48668
diff
changeset
|
861 |
fun commit last adds reps flops = |
48404 | 862 |
(if debug andalso auto_level = 0 then |
863 |
Output.urgent_message "Committing..." |
|
864 |
else |
|
865 |
(); |
|
48669
cdcdb0547f29
remember ATP flops to avoid repeating them too quickly
blanchet
parents:
48668
diff
changeset
|
866 |
mash_map ctxt (do_commit (rev adds) reps flops); |
48404 | 867 |
if not last andalso auto_level = 0 then |
868 |
let val num_proofs = length adds + length reps in |
|
869 |
"Learned " ^ string_of_int num_proofs ^ " " ^ |
|
870 |
(if atp then "ATP" else "Isar") ^ " proof" ^ |
|
871 |
plural_s num_proofs ^ " in the last " ^ |
|
48392
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
872 |
string_from_time commit_timeout ^ "." |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
873 |
|> Output.urgent_message |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
874 |
end |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
875 |
else |
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
876 |
()) |
48404 | 877 |
fun learn_new_fact _ (accum as (_, (_, _, _, true))) = accum |
48439
67a6bcbd3587
removed MaSh junk arising from primrec definitions
blanchet
parents:
48438
diff
changeset
|
878 |
| learn_new_fact ((_, stature as (_, status)), th) |
48404 | 879 |
(adds, (parents, n, next_commit, _)) = |
48318 | 880 |
let |
48378 | 881 |
val name = nickname_of th |
48332
271a4a6af734
optimize parent computation in MaSh + remove temporary files
blanchet
parents:
48330
diff
changeset
|
882 |
val feats = |
48385 | 883 |
features_of ctxt prover (theory_of_thm th) stature [prop_of th] |
48439
67a6bcbd3587
removed MaSh junk arising from primrec definitions
blanchet
parents:
48438
diff
changeset
|
884 |
val deps = deps_of status th |> these |
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
|
885 |
val n = n |> not (null deps) ? Integer.add 1 |
48404 | 886 |
val adds = (name, parents, feats, deps) :: adds |
887 |
val (adds, next_commit) = |
|
48392
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
888 |
if Time.> (Timer.checkRealTimer timer, next_commit) then |
48669
cdcdb0547f29
remember ATP flops to avoid repeating them too quickly
blanchet
parents:
48668
diff
changeset
|
889 |
(commit false adds [] []; ([], next_commit_time ())) |
48392
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
890 |
else |
48404 | 891 |
(adds, next_commit) |
892 |
val timed_out = Time.> (Timer.checkRealTimer timer, learn_timeout) |
|
893 |
in (adds, ([name], n, next_commit, timed_out)) end |
|
894 |
val n = |
|
895 |
if null new_facts then |
|
896 |
0 |
|
897 |
else |
|
898 |
let |
|
899 |
val last_th = new_facts |> List.last |> snd |
|
900 |
(* crude approximation *) |
|
901 |
val ancestors = |
|
902 |
old_facts |
|
903 |
|> filter (fn (_, th) => thm_ord (th, last_th) <> GREATER) |
|
48407 | 904 |
val parents = maximal_in_graph fact_G ancestors |
48404 | 905 |
val (adds, (_, n, _, _)) = |
906 |
([], (parents, 0, next_commit_time (), false)) |
|
907 |
|> fold learn_new_fact new_facts |
|
48669
cdcdb0547f29
remember ATP flops to avoid repeating them too quickly
blanchet
parents:
48668
diff
changeset
|
908 |
in commit true adds [] []; n end |
48404 | 909 |
fun relearn_old_fact _ (accum as (_, (_, _, true))) = accum |
48439
67a6bcbd3587
removed MaSh junk arising from primrec definitions
blanchet
parents:
48438
diff
changeset
|
910 |
| relearn_old_fact ((_, (_, status)), th) |
48669
cdcdb0547f29
remember ATP flops to avoid repeating them too quickly
blanchet
parents:
48668
diff
changeset
|
911 |
((reps, flops), (n, next_commit, _)) = |
48404 | 912 |
let |
913 |
val name = nickname_of th |
|
48669
cdcdb0547f29
remember ATP flops to avoid repeating them too quickly
blanchet
parents:
48668
diff
changeset
|
914 |
val (n, reps, flops) = |
48439
67a6bcbd3587
removed MaSh junk arising from primrec definitions
blanchet
parents:
48438
diff
changeset
|
915 |
case deps_of status th of |
48669
cdcdb0547f29
remember ATP flops to avoid repeating them too quickly
blanchet
parents:
48668
diff
changeset
|
916 |
SOME deps => (n + 1, (name, deps) :: reps, flops) |
cdcdb0547f29
remember ATP flops to avoid repeating them too quickly
blanchet
parents:
48668
diff
changeset
|
917 |
| NONE => (n, reps, name :: flops) |
cdcdb0547f29
remember ATP flops to avoid repeating them too quickly
blanchet
parents:
48668
diff
changeset
|
918 |
val (reps, flops, next_commit) = |
48404 | 919 |
if Time.> (Timer.checkRealTimer timer, next_commit) then |
48669
cdcdb0547f29
remember ATP flops to avoid repeating them too quickly
blanchet
parents:
48668
diff
changeset
|
920 |
(commit false [] reps flops; ([], [], next_commit_time ())) |
48404 | 921 |
else |
48669
cdcdb0547f29
remember ATP flops to avoid repeating them too quickly
blanchet
parents:
48668
diff
changeset
|
922 |
(reps, flops, next_commit) |
48404 | 923 |
val timed_out = Time.> (Timer.checkRealTimer timer, learn_timeout) |
48669
cdcdb0547f29
remember ATP flops to avoid repeating them too quickly
blanchet
parents:
48668
diff
changeset
|
924 |
in ((reps, flops), (n, next_commit, timed_out)) end |
48404 | 925 |
val n = |
48433 | 926 |
if not atp orelse null old_facts then |
48404 | 927 |
n |
928 |
else |
|
929 |
let |
|
48668
5d63c23b4042
remember which MaSh proofs were found using ATPs
blanchet
parents:
48667
diff
changeset
|
930 |
val max_isar = 1000 * max_dependencies |
48669
cdcdb0547f29
remember ATP flops to avoid repeating them too quickly
blanchet
parents:
48668
diff
changeset
|
931 |
fun kind_of_proof th = |
48668
5d63c23b4042
remember which MaSh proofs were found using ATPs
blanchet
parents:
48667
diff
changeset
|
932 |
try (Graph.get_node fact_G) (nickname_of th) |
48669
cdcdb0547f29
remember ATP flops to avoid repeating them too quickly
blanchet
parents:
48668
diff
changeset
|
933 |
|> the_default Isar_Proof |
48406 | 934 |
fun priority_of (_, th) = |
48668
5d63c23b4042
remember which MaSh proofs were found using ATPs
blanchet
parents:
48667
diff
changeset
|
935 |
random_range 0 max_isar |
48669
cdcdb0547f29
remember ATP flops to avoid repeating them too quickly
blanchet
parents:
48668
diff
changeset
|
936 |
+ (case kind_of_proof th of |
cdcdb0547f29
remember ATP flops to avoid repeating them too quickly
blanchet
parents:
48668
diff
changeset
|
937 |
Isar_Proof => 0 |
cdcdb0547f29
remember ATP flops to avoid repeating them too quickly
blanchet
parents:
48668
diff
changeset
|
938 |
| ATP_Proof => 2 * max_isar |
cdcdb0547f29
remember ATP flops to avoid repeating them too quickly
blanchet
parents:
48668
diff
changeset
|
939 |
| Isar_Proof_wegen_ATP_Flop => max_isar) |
48404 | 940 |
- 500 * (th |> isar_dependencies_of all_names |
941 |
|> Option.map length |
|
942 |
|> the_default max_dependencies) |
|
943 |
val old_facts = |
|
48406 | 944 |
old_facts |> map (`priority_of) |
48404 | 945 |
|> sort (int_ord o pairself fst) |
946 |
|> map snd |
|
48669
cdcdb0547f29
remember ATP flops to avoid repeating them too quickly
blanchet
parents:
48668
diff
changeset
|
947 |
val ((reps, flops), (n, _, _)) = |
cdcdb0547f29
remember ATP flops to avoid repeating them too quickly
blanchet
parents:
48668
diff
changeset
|
948 |
(([], []), (n, next_commit_time (), false)) |
48404 | 949 |
|> fold relearn_old_fact old_facts |
48669
cdcdb0547f29
remember ATP flops to avoid repeating them too quickly
blanchet
parents:
48668
diff
changeset
|
950 |
in commit true [] reps flops; n end |
48318 | 951 |
in |
48404 | 952 |
if verbose orelse auto_level < 2 then |
953 |
"Learned " ^ string_of_int n ^ " nontrivial " ^ |
|
954 |
(if atp then "ATP" else "Isar") ^ " proof" ^ plural_s n ^ |
|
48319
340187063d84
use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents:
48318
diff
changeset
|
955 |
(if verbose then |
340187063d84
use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents:
48318
diff
changeset
|
956 |
" in " ^ string_from_time (Timer.checkRealTimer timer) |
340187063d84
use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents:
48318
diff
changeset
|
957 |
else |
340187063d84
use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents:
48318
diff
changeset
|
958 |
"") ^ "." |
340187063d84
use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents:
48318
diff
changeset
|
959 |
else |
340187063d84
use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents:
48318
diff
changeset
|
960 |
"" |
48318 | 961 |
end |
48308 | 962 |
end |
48304 | 963 |
|
48404 | 964 |
fun mash_learn ctxt (params as {provers, timeout, ...}) fact_override chained |
965 |
atp = |
|
48316
252f45c04042
drastic overhaul of MaSh data structures + fixed a few performance issues
blanchet
parents:
48315
diff
changeset
|
966 |
let |
48396 | 967 |
val css = Sledgehammer_Fact.clasimpset_rule_table_of ctxt |
48395
85a7fb65507a
learning should honor the fact override and the chained facts
blanchet
parents:
48394
diff
changeset
|
968 |
val ctxt = ctxt |> Config.put instantiate_inducts false |
85a7fb65507a
learning should honor the fact override and the chained facts
blanchet
parents:
48394
diff
changeset
|
969 |
val facts = |
48396 | 970 |
nearly_all_facts ctxt false fact_override Symtab.empty css chained [] |
971 |
@{prop True} |
|
48404 | 972 |
val num_facts = length facts |
973 |
val prover = hd provers |
|
974 |
fun learn auto_level atp = |
|
975 |
mash_learn_facts ctxt params prover auto_level atp infinite_timeout facts |
|
976 |
|> Output.urgent_message |
|
48316
252f45c04042
drastic overhaul of MaSh data structures + fixed a few performance issues
blanchet
parents:
48315
diff
changeset
|
977 |
in |
50340
72519bf5f135
simplify MaSh term patterns + add missing global facts if there aren't too many
blanchet
parents:
50339
diff
changeset
|
978 |
if atp then |
72519bf5f135
simplify MaSh term patterns + add missing global facts if there aren't too many
blanchet
parents:
50339
diff
changeset
|
979 |
("MaShing through " ^ string_of_int num_facts ^ " fact" ^ |
72519bf5f135
simplify MaSh term patterns + add missing global facts if there aren't too many
blanchet
parents:
50339
diff
changeset
|
980 |
plural_s num_facts ^ " for ATP proofs (" ^ quote prover ^ " timeout: " ^ |
72519bf5f135
simplify MaSh term patterns + add missing global facts if there aren't too many
blanchet
parents:
50339
diff
changeset
|
981 |
string_from_time timeout ^ ").\n\nCollecting Isar proofs first..." |
72519bf5f135
simplify MaSh term patterns + add missing global facts if there aren't too many
blanchet
parents:
50339
diff
changeset
|
982 |
|> Output.urgent_message; |
72519bf5f135
simplify MaSh term patterns + add missing global facts if there aren't too many
blanchet
parents:
50339
diff
changeset
|
983 |
learn 1 false; |
72519bf5f135
simplify MaSh term patterns + add missing global facts if there aren't too many
blanchet
parents:
50339
diff
changeset
|
984 |
"Now collecting ATP proofs. This may take several hours. You can \ |
72519bf5f135
simplify MaSh term patterns + add missing global facts if there aren't too many
blanchet
parents:
50339
diff
changeset
|
985 |
\safely stop the learning process at any point." |
72519bf5f135
simplify MaSh term patterns + add missing global facts if there aren't too many
blanchet
parents:
50339
diff
changeset
|
986 |
|> Output.urgent_message; |
72519bf5f135
simplify MaSh term patterns + add missing global facts if there aren't too many
blanchet
parents:
50339
diff
changeset
|
987 |
learn 0 true) |
72519bf5f135
simplify MaSh term patterns + add missing global facts if there aren't too many
blanchet
parents:
50339
diff
changeset
|
988 |
else |
72519bf5f135
simplify MaSh term patterns + add missing global facts if there aren't too many
blanchet
parents:
50339
diff
changeset
|
989 |
("MaShing through " ^ string_of_int num_facts ^ " fact" ^ |
72519bf5f135
simplify MaSh term patterns + add missing global facts if there aren't too many
blanchet
parents:
50339
diff
changeset
|
990 |
plural_s num_facts ^ " for Isar proofs..." |
72519bf5f135
simplify MaSh term patterns + add missing global facts if there aren't too many
blanchet
parents:
50339
diff
changeset
|
991 |
|> Output.urgent_message; |
72519bf5f135
simplify MaSh term patterns + add missing global facts if there aren't too many
blanchet
parents:
50339
diff
changeset
|
992 |
learn 0 false) |
48316
252f45c04042
drastic overhaul of MaSh data structures + fixed a few performance issues
blanchet
parents:
48315
diff
changeset
|
993 |
end |
48249 | 994 |
|
50311 | 995 |
fun is_mash_enabled () = (getenv "MASH" = "yes") |
996 |
fun mash_can_suggest_facts ctxt = not (Graph.is_empty (#fact_G (mash_get ctxt))) |
|
997 |
||
48318 | 998 |
(* The threshold should be large enough so that MaSh doesn't kick in for Auto |
999 |
Sledgehammer and Try. *) |
|
1000 |
val min_secs_for_learning = 15 |
|
1001 |
||
48321 | 1002 |
fun relevant_facts ctxt (params as {learn, fact_filter, timeout, ...}) prover |
1003 |
max_facts ({add, only, ...} : fact_override) hyp_ts concl_t facts = |
|
48314
ee33ba3c0e05
added option to control which fact filter is used
blanchet
parents:
48313
diff
changeset
|
1004 |
if not (subset (op =) (the_list fact_filter, fact_filters)) then |
ee33ba3c0e05
added option to control which fact filter is used
blanchet
parents:
48313
diff
changeset
|
1005 |
error ("Unknown fact filter: " ^ quote (the fact_filter) ^ ".") |
ee33ba3c0e05
added option to control which fact filter is used
blanchet
parents:
48313
diff
changeset
|
1006 |
else if only then |
48289 | 1007 |
facts |
48321 | 1008 |
else if max_facts <= 0 orelse null facts then |
48288
255c6e1fd505
rationalize relevance filter, slowing moving code from Iter to MaSh
blanchet
parents:
48251
diff
changeset
|
1009 |
[] |
255c6e1fd505
rationalize relevance filter, slowing moving code from Iter to MaSh
blanchet
parents:
48251
diff
changeset
|
1010 |
else |
255c6e1fd505
rationalize relevance filter, slowing moving code from Iter to MaSh
blanchet
parents:
48251
diff
changeset
|
1011 |
let |
48327
568b3193e53e
don't include hidden facts in relevance filter + tweak MaSh learning
blanchet
parents:
48326
diff
changeset
|
1012 |
fun maybe_learn () = |
48384
83dc102041e6
learn on explicit "min" command but do the learning in a thread, since it may take a couple of seconds
blanchet
parents:
48383
diff
changeset
|
1013 |
if learn andalso not (Async_Manager.has_running_threads MaShN) andalso |
83dc102041e6
learn on explicit "min" command but do the learning in a thread, since it may take a couple of seconds
blanchet
parents:
48383
diff
changeset
|
1014 |
Time.toSeconds timeout >= min_secs_for_learning then |
83dc102041e6
learn on explicit "min" command but do the learning in a thread, since it may take a couple of seconds
blanchet
parents:
48383
diff
changeset
|
1015 |
let val timeout = time_mult learn_timeout_slack timeout in |
83dc102041e6
learn on explicit "min" command but do the learning in a thread, since it may take a couple of seconds
blanchet
parents:
48383
diff
changeset
|
1016 |
launch_thread timeout |
48404 | 1017 |
(fn () => (true, mash_learn_facts ctxt params prover 2 false |
48392
ca998fa08cd9
added "learn_from_atp" command to MaSh, for patient users
blanchet
parents:
48390
diff
changeset
|
1018 |
timeout facts)) |
48319
340187063d84
use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents:
48318
diff
changeset
|
1019 |
end |
48318 | 1020 |
else |
1021 |
() |
|
48314
ee33ba3c0e05
added option to control which fact filter is used
blanchet
parents:
48313
diff
changeset
|
1022 |
val fact_filter = |
ee33ba3c0e05
added option to control which fact filter is used
blanchet
parents:
48313
diff
changeset
|
1023 |
case fact_filter of |
48379
2b5ad61e2ccc
renamed "iter" fact filter to "MePo" (Meng--Paulson)
blanchet
parents:
48378
diff
changeset
|
1024 |
SOME ff => (() |> ff <> mepoN ? maybe_learn; ff) |
48318 | 1025 |
| NONE => |
48407 | 1026 |
if is_smt_prover ctxt prover then |
1027 |
mepoN |
|
50229 | 1028 |
else if is_mash_enabled () then |
48407 | 1029 |
(maybe_learn (); |
1030 |
if mash_can_suggest_facts ctxt then meshN else mepoN) |
|
1031 |
else |
|
1032 |
mepoN |
|
48288
255c6e1fd505
rationalize relevance filter, slowing moving code from Iter to MaSh
blanchet
parents:
48251
diff
changeset
|
1033 |
val add_ths = Attrib.eval_thms ctxt add |
48292 | 1034 |
fun prepend_facts ths accepts = |
48288
255c6e1fd505
rationalize relevance filter, slowing moving code from Iter to MaSh
blanchet
parents:
48251
diff
changeset
|
1035 |
((facts |> filter (member Thm.eq_thm_prop ths o snd)) @ |
48292 | 1036 |
(accepts |> filter_out (member Thm.eq_thm_prop ths o snd))) |
48293 | 1037 |
|> take max_facts |
48406 | 1038 |
fun mepo () = |
50382 | 1039 |
mepo_suggested_facts ctxt params prover max_facts NONE hyp_ts concl_t |
1040 |
facts |
|
1041 |
|> weight_mepo_facts |
|
48314
ee33ba3c0e05
added option to control which fact filter is used
blanchet
parents:
48313
diff
changeset
|
1042 |
fun mash () = |
48406 | 1043 |
mash_suggested_facts ctxt params prover max_facts hyp_ts concl_t facts |
50382 | 1044 |
|>> weight_mash_facts |
48314
ee33ba3c0e05
added option to control which fact filter is used
blanchet
parents:
48313
diff
changeset
|
1045 |
val mess = |
48406 | 1046 |
[] |> (if fact_filter <> mashN then cons (mepo (), []) else I) |
48379
2b5ad61e2ccc
renamed "iter" fact filter to "MePo" (Meng--Paulson)
blanchet
parents:
48378
diff
changeset
|
1047 |
|> (if fact_filter <> mepoN then cons (mash ()) else I) |
48288
255c6e1fd505
rationalize relevance filter, slowing moving code from Iter to MaSh
blanchet
parents:
48251
diff
changeset
|
1048 |
in |
48313
0faafdffa662
mesh facts by taking into consideration whether a fact is known to MeSh
blanchet
parents:
48312
diff
changeset
|
1049 |
mesh_facts max_facts mess |
48288
255c6e1fd505
rationalize relevance filter, slowing moving code from Iter to MaSh
blanchet
parents:
48251
diff
changeset
|
1050 |
|> not (null add_ths) ? prepend_facts add_ths |
255c6e1fd505
rationalize relevance filter, slowing moving code from Iter to MaSh
blanchet
parents:
48251
diff
changeset
|
1051 |
end |
255c6e1fd505
rationalize relevance filter, slowing moving code from Iter to MaSh
blanchet
parents:
48251
diff
changeset
|
1052 |
|
48319
340187063d84
use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents:
48318
diff
changeset
|
1053 |
fun kill_learners () = Async_Manager.kill_threads MaShN "learner" |
340187063d84
use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents:
48318
diff
changeset
|
1054 |
fun running_learners () = Async_Manager.running_threads MaShN "learner" |
340187063d84
use async manager to manage MaSh learners to make sure they get killed cleanly
blanchet
parents:
48318
diff
changeset
|
1055 |
|
48248 | 1056 |
end; |