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