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