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