src/HOL/Tools/Lifting/lifting_def.ML
author kuncar
Fri Apr 20 18:29:21 2012 +0200 (2012-04-20)
changeset 47634 091bcd569441
parent 47607 5c17ef8feac7
child 47675 4483c004499a
permissions -rw-r--r--
hide the invariant constant for relators: invariant_commute infrastracture
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@47308
     9
  val add_lift_def:
kuncar@47308
    10
    (binding * mixfix) -> typ -> term -> thm -> local_theory -> local_theory
kuncar@47308
    11
kuncar@47308
    12
  val lift_def_cmd:
kuncar@47308
    13
    (binding * string option * mixfix) * string -> local_theory -> Proof.state
kuncar@47308
    14
kuncar@47308
    15
  val can_generate_code_cert: thm -> bool
kuncar@47308
    16
end;
kuncar@47308
    17
kuncar@47308
    18
structure Lifting_Def: LIFTING_DEF =
kuncar@47308
    19
struct
kuncar@47308
    20
kuncar@47308
    21
(** Interface and Syntax Setup **)
kuncar@47308
    22
kuncar@47308
    23
(* Generation of the code certificate from the rsp theorem *)
kuncar@47308
    24
kuncar@47308
    25
infix 0 MRSL
kuncar@47308
    26
kuncar@47308
    27
fun ants MRSL thm = fold (fn rl => fn thm => rl RS thm) ants thm
kuncar@47308
    28
kuncar@47308
    29
fun get_body_types (Type ("fun", [_, U]), Type ("fun", [_, V])) = get_body_types (U, V)
kuncar@47308
    30
  | get_body_types (U, V)  = (U, V)
kuncar@47308
    31
kuncar@47308
    32
fun get_binder_types (Type ("fun", [T, U]), Type ("fun", [V, W])) = (T, V) :: get_binder_types (U, W)
kuncar@47308
    33
  | get_binder_types _ = []
kuncar@47308
    34
kuncar@47308
    35
fun force_rty_type ctxt rty rhs = 
kuncar@47308
    36
  let
kuncar@47308
    37
    val thy = Proof_Context.theory_of ctxt
kuncar@47308
    38
    val rhs_schematic = singleton (Variable.polymorphic ctxt) rhs
kuncar@47308
    39
    val rty_schematic = fastype_of rhs_schematic
kuncar@47308
    40
    val match = Sign.typ_match thy (rty_schematic, rty) Vartab.empty
kuncar@47308
    41
  in
kuncar@47308
    42
    Envir.subst_term_types match rhs_schematic
kuncar@47308
    43
  end
kuncar@47308
    44
kuncar@47308
    45
fun unabs_def ctxt def = 
kuncar@47308
    46
  let
kuncar@47308
    47
    val (_, rhs) = Thm.dest_equals (cprop_of def)
kuncar@47308
    48
    fun dest_abs (Abs (var_name, T, _)) = (var_name, T)
kuncar@47308
    49
      | dest_abs tm = raise TERM("get_abs_var",[tm])
kuncar@47308
    50
    val (var_name, T) = dest_abs (term_of rhs)
