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