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