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