src/HOL/Tools/Lifting/lifting_def.ML
author kuncar
Tue, 03 Apr 2012 16:26:48 +0200
changeset 47308 9caab698dbe4
child 47351 0193e663a19e
permissions -rw-r--r--
new package Lifting - initial commit
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
     1
(*  Title:      HOL/Tools/Lifting/lifting_def.ML
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
     2
    Author:     Ondrej Kuncar
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
     3
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
     4
Definitions for constants on quotient types.
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
     5
*)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
     6
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
     7
signature LIFTING_DEF =
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
     8
sig
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
     9
  val add_lift_def:
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    10
    (binding * mixfix) -> typ -> term -> thm -> local_theory -> local_theory
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    11
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    12
  val lift_def_cmd:
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    13
    (binding * string option * mixfix) * string -> local_theory -> Proof.state
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    14
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    15
  val can_generate_code_cert: thm -> bool
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    16
end;
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    17
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    18
structure Lifting_Def: LIFTING_DEF =
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    19
struct
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    20
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    21
(** Interface and Syntax Setup **)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    22
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    23
(* Generation of the code certificate from the rsp theorem *)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    24
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    25
infix 0 MRSL
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    26
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    27
fun ants MRSL thm = fold (fn rl => fn thm => rl RS thm) ants thm
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    28
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    29
fun get_body_types (Type ("fun", [_, U]), Type ("fun", [_, V])) = get_body_types (U, V)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    30
  | get_body_types (U, V)  = (U, V)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    31
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    32
fun get_binder_types (Type ("fun", [T, U]), Type ("fun", [V, W])) = (T, V) :: get_binder_types (U, W)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    33
  | get_binder_types _ = []
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    34
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    35
fun force_rty_type ctxt rty rhs = 
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    36
  let
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    37
    val thy = Proof_Context.theory_of ctxt
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    38
    val rhs_schematic = singleton (Variable.polymorphic ctxt) rhs
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    39
    val rty_schematic = fastype_of rhs_schematic
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    40
    val match = Sign.typ_match thy (rty_schematic, rty) Vartab.empty
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    41
  in
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    42
    Envir.subst_term_types match rhs_schematic
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    43
  end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    44
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    45
fun unabs_def ctxt def = 
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    46
  let
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    47
    val (_, rhs) = Thm.dest_equals (cprop_of def)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    48
    fun dest_abs (Abs (var_name, T, _)) = (var_name, T)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    49
      | dest_abs tm = raise TERM("get_abs_var",[tm])
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    50
    val (var_name, T) = dest_abs (term_of rhs)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    51
    val (new_var_names, ctxt') = Variable.variant_fixes [var_name] ctxt
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    52
    val thy = Proof_Context.theory_of ctxt'
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    53
    val refl_thm = Thm.reflexive (cterm_of thy (Free (hd new_var_names, T)))
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    54
  in
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    55
    Thm.combination def refl_thm |>
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    56
    singleton (Proof_Context.export ctxt' ctxt)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    57
  end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    58
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    59
fun unabs_all_def ctxt def = 
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    60
  let
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    61
    val (_, rhs) = Thm.dest_equals (cprop_of def)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    62
    val xs = strip_abs_vars (term_of rhs)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    63
  in  
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    64
    fold (K (unabs_def ctxt)) xs def
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    65
  end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    66
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    67
val map_fun_unfolded = 
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    68
  @{thm map_fun_def[abs_def]} |>
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    69
  unabs_def @{context} |>
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    70
  unabs_def @{context} |>
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    71
  Local_Defs.unfold @{context} [@{thm comp_def}]
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    72
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    73
fun unfold_fun_maps ctm =
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    74
  let
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    75
    fun unfold_conv ctm =
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    76
      case (Thm.term_of ctm) of
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    77
        Const (@{const_name "map_fun"}, _) $ _ $ _ => 
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    78
          (Conv.arg_conv unfold_conv then_conv Conv.rewr_conv map_fun_unfolded) ctm
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    79
        | _ => Conv.all_conv ctm
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    80
    val try_beta_conv = Conv.try_conv (Thm.beta_conversion false)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    81
  in
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    82
    (Conv.arg_conv (Conv.fun_conv unfold_conv then_conv try_beta_conv)) ctm
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    83
  end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    84
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    85
fun prove_rel ctxt rsp_thm (rty, qty) =
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    86
  let
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    87
    val ty_args = get_binder_types (rty, qty)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    88
    fun disch_arg args_ty thm = 
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    89
      let
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    90
        val quot_thm = Lifting_Term.prove_quot_theorem ctxt args_ty
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    91
      in
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    92
        [quot_thm, thm] MRSL @{thm apply_rsp''}
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    93
      end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    94
  in
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    95
    fold disch_arg ty_args rsp_thm
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    96
  end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    97
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    98
exception CODE_CERT_GEN of string
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    99
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   100
fun simplify_code_eq ctxt def_thm = 
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   101
  Local_Defs.unfold ctxt [@{thm o_def}, @{thm map_fun_def}, @{thm id_def}] def_thm
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   102
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   103
fun can_generate_code_cert quot_thm  =
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   104
  case Lifting_Term.quot_thm_rel quot_thm of
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   105
    Const (@{const_name HOL.eq}, _) => true
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   106
    | Const (@{const_name invariant}, _) $ _  => true
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   107
    | _ => false
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   108
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   109
fun generate_code_cert ctxt def_thm rsp_thm (rty, qty) =
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   110
  let
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   111
    val thy = Proof_Context.theory_of ctxt
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   112
    val quot_thm = Lifting_Term.prove_quot_theorem ctxt (get_body_types (rty, qty))
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   113
    val fun_rel = prove_rel ctxt rsp_thm (rty, qty)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   114
    val abs_rep_thm = [quot_thm, fun_rel] MRSL @{thm Quotient_rep_abs}
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   115
    val abs_rep_eq = 
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   116
      case (HOLogic.dest_Trueprop o prop_of) fun_rel of
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   117
        Const (@{const_name HOL.eq}, _) $ _ $ _ => abs_rep_thm
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   118
        | Const (@{const_name invariant}, _) $ _ $ _ $ _ => abs_rep_thm RS @{thm invariant_to_eq}
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   119
        | _ => raise CODE_CERT_GEN "relation is neither equality nor invariant"
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   120
    val unfolded_def = Conv.fconv_rule unfold_fun_maps def_thm
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   121
    val unabs_def = unabs_all_def ctxt unfolded_def
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   122
    val rep = (cterm_of thy o Lifting_Term.quot_thm_rep) quot_thm
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   123
    val rep_refl = Thm.reflexive rep RS @{thm meta_eq_to_obj_eq}
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   124
    val repped_eq = [rep_refl, unabs_def RS @{thm meta_eq_to_obj_eq}] MRSL @{thm cong}
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   125
    val code_cert = [repped_eq, abs_rep_eq] MRSL @{thm trans}
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   126
  in
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   127
    simplify_code_eq ctxt code_cert
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   128
  end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   129
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   130
fun define_code_cert code_eqn_thm_name def_thm rsp_thm (rty, qty) lthy = 
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   131
  let
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   132
    val quot_thm = Lifting_Term.prove_quot_theorem lthy (get_body_types (rty, qty))
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   133
  in
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   134
    if can_generate_code_cert quot_thm then
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   135
      let
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   136
        val code_cert = generate_code_cert lthy def_thm rsp_thm (rty, qty)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   137
        val add_abs_eqn_attribute = 
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   138
          Thm.declaration_attribute (fn thm => Context.mapping (Code.add_abs_eqn thm) I)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   139
        val add_abs_eqn_attrib = Attrib.internal (K add_abs_eqn_attribute);
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   140
      in
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   141
        lthy
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   142
          |> (snd oo Local_Theory.note) ((code_eqn_thm_name, [add_abs_eqn_attrib]), [code_cert])
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   143
      end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   144
    else
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   145
      lthy
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   146
  end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   147
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   148
fun define_code_eq code_eqn_thm_name def_thm lthy =
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   149
  let
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   150
    val unfolded_def = Conv.fconv_rule unfold_fun_maps def_thm
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   151
    val code_eq = unabs_all_def lthy unfolded_def
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   152
    val simp_code_eq = simplify_code_eq lthy code_eq
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   153
  in
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   154
    lthy
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   155
      |> (snd oo Local_Theory.note) ((code_eqn_thm_name, [Code.add_default_eqn_attrib]), [simp_code_eq])
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   156
  end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   157
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   158
fun define_code code_eqn_thm_name def_thm rsp_thm (rty, qty) lthy =
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   159
  if body_type rty = body_type qty then 
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   160
    define_code_eq code_eqn_thm_name def_thm lthy
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   161
  else 
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   162
    define_code_cert code_eqn_thm_name def_thm rsp_thm (rty, qty) lthy
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   163
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   164
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   165
fun add_lift_def var qty rhs rsp_thm lthy =
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   166
  let
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   167
    val rty = fastype_of rhs
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   168
    val absrep_trm =  Lifting_Term.absrep_fun lthy (rty, qty)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   169
    val rty_forced = (domain_type o fastype_of) absrep_trm
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   170
    val forced_rhs = force_rty_type lthy rty_forced rhs
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   171
    val lhs = Free (Binding.print (#1 var), qty)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   172
    val prop = Logic.mk_equals (lhs, absrep_trm $ forced_rhs)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   173
    val (_, prop') = Local_Defs.cert_def lthy prop
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   174
    val (_, newrhs) = Local_Defs.abs_def prop'
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   175
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   176
    val ((_, (_ , def_thm)), lthy') = 
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   177
      Local_Theory.define (var, ((Thm.def_binding (#1 var), []), newrhs)) lthy
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   178
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   179
    fun qualify defname suffix = Binding.name suffix
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   180
      |> Binding.qualify true defname
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   181
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   182
    val lhs_name = Binding.name_of (#1 var)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   183
    val rsp_thm_name = qualify lhs_name "rsp"
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   184
    val code_eqn_thm_name = qualify lhs_name "rep_eq"
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   185
  in
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   186
    lthy'
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   187
      |> (snd oo Local_Theory.note) ((rsp_thm_name, []), [rsp_thm])
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   188
      |> define_code code_eqn_thm_name def_thm rsp_thm (rty_forced, qty)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   189
  end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   190
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   191
fun mk_readable_rsp_thm_eq tm lthy =
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   192
  let
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   193
    val ctm = cterm_of (Proof_Context.theory_of lthy) tm
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   194
    
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   195
    fun norm_fun_eq ctm = 
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   196
      let
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   197
        fun abs_conv2 cv = Conv.abs_conv (K (Conv.abs_conv (K cv) lthy)) lthy
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   198
        fun erase_quants ctm' =
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   199
          case (Thm.term_of ctm') of
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   200
            Const ("HOL.eq", _) $ _ $ _ => Conv.all_conv ctm'
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   201
            | _ => (Conv.binder_conv (K erase_quants) lthy then_conv 
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   202
              Conv.rewr_conv @{thm fun_eq_iff[symmetric, THEN eq_reflection]}) ctm'
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   203
      in
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   204
        (abs_conv2 erase_quants then_conv Thm.eta_conversion) ctm
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   205
      end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   206
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   207
    fun simp_arrows_conv ctm =
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   208
      let
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   209
        val unfold_conv = Conv.rewrs_conv 
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   210
          [@{thm fun_rel_eq_invariant[THEN eq_reflection]}, @{thm fun_rel_eq_rel[THEN eq_reflection]}, 
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   211
            @{thm fun_rel_def[THEN eq_reflection]}]
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   212
        val left_conv = simp_arrows_conv then_conv Conv.try_conv norm_fun_eq
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   213
        fun binop_conv2 cv1 cv2 = Conv.combination_conv (Conv.arg_conv cv1) cv2
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   214
      in
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   215
        case (Thm.term_of ctm) of
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   216
          Const (@{const_name "fun_rel"}, _) $ _ $ _ => 
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   217
            (binop_conv2  left_conv simp_arrows_conv then_conv unfold_conv) ctm
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   218
          | _ => Conv.all_conv ctm
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   219
      end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   220
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   221
    val unfold_ret_val_invs = Conv.bottom_conv 
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   222
      (K (Conv.try_conv (Conv.rewr_conv @{thm invariant_same_args}))) lthy 
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   223
    val simp_conv = Conv.arg_conv (Conv.fun2_conv simp_arrows_conv)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   224
    val univq_conv = Conv.rewr_conv @{thm HOL.all_simps(6)[symmetric, THEN eq_reflection]}
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   225
    val univq_prenex_conv = Conv.top_conv (K (Conv.try_conv univq_conv)) lthy
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   226
    val beta_conv = Thm.beta_conversion true
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   227
    val eq_thm = 
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   228
      (simp_conv then_conv univq_prenex_conv then_conv beta_conv then_conv unfold_ret_val_invs) ctm
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   229
  in
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   230
    Object_Logic.rulify(eq_thm RS Drule.equal_elim_rule2)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   231
  end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   232
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   233
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   234
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   235
fun lift_def_cmd (raw_var, rhs_raw) lthy =
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   236
  let
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   237
    val ((binding, SOME qty, mx), ctxt) = yield_singleton Proof_Context.read_vars raw_var lthy 
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   238
    val rhs = (Syntax.check_term ctxt o Syntax.parse_term ctxt) rhs_raw
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   239
 
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   240
    fun try_to_prove_refl thm = 
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   241
      let
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   242
        val lhs_eq =
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   243
          thm
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   244
          |> prop_of
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   245
          |> Logic.dest_implies
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   246
          |> fst
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   247
          |> strip_all_body
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   248
          |> try HOLogic.dest_Trueprop
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   249
      in
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   250
        case lhs_eq of
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   251
          SOME (Const ("HOL.eq", _) $ _ $ _) => SOME (@{thm refl} RS thm)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   252
          | _ => NONE
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   253
      end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   254
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   255
    val rsp_rel = Lifting_Term.equiv_relation lthy (fastype_of rhs, qty)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   256
    val rty_forced = (domain_type o fastype_of) rsp_rel;
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   257
    val forced_rhs = force_rty_type lthy rty_forced rhs;
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   258
    val internal_rsp_tm = HOLogic.mk_Trueprop (rsp_rel $ forced_rhs $ forced_rhs)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   259
    val readable_rsp_thm_eq = mk_readable_rsp_thm_eq internal_rsp_tm lthy
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   260
    val maybe_proven_rsp_thm = try_to_prove_refl readable_rsp_thm_eq
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   261
    val (readable_rsp_tm, _) = Logic.dest_implies (prop_of readable_rsp_thm_eq)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   262
  
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   263
    fun after_qed thm_list lthy = 
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   264
      let
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   265
        val internal_rsp_thm =
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   266
          case thm_list of
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   267
            [] => the maybe_proven_rsp_thm
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   268
          | [[thm]] => Goal.prove ctxt [] [] internal_rsp_tm 
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   269
            (fn _ => rtac readable_rsp_thm_eq 1 THEN Proof_Context.fact_tac [thm] 1)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   270
      in
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   271
        add_lift_def (binding, mx) qty rhs internal_rsp_thm lthy
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   272
      end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   273
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   274
  in
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   275
    case maybe_proven_rsp_thm of
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   276
      SOME _ => Proof.theorem NONE after_qed [] lthy
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   277
      | NONE =>  Proof.theorem NONE after_qed [[(readable_rsp_tm,[])]] lthy
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   278
  end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   279
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   280
(* parser and command *)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   281
val liftdef_parser =
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   282
  ((Parse.binding -- (@{keyword "::"} |-- (Parse.typ >> SOME) -- Parse.opt_mixfix')) >> Parse.triple2)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   283
    --| @{keyword "is"} -- Parse.term
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   284
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   285
val _ =
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   286
  Outer_Syntax.local_theory_to_proof @{command_spec "lift_definition"}
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   287
    "definition for constants over the quotient type"
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   288
      (liftdef_parser >> lift_def_cmd)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   289
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   290
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   291
end; (* structure *)