src/HOL/Tools/Lifting/lifting_def.ML
author kuncar
Tue May 29 19:13:02 2012 +0200 (2012-05-29)
changeset 48024 7599b28b707f
parent 47982 7aa35601ff65
child 49625 06cf80661e7a
permissions -rw-r--r--
don't be so aggressive during unfolding id and o
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@47698
    21
open Lifting_Util
kuncar@47308
    22
kuncar@47308
    23
infix 0 MRSL
kuncar@47308
    24
kuncar@47698
    25
(* Generation of the code certificate from the rsp theorem *)
kuncar@47308
    26
kuncar@47308
    27
fun get_body_types (Type ("fun", [_, U]), Type ("fun", [_, V])) = get_body_types (U, V)
kuncar@47308
    28
  | get_body_types (U, V)  = (U, V)
kuncar@47308
    29
kuncar@47308
    30
fun get_binder_types (Type ("fun", [T, U]), Type ("fun", [V, W])) = (T, V) :: get_binder_types (U, W)
kuncar@47308
    31
  | get_binder_types _ = []
kuncar@47308
    32
kuncar@47937
    33
fun get_binder_types_by_rel (Const (@{const_name "fun_rel"}, _) $ _ $ S) (Type ("fun", [T, U]), Type ("fun", [V, W])) = 
kuncar@47937
    34
    (T, V) :: get_binder_types_by_rel S (U, W)
kuncar@47937
    35
  | get_binder_types_by_rel _ _ = []
kuncar@47937
    36
kuncar@47937
    37
fun get_body_type_by_rel (Const (@{const_name "fun_rel"}, _) $ _ $ S) (Type ("fun", [_, U]), Type ("fun", [_, V])) = 
kuncar@47937
    38
    get_body_type_by_rel S (U, V)
kuncar@47937
    39
  | get_body_type_by_rel _ (U, V)  = (U, V)
kuncar@47937
    40
kuncar@47308
    41
fun force_rty_type ctxt rty rhs = 
kuncar@47308
    42
  let
kuncar@47308
    43
    val thy = Proof_Context.theory_of ctxt
kuncar@47308
    44
    val rhs_schematic = singleton (Variable.polymorphic ctxt) rhs
kuncar@47308
    45
    val rty_schematic = fastype_of rhs_schematic
kuncar@47308
    46
    val match = Sign.typ_match thy (rty_schematic, rty) Vartab.empty
kuncar@47308
    47
  in
kuncar@47308
    48
    Envir.subst_term_types match rhs_schematic
kuncar@47308
    49
  end
kuncar@47308
    50
kuncar@47308
    51
fun unabs_def ctxt def = 
kuncar@47308
    52
  let
kuncar@47308
    53
    val (_, rhs) = Thm.dest_equals (cprop_of def)
kuncar@47308
    54
    fun dest_abs (Abs (var_name, T, _)) = (var_name, T)
kuncar@47308
    55
      | dest_abs tm = raise TERM("get_abs_var",[tm])
kuncar@47308
    56
    val (var_name, T) = dest_abs (term_of rhs)
