author | huffman |
Tue, 02 Mar 2010 09:54:50 -0800 | |
changeset 35512 | d1ef88d7de5a |
parent 35408 | b48ab741683b |
child 35845 | e5980f0ad025 |
permissions | -rw-r--r-- |
14620
1be590fd2422
Minor cleanup of headers and some speedup of the HOL4 import.
skalberg
parents:
14518
diff
changeset
|
1 |
(* Title: HOL/Import/shuffler.ML |
14516 | 2 |
Author: Sebastian Skalberg, TU Muenchen |
3 |
||
4 |
Package for proving two terms equal by normalizing (hence the |
|
5 |
"shuffler" name). Uses the simplifier for the normalization. |
|
6 |
*) |
|
7 |
||
8 |
signature Shuffler = |
|
9 |
sig |
|
32740 | 10 |
val debug : bool Unsynchronized.ref |
14516 | 11 |
|
12 |
val norm_term : theory -> term -> thm |
|
13 |
val make_equal : theory -> term -> term -> thm option |
|
14 |
val set_prop : theory -> term -> (string * thm) list -> (string * thm) option |
|
15 |
||
16 |
val find_potential: theory -> term -> (string * thm) list |
|
17 |
||
31241 | 18 |
val gen_shuffle_tac: Proof.context -> bool -> (string * thm) list -> int -> tactic |
19 |
val shuffle_tac: Proof.context -> thm list -> int -> tactic |
|
20 |
val search_tac : Proof.context -> int -> tactic |
|
14516 | 21 |
|
22 |
val print_shuffles: theory -> unit |
|
23 |
||
24 |
val add_shuffle_rule: thm -> theory -> theory |
|
18728 | 25 |
val shuffle_attr: attribute |
14516 | 26 |
|
18708 | 27 |
val setup : theory -> theory |
14516 | 28 |
end |
29 |
||
30 |
structure Shuffler :> Shuffler = |
|
31 |
struct |
|
32 |
||
32740 | 33 |
val debug = Unsynchronized.ref false |
14516 | 34 |
|
35 |
fun if_debug f x = if !debug then f x else () |
|
36 |
val message = if_debug writeln |
|
37 |
||
38 |
(*Prints exceptions readably to users*) |
|
21588 | 39 |
fun print_sign_exn_unit sign e = |
14516 | 40 |
case e of |
41 |
THM (msg,i,thms) => |
|
21588 | 42 |
(writeln ("Exception THM " ^ string_of_int i ^ " raised:\n" ^ msg); |
32091
30e2ffbba718
proper context for Display.pretty_thm etc. or old-style versions Display.pretty_thm_global, Display.pretty_thm_without_context etc.;
wenzelm
parents:
31945
diff
changeset
|
43 |
List.app (writeln o Display.string_of_thm_global sign) thms) |
14516 | 44 |
| THEORY (msg,thys) => |
21588 | 45 |
(writeln ("Exception THEORY raised:\n" ^ msg); |
46 |
List.app (writeln o Context.str_of_thy) thys) |
|
14516 | 47 |
| TERM (msg,ts) => |
21588 | 48 |
(writeln ("Exception TERM raised:\n" ^ msg); |
26939
1035c89b4c02
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26928
diff
changeset
|
49 |
List.app (writeln o Syntax.string_of_term_global sign) ts) |
14516 | 50 |
| TYPE (msg,Ts,ts) => |
21588 | 51 |
(writeln ("Exception TYPE raised:\n" ^ msg); |
26939
1035c89b4c02
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26928
diff
changeset
|
52 |
List.app (writeln o Syntax.string_of_typ_global sign) Ts; |
1035c89b4c02
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26928
diff
changeset
|
53 |
List.app (writeln o Syntax.string_of_term_global sign) ts) |
14516 | 54 |
| e => raise e |
55 |
||
56 |
(*Prints an exception, then fails*) |
|
57 |
fun print_sign_exn sign e = (print_sign_exn_unit sign e; raise e) |
|
58 |
||
32091
30e2ffbba718
proper context for Display.pretty_thm etc. or old-style versions Display.pretty_thm_global, Display.pretty_thm_without_context etc.;
wenzelm
parents:
31945
diff
changeset
|
59 |
val string_of_thm = PrintMode.setmp [] Display.string_of_thm_without_context; |
14516 | 60 |
|
61 |
fun mk_meta_eq th = |
|
62 |
(case concl_of th of |
|
21588 | 63 |
Const("Trueprop",_) $ (Const("op =",_) $ _ $ _) => th RS eq_reflection |
14516 | 64 |
| Const("==",_) $ _ $ _ => th |
65 |
| _ => raise THM("Not an equality",0,[th])) |
|
28397
389c5e494605
handle _ should be avoided (spurious Interrupt will spoil the game);
wenzelm
parents:
27865
diff
changeset
|
66 |
handle _ => raise THM("Couldn't make meta equality",0,[th]) (* FIXME avoid handle _ *) |
21588 | 67 |
|
14516 | 68 |
fun mk_obj_eq th = |
69 |
(case concl_of th of |
|
21588 | 70 |
Const("Trueprop",_) $ (Const("op =",_) $ _ $ _) => th |
14516 | 71 |
| Const("==",_) $ _ $ _ => th RS meta_eq_to_obj_eq |
72 |
| _ => raise THM("Not an equality",0,[th])) |
|
28397
389c5e494605
handle _ should be avoided (spurious Interrupt will spoil the game);
wenzelm
parents:
27865
diff
changeset
|
73 |
handle _ => raise THM("Couldn't make object equality",0,[th]) (* FIXME avoid handle _ *) |
14516 | 74 |
|
33522 | 75 |
structure ShuffleData = Theory_Data |
22846 | 76 |
( |
77 |
type T = thm list |
|
78 |
val empty = [] |
|
79 |
val extend = I |
|
33522 | 80 |
val merge = Thm.merge_thms |
22846 | 81 |
) |
14516 | 82 |
|
22846 | 83 |
fun print_shuffles thy = |
84 |
Pretty.writeln (Pretty.big_list "Shuffle theorems:" |
|
32091
30e2ffbba718
proper context for Display.pretty_thm etc. or old-style versions Display.pretty_thm_global, Display.pretty_thm_without_context etc.;
wenzelm
parents:
31945
diff
changeset
|
85 |
(map (Display.pretty_thm_global thy) (ShuffleData.get thy))) |
14516 | 86 |
|
87 |
val weaken = |
|
88 |
let |
|
26424 | 89 |
val cert = cterm_of Pure.thy |
21588 | 90 |
val P = Free("P",propT) |
91 |
val Q = Free("Q",propT) |
|
92 |
val PQ = Logic.mk_implies(P,Q) |
|
93 |
val PPQ = Logic.mk_implies(P,PQ) |
|
94 |
val cP = cert P |
|
95 |
val cQ = cert Q |
|
96 |
val cPQ = cert PQ |
|
97 |
val cPPQ = cert PPQ |
|
98 |
val th1 = assume cPQ |> implies_intr_list [cPQ,cP] |
|
99 |
val th3 = assume cP |
|
100 |
val th4 = implies_elim_list (assume cPPQ) [th3,th3] |
|
101 |
|> implies_intr_list [cPPQ,cP] |
|
14516 | 102 |
in |
35021
c839a4c670c6
renamed old-style Drule.standard to Drule.export_without_context, to emphasize that this is in no way a standard operation;
wenzelm
parents:
33832
diff
changeset
|
103 |
equal_intr th4 th1 |> Drule.export_without_context |
14516 | 104 |
end |
105 |
||
106 |
val imp_comm = |
|
107 |
let |
|
26424 | 108 |
val cert = cterm_of Pure.thy |
21588 | 109 |
val P = Free("P",propT) |
110 |
val Q = Free("Q",propT) |
|
111 |
val R = Free("R",propT) |
|
112 |
val PQR = Logic.mk_implies(P,Logic.mk_implies(Q,R)) |
|
113 |
val QPR = Logic.mk_implies(Q,Logic.mk_implies(P,R)) |
|
114 |
val cP = cert P |
|
115 |
val cQ = cert Q |
|
116 |
val cPQR = cert PQR |
|
117 |
val cQPR = cert QPR |
|
118 |
val th1 = implies_elim_list (assume cPQR) [assume cP,assume cQ] |
|
119 |
|> implies_intr_list [cPQR,cQ,cP] |
|
120 |
val th2 = implies_elim_list (assume cQPR) [assume cQ,assume cP] |
|
121 |
|> implies_intr_list [cQPR,cP,cQ] |
|
14516 | 122 |
in |
35021
c839a4c670c6
renamed old-style Drule.standard to Drule.export_without_context, to emphasize that this is in no way a standard operation;
wenzelm
parents:
33832
diff
changeset
|
123 |
equal_intr th1 th2 |> Drule.export_without_context |
14516 | 124 |
end |
125 |
||
126 |
val def_norm = |
|
127 |
let |
|
26424 | 128 |
val cert = cterm_of Pure.thy |
21588 | 129 |
val aT = TFree("'a",[]) |
130 |
val bT = TFree("'b",[]) |
|
131 |
val v = Free("v",aT) |
|
132 |
val P = Free("P",aT-->bT) |
|
133 |
val Q = Free("Q",aT-->bT) |
|
134 |
val cvPQ = cert (list_all ([("v",aT)],Logic.mk_equals(P $ Bound 0,Q $ Bound 0))) |
|
135 |
val cPQ = cert (Logic.mk_equals(P,Q)) |
|
136 |
val cv = cert v |
|
137 |
val rew = assume cvPQ |
|
138 |
|> forall_elim cv |
|
139 |
|> abstract_rule "v" cv |
|
140 |
val (lhs,rhs) = Logic.dest_equals(concl_of rew) |
|
141 |
val th1 = transitive (transitive |
|
142 |
(eta_conversion (cert lhs) |> symmetric) |
|
143 |
rew) |
|
144 |
(eta_conversion (cert rhs)) |
|
145 |
|> implies_intr cvPQ |
|
146 |
val th2 = combination (assume cPQ) (reflexive cv) |
|
147 |
|> forall_intr cv |
|
148 |
|> implies_intr cPQ |
|
14516 | 149 |
in |
35021
c839a4c670c6
renamed old-style Drule.standard to Drule.export_without_context, to emphasize that this is in no way a standard operation;
wenzelm
parents:
33832
diff
changeset
|
150 |
equal_intr th1 th2 |> Drule.export_without_context |
14516 | 151 |
end |
152 |
||
153 |
val all_comm = |
|
154 |
let |
|
26424 | 155 |
val cert = cterm_of Pure.thy |
21588 | 156 |
val xT = TFree("'a",[]) |
157 |
val yT = TFree("'b",[]) |
|
27330 | 158 |
val x = Free("x",xT) |
159 |
val y = Free("y",yT) |
|
21588 | 160 |
val P = Free("P",xT-->yT-->propT) |
27330 | 161 |
val lhs = Logic.all x (Logic.all y (P $ x $ y)) |
162 |
val rhs = Logic.all y (Logic.all x (P $ x $ y)) |
|
21588 | 163 |
val cl = cert lhs |
164 |
val cr = cert rhs |
|
27330 | 165 |
val cx = cert x |
166 |
val cy = cert y |
|
21588 | 167 |
val th1 = assume cr |
168 |
|> forall_elim_list [cy,cx] |
|
169 |
|> forall_intr_list [cx,cy] |
|
170 |
|> implies_intr cr |
|
171 |
val th2 = assume cl |
|
172 |
|> forall_elim_list [cx,cy] |
|
173 |
|> forall_intr_list [cy,cx] |
|
174 |
|> implies_intr cl |
|
14516 | 175 |
in |
35021
c839a4c670c6
renamed old-style Drule.standard to Drule.export_without_context, to emphasize that this is in no way a standard operation;
wenzelm
parents:
33832
diff
changeset
|
176 |
equal_intr th1 th2 |> Drule.export_without_context |
14516 | 177 |
end |
178 |
||
179 |
val equiv_comm = |
|
180 |
let |
|
26424 | 181 |
val cert = cterm_of Pure.thy |
21588 | 182 |
val T = TFree("'a",[]) |
183 |
val t = Free("t",T) |
|
184 |
val u = Free("u",T) |
|
185 |
val ctu = cert (Logic.mk_equals(t,u)) |
|
186 |
val cut = cert (Logic.mk_equals(u,t)) |
|
187 |
val th1 = assume ctu |> symmetric |> implies_intr ctu |
|
188 |
val th2 = assume cut |> symmetric |> implies_intr cut |
|
14516 | 189 |
in |
35021
c839a4c670c6
renamed old-style Drule.standard to Drule.export_without_context, to emphasize that this is in no way a standard operation;
wenzelm
parents:
33832
diff
changeset
|
190 |
equal_intr th1 th2 |> Drule.export_without_context |
14516 | 191 |
end |
192 |
||
193 |
(* This simplification procedure rewrites !!x y. P x y |
|
194 |
deterministicly, in order for the normalization function, defined |
|
195 |
below, to handle nested quantifiers robustly *) |
|
196 |
||
197 |
local |
|
198 |
||
199 |
exception RESULT of int |
|
200 |
||
201 |
fun find_bound n (Bound i) = if i = n then raise RESULT 0 |
|
21588 | 202 |
else if i = n+1 then raise RESULT 1 |
203 |
else () |
|
14516 | 204 |
| find_bound n (t $ u) = (find_bound n t; find_bound n u) |
205 |
| find_bound n (Abs(_,_,t)) = find_bound (n+1) t |
|
206 |
| find_bound _ _ = () |
|
207 |
||
208 |
fun swap_bound n (Bound i) = if i = n then Bound (n+1) |
|
21588 | 209 |
else if i = n+1 then Bound n |
210 |
else Bound i |
|
14516 | 211 |
| swap_bound n (t $ u) = (swap_bound n t $ swap_bound n u) |
212 |
| swap_bound n (Abs(x,xT,t)) = Abs(x,xT,swap_bound (n+1) t) |
|
213 |
| swap_bound n t = t |
|
214 |
||
21078 | 215 |
fun rew_th thy (xv as (x,xT)) (yv as (y,yT)) t = |
14516 | 216 |
let |
21588 | 217 |
val lhs = list_all ([xv,yv],t) |
218 |
val rhs = list_all ([yv,xv],swap_bound 0 t) |
|
219 |
val rew = Logic.mk_equals (lhs,rhs) |
|
220 |
val init = trivial (cterm_of thy rew) |
|
14516 | 221 |
in |
21588 | 222 |
(all_comm RS init handle e => (message "rew_th"; OldGoals.print_exn e)) |
14516 | 223 |
end |
224 |
||
21078 | 225 |
fun quant_rewrite thy assumes (t as Const("all",T1) $ (Abs(x,xT,Const("all",T2) $ Abs(y,yT,body)))) = |
14516 | 226 |
let |
21588 | 227 |
val res = (find_bound 0 body;2) handle RESULT i => i |
14516 | 228 |
in |
21588 | 229 |
case res of |
230 |
0 => SOME (rew_th thy (x,xT) (y,yT) body) |
|
231 |
| 1 => if string_ord(y,x) = LESS |
|
232 |
then |
|
233 |
let |
|
234 |
val newt = Const("all",T1) $ (Abs(y,xT,Const("all",T2) $ Abs(x,yT,body))) |
|
235 |
val t_th = reflexive (cterm_of thy t) |
|
236 |
val newt_th = reflexive (cterm_of thy newt) |
|
237 |
in |
|
238 |
SOME (transitive t_th newt_th) |
|
239 |
end |
|
240 |
else NONE |
|
241 |
| _ => error "norm_term (quant_rewrite) internal error" |
|
14516 | 242 |
end |
15531 | 243 |
| quant_rewrite _ _ _ = (warning "quant_rewrite: Unknown lhs"; NONE) |
14516 | 244 |
|
245 |
fun freeze_thaw_term t = |
|
246 |
let |
|
29270
0eade173f77e
moved old add_type_XXX, add_term_XXX etc. to structure OldTerm;
wenzelm
parents:
29269
diff
changeset
|
247 |
val tvars = OldTerm.term_tvars t |
0eade173f77e
moved old add_type_XXX, add_term_XXX etc. to structure OldTerm;
wenzelm
parents:
29269
diff
changeset
|
248 |
val tfree_names = OldTerm.add_term_tfree_names(t,[]) |
21588 | 249 |
val (type_inst,_) = |
33245 | 250 |
fold (fn (w as (v,_), S) => fn (inst, used) => |
21588 | 251 |
let |
252 |
val v' = Name.variant used v |
|
253 |
in |
|
254 |
((w,TFree(v',S))::inst,v'::used) |
|
255 |
end) |
|
33245 | 256 |
tvars ([], tfree_names) |
21588 | 257 |
val t' = subst_TVars type_inst t |
14516 | 258 |
in |
33245 | 259 |
(t', map (fn (w,TFree(v,S)) => (v,TVar(w,S)) |
21588 | 260 |
| _ => error "Internal error in Shuffler.freeze_thaw") type_inst) |
14516 | 261 |
end |
262 |
||
21078 | 263 |
fun inst_tfrees thy [] thm = thm |
21588 | 264 |
| inst_tfrees thy ((name,U)::rest) thm = |
14516 | 265 |
let |
21588 | 266 |
val cU = ctyp_of thy U |
29270
0eade173f77e
moved old add_type_XXX, add_term_XXX etc. to structure OldTerm;
wenzelm
parents:
29269
diff
changeset
|
267 |
val tfrees = OldTerm.add_term_tfrees (prop_of thm,[]) |
21588 | 268 |
val (rens, thm') = Thm.varifyT' |
20951
868120282837
gen_rem(s) abandoned in favour of remove / subtract
haftmann
parents:
20897
diff
changeset
|
269 |
(remove (op = o apsnd fst) name tfrees) thm |
21588 | 270 |
val mid = |
271 |
case rens of |
|
272 |
[] => thm' |
|
273 |
| [((_, S), idx)] => instantiate |
|
21078 | 274 |
([(ctyp_of thy (TVar (idx, S)), cU)], []) thm' |
21588 | 275 |
| _ => error "Shuffler.inst_tfrees internal error" |
14516 | 276 |
in |
21588 | 277 |
inst_tfrees thy rest mid |
14516 | 278 |
end |
279 |
||
280 |
fun is_Abs (Abs _) = true |
|
281 |
| is_Abs _ = false |
|
282 |
||
283 |
fun eta_redex (t $ Bound 0) = |
|
284 |
let |
|
21588 | 285 |
fun free n (Bound i) = i = n |
286 |
| free n (t $ u) = free n t orelse free n u |
|
287 |
| free n (Abs(_,_,t)) = free (n+1) t |
|
288 |
| free n _ = false |
|
14516 | 289 |
in |
21588 | 290 |
not (free 0 t) |
14516 | 291 |
end |
292 |
| eta_redex _ = false |
|
293 |
||
21078 | 294 |
fun eta_contract thy assumes origt = |
14516 | 295 |
let |
21588 | 296 |
val (typet,Tinst) = freeze_thaw_term origt |
33832 | 297 |
val (init,thaw) = Drule.legacy_freeze_thaw (reflexive (cterm_of thy typet)) |
21588 | 298 |
val final = inst_tfrees thy Tinst o thaw |
299 |
val t = #1 (Logic.dest_equals (prop_of init)) |
|
300 |
val _ = |
|
301 |
let |
|
302 |
val lhs = #1 (Logic.dest_equals (prop_of (final init))) |
|
303 |
in |
|
304 |
if not (lhs aconv origt) |
|
32432
64f30bdd3ba1
modernized messages -- eliminated ctyp/cterm operations;
wenzelm
parents:
32091
diff
changeset
|
305 |
then |
64f30bdd3ba1
modernized messages -- eliminated ctyp/cterm operations;
wenzelm
parents:
32091
diff
changeset
|
306 |
writeln (cat_lines |
64f30bdd3ba1
modernized messages -- eliminated ctyp/cterm operations;
wenzelm
parents:
32091
diff
changeset
|
307 |
(["Something is utterly wrong: (orig, lhs, frozen type, t, tinst)", |
64f30bdd3ba1
modernized messages -- eliminated ctyp/cterm operations;
wenzelm
parents:
32091
diff
changeset
|
308 |
Syntax.string_of_term_global thy origt, |
64f30bdd3ba1
modernized messages -- eliminated ctyp/cterm operations;
wenzelm
parents:
32091
diff
changeset
|
309 |
Syntax.string_of_term_global thy lhs, |
64f30bdd3ba1
modernized messages -- eliminated ctyp/cterm operations;
wenzelm
parents:
32091
diff
changeset
|
310 |
Syntax.string_of_term_global thy typet, |
64f30bdd3ba1
modernized messages -- eliminated ctyp/cterm operations;
wenzelm
parents:
32091
diff
changeset
|
311 |
Syntax.string_of_term_global thy t] @ |
64f30bdd3ba1
modernized messages -- eliminated ctyp/cterm operations;
wenzelm
parents:
32091
diff
changeset
|
312 |
map (fn (n, T) => n ^ ": " ^ Syntax.string_of_typ_global thy T) Tinst)) |
21588 | 313 |
else () |
314 |
end |
|
14516 | 315 |
in |
21588 | 316 |
case t of |
317 |
Const("all",_) $ (Abs(x,xT,Const("==",eqT) $ P $ Q)) => |
|
318 |
((if eta_redex P andalso eta_redex Q |
|
319 |
then |
|
320 |
let |
|
321 |
val cert = cterm_of thy |
|
29281 | 322 |
val v = Free (Name.variant (Term.add_free_names t []) "v", xT) |
21588 | 323 |
val cv = cert v |
324 |
val ct = cert t |
|
325 |
val th = (assume ct) |
|
326 |
|> forall_elim cv |
|
327 |
|> abstract_rule x cv |
|
328 |
val ext_th = eta_conversion (cert (Abs(x,xT,P))) |
|
329 |
val th' = transitive (symmetric ext_th) th |
|
330 |
val cu = cert (prop_of th') |
|
331 |
val uth = combination (assume cu) (reflexive cv) |
|
332 |
val uth' = (beta_conversion false (cert (Abs(x,xT,Q) $ v))) |
|
333 |
|> transitive uth |
|
334 |
|> forall_intr cv |
|
335 |
|> implies_intr cu |
|
336 |
val rew_th = equal_intr (th' |> implies_intr ct) uth' |
|
337 |
val res = final rew_th |
|
338 |
val lhs = (#1 (Logic.dest_equals (prop_of res))) |
|
339 |
in |
|
340 |
SOME res |
|
341 |
end |
|
342 |
else NONE) |
|
343 |
handle e => OldGoals.print_exn e) |
|
344 |
| _ => NONE |
|
17440
df77edc4f5d0
fixed HOL-light/Isabelle syntax incompatability via more protect_xxx functions
obua
parents:
17188
diff
changeset
|
345 |
end |
14516 | 346 |
|
21078 | 347 |
fun beta_fun thy assume t = |
348 |
SOME (beta_conversion true (cterm_of thy t)) |
|
14516 | 349 |
|
17188 | 350 |
val meta_sym_rew = thm "refl" |
351 |
||
21078 | 352 |
fun equals_fun thy assume t = |
17188 | 353 |
case t of |
35408 | 354 |
Const("op ==",_) $ u $ v => if Term_Ord.term_ord (u,v) = LESS then SOME (meta_sym_rew) else NONE |
17188 | 355 |
| _ => NONE |
356 |
||
21078 | 357 |
fun eta_expand thy assumes origt = |
14516 | 358 |
let |
21588 | 359 |
val (typet,Tinst) = freeze_thaw_term origt |
33832 | 360 |
val (init,thaw) = Drule.legacy_freeze_thaw (reflexive (cterm_of thy typet)) |
21588 | 361 |
val final = inst_tfrees thy Tinst o thaw |
362 |
val t = #1 (Logic.dest_equals (prop_of init)) |
|
363 |
val _ = |
|
364 |
let |
|
365 |
val lhs = #1 (Logic.dest_equals (prop_of (final init))) |
|
366 |
in |
|
367 |
if not (lhs aconv origt) |
|
32432
64f30bdd3ba1
modernized messages -- eliminated ctyp/cterm operations;
wenzelm
parents:
32091
diff
changeset
|
368 |
then |
64f30bdd3ba1
modernized messages -- eliminated ctyp/cterm operations;
wenzelm
parents:
32091
diff
changeset
|
369 |
writeln (cat_lines |
64f30bdd3ba1
modernized messages -- eliminated ctyp/cterm operations;
wenzelm
parents:
32091
diff
changeset
|
370 |
(["Something is utterly wrong: (orig, lhs, frozen type, t, tinst)", |
64f30bdd3ba1
modernized messages -- eliminated ctyp/cterm operations;
wenzelm
parents:
32091
diff
changeset
|
371 |
Syntax.string_of_term_global thy origt, |
64f30bdd3ba1
modernized messages -- eliminated ctyp/cterm operations;
wenzelm
parents:
32091
diff
changeset
|
372 |
Syntax.string_of_term_global thy lhs, |
64f30bdd3ba1
modernized messages -- eliminated ctyp/cterm operations;
wenzelm
parents:
32091
diff
changeset
|
373 |
Syntax.string_of_term_global thy typet, |
64f30bdd3ba1
modernized messages -- eliminated ctyp/cterm operations;
wenzelm
parents:
32091
diff
changeset
|
374 |
Syntax.string_of_term_global thy t] @ |
64f30bdd3ba1
modernized messages -- eliminated ctyp/cterm operations;
wenzelm
parents:
32091
diff
changeset
|
375 |
map (fn (n, T) => n ^ ": " ^ Syntax.string_of_typ_global thy T) Tinst)) |
21588 | 376 |
else () |
377 |
end |
|
14516 | 378 |
in |
21588 | 379 |
case t of |
380 |
Const("==",T) $ P $ Q => |
|
381 |
if is_Abs P orelse is_Abs Q |
|
382 |
then (case domain_type T of |
|
383 |
Type("fun",[aT,bT]) => |
|
384 |
let |
|
385 |
val cert = cterm_of thy |
|
29281 | 386 |
val vname = Name.variant (Term.add_free_names t []) "v" |
21588 | 387 |
val v = Free(vname,aT) |
388 |
val cv = cert v |
|
389 |
val ct = cert t |
|
390 |
val th1 = (combination (assume ct) (reflexive cv)) |
|
391 |
|> forall_intr cv |
|
392 |
|> implies_intr ct |
|
393 |
val concl = cert (concl_of th1) |
|
394 |
val th2 = (assume concl) |
|
395 |
|> forall_elim cv |
|
396 |
|> abstract_rule vname cv |
|
397 |
val (lhs,rhs) = Logic.dest_equals (prop_of th2) |
|
398 |
val elhs = eta_conversion (cert lhs) |
|
399 |
val erhs = eta_conversion (cert rhs) |
|
400 |
val th2' = transitive |
|
401 |
(transitive (symmetric elhs) th2) |
|
402 |
erhs |
|
403 |
val res = equal_intr th1 (th2' |> implies_intr concl) |
|
404 |
val res' = final res |
|
405 |
in |
|
406 |
SOME res' |
|
407 |
end |
|
408 |
| _ => NONE) |
|
409 |
else NONE |
|
32432
64f30bdd3ba1
modernized messages -- eliminated ctyp/cterm operations;
wenzelm
parents:
32091
diff
changeset
|
410 |
| _ => error ("Bad eta_expand argument" ^ Syntax.string_of_term_global thy t) |
64f30bdd3ba1
modernized messages -- eliminated ctyp/cterm operations;
wenzelm
parents:
32091
diff
changeset
|
411 |
end; |
14516 | 412 |
|
14854 | 413 |
fun mk_tfree s = TFree("'"^s,[]) |
20326 | 414 |
fun mk_free s t = Free (s,t) |
14516 | 415 |
val xT = mk_tfree "a" |
416 |
val yT = mk_tfree "b" |
|
27330 | 417 |
val x = Free ("x", xT) |
418 |
val y = Free ("y", yT) |
|
20326 | 419 |
val P = mk_free "P" (xT-->yT-->propT) |
420 |
val Q = mk_free "Q" (xT-->yT) |
|
421 |
val R = mk_free "R" (xT-->yT) |
|
422 |
val S = mk_free "S" xT |
|
423 |
val S' = mk_free "S'" xT |
|
14516 | 424 |
in |
21078 | 425 |
fun beta_simproc thy = Simplifier.simproc_i |
21588 | 426 |
thy |
427 |
"Beta-contraction" |
|
428 |
[Abs("x",xT,Q) $ S] |
|
429 |
beta_fun |
|
14516 | 430 |
|
21078 | 431 |
fun equals_simproc thy = Simplifier.simproc_i |
21588 | 432 |
thy |
433 |
"Ordered rewriting of meta equalities" |
|
434 |
[Const("op ==",xT) $ S $ S'] |
|
435 |
equals_fun |
|
17188 | 436 |
|
21078 | 437 |
fun quant_simproc thy = Simplifier.simproc_i |
21588 | 438 |
thy |
439 |
"Ordered rewriting of nested quantifiers" |
|
27330 | 440 |
[Logic.all x (Logic.all y (P $ x $ y))] |
21588 | 441 |
quant_rewrite |
21078 | 442 |
fun eta_expand_simproc thy = Simplifier.simproc_i |
21588 | 443 |
thy |
444 |
"Smart eta-expansion by equivalences" |
|
445 |
[Logic.mk_equals(Q,R)] |
|
446 |
eta_expand |
|
21078 | 447 |
fun eta_contract_simproc thy = Simplifier.simproc_i |
21588 | 448 |
thy |
449 |
"Smart handling of eta-contractions" |
|
27330 | 450 |
[Logic.all x (Logic.mk_equals (Q $ x, R $ x))] |
21588 | 451 |
eta_contract |
14516 | 452 |
end |
453 |
||
454 |
(* Disambiguates the names of bound variables in a term, returning t |
|
455 |
== t' where all the names of bound variables in t' are unique *) |
|
456 |
||
21078 | 457 |
fun disamb_bound thy t = |
14516 | 458 |
let |
21588 | 459 |
|
460 |
fun F (t $ u,idx) = |
|
461 |
let |
|
462 |
val (t',idx') = F (t,idx) |
|
463 |
val (u',idx'') = F (u,idx') |
|
464 |
in |
|
465 |
(t' $ u',idx'') |
|
466 |
end |
|
467 |
| F (Abs(x,xT,t),idx) = |
|
468 |
let |
|
469 |
val x' = "x" ^ (LargeInt.toString idx) (* amazing *) |
|
470 |
val (t',idx') = F (t,idx+1) |
|
471 |
in |
|
472 |
(Abs(x',xT,t'),idx') |
|
473 |
end |
|
474 |
| F arg = arg |
|
475 |
val (t',_) = F (t,0) |
|
476 |
val ct = cterm_of thy t |
|
477 |
val ct' = cterm_of thy t' |
|
478 |
val res = transitive (reflexive ct) (reflexive ct') |
|
479 |
val _ = message ("disamb_term: " ^ (string_of_thm res)) |
|
14516 | 480 |
in |
21588 | 481 |
res |
14516 | 482 |
end |
483 |
||
484 |
(* Transforms a term t to some normal form t', returning the theorem t |
|
485 |
== t'. This is originally a help function for make_equal, but might |
|
486 |
be handy in its own right, for example for indexing terms. *) |
|
487 |
||
488 |
fun norm_term thy t = |
|
489 |
let |
|
21588 | 490 |
val norms = ShuffleData.get thy |
35232
f588e1169c8b
renamed Simplifier.theory_context to Simplifier.global_context to emphasize that this is not the real thing;
wenzelm
parents:
35021
diff
changeset
|
491 |
val ss = Simplifier.global_context thy empty_ss |
17892 | 492 |
setmksimps single |
21588 | 493 |
addsimps (map (Thm.transfer thy) norms) |
21078 | 494 |
addsimprocs [quant_simproc thy, eta_expand_simproc thy,eta_contract_simproc thy] |
21588 | 495 |
fun chain f th = |
496 |
let |
|
22902
ac833b4bb7ee
moved some Drule operations to Thm (see more_thm.ML);
wenzelm
parents:
22846
diff
changeset
|
497 |
val rhs = Thm.rhs_of th |
21588 | 498 |
in |
499 |
transitive th (f rhs) |
|
500 |
end |
|
501 |
val th = |
|
21078 | 502 |
t |> disamb_bound thy |
21588 | 503 |
|> chain (Simplifier.full_rewrite ss) |
20326 | 504 |
|> chain eta_conversion |
21588 | 505 |
|> strip_shyps |
506 |
val _ = message ("norm_term: " ^ (string_of_thm th)) |
|
14516 | 507 |
in |
21588 | 508 |
th |
17463 | 509 |
end |
21078 | 510 |
handle e => (writeln "norm_term internal error"; print_sign_exn thy e) |
14516 | 511 |
|
512 |
||
513 |
(* Closes a theorem with respect to free and schematic variables (does |
|
514 |
not touch type variables, though). *) |
|
515 |
||
516 |
fun close_thm th = |
|
517 |
let |
|
22578 | 518 |
val thy = Thm.theory_of_thm th |
21588 | 519 |
val c = prop_of th |
29265
5b4247055bd7
moved old add_term_vars, add_term_frees etc. to structure OldTerm;
wenzelm
parents:
28397
diff
changeset
|
520 |
val vars = OldTerm.add_term_frees (c, OldTerm.add_term_vars(c,[])) |
14516 | 521 |
in |
21588 | 522 |
Drule.forall_intr_list (map (cterm_of thy) vars) th |
14516 | 523 |
end |
17959 | 524 |
handle e => (writeln "close_thm internal error"; OldGoals.print_exn e) |
14516 | 525 |
|
526 |
(* Normalizes a theorem's conclusion using norm_term. *) |
|
527 |
||
528 |
fun norm_thm thy th = |
|
529 |
let |
|
21588 | 530 |
val c = prop_of th |
14516 | 531 |
in |
21588 | 532 |
equal_elim (norm_term thy c) th |
14516 | 533 |
end |
534 |
||
21078 | 535 |
(* make_equal thy t u tries to construct the theorem t == u under the |
536 |
signature thy. If it succeeds, SOME (t == u) is returned, otherwise |
|
15531 | 537 |
NONE is returned. *) |
14516 | 538 |
|
21078 | 539 |
fun make_equal thy t u = |
14516 | 540 |
let |
21588 | 541 |
val t_is_t' = norm_term thy t |
542 |
val u_is_u' = norm_term thy u |
|
543 |
val th = transitive t_is_t' (symmetric u_is_u') |
|
544 |
val _ = message ("make_equal: SOME " ^ (string_of_thm th)) |
|
14516 | 545 |
in |
21588 | 546 |
SOME th |
14516 | 547 |
end |
15531 | 548 |
handle e as THM _ => (message "make_equal: NONE";NONE) |
21588 | 549 |
|
14516 | 550 |
fun match_consts ignore t (* th *) = |
551 |
let |
|
21588 | 552 |
fun add_consts (Const (c, _), cs) = |
553 |
if c mem_string ignore |
|
554 |
then cs |
|
555 |
else insert (op =) c cs |
|
556 |
| add_consts (t $ u, cs) = add_consts (t, add_consts (u, cs)) |
|
557 |
| add_consts (Abs (_, _, t), cs) = add_consts (t, cs) |
|
558 |
| add_consts (_, cs) = cs |
|
559 |
val t_consts = add_consts(t,[]) |
|
14516 | 560 |
in |
561 |
fn (name,th) => |
|
21588 | 562 |
let |
563 |
val th_consts = add_consts(prop_of th,[]) |
|
564 |
in |
|
33038 | 565 |
eq_set (op =) (t_consts, th_consts) |
21588 | 566 |
end |
14516 | 567 |
end |
21588 | 568 |
|
33040 | 569 |
val collect_ignored = fold_rev (fn thm => fn cs => |
570 |
let |
|
571 |
val (lhs, rhs) = Logic.dest_equals (prop_of thm); |
|
572 |
val consts_lhs = Term.add_const_names lhs []; |
|
573 |
val consts_rhs = Term.add_const_names rhs []; |
|
574 |
val ignore_lhs = subtract (op =) consts_rhs consts_lhs; |
|
575 |
val ignore_rhs = subtract (op =) consts_lhs consts_rhs; |
|
576 |
in |
|
577 |
fold_rev (insert (op =)) cs (ignore_lhs @ ignore_rhs) |
|
578 |
end) |
|
14516 | 579 |
|
580 |
(* set_prop t thms tries to make a theorem with the proposition t from |
|
581 |
one of the theorems thms, by shuffling the propositions around. If it |
|
15531 | 582 |
succeeds, SOME theorem is returned, otherwise NONE. *) |
14516 | 583 |
|
584 |
fun set_prop thy t = |
|
585 |
let |
|
29265
5b4247055bd7
moved old add_term_vars, add_term_frees etc. to structure OldTerm;
wenzelm
parents:
28397
diff
changeset
|
586 |
val vars = OldTerm.add_term_frees (t, OldTerm.add_term_vars (t,[])) |
27330 | 587 |
val closed_t = fold_rev Logic.all vars t |
21588 | 588 |
val rew_th = norm_term thy closed_t |
22902
ac833b4bb7ee
moved some Drule operations to Thm (see more_thm.ML);
wenzelm
parents:
22846
diff
changeset
|
589 |
val rhs = Thm.rhs_of rew_th |
14516 | 590 |
|
21588 | 591 |
val shuffles = ShuffleData.get thy |
592 |
fun process [] = NONE |
|
593 |
| process ((name,th)::thms) = |
|
594 |
let |
|
595 |
val norm_th = Thm.varifyT (norm_thm thy (close_thm (Thm.transfer thy th))) |
|
596 |
val triv_th = trivial rhs |
|
597 |
val _ = message ("Shuffler.set_prop: Gluing together " ^ (string_of_thm norm_th) ^ " and " ^ (string_of_thm triv_th)) |
|
31945 | 598 |
val mod_th = case Seq.pull (Thm.bicompose false (*true*) (false,norm_th,0) 1 triv_th) of |
21588 | 599 |
SOME(th,_) => SOME th |
600 |
| NONE => NONE |
|
601 |
in |
|
602 |
case mod_th of |
|
603 |
SOME mod_th => |
|
604 |
let |
|
605 |
val closed_th = equal_elim (symmetric rew_th) mod_th |
|
606 |
in |
|
607 |
message ("Shuffler.set_prop succeeded by " ^ name); |
|
608 |
SOME (name,forall_elim_list (map (cterm_of thy) vars) closed_th) |
|
609 |
end |
|
610 |
| NONE => process thms |
|
611 |
end |
|
612 |
handle e as THM _ => process thms |
|
14516 | 613 |
in |
21588 | 614 |
fn thms => |
615 |
case process thms of |
|
616 |
res as SOME (name,th) => if (prop_of th) aconv t |
|
617 |
then res |
|
618 |
else error "Internal error in set_prop" |
|
619 |
| NONE => NONE |
|
14516 | 620 |
end |
17959 | 621 |
handle e => (writeln "set_prop internal error"; OldGoals.print_exn e) |
14516 | 622 |
|
623 |
fun find_potential thy t = |
|
624 |
let |
|
21588 | 625 |
val shuffles = ShuffleData.get thy |
626 |
val ignored = collect_ignored shuffles [] |
|
26662 | 627 |
val all_thms = |
27865
27a8ad9612a3
moved basic thm operations from structure PureThy to Thm (cf. more_thm.ML);
wenzelm
parents:
27330
diff
changeset
|
628 |
map (`Thm.get_name_hint) (maps #2 (Facts.dest_static [] (PureThy.facts_of thy))) |
14516 | 629 |
in |
33317 | 630 |
filter (match_consts ignored t) all_thms |
14516 | 631 |
end |
632 |
||
31241 | 633 |
fun gen_shuffle_tac ctxt search thms i st = |
14516 | 634 |
let |
31241 | 635 |
val thy = ProofContext.theory_of ctxt |
21588 | 636 |
val _ = message ("Shuffling " ^ (string_of_thm st)) |
637 |
val t = List.nth(prems_of st,i-1) |
|
638 |
val set = set_prop thy t |
|
639 |
fun process_tac thms st = |
|
640 |
case set thms of |
|
641 |
SOME (_,th) => Seq.of_list (compose (th,i,st)) |
|
642 |
| NONE => Seq.empty |
|
14516 | 643 |
in |
21588 | 644 |
(process_tac thms APPEND (if search |
645 |
then process_tac (find_potential thy t) |
|
646 |
else no_tac)) st |
|
14516 | 647 |
end |
648 |
||
31244
4ed31c673baf
fixed superficial ML lapses introduced in b3c7044d47b6;
wenzelm
parents:
31241
diff
changeset
|
649 |
fun shuffle_tac ctxt thms = |
4ed31c673baf
fixed superficial ML lapses introduced in b3c7044d47b6;
wenzelm
parents:
31241
diff
changeset
|
650 |
gen_shuffle_tac ctxt false (map (pair "") thms); |
4ed31c673baf
fixed superficial ML lapses introduced in b3c7044d47b6;
wenzelm
parents:
31241
diff
changeset
|
651 |
|
4ed31c673baf
fixed superficial ML lapses introduced in b3c7044d47b6;
wenzelm
parents:
31241
diff
changeset
|
652 |
fun search_tac ctxt = |
4ed31c673baf
fixed superficial ML lapses introduced in b3c7044d47b6;
wenzelm
parents:
31241
diff
changeset
|
653 |
gen_shuffle_tac ctxt true (map (pair "premise") (Assumption.all_prems_of ctxt)); |
14516 | 654 |
|
655 |
fun add_shuffle_rule thm thy = |
|
656 |
let |
|
21588 | 657 |
val shuffles = ShuffleData.get thy |
14516 | 658 |
in |
21588 | 659 |
if exists (curry Thm.eq_thm thm) shuffles |
660 |
then (warning ((string_of_thm thm) ^ " already known to the shuffler"); |
|
661 |
thy) |
|
662 |
else ShuffleData.put (thm::shuffles) thy |
|
14516 | 663 |
end |
664 |
||
20897 | 665 |
val shuffle_attr = Thm.declaration_attribute (fn th => Context.mapping (add_shuffle_rule th) I); |
14516 | 666 |
|
18708 | 667 |
val setup = |
31241 | 668 |
Method.setup @{binding shuffle_tac} |
31244
4ed31c673baf
fixed superficial ML lapses introduced in b3c7044d47b6;
wenzelm
parents:
31241
diff
changeset
|
669 |
(Attrib.thms >> (fn ths => fn ctxt => SIMPLE_METHOD' (shuffle_tac ctxt ths))) |
31241 | 670 |
"solve goal by shuffling terms around" #> |
671 |
Method.setup @{binding search_tac} |
|
672 |
(Scan.succeed (SIMPLE_METHOD' o search_tac)) "search for suitable theorems" #> |
|
18708 | 673 |
add_shuffle_rule weaken #> |
674 |
add_shuffle_rule equiv_comm #> |
|
675 |
add_shuffle_rule imp_comm #> |
|
676 |
add_shuffle_rule Drule.norm_hhf_eq #> |
|
677 |
add_shuffle_rule Drule.triv_forall_equality #> |
|
30528 | 678 |
Attrib.setup @{binding shuffle_rule} (Scan.succeed shuffle_attr) "declare rule for shuffler"; |
18708 | 679 |
|
14516 | 680 |
end |