kuncar@47308
    51
    val (new_var_names, ctxt') = Variable.variant_fixes [var_name] ctxt
kuncar@47308
    52
    val thy = Proof_Context.theory_of ctxt'
kuncar@47308
    53
    val refl_thm = Thm.reflexive (cterm_of thy (Free (hd new_var_names, T)))
kuncar@47308
    54
  in
kuncar@47308
    55
    Thm.combination def refl_thm |>
kuncar@47308
    56
    singleton (Proof_Context.export ctxt' ctxt)
kuncar@47308
    57
  end
kuncar@47308
    58
kuncar@47308
    59
fun unabs_all_def ctxt def = 
kuncar@47308
    60
  let
kuncar@47308
    61
    val (_, rhs) = Thm.dest_equals (cprop_of def)
kuncar@47308
    62
    val xs = strip_abs_vars (term_of rhs)
kuncar@47308
    63
  in  
kuncar@47308
    64
    fold (K (unabs_def ctxt)) xs def
kuncar@47308
    65
  end
kuncar@47308
    66
kuncar@47308
    67
val map_fun_unfolded = 
kuncar@47308
    68
  @{thm map_fun_def[abs_def]} |>
kuncar@47308
    69
  unabs_def @{context} |>
kuncar@47308
    70
  unabs_def @{context} |>
kuncar@47308
    71
  Local_Defs.unfold @{context} [@{thm comp_def}]
kuncar@47308
    72
kuncar@47308
    73
fun unfold_fun_maps ctm =
kuncar@47308
    74
  let
kuncar@47308
    75
    fun unfold_conv ctm =
kuncar@47308
    76
      case (Thm.term_of ctm) of
kuncar@47308
    77
        Const (@{const_name "map_fun"}, _) $ _ $ _ => 
kuncar@47308
    78
          (Conv.arg_conv unfold_conv then_conv Conv.rewr_conv map_fun_unfolded) ctm
kuncar@47308
    79
        | _ => Conv.all_conv ctm
kuncar@47308
    80
    val try_beta_conv = Conv.try_conv (Thm.beta_conversion false)
kuncar@47308
    81
  in
kuncar@47308
    82
    (Conv.arg_conv (Conv.fun_conv unfold_conv then_conv try_beta_conv)) ctm
kuncar@47308
    83
  end
kuncar@47308
    84
kuncar@47308
    85
fun prove_rel ctxt rsp_thm (rty, qty) =
kuncar@47308
    86
  let
kuncar@47308
    87
    val ty_args = get_binder_types (rty, qty)
kuncar@47308
    88
    fun disch_arg args_ty thm = 
kuncar@47308
    89
      let
kuncar@47504
    90
        val quot_thm = Lifting_Term.prove_quot_thm ctxt args_ty
kuncar@47308
    91
      in
kuncar@47308
    92
        [quot_thm, thm] MRSL @{thm apply_rsp''}
kuncar@47308
    93
      end
kuncar@47308
    94
  in
kuncar@47308
    95
    fold disch_arg ty_args rsp_thm
kuncar@47308
    96
  end
kuncar@47308
    97
kuncar@47308
    98
exception CODE_CERT_GEN of string
kuncar@47308
    99
kuncar@47308
   100
fun simplify_code_eq ctxt def_thm = 
kuncar@47308
   101
  Local_Defs.unfold ctxt [@{thm o_def}, @{thm map_fun_def}, @{thm id_def}] def_thm
kuncar@47308
   102
kuncar@47308
   103
fun can_generate_code_cert quot_thm  =
kuncar@47308
   104
  case Lifting_Term.quot_thm_rel quot_thm of
kuncar@47308
   105
    Const (@{const_name HOL.eq}, _) => true
kuncar@47308
   106
    | Const (@{const_name invariant}, _) $ _  => true
kuncar@47308
   107
    | _ => false
kuncar@47308
   108
kuncar@47308
   109
fun generate_code_cert ctxt def_thm rsp_thm (rty, qty) =
kuncar@47308
   110
  let
kuncar@47308
   111
    val thy = Proof_Context.theory_of ctxt
kuncar@47504
   112
    val quot_thm = Lifting_Term.prove_quot_thm ctxt (get_body_types (rty, qty))
kuncar@47308
   113
    val fun_rel = prove_rel ctxt rsp_thm (rty, qty)
kuncar@47308
   114
    val abs_rep_thm = [quot_thm, fun_rel] MRSL @{thm Quotient_rep_abs}
kuncar@47308
   115
    val abs_rep_eq = 
kuncar@47308
   116
      case (HOLogic.dest_Trueprop o prop_of) fun_rel of
kuncar@47308
   117
        Const (@{const_name HOL.eq}, _) $ _ $ _ => abs_rep_thm
kuncar@47308
   118
        | Const (@{const_name invariant}, _) $ _ $ _ $ _ => abs_rep_thm RS @{thm invariant_to_eq}
kuncar@47308
   119
        | _ => raise CODE_CERT_GEN "relation is neither equality nor invariant"
kuncar@47308
   120
    val unfolded_def = Conv.fconv_rule unfold_fun_maps def_thm
kuncar@47308
   121
    val unabs_def = unabs_all_def ctxt unfolded_def
kuncar@47308
   122
    val rep = (cterm_of thy o Lifting_Term.quot_thm_rep) quot_thm
kuncar@47308
   123
    val rep_refl = Thm.reflexive rep RS @{thm meta_eq_to_obj_eq}
kuncar@47308
   124
    val repped_eq = [rep_refl, unabs_def RS @{thm meta_eq_to_obj_eq}] MRSL @{thm cong}
kuncar@47308
   125
    val code_cert = [repped_eq, abs_rep_eq] MRSL @{thm trans}
kuncar@47308
   126
  in
kuncar@47308
   127
    simplify_code_eq ctxt code_cert
kuncar@47308
   128
  end
kuncar@47308
   129
kuncar@47566
   130
fun is_abstype ctxt typ =
kuncar@47566
   131
  let
kuncar@47566
   132
    val thy = Proof_Context.theory_of ctxt
kuncar@47566
   133
    val type_name = (fst o dest_Type) typ
kuncar@47566
   134
  in
kuncar@47566
   135
    (snd oo Code.get_type) thy type_name
kuncar@47566
   136
  end
kuncar@47566
   137
  
kuncar@47566
   138
kuncar@47308
   139
fun define_code_cert code_eqn_thm_name def_thm rsp_thm (rty, qty) lthy = 
kuncar@47308
   140
  let
kuncar@47566
   141
    val (rty_body, qty_body) = get_body_types (rty, qty)
kuncar@47566
   142
    val quot_thm = Lifting_Term.prove_quot_thm lthy (rty_body, qty_body)
kuncar@47308
   143
  in
kuncar@47308
   144
    if can_generate_code_cert quot_thm then
kuncar@47308
   145
      let
kuncar@47308
   146
        val code_cert = generate_code_cert lthy def_thm rsp_thm (rty, qty)
kuncar@47308
   147
        val add_abs_eqn_attribute = 
kuncar@47308
   148
          Thm.declaration_attribute (fn thm => Context.mapping (Code.add_abs_eqn thm) I)
kuncar@47308
   149
        val add_abs_eqn_attrib = Attrib.internal (K add_abs_eqn_attribute);
kuncar@47566
   150
        val lthy' = 
kuncar@47566
   151
          (snd oo Local_Theory.note) ((code_eqn_thm_name, []), [code_cert]) lthy
kuncar@47308
   152
      in
kuncar@47566
   153
        if is_abstype lthy qty_body then
kuncar@47566
   154
          (snd oo Local_Theory.note) ((Binding.empty, [add_abs_eqn_attrib]), [code_cert]) lthy'
kuncar@47566
   155
        else
kuncar@47566
   156
          lthy'
kuncar@47308
   157
      end
kuncar@47308
   158
    else
kuncar@47308
   159
      lthy
kuncar@47308
   160
  end
kuncar@47308
   161
kuncar@47308
   162
fun define_code_eq code_eqn_thm_name def_thm lthy =
kuncar@47308
   163
  let
kuncar@47308
   164
    val unfolded_def = Conv.fconv_rule unfold_fun_maps def_thm
kuncar@47308
   165
    val code_eq = unabs_all_def lthy unfolded_def
kuncar@47308
   166
    val simp_code_eq = simplify_code_eq lthy code_eq
kuncar@47308
   167
  in
kuncar@47308
   168
    lthy
kuncar@47308
   169
      |> (snd oo Local_Theory.note) ((code_eqn_thm_name, [Code.add_default_eqn_attrib]), [simp_code_eq])
kuncar@47308
   170
  end
kuncar@47308
   171
kuncar@47308
   172
fun define_code code_eqn_thm_name def_thm rsp_thm (rty, qty) lthy =
kuncar@47308
   173
  if body_type rty = body_type qty then 
kuncar@47308
   174
    define_code_eq code_eqn_thm_name def_thm lthy
kuncar@47308
   175
  else 
kuncar@47308
   176
    define_code_cert code_eqn_thm_name def_thm rsp_thm (rty, qty) lthy
kuncar@47308
   177
kuncar@47308
   178
kuncar@47308
   179
fun add_lift_def var qty rhs rsp_thm lthy =
kuncar@47308
   180
  let
kuncar@47308
   181
    val rty = fastype_of rhs
kuncar@47504
   182
    val quotient_thm = Lifting_Term.prove_quot_thm lthy (rty, qty)
huffman@47351
   183
    val absrep_trm =  Lifting_Term.quot_thm_abs quotient_thm
kuncar@47308
   184
    val rty_forced = (domain_type o fastype_of) absrep_trm
kuncar@47308
   185
    val forced_rhs = force_rty_type lthy rty_forced rhs
kuncar@47308
   186
    val lhs = Free (Binding.print (#1 var), qty)
kuncar@47308
   187
    val prop = Logic.mk_equals (lhs, absrep_trm $ forced_rhs)
kuncar@47308
   188
    val (_, prop') = Local_Defs.cert_def lthy prop
kuncar@47308
   189
    val (_, newrhs) = Local_Defs.abs_def prop'
kuncar@47308
   190
kuncar@47308
   191
    val ((_, (_ , def_thm)), lthy') = 
kuncar@47308
   192
      Local_Theory.define (var, ((Thm.def_binding (#1 var), []), newrhs)) lthy
kuncar@47308
   193
kuncar@47361
   194
    val transfer_thm = [quotient_thm, rsp_thm, def_thm] MRSL @{thm Quotient_to_transfer}
huffman@47503
   195
        |> Raw_Simplifier.rewrite_rule (Transfer.get_relator_eq lthy')
huffman@47351
   196
kuncar@47545
   197
    fun qualify defname suffix = Binding.qualified true suffix defname
kuncar@47308
   198
kuncar@47545
   199
    val lhs_name = (#1 var)
kuncar@47308
   200
    val rsp_thm_name = qualify lhs_name "rsp"
kuncar@47308
   201
    val code_eqn_thm_name = qualify lhs_name "rep_eq"
huffman@47351
   202
    val transfer_thm_name = qualify lhs_name "transfer"
huffman@47373
   203
    val transfer_attr = Attrib.internal (K Transfer.transfer_add)
kuncar@47308
   204
  in
kuncar@47308
   205
    lthy'
kuncar@47308
   206
      |> (snd oo Local_Theory.note) ((rsp_thm_name, []), [rsp_thm])
huffman@47373
   207
      |> (snd oo Local_Theory.note) ((transfer_thm_name, [transfer_attr]), [transfer_thm])
kuncar@47308
   208
      |> define_code code_eqn_thm_name def_thm rsp_thm (rty_forced, qty)
kuncar@47308
   209
  end
kuncar@47308
   210
kuncar@47308
   211
fun mk_readable_rsp_thm_eq tm lthy =
kuncar@47308
   212
  let
kuncar@47308
   213
    val ctm = cterm_of (Proof_Context.theory_of lthy) tm
kuncar@47308
   214
    
kuncar@47308
   215
    fun norm_fun_eq ctm = 
kuncar@47308
   216
      let
kuncar@47308
   217
        fun abs_conv2 cv = Conv.abs_conv (K (Conv.abs_conv (K cv) lthy)) lthy
kuncar@47308
   218
        fun erase_quants ctm' =
kuncar@47308
   219
          case (Thm.term_of ctm') of
kuncar@47308
   220
            Const ("HOL.eq", _) $ _ $ _ => Conv.all_conv ctm'
kuncar@47308
   221
            | _ => (Conv.binder_conv (K erase_quants) lthy then_conv 
kuncar@47308
   222
              Conv.rewr_conv @{thm fun_eq_iff[symmetric, THEN eq_reflection]}) ctm'
kuncar@47308
   223
      in
kuncar@47308
   224
        (abs_conv2 erase_quants then_conv Thm.eta_conversion) ctm
kuncar@47308
   225
      end
kuncar@47308
   226
kuncar@47308
   227
    fun simp_arrows_conv ctm =
kuncar@47308
   228
      let
kuncar@47308
   229
        val unfold_conv = Conv.rewrs_conv 
kuncar@47308
   230
          [@{thm fun_rel_eq_invariant[THEN eq_reflection]}, @{thm fun_rel_eq_rel[THEN eq_reflection]}, 
kuncar@47308
   231
            @{thm fun_rel_def[THEN eq_reflection]}]
kuncar@47308
   232
        val left_conv = simp_arrows_conv then_conv Conv.try_conv norm_fun_eq
kuncar@47308
   233
        fun binop_conv2 cv1 cv2 = Conv.combination_conv (Conv.arg_conv cv1) cv2
kuncar@47634
   234
        val invariant_commute_conv = Conv.bottom_conv
kuncar@47634
   235
          (K (Conv.try_conv (Conv.rewrs_conv (Lifting_Info.get_invariant_commute_rules lthy)))) lthy
kuncar@47308
   236
      in
kuncar@47308
   237
        case (Thm.term_of ctm) of
kuncar@47308
   238
          Const (@{const_name "fun_rel"}, _) $ _ $ _ => 
kuncar@47308
   239
            (binop_conv2  left_conv simp_arrows_conv then_conv unfold_conv) ctm
kuncar@47634
   240
          | _ => invariant_commute_conv ctm
kuncar@47308
   241
      end
kuncar@47308
   242
kuncar@47308
   243
    val unfold_ret_val_invs = Conv.bottom_conv 
kuncar@47308
   244
      (K (Conv.try_conv (Conv.rewr_conv @{thm invariant_same_args}))) lthy 
kuncar@47308
   245
    val simp_conv = Conv.arg_conv (Conv.fun2_conv simp_arrows_conv)
kuncar@47308
   246
    val univq_conv = Conv.rewr_conv @{thm HOL.all_simps(6)[symmetric, THEN eq_reflection]}
kuncar@47308
   247
    val univq_prenex_conv = Conv.top_conv (K (Conv.try_conv univq_conv)) lthy
kuncar@47308
   248
    val beta_conv = Thm.beta_conversion true
kuncar@47308
   249
    val eq_thm = 
kuncar@47308
   250
      (simp_conv then_conv univq_prenex_conv then_conv beta_conv then_conv unfold_ret_val_invs) ctm
kuncar@47308
   251
  in
kuncar@47308
   252
    Object_Logic.rulify(eq_thm RS Drule.equal_elim_rule2)
kuncar@47308
   253
  end
kuncar@47308
   254
kuncar@47607
   255
fun rename_to_tnames ctxt term =
kuncar@47607
   256
  let
kuncar@47607
   257
    fun all_typs (Const ("all", _) $ Abs (_, T, t)) = T :: all_typs t
kuncar@47607
   258
      | all_typs _ = []
kuncar@47308
   259
kuncar@47607
   260
    fun rename (Const ("all", T1) $ Abs (_, T2, t)) (new_name :: names) = 
kuncar@47607
   261
        (Const ("all", T1) $ Abs (new_name, T2, rename t names)) 
kuncar@47607
   262
      | rename t _ = t
kuncar@47607
   263
kuncar@47607
   264
    val (fixed_def_t, _) = yield_singleton (Variable.importT_terms) term ctxt
kuncar@47607
   265
    val new_names = Datatype_Prop.make_tnames (all_typs fixed_def_t)
kuncar@47607
   266
  in
kuncar@47607
   267
    rename term new_names
kuncar@47607
   268
  end
kuncar@47308
   269
kuncar@47308
   270
fun lift_def_cmd (raw_var, rhs_raw) lthy =
kuncar@47308
   271
  let
kuncar@47504
   272
    val ((binding, SOME qty, mx), lthy') = yield_singleton Proof_Context.read_vars raw_var lthy 
kuncar@47504
   273
    val rhs = (Syntax.check_term lthy' o Syntax.parse_term lthy') rhs_raw
kuncar@47308
   274
 
kuncar@47308
   275
    fun try_to_prove_refl thm = 
kuncar@47308
   276
      let
kuncar@47308
   277
        val lhs_eq =
kuncar@47308
   278
          thm
kuncar@47308
   279
          |> prop_of
kuncar@47308
   280
          |> Logic.dest_implies
kuncar@47308
   281
          |> fst
kuncar@47308
   282
          |> strip_all_body
kuncar@47308
   283
          |> try HOLogic.dest_Trueprop
kuncar@47308
   284
      in
kuncar@47308
   285
        case lhs_eq of
kuncar@47308
   286
          SOME (Const ("HOL.eq", _) $ _ $ _) => SOME (@{thm refl} RS thm)
kuncar@47308
   287
          | _ => NONE
kuncar@47308
   288
      end
kuncar@47308
   289
kuncar@47504
   290
    val rsp_rel = Lifting_Term.equiv_relation lthy' (fastype_of rhs, qty)
kuncar@47308
   291
    val rty_forced = (domain_type o fastype_of) rsp_rel;
kuncar@47504
   292
    val forced_rhs = force_rty_type lthy' rty_forced rhs;
kuncar@47308
   293
    val internal_rsp_tm = HOLogic.mk_Trueprop (rsp_rel $ forced_rhs $ forced_rhs)
kuncar@47504
   294
    val readable_rsp_thm_eq = mk_readable_rsp_thm_eq internal_rsp_tm lthy'
kuncar@47308
   295
    val maybe_proven_rsp_thm = try_to_prove_refl readable_rsp_thm_eq
kuncar@47308
   296
    val (readable_rsp_tm, _) = Logic.dest_implies (prop_of readable_rsp_thm_eq)
kuncar@47607
   297
    val readable_rsp_tm_tnames = rename_to_tnames lthy' readable_rsp_tm
kuncar@47607
   298
kuncar@47308
   299
    fun after_qed thm_list lthy = 
kuncar@47308
   300
      let
kuncar@47308
   301
        val internal_rsp_thm =
kuncar@47308
   302
          case thm_list of
kuncar@47308
   303
            [] => the maybe_proven_rsp_thm
kuncar@47504
   304
          | [[thm]] => Goal.prove lthy [] [] internal_rsp_tm 
kuncar@47308
   305
            (fn _ => rtac readable_rsp_thm_eq 1 THEN Proof_Context.fact_tac [thm] 1)
kuncar@47308
   306
      in
kuncar@47308
   307
        add_lift_def (binding, mx) qty rhs internal_rsp_thm lthy
kuncar@47308
   308
      end
kuncar@47308
   309
kuncar@47308
   310
  in
kuncar@47308
   311
    case maybe_proven_rsp_thm of
kuncar@47504
   312
      SOME _ => Proof.theorem NONE after_qed [] lthy'
kuncar@47607
   313
      | NONE =>  Proof.theorem NONE after_qed [[(readable_rsp_tm_tnames,[])]] lthy'
kuncar@47308
   314
  end
kuncar@47308
   315
kuncar@47379
   316
fun quot_thm_err ctxt (rty, qty) pretty_msg =
kuncar@47379
   317
  let
kuncar@47379
   318
    val error_msg = cat_lines
kuncar@47379
   319
       ["Lifting failed for the following types:",
kuncar@47379
   320
        Pretty.string_of (Pretty.block
kuncar@47379
   321
         [Pretty.str "Raw type:", Pretty.brk 2, Syntax.pretty_typ ctxt rty]),
kuncar@47379
   322
        Pretty.string_of (Pretty.block
kuncar@47379
   323
         [Pretty.str "Abstract type:", Pretty.brk 2, Syntax.pretty_typ ctxt qty]),
kuncar@47379
   324
        "",
kuncar@47379
   325
        (Pretty.string_of (Pretty.block
kuncar@47379
   326
         [Pretty.str "Reason:", Pretty.brk 2, pretty_msg]))]
kuncar@47379
   327
  in
kuncar@47379
   328
    error error_msg
kuncar@47379
   329
  end
kuncar@47379
   330
kuncar@47504
   331
fun check_rty_err ctxt (rty_schematic, rty_forced) (raw_var, rhs_raw) =
kuncar@47379
   332
  let
kuncar@47504
   333
    val (_, ctxt') = yield_singleton Proof_Context.read_vars raw_var ctxt 
kuncar@47504
   334
    val rhs = (Syntax.check_term ctxt' o Syntax.parse_term ctxt') rhs_raw
kuncar@47379
   335
    val error_msg = cat_lines
kuncar@47379
   336
       ["Lifting failed for the following term:",
kuncar@47379
   337
        Pretty.string_of (Pretty.block
kuncar@47379
   338
         [Pretty.str "Term:", Pretty.brk 2, Syntax.pretty_term ctxt rhs]),
kuncar@47379
   339
        Pretty.string_of (Pretty.block
kuncar@47504
   340
         [Pretty.str "Type:", Pretty.brk 2, Syntax.pretty_typ ctxt rty_schematic]),
kuncar@47379
   341
        "",
kuncar@47379
   342
        (Pretty.string_of (Pretty.block
kuncar@47379
   343
         [Pretty.str "Reason:", 
kuncar@47379
   344
          Pretty.brk 2, 
kuncar@47379
   345
          Pretty.str "The type of the term cannot be instancied to",
kuncar@47379
   346
          Pretty.brk 1,
kuncar@47504
   347
          Pretty.quote (Syntax.pretty_typ ctxt rty_forced),
kuncar@47379
   348
          Pretty.str "."]))]
kuncar@47379
   349
    in
kuncar@47379
   350
      error error_msg
kuncar@47379
   351
    end
kuncar@47379
   352
kuncar@47379
   353
fun lift_def_cmd_with_err_handling (raw_var, rhs_raw) lthy =
kuncar@47379
   354
  (lift_def_cmd (raw_var, rhs_raw) lthy
kuncar@47379
   355
    handle Lifting_Term.QUOT_THM (rty, qty, msg) => quot_thm_err lthy (rty, qty) msg)
kuncar@47504
   356
    handle Lifting_Term.CHECK_RTY (rty_schematic, rty_forced) => 
kuncar@47504
   357
      check_rty_err lthy (rty_schematic, rty_forced) (raw_var, rhs_raw)
kuncar@47379
   358
kuncar@47308
   359
(* parser and command *)
kuncar@47308
   360
val liftdef_parser =
kuncar@47308
   361
  ((Parse.binding -- (@{keyword "::"} |-- (Parse.typ >> SOME) -- Parse.opt_mixfix')) >> Parse.triple2)
kuncar@47308
   362
    --| @{keyword "is"} -- Parse.term
kuncar@47308
   363
kuncar@47308
   364
val _ =
kuncar@47308
   365
  Outer_Syntax.local_theory_to_proof @{command_spec "lift_definition"}
kuncar@47308
   366
    "definition for constants over the quotient type"
kuncar@47379
   367
      (liftdef_parser >> lift_def_cmd_with_err_handling)
kuncar@47308
   368
kuncar@47308
   369
kuncar@47308
   370
end; (* structure *)