kuncar@47308
    57
    val (new_var_names, ctxt') = Variable.variant_fixes [var_name] ctxt
kuncar@47308
    58
    val thy = Proof_Context.theory_of ctxt'
kuncar@47308
    59
    val refl_thm = Thm.reflexive (cterm_of thy (Free (hd new_var_names, T)))
kuncar@47308
    60
  in
kuncar@47308
    61
    Thm.combination def refl_thm |>
kuncar@47308
    62
    singleton (Proof_Context.export ctxt' ctxt)
kuncar@47308
    63
  end
kuncar@47308
    64
kuncar@47308
    65
fun unabs_all_def ctxt def = 
kuncar@47308
    66
  let
kuncar@47308
    67
    val (_, rhs) = Thm.dest_equals (cprop_of def)
kuncar@47308
    68
    val xs = strip_abs_vars (term_of rhs)
kuncar@47308
    69
  in  
kuncar@47308
    70
    fold (K (unabs_def ctxt)) xs def
kuncar@47308
    71
  end
kuncar@47308
    72
kuncar@47308
    73
val map_fun_unfolded = 
kuncar@47308
    74
  @{thm map_fun_def[abs_def]} |>
kuncar@47308
    75
  unabs_def @{context} |>
kuncar@47308
    76
  unabs_def @{context} |>
kuncar@47308
    77
  Local_Defs.unfold @{context} [@{thm comp_def}]
kuncar@47308
    78
kuncar@47308
    79
fun unfold_fun_maps ctm =
kuncar@47308
    80
  let
kuncar@47308
    81
    fun unfold_conv ctm =
kuncar@47308
    82
      case (Thm.term_of ctm) of
kuncar@47308
    83
        Const (@{const_name "map_fun"}, _) $ _ $ _ => 
kuncar@47308
    84
          (Conv.arg_conv unfold_conv then_conv Conv.rewr_conv map_fun_unfolded) ctm
kuncar@47308
    85
        | _ => Conv.all_conv ctm
kuncar@47308
    86
  in
kuncar@47937
    87
    (Conv.fun_conv unfold_conv) ctm
kuncar@47937
    88
  end
kuncar@47937
    89
kuncar@47937
    90
fun unfold_fun_maps_beta ctm =
kuncar@47937
    91
  let val try_beta_conv = Conv.try_conv (Thm.beta_conversion false)
kuncar@47937
    92
  in 
kuncar@47937
    93
    (unfold_fun_maps then_conv try_beta_conv) ctm 
kuncar@47308
    94
  end
kuncar@47308
    95
kuncar@47308
    96
fun prove_rel ctxt rsp_thm (rty, qty) =
kuncar@47308
    97
  let
kuncar@47308
    98
    val ty_args = get_binder_types (rty, qty)
kuncar@47308
    99
    fun disch_arg args_ty thm = 
kuncar@47308
   100
      let
kuncar@47504
   101
        val quot_thm = Lifting_Term.prove_quot_thm ctxt args_ty
kuncar@47308
   102
      in
kuncar@47308
   103
        [quot_thm, thm] MRSL @{thm apply_rsp''}
kuncar@47308
   104
      end
kuncar@47308
   105
  in
kuncar@47308
   106
    fold disch_arg ty_args rsp_thm
kuncar@47308
   107
  end
kuncar@47308
   108
kuncar@47308
   109
exception CODE_CERT_GEN of string
kuncar@47308
   110
kuncar@47308
   111
fun simplify_code_eq ctxt def_thm = 
kuncar@48024
   112
  Local_Defs.unfold ctxt [@{thm o_apply}, @{thm map_fun_def}, @{thm id_apply}] def_thm
kuncar@47308
   113
kuncar@47852
   114
(*
kuncar@47852
   115
  quot_thm - quotient theorem (Quotient R Abs Rep T).
kuncar@47852
   116
  returns: whether the Lifting package is capable to generate code for the abstract type
kuncar@47852
   117
    represented by quot_thm
kuncar@47852
   118
*)
kuncar@47852
   119
kuncar@47308
   120
fun can_generate_code_cert quot_thm  =
kuncar@47951
   121
  case quot_thm_rel quot_thm of
kuncar@47308
   122
    Const (@{const_name HOL.eq}, _) => true
kuncar@47308
   123
    | Const (@{const_name invariant}, _) $ _  => true
kuncar@47308
   124
    | _ => false
kuncar@47308
   125
kuncar@47308
   126
fun generate_code_cert ctxt def_thm rsp_thm (rty, qty) =
kuncar@47308
   127
  let
kuncar@47308
   128
    val thy = Proof_Context.theory_of ctxt
kuncar@47504
   129
    val quot_thm = Lifting_Term.prove_quot_thm ctxt (get_body_types (rty, qty))
kuncar@47308
   130
    val fun_rel = prove_rel ctxt rsp_thm (rty, qty)
kuncar@47308
   131
    val abs_rep_thm = [quot_thm, fun_rel] MRSL @{thm Quotient_rep_abs}
kuncar@47308
   132
    val abs_rep_eq = 
kuncar@47308
   133
      case (HOLogic.dest_Trueprop o prop_of) fun_rel of
kuncar@47308
   134
        Const (@{const_name HOL.eq}, _) $ _ $ _ => abs_rep_thm
kuncar@47308
   135
        | Const (@{const_name invariant}, _) $ _ $ _ $ _ => abs_rep_thm RS @{thm invariant_to_eq}
kuncar@47308
   136
        | _ => raise CODE_CERT_GEN "relation is neither equality nor invariant"
kuncar@47937
   137
    val unfolded_def = Conv.fconv_rule (Conv.arg_conv unfold_fun_maps_beta) def_thm
kuncar@47308
   138
    val unabs_def = unabs_all_def ctxt unfolded_def
kuncar@47951
   139
    val rep = (cterm_of thy o quot_thm_rep) quot_thm
kuncar@47308
   140
    val rep_refl = Thm.reflexive rep RS @{thm meta_eq_to_obj_eq}
kuncar@47308
   141
    val repped_eq = [rep_refl, unabs_def RS @{thm meta_eq_to_obj_eq}] MRSL @{thm cong}
kuncar@47308
   142
    val code_cert = [repped_eq, abs_rep_eq] MRSL @{thm trans}
kuncar@47308
   143
  in
kuncar@47308
   144
    simplify_code_eq ctxt code_cert
kuncar@47308
   145
  end
kuncar@47308
   146
kuncar@47937
   147
fun generate_trivial_rep_eq ctxt def_thm =
kuncar@47566
   148
  let
kuncar@47937
   149
    val unfolded_def = Conv.fconv_rule (Conv.arg_conv unfold_fun_maps_beta) def_thm
kuncar@47937
   150
    val code_eq = unabs_all_def ctxt unfolded_def
kuncar@47937
   151
    val simp_code_eq = simplify_code_eq ctxt code_eq
kuncar@47566
   152
  in
kuncar@47937
   153
    simp_code_eq
kuncar@47566
   154
  end
kuncar@47937
   155
kuncar@47937
   156
fun generate_rep_eq ctxt def_thm rsp_thm (rty, qty) =
kuncar@47937
   157
  if body_type rty = body_type qty then 
kuncar@47937
   158
    SOME (generate_trivial_rep_eq ctxt def_thm)
kuncar@47937
   159
  else 
kuncar@47937
   160
    let
kuncar@47937
   161
      val (rty_body, qty_body) = get_body_types (rty, qty)
kuncar@47937
   162
      val quot_thm = Lifting_Term.prove_quot_thm ctxt (rty_body, qty_body)
kuncar@47937
   163
    in
kuncar@47937
   164
      if can_generate_code_cert quot_thm then
kuncar@47937
   165
        SOME (generate_code_cert ctxt def_thm rsp_thm (rty, qty))
kuncar@47937
   166
      else 
kuncar@47937
   167
        NONE
kuncar@47937
   168
    end
kuncar@47566
   169
kuncar@47937
   170
fun generate_abs_eq ctxt def_thm rsp_thm quot_thm =
kuncar@47308
   171
  let
kuncar@47937
   172
    fun refl_tac ctxt =
kuncar@47937
   173
      let
kuncar@47937
   174
        fun intro_reflp_tac (t, i) = 
kuncar@47937
   175
        let
kuncar@47937
   176
          val concl_pat = Drule.strip_imp_concl (cprop_of @{thm reflpD})
kuncar@47937
   177
          val insts = Thm.first_order_match (concl_pat, t)
kuncar@47937
   178
        in
kuncar@47937
   179
          rtac (Drule.instantiate_normalize insts @{thm reflpD}) i
kuncar@47937
   180
        end
kuncar@47937
   181
        handle Pattern.MATCH => no_tac
kuncar@47937
   182
        
kuncar@47937
   183
        val fun_rel_meta_eq = mk_meta_eq @{thm fun_rel_eq}
kuncar@47937
   184
        val conv = Conv.bottom_conv (K (Conv.try_conv (Conv.rewr_conv fun_rel_meta_eq))) ctxt
kuncar@47982
   185
        val rules = Lifting_Info.get_reflexivity_rules ctxt
kuncar@47937
   186
      in
kuncar@47937
   187
        EVERY' [CSUBGOAL intro_reflp_tac, 
kuncar@47937
   188
                CONVERSION conv,
kuncar@47937
   189
                REPEAT_ALL_NEW (resolve_tac rules)]
kuncar@47937
   190
      end
kuncar@47937
   191
    
kuncar@47937
   192
    fun try_prove_prem ctxt prop =
kuncar@47937
   193
      SOME (Goal.prove ctxt [] [] prop (fn {context, ...} => refl_tac context 1))
kuncar@47937
   194
        handle ERROR _ => NONE
kuncar@47937
   195
kuncar@47937
   196
    val abs_eq_with_assms =
kuncar@47937
   197
      let
kuncar@47951
   198
        val (rty, qty) = quot_thm_rty_qty quot_thm
kuncar@47951
   199
        val rel = quot_thm_rel quot_thm
kuncar@47937
   200
        val ty_args = get_binder_types_by_rel rel (rty, qty)
kuncar@47937
   201
        val body_type = get_body_type_by_rel rel (rty, qty)
kuncar@47937
   202
        val quot_ret_thm = Lifting_Term.prove_quot_thm ctxt body_type
kuncar@47937
   203
        
kuncar@47937
   204
        val rep_abs_folded_unmapped_thm = 
kuncar@47937
   205
          let
kuncar@47937
   206
            val rep_id = [quot_thm, def_thm] MRSL @{thm Quotient_Rep_eq}
kuncar@47937
   207
            val ctm = Thm.dest_equals_lhs (cprop_of rep_id)
kuncar@47937
   208
            val unfolded_maps_eq = unfold_fun_maps ctm
kuncar@47937
   209
            val t1 = [quot_thm, def_thm, rsp_thm] MRSL @{thm Quotient_rep_abs_fold_unmap}
kuncar@47937
   210
            val prems_pat = (hd o Drule.cprems_of) t1
kuncar@47937
   211
            val insts = Thm.first_order_match (prems_pat, cprop_of unfolded_maps_eq)
kuncar@47937
   212
          in
kuncar@47937
   213
            unfolded_maps_eq RS (Drule.instantiate_normalize insts t1)
kuncar@47937
   214
          end
kuncar@47937
   215
      in
kuncar@47937
   216
        rep_abs_folded_unmapped_thm
kuncar@47937
   217
        |> fold (fn _ => fn thm => thm RS @{thm fun_relD2}) ty_args
kuncar@47937
   218
        |> (fn x => x RS (@{thm Quotient_rel_abs2} OF [quot_ret_thm]))
kuncar@47937
   219
      end
kuncar@47937
   220
    
kuncar@47937
   221
    val prems = prems_of abs_eq_with_assms
kuncar@47937
   222
    val indexed_prems = map_index (apfst (fn x => x + 1)) prems
kuncar@47937
   223
    val indexed_assms = map (apsnd (try_prove_prem ctxt)) indexed_prems
kuncar@47937
   224
    val proved_assms = map (apsnd the) (filter (is_some o snd) indexed_assms)
kuncar@47937
   225
    val abs_eq = fold_rev (fn (i, assms) => fn thm => assms RSN (i, thm)) proved_assms abs_eq_with_assms
kuncar@47308
   226
  in
kuncar@47937
   227
    simplify_code_eq ctxt abs_eq
kuncar@47937
   228
  end
kuncar@47937
   229
kuncar@47937
   230
fun define_code_using_abs_eq abs_eq_thm lthy =
kuncar@47937
   231
  if null (Logic.strip_imp_prems(prop_of abs_eq_thm)) then
kuncar@47937
   232
    (snd oo Local_Theory.note) ((Binding.empty, [Code.add_default_eqn_attrib]), [abs_eq_thm]) lthy
kuncar@47937
   233
  else
kuncar@47937
   234
    lthy
kuncar@47937
   235
  
kuncar@47937
   236
fun define_code_using_rep_eq maybe_rep_eq_thm lthy = 
kuncar@47937
   237
  case maybe_rep_eq_thm of
kuncar@47937
   238
    SOME rep_eq_thm =>   
kuncar@47308
   239
      let
kuncar@47308
   240
        val add_abs_eqn_attribute = 
kuncar@47308
   241
          Thm.declaration_attribute (fn thm => Context.mapping (Code.add_abs_eqn thm) I)
kuncar@47308
   242
        val add_abs_eqn_attrib = Attrib.internal (K add_abs_eqn_attribute);
kuncar@47308
   243
      in
kuncar@47937
   244
        (snd oo Local_Theory.note) ((Binding.empty, [add_abs_eqn_attrib]), [rep_eq_thm]) lthy
kuncar@47308
   245
      end
kuncar@47937
   246
    | NONE => lthy
kuncar@47937
   247
kuncar@47937
   248
fun has_constr ctxt quot_thm =
kuncar@47937
   249
  let
kuncar@47937
   250
    val thy = Proof_Context.theory_of ctxt
kuncar@47951
   251
    val abs_fun = quot_thm_abs quot_thm
kuncar@47937
   252
  in
kuncar@47937
   253
    if is_Const abs_fun then
kuncar@47937
   254
      Code.is_constr thy ((fst o dest_Const) abs_fun)
kuncar@47308
   255
    else
kuncar@47937
   256
      false
kuncar@47308
   257
  end
kuncar@47308
   258
kuncar@47937
   259
fun has_abstr ctxt quot_thm =
kuncar@47308
   260
  let
kuncar@47937
   261
    val thy = Proof_Context.theory_of ctxt
kuncar@47951
   262
    val abs_fun = quot_thm_abs quot_thm
kuncar@47308
   263
  in
kuncar@47937
   264
    if is_Const abs_fun then
kuncar@47937
   265
      Code.is_abstr thy ((fst o dest_Const) abs_fun)
kuncar@47937
   266
    else
kuncar@47937
   267
      false
kuncar@47308
   268
  end
kuncar@47308
   269
kuncar@47937
   270
fun define_code abs_eq_thm maybe_rep_eq_thm (rty, qty) lthy =
kuncar@47937
   271
  let
kuncar@47937
   272
    val (rty_body, qty_body) = get_body_types (rty, qty)
kuncar@47937
   273
  in
kuncar@47937
   274
    if rty_body = qty_body then
kuncar@47937
   275
      if null (Logic.strip_imp_prems(prop_of abs_eq_thm)) then
kuncar@47937
   276
        (snd oo Local_Theory.note) ((Binding.empty, [Code.add_default_eqn_attrib]), [abs_eq_thm]) lthy
kuncar@47937
   277
      else
kuncar@47937
   278
        (snd oo Local_Theory.note) ((Binding.empty, [Code.add_default_eqn_attrib]), [the maybe_rep_eq_thm]) lthy
kuncar@47937
   279
    else
kuncar@47937
   280
      let 
kuncar@47937
   281
        val body_quot_thm = Lifting_Term.prove_quot_thm lthy (rty_body, qty_body)
kuncar@47937
   282
      in
kuncar@47937
   283
        if has_constr lthy body_quot_thm then
kuncar@47937
   284
          define_code_using_abs_eq abs_eq_thm lthy
kuncar@47937
   285
        else if has_abstr lthy body_quot_thm then
kuncar@47937
   286
          define_code_using_rep_eq maybe_rep_eq_thm lthy
kuncar@47937
   287
        else
kuncar@47937
   288
          lthy
kuncar@47937
   289
      end
kuncar@47937
   290
  end
kuncar@47308
   291
kuncar@47852
   292
(*
kuncar@47852
   293
  Defines an operation on an abstract type in terms of a corresponding operation 
kuncar@47852
   294
    on a representation type.
kuncar@47852
   295
kuncar@47852
   296
  var - a binding and a mixfix of the new constant being defined
kuncar@47852
   297
  qty - an abstract type of the new constant
kuncar@47852
   298
  rhs - a term representing the new constant on the raw level
kuncar@47937
   299
  rsp_thm - a respectfulness theorem in the internal tagged form (like '(R ===> R ===> R) f f'),
kuncar@47852
   300
    i.e. "(Lifting_Term.equiv_relation (fastype_of rhs, qty)) $ rhs $ rhs"
kuncar@47852
   301
*)
kuncar@47308
   302
kuncar@47308
   303
fun add_lift_def var qty rhs rsp_thm lthy =
kuncar@47308
   304
  let
kuncar@47308
   305
    val rty = fastype_of rhs
kuncar@47937
   306
    val quot_thm = Lifting_Term.prove_quot_thm lthy (rty, qty)
kuncar@47951
   307
    val absrep_trm =  quot_thm_abs quot_thm
kuncar@47308
   308
    val rty_forced = (domain_type o fastype_of) absrep_trm
kuncar@47308
   309
    val forced_rhs = force_rty_type lthy rty_forced rhs
kuncar@47308
   310
    val lhs = Free (Binding.print (#1 var), qty)
kuncar@47308
   311
    val prop = Logic.mk_equals (lhs, absrep_trm $ forced_rhs)
kuncar@47308
   312
    val (_, prop') = Local_Defs.cert_def lthy prop
kuncar@47308
   313
    val (_, newrhs) = Local_Defs.abs_def prop'
kuncar@47308
   314
kuncar@47308
   315
    val ((_, (_ , def_thm)), lthy') = 
kuncar@47308
   316
      Local_Theory.define (var, ((Thm.def_binding (#1 var), []), newrhs)) lthy
kuncar@47308
   317
kuncar@47937
   318
    val transfer_thm = ([quot_thm, rsp_thm, def_thm] MRSL @{thm Quotient_to_transfer})
huffman@47503
   319
        |> Raw_Simplifier.rewrite_rule (Transfer.get_relator_eq lthy')
kuncar@47937
   320
     
kuncar@47937
   321
    val abs_eq_thm = generate_abs_eq lthy' def_thm rsp_thm quot_thm
kuncar@47937
   322
    val maybe_rep_eq_thm = generate_rep_eq lthy' def_thm rsp_thm (rty_forced, qty)
huffman@47351
   323
kuncar@47545
   324
    fun qualify defname suffix = Binding.qualified true suffix defname
kuncar@47308
   325
kuncar@47545
   326
    val lhs_name = (#1 var)
kuncar@47308
   327
    val rsp_thm_name = qualify lhs_name "rsp"
kuncar@47937
   328
    val abs_eq_thm_name = qualify lhs_name "abs_eq"
kuncar@47937
   329
    val rep_eq_thm_name = qualify lhs_name "rep_eq"
huffman@47351
   330
    val transfer_thm_name = qualify lhs_name "transfer"
huffman@47373
   331
    val transfer_attr = Attrib.internal (K Transfer.transfer_add)
kuncar@47308
   332
  in
kuncar@47308
   333
    lthy'
kuncar@47308
   334
      |> (snd oo Local_Theory.note) ((rsp_thm_name, []), [rsp_thm])
huffman@47373
   335
      |> (snd oo Local_Theory.note) ((transfer_thm_name, [transfer_attr]), [transfer_thm])
kuncar@47937
   336
      |> (snd oo Local_Theory.note) ((abs_eq_thm_name, []), [abs_eq_thm])
kuncar@47937
   337
      |> (case maybe_rep_eq_thm of 
kuncar@47937
   338
            SOME rep_eq_thm => (snd oo Local_Theory.note) ((rep_eq_thm_name, []), [rep_eq_thm])
kuncar@47937
   339
            | NONE => I)
kuncar@47937
   340
      |> define_code abs_eq_thm maybe_rep_eq_thm (rty_forced, qty)
kuncar@47308
   341
  end
kuncar@47308
   342
kuncar@47308
   343
fun mk_readable_rsp_thm_eq tm lthy =
kuncar@47308
   344
  let
kuncar@47308
   345
    val ctm = cterm_of (Proof_Context.theory_of lthy) tm
kuncar@47308
   346
    
kuncar@47308
   347
    fun norm_fun_eq ctm = 
kuncar@47308
   348
      let
kuncar@47308
   349
        fun abs_conv2 cv = Conv.abs_conv (K (Conv.abs_conv (K cv) lthy)) lthy
kuncar@47308
   350
        fun erase_quants ctm' =
kuncar@47308
   351
          case (Thm.term_of ctm') of
kuncar@47308
   352
            Const ("HOL.eq", _) $ _ $ _ => Conv.all_conv ctm'
kuncar@47308
   353
            | _ => (Conv.binder_conv (K erase_quants) lthy then_conv 
kuncar@47308
   354
              Conv.rewr_conv @{thm fun_eq_iff[symmetric, THEN eq_reflection]}) ctm'
kuncar@47308
   355
      in
kuncar@47308
   356
        (abs_conv2 erase_quants then_conv Thm.eta_conversion) ctm
kuncar@47308
   357
      end
kuncar@47308
   358
kuncar@47308
   359
    fun simp_arrows_conv ctm =
kuncar@47308
   360
      let
kuncar@47308
   361
        val unfold_conv = Conv.rewrs_conv 
kuncar@47308
   362
          [@{thm fun_rel_eq_invariant[THEN eq_reflection]}, @{thm fun_rel_eq_rel[THEN eq_reflection]}, 
kuncar@47308
   363
            @{thm fun_rel_def[THEN eq_reflection]}]
kuncar@47308
   364
        val left_conv = simp_arrows_conv then_conv Conv.try_conv norm_fun_eq
kuncar@47308
   365
        fun binop_conv2 cv1 cv2 = Conv.combination_conv (Conv.arg_conv cv1) cv2
kuncar@47634
   366
        val invariant_commute_conv = Conv.bottom_conv
kuncar@47634
   367
          (K (Conv.try_conv (Conv.rewrs_conv (Lifting_Info.get_invariant_commute_rules lthy)))) lthy
kuncar@47308
   368
      in
kuncar@47308
   369
        case (Thm.term_of ctm) of
kuncar@47308
   370
          Const (@{const_name "fun_rel"}, _) $ _ $ _ => 
kuncar@47308
   371
            (binop_conv2  left_conv simp_arrows_conv then_conv unfold_conv) ctm
kuncar@47634
   372
          | _ => invariant_commute_conv ctm
kuncar@47308
   373
      end
kuncar@47937
   374
    
kuncar@47308
   375
    val unfold_ret_val_invs = Conv.bottom_conv 
kuncar@47308
   376
      (K (Conv.try_conv (Conv.rewr_conv @{thm invariant_same_args}))) lthy 
kuncar@47699
   377
    val simp_conv = Trueprop_conv (Conv.fun2_conv simp_arrows_conv)
kuncar@47308
   378
    val univq_conv = Conv.rewr_conv @{thm HOL.all_simps(6)[symmetric, THEN eq_reflection]}
kuncar@47308
   379
    val univq_prenex_conv = Conv.top_conv (K (Conv.try_conv univq_conv)) lthy
kuncar@47308
   380
    val beta_conv = Thm.beta_conversion true
kuncar@47308
   381
    val eq_thm = 
kuncar@47308
   382
      (simp_conv then_conv univq_prenex_conv then_conv beta_conv then_conv unfold_ret_val_invs) ctm
kuncar@47308
   383
  in
kuncar@47308
   384
    Object_Logic.rulify(eq_thm RS Drule.equal_elim_rule2)
kuncar@47308
   385
  end
kuncar@47308
   386
kuncar@47607
   387
fun rename_to_tnames ctxt term =
kuncar@47607
   388
  let
kuncar@47607
   389
    fun all_typs (Const ("all", _) $ Abs (_, T, t)) = T :: all_typs t
kuncar@47607
   390
      | all_typs _ = []
kuncar@47308
   391
kuncar@47607
   392
    fun rename (Const ("all", T1) $ Abs (_, T2, t)) (new_name :: names) = 
kuncar@47607
   393
        (Const ("all", T1) $ Abs (new_name, T2, rename t names)) 
kuncar@47607
   394
      | rename t _ = t
kuncar@47607
   395
kuncar@47607
   396
    val (fixed_def_t, _) = yield_singleton (Variable.importT_terms) term ctxt
kuncar@47607
   397
    val new_names = Datatype_Prop.make_tnames (all_typs fixed_def_t)
kuncar@47607
   398
  in
kuncar@47607
   399
    rename term new_names
kuncar@47607
   400
  end
kuncar@47308
   401
kuncar@47852
   402
(*
kuncar@47852
   403
kuncar@47852
   404
  lifting_definition command. It opens a proof of a corresponding respectfulness 
kuncar@47852
   405
  theorem in a user-friendly, readable form. Then add_lift_def is called internally.
kuncar@47852
   406
kuncar@47852
   407
*)
kuncar@47852
   408
kuncar@47308
   409
fun lift_def_cmd (raw_var, rhs_raw) lthy =
kuncar@47308
   410
  let
kuncar@47504
   411
    val ((binding, SOME qty, mx), lthy') = yield_singleton Proof_Context.read_vars raw_var lthy 
kuncar@47504
   412
    val rhs = (Syntax.check_term lthy' o Syntax.parse_term lthy') rhs_raw
kuncar@47308
   413
 
kuncar@47308
   414
    fun try_to_prove_refl thm = 
kuncar@47308
   415
      let
kuncar@47308
   416
        val lhs_eq =
kuncar@47308
   417
          thm
kuncar@47308
   418
          |> prop_of
kuncar@47308
   419
          |> Logic.dest_implies
kuncar@47308
   420
          |> fst
kuncar@47308
   421
          |> strip_all_body
kuncar@47308
   422
          |> try HOLogic.dest_Trueprop
kuncar@47308
   423
      in
kuncar@47308
   424
        case lhs_eq of
kuncar@47308
   425
          SOME (Const ("HOL.eq", _) $ _ $ _) => SOME (@{thm refl} RS thm)
kuncar@47308
   426
          | _ => NONE
kuncar@47308
   427
      end
kuncar@47308
   428
kuncar@47504
   429
    val rsp_rel = Lifting_Term.equiv_relation lthy' (fastype_of rhs, qty)
kuncar@47308
   430
    val rty_forced = (domain_type o fastype_of) rsp_rel;
kuncar@47504
   431
    val forced_rhs = force_rty_type lthy' rty_forced rhs;
kuncar@47308
   432
    val internal_rsp_tm = HOLogic.mk_Trueprop (rsp_rel $ forced_rhs $ forced_rhs)
kuncar@47504
   433
    val readable_rsp_thm_eq = mk_readable_rsp_thm_eq internal_rsp_tm lthy'
kuncar@47308
   434
    val maybe_proven_rsp_thm = try_to_prove_refl readable_rsp_thm_eq
kuncar@47308
   435
    val (readable_rsp_tm, _) = Logic.dest_implies (prop_of readable_rsp_thm_eq)
kuncar@47607
   436
    val readable_rsp_tm_tnames = rename_to_tnames lthy' readable_rsp_tm
kuncar@47607
   437
kuncar@47308
   438
    fun after_qed thm_list lthy = 
kuncar@47308
   439
      let
kuncar@47308
   440
        val internal_rsp_thm =
kuncar@47308
   441
          case thm_list of
kuncar@47308
   442
            [] => the maybe_proven_rsp_thm
kuncar@47504
   443
          | [[thm]] => Goal.prove lthy [] [] internal_rsp_tm 
kuncar@47308
   444
            (fn _ => rtac readable_rsp_thm_eq 1 THEN Proof_Context.fact_tac [thm] 1)
kuncar@47308
   445
      in
kuncar@47308
   446
        add_lift_def (binding, mx) qty rhs internal_rsp_thm lthy
kuncar@47308
   447
      end
kuncar@47308
   448
kuncar@47308
   449
  in
kuncar@47308
   450
    case maybe_proven_rsp_thm of
kuncar@47504
   451
      SOME _ => Proof.theorem NONE after_qed [] lthy'
kuncar@47607
   452
      | NONE =>  Proof.theorem NONE after_qed [[(readable_rsp_tm_tnames,[])]] lthy'
kuncar@47308
   453
  end
kuncar@47308
   454
kuncar@47379
   455
fun quot_thm_err ctxt (rty, qty) pretty_msg =
kuncar@47379
   456
  let
kuncar@47379
   457
    val error_msg = cat_lines
kuncar@47379
   458
       ["Lifting failed for the following types:",
kuncar@47379
   459
        Pretty.string_of (Pretty.block
kuncar@47379
   460
         [Pretty.str "Raw type:", Pretty.brk 2, Syntax.pretty_typ ctxt rty]),
kuncar@47379
   461
        Pretty.string_of (Pretty.block
kuncar@47379
   462
         [Pretty.str "Abstract type:", Pretty.brk 2, Syntax.pretty_typ ctxt qty]),
kuncar@47379
   463
        "",
kuncar@47379
   464
        (Pretty.string_of (Pretty.block
kuncar@47379
   465
         [Pretty.str "Reason:", Pretty.brk 2, pretty_msg]))]
kuncar@47379
   466
  in
kuncar@47379
   467
    error error_msg
kuncar@47379
   468
  end
kuncar@47379
   469
kuncar@47504
   470
fun check_rty_err ctxt (rty_schematic, rty_forced) (raw_var, rhs_raw) =
kuncar@47379
   471
  let
kuncar@47504
   472
    val (_, ctxt') = yield_singleton Proof_Context.read_vars raw_var ctxt 
kuncar@47504
   473
    val rhs = (Syntax.check_term ctxt' o Syntax.parse_term ctxt') rhs_raw
kuncar@47379
   474
    val error_msg = cat_lines
kuncar@47379
   475
       ["Lifting failed for the following term:",
kuncar@47379
   476
        Pretty.string_of (Pretty.block
kuncar@47379
   477
         [Pretty.str "Term:", Pretty.brk 2, Syntax.pretty_term ctxt rhs]),
kuncar@47379
   478
        Pretty.string_of (Pretty.block
kuncar@47504
   479
         [Pretty.str "Type:", Pretty.brk 2, Syntax.pretty_typ ctxt rty_schematic]),
kuncar@47379
   480
        "",
kuncar@47379
   481
        (Pretty.string_of (Pretty.block
kuncar@47379
   482
         [Pretty.str "Reason:", 
kuncar@47379
   483
          Pretty.brk 2, 
kuncar@47379
   484
          Pretty.str "The type of the term cannot be instancied to",
kuncar@47379
   485
          Pretty.brk 1,
kuncar@47504
   486
          Pretty.quote (Syntax.pretty_typ ctxt rty_forced),
kuncar@47379
   487
          Pretty.str "."]))]
kuncar@47379
   488
    in
kuncar@47379
   489
      error error_msg
kuncar@47379
   490
    end
kuncar@47379
   491
kuncar@47379
   492
fun lift_def_cmd_with_err_handling (raw_var, rhs_raw) lthy =
kuncar@47379
   493
  (lift_def_cmd (raw_var, rhs_raw) lthy
kuncar@47379
   494
    handle Lifting_Term.QUOT_THM (rty, qty, msg) => quot_thm_err lthy (rty, qty) msg)
kuncar@47504
   495
    handle Lifting_Term.CHECK_RTY (rty_schematic, rty_forced) => 
kuncar@47504
   496
      check_rty_err lthy (rty_schematic, rty_forced) (raw_var, rhs_raw)
kuncar@47379
   497
kuncar@47308
   498
(* parser and command *)
kuncar@47308
   499
val liftdef_parser =
kuncar@47308
   500
  ((Parse.binding -- (@{keyword "::"} |-- (Parse.typ >> SOME) -- Parse.opt_mixfix')) >> Parse.triple2)
kuncar@47308
   501
    --| @{keyword "is"} -- Parse.term
kuncar@47308
   502
kuncar@47308
   503
val _ =
kuncar@47308
   504
  Outer_Syntax.local_theory_to_proof @{command_spec "lift_definition"}
kuncar@47308
   505
    "definition for constants over the quotient type"
kuncar@47379
   506
      (liftdef_parser >> lift_def_cmd_with_err_handling)
kuncar@47308
   507
kuncar@47308
   508
kuncar@47308
   509
end; (* structure *)