src/HOL/Tools/Lifting/lifting_def.ML
author wenzelm
Tue Feb 10 14:48:26 2015 +0100 (2015-02-10)
changeset 59498 50b60f501b05
parent 59458 9de8ac92cafa
child 59580 cbc38731d42f
permissions -rw-r--r--
proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
occasionally clarified use of context;
kuncar@47308
     1
(*  Title:      HOL/Tools/Lifting/lifting_def.ML
kuncar@47308
     2
    Author:     Ondrej Kuncar
kuncar@47308
     3
kuncar@47308
     4
Definitions for constants on quotient types.
kuncar@47308
     5
*)
kuncar@47308
     6
kuncar@47308
     7
signature LIFTING_DEF =
kuncar@47308
     8
sig
kuncar@51374
     9
  val generate_parametric_transfer_rule:
kuncar@51374
    10
    Proof.context -> thm -> thm -> thm
kuncar@51374
    11
kuncar@47308
    12
  val add_lift_def:
kuncar@53951
    13
    (binding * mixfix) -> typ -> term -> thm -> thm list -> local_theory -> local_theory
kuncar@47308
    14
kuncar@47308
    15
  val lift_def_cmd:
wenzelm@58011
    16
    (binding * string option * mixfix) * string * (Facts.ref * Token.src list) list ->
wenzelm@58011
    17
    local_theory -> Proof.state
kuncar@47308
    18
kuncar@47308
    19
  val can_generate_code_cert: thm -> bool
kuncar@53651
    20
end
kuncar@47308
    21
kuncar@47308
    22
structure Lifting_Def: LIFTING_DEF =
kuncar@47308
    23
struct
kuncar@47308
    24
kuncar@47698
    25
open Lifting_Util
kuncar@47308
    26
kuncar@47308
    27
infix 0 MRSL
kuncar@47308
    28
kuncar@51994
    29
(* Reflexivity prover *)
kuncar@51994
    30
kuncar@55609
    31
fun mono_eq_prover ctxt prop =
kuncar@51994
    32
  let
kuncar@56518
    33
    val refl_rules = Lifting_Info.get_reflexivity_rules ctxt
kuncar@56518
    34
    val transfer_rules = Transfer.get_transfer_raw ctxt
kuncar@56518
    35
    
wenzelm@59498
    36
    fun main_tac i = (REPEAT_ALL_NEW (DETERM o resolve_tac ctxt refl_rules) THEN_ALL_NEW 
wenzelm@59498
    37
      (REPEAT_ALL_NEW (DETERM o resolve_tac ctxt transfer_rules))) i
kuncar@51994
    38
  in
kuncar@56540
    39
    SOME (Goal.prove ctxt [] [] prop (K (main_tac 1)))
kuncar@55609
    40
      handle ERROR _ => NONE
kuncar@51994
    41
  end
kuncar@56518
    42
kuncar@57642
    43
fun try_prove_refl_rel ctxt rel =
kuncar@57642
    44
  let
kuncar@57642
    45
    fun mk_ge_eq x =
kuncar@57642
    46
      let
kuncar@57642
    47
        val T = fastype_of x
kuncar@57642
    48
      in
kuncar@57642
    49
        Const (@{const_name "less_eq"}, T --> T --> HOLogic.boolT) $ 
kuncar@57642
    50
          (Const (@{const_name HOL.eq}, T)) $ x
kuncar@57642
    51
      end;
kuncar@57642
    52
    val goal = HOLogic.mk_Trueprop (mk_ge_eq rel);
kuncar@57642
    53
  in
kuncar@57642
    54
     mono_eq_prover ctxt goal
kuncar@57642
    55
  end;
kuncar@57642
    56
kuncar@51994
    57
fun try_prove_reflexivity ctxt prop =
kuncar@55609
    58
  let
kuncar@55609
    59
    val thy = Proof_Context.theory_of ctxt
kuncar@55609
    60
    val cprop = cterm_of thy prop
kuncar@55609
    61
    val rule = @{thm ge_eq_refl}
kuncar@55609
    62
    val concl_pat = Drule.strip_imp_concl (cprop_of rule)
kuncar@55609
    63
    val insts = Thm.first_order_match (concl_pat, cprop)
kuncar@55609
    64
    val rule = Drule.instantiate_normalize insts rule
kuncar@55609
    65
    val prop = hd (prems_of rule)
kuncar@55609
    66
  in
kuncar@55609
    67
    case mono_eq_prover ctxt prop of
kuncar@55610
    68
      SOME thm => SOME (thm RS rule)
kuncar@55609
    69
      | NONE => NONE
kuncar@55609
    70
  end
kuncar@51994
    71
kuncar@54335
    72
(* 
kuncar@54335
    73
  Generates a parametrized transfer rule.
kuncar@54335
    74
  transfer_rule - of the form T t f
kuncar@54335
    75
  parametric_transfer_rule - of the form par_R t' t
kuncar@54335
    76
  
kuncar@54947
    77
  Result: par_T t' f, after substituing op= for relations in par_R that relate
kuncar@54335
    78
    a type constructor to the same type constructor, it is a merge of (par_R' OO T) t' f
kuncar@54335
    79
    using Lifting_Term.merge_transfer_relations
kuncar@54335
    80
*)
kuncar@51374
    81
kuncar@51374
    82
fun generate_parametric_transfer_rule ctxt transfer_rule parametric_transfer_rule =
kuncar@51374
    83
  let
kuncar@51374
    84
    fun preprocess ctxt thm =
kuncar@51374
    85
      let
kuncar@51374
    86
        val tm = (strip_args 2 o HOLogic.dest_Trueprop o concl_of) thm;
kuncar@51374
    87
        val param_rel = (snd o dest_comb o fst o dest_comb) tm;
kuncar@51374
    88
        val thy = Proof_Context.theory_of ctxt;
kuncar@51374
    89
        val free_vars = Term.add_vars param_rel [];
kuncar@51374
    90
        
kuncar@51374
    91
        fun make_subst (var as (_, typ)) subst = 
kuncar@51374
    92
          let
kuncar@51374
    93
            val [rty, rty'] = binder_types typ
kuncar@51374
    94
          in
kuncar@51374
    95
            if (Term.is_TVar rty andalso is_Type rty') then
kuncar@51374
    96
              (Var var, HOLogic.eq_const rty')::subst
kuncar@51374
    97
            else
kuncar@51374
    98
              subst
kuncar@51374
    99
          end;
kuncar@51374
   100
        
kuncar@51374
   101
        val subst = fold make_subst free_vars [];
wenzelm@59058
   102
        val csubst = map (apply2 (cterm_of thy)) subst;
kuncar@51374
   103
        val inst_thm = Drule.cterm_instantiate csubst thm;
kuncar@51374
   104
      in
kuncar@51374
   105
        Conv.fconv_rule 
kuncar@51374
   106
          ((Conv.concl_conv (nprems_of inst_thm) o HOLogic.Trueprop_conv o Conv.fun2_conv o Conv.arg1_conv)
wenzelm@54742
   107
            (Raw_Simplifier.rewrite ctxt false (Transfer.get_sym_relator_eq ctxt))) inst_thm
kuncar@51374
   108
      end
kuncar@51374
   109
kuncar@51374
   110
    fun inst_relcomppI thy ant1 ant2 =
kuncar@51374
   111
      let
kuncar@51374
   112
        val t1 = (HOLogic.dest_Trueprop o concl_of) ant1
kuncar@51374
   113
        val t2 = (HOLogic.dest_Trueprop o prop_of) ant2
kuncar@51374
   114
        val fun1 = cterm_of thy (strip_args 2 t1)
kuncar@51374
   115
        val args1 = map (cterm_of thy) (get_args 2 t1)
kuncar@51374
   116
        val fun2 = cterm_of thy (strip_args 2 t2)
kuncar@51374
   117
        val args2 = map (cterm_of thy) (get_args 1 t2)
kuncar@51374
   118
        val relcomppI = Drule.incr_indexes2 ant1 ant2 @{thm relcomppI}
kuncar@51374
   119
        val vars = (rev (Term.add_vars (prop_of relcomppI) []))
kuncar@51374
   120
        val subst = map (apfst ((cterm_of thy) o Var)) (vars ~~ ([fun1] @ args1 @ [fun2] @ args2))
kuncar@51374
   121
      in
kuncar@51374
   122
        Drule.cterm_instantiate subst relcomppI
kuncar@51374
   123
      end
kuncar@51374
   124
wenzelm@54995
   125
    fun zip_transfer_rules ctxt thm =
wenzelm@54995
   126
      let
kuncar@51374
   127
        val thy = Proof_Context.theory_of ctxt
kuncar@51374
   128
        fun mk_POS ty = Const (@{const_name POS}, ty --> ty --> HOLogic.boolT)
kuncar@51374
   129
        val rel = (Thm.dest_fun2 o Thm.dest_arg o cprop_of) thm
kuncar@51374
   130
        val typ = (typ_of o ctyp_of_term) rel
kuncar@51374
   131
        val POS_const = cterm_of thy (mk_POS typ)
kuncar@51374
   132
        val var = cterm_of thy (Var (("X", #maxidx (rep_cterm (rel)) + 1), typ))
kuncar@51374
   133
        val goal = Thm.apply (cterm_of thy HOLogic.Trueprop) (Thm.apply (Thm.apply POS_const rel) var)
kuncar@51374
   134
      in
kuncar@51374
   135
        [Lifting_Term.merge_transfer_relations ctxt goal, thm] MRSL @{thm POS_apply}
kuncar@51374
   136
      end
kuncar@51374
   137
     
kuncar@51374
   138
    val thm = (inst_relcomppI (Proof_Context.theory_of ctxt) parametric_transfer_rule transfer_rule) 
kuncar@51374
   139
                OF [parametric_transfer_rule, transfer_rule]
kuncar@51374
   140
    val preprocessed_thm = preprocess ctxt thm
kuncar@51374
   141
    val orig_ctxt = ctxt
kuncar@51374
   142
    val (fixed_thm, ctxt) = yield_singleton (apfst snd oo Variable.import true) preprocessed_thm ctxt
kuncar@51374
   143
    val assms = cprems_of fixed_thm
kuncar@51374
   144
    val add_transfer_rule = Thm.attribute_declaration Transfer.transfer_add
wenzelm@54995
   145
    val (prems, ctxt) = fold_map Thm.assume_hyps assms ctxt
wenzelm@54995
   146
    val ctxt = Context.proof_map (fold add_transfer_rule prems) ctxt
kuncar@51374
   147
    val zipped_thm =
kuncar@51374
   148
      fixed_thm
kuncar@51374
   149
      |> undisch_all
kuncar@51374
   150
      |> zip_transfer_rules ctxt
kuncar@51374
   151
      |> implies_intr_list assms
kuncar@51374
   152
      |> singleton (Variable.export ctxt orig_ctxt)
kuncar@51374
   153
  in
kuncar@51374
   154
    zipped_thm
kuncar@51374
   155
  end
kuncar@51374
   156
kuncar@51374
   157
fun print_generate_transfer_info msg = 
kuncar@51374
   158
  let
kuncar@51374
   159
    val error_msg = cat_lines 
kuncar@51374
   160
      ["Generation of a parametric transfer rule failed.",
kuncar@51374
   161
      (Pretty.string_of (Pretty.block
kuncar@51374
   162
         [Pretty.str "Reason:", Pretty.brk 2, msg]))]
kuncar@51374
   163
  in
kuncar@51374
   164
    error error_msg
kuncar@51374
   165
  end
kuncar@51374
   166
kuncar@53951
   167
fun map_ter _ x [] = x
kuncar@53951
   168
    | map_ter f _ xs = map f xs
kuncar@53951
   169
kuncar@53951
   170
fun generate_transfer_rules lthy quot_thm rsp_thm def_thm par_thms =
kuncar@51374
   171
  let
kuncar@51374
   172
    val transfer_rule =
kuncar@51374
   173
      ([quot_thm, rsp_thm, def_thm] MRSL @{thm Quotient_to_transfer})
kuncar@51374
   174
      |> Lifting_Term.parametrize_transfer_rule lthy
kuncar@51374
   175
  in
kuncar@53951
   176
    (map_ter (generate_parametric_transfer_rule lthy transfer_rule) [transfer_rule] par_thms
kuncar@53951
   177
    handle Lifting_Term.MERGE_TRANSFER_REL msg => (print_generate_transfer_info msg; [transfer_rule]))
kuncar@51374
   178
  end
kuncar@51374
   179
kuncar@47698
   180
(* Generation of the code certificate from the rsp theorem *)
kuncar@47308
   181
kuncar@47308
   182
fun get_body_types (Type ("fun", [_, U]), Type ("fun", [_, V])) = get_body_types (U, V)
kuncar@47308
   183
  | get_body_types (U, V)  = (U, V)
kuncar@47308
   184
kuncar@47308
   185
fun get_binder_types (Type ("fun", [T, U]), Type ("fun", [V, W])) = (T, V) :: get_binder_types (U, W)
kuncar@47308
   186
  | get_binder_types _ = []
kuncar@47308
   187
blanchet@55945
   188
fun get_binder_types_by_rel (Const (@{const_name "rel_fun"}, _) $ _ $ S) (Type ("fun", [T, U]), Type ("fun", [V, W])) = 
kuncar@47937
   189
    (T, V) :: get_binder_types_by_rel S (U, W)
kuncar@47937
   190
  | get_binder_types_by_rel _ _ = []
kuncar@47937
   191
blanchet@55945
   192
fun get_body_type_by_rel (Const (@{const_name "rel_fun"}, _) $ _ $ S) (Type ("fun", [_, U]), Type ("fun", [_, V])) = 
kuncar@47937
   193
    get_body_type_by_rel S (U, V)
kuncar@47937
   194
  | get_body_type_by_rel _ (U, V)  = (U, V)
kuncar@47937
   195
kuncar@57642
   196
fun get_binder_rels (Const (@{const_name "rel_fun"}, _) $ R $ S) = R :: get_binder_rels S
kuncar@57642
   197
  | get_binder_rels _ = []
kuncar@57642
   198
kuncar@47308
   199
fun force_rty_type ctxt rty rhs = 
kuncar@47308
   200
  let
kuncar@47308
   201
    val thy = Proof_Context.theory_of ctxt
kuncar@47308
   202
    val rhs_schematic = singleton (Variable.polymorphic ctxt) rhs
kuncar@47308
   203
    val rty_schematic = fastype_of rhs_schematic
kuncar@47308
   204
    val match = Sign.typ_match thy (rty_schematic, rty) Vartab.empty
kuncar@47308
   205
  in
kuncar@47308
   206
    Envir.subst_term_types match rhs_schematic
kuncar@47308
   207
  end
kuncar@47308
   208
kuncar@47308
   209
fun unabs_def ctxt def = 
kuncar@47308
   210
  let
kuncar@47308
   211
    val (_, rhs) = Thm.dest_equals (cprop_of def)
kuncar@47308
   212
    fun dest_abs (Abs (var_name, T, _)) = (var_name, T)
kuncar@47308
   213
      | dest_abs tm = raise TERM("get_abs_var",[tm])
kuncar@47308
   214
    val (var_name, T) = dest_abs (term_of rhs)
kuncar@47308
   215
    val (new_var_names, ctxt') = Variable.variant_fixes [var_name] ctxt
kuncar@47308
   216
    val thy = Proof_Context.theory_of ctxt'
kuncar@47308
   217
    val refl_thm = Thm.reflexive (cterm_of thy (Free (hd new_var_names, T)))
kuncar@47308
   218
  in
kuncar@47308
   219
    Thm.combination def refl_thm |>
kuncar@47308
   220
    singleton (Proof_Context.export ctxt' ctxt)
kuncar@47308
   221
  end
kuncar@47308
   222
kuncar@47308
   223
fun unabs_all_def ctxt def = 
kuncar@47308
   224
  let
kuncar@47308
   225
    val (_, rhs) = Thm.dest_equals (cprop_of def)
kuncar@47308
   226
    val xs = strip_abs_vars (term_of rhs)
kuncar@47308
   227
  in  
kuncar@47308
   228
    fold (K (unabs_def ctxt)) xs def
kuncar@47308
   229
  end
kuncar@47308
   230
kuncar@47308
   231
val map_fun_unfolded = 
kuncar@47308
   232
  @{thm map_fun_def[abs_def]} |>
kuncar@47308
   233
  unabs_def @{context} |>
kuncar@47308
   234
  unabs_def @{context} |>
kuncar@47308
   235
  Local_Defs.unfold @{context} [@{thm comp_def}]
kuncar@47308
   236
kuncar@47308
   237
fun unfold_fun_maps ctm =
kuncar@47308
   238
  let
kuncar@47308
   239
    fun unfold_conv ctm =
kuncar@47308
   240
      case (Thm.term_of ctm) of
kuncar@47308
   241
        Const (@{const_name "map_fun"}, _) $ _ $ _ => 
kuncar@47308
   242
          (Conv.arg_conv unfold_conv then_conv Conv.rewr_conv map_fun_unfolded) ctm
kuncar@47308
   243
        | _ => Conv.all_conv ctm
kuncar@47308
   244
  in
kuncar@47937
   245
    (Conv.fun_conv unfold_conv) ctm
kuncar@47937
   246
  end
kuncar@47937
   247
kuncar@47937
   248
fun unfold_fun_maps_beta ctm =
kuncar@47937
   249
  let val try_beta_conv = Conv.try_conv (Thm.beta_conversion false)
kuncar@47937
   250
  in 
kuncar@47937
   251
    (unfold_fun_maps then_conv try_beta_conv) ctm 
kuncar@47308
   252
  end
kuncar@47308
   253
kuncar@47308
   254
fun prove_rel ctxt rsp_thm (rty, qty) =
kuncar@47308
   255
  let
kuncar@47308
   256
    val ty_args = get_binder_types (rty, qty)
kuncar@47308
   257
    fun disch_arg args_ty thm = 
kuncar@47308
   258
      let
kuncar@47504
   259
        val quot_thm = Lifting_Term.prove_quot_thm ctxt args_ty
kuncar@47308
   260
      in
kuncar@47308
   261
        [quot_thm, thm] MRSL @{thm apply_rsp''}
kuncar@47308
   262
      end
kuncar@47308
   263
  in
kuncar@47308
   264
    fold disch_arg ty_args rsp_thm
kuncar@47308
   265
  end
kuncar@47308
   266
kuncar@47308
   267
exception CODE_CERT_GEN of string
kuncar@47308
   268
kuncar@47308
   269
fun simplify_code_eq ctxt def_thm = 
kuncar@48024
   270
  Local_Defs.unfold ctxt [@{thm o_apply}, @{thm map_fun_def}, @{thm id_apply}] def_thm
kuncar@47308
   271
kuncar@47852
   272
(*
kuncar@47852
   273
  quot_thm - quotient theorem (Quotient R Abs Rep T).
kuncar@47852
   274
  returns: whether the Lifting package is capable to generate code for the abstract type
kuncar@47852
   275
    represented by quot_thm
kuncar@47852
   276
*)
kuncar@47852
   277
kuncar@47308
   278
fun can_generate_code_cert quot_thm  =
kuncar@47951
   279
  case quot_thm_rel quot_thm of
kuncar@47308
   280
    Const (@{const_name HOL.eq}, _) => true
kuncar@56519
   281
    | Const (@{const_name eq_onp}, _) $ _  => true
kuncar@47308
   282
    | _ => false
kuncar@47308
   283
kuncar@55610
   284
fun generate_rep_eq ctxt def_thm rsp_thm (rty, qty) =
kuncar@47308
   285
  let
kuncar@47937
   286
    val unfolded_def = Conv.fconv_rule (Conv.arg_conv unfold_fun_maps_beta) def_thm
kuncar@47308
   287
    val unabs_def = unabs_all_def ctxt unfolded_def
kuncar@55610
   288
  in  
kuncar@55610
   289
    if body_type rty = body_type qty then 
kuncar@55723
   290
      SOME (simplify_code_eq ctxt (unabs_def RS @{thm meta_eq_to_obj_eq}))
kuncar@55610
   291
    else 
kuncar@55610
   292
      let
kuncar@55610
   293
        val thy = Proof_Context.theory_of ctxt
kuncar@55610
   294
        val quot_thm = Lifting_Term.prove_quot_thm ctxt (get_body_types (rty, qty))
blanchet@55945
   295
        val rel_fun = prove_rel ctxt rsp_thm (rty, qty)
blanchet@55945
   296
        val rep_abs_thm = [quot_thm, rel_fun] MRSL @{thm Quotient_rep_abs_eq}
kuncar@55610
   297
      in
kuncar@55610
   298
        case mono_eq_prover ctxt (hd(prems_of rep_abs_thm)) of
kuncar@55610
   299
          SOME mono_eq_thm =>
kuncar@55610
   300
            let
kuncar@55610
   301
              val rep_abs_eq = mono_eq_thm RS rep_abs_thm
kuncar@55610
   302
              val rep = (cterm_of thy o quot_thm_rep) quot_thm
kuncar@55610
   303
              val rep_refl = Thm.reflexive rep RS @{thm meta_eq_to_obj_eq}
kuncar@55610
   304
              val repped_eq = [rep_refl, unabs_def RS @{thm meta_eq_to_obj_eq}] MRSL @{thm cong}
kuncar@55610
   305
              val code_cert = [repped_eq, rep_abs_eq] MRSL trans
kuncar@55610
   306
            in
kuncar@55610
   307
              SOME (simplify_code_eq ctxt code_cert)
kuncar@55610
   308
            end
kuncar@55610
   309
          | NONE => NONE
kuncar@55610
   310
      end
kuncar@47308
   311
  end
kuncar@47308
   312
kuncar@47937
   313
fun generate_abs_eq ctxt def_thm rsp_thm quot_thm =
kuncar@47308
   314
  let
kuncar@47937
   315
    val abs_eq_with_assms =
kuncar@47937
   316
      let
kuncar@47951
   317
        val (rty, qty) = quot_thm_rty_qty quot_thm
kuncar@47951
   318
        val rel = quot_thm_rel quot_thm
kuncar@47937
   319
        val ty_args = get_binder_types_by_rel rel (rty, qty)
kuncar@47937
   320
        val body_type = get_body_type_by_rel rel (rty, qty)
kuncar@47937
   321
        val quot_ret_thm = Lifting_Term.prove_quot_thm ctxt body_type
kuncar@47937
   322
        
kuncar@47937
   323
        val rep_abs_folded_unmapped_thm = 
kuncar@47937
   324
          let
kuncar@47937
   325
            val rep_id = [quot_thm, def_thm] MRSL @{thm Quotient_Rep_eq}
kuncar@47937
   326
            val ctm = Thm.dest_equals_lhs (cprop_of rep_id)
kuncar@47937
   327
            val unfolded_maps_eq = unfold_fun_maps ctm
kuncar@47937
   328
            val t1 = [quot_thm, def_thm, rsp_thm] MRSL @{thm Quotient_rep_abs_fold_unmap}
kuncar@47937
   329
            val prems_pat = (hd o Drule.cprems_of) t1
kuncar@47937
   330
            val insts = Thm.first_order_match (prems_pat, cprop_of unfolded_maps_eq)
kuncar@47937
   331
          in
kuncar@47937
   332
            unfolded_maps_eq RS (Drule.instantiate_normalize insts t1)
kuncar@47937
   333
          end
kuncar@47937
   334
      in
kuncar@47937
   335
        rep_abs_folded_unmapped_thm
blanchet@55945
   336
        |> fold (fn _ => fn thm => thm RS @{thm rel_funD2}) ty_args
kuncar@47937
   337
        |> (fn x => x RS (@{thm Quotient_rel_abs2} OF [quot_ret_thm]))
kuncar@47937
   338
      end
kuncar@47937
   339
    
kuncar@57642
   340
    val prem_rels = get_binder_rels (quot_thm_rel quot_thm);
kuncar@57642
   341
    val proved_assms = prem_rels |> map (try_prove_refl_rel ctxt) 
kuncar@57642
   342
      |> map_index (apfst (fn x => x + 1)) |> filter (is_some o snd) |> map (apsnd the) 
kuncar@57642
   343
      |> map (apsnd (fn assm => assm RS @{thm ge_eq_refl}))
kuncar@57642
   344
    val abs_eq = fold_rev (fn (i, assm) => fn thm => assm RSN (i, thm)) proved_assms abs_eq_with_assms
kuncar@47308
   345
  in
kuncar@47937
   346
    simplify_code_eq ctxt abs_eq
kuncar@47937
   347
  end
kuncar@47937
   348
kuncar@47937
   349
kuncar@55724
   350
fun register_code_eq_thy abs_eq_thm opt_rep_eq_thm (rty, qty) thy =
kuncar@47937
   351
  let
kuncar@55724
   352
    fun no_abstr (t $ u) = no_abstr t andalso no_abstr u
kuncar@55724
   353
      | no_abstr (Abs (_, _, t)) = no_abstr t
kuncar@55724
   354
      | no_abstr (Const (name, _)) = not (Code.is_abstr thy name)
kuncar@55724
   355
      | no_abstr _ = true
kuncar@55724
   356
    fun is_valid_eq eqn = can (Code.assert_eqn thy) (mk_meta_eq eqn, true) 
kuncar@55724
   357
      andalso no_abstr (prop_of eqn)
kuncar@55724
   358
    fun is_valid_abs_eq abs_eq = can (Code.assert_abs_eqn thy NONE) (mk_meta_eq abs_eq)
kuncar@47308
   359
kuncar@47308
   360
  in
kuncar@55724
   361
    if is_valid_eq abs_eq_thm then
kuncar@55724
   362
      Code.add_default_eqn abs_eq_thm thy
kuncar@47937
   363
    else
kuncar@55724
   364
      let
kuncar@55724
   365
        val (rty_body, qty_body) = get_body_types (rty, qty)
kuncar@47937
   366
      in
kuncar@55724
   367
        if rty_body = qty_body then
kuncar@55724
   368
         Code.add_default_eqn (the opt_rep_eq_thm) thy
kuncar@47937
   369
        else
kuncar@55724
   370
          if is_some opt_rep_eq_thm andalso is_valid_abs_eq (the opt_rep_eq_thm)
kuncar@55724
   371
          then
haftmann@59458
   372
            Code.add_abs_default_eqn (the opt_rep_eq_thm) thy
kuncar@55724
   373
          else
kuncar@55724
   374
            thy
kuncar@47937
   375
      end
kuncar@47937
   376
  end
kuncar@47308
   377
kuncar@55724
   378
local
kuncar@55724
   379
  fun encode_code_eq thy abs_eq opt_rep_eq (rty, qty) = 
kuncar@55724
   380
    let
kuncar@55724
   381
      fun mk_type typ = typ |> Logic.mk_type |> cterm_of thy |> Drule.mk_term
kuncar@55724
   382
    in
kuncar@55724
   383
      Conjunction.intr_balanced [abs_eq, (the_default TrueI opt_rep_eq), mk_type rty, mk_type qty]
kuncar@55724
   384
    end
kuncar@55724
   385
  
kuncar@57664
   386
  exception DECODE
kuncar@57664
   387
    
kuncar@55724
   388
  fun decode_code_eq thm =
kuncar@57664
   389
    if nprems_of thm > 0 then raise DECODE 
kuncar@57664
   390
    else
kuncar@57664
   391
      let
kuncar@57664
   392
        val [abs_eq, rep_eq, rty, qty] = Conjunction.elim_balanced 4 thm
kuncar@57664
   393
        val opt_rep_eq = if Thm.eq_thm_prop (rep_eq, TrueI) then NONE else SOME rep_eq
kuncar@57664
   394
        fun dest_type typ = typ |> Drule.dest_term |> term_of |> Logic.dest_type
kuncar@57664
   395
      in
kuncar@57664
   396
        (abs_eq, opt_rep_eq, (dest_type rty, dest_type qty)) 
kuncar@57664
   397
      end
kuncar@55724
   398
  
kuncar@55724
   399
  fun register_encoded_code_eq thm thy =
kuncar@55724
   400
    let
kuncar@55724
   401
      val (abs_eq_thm, opt_rep_eq_thm, (rty, qty)) = decode_code_eq thm
kuncar@55724
   402
    in
kuncar@55724
   403
      register_code_eq_thy abs_eq_thm opt_rep_eq_thm (rty, qty) thy
kuncar@55724
   404
    end
kuncar@57664
   405
    handle DECODE => thy
kuncar@55724
   406
  
kuncar@55724
   407
  val register_code_eq_attribute = Thm.declaration_attribute
kuncar@55724
   408
    (fn thm => Context.mapping (register_encoded_code_eq thm) I)
kuncar@55724
   409
  val register_code_eq_attrib = Attrib.internal (K register_code_eq_attribute)
kuncar@57663
   410
kuncar@57663
   411
  fun no_no_code ctxt (rty, qty) =
kuncar@57663
   412
    if same_type_constrs (rty, qty) then
kuncar@57663
   413
      forall (no_no_code ctxt) (Targs rty ~~ Targs qty)
kuncar@57663
   414
    else
kuncar@57663
   415
      if is_Type qty then
kuncar@57663
   416
        if Lifting_Info.is_no_code_type ctxt (Tname qty) then false
kuncar@57663
   417
        else 
kuncar@57663
   418
          let 
kuncar@57663
   419
            val (rty', rtyq) = Lifting_Term.instantiate_rtys ctxt (rty, qty)
kuncar@57663
   420
            val (rty's, rtyqs) = (Targs rty', Targs rtyq)
kuncar@57663
   421
          in
kuncar@57663
   422
            forall (no_no_code ctxt) (rty's ~~ rtyqs)
kuncar@57663
   423
          end
kuncar@57663
   424
      else
kuncar@57663
   425
        true
kuncar@55724
   426
in
kuncar@55724
   427
kuncar@55724
   428
fun register_code_eq abs_eq_thm opt_rep_eq_thm (rty, qty) lthy =
kuncar@55724
   429
  let
kuncar@55724
   430
    val thy = Proof_Context.theory_of lthy
kuncar@55724
   431
    val encoded_code_eq = encode_code_eq thy abs_eq_thm opt_rep_eq_thm (rty, qty)
kuncar@55724
   432
  in
kuncar@57663
   433
    if no_no_code lthy (rty, qty) then 
kuncar@57663
   434
      (snd oo Local_Theory.note) ((Binding.empty, [register_code_eq_attrib]), [encoded_code_eq]) lthy
kuncar@57663
   435
    else
kuncar@57663
   436
      lthy
kuncar@55724
   437
  end
kuncar@55724
   438
end
kuncar@55724
   439
            
kuncar@47852
   440
(*
kuncar@47852
   441
  Defines an operation on an abstract type in terms of a corresponding operation 
kuncar@47852
   442
    on a representation type.
kuncar@47852
   443
kuncar@47852
   444
  var - a binding and a mixfix of the new constant being defined
kuncar@47852
   445
  qty - an abstract type of the new constant
kuncar@47852
   446
  rhs - a term representing the new constant on the raw level
kuncar@47937
   447
  rsp_thm - a respectfulness theorem in the internal tagged form (like '(R ===> R ===> R) f f'),
kuncar@47852
   448
    i.e. "(Lifting_Term.equiv_relation (fastype_of rhs, qty)) $ rhs $ rhs"
kuncar@54335
   449
  par_thms - a parametricity theorem for rhs
kuncar@47852
   450
*)
kuncar@47308
   451
kuncar@53951
   452
fun add_lift_def var qty rhs rsp_thm par_thms lthy =
kuncar@47308
   453
  let
kuncar@47308
   454
    val rty = fastype_of rhs
kuncar@47937
   455
    val quot_thm = Lifting_Term.prove_quot_thm lthy (rty, qty)
kuncar@47951
   456
    val absrep_trm =  quot_thm_abs quot_thm
kuncar@47308
   457
    val rty_forced = (domain_type o fastype_of) absrep_trm
kuncar@47308
   458
    val forced_rhs = force_rty_type lthy rty_forced rhs
wenzelm@53207
   459
    val lhs = Free (Binding.name_of (#1 var), qty)
kuncar@47308
   460
    val prop = Logic.mk_equals (lhs, absrep_trm $ forced_rhs)
kuncar@47308
   461
    val (_, prop') = Local_Defs.cert_def lthy prop
kuncar@47308
   462
    val (_, newrhs) = Local_Defs.abs_def prop'
kuncar@47308
   463
kuncar@47308
   464
    val ((_, (_ , def_thm)), lthy') = 
kuncar@47308
   465
      Local_Theory.define (var, ((Thm.def_binding (#1 var), []), newrhs)) lthy
kuncar@47308
   466
kuncar@53951
   467
    val transfer_rules = generate_transfer_rules lthy' quot_thm rsp_thm def_thm par_thms
huffman@49975
   468
kuncar@47937
   469
    val abs_eq_thm = generate_abs_eq lthy' def_thm rsp_thm quot_thm
kuncar@51374
   470
    val opt_rep_eq_thm = generate_rep_eq lthy' def_thm rsp_thm (rty_forced, qty)
huffman@47351
   471
kuncar@47545
   472
    fun qualify defname suffix = Binding.qualified true suffix defname
kuncar@47308
   473
kuncar@47545
   474
    val lhs_name = (#1 var)
kuncar@47308
   475
    val rsp_thm_name = qualify lhs_name "rsp"
kuncar@47937
   476
    val abs_eq_thm_name = qualify lhs_name "abs_eq"
kuncar@47937
   477
    val rep_eq_thm_name = qualify lhs_name "rep_eq"
kuncar@51374
   478
    val transfer_rule_name = qualify lhs_name "transfer"
huffman@47373
   479
    val transfer_attr = Attrib.internal (K Transfer.transfer_add)
kuncar@47308
   480
  in
kuncar@47308
   481
    lthy'
kuncar@47308
   482
      |> (snd oo Local_Theory.note) ((rsp_thm_name, []), [rsp_thm])
kuncar@53951
   483
      |> (snd oo Local_Theory.note) ((transfer_rule_name, [transfer_attr]), transfer_rules)
kuncar@47937
   484
      |> (snd oo Local_Theory.note) ((abs_eq_thm_name, []), [abs_eq_thm])
kuncar@51374
   485
      |> (case opt_rep_eq_thm of 
kuncar@47937
   486
            SOME rep_eq_thm => (snd oo Local_Theory.note) ((rep_eq_thm_name, []), [rep_eq_thm])
kuncar@47937
   487
            | NONE => I)
kuncar@55724
   488
      |> register_code_eq abs_eq_thm opt_rep_eq_thm (rty_forced, qty)
kuncar@47308
   489
  end
kuncar@47308
   490
kuncar@55731
   491
local
kuncar@56519
   492
  val eq_onp_assms_tac_fixed_rules = map (Transfer.prep_transfer_domain_thm @{context})
kuncar@55731
   493
    [@{thm pcr_Domainp_total}, @{thm pcr_Domainp_par_left_total}, @{thm pcr_Domainp_par}, 
kuncar@55731
   494
      @{thm pcr_Domainp}]
kuncar@55731
   495
in
kuncar@47308
   496
fun mk_readable_rsp_thm_eq tm lthy =
kuncar@47308
   497
  let
kuncar@47308
   498
    val ctm = cterm_of (Proof_Context.theory_of lthy) tm
kuncar@47308
   499
    
kuncar@55731
   500
    fun assms_rewr_conv tactic rule ct =
kuncar@55731
   501
      let
kuncar@55731
   502
        fun prove_extra_assms thm =
kuncar@55731
   503
          let
kuncar@55731
   504
            val assms = cprems_of thm
kuncar@55731
   505
            fun finish thm = if Thm.no_prems thm then SOME (Goal.conclude thm) else NONE
kuncar@55731
   506
            fun prove ctm = Option.mapPartial finish (SINGLE tactic (Goal.init ctm))
kuncar@55731
   507
          in
kuncar@55731
   508
            map_interrupt prove assms
kuncar@55731
   509
          end
kuncar@55731
   510
    
kuncar@55731
   511
        fun cconl_of thm = Drule.strip_imp_concl (cprop_of thm)
kuncar@55731
   512
        fun lhs_of thm = fst (Thm.dest_equals (cconl_of thm))
kuncar@55731
   513
        fun rhs_of thm = snd (Thm.dest_equals (cconl_of thm))
kuncar@55731
   514
        val rule1 = Thm.incr_indexes (#maxidx (Thm.rep_cterm ct) + 1) rule;
kuncar@55731
   515
        val lhs = lhs_of rule1;
kuncar@55731
   516
        val rule2 = Thm.rename_boundvars (Thm.term_of lhs) (Thm.term_of ct) rule1;
kuncar@55731
   517
        val rule3 =
kuncar@55731
   518
          Thm.instantiate (Thm.match (lhs, ct)) rule2
kuncar@55731
   519
            handle Pattern.MATCH => raise CTERM ("assms_rewr_conv", [lhs, ct]);
kuncar@55731
   520
        val proved_assms = prove_extra_assms rule3
kuncar@55731
   521
      in
kuncar@55731
   522
        case proved_assms of
kuncar@55731
   523
          SOME proved_assms =>
kuncar@55731
   524
            let
kuncar@55731
   525
              val rule3 = proved_assms MRSL rule3
kuncar@55731
   526
              val rule4 =
kuncar@55731
   527
                if lhs_of rule3 aconvc ct then rule3
kuncar@55731
   528
                else
kuncar@55731
   529
                  let val ceq = Thm.dest_fun2 (Thm.cprop_of rule3)
kuncar@55731
   530
                  in rule3 COMP Thm.trivial (Thm.mk_binop ceq ct (rhs_of rule3)) end
kuncar@55731
   531
            in Thm.transitive rule4 (Thm.beta_conversion true (rhs_of rule4)) end
kuncar@55731
   532
          | NONE => Conv.no_conv ct
kuncar@55731
   533
      end
kuncar@55731
   534
kuncar@55731
   535
    fun assms_rewrs_conv tactic rules = Conv.first_conv (map (assms_rewr_conv tactic) rules)
kuncar@55731
   536
kuncar@47308
   537
    fun simp_arrows_conv ctm =
kuncar@47308
   538
      let
kuncar@47308
   539
        val unfold_conv = Conv.rewrs_conv 
kuncar@56519
   540
          [@{thm rel_fun_eq_eq_onp[THEN eq_reflection]}, 
kuncar@56519
   541
            @{thm rel_fun_eq_onp_rel[THEN eq_reflection]},
blanchet@55945
   542
            @{thm rel_fun_eq[THEN eq_reflection]},
blanchet@55945
   543
            @{thm rel_fun_eq_rel[THEN eq_reflection]}, 
blanchet@55945
   544
            @{thm rel_fun_def[THEN eq_reflection]}]
kuncar@47308
   545
        fun binop_conv2 cv1 cv2 = Conv.combination_conv (Conv.arg_conv cv1) cv2
kuncar@56519
   546
        val eq_onp_assms_tac_rules = @{thm left_unique_OO} :: 
kuncar@56519
   547
            eq_onp_assms_tac_fixed_rules @ (Transfer.get_transfer_raw lthy)
wenzelm@59498
   548
        val eq_onp_assms_tac = (TRY o REPEAT_ALL_NEW (resolve_tac lthy eq_onp_assms_tac_rules) 
kuncar@55731
   549
          THEN_ALL_NEW (DETERM o Transfer.eq_tac lthy)) 1
kuncar@56519
   550
        val relator_eq_onp_conv = Conv.bottom_conv
kuncar@56519
   551
          (K (Conv.try_conv (assms_rewrs_conv eq_onp_assms_tac
kuncar@56519
   552
            (Lifting_Info.get_relator_eq_onp_rules lthy)))) lthy
kuncar@49626
   553
        val relator_eq_conv = Conv.bottom_conv
kuncar@49626
   554
          (K (Conv.try_conv (Conv.rewrs_conv (Transfer.get_relator_eq lthy)))) lthy
kuncar@47308
   555
      in
kuncar@47308
   556
        case (Thm.term_of ctm) of
blanchet@55945
   557
          Const (@{const_name "rel_fun"}, _) $ _ $ _ => 
kuncar@49626
   558
            (binop_conv2 simp_arrows_conv simp_arrows_conv then_conv unfold_conv) ctm
kuncar@56519
   559
          | _ => (relator_eq_onp_conv then_conv relator_eq_conv) ctm
kuncar@47308
   560
      end
kuncar@47937
   561
    
kuncar@47308
   562
    val unfold_ret_val_invs = Conv.bottom_conv 
kuncar@56524
   563
      (K (Conv.try_conv (Conv.rewr_conv @{thm eq_onp_same_args[THEN eq_reflection]}))) lthy
kuncar@55773
   564
    val unfold_inv_conv = 
kuncar@56519
   565
      Conv.top_sweep_conv (K (Conv.rewr_conv @{thm eq_onp_def[THEN eq_reflection]})) lthy
kuncar@56540
   566
    val simp_conv = HOLogic.Trueprop_conv (Conv.fun2_conv simp_arrows_conv)
kuncar@47308
   567
    val univq_conv = Conv.rewr_conv @{thm HOL.all_simps(6)[symmetric, THEN eq_reflection]}
kuncar@47308
   568
    val univq_prenex_conv = Conv.top_conv (K (Conv.try_conv univq_conv)) lthy
kuncar@47308
   569
    val beta_conv = Thm.beta_conversion true
kuncar@47308
   570
    val eq_thm = 
kuncar@55773
   571
      (simp_conv then_conv univq_prenex_conv then_conv beta_conv then_conv unfold_ret_val_invs
kuncar@55773
   572
         then_conv unfold_inv_conv) ctm
kuncar@47308
   573
  in
wenzelm@54742
   574
    Object_Logic.rulify lthy (eq_thm RS Drule.equal_elim_rule2)
kuncar@47308
   575
  end
kuncar@55731
   576
end
kuncar@47308
   577
kuncar@47607
   578
fun rename_to_tnames ctxt term =
kuncar@47607
   579
  let
wenzelm@56245
   580
    fun all_typs (Const (@{const_name Pure.all}, _) $ Abs (_, T, t)) = T :: all_typs t
kuncar@47607
   581
      | all_typs _ = []
kuncar@47308
   582
wenzelm@56245
   583
    fun rename (Const (@{const_name Pure.all}, T1) $ Abs (_, T2, t)) (new_name :: names) = 
wenzelm@56245
   584
        (Const (@{const_name Pure.all}, T1) $ Abs (new_name, T2, rename t names)) 
kuncar@47607
   585
      | rename t _ = t
kuncar@47607
   586
kuncar@47607
   587
    val (fixed_def_t, _) = yield_singleton (Variable.importT_terms) term ctxt
blanchet@58112
   588
    val new_names = Old_Datatype_Prop.make_tnames (all_typs fixed_def_t)
kuncar@47607
   589
  in
kuncar@47607
   590
    rename term new_names
kuncar@47607
   591
  end
kuncar@47308
   592
kuncar@56540
   593
(* This is not very cheap way of getting the rules but we have only few active
kuncar@56540
   594
  liftings in the current setting *)
kuncar@56540
   595
fun get_cr_pcr_eqs ctxt =
kuncar@56540
   596
  let
kuncar@56540
   597
    fun collect (data : Lifting_Info.quotient) l =
kuncar@56540
   598
      if is_some (#pcr_info data) 
kuncar@56540
   599
      then ((Thm.symmetric o safe_mk_meta_eq o #pcr_cr_eq o the o #pcr_info) data :: l) 
kuncar@56540
   600
      else l
kuncar@56540
   601
    val table = Lifting_Info.get_quotients ctxt
kuncar@56540
   602
  in
kuncar@56540
   603
    Symtab.fold (fn (_, data) => fn l => collect data l) table []
kuncar@56540
   604
  end
kuncar@56540
   605
kuncar@47852
   606
(*
kuncar@47852
   607
kuncar@47852
   608
  lifting_definition command. It opens a proof of a corresponding respectfulness 
kuncar@47852
   609
  theorem in a user-friendly, readable form. Then add_lift_def is called internally.
kuncar@47852
   610
kuncar@47852
   611
*)
kuncar@47852
   612
kuncar@53951
   613
fun lift_def_cmd (raw_var, rhs_raw, par_xthms) lthy =
kuncar@47308
   614
  let
kuncar@51994
   615
    val ((binding, SOME qty, mx), lthy) = yield_singleton Proof_Context.read_vars raw_var lthy 
kuncar@51994
   616
    val rhs = (Syntax.check_term lthy o Syntax.parse_term lthy) rhs_raw
kuncar@51994
   617
    val rsp_rel = Lifting_Term.equiv_relation lthy (fastype_of rhs, qty)
kuncar@47308
   618
    val rty_forced = (domain_type o fastype_of) rsp_rel;
kuncar@51994
   619
    val forced_rhs = force_rty_type lthy rty_forced rhs;
kuncar@56540
   620
    val cr_to_pcr_conv = HOLogic.Trueprop_conv (Conv.fun2_conv 
kuncar@56540
   621
      (Raw_Simplifier.rewrite lthy false (get_cr_pcr_eqs lthy)))
kuncar@56540
   622
    val (prsp_tm, rsp_prsp_eq) = HOLogic.mk_Trueprop (rsp_rel $ forced_rhs $ forced_rhs)
kuncar@56540
   623
      |> cterm_of (Proof_Context.theory_of lthy)
kuncar@56540
   624
      |> cr_to_pcr_conv
kuncar@56540
   625
      |> ` concl_of
kuncar@56540
   626
      |>> Logic.dest_equals
kuncar@56540
   627
      |>> snd
kuncar@56540
   628
    val to_rsp = rsp_prsp_eq RS Drule.equal_elim_rule2
kuncar@56540
   629
    val opt_proven_rsp_thm = try_prove_reflexivity lthy prsp_tm
kuncar@53951
   630
    val par_thms = Attrib.eval_thms lthy par_xthms
kuncar@51994
   631
    
kuncar@51994
   632
    fun after_qed internal_rsp_thm lthy = 
kuncar@56540
   633
      add_lift_def (binding, mx) qty rhs (internal_rsp_thm RS to_rsp) par_thms lthy
kuncar@47308
   634
  in
kuncar@51374
   635
    case opt_proven_rsp_thm of
kuncar@51994
   636
      SOME thm => Proof.theorem NONE (K (after_qed thm)) [] lthy
kuncar@51994
   637
      | NONE =>  
kuncar@51994
   638
        let
kuncar@56540
   639
          val readable_rsp_thm_eq = mk_readable_rsp_thm_eq prsp_tm lthy
kuncar@51994
   640
          val (readable_rsp_tm, _) = Logic.dest_implies (prop_of readable_rsp_thm_eq)
kuncar@51994
   641
          val readable_rsp_tm_tnames = rename_to_tnames lthy readable_rsp_tm
kuncar@51994
   642
      
kuncar@51994
   643
          fun after_qed' thm_list lthy = 
kuncar@51994
   644
            let
kuncar@56540
   645
              val internal_rsp_thm = Goal.prove lthy [] [] prsp_tm 
wenzelm@54742
   646
                  (fn {context = ctxt, ...} =>
wenzelm@54742
   647
                    rtac readable_rsp_thm_eq 1 THEN Proof_Context.fact_tac ctxt (hd thm_list) 1)
kuncar@51994
   648
            in
kuncar@51994
   649
              after_qed internal_rsp_thm lthy
kuncar@51994
   650
            end
kuncar@51994
   651
        in
kuncar@51994
   652
          Proof.theorem NONE after_qed' [[(readable_rsp_tm_tnames,[])]] lthy
kuncar@51994
   653
        end 
kuncar@47308
   654
  end
kuncar@47308
   655
kuncar@47379
   656
fun quot_thm_err ctxt (rty, qty) pretty_msg =
kuncar@47379
   657
  let
kuncar@47379
   658
    val error_msg = cat_lines
kuncar@47379
   659
       ["Lifting failed for the following types:",
kuncar@47379
   660
        Pretty.string_of (Pretty.block
kuncar@47379
   661
         [Pretty.str "Raw type:", Pretty.brk 2, Syntax.pretty_typ ctxt rty]),
kuncar@47379
   662
        Pretty.string_of (Pretty.block
kuncar@47379
   663
         [Pretty.str "Abstract type:", Pretty.brk 2, Syntax.pretty_typ ctxt qty]),
kuncar@47379
   664
        "",
kuncar@47379
   665
        (Pretty.string_of (Pretty.block
kuncar@47379
   666
         [Pretty.str "Reason:", Pretty.brk 2, pretty_msg]))]
kuncar@47379
   667
  in
kuncar@47379
   668
    error error_msg
kuncar@47379
   669
  end
kuncar@47379
   670
kuncar@47504
   671
fun check_rty_err ctxt (rty_schematic, rty_forced) (raw_var, rhs_raw) =
kuncar@47379
   672
  let
kuncar@47504
   673
    val (_, ctxt') = yield_singleton Proof_Context.read_vars raw_var ctxt 
kuncar@47504
   674
    val rhs = (Syntax.check_term ctxt' o Syntax.parse_term ctxt') rhs_raw
kuncar@47379
   675
    val error_msg = cat_lines
kuncar@47379
   676
       ["Lifting failed for the following term:",
kuncar@47379
   677
        Pretty.string_of (Pretty.block
kuncar@47379
   678
         [Pretty.str "Term:", Pretty.brk 2, Syntax.pretty_term ctxt rhs]),
kuncar@47379
   679
        Pretty.string_of (Pretty.block
kuncar@47504
   680
         [Pretty.str "Type:", Pretty.brk 2, Syntax.pretty_typ ctxt rty_schematic]),
kuncar@47379
   681
        "",
kuncar@47379
   682
        (Pretty.string_of (Pretty.block
kuncar@47379
   683
         [Pretty.str "Reason:", 
kuncar@47379
   684
          Pretty.brk 2, 
kuncar@51957
   685
          Pretty.str "The type of the term cannot be instantiated to",
kuncar@47379
   686
          Pretty.brk 1,
kuncar@47504
   687
          Pretty.quote (Syntax.pretty_typ ctxt rty_forced),
kuncar@47379
   688
          Pretty.str "."]))]
kuncar@47379
   689
    in
kuncar@47379
   690
      error error_msg
kuncar@47379
   691
    end
kuncar@47379
   692
kuncar@53951
   693
fun lift_def_cmd_with_err_handling (raw_var, rhs_raw, par_xthms) lthy =
kuncar@53951
   694
  (lift_def_cmd (raw_var, rhs_raw, par_xthms) lthy
kuncar@47379
   695
    handle Lifting_Term.QUOT_THM (rty, qty, msg) => quot_thm_err lthy (rty, qty) msg)
kuncar@47504
   696
    handle Lifting_Term.CHECK_RTY (rty_schematic, rty_forced) => 
kuncar@47504
   697
      check_rty_err lthy (rty_schematic, rty_forced) (raw_var, rhs_raw)
kuncar@47379
   698
kuncar@47308
   699
(* parser and command *)
kuncar@47308
   700
val liftdef_parser =
kuncar@51374
   701
  (((Parse.binding -- (@{keyword "::"} |-- (Parse.typ >> SOME) -- Parse.opt_mixfix')) >> Parse.triple2)
kuncar@53951
   702
    --| @{keyword "is"} -- Parse.term -- 
wenzelm@58028
   703
      Scan.optional (@{keyword "parametric"} |-- Parse.!!! Parse.xthms1) []) >> Parse.triple1
kuncar@47308
   704
val _ =
kuncar@47308
   705
  Outer_Syntax.local_theory_to_proof @{command_spec "lift_definition"}
kuncar@47308
   706
    "definition for constants over the quotient type"
kuncar@47379
   707
      (liftdef_parser >> lift_def_cmd_with_err_handling)
kuncar@47308
   708
kuncar@47308
   709
kuncar@53651
   710
end (* structure *)