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