author | nipkow |
Thu, 10 Sep 2009 15:57:55 +0200 | |
changeset 32551 | 421323205efd |
parent 32521 | f20cc66b2c74 |
child 32564 | 378528d2f7eb |
permissions | -rw-r--r-- |
32381 | 1 |
(* Title: mirabelle.ML |
2 |
Author: Jasmin Blanchette and Sascha Boehme |
|
3 |
*) |
|
4 |
||
5 |
signature MIRABELLE = |
|
6 |
sig |
|
32495 | 7 |
(* configuration *) |
32396 | 8 |
val logfile : string Config.T |
32381 | 9 |
val timeout : int Config.T |
32382
98674ac811c4
Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
32381
diff
changeset
|
10 |
val start_line : int Config.T |
98674ac811c4
Mirabelle tool script conforming to standard Isabelle tool interface,
boehmes
parents:
32381
diff
changeset
|
11 |
val end_line : int Config.T |
32495 | 12 |
val setup : theory -> theory |
32381 | 13 |
|
32495 | 14 |
(* core *) |
32521 | 15 |
type init_action |
16 |
type done_action |
|
17 |
type run_action |
|
32495 | 18 |
type action |
32521 | 19 |
val catch : (int -> string) -> run_action -> run_action |
32515
e7c0d3c0494a
Mirabelle: actions are responsible for catching exceptions and producing suitable log messages (makes log message uniform),
boehmes
parents:
32504
diff
changeset
|
20 |
val register : action -> theory -> theory |
32495 | 21 |
val step_hook : Toplevel.transition -> Toplevel.state -> Toplevel.state -> |
22 |
unit |
|
23 |
||
24 |
(* utility functions *) |
|
32381 | 25 |
val goal_thm_of : Proof.state -> thm |
32469 | 26 |
val can_apply : Time.time -> (Proof.context -> int -> tactic) -> |
27 |
Proof.state -> bool |
|
32381 | 28 |
val theorems_in_proof_term : Thm.thm -> Thm.thm list |
32385
594890623c46
split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents:
32383
diff
changeset
|
29 |
val theorems_of_sucessful_proof : Toplevel.state option -> Thm.thm list |
594890623c46
split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents:
32383
diff
changeset
|
30 |
val get_setting : (string * string) list -> string * string -> string |
594890623c46
split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents:
32383
diff
changeset
|
31 |
val get_int_setting : (string * string) list -> string * int -> int |
32498
1132c7c13f36
Mirabelle: actions are responsible for handling exceptions,
boehmes
parents:
32497
diff
changeset
|
32 |
val cpu_time : ('a -> 'b) -> 'a -> 'b * int |
32381 | 33 |
end |
34 |
||
35 |
||
36 |
||
32497 | 37 |
structure Mirabelle : MIRABELLE = |
32495 | 38 |
struct |
39 |
||
40 |
(* Mirabelle configuration *) |
|
41 |
||
42 |
val (logfile, setup1) = Attrib.config_string "mirabelle_logfile" "" |
|
43 |
val (timeout, setup2) = Attrib.config_int "mirabelle_timeout" 30 |
|
44 |
val (start_line, setup3) = Attrib.config_int "mirabelle_start_line" 0 |
|
45 |
val (end_line, setup4) = Attrib.config_int "mirabelle_end_line" ~1 |
|
46 |
||
47 |
val setup = setup1 #> setup2 #> setup3 #> setup4 |
|
32385
594890623c46
split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents:
32383
diff
changeset
|
48 |
|
594890623c46
split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents:
32383
diff
changeset
|
49 |
|
594890623c46
split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents:
32383
diff
changeset
|
50 |
|
32381 | 51 |
(* Mirabelle core *) |
52 |
||
32521 | 53 |
|
54 |
type init_action = int -> theory -> theory |
|
55 |
type done_action = int -> {last: Toplevel.state, log: string -> unit} -> unit |
|
56 |
type run_action = int -> {pre: Proof.state, post: Toplevel.state option, |
|
32551
421323205efd
position information is now passed to all actions;
nipkow
parents:
32521
diff
changeset
|
57 |
timeout: Time.time, log: string -> unit, pos: Position.T} -> unit |
32521 | 58 |
type action = init_action * run_action * done_action |
32381 | 59 |
|
32385
594890623c46
split actions from Mirabelle core (Mirabelle may thus be applied to basic theories in HOL)
boehmes
parents:
32383
diff
changeset
|
60 |
structure Actions = TheoryDataFun |
32381 | 61 |
( |
32521 | 62 |
type T = (int * run_action * done_action) list |
32515
e7c0d3c0494a
Mirabelle: actions are responsible for catching exceptions and producing suitable log messages (makes log message uniform),
boehmes
parents:
32504
diff
changeset
|
63 |
val empty = [] |
32381 | 64 |
val copy = I |
65 |
val extend = I |
|
32515
e7c0d3c0494a
Mirabelle: actions are responsible for catching exceptions and producing suitable log messages (makes log message uniform),
boehmes
parents:
32504
diff
changeset
|
66 |
fun merge _ = Library.merge (K true) |
32381 | 67 |
) |
68 |
||
32515
e7c0d3c0494a
Mirabelle: actions are responsible for catching exceptions and producing suitable log messages (makes log message uniform),
boehmes
parents:
32504
diff
changeset
|
69 |
|
e7c0d3c0494a
Mirabelle: actions are responsible for catching exceptions and producing suitable log messages (makes log message uniform),
boehmes
parents:
32504
diff
changeset
|
70 |
fun app_with f g x = (g x; ()) |
e7c0d3c0494a
Mirabelle: actions are responsible for catching exceptions and producing suitable log messages (makes log message uniform),
boehmes
parents:
32504
diff
changeset
|
71 |
handle (exn as Exn.Interrupt) => reraise exn | exn => (f exn; ()) |
e7c0d3c0494a
Mirabelle: actions are responsible for catching exceptions and producing suitable log messages (makes log message uniform),
boehmes
parents:
32504
diff
changeset
|
72 |
|
32521 | 73 |
fun catch tag f id (st as {log, ...}) = |
74 |
let fun log_exn e = log (tag id ^ "exception:\n" ^ General.exnMessage e) |
|
75 |
in app_with log_exn (f id) st end |
|
32515
e7c0d3c0494a
Mirabelle: actions are responsible for catching exceptions and producing suitable log messages (makes log message uniform),
boehmes
parents:
32504
diff
changeset
|
76 |
|
32521 | 77 |
fun register (init, run, done) thy = |
78 |
let val id = length (Actions.get thy) + 1 |
|
79 |
in |
|
80 |
thy |
|
81 |
|> init id |
|
82 |
|> Actions.map (cons (id, run, done)) |
|
83 |
end |
|
32381 | 84 |
|
85 |
local |
|
86 |
||
87 |
fun log thy s = |
|
88 |
let fun append_to n = if n = "" then K () else File.append (Path.explode n) |
|
89 |
in append_to (Config.get_thy thy logfile) (s ^ "\n") end |
|
90 |
(* FIXME: with multithreading and parallel proofs enabled, we might need to |
|
91 |
encapsulate this inside a critical section *) |
|
92 |
||
32498
1132c7c13f36
Mirabelle: actions are responsible for handling exceptions,
boehmes
parents:
32497
diff
changeset
|
93 |
fun log_sep thy = log thy "------------------" |
1132c7c13f36
Mirabelle: actions are responsible for handling exceptions,
boehmes
parents:
32497
diff
changeset
|
94 |
|
32551
421323205efd
position information is now passed to all actions;
nipkow
parents:
32521
diff
changeset
|
95 |
fun apply_actions thy pos info (pre, post, time) actions = |
32472
7b92a8b8daaf
Mirabelle: actions are responsible for their log messages, output is better readable
boehmes
parents:
32469
diff
changeset
|
96 |
let |
32551
421323205efd
position information is now passed to all actions;
nipkow
parents:
32521
diff
changeset
|
97 |
fun apply f = f {pre=pre, post=post, timeout=time, log=log thy, pos=pos} |
32521 | 98 |
fun run (id, run, _) = (apply (run id); log_sep thy) |
32515
e7c0d3c0494a
Mirabelle: actions are responsible for catching exceptions and producing suitable log messages (makes log message uniform),
boehmes
parents:
32504
diff
changeset
|
99 |
in (log thy info; log_sep thy; List.app run actions) end |
32381 | 100 |
|
101 |
fun in_range _ _ NONE = true |
|
102 |
| in_range l r (SOME i) = (l <= i andalso (r < 0 orelse i <= r)) |
|
103 |
||
104 |
fun only_within_range thy pos f x = |
|
105 |
let val l = Config.get_thy thy start_line and r = Config.get_thy thy end_line |
|
32472
7b92a8b8daaf
Mirabelle: actions are responsible for their log messages, output is better readable
boehmes
parents:
32469
diff
changeset
|
106 |
in if in_range l r (Position.line_of pos) then f x else () end |
32381 | 107 |
|
108 |
in |
|
109 |
||
32521 | 110 |
fun run_actions tr pre post = |
32381 | 111 |
let |
112 |
val thy = Proof.theory_of pre |
|
113 |
val pos = Toplevel.pos_of tr |
|
114 |
val name = Toplevel.name_of tr |
|
32472
7b92a8b8daaf
Mirabelle: actions are responsible for their log messages, output is better readable
boehmes
parents:
32469
diff
changeset
|
115 |
val st = (pre, post, Time.fromSeconds (Config.get_thy thy timeout)) |
7b92a8b8daaf
Mirabelle: actions are responsible for their log messages, output is better readable
boehmes
parents:
32469
diff
changeset
|
116 |
|
7b92a8b8daaf
Mirabelle: actions are responsible for their log messages, output is better readable
boehmes
parents:
32469
diff
changeset
|
117 |
val str0 = string_of_int o the_default 0 |
7b92a8b8daaf
Mirabelle: actions are responsible for their log messages, output is better readable
boehmes
parents:
32469
diff
changeset
|
118 |
val loc = str0 (Position.line_of pos) ^ ":" ^ str0 (Position.column_of pos) |
7b92a8b8daaf
Mirabelle: actions are responsible for their log messages, output is better readable
boehmes
parents:
32469
diff
changeset
|
119 |
val info = "\n\nat " ^ loc ^ " (" ^ name ^ "):" |
32381 | 120 |
in |
32551
421323205efd
position information is now passed to all actions;
nipkow
parents:
32521
diff
changeset
|
121 |
only_within_range thy pos (apply_actions thy pos info st) (Actions.get thy) |
32381 | 122 |
end |
123 |
||
32521 | 124 |
fun done_actions st = |
125 |
let |
|
126 |
val thy = Toplevel.theory_of st |
|
127 |
val _ = log thy "\n\n"; |
|
128 |
in |
|
129 |
thy |
|
130 |
|> Actions.get |
|
131 |
|> List.app (fn (id, _, done) => done id {last=st, log=log thy}) |
|
132 |
end |
|
133 |
||
32381 | 134 |
end |
135 |
||
32504 | 136 |
val whitelist = ["apply", "by", "proof"] |
32468
3e6f5365971e
Mirabelle: explicit command blacklist, preliminary documentation
boehmes
parents:
32396
diff
changeset
|
137 |
|
32381 | 138 |
fun step_hook tr pre post = |
139 |
(* FIXME: might require wrapping into "interruptible" *) |
|
140 |
if can (Proof.assert_backward o Toplevel.proof_of) pre andalso |
|
32504 | 141 |
member (op =) whitelist (Toplevel.name_of tr) |
32521 | 142 |
then run_actions tr (Toplevel.proof_of pre) (SOME post) |
143 |
else if not (Toplevel.is_toplevel pre) andalso Toplevel.is_toplevel post |
|
144 |
then done_actions pre |
|
32381 | 145 |
else () (* FIXME: add theory_hook here *) |
146 |
||
147 |
||
148 |
||
149 |
(* Mirabelle utility functions *) |
|
150 |
||
151 |
val goal_thm_of = snd o snd o Proof.get_goal |
|
152 |
||
32472
7b92a8b8daaf
Mirabelle: actions are responsible for their log messages, output is better readable
boehmes
parents:
32469
diff
changeset
|
153 |
fun can_apply time tac st = |
32469 | 154 |
let |
155 |
val (ctxt, (facts, goal)) = Proof.get_goal st |
|
156 |
val full_tac = HEADGOAL (Method.insert_tac facts THEN' tac ctxt) |
|
32381 | 157 |
in |
32472
7b92a8b8daaf
Mirabelle: actions are responsible for their log messages, output is better readable
boehmes
parents:
32469
diff
changeset
|
158 |
(case TimeLimit.timeLimit time (Seq.pull o full_tac) goal of |
32381 | 159 |
SOME (thm, _) => true |
160 |
| NONE => false) |
|
161 |
end |
|
162 |
||
163 |
local |
|
164 |
||
165 |
fun fold_body_thms f = |
|
166 |
let |
|
167 |
fun app n (PBody {thms, ...}) = thms |> fold (fn (i, (name, prop, body)) => |
|
168 |
fn (x, seen) => |
|
169 |
if Inttab.defined seen i then (x, seen) |
|
170 |
else |
|
171 |
let |
|
172 |
val body' = Future.join body |
|
173 |
val (x', seen') = app (n + (if name = "" then 0 else 1)) body' |
|
174 |
(x, Inttab.update (i, ()) seen) |
|
175 |
in (x' |> n = 0 ? f (name, prop, body'), seen') end) |
|
176 |
in fn bodies => fn x => #1 (fold (app 0) bodies (x, Inttab.empty)) end |
|
177 |
||
178 |
in |
|
179 |
||
180 |
fun theorems_in_proof_term thm = |
|
181 |
let |
|
182 |
val all_thms = PureThy.all_thms_of (Thm.theory_of_thm thm) |
|
183 |
fun collect (s, _, _) = if s <> "" then insert (op =) s else I |
|
184 |
fun member_of xs (x, y) = if member (op =) xs x then SOME y else NONE |
|
185 |
fun resolve_thms names = map_filter (member_of names) all_thms |
|
186 |
in |
|
187 |
resolve_thms (fold_body_thms collect [Thm.proof_body_of thm] []) |
|
188 |
end |
|
189 |
||
190 |
end |
|
191 |
||
192 |
fun theorems_of_sucessful_proof state = |
|
193 |
(case state of |
|
194 |
NONE => [] |
|
195 |
| SOME st => |
|
196 |
if not (Toplevel.is_proof st) then [] |
|
197 |
else theorems_in_proof_term (goal_thm_of (Toplevel.proof_of st))) |
|
198 |
||
199 |
fun get_setting settings (key, default) = |
|
200 |
the_default default (AList.lookup (op =) settings key) |
|
201 |
||
202 |
fun get_int_setting settings (key, default) = |
|
203 |
(case Option.map Int.fromString (AList.lookup (op =) settings key) of |
|
204 |
SOME (SOME i) => i |
|
205 |
| SOME NONE => error ("bad option: " ^ key) |
|
206 |
| NONE => default) |
|
207 |
||
32498
1132c7c13f36
Mirabelle: actions are responsible for handling exceptions,
boehmes
parents:
32497
diff
changeset
|
208 |
fun cpu_time f x = |
1132c7c13f36
Mirabelle: actions are responsible for handling exceptions,
boehmes
parents:
32497
diff
changeset
|
209 |
let |
1132c7c13f36
Mirabelle: actions are responsible for handling exceptions,
boehmes
parents:
32497
diff
changeset
|
210 |
val start = start_timing () |
1132c7c13f36
Mirabelle: actions are responsible for handling exceptions,
boehmes
parents:
32497
diff
changeset
|
211 |
val result = Exn.capture (fn () => f x) () |
1132c7c13f36
Mirabelle: actions are responsible for handling exceptions,
boehmes
parents:
32497
diff
changeset
|
212 |
val time = Time.toMilliseconds (#cpu (end_timing start)) |
1132c7c13f36
Mirabelle: actions are responsible for handling exceptions,
boehmes
parents:
32497
diff
changeset
|
213 |
in (Exn.release result, time) end |
1132c7c13f36
Mirabelle: actions are responsible for handling exceptions,
boehmes
parents:
32497
diff
changeset
|
214 |
|
32381 | 215 |
end |