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