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