author | wenzelm |
Wed, 25 Aug 2010 18:36:22 +0200 | |
changeset 38715 | 6513ea67d95d |
parent 38558 | 32ad17fe2b9c |
child 38864 | 4abe644fcea5 |
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 |
||
37146
f652333bbf8e
renamed structure PrintMode to Print_Mode, keeping the old name as legacy alias for some time;
wenzelm
parents:
36945
diff
changeset
|
59 |
val string_of_thm = Print_Mode.setmp [] Display.string_of_thm_without_context; |
14516 | 60 |
|
61 |
fun mk_meta_eq th = |
|
62 |
(case concl_of th of |
|
38558 | 63 |
Const(@{const_name Trueprop},_) $ (Const(@{const_name "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 |
|
38558 | 70 |
Const(@{const_name Trueprop},_) $ (Const(@{const_name "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 |
|
36945 | 98 |
val th1 = Thm.assume cPQ |> implies_intr_list [cPQ,cP] |
99 |
val th3 = Thm.assume cP |
|
100 |
val th4 = implies_elim_list (Thm.assume cPPQ) [th3,th3] |
|
21588 | 101 |
|> implies_intr_list [cPPQ,cP] |
14516 | 102 |
in |
36945 | 103 |
Thm.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 |
|
36945 | 118 |
val th1 = implies_elim_list (Thm.assume cPQR) [Thm.assume cP,Thm.assume cQ] |
21588 | 119 |
|> implies_intr_list [cPQR,cQ,cP] |
36945 | 120 |
val th2 = implies_elim_list (Thm.assume cQPR) [Thm.assume cQ,Thm.assume cP] |
21588 | 121 |
|> implies_intr_list [cQPR,cP,cQ] |
14516 | 122 |
in |
36945 | 123 |
Thm.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 |
|
36945 | 137 |
val rew = Thm.assume cvPQ |
138 |
|> Thm.forall_elim cv |
|
139 |
|> Thm.abstract_rule "v" cv |
|
21588 | 140 |
val (lhs,rhs) = Logic.dest_equals(concl_of rew) |
36945 | 141 |
val th1 = Thm.transitive (Thm.transitive |
142 |
(Thm.eta_conversion (cert lhs) |> Thm.symmetric) |
|
21588 | 143 |
rew) |
36945 | 144 |
(Thm.eta_conversion (cert rhs)) |
145 |
|> Thm.implies_intr cvPQ |
|
146 |
val th2 = Thm.combination (Thm.assume cPQ) (Thm.reflexive cv) |
|
147 |
|> Thm.forall_intr cv |
|
148 |
|> Thm.implies_intr cPQ |
|
14516 | 149 |
in |
36945 | 150 |
Thm.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 |
|
36945 | 167 |
val th1 = Thm.assume cr |
21588 | 168 |
|> forall_elim_list [cy,cx] |
169 |
|> forall_intr_list [cx,cy] |
|
36945 | 170 |
|> Thm.implies_intr cr |
171 |
val th2 = Thm.assume cl |
|
21588 | 172 |
|> forall_elim_list [cx,cy] |
173 |
|> forall_intr_list [cy,cx] |
|
36945 | 174 |
|> Thm.implies_intr cl |
14516 | 175 |
in |
36945 | 176 |
Thm.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)) |
|
36945 | 187 |
val th1 = Thm.assume ctu |> Thm.symmetric |> Thm.implies_intr ctu |
188 |
val th2 = Thm.assume cut |> Thm.symmetric |> Thm.implies_intr cut |
|
14516 | 189 |
in |
36945 | 190 |
Thm.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) |
|
36945 | 220 |
val init = Thm.trivial (cterm_of thy rew) |
14516 | 221 |
in |
37778
87b5dfe00387
do not intercept ML exceptions -- printing exception positions/text is the job of the Isar/ML toplevel;
wenzelm
parents:
37146
diff
changeset
|
222 |
all_comm RS init |
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))) |
|
36945 | 235 |
val t_th = Thm.reflexive (cterm_of thy t) |
236 |
val newt_th = Thm.reflexive (cterm_of thy newt) |
|
21588 | 237 |
in |
36945 | 238 |
SOME (Thm.transitive t_th newt_th) |
21588 | 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,[]) |
35845
e5980f0ad025
renamed varify/unvarify operations to varify_global/unvarify_global to emphasize that these only work in a global situation;
wenzelm
parents:
35408
diff
changeset
|
268 |
val (rens, thm') = Thm.varifyT_global' |
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 |
36945 | 297 |
val (init,thaw) = Drule.legacy_freeze_thaw (Thm.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)) => |
|
37778
87b5dfe00387
do not intercept ML exceptions -- printing exception positions/text is the job of the Isar/ML toplevel;
wenzelm
parents:
37146
diff
changeset
|
318 |
(if eta_redex P andalso eta_redex Q |
21588 | 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 |
|
36945 | 325 |
val th = (Thm.assume ct) |
326 |
|> Thm.forall_elim cv |
|
327 |
|> Thm.abstract_rule x cv |
|
328 |
val ext_th = Thm.eta_conversion (cert (Abs(x,xT,P))) |
|
329 |
val th' = Thm.transitive (Thm.symmetric ext_th) th |
|
21588 | 330 |
val cu = cert (prop_of th') |
36945 | 331 |
val uth = Thm.combination (Thm.assume cu) (Thm.reflexive cv) |
332 |
val uth' = (Thm.beta_conversion false (cert (Abs(x,xT,Q) $ v))) |
|
333 |
|> Thm.transitive uth |
|
334 |
|> Thm.forall_intr cv |
|
335 |
|> Thm.implies_intr cu |
|
336 |
val rew_th = Thm.equal_intr (th' |> Thm.implies_intr ct) uth' |
|
21588 | 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 |
| _ => NONE |
|
17440
df77edc4f5d0
fixed HOL-light/Isabelle syntax incompatability via more protect_xxx functions
obua
parents:
17188
diff
changeset
|
344 |
end |
14516 | 345 |
|
21078 | 346 |
fun beta_fun thy assume t = |
36945 | 347 |
SOME (Thm.beta_conversion true (cterm_of thy t)) |
14516 | 348 |
|
17188 | 349 |
val meta_sym_rew = thm "refl" |
350 |
||
21078 | 351 |
fun equals_fun thy assume t = |
17188 | 352 |
case t of |
35408 | 353 |
Const("op ==",_) $ u $ v => if Term_Ord.term_ord (u,v) = LESS then SOME (meta_sym_rew) else NONE |
17188 | 354 |
| _ => NONE |
355 |
||
21078 | 356 |
fun eta_expand thy assumes origt = |
14516 | 357 |
let |
21588 | 358 |
val (typet,Tinst) = freeze_thaw_term origt |
36945 | 359 |
val (init,thaw) = Drule.legacy_freeze_thaw (Thm.reflexive (cterm_of thy typet)) |
21588 | 360 |
val final = inst_tfrees thy Tinst o thaw |
361 |
val t = #1 (Logic.dest_equals (prop_of init)) |
|
362 |
val _ = |
|
363 |
let |
|
364 |
val lhs = #1 (Logic.dest_equals (prop_of (final init))) |
|
365 |
in |
|
366 |
if not (lhs aconv origt) |
|
32432
64f30bdd3ba1
modernized messages -- eliminated ctyp/cterm operations;
wenzelm
parents:
32091
diff
changeset
|
367 |
then |
64f30bdd3ba1
modernized messages -- eliminated ctyp/cterm operations;
wenzelm
parents:
32091
diff
changeset
|
368 |
writeln (cat_lines |
64f30bdd3ba1
modernized messages -- eliminated ctyp/cterm operations;
wenzelm
parents:
32091
diff
changeset
|
369 |
(["Something is utterly wrong: (orig, lhs, frozen type, t, tinst)", |
64f30bdd3ba1
modernized messages -- eliminated ctyp/cterm operations;
wenzelm
parents:
32091
diff
changeset
|
370 |
Syntax.string_of_term_global thy origt, |
64f30bdd3ba1
modernized messages -- eliminated ctyp/cterm operations;
wenzelm
parents:
32091
diff
changeset
|
371 |
Syntax.string_of_term_global thy lhs, |
64f30bdd3ba1
modernized messages -- eliminated ctyp/cterm operations;
wenzelm
parents:
32091
diff
changeset
|
372 |
Syntax.string_of_term_global thy typet, |
64f30bdd3ba1
modernized messages -- eliminated ctyp/cterm operations;
wenzelm
parents:
32091
diff
changeset
|
373 |
Syntax.string_of_term_global thy t] @ |
64f30bdd3ba1
modernized messages -- eliminated ctyp/cterm operations;
wenzelm
parents:
32091
diff
changeset
|
374 |
map (fn (n, T) => n ^ ": " ^ Syntax.string_of_typ_global thy T) Tinst)) |
21588 | 375 |
else () |
376 |
end |
|
14516 | 377 |
in |
21588 | 378 |
case t of |
379 |
Const("==",T) $ P $ Q => |
|
380 |
if is_Abs P orelse is_Abs Q |
|
381 |
then (case domain_type T of |
|
382 |
Type("fun",[aT,bT]) => |
|
383 |
let |
|
384 |
val cert = cterm_of thy |
|
29281 | 385 |
val vname = Name.variant (Term.add_free_names t []) "v" |
21588 | 386 |
val v = Free(vname,aT) |
387 |
val cv = cert v |
|
388 |
val ct = cert t |
|
36945 | 389 |
val th1 = (Thm.combination (Thm.assume ct) (Thm.reflexive cv)) |
390 |
|> Thm.forall_intr cv |
|
391 |
|> Thm.implies_intr ct |
|
21588 | 392 |
val concl = cert (concl_of th1) |
36945 | 393 |
val th2 = (Thm.assume concl) |
394 |
|> Thm.forall_elim cv |
|
395 |
|> Thm.abstract_rule vname cv |
|
21588 | 396 |
val (lhs,rhs) = Logic.dest_equals (prop_of th2) |
36945 | 397 |
val elhs = Thm.eta_conversion (cert lhs) |
398 |
val erhs = Thm.eta_conversion (cert rhs) |
|
399 |
val th2' = Thm.transitive |
|
400 |
(Thm.transitive (Thm.symmetric elhs) th2) |
|
21588 | 401 |
erhs |
36945 | 402 |
val res = Thm.equal_intr th1 (th2' |> Thm.implies_intr concl) |
21588 | 403 |
val res' = final res |
404 |
in |
|
405 |
SOME res' |
|
406 |
end |
|
407 |
| _ => NONE) |
|
408 |
else NONE |
|
32432
64f30bdd3ba1
modernized messages -- eliminated ctyp/cterm operations;
wenzelm
parents:
32091
diff
changeset
|
409 |
| _ => error ("Bad eta_expand argument" ^ Syntax.string_of_term_global thy t) |
64f30bdd3ba1
modernized messages -- eliminated ctyp/cterm operations;
wenzelm
parents:
32091
diff
changeset
|
410 |
end; |
14516 | 411 |
|
14854 | 412 |
fun mk_tfree s = TFree("'"^s,[]) |
20326 | 413 |
fun mk_free s t = Free (s,t) |
14516 | 414 |
val xT = mk_tfree "a" |
415 |
val yT = mk_tfree "b" |
|
27330 | 416 |
val x = Free ("x", xT) |
417 |
val y = Free ("y", yT) |
|
20326 | 418 |
val P = mk_free "P" (xT-->yT-->propT) |
419 |
val Q = mk_free "Q" (xT-->yT) |
|
420 |
val R = mk_free "R" (xT-->yT) |
|
421 |
val S = mk_free "S" xT |
|
422 |
val S' = mk_free "S'" xT |
|
14516 | 423 |
in |
38715
6513ea67d95d
renamed Simplifier.simproc(_i) to Simplifier.simproc_global(_i) to emphasize that this is not the real thing;
wenzelm
parents:
38558
diff
changeset
|
424 |
fun beta_simproc thy = Simplifier.simproc_global_i |
21588 | 425 |
thy |
426 |
"Beta-contraction" |
|
427 |
[Abs("x",xT,Q) $ S] |
|
428 |
beta_fun |
|
14516 | 429 |
|
38715
6513ea67d95d
renamed Simplifier.simproc(_i) to Simplifier.simproc_global(_i) to emphasize that this is not the real thing;
wenzelm
parents:
38558
diff
changeset
|
430 |
fun equals_simproc thy = Simplifier.simproc_global_i |
21588 | 431 |
thy |
432 |
"Ordered rewriting of meta equalities" |
|
433 |
[Const("op ==",xT) $ S $ S'] |
|
434 |
equals_fun |
|
17188 | 435 |
|
38715
6513ea67d95d
renamed Simplifier.simproc(_i) to Simplifier.simproc_global(_i) to emphasize that this is not the real thing;
wenzelm
parents:
38558
diff
changeset
|
436 |
fun quant_simproc thy = Simplifier.simproc_global_i |
21588 | 437 |
thy |
438 |
"Ordered rewriting of nested quantifiers" |
|
27330 | 439 |
[Logic.all x (Logic.all y (P $ x $ y))] |
21588 | 440 |
quant_rewrite |
38715
6513ea67d95d
renamed Simplifier.simproc(_i) to Simplifier.simproc_global(_i) to emphasize that this is not the real thing;
wenzelm
parents:
38558
diff
changeset
|
441 |
fun eta_expand_simproc thy = Simplifier.simproc_global_i |
21588 | 442 |
thy |
443 |
"Smart eta-expansion by equivalences" |
|
444 |
[Logic.mk_equals(Q,R)] |
|
445 |
eta_expand |
|
38715
6513ea67d95d
renamed Simplifier.simproc(_i) to Simplifier.simproc_global(_i) to emphasize that this is not the real thing;
wenzelm
parents:
38558
diff
changeset
|
446 |
fun eta_contract_simproc thy = Simplifier.simproc_global_i |
21588 | 447 |
thy |
448 |
"Smart handling of eta-contractions" |
|
27330 | 449 |
[Logic.all x (Logic.mk_equals (Q $ x, R $ x))] |
21588 | 450 |
eta_contract |
14516 | 451 |
end |
452 |
||
453 |
(* Disambiguates the names of bound variables in a term, returning t |
|
454 |
== t' where all the names of bound variables in t' are unique *) |
|
455 |
||
21078 | 456 |
fun disamb_bound thy t = |
14516 | 457 |
let |
21588 | 458 |
|
459 |
fun F (t $ u,idx) = |
|
460 |
let |
|
461 |
val (t',idx') = F (t,idx) |
|
462 |
val (u',idx'') = F (u,idx') |
|
463 |
in |
|
464 |
(t' $ u',idx'') |
|
465 |
end |
|
466 |
| F (Abs(x,xT,t),idx) = |
|
467 |
let |
|
468 |
val x' = "x" ^ (LargeInt.toString idx) (* amazing *) |
|
469 |
val (t',idx') = F (t,idx+1) |
|
470 |
in |
|
471 |
(Abs(x',xT,t'),idx') |
|
472 |
end |
|
473 |
| F arg = arg |
|
474 |
val (t',_) = F (t,0) |
|
475 |
val ct = cterm_of thy t |
|
476 |
val ct' = cterm_of thy t' |
|
36945 | 477 |
val res = Thm.transitive (Thm.reflexive ct) (Thm.reflexive ct') |
21588 | 478 |
val _ = message ("disamb_term: " ^ (string_of_thm res)) |
14516 | 479 |
in |
21588 | 480 |
res |
14516 | 481 |
end |
482 |
||
483 |
(* Transforms a term t to some normal form t', returning the theorem t |
|
484 |
== t'. This is originally a help function for make_equal, but might |
|
485 |
be handy in its own right, for example for indexing terms. *) |
|
486 |
||
487 |
fun norm_term thy t = |
|
488 |
let |
|
21588 | 489 |
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
|
490 |
val ss = Simplifier.global_context thy empty_ss |
36543
0e7fc5bf38de
proper context for mksimps etc. -- via simpset of the running Simplifier;
wenzelm
parents:
35845
diff
changeset
|
491 |
setmksimps (K single) |
21588 | 492 |
addsimps (map (Thm.transfer thy) norms) |
21078 | 493 |
addsimprocs [quant_simproc thy, eta_expand_simproc thy,eta_contract_simproc thy] |
21588 | 494 |
fun chain f th = |
495 |
let |
|
22902
ac833b4bb7ee
moved some Drule operations to Thm (see more_thm.ML);
wenzelm
parents:
22846
diff
changeset
|
496 |
val rhs = Thm.rhs_of th |
21588 | 497 |
in |
36945 | 498 |
Thm.transitive th (f rhs) |
21588 | 499 |
end |
500 |
val th = |
|
21078 | 501 |
t |> disamb_bound thy |
21588 | 502 |
|> chain (Simplifier.full_rewrite ss) |
36945 | 503 |
|> chain Thm.eta_conversion |
36614
b6c031ad3690
minor tuning of Thm.strip_shyps -- no longer pervasive;
wenzelm
parents:
36543
diff
changeset
|
504 |
|> Thm.strip_shyps |
21588 | 505 |
val _ = message ("norm_term: " ^ (string_of_thm th)) |
14516 | 506 |
in |
21588 | 507 |
th |
17463 | 508 |
end |
21078 | 509 |
handle e => (writeln "norm_term internal error"; print_sign_exn thy e) |
14516 | 510 |
|
511 |
||
512 |
(* Closes a theorem with respect to free and schematic variables (does |
|
513 |
not touch type variables, though). *) |
|
514 |
||
515 |
fun close_thm th = |
|
516 |
let |
|
22578 | 517 |
val thy = Thm.theory_of_thm th |
21588 | 518 |
val c = prop_of th |
29265
5b4247055bd7
moved old add_term_vars, add_term_frees etc. to structure OldTerm;
wenzelm
parents:
28397
diff
changeset
|
519 |
val vars = OldTerm.add_term_frees (c, OldTerm.add_term_vars(c,[])) |
14516 | 520 |
in |
21588 | 521 |
Drule.forall_intr_list (map (cterm_of thy) vars) th |
14516 | 522 |
end |
37778
87b5dfe00387
do not intercept ML exceptions -- printing exception positions/text is the job of the Isar/ML toplevel;
wenzelm
parents:
37146
diff
changeset
|
523 |
|
14516 | 524 |
|
525 |
(* Normalizes a theorem's conclusion using norm_term. *) |
|
526 |
||
527 |
fun norm_thm thy th = |
|
528 |
let |
|
21588 | 529 |
val c = prop_of th |
14516 | 530 |
in |
36945 | 531 |
Thm.equal_elim (norm_term thy c) th |
14516 | 532 |
end |
533 |
||
21078 | 534 |
(* make_equal thy t u tries to construct the theorem t == u under the |
535 |
signature thy. If it succeeds, SOME (t == u) is returned, otherwise |
|
15531 | 536 |
NONE is returned. *) |
14516 | 537 |
|
21078 | 538 |
fun make_equal thy t u = |
14516 | 539 |
let |
21588 | 540 |
val t_is_t' = norm_term thy t |
541 |
val u_is_u' = norm_term thy u |
|
36945 | 542 |
val th = Thm.transitive t_is_t' (Thm.symmetric u_is_u') |
21588 | 543 |
val _ = message ("make_equal: SOME " ^ (string_of_thm th)) |
14516 | 544 |
in |
21588 | 545 |
SOME th |
14516 | 546 |
end |
15531 | 547 |
handle e as THM _ => (message "make_equal: NONE";NONE) |
21588 | 548 |
|
14516 | 549 |
fun match_consts ignore t (* th *) = |
550 |
let |
|
21588 | 551 |
fun add_consts (Const (c, _), cs) = |
36692
54b64d4ad524
farewell to old-style mem infixes -- type inference in situations with mem_int and mem_string should provide enough information to resolve the type of (op =)
haftmann
parents:
36614
diff
changeset
|
552 |
if member (op =) ignore c |
21588 | 553 |
then cs |
554 |
else insert (op =) c cs |
|
555 |
| add_consts (t $ u, cs) = add_consts (t, add_consts (u, cs)) |
|
556 |
| add_consts (Abs (_, _, t), cs) = add_consts (t, cs) |
|
557 |
| add_consts (_, cs) = cs |
|
558 |
val t_consts = add_consts(t,[]) |
|
14516 | 559 |
in |
560 |
fn (name,th) => |
|
21588 | 561 |
let |
562 |
val th_consts = add_consts(prop_of th,[]) |
|
563 |
in |
|
33038 | 564 |
eq_set (op =) (t_consts, th_consts) |
21588 | 565 |
end |
14516 | 566 |
end |
21588 | 567 |
|
33040 | 568 |
val collect_ignored = fold_rev (fn thm => fn cs => |
569 |
let |
|
570 |
val (lhs, rhs) = Logic.dest_equals (prop_of thm); |
|
571 |
val consts_lhs = Term.add_const_names lhs []; |
|
572 |
val consts_rhs = Term.add_const_names rhs []; |
|
573 |
val ignore_lhs = subtract (op =) consts_rhs consts_lhs; |
|
574 |
val ignore_rhs = subtract (op =) consts_lhs consts_rhs; |
|
575 |
in |
|
576 |
fold_rev (insert (op =)) cs (ignore_lhs @ ignore_rhs) |
|
577 |
end) |
|
14516 | 578 |
|
579 |
(* set_prop t thms tries to make a theorem with the proposition t from |
|
580 |
one of the theorems thms, by shuffling the propositions around. If it |
|
15531 | 581 |
succeeds, SOME theorem is returned, otherwise NONE. *) |
14516 | 582 |
|
583 |
fun set_prop thy t = |
|
584 |
let |
|
29265
5b4247055bd7
moved old add_term_vars, add_term_frees etc. to structure OldTerm;
wenzelm
parents:
28397
diff
changeset
|
585 |
val vars = OldTerm.add_term_frees (t, OldTerm.add_term_vars (t,[])) |
27330 | 586 |
val closed_t = fold_rev Logic.all vars t |
21588 | 587 |
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
|
588 |
val rhs = Thm.rhs_of rew_th |
14516 | 589 |
|
21588 | 590 |
val shuffles = ShuffleData.get thy |
591 |
fun process [] = NONE |
|
592 |
| process ((name,th)::thms) = |
|
593 |
let |
|
35845
e5980f0ad025
renamed varify/unvarify operations to varify_global/unvarify_global to emphasize that these only work in a global situation;
wenzelm
parents:
35408
diff
changeset
|
594 |
val norm_th = Thm.varifyT_global (norm_thm thy (close_thm (Thm.transfer thy th))) |
36945 | 595 |
val triv_th = Thm.trivial rhs |
21588 | 596 |
val _ = message ("Shuffler.set_prop: Gluing together " ^ (string_of_thm norm_th) ^ " and " ^ (string_of_thm triv_th)) |
31945 | 597 |
val mod_th = case Seq.pull (Thm.bicompose false (*true*) (false,norm_th,0) 1 triv_th) of |
21588 | 598 |
SOME(th,_) => SOME th |
599 |
| NONE => NONE |
|
600 |
in |
|
601 |
case mod_th of |
|
602 |
SOME mod_th => |
|
603 |
let |
|
36945 | 604 |
val closed_th = Thm.equal_elim (Thm.symmetric rew_th) mod_th |
21588 | 605 |
in |
606 |
message ("Shuffler.set_prop succeeded by " ^ name); |
|
607 |
SOME (name,forall_elim_list (map (cterm_of thy) vars) closed_th) |
|
608 |
end |
|
609 |
| NONE => process thms |
|
610 |
end |
|
611 |
handle e as THM _ => process thms |
|
14516 | 612 |
in |
21588 | 613 |
fn thms => |
614 |
case process thms of |
|
615 |
res as SOME (name,th) => if (prop_of th) aconv t |
|
616 |
then res |
|
617 |
else error "Internal error in set_prop" |
|
618 |
| NONE => NONE |
|
14516 | 619 |
end |
620 |
||
621 |
fun find_potential thy t = |
|
622 |
let |
|
21588 | 623 |
val shuffles = ShuffleData.get thy |
624 |
val ignored = collect_ignored shuffles [] |
|
26662 | 625 |
val all_thms = |
27865
27a8ad9612a3
moved basic thm operations from structure PureThy to Thm (cf. more_thm.ML);
wenzelm
parents:
27330
diff
changeset
|
626 |
map (`Thm.get_name_hint) (maps #2 (Facts.dest_static [] (PureThy.facts_of thy))) |
14516 | 627 |
in |
33317 | 628 |
filter (match_consts ignored t) all_thms |
14516 | 629 |
end |
630 |
||
31241 | 631 |
fun gen_shuffle_tac ctxt search thms i st = |
14516 | 632 |
let |
31241 | 633 |
val thy = ProofContext.theory_of ctxt |
21588 | 634 |
val _ = message ("Shuffling " ^ (string_of_thm st)) |
635 |
val t = List.nth(prems_of st,i-1) |
|
636 |
val set = set_prop thy t |
|
637 |
fun process_tac thms st = |
|
638 |
case set thms of |
|
639 |
SOME (_,th) => Seq.of_list (compose (th,i,st)) |
|
640 |
| NONE => Seq.empty |
|
14516 | 641 |
in |
21588 | 642 |
(process_tac thms APPEND (if search |
643 |
then process_tac (find_potential thy t) |
|
644 |
else no_tac)) st |
|
14516 | 645 |
end |
646 |
||
31244
4ed31c673baf
fixed superficial ML lapses introduced in b3c7044d47b6;
wenzelm
parents:
31241
diff
changeset
|
647 |
fun shuffle_tac ctxt thms = |
4ed31c673baf
fixed superficial ML lapses introduced in b3c7044d47b6;
wenzelm
parents:
31241
diff
changeset
|
648 |
gen_shuffle_tac ctxt false (map (pair "") thms); |
4ed31c673baf
fixed superficial ML lapses introduced in b3c7044d47b6;
wenzelm
parents:
31241
diff
changeset
|
649 |
|
4ed31c673baf
fixed superficial ML lapses introduced in b3c7044d47b6;
wenzelm
parents:
31241
diff
changeset
|
650 |
fun search_tac ctxt = |
4ed31c673baf
fixed superficial ML lapses introduced in b3c7044d47b6;
wenzelm
parents:
31241
diff
changeset
|
651 |
gen_shuffle_tac ctxt true (map (pair "premise") (Assumption.all_prems_of ctxt)); |
14516 | 652 |
|
653 |
fun add_shuffle_rule thm thy = |
|
654 |
let |
|
21588 | 655 |
val shuffles = ShuffleData.get thy |
14516 | 656 |
in |
21588 | 657 |
if exists (curry Thm.eq_thm thm) shuffles |
658 |
then (warning ((string_of_thm thm) ^ " already known to the shuffler"); |
|
659 |
thy) |
|
660 |
else ShuffleData.put (thm::shuffles) thy |
|
14516 | 661 |
end |
662 |
||
20897 | 663 |
val shuffle_attr = Thm.declaration_attribute (fn th => Context.mapping (add_shuffle_rule th) I); |
14516 | 664 |
|
18708 | 665 |
val setup = |
31241 | 666 |
Method.setup @{binding shuffle_tac} |
31244
4ed31c673baf
fixed superficial ML lapses introduced in b3c7044d47b6;
wenzelm
parents:
31241
diff
changeset
|
667 |
(Attrib.thms >> (fn ths => fn ctxt => SIMPLE_METHOD' (shuffle_tac ctxt ths))) |
31241 | 668 |
"solve goal by shuffling terms around" #> |
669 |
Method.setup @{binding search_tac} |
|
670 |
(Scan.succeed (SIMPLE_METHOD' o search_tac)) "search for suitable theorems" #> |
|
18708 | 671 |
add_shuffle_rule weaken #> |
672 |
add_shuffle_rule equiv_comm #> |
|
673 |
add_shuffle_rule imp_comm #> |
|
674 |
add_shuffle_rule Drule.norm_hhf_eq #> |
|
675 |
add_shuffle_rule Drule.triv_forall_equality #> |
|
30528 | 676 |
Attrib.setup @{binding shuffle_rule} (Scan.succeed shuffle_attr) "declare rule for shuffler"; |
18708 | 677 |
|
14516 | 678 |
end |