src/HOL/Tools/Quotient/quotient_tacs.ML
author wenzelm
Thu Jun 09 23:12:02 2011 +0200 (2011-06-09)
changeset 43333 2bdec7f430d3
parent 42361 23f352990944
child 43596 78211f66cf8d
permissions -rw-r--r--
renamed Drule.instantiate to Drule.instantiate_normalize to emphasize its meaning as opposed to plain Thm.instantiate;
haftmann@37744
     1
(*  Title:      HOL/Tools/Quotient/quotient_tacs.ML
kaliszyk@35222
     2
    Author:     Cezary Kaliszyk and Christian Urban
kaliszyk@35222
     3
wenzelm@35788
     4
Tactics for solving goal arising from lifting theorems to quotient
wenzelm@35788
     5
types.
kaliszyk@35222
     6
*)
kaliszyk@35222
     7
kaliszyk@35222
     8
signature QUOTIENT_TACS =
kaliszyk@35222
     9
sig
kaliszyk@35222
    10
  val regularize_tac: Proof.context -> int -> tactic
kaliszyk@35222
    11
  val injection_tac: Proof.context -> int -> tactic
kaliszyk@35222
    12
  val all_injection_tac: Proof.context -> int -> tactic
kaliszyk@35222
    13
  val clean_tac: Proof.context -> int -> tactic
wenzelm@41444
    14
urbanc@38859
    15
  val descend_procedure_tac: Proof.context -> thm list -> int -> tactic
urbanc@38859
    16
  val descend_tac: Proof.context -> thm list -> int -> tactic
wenzelm@41444
    17
urbanc@38859
    18
  val lift_procedure_tac: Proof.context -> thm list -> thm -> int -> tactic
urbanc@38859
    19
  val lift_tac: Proof.context -> thm list -> thm list -> int -> tactic
urbanc@37593
    20
urbanc@38625
    21
  val lifted: Proof.context -> typ list -> thm list -> thm -> thm
kaliszyk@35222
    22
  val lifted_attrib: attribute
kaliszyk@35222
    23
end;
kaliszyk@35222
    24
kaliszyk@35222
    25
structure Quotient_Tacs: QUOTIENT_TACS =
kaliszyk@35222
    26
struct
kaliszyk@35222
    27
kaliszyk@35222
    28
(** various helper fuctions **)
kaliszyk@35222
    29
kaliszyk@35222
    30
(* Since HOL_basic_ss is too "big" for us, we *)
kaliszyk@35222
    31
(* need to set up our own minimal simpset.    *)
kaliszyk@35222
    32
fun mk_minimal_ss ctxt =
kaliszyk@35222
    33
  Simplifier.context ctxt empty_ss
kaliszyk@35222
    34
    setsubgoaler asm_simp_tac
kaliszyk@35222
    35
    setmksimps (mksimps [])
kaliszyk@35222
    36
kaliszyk@35222
    37
(* composition of two theorems, used in maps *)
kaliszyk@35222
    38
fun OF1 thm1 thm2 = thm2 RS thm1
kaliszyk@35222
    39
kaliszyk@35222
    40
fun atomize_thm thm =
wenzelm@41444
    41
  let
wenzelm@41444
    42
    val thm' = Thm.legacy_freezeT (forall_intr_vars thm) (* FIXME/TODO: is this proper Isar-technology? no! *)
wenzelm@41444
    43
    val thm'' = Object_Logic.atomize (cprop_of thm')
wenzelm@41444
    44
  in
wenzelm@41444
    45
    @{thm equal_elim_rule1} OF [thm'', thm']
wenzelm@41444
    46
  end
kaliszyk@35222
    47
kaliszyk@35222
    48
kaliszyk@35222
    49
kaliszyk@35222
    50
(*** Regularize Tactic ***)
kaliszyk@35222
    51
kaliszyk@35222
    52
(** solvers for equivp and quotient assumptions **)
kaliszyk@35222
    53
kaliszyk@35222
    54
fun equiv_tac ctxt =
wenzelm@41451
    55
  REPEAT_ALL_NEW (resolve_tac (Quotient_Info.equiv_rules_get ctxt))
kaliszyk@35222
    56
kaliszyk@35222
    57
fun equiv_solver_tac ss = equiv_tac (Simplifier.the_context ss)
kaliszyk@35222
    58
val equiv_solver = Simplifier.mk_solver' "Equivalence goal solver" equiv_solver_tac
kaliszyk@35222
    59
kaliszyk@35222
    60
fun quotient_tac ctxt =
kaliszyk@35222
    61
  (REPEAT_ALL_NEW (FIRST'
kaliszyk@35222
    62
    [rtac @{thm identity_quotient},
wenzelm@41451
    63
     resolve_tac (Quotient_Info.quotient_rules_get ctxt)]))
kaliszyk@35222
    64
kaliszyk@35222
    65
fun quotient_solver_tac ss = quotient_tac (Simplifier.the_context ss)
kaliszyk@35222
    66
val quotient_solver =
kaliszyk@35222
    67
  Simplifier.mk_solver' "Quotient goal solver" quotient_solver_tac
kaliszyk@35222
    68
kaliszyk@35222
    69
fun solve_quotient_assm ctxt thm =
kaliszyk@35222
    70
  case Seq.pull (quotient_tac ctxt 1 thm) of
kaliszyk@35222
    71
    SOME (t, _) => t
kaliszyk@35222
    72
  | _ => error "Solve_quotient_assm failed. Possibly a quotient theorem is missing."
kaliszyk@35222
    73
kaliszyk@35222
    74
kaliszyk@35222
    75
fun prep_trm thy (x, (T, t)) =
kaliszyk@35222
    76
  (cterm_of thy (Var (x, T)), cterm_of thy t)
kaliszyk@35222
    77
kaliszyk@35222
    78
fun prep_ty thy (x, (S, ty)) =
kaliszyk@35222
    79
  (ctyp_of thy (TVar (x, S)), ctyp_of thy ty)
kaliszyk@35222
    80
kaliszyk@35222
    81
fun get_match_inst thy pat trm =
wenzelm@41444
    82
  let
wenzelm@41444
    83
    val univ = Unify.matchers thy [(pat, trm)]
wenzelm@41444
    84
    val SOME (env, _) = Seq.pull univ           (* raises Bind, if no unifier *) (* FIXME fragile *)
wenzelm@41444
    85
    val tenv = Vartab.dest (Envir.term_env env)
wenzelm@41444
    86
    val tyenv = Vartab.dest (Envir.type_env env)
wenzelm@41444
    87
  in
wenzelm@41444
    88
    (map (prep_ty thy) tyenv, map (prep_trm thy) tenv)
wenzelm@41444
    89
  end
kaliszyk@35222
    90
kaliszyk@35222
    91
(* Calculates the instantiations for the lemmas:
kaliszyk@35222
    92
kaliszyk@35222
    93
      ball_reg_eqv_range and bex_reg_eqv_range
kaliszyk@35222
    94
kaliszyk@35222
    95
   Since the left-hand-side contains a non-pattern '?P (f ?x)'
kaliszyk@35222
    96
   we rely on unification/instantiation to check whether the
kaliszyk@35222
    97
   theorem applies and return NONE if it doesn't.
kaliszyk@35222
    98
*)
kaliszyk@35222
    99
fun calculate_inst ctxt ball_bex_thm redex R1 R2 =
wenzelm@41444
   100
  let
wenzelm@42361
   101
    val thy = Proof_Context.theory_of ctxt
wenzelm@41444
   102
    fun get_lhs thm = fst (Logic.dest_equals (Thm.concl_of thm))
wenzelm@41444
   103
    val ty_inst = map (SOME o ctyp_of thy) [domain_type (fastype_of R2)]
wenzelm@41444
   104
    val trm_inst = map (SOME o cterm_of thy) [R2, R1]
wenzelm@41444
   105
  in
wenzelm@41444
   106
    (case try (Drule.instantiate' ty_inst trm_inst) ball_bex_thm of
wenzelm@41444
   107
      NONE => NONE
wenzelm@41444
   108
    | SOME thm' =>
wenzelm@41444
   109
        (case try (get_match_inst thy (get_lhs thm')) redex of
wenzelm@41444
   110
          NONE => NONE
wenzelm@43333
   111
        | SOME inst2 => try (Drule.instantiate_normalize inst2) thm'))
wenzelm@41444
   112
  end
kaliszyk@35222
   113
kaliszyk@35222
   114
fun ball_bex_range_simproc ss redex =
wenzelm@41444
   115
  let
wenzelm@41444
   116
    val ctxt = Simplifier.the_context ss
wenzelm@41444
   117
  in
wenzelm@41444
   118
    case redex of
wenzelm@41444
   119
      (Const (@{const_name "Ball"}, _) $ (Const (@{const_name "Respects"}, _) $
wenzelm@41444
   120
        (Const (@{const_name "fun_rel"}, _) $ R1 $ R2)) $ _) =>
wenzelm@41444
   121
          calculate_inst ctxt @{thm ball_reg_eqv_range[THEN eq_reflection]} redex R1 R2
kaliszyk@35222
   122
wenzelm@41444
   123
    | (Const (@{const_name "Bex"}, _) $ (Const (@{const_name "Respects"}, _) $
wenzelm@41444
   124
        (Const (@{const_name "fun_rel"}, _) $ R1 $ R2)) $ _) =>
wenzelm@41444
   125
          calculate_inst ctxt @{thm bex_reg_eqv_range[THEN eq_reflection]} redex R1 R2
kaliszyk@35222
   126
wenzelm@41444
   127
    | _ => NONE
wenzelm@41444
   128
  end
kaliszyk@35222
   129
kaliszyk@35222
   130
(* Regularize works as follows:
kaliszyk@35222
   131
kaliszyk@35222
   132
  0. preliminary simplification step according to
kaliszyk@35222
   133
     ball_reg_eqv bex_reg_eqv babs_reg_eqv ball_reg_eqv_range bex_reg_eqv_range
kaliszyk@35222
   134
kaliszyk@35222
   135
  1. eliminating simple Ball/Bex instances (ball_reg_right bex_reg_left)
kaliszyk@35222
   136
kaliszyk@35222
   137
  2. monos
kaliszyk@35222
   138
kaliszyk@35222
   139
  3. commutation rules for ball and bex (ball_all_comm bex_ex_comm)
kaliszyk@35222
   140
kaliszyk@35222
   141
  4. then rel-equalities, which need to be instantiated with 'eq_imp_rel'
kaliszyk@35222
   142
     to avoid loops
kaliszyk@35222
   143
kaliszyk@35222
   144
  5. then simplification like 0
kaliszyk@35222
   145
kaliszyk@35222
   146
  finally jump back to 1
kaliszyk@35222
   147
*)
kaliszyk@35222
   148
kaliszyk@37493
   149
fun reflp_get ctxt =
kaliszyk@37493
   150
  map_filter (fn th => if prems_of th = [] then SOME (OF1 @{thm equivp_reflp} th) else NONE
wenzelm@41451
   151
    handle THM _ => NONE) (Quotient_Info.equiv_rules_get ctxt)
kaliszyk@37493
   152
kaliszyk@37493
   153
val eq_imp_rel = @{lemma "equivp R ==> a = b --> R a b" by (simp add: equivp_reflp)}
kaliszyk@37493
   154
wenzelm@41451
   155
fun eq_imp_rel_get ctxt = map (OF1 eq_imp_rel) (Quotient_Info.equiv_rules_get ctxt)
kaliszyk@37493
   156
kaliszyk@35222
   157
fun regularize_tac ctxt =
wenzelm@41444
   158
  let
wenzelm@42361
   159
    val thy = Proof_Context.theory_of ctxt
wenzelm@41444
   160
    val ball_pat = @{term "Ball (Respects (R1 ===> R2)) P"}
wenzelm@41444
   161
    val bex_pat = @{term "Bex (Respects (R1 ===> R2)) P"}
wenzelm@41444
   162
    val simproc =
wenzelm@41444
   163
      Simplifier.simproc_global_i thy "" [ball_pat, bex_pat] (K (ball_bex_range_simproc))
wenzelm@41444
   164
    val simpset =
wenzelm@41444
   165
      mk_minimal_ss ctxt
wenzelm@41444
   166
      addsimps @{thms ball_reg_eqv bex_reg_eqv babs_reg_eqv babs_simp}
wenzelm@41444
   167
      addsimprocs [simproc]
wenzelm@41444
   168
      addSolver equiv_solver addSolver quotient_solver
wenzelm@41444
   169
    val eq_eqvs = eq_imp_rel_get ctxt
wenzelm@41444
   170
  in
wenzelm@41444
   171
    simp_tac simpset THEN'
wenzelm@41444
   172
    REPEAT_ALL_NEW (CHANGED o FIRST'
wenzelm@41444
   173
      [resolve_tac @{thms ball_reg_right bex_reg_left bex1_bexeq_reg},
wenzelm@41444
   174
       resolve_tac (Inductive.get_monos ctxt),
wenzelm@41444
   175
       resolve_tac @{thms ball_all_comm bex_ex_comm},
wenzelm@41444
   176
       resolve_tac eq_eqvs,
wenzelm@41444
   177
       simp_tac simpset])
wenzelm@41444
   178
  end
kaliszyk@35222
   179
kaliszyk@35222
   180
kaliszyk@35222
   181
kaliszyk@35222
   182
(*** Injection Tactic ***)
kaliszyk@35222
   183
kaliszyk@35222
   184
(* Looks for Quot_True assumptions, and in case its parameter
kaliszyk@35222
   185
   is an application, it returns the function and the argument.
kaliszyk@35222
   186
*)
kaliszyk@35222
   187
fun find_qt_asm asms =
wenzelm@41444
   188
  let
wenzelm@41444
   189
    fun find_fun trm =
wenzelm@41444
   190
      (case trm of
wenzelm@41444
   191
        (Const (@{const_name Trueprop}, _) $ (Const (@{const_name Quot_True}, _) $ _)) => true
wenzelm@41444
   192
      | _ => false)
wenzelm@41444
   193
  in
wenzelm@41444
   194
     (case find_first find_fun asms of
wenzelm@41444
   195
       SOME (_ $ (_ $ (f $ a))) => SOME (f, a)
wenzelm@41444
   196
     | _ => NONE)
wenzelm@41444
   197
  end
kaliszyk@35222
   198
kaliszyk@35222
   199
fun quot_true_simple_conv ctxt fnctn ctrm =
wenzelm@41444
   200
  case term_of ctrm of
kaliszyk@35222
   201
    (Const (@{const_name Quot_True}, _) $ x) =>
wenzelm@41444
   202
      let
wenzelm@41444
   203
        val fx = fnctn x;
wenzelm@42361
   204
        val thy = Proof_Context.theory_of ctxt;
wenzelm@41444
   205
        val cx = cterm_of thy x;
wenzelm@41444
   206
        val cfx = cterm_of thy fx;
wenzelm@41444
   207
        val cxt = ctyp_of thy (fastype_of x);
wenzelm@41444
   208
        val cfxt = ctyp_of thy (fastype_of fx);
wenzelm@41444
   209
        val thm = Drule.instantiate' [SOME cxt, SOME cfxt] [SOME cx, SOME cfx] @{thm QT_imp}
wenzelm@41444
   210
      in
wenzelm@41444
   211
        Conv.rewr_conv thm ctrm
wenzelm@41444
   212
      end
kaliszyk@35222
   213
kaliszyk@35222
   214
fun quot_true_conv ctxt fnctn ctrm =
wenzelm@41444
   215
  (case term_of ctrm of
kaliszyk@35222
   216
    (Const (@{const_name Quot_True}, _) $ _) =>
kaliszyk@35222
   217
      quot_true_simple_conv ctxt fnctn ctrm
kaliszyk@35222
   218
  | _ $ _ => Conv.comb_conv (quot_true_conv ctxt fnctn) ctrm
kaliszyk@35222
   219
  | Abs _ => Conv.abs_conv (fn (_, ctxt) => quot_true_conv ctxt fnctn) ctxt ctrm
wenzelm@41444
   220
  | _ => Conv.all_conv ctrm)
kaliszyk@35222
   221
kaliszyk@35222
   222
fun quot_true_tac ctxt fnctn =
wenzelm@41444
   223
  CONVERSION
kaliszyk@35222
   224
    ((Conv.params_conv ~1 (fn ctxt =>
wenzelm@41444
   225
        (Conv.prems_conv ~1 (quot_true_conv ctxt fnctn)))) ctxt)
kaliszyk@35222
   226
kaliszyk@35222
   227
fun dest_comb (f $ a) = (f, a)
kaliszyk@35222
   228
fun dest_bcomb ((_ $ l) $ r) = (l, r)
kaliszyk@35222
   229
kaliszyk@35222
   230
fun unlam t =
wenzelm@41444
   231
  (case t of
wenzelm@41444
   232
    Abs a => snd (Term.dest_abs a)
wenzelm@41444
   233
  | _ => unlam (Abs("", domain_type (fastype_of t), (incr_boundvars 1 t) $ (Bound 0))))
kaliszyk@35222
   234
kaliszyk@35222
   235
val bare_concl = HOLogic.dest_Trueprop o Logic.strip_assums_concl
kaliszyk@35222
   236
kaliszyk@35222
   237
(* We apply apply_rsp only in case if the type needs lifting.
kaliszyk@35222
   238
   This is the case if the type of the data in the Quot_True
kaliszyk@35222
   239
   assumption is different from the corresponding type in the goal.
kaliszyk@35222
   240
*)
kaliszyk@35222
   241
val apply_rsp_tac =
kaliszyk@35222
   242
  Subgoal.FOCUS (fn {concl, asms, context,...} =>
wenzelm@41444
   243
    let
wenzelm@41444
   244
      val bare_concl = HOLogic.dest_Trueprop (term_of concl)
wenzelm@41444
   245
      val qt_asm = find_qt_asm (map term_of asms)
wenzelm@41444
   246
    in
wenzelm@41444
   247
      case (bare_concl, qt_asm) of
wenzelm@41444
   248
        (R2 $ (f $ x) $ (g $ y), SOME (qt_fun, qt_arg)) =>
wenzelm@41444
   249
          if fastype_of qt_fun = fastype_of f
wenzelm@41444
   250
          then no_tac
wenzelm@41444
   251
          else
wenzelm@41444
   252
            let
wenzelm@41444
   253
              val ty_x = fastype_of x
wenzelm@41444
   254
              val ty_b = fastype_of qt_arg
wenzelm@41444
   255
              val ty_f = range_type (fastype_of f)
wenzelm@42361
   256
              val thy = Proof_Context.theory_of context
wenzelm@41444
   257
              val ty_inst = map (SOME o (ctyp_of thy)) [ty_x, ty_b, ty_f]
wenzelm@41444
   258
              val t_inst = map (SOME o (cterm_of thy)) [R2, f, g, x, y];
wenzelm@41444
   259
              val inst_thm = Drule.instantiate' ty_inst
wenzelm@41444
   260
                ([NONE, NONE, NONE] @ t_inst) @{thm apply_rsp}
wenzelm@41444
   261
            in
wenzelm@41444
   262
              (rtac inst_thm THEN' SOLVED' (quotient_tac context)) 1
wenzelm@41444
   263
            end
wenzelm@41444
   264
      | _ => no_tac
wenzelm@41444
   265
    end)
kaliszyk@35222
   266
kaliszyk@35222
   267
(* Instantiates and applies 'equals_rsp'. Since the theorem is
kaliszyk@35222
   268
   complex we rely on instantiation to tell us if it applies
kaliszyk@35222
   269
*)
kaliszyk@35222
   270
fun equals_rsp_tac R ctxt =
wenzelm@41444
   271
  let
wenzelm@42361
   272
    val thy = Proof_Context.theory_of ctxt
wenzelm@41444
   273
  in
wenzelm@41444
   274
    case try (cterm_of thy) R of (* There can be loose bounds in R *)
wenzelm@41444
   275
      SOME ctm =>
wenzelm@41444
   276
        let
wenzelm@41444
   277
          val ty = domain_type (fastype_of R)
wenzelm@41444
   278
        in
wenzelm@41444
   279
          case try (Drule.instantiate' [SOME (ctyp_of thy ty)]
wenzelm@41444
   280
              [SOME (cterm_of thy R)]) @{thm equals_rsp} of
wenzelm@41444
   281
            SOME thm => rtac thm THEN' quotient_tac ctxt
wenzelm@41444
   282
          | NONE => K no_tac
wenzelm@41444
   283
        end
wenzelm@41444
   284
    | _ => K no_tac
wenzelm@41444
   285
  end
kaliszyk@35222
   286
kaliszyk@35222
   287
fun rep_abs_rsp_tac ctxt =
kaliszyk@35222
   288
  SUBGOAL (fn (goal, i) =>
wenzelm@41444
   289
    (case try bare_concl goal of
kaliszyk@35843
   290
      SOME (rel $ _ $ (rep $ (Bound _ $ _))) => no_tac
kaliszyk@35843
   291
    | SOME (rel $ _ $ (rep $ (abs $ _))) =>
kaliszyk@35222
   292
        let
wenzelm@42361
   293
          val thy = Proof_Context.theory_of ctxt;
wenzelm@40840
   294
          val (ty_a, ty_b) = dest_funT (fastype_of abs);
kaliszyk@35222
   295
          val ty_inst = map (SOME o (ctyp_of thy)) [ty_a, ty_b];
kaliszyk@35222
   296
        in
kaliszyk@35222
   297
          case try (map (SOME o (cterm_of thy))) [rel, abs, rep] of
kaliszyk@35222
   298
            SOME t_inst =>
kaliszyk@35222
   299
              (case try (Drule.instantiate' ty_inst t_inst) @{thm rep_abs_rsp} of
kaliszyk@35222
   300
                SOME inst_thm => (rtac inst_thm THEN' quotient_tac ctxt) i
kaliszyk@35222
   301
              | NONE => no_tac)
kaliszyk@35222
   302
          | NONE => no_tac
kaliszyk@35222
   303
        end
wenzelm@41444
   304
    | _ => no_tac))
kaliszyk@35222
   305
kaliszyk@35222
   306
kaliszyk@35222
   307
urbanc@38718
   308
(* Injection means to prove that the regularized theorem implies
kaliszyk@35222
   309
   the abs/rep injected one.
kaliszyk@35222
   310
kaliszyk@35222
   311
   The deterministic part:
kaliszyk@35222
   312
    - remove lambdas from both sides
kaliszyk@35222
   313
    - prove Ball/Bex/Babs equalities using ball_rsp, bex_rsp, babs_rsp
urbanc@38317
   314
    - prove Ball/Bex relations using fun_relI
kaliszyk@35222
   315
    - reflexivity of equality
kaliszyk@35222
   316
    - prove equality of relations using equals_rsp
kaliszyk@35222
   317
    - use user-supplied RSP theorems
kaliszyk@35222
   318
    - solve 'relation of relations' goals using quot_rel_rsp
kaliszyk@35222
   319
    - remove rep_abs from the right side
kaliszyk@35222
   320
      (Lambdas under respects may have left us some assumptions)
kaliszyk@35222
   321
kaliszyk@35222
   322
   Then in order:
kaliszyk@35222
   323
    - split applications of lifted type (apply_rsp)
kaliszyk@35222
   324
    - split applications of non-lifted type (cong_tac)
kaliszyk@35222
   325
    - apply extentionality
kaliszyk@35222
   326
    - assumption
kaliszyk@35222
   327
    - reflexivity of the relation
kaliszyk@35222
   328
*)
kaliszyk@35222
   329
fun injection_match_tac ctxt = SUBGOAL (fn (goal, i) =>
wenzelm@41444
   330
  (case bare_concl goal of
wenzelm@41444
   331
      (* (R1 ===> R2) (%x...) (%x...) ----> [|R1 x y|] ==> R2 (...x) (...y) *)
wenzelm@41444
   332
    (Const (@{const_name fun_rel}, _) $ _ $ _) $ (Abs _) $ (Abs _)
wenzelm@41444
   333
        => rtac @{thm fun_relI} THEN' quot_true_tac ctxt unlam
kaliszyk@35222
   334
wenzelm@41444
   335
      (* (op =) (Ball...) (Ball...) ----> (op =) (...) (...) *)
wenzelm@41444
   336
  | (Const (@{const_name HOL.eq},_) $
wenzelm@41444
   337
      (Const(@{const_name Ball},_) $ (Const (@{const_name Respects}, _) $ _) $ _) $
wenzelm@41444
   338
      (Const(@{const_name Ball},_) $ (Const (@{const_name Respects}, _) $ _) $ _))
wenzelm@41444
   339
        => rtac @{thm ball_rsp} THEN' dtac @{thm QT_all}
kaliszyk@35222
   340
wenzelm@41444
   341
      (* (R1 ===> op =) (Ball...) (Ball...) ----> [|R1 x y|] ==> (Ball...x) = (Ball...y) *)
wenzelm@41444
   342
  | (Const (@{const_name fun_rel}, _) $ _ $ _) $
wenzelm@41444
   343
      (Const(@{const_name Ball},_) $ (Const (@{const_name Respects}, _) $ _) $ _) $
wenzelm@41444
   344
      (Const(@{const_name Ball},_) $ (Const (@{const_name Respects}, _) $ _) $ _)
wenzelm@41444
   345
        => rtac @{thm fun_relI} THEN' quot_true_tac ctxt unlam
kaliszyk@35222
   346
wenzelm@41444
   347
      (* (op =) (Bex...) (Bex...) ----> (op =) (...) (...) *)
wenzelm@41444
   348
  | Const (@{const_name HOL.eq},_) $
wenzelm@41444
   349
      (Const(@{const_name Bex},_) $ (Const (@{const_name Respects}, _) $ _) $ _) $
wenzelm@41444
   350
      (Const(@{const_name Bex},_) $ (Const (@{const_name Respects}, _) $ _) $ _)
wenzelm@41444
   351
        => rtac @{thm bex_rsp} THEN' dtac @{thm QT_ex}
kaliszyk@35222
   352
wenzelm@41444
   353
      (* (R1 ===> op =) (Bex...) (Bex...) ----> [|R1 x y|] ==> (Bex...x) = (Bex...y) *)
wenzelm@41444
   354
  | (Const (@{const_name fun_rel}, _) $ _ $ _) $
wenzelm@41444
   355
      (Const(@{const_name Bex},_) $ (Const (@{const_name Respects}, _) $ _) $ _) $
wenzelm@41444
   356
      (Const(@{const_name Bex},_) $ (Const (@{const_name Respects}, _) $ _) $ _)
wenzelm@41444
   357
        => rtac @{thm fun_relI} THEN' quot_true_tac ctxt unlam
kaliszyk@35222
   358
wenzelm@41444
   359
  | (Const (@{const_name fun_rel}, _) $ _ $ _) $
wenzelm@41444
   360
      (Const(@{const_name Bex1_rel},_) $ _) $ (Const(@{const_name Bex1_rel},_) $ _)
wenzelm@41444
   361
        => rtac @{thm bex1_rel_rsp} THEN' quotient_tac ctxt
kaliszyk@35222
   362
wenzelm@41444
   363
  | (_ $
wenzelm@41444
   364
      (Const(@{const_name Babs},_) $ (Const (@{const_name Respects}, _) $ _) $ _) $
wenzelm@41444
   365
      (Const(@{const_name Babs},_) $ (Const (@{const_name Respects}, _) $ _) $ _))
wenzelm@41444
   366
        => rtac @{thm babs_rsp} THEN' RANGE [quotient_tac ctxt]
kaliszyk@35222
   367
wenzelm@41444
   368
  | Const (@{const_name HOL.eq},_) $ (R $ _ $ _) $ (_ $ _ $ _) =>
wenzelm@41444
   369
     (rtac @{thm refl} ORELSE'
wenzelm@41444
   370
      (equals_rsp_tac R ctxt THEN' RANGE [
wenzelm@41444
   371
         quot_true_tac ctxt (fst o dest_bcomb), quot_true_tac ctxt (snd o dest_bcomb)]))
kaliszyk@35222
   372
wenzelm@41444
   373
      (* reflexivity of operators arising from Cong_tac *)
wenzelm@41444
   374
  | Const (@{const_name HOL.eq},_) $ _ $ _ => rtac @{thm refl}
kaliszyk@35222
   375
wenzelm@41444
   376
     (* respectfulness of constants; in particular of a simple relation *)
wenzelm@41444
   377
  | _ $ (Const _) $ (Const _)  (* fun_rel, list_rel, etc but not equality *)
wenzelm@41451
   378
      => resolve_tac (Quotient_Info.rsp_rules_get ctxt) THEN_ALL_NEW quotient_tac ctxt
kaliszyk@35222
   379
wenzelm@41444
   380
      (* R (...) (Rep (Abs ...)) ----> R (...) (...) *)
wenzelm@41444
   381
      (* observe map_fun *)
wenzelm@41444
   382
  | _ $ _ $ _
wenzelm@41444
   383
      => (rtac @{thm quot_rel_rsp} THEN_ALL_NEW quotient_tac ctxt)
wenzelm@41444
   384
         ORELSE' rep_abs_rsp_tac ctxt
kaliszyk@35222
   385
wenzelm@41444
   386
  | _ => K no_tac) i)
kaliszyk@35222
   387
kaliszyk@35222
   388
fun injection_step_tac ctxt rel_refl =
wenzelm@41444
   389
  FIRST' [
kaliszyk@35222
   390
    injection_match_tac ctxt,
kaliszyk@35222
   391
kaliszyk@35222
   392
    (* R (t $ ...) (t' $ ...) ----> apply_rsp   provided type of t needs lifting *)
kaliszyk@35222
   393
    apply_rsp_tac ctxt THEN'
kaliszyk@35222
   394
                 RANGE [quot_true_tac ctxt (fst o dest_comb), quot_true_tac ctxt (snd o dest_comb)],
kaliszyk@35222
   395
kaliszyk@35222
   396
    (* (op =) (t $ ...) (t' $ ...) ----> Cong   provided type of t does not need lifting *)
kaliszyk@35222
   397
    (* merge with previous tactic *)
kaliszyk@35222
   398
    Cong_Tac.cong_tac @{thm cong} THEN'
kaliszyk@35222
   399
                 RANGE [quot_true_tac ctxt (fst o dest_comb), quot_true_tac ctxt (snd o dest_comb)],
kaliszyk@35222
   400
kaliszyk@35222
   401
    (* (op =) (%x...) (%y...) ----> (op =) (...) (...) *)
kaliszyk@35222
   402
    rtac @{thm ext} THEN' quot_true_tac ctxt unlam,
kaliszyk@35222
   403
kaliszyk@35222
   404
    (* resolving with R x y assumptions *)
kaliszyk@35222
   405
    atac,
kaliszyk@35222
   406
kaliszyk@35222
   407
    (* reflexivity of the basic relations *)
kaliszyk@35222
   408
    (* R ... ... *)
kaliszyk@35222
   409
    resolve_tac rel_refl]
kaliszyk@35222
   410
kaliszyk@35222
   411
fun injection_tac ctxt =
wenzelm@41444
   412
  let
wenzelm@41444
   413
    val rel_refl = reflp_get ctxt
wenzelm@41444
   414
  in
wenzelm@41444
   415
    injection_step_tac ctxt rel_refl
wenzelm@41444
   416
  end
kaliszyk@35222
   417
kaliszyk@35222
   418
fun all_injection_tac ctxt =
kaliszyk@35222
   419
  REPEAT_ALL_NEW (injection_tac ctxt)
kaliszyk@35222
   420
kaliszyk@35222
   421
kaliszyk@35222
   422
kaliszyk@35222
   423
(*** Cleaning of the Theorem ***)
kaliszyk@35222
   424
haftmann@40602
   425
(* expands all map_funs, except in front of the (bound) variables listed in xs *)
haftmann@40602
   426
fun map_fun_simple_conv xs ctrm =
wenzelm@41444
   427
  (case term_of ctrm of
haftmann@40602
   428
    ((Const (@{const_name "map_fun"}, _) $ _ $ _) $ h $ _) =>
kaliszyk@35222
   429
        if member (op=) xs h
kaliszyk@35222
   430
        then Conv.all_conv ctrm
haftmann@40602
   431
        else Conv.rewr_conv @{thm map_fun_apply [THEN eq_reflection]} ctrm
wenzelm@41444
   432
  | _ => Conv.all_conv ctrm)
kaliszyk@35222
   433
haftmann@40602
   434
fun map_fun_conv xs ctxt ctrm =
wenzelm@41444
   435
  (case term_of ctrm of
wenzelm@41444
   436
    _ $ _ =>
wenzelm@41444
   437
      (Conv.comb_conv (map_fun_conv xs ctxt) then_conv
wenzelm@41444
   438
        map_fun_simple_conv xs) ctrm
wenzelm@41444
   439
  | Abs _ => Conv.abs_conv (fn (x, ctxt) => map_fun_conv ((term_of x)::xs) ctxt) ctxt ctrm
wenzelm@41444
   440
  | _ => Conv.all_conv ctrm)
kaliszyk@35222
   441
haftmann@40602
   442
fun map_fun_tac ctxt = CONVERSION (map_fun_conv [] ctxt)
kaliszyk@35222
   443
kaliszyk@35222
   444
(* custom matching functions *)
kaliszyk@35222
   445
fun mk_abs u i t =
wenzelm@41444
   446
  if incr_boundvars i u aconv t then Bound i
wenzelm@41444
   447
  else
wenzelm@41444
   448
    case t of
wenzelm@41444
   449
      t1 $ t2 => mk_abs u i t1 $ mk_abs u i t2
wenzelm@41444
   450
    | Abs (s, T, t') => Abs (s, T, mk_abs u (i + 1) t')
wenzelm@41444
   451
    | Bound j => if i = j then error "make_inst" else t
wenzelm@41444
   452
    | _ => t
kaliszyk@35222
   453
kaliszyk@35222
   454
fun make_inst lhs t =
wenzelm@41444
   455
  let
wenzelm@41444
   456
    val _ $ (Abs (_, _, (_ $ ((f as Var (_, Type ("fun", [T, _]))) $ u)))) = lhs;
wenzelm@41444
   457
    val _ $ (Abs (_, _, (_ $ g))) = t;
wenzelm@41444
   458
  in
wenzelm@41444
   459
    (f, Abs ("x", T, mk_abs u 0 g))
wenzelm@41444
   460
  end
kaliszyk@35222
   461
kaliszyk@35222
   462
fun make_inst_id lhs t =
wenzelm@41444
   463
  let
wenzelm@41444
   464
    val _ $ (Abs (_, _, (f as Var (_, Type ("fun", [T, _]))) $ u)) = lhs;
wenzelm@41444
   465
    val _ $ (Abs (_, _, g)) = t;
wenzelm@41444
   466
  in
wenzelm@41444
   467
    (f, Abs ("x", T, mk_abs u 0 g))
wenzelm@41444
   468
  end
kaliszyk@35222
   469
kaliszyk@35222
   470
(* Simplifies a redex using the 'lambda_prs' theorem.
kaliszyk@35222
   471
   First instantiates the types and known subterms.
kaliszyk@35222
   472
   Then solves the quotient assumptions to get Rep2 and Abs1
kaliszyk@35222
   473
   Finally instantiates the function f using make_inst
kaliszyk@35222
   474
   If Rep2 is an identity then the pattern is simpler and
kaliszyk@35222
   475
   make_inst_id is used
kaliszyk@35222
   476
*)
kaliszyk@35222
   477
fun lambda_prs_simple_conv ctxt ctrm =
wenzelm@41444
   478
  (case term_of ctrm of
haftmann@40602
   479
    (Const (@{const_name map_fun}, _) $ r1 $ a2) $ (Abs _) =>
kaliszyk@35222
   480
      let
wenzelm@42361
   481
        val thy = Proof_Context.theory_of ctxt
wenzelm@40840
   482
        val (ty_b, ty_a) = dest_funT (fastype_of r1)
wenzelm@40840
   483
        val (ty_c, ty_d) = dest_funT (fastype_of a2)
kaliszyk@35222
   484
        val tyinst = map (SOME o (ctyp_of thy)) [ty_a, ty_b, ty_c, ty_d]
kaliszyk@35222
   485
        val tinst = [NONE, NONE, SOME (cterm_of thy r1), NONE, SOME (cterm_of thy a2)]
kaliszyk@35222
   486
        val thm1 = Drule.instantiate' tyinst tinst @{thm lambda_prs[THEN eq_reflection]}
kaliszyk@35222
   487
        val thm2 = solve_quotient_assm ctxt (solve_quotient_assm ctxt thm1)
wenzelm@41228
   488
        val thm3 = Raw_Simplifier.rewrite_rule @{thms id_apply[THEN eq_reflection]} thm2
kaliszyk@35222
   489
        val (insp, inst) =
kaliszyk@35222
   490
          if ty_c = ty_d
kaliszyk@35222
   491
          then make_inst_id (term_of (Thm.lhs_of thm3)) (term_of ctrm)
kaliszyk@35222
   492
          else make_inst (term_of (Thm.lhs_of thm3)) (term_of ctrm)
wenzelm@43333
   493
        val thm4 = Drule.instantiate_normalize ([], [(cterm_of thy insp, cterm_of thy inst)]) thm3
kaliszyk@35222
   494
      in
kaliszyk@35222
   495
        Conv.rewr_conv thm4 ctrm
kaliszyk@35222
   496
      end
wenzelm@41444
   497
  | _ => Conv.all_conv ctrm)
kaliszyk@35222
   498
wenzelm@36936
   499
fun lambda_prs_conv ctxt = Conv.top_conv lambda_prs_simple_conv ctxt
kaliszyk@35222
   500
fun lambda_prs_tac ctxt = CONVERSION (lambda_prs_conv ctxt)
kaliszyk@35222
   501
kaliszyk@35222
   502
kaliszyk@35222
   503
(* Cleaning consists of:
kaliszyk@35222
   504
kaliszyk@35222
   505
  1. unfolding of ---> in front of everything, except
kaliszyk@35222
   506
     bound variables (this prevents lambda_prs from
kaliszyk@35222
   507
     becoming stuck)
kaliszyk@35222
   508
kaliszyk@35222
   509
  2. simplification with lambda_prs
kaliszyk@35222
   510
kaliszyk@35222
   511
  3. simplification with:
kaliszyk@35222
   512
kaliszyk@35222
   513
      - Quotient_abs_rep Quotient_rel_rep
kaliszyk@35222
   514
        babs_prs all_prs ex_prs ex1_prs
kaliszyk@35222
   515
kaliszyk@35222
   516
      - id_simps and preservation lemmas and
kaliszyk@35222
   517
kaliszyk@35222
   518
      - symmetric versions of the definitions
kaliszyk@35222
   519
        (that is definitions of quotient constants
kaliszyk@35222
   520
         are folded)
kaliszyk@35222
   521
kaliszyk@35222
   522
  4. test for refl
kaliszyk@35222
   523
*)
kaliszyk@35222
   524
fun clean_tac lthy =
wenzelm@41444
   525
  let
wenzelm@41451
   526
    val defs = map (Thm.symmetric o #def) (Quotient_Info.qconsts_dest lthy)
wenzelm@41451
   527
    val prs = Quotient_Info.prs_rules_get lthy
wenzelm@41451
   528
    val ids = Quotient_Info.id_simps_get lthy
wenzelm@41444
   529
    val thms =
wenzelm@41444
   530
      @{thms Quotient_abs_rep Quotient_rel_rep babs_prs all_prs ex_prs ex1_prs} @ ids @ prs @ defs
kaliszyk@35222
   531
wenzelm@41444
   532
    val ss = (mk_minimal_ss lthy) addsimps thms addSolver quotient_solver
wenzelm@41444
   533
  in
wenzelm@41451
   534
    EVERY' [
wenzelm@41451
   535
      map_fun_tac lthy,
wenzelm@41451
   536
      lambda_prs_tac lthy,
wenzelm@41451
   537
      simp_tac ss,
wenzelm@41451
   538
      TRY o rtac refl]
wenzelm@41444
   539
  end
kaliszyk@35222
   540
kaliszyk@35222
   541
urbanc@38718
   542
(* Tactic for Generalising Free Variables in a Goal *)
kaliszyk@35222
   543
kaliszyk@35222
   544
fun inst_spec ctrm =
wenzelm@41444
   545
  Drule.instantiate' [SOME (ctyp_of_term ctrm)] [NONE, SOME ctrm] @{thm spec}
kaliszyk@35222
   546
kaliszyk@35222
   547
fun inst_spec_tac ctrms =
kaliszyk@35222
   548
  EVERY' (map (dtac o inst_spec) ctrms)
kaliszyk@35222
   549
kaliszyk@35222
   550
fun all_list xs trm =
kaliszyk@35222
   551
  fold (fn (x, T) => fn t' => HOLogic.mk_all (x, T, t')) xs trm
kaliszyk@35222
   552
kaliszyk@35222
   553
fun apply_under_Trueprop f =
kaliszyk@35222
   554
  HOLogic.dest_Trueprop #> f #> HOLogic.mk_Trueprop
kaliszyk@35222
   555
kaliszyk@35222
   556
fun gen_frees_tac ctxt =
kaliszyk@35222
   557
  SUBGOAL (fn (concl, i) =>
kaliszyk@35222
   558
    let
wenzelm@42361
   559
      val thy = Proof_Context.theory_of ctxt
kaliszyk@35222
   560
      val vrs = Term.add_frees concl []
kaliszyk@35222
   561
      val cvrs = map (cterm_of thy o Free) vrs
kaliszyk@35222
   562
      val concl' = apply_under_Trueprop (all_list vrs) concl
kaliszyk@35222
   563
      val goal = Logic.mk_implies (concl', concl)
kaliszyk@35222
   564
      val rule = Goal.prove ctxt [] [] goal
kaliszyk@35222
   565
        (K (EVERY1 [inst_spec_tac (rev cvrs), atac]))
kaliszyk@35222
   566
    in
kaliszyk@35222
   567
      rtac rule i
kaliszyk@35222
   568
    end)
kaliszyk@35222
   569
kaliszyk@35222
   570
kaliszyk@35222
   571
(** The General Shape of the Lifting Procedure **)
kaliszyk@35222
   572
kaliszyk@35222
   573
(* - A is the original raw theorem
kaliszyk@35222
   574
   - B is the regularized theorem
kaliszyk@35222
   575
   - C is the rep/abs injected version of B
kaliszyk@35222
   576
   - D is the lifted theorem
kaliszyk@35222
   577
kaliszyk@35222
   578
   - 1st prem is the regularization step
kaliszyk@35222
   579
   - 2nd prem is the rep/abs injection step
kaliszyk@35222
   580
   - 3rd prem is the cleaning part
kaliszyk@35222
   581
kaliszyk@35222
   582
   the Quot_True premise in 2nd records the lifted theorem
kaliszyk@35222
   583
*)
kaliszyk@35222
   584
val lifting_procedure_thm =
kaliszyk@35222
   585
  @{lemma  "[|A;
kaliszyk@35222
   586
              A --> B;
kaliszyk@35222
   587
              Quot_True D ==> B = C;
kaliszyk@35222
   588
              C = D|] ==> D"
kaliszyk@35222
   589
      by (simp add: Quot_True_def)}
kaliszyk@35222
   590
kaliszyk@35222
   591
fun lift_match_error ctxt msg rtrm qtrm =
wenzelm@41444
   592
  let
wenzelm@41444
   593
    val rtrm_str = Syntax.string_of_term ctxt rtrm
wenzelm@41444
   594
    val qtrm_str = Syntax.string_of_term ctxt qtrm
wenzelm@41444
   595
    val msg = cat_lines [enclose "[" "]" msg, "The quotient theorem", qtrm_str,
wenzelm@41444
   596
      "", "does not match with original theorem", rtrm_str]
wenzelm@41444
   597
  in
wenzelm@41444
   598
    error msg
wenzelm@41444
   599
  end
kaliszyk@35222
   600
kaliszyk@35222
   601
fun procedure_inst ctxt rtrm qtrm =
wenzelm@41444
   602
  let
wenzelm@42361
   603
    val thy = Proof_Context.theory_of ctxt
wenzelm@41444
   604
    val rtrm' = HOLogic.dest_Trueprop rtrm
wenzelm@41444
   605
    val qtrm' = HOLogic.dest_Trueprop qtrm
wenzelm@41451
   606
    val reg_goal = Quotient_Term.regularize_trm_chk ctxt (rtrm', qtrm')
wenzelm@41451
   607
      handle Quotient_Term.LIFT_MATCH msg => lift_match_error ctxt msg rtrm qtrm
wenzelm@41451
   608
    val inj_goal = Quotient_Term.inj_repabs_trm_chk ctxt (reg_goal, qtrm')
wenzelm@41451
   609
      handle Quotient_Term.LIFT_MATCH msg => lift_match_error ctxt msg rtrm qtrm
wenzelm@41444
   610
  in
wenzelm@41444
   611
    Drule.instantiate' []
wenzelm@41444
   612
      [SOME (cterm_of thy rtrm'),
wenzelm@41444
   613
       SOME (cterm_of thy reg_goal),
wenzelm@41444
   614
       NONE,
wenzelm@41444
   615
       SOME (cterm_of thy inj_goal)] lifting_procedure_thm
wenzelm@41444
   616
  end
kaliszyk@35222
   617
urbanc@37593
   618
urbanc@38860
   619
(* Since we use Ball and Bex during the lifting and descending,
kaliszyk@38862
   620
   we cannot deal with lemmas containing them, unless we unfold
kaliszyk@38862
   621
   them by default. *)
urbanc@38860
   622
kaliszyk@38862
   623
val default_unfolds = @{thms Ball_def Bex_def}
urbanc@38860
   624
urbanc@38860
   625
urbanc@37593
   626
(** descending as tactic **)
urbanc@37593
   627
urbanc@38859
   628
fun descend_procedure_tac ctxt simps =
wenzelm@41444
   629
  let
wenzelm@41444
   630
    val ss = (mk_minimal_ss ctxt) addsimps (simps @ default_unfolds)
wenzelm@41444
   631
  in
wenzelm@41444
   632
    full_simp_tac ss
wenzelm@41444
   633
    THEN' Object_Logic.full_atomize_tac
wenzelm@41444
   634
    THEN' gen_frees_tac ctxt
wenzelm@41444
   635
    THEN' SUBGOAL (fn (goal, i) =>
wenzelm@41444
   636
      let
wenzelm@41444
   637
        val qtys = map #qtyp (Quotient_Info.quotdata_dest ctxt)
wenzelm@41451
   638
        val rtrm = Quotient_Term.derive_rtrm ctxt qtys goal
wenzelm@41444
   639
        val rule = procedure_inst ctxt rtrm  goal
wenzelm@41444
   640
      in
wenzelm@41444
   641
        rtac rule i
wenzelm@41444
   642
      end)
wenzelm@41444
   643
  end
urbanc@37593
   644
urbanc@38859
   645
fun descend_tac ctxt simps =
wenzelm@41444
   646
  let
wenzelm@41444
   647
    val mk_tac_raw =
wenzelm@41444
   648
      descend_procedure_tac ctxt simps
wenzelm@41444
   649
      THEN' RANGE
wenzelm@41444
   650
        [Object_Logic.rulify_tac THEN' (K all_tac),
wenzelm@41444
   651
         regularize_tac ctxt,
wenzelm@41444
   652
         all_injection_tac ctxt,
wenzelm@41444
   653
         clean_tac ctxt]
wenzelm@41444
   654
  in
wenzelm@41444
   655
    Goal.conjunction_tac THEN_ALL_NEW mk_tac_raw
wenzelm@41444
   656
  end
kaliszyk@35222
   657
kaliszyk@35222
   658
urbanc@38625
   659
(** lifting as a tactic **)
urbanc@37593
   660
urbanc@38718
   661
urbanc@37593
   662
(* the tactic leaves three subgoals to be proved *)
urbanc@38859
   663
fun lift_procedure_tac ctxt simps rthm =
wenzelm@41444
   664
  let
wenzelm@41444
   665
    val ss = (mk_minimal_ss ctxt) addsimps (simps @ default_unfolds)
wenzelm@41444
   666
  in
wenzelm@41444
   667
    full_simp_tac ss
wenzelm@41444
   668
    THEN' Object_Logic.full_atomize_tac
wenzelm@41444
   669
    THEN' gen_frees_tac ctxt
wenzelm@41444
   670
    THEN' SUBGOAL (fn (goal, i) =>
wenzelm@41444
   671
      let
wenzelm@41444
   672
        (* full_atomize_tac contracts eta redexes,
wenzelm@41444
   673
           so we do it also in the original theorem *)
wenzelm@41444
   674
        val rthm' =
wenzelm@41444
   675
          rthm |> full_simplify ss
wenzelm@41444
   676
               |> Drule.eta_contraction_rule
wenzelm@41444
   677
               |> Thm.forall_intr_frees
wenzelm@41444
   678
               |> atomize_thm
urbanc@38717
   679
wenzelm@41444
   680
        val rule = procedure_inst ctxt (prop_of rthm') goal
wenzelm@41444
   681
      in
wenzelm@41444
   682
        (rtac rule THEN' rtac rthm') i
wenzelm@41444
   683
      end)
wenzelm@41444
   684
  end
urbanc@38625
   685
wenzelm@41444
   686
fun lift_single_tac ctxt simps rthm =
urbanc@38859
   687
  lift_procedure_tac ctxt simps rthm
urbanc@38625
   688
  THEN' RANGE
urbanc@38625
   689
    [ regularize_tac ctxt,
urbanc@38625
   690
      all_injection_tac ctxt,
urbanc@38625
   691
      clean_tac ctxt ]
urbanc@38625
   692
urbanc@38859
   693
fun lift_tac ctxt simps rthms =
wenzelm@41444
   694
  Goal.conjunction_tac
urbanc@38859
   695
  THEN' RANGE (map (lift_single_tac ctxt simps) rthms)
urbanc@38625
   696
urbanc@38625
   697
urbanc@38625
   698
(* automated lifting with pre-simplification of the theorems;
urbanc@38625
   699
   for internal usage *)
urbanc@38625
   700
fun lifted ctxt qtys simps rthm =
wenzelm@41444
   701
  let
wenzelm@41444
   702
    val ((_, [rthm']), ctxt') = Variable.import true [rthm] ctxt
wenzelm@41451
   703
    val goal = Quotient_Term.derive_qtrm ctxt' qtys (prop_of rthm')
wenzelm@41444
   704
  in
wenzelm@41444
   705
    Goal.prove ctxt' [] [] goal
wenzelm@41444
   706
      (K (HEADGOAL (lift_single_tac ctxt' simps rthm')))
wenzelm@42361
   707
    |> singleton (Proof_Context.export ctxt' ctxt)
wenzelm@41444
   708
  end
kaliszyk@35222
   709
urbanc@37593
   710
urbanc@38625
   711
(* lifting as an attribute *)
kaliszyk@35222
   712
wenzelm@41444
   713
val lifted_attrib = Thm.rule_attribute (fn context =>
urbanc@37593
   714
  let
urbanc@37593
   715
    val ctxt = Context.proof_of context
urbanc@37593
   716
    val qtys = map #qtyp (Quotient_Info.quotdata_dest ctxt)
urbanc@37593
   717
  in
urbanc@38625
   718
    lifted ctxt qtys []
urbanc@37593
   719
  end)
kaliszyk@35222
   720
kaliszyk@35222
   721
end; (* structure *)