src/HOL/Tools/Lifting/lifting_def_code_dt.ML
author wenzelm
Fri, 04 Jan 2019 23:22:53 +0100
changeset 69593 3dda49e08b9d
parent 67713 041898537c19
child 69597 ff784d5a5bfb
permissions -rw-r--r--
isabelle update -u control_cartouches;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
     1
(*  Title:      HOL/Tools/Lifting/lifting_def_code_dt.ML
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
     2
    Author:     Ondrej Kuncar
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
     3
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
     4
Workaround that allows us to execute lifted constants that have
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
     5
as a return type a datatype containing a subtype; lift_definition command
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
     6
*)
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
     7
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
     8
signature LIFTING_DEF_CODE_DT =
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
     9
sig
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    10
  type rep_isom_data
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    11
  val isom_of_rep_isom_data: rep_isom_data -> term
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    12
  val transfer_of_rep_isom_data: rep_isom_data -> thm
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    13
  val bundle_name_of_rep_isom_data: rep_isom_data -> string
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    14
  val pointer_of_rep_isom_data: rep_isom_data -> string
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    15
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    16
  type code_dt
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    17
  val rty_of_code_dt: code_dt -> typ
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    18
  val qty_of_code_dt: code_dt -> typ
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    19
  val wit_of_code_dt: code_dt -> term
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    20
  val wit_thm_of_code_dt: code_dt -> thm
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    21
  val rep_isom_data_of_code_dt: code_dt -> rep_isom_data option
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    22
  val morph_code_dt: morphism -> code_dt -> code_dt
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    23
  val mk_witness_of_code_dt: typ -> code_dt -> term
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    24
  val mk_rep_isom_of_code_dt: typ -> code_dt -> term option
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    25
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    26
  val code_dt_of: Proof.context -> typ * typ -> code_dt option
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    27
  val code_dt_of_global: theory -> typ * typ -> code_dt option
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    28
  val all_code_dt_of: Proof.context -> code_dt list
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    29
  val all_code_dt_of_global: theory -> code_dt list
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    30
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    31
  type config_code_dt = { code_dt: bool, lift_config: Lifting_Def.config }
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    32
  val default_config_code_dt: config_code_dt
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    33
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    34
  val add_lift_def_code_dt:
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    35
    config_code_dt -> binding * mixfix -> typ -> term -> thm -> thm list -> local_theory ->
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    36
      Lifting_Def.lift_def * local_theory
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    37
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    38
  val lift_def_code_dt:
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    39
    config_code_dt -> binding * mixfix -> typ -> term -> (Proof.context -> tactic) -> thm list ->
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    40
    local_theory -> Lifting_Def.lift_def * local_theory
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    41
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    42
  val lift_def_cmd:
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    43
    string list * (binding * string option * mixfix) * string * (Facts.ref * Token.src list) list ->
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    44
    local_theory -> Proof.state
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    45
end
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    46
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    47
structure Lifting_Def_Code_Dt: LIFTING_DEF_CODE_DT =
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    48
struct
60231
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
    49
                                                                       
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
    50
open Ctr_Sugar_Util BNF_Util BNF_FP_Util BNF_FP_Def_Sugar Lifting_Def Lifting_Util
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    51
60231
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
    52
infix 0 MRSL
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    53
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    54
(** data structures **)
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    55
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    56
(* all type variables in qty are in rty *)
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    57
datatype rep_isom_data = REP_ISOM of { isom: term, transfer: thm, bundle_name: string, pointer: string }
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    58
fun isom_of_rep_isom_data (REP_ISOM rep_isom) = #isom rep_isom;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    59
fun transfer_of_rep_isom_data (REP_ISOM rep_isom) = #transfer rep_isom;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    60
fun bundle_name_of_rep_isom_data (REP_ISOM rep_isom) = #bundle_name rep_isom;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    61
fun pointer_of_rep_isom_data (REP_ISOM rep_isom) = #pointer rep_isom;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    62
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    63
datatype code_dt = CODE_DT of { rty: typ, qty: typ, wit: term, wit_thm: thm,
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    64
  rep_isom_data: rep_isom_data option };
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    65
fun rty_of_code_dt (CODE_DT code_dt) = #rty code_dt;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    66
fun qty_of_code_dt (CODE_DT code_dt) = #qty code_dt;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    67
fun wit_of_code_dt (CODE_DT code_dt) = #wit code_dt;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    68
fun wit_thm_of_code_dt (CODE_DT code_dt) = #wit_thm code_dt;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    69
fun rep_isom_data_of_code_dt (CODE_DT code_dt) = #rep_isom_data code_dt;
60232
29ac1c6a1fbb equivalence in code_dt data structure must respect both rty and qty
kuncar
parents: 60231
diff changeset
    70
fun ty_alpha_equiv (T, U) = Type.raw_instance (T, U) andalso Type.raw_instance (U, T);
29ac1c6a1fbb equivalence in code_dt data structure must respect both rty and qty
kuncar
parents: 60231
diff changeset
    71
fun code_dt_eq c = (ty_alpha_equiv o apply2 rty_of_code_dt) c 
29ac1c6a1fbb equivalence in code_dt data structure must respect both rty and qty
kuncar
parents: 60231
diff changeset
    72
  andalso (ty_alpha_equiv o apply2 qty_of_code_dt) c;
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    73
fun term_of_code_dt code_dt = code_dt |> `rty_of_code_dt ||> qty_of_code_dt |> HOLogic.mk_prodT
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    74
  |> Net.encode_type |> single;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    75
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    76
(* modulo renaming, typ must contain TVars *)
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    77
fun is_code_dt_of_type (rty, qty) code_dt = code_dt |> `rty_of_code_dt ||> qty_of_code_dt
60232
29ac1c6a1fbb equivalence in code_dt data structure must respect both rty and qty
kuncar
parents: 60231
diff changeset
    78
  |> HOLogic.mk_prodT |> curry ty_alpha_equiv (HOLogic.mk_prodT (rty, qty));
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    79
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    80
fun mk_rep_isom_data isom transfer bundle_name pointer =
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    81
  REP_ISOM { isom = isom, transfer = transfer, bundle_name = bundle_name, pointer = pointer}
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    82
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    83
fun mk_code_dt rty qty wit wit_thm rep_isom_data =
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    84
  CODE_DT { rty = rty, qty = qty, wit = wit, wit_thm = wit_thm, rep_isom_data = rep_isom_data };
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    85
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    86
fun map_rep_isom_data f1 f2 f3 f4
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    87
  (REP_ISOM { isom = isom, transfer = transfer, bundle_name = bundle_name, pointer = pointer }) =
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    88
  REP_ISOM { isom = f1 isom, transfer = f2 transfer, bundle_name = f3 bundle_name, pointer = f4 pointer };
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    89
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    90
fun map_code_dt f1 f2 f3 f4 f5 f6 f7 f8
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    91
  (CODE_DT {rty = rty, qty = qty, wit = wit, wit_thm = wit_thm, rep_isom_data = rep_isom_data}) =
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    92
  CODE_DT {rty = f1 rty, qty = f2 qty, wit = f3 wit, wit_thm = f4 wit_thm,
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    93
    rep_isom_data = Option.map (map_rep_isom_data f5 f6 f7 f8) rep_isom_data};
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    94
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    95
fun update_rep_isom isom transfer binding pointer i = mk_code_dt (rty_of_code_dt i) (qty_of_code_dt i)
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    96
  (wit_of_code_dt i) (wit_thm_of_code_dt i) (SOME (mk_rep_isom_data isom transfer binding pointer))
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    97
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    98
fun morph_code_dt phi =
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
    99
  let
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   100
    val mty = Morphism.typ phi
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   101
    val mterm = Morphism.term phi
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   102
    val mthm = Morphism.thm phi
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   103
  in
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   104
    map_code_dt mty mty mterm mthm mterm mthm I I
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   105
  end
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   106
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   107
val transfer_code_dt = morph_code_dt o Morphism.transfer_morphism;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   108
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   109
structure Data = Generic_Data
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   110
(
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   111
  type T = code_dt Item_Net.T
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   112
  val empty = Item_Net.init code_dt_eq term_of_code_dt
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   113
  val extend = I
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   114
  val merge = Item_Net.merge
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   115
);
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   116
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   117
fun code_dt_of_generic context (rty, qty) =
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   118
  let
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   119
    val typ = HOLogic.mk_prodT (rty, qty)
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   120
    val prefiltred = Item_Net.retrieve_matching (Data.get context) (Net.encode_type typ)
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   121
  in
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   122
    prefiltred |> filter (is_code_dt_of_type (rty, qty))
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   123
    |> map (transfer_code_dt (Context.theory_of context)) |> find_first (fn _ => true)
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   124
  end;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   125
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   126
fun code_dt_of ctxt (rty, qty) =
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   127
  let
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   128
    val sch_rty = Logic.type_map (singleton (Variable.polymorphic ctxt)) rty
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   129
    val sch_qty = Logic.type_map (singleton (Variable.polymorphic ctxt)) qty
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   130
  in
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   131
    code_dt_of_generic (Context.Proof ctxt) (sch_rty, sch_qty)
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   132
  end;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   133
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   134
fun code_dt_of_global thy (rty, qty) =
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   135
  let
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   136
    val sch_rty = Logic.varifyT_global rty
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   137
    val sch_qty = Logic.varifyT_global qty
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   138
  in
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   139
    code_dt_of_generic (Context.Theory thy) (sch_rty, sch_qty)
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   140
  end;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   141
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   142
fun all_code_dt_of_generic context =
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   143
    Item_Net.content (Data.get context) |> map (transfer_code_dt (Context.theory_of context));
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   144
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   145
val all_code_dt_of = all_code_dt_of_generic o Context.Proof;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   146
val all_code_dt_of_global = all_code_dt_of_generic o Context.Theory;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   147
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   148
fun update_code_dt code_dt =
66017
57acac0fd29b avoid Local_Theory.reset in application space
haftmann
parents: 63352
diff changeset
   149
  Local_Theory.open_target #> snd
57acac0fd29b avoid Local_Theory.reset in application space
haftmann
parents: 63352
diff changeset
   150
  #> Local_Theory.declaration {syntax = false, pervasive = true}
57acac0fd29b avoid Local_Theory.reset in application space
haftmann
parents: 63352
diff changeset
   151
    (fn phi => Data.map (Item_Net.update (morph_code_dt phi code_dt)))
57acac0fd29b avoid Local_Theory.reset in application space
haftmann
parents: 63352
diff changeset
   152
  #> Local_Theory.close_target
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   153
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   154
fun mk_match_of_code_dt qty code_dt = Vartab.empty |> Type.raw_match (qty_of_code_dt code_dt, qty)
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   155
  |> Vartab.dest |> map (fn (x, (S, T)) => (TVar (x, S), T));
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   156
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   157
fun mk_witness_of_code_dt qty code_dt =
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   158
  Term.subst_atomic_types (mk_match_of_code_dt qty code_dt) (wit_of_code_dt code_dt)
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   159
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   160
fun mk_rep_isom_of_code_dt qty code_dt = Option.map
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   161
  (isom_of_rep_isom_data #> Term.subst_atomic_types (mk_match_of_code_dt qty code_dt))
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   162
    (rep_isom_data_of_code_dt code_dt)
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   163
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   164
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   165
(** unique name for a type **)
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   166
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 67713
diff changeset
   167
fun var_name name sort = if sort = \<^sort>\<open>{type}\<close> orelse sort = [] then ["x" ^ name]
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   168
  else "x" ^ name :: "x_" :: sort @ ["x_"];
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   169
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   170
fun concat_Tnames (Type (name, ts)) = name :: maps concat_Tnames ts
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   171
  | concat_Tnames (TFree (name, sort)) = var_name name sort
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   172
  | concat_Tnames (TVar ((name, _), sort)) = var_name name sort;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   173
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   174
fun unique_Tname (rty, qty) =
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   175
  let
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   176
    val Tnames = map Long_Name.base_name (concat_Tnames rty @ ["x_x"] @ concat_Tnames qty);
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   177
  in
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   178
    fold (Binding.qualify false) (tl Tnames) (Binding.name (hd Tnames))
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   179
  end;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   180
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   181
(** witnesses **)
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   182
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 67713
diff changeset
   183
fun mk_undefined T = Const (\<^const_name>\<open>undefined\<close>, T);
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   184
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   185
fun mk_witness quot_thm =
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   186
  let
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   187
    val wit_thm = quot_thm RS @{thm type_definition_Quotient_not_empty_witness}
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   188
    val wit = quot_thm_rep quot_thm $ mk_undefined (quot_thm_rty_qty quot_thm |> snd)
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   189
  in
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   190
    (wit, wit_thm)
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   191
  end
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   192
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   193
(** config **)
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   194
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   195
type config_code_dt = { code_dt: bool, lift_config: config }
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   196
val default_config_code_dt = { code_dt = false, lift_config = default_config }
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   197
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   198
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   199
(** Main code **)
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   200
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   201
val ld_no_notes = { notes = false }
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   202
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   203
fun comp_lift_error _ _ = error "Composition of abstract types has not been implemented yet."
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   204
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   205
fun lift qty (quot_thm, (lthy, rel_eq_onps)) =
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   206
  let
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   207
    val quot_thm = Lifting_Term.force_qty_type lthy qty quot_thm
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   208
    val (rty, qty) = quot_thm_rty_qty quot_thm;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   209
  in
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   210
    if is_none (code_dt_of lthy (rty, qty)) then
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   211
      let
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   212
        val (wit, wit_thm) = (mk_witness quot_thm
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   213
          handle THM _ => error ("code_dt: " ^ quote (Tname qty) ^ " was not defined as a subtype."))
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   214
        val code_dt = mk_code_dt rty qty wit wit_thm NONE
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   215
      in
66017
57acac0fd29b avoid Local_Theory.reset in application space
haftmann
parents: 63352
diff changeset
   216
        (quot_thm, (update_code_dt code_dt lthy, rel_eq_onps))
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   217
      end
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   218
    else
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   219
      (quot_thm, (lthy, rel_eq_onps))
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   220
  end;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   221
60784
4f590c08fd5d updated to infer_instantiate;
wenzelm
parents: 60752
diff changeset
   222
fun case_tac rule =
4f590c08fd5d updated to infer_instantiate;
wenzelm
parents: 60752
diff changeset
   223
  Subgoal.FOCUS_PARAMS (fn {context = ctxt, params, ...} =>
4f590c08fd5d updated to infer_instantiate;
wenzelm
parents: 60752
diff changeset
   224
    HEADGOAL (rtac ctxt (infer_instantiate' ctxt [SOME (snd (hd params))] rule)));
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   225
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   226
fun bundle_name_of_bundle_binding binding phi context  =
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   227
  Name_Space.full_name (Name_Space.naming_of context) (Morphism.binding phi binding);
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   228
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   229
fun prove_schematic_quot_thm actions ctxt = Lifting_Term.prove_schematic_quot_thm actions
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   230
 (Lifting_Info.get_quotients ctxt) ctxt
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   231
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   232
fun prove_code_dt (rty, qty) lthy =
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   233
  let
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   234
    val (fold_quot_thm: (local_theory * thm list) Lifting_Term.fold_quot_thm) =
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   235
      { constr = constr, lift = lift, comp_lift = comp_lift_error };
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   236
  in prove_schematic_quot_thm fold_quot_thm lthy (rty, qty) (lthy, []) |> snd end
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   237
and add_lift_def_code_dt config var qty rhs rsp_thm par_thms lthy =
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   238
  let
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   239
    fun binop_conv2 cv1 cv2 = Conv.combination_conv (Conv.arg_conv cv1) cv2
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   240
    fun ret_rel_conv conv ctm =
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   241
      case (Thm.term_of ctm) of
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 67713
diff changeset
   242
        Const (\<^const_name>\<open>rel_fun\<close>, _) $ _ $ _ =>
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   243
          binop_conv2 Conv.all_conv conv ctm
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   244
        | _ => conv ctm
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   245
    fun R_conv rel_eq_onps = Transfer.top_sweep_rewr_conv @{thms eq_onp_top_eq_eq[symmetric, THEN eq_reflection]}
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   246
      then_conv Transfer.bottom_rewr_conv rel_eq_onps
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   247
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   248
    val (ret_lift_def, lthy) = add_lift_def (#lift_config config) var qty rhs rsp_thm par_thms lthy
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   249
  in
60230
4857d553c52c 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
parents: 60229
diff changeset
   250
    if (not (#code_dt config) orelse (code_eq_of_lift_def ret_lift_def <> NONE_EQ)
4857d553c52c 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
parents: 60229
diff changeset
   251
      andalso (code_eq_of_lift_def ret_lift_def <> UNKNOWN_EQ))
4857d553c52c 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
parents: 60229
diff changeset
   252
      (* Let us try even in case of UNKNOWN_EQ. If this leads to problems, the user can always
4857d553c52c 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
parents: 60229
diff changeset
   253
        say that they do not want this workaround. *)
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   254
    then  (ret_lift_def, lthy)
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   255
    else
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   256
      let
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   257
        val lift_def = inst_of_lift_def lthy qty ret_lift_def
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   258
        val rty = rty_of_lift_def lift_def
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   259
        val rty_ret = body_type rty
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   260
        val qty_ret = body_type qty
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   261
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   262
        val (lthy, rel_eq_onps) = prove_code_dt (rty_ret, qty_ret) lthy
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   263
        val code_dt = code_dt_of lthy (rty_ret, qty_ret)
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   264
      in
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   265
        if is_none code_dt orelse is_none (rep_isom_data_of_code_dt (the code_dt)) then (ret_lift_def, lthy)
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   266
        else
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   267
          let
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   268
            val code_dt = the code_dt
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   269
            val rhs = dest_comb (rhs_of_lift_def lift_def) |> snd
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   270
            val rep_isom_data = code_dt |> rep_isom_data_of_code_dt |> the
60235
3cab6f891c2f reorder some steps in the construction to support mutual datatypes
kuncar
parents: 60234
diff changeset
   271
            val pointer = pointer_of_rep_isom_data rep_isom_data
3cab6f891c2f reorder some steps in the construction to support mutual datatypes
kuncar
parents: 60234
diff changeset
   272
            val quot_active = 
3cab6f891c2f reorder some steps in the construction to support mutual datatypes
kuncar
parents: 60234
diff changeset
   273
              Lifting_Info.lookup_restore_data lthy pointer |> the |> #quotient |> #quot_thm
3cab6f891c2f reorder some steps in the construction to support mutual datatypes
kuncar
parents: 60234
diff changeset
   274
              |> Lifting_Info.lookup_quot_thm_quotients lthy |> is_some
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   275
            val qty_code_dt_bundle_name = bundle_name_of_rep_isom_data rep_isom_data
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   276
            val rep_isom = mk_rep_isom_of_code_dt qty_ret code_dt |> the
60235
3cab6f891c2f reorder some steps in the construction to support mutual datatypes
kuncar
parents: 60234
diff changeset
   277
            val lthy = if quot_active then lthy else Bundle.includes [qty_code_dt_bundle_name] lthy
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   278
            fun qty_isom_of_rep_isom rep = rep |> dest_Const |> snd |> domain_type
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   279
            val qty_isom = qty_isom_of_rep_isom rep_isom
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   280
            val f'_var = (Binding.suffix_name "_aux" (fst var), NoSyn);
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   281
            val f'_qty = strip_type qty |> fst |> rpair qty_isom |> op --->
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   282
            val f'_rsp_rel = Lifting_Term.equiv_relation lthy (rty, f'_qty);
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   283
            val rsp = rsp_thm_of_lift_def lift_def
60231
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   284
            val rel_eq_onps_conv = HOLogic.Trueprop_conv (Conv.fun2_conv (ret_rel_conv (R_conv rel_eq_onps)))
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   285
            val rsp_norm = Conv.fconv_rule rel_eq_onps_conv rsp
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   286
            val f'_rsp_goal = HOLogic.mk_Trueprop (f'_rsp_rel $ rhs $ rhs);
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   287
            val f'_rsp = Goal.prove_sorry lthy [] [] f'_rsp_goal
60728
26ffdb966759 {r,e,d,f}tac with proper context in BNF
traytel
parents: 60669
diff changeset
   288
              (fn {context = ctxt, prems = _} =>
26ffdb966759 {r,e,d,f}tac with proper context in BNF
traytel
parents: 60669
diff changeset
   289
                HEADGOAL (CONVERSION (rel_eq_onps_conv) THEN' rtac ctxt rsp_norm))
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   290
              |> Thm.close_derivation
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   291
            val (f'_lift_def, lthy) = add_lift_def ld_no_notes f'_var f'_qty rhs f'_rsp [] lthy
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   292
            val f'_lift_def = inst_of_lift_def lthy f'_qty f'_lift_def
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   293
            val f'_lift_const = mk_lift_const_of_lift_def f'_qty f'_lift_def
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   294
            val args_lthy = lthy
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   295
            val (args, lthy) = mk_Frees "x" (binder_types qty) lthy
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   296
            val f_alt_def_goal_lhs = list_comb (lift_const_of_lift_def lift_def, args);
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   297
            val f_alt_def_goal_rhs = rep_isom $ list_comb (f'_lift_const, args);
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   298
            val f_alt_def_goal = HOLogic.mk_Trueprop (HOLogic.mk_eq (f_alt_def_goal_lhs, f_alt_def_goal_rhs));
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   299
            fun f_alt_def_tac ctxt i =
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   300
              EVERY' [Transfer.gen_frees_tac [] ctxt, DETERM o Transfer.transfer_tac true ctxt,
63170
eae6549dbea2 tuned proofs, to allow unfold_abs_def;
wenzelm
parents: 63003
diff changeset
   301
                SELECT_GOAL (Local_Defs.unfold0_tac ctxt [id_apply]), rtac ctxt refl] i;
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   302
            val rep_isom_transfer = transfer_of_rep_isom_data rep_isom_data
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63342
diff changeset
   303
            val (_, transfer_lthy) =
67713
041898537c19 tuned signature;
wenzelm
parents: 66017
diff changeset
   304
              Proof_Context.note_thms ""
041898537c19 tuned signature;
wenzelm
parents: 66017
diff changeset
   305
                (Binding.empty_atts, [([rep_isom_transfer], [Transfer.transfer_add])]) lthy
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   306
            val f_alt_def = Goal.prove_sorry transfer_lthy [] [] f_alt_def_goal
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   307
              (fn {context = ctxt, prems = _} => HEADGOAL (f_alt_def_tac ctxt))
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   308
              |> Thm.close_derivation
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   309
              |> singleton (Variable.export lthy args_lthy)
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   310
            val lthy = args_lthy
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   311
            val lthy =  lthy
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   312
              |> Local_Theory.note ((Binding.empty, @{attributes [code]}), [f_alt_def])
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   313
              |> snd
60235
3cab6f891c2f reorder some steps in the construction to support mutual datatypes
kuncar
parents: 60234
diff changeset
   314
              (* if processing a mutual datatype (there is a cycle!) the corresponding quotient 
3cab6f891c2f reorder some steps in the construction to support mutual datatypes
kuncar
parents: 60234
diff changeset
   315
                 will be needed later and will be forgotten later *)
3cab6f891c2f reorder some steps in the construction to support mutual datatypes
kuncar
parents: 60234
diff changeset
   316
              |> (if quot_active then I else Lifting_Setup.lifting_forget pointer)
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   317
          in
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   318
            (ret_lift_def, lthy)
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   319
          end
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   320
       end
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   321
    end
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   322
and mk_rep_isom qty_isom_bundle (rty, qty, qty_isom) lthy =
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   323
  let
60235
3cab6f891c2f reorder some steps in the construction to support mutual datatypes
kuncar
parents: 60234
diff changeset
   324
    (* logical definition of qty qty_isom isomorphism *) 
3cab6f891c2f reorder some steps in the construction to support mutual datatypes
kuncar
parents: 60234
diff changeset
   325
    val uTname = unique_Tname (rty, qty)
63170
eae6549dbea2 tuned proofs, to allow unfold_abs_def;
wenzelm
parents: 63003
diff changeset
   326
    fun eq_onp_to_top_tac ctxt = SELECT_GOAL (Local_Defs.unfold0_tac ctxt 
60235
3cab6f891c2f reorder some steps in the construction to support mutual datatypes
kuncar
parents: 60234
diff changeset
   327
      (@{thm eq_onp_top_eq_eq[symmetric]} :: Lifting_Info.get_relator_eq_onp_rules ctxt))
3cab6f891c2f reorder some steps in the construction to support mutual datatypes
kuncar
parents: 60234
diff changeset
   328
    fun lift_isom_tac ctxt = HEADGOAL (eq_onp_to_top_tac ctxt
60728
26ffdb966759 {r,e,d,f}tac with proper context in BNF
traytel
parents: 60669
diff changeset
   329
      THEN' (rtac ctxt @{thm id_transfer}));
60235
3cab6f891c2f reorder some steps in the construction to support mutual datatypes
kuncar
parents: 60234
diff changeset
   330
63003
bf5fcc65586b clarified signature;
wenzelm
parents: 62969
diff changeset
   331
    val (rep_isom_lift_def, lthy) =
66017
57acac0fd29b avoid Local_Theory.reset in application space
haftmann
parents: 63352
diff changeset
   332
      lthy
57acac0fd29b avoid Local_Theory.reset in application space
haftmann
parents: 63352
diff changeset
   333
      |> Local_Theory.open_target |> snd
57acac0fd29b avoid Local_Theory.reset in application space
haftmann
parents: 63352
diff changeset
   334
      |> lift_def ld_no_notes (Binding.qualify_name true uTname "Rep_isom", NoSyn)
57acac0fd29b avoid Local_Theory.reset in application space
haftmann
parents: 63352
diff changeset
   335
        (qty_isom --> qty) (HOLogic.id_const rty) lift_isom_tac []
57acac0fd29b avoid Local_Theory.reset in application space
haftmann
parents: 63352
diff changeset
   336
      |>> inst_of_lift_def lthy (qty_isom --> qty);
63003
bf5fcc65586b clarified signature;
wenzelm
parents: 62969
diff changeset
   337
    val (abs_isom, lthy) =
66017
57acac0fd29b avoid Local_Theory.reset in application space
haftmann
parents: 63352
diff changeset
   338
      lthy
57acac0fd29b avoid Local_Theory.reset in application space
haftmann
parents: 63352
diff changeset
   339
      |> lift_def ld_no_notes (Binding.qualify_name true uTname "Abs_isom", NoSyn)
57acac0fd29b avoid Local_Theory.reset in application space
haftmann
parents: 63352
diff changeset
   340
        (qty --> qty_isom) (HOLogic.id_const rty) lift_isom_tac []
57acac0fd29b avoid Local_Theory.reset in application space
haftmann
parents: 63352
diff changeset
   341
      |>> mk_lift_const_of_lift_def (qty --> qty_isom);
60235
3cab6f891c2f reorder some steps in the construction to support mutual datatypes
kuncar
parents: 60234
diff changeset
   342
    val rep_isom = lift_const_of_lift_def rep_isom_lift_def
3cab6f891c2f reorder some steps in the construction to support mutual datatypes
kuncar
parents: 60234
diff changeset
   343
3cab6f891c2f reorder some steps in the construction to support mutual datatypes
kuncar
parents: 60234
diff changeset
   344
    val pointer = Lifting_Setup.pointer_of_bundle_binding lthy qty_isom_bundle
3cab6f891c2f reorder some steps in the construction to support mutual datatypes
kuncar
parents: 60234
diff changeset
   345
    fun code_dt phi context = code_dt_of lthy (rty, qty) |> the |>
3cab6f891c2f reorder some steps in the construction to support mutual datatypes
kuncar
parents: 60234
diff changeset
   346
      update_rep_isom rep_isom (transfer_rules_of_lift_def rep_isom_lift_def |> hd)
3cab6f891c2f reorder some steps in the construction to support mutual datatypes
kuncar
parents: 60234
diff changeset
   347
       (bundle_name_of_bundle_binding qty_isom_bundle phi context) pointer;
3cab6f891c2f reorder some steps in the construction to support mutual datatypes
kuncar
parents: 60234
diff changeset
   348
    val lthy = lthy  
3cab6f891c2f reorder some steps in the construction to support mutual datatypes
kuncar
parents: 60234
diff changeset
   349
      |> Local_Theory.declaration {syntax = false, pervasive = true}
3cab6f891c2f reorder some steps in the construction to support mutual datatypes
kuncar
parents: 60234
diff changeset
   350
        (fn phi => fn context => Data.map (Item_Net.update (morph_code_dt phi (code_dt phi context))) context)
66017
57acac0fd29b avoid Local_Theory.reset in application space
haftmann
parents: 63352
diff changeset
   351
      |> Local_Theory.close_target
60235
3cab6f891c2f reorder some steps in the construction to support mutual datatypes
kuncar
parents: 60234
diff changeset
   352
3cab6f891c2f reorder some steps in the construction to support mutual datatypes
kuncar
parents: 60234
diff changeset
   353
    (* in order to make the qty qty_isom isomorphism executable we have to define discriminators 
3cab6f891c2f reorder some steps in the construction to support mutual datatypes
kuncar
parents: 60234
diff changeset
   354
      and selectors for qty_isom *)
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   355
    val (rty_name, typs) = dest_Type rty
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   356
    val (_, qty_typs) = dest_Type qty
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   357
    val fp = BNF_FP_Def_Sugar.fp_sugar_of lthy rty_name
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   358
    val fp = if is_some fp then the fp
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   359
      else error ("code_dt: " ^ quote rty_name ^ " is not a datatype.")
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   360
    val ctr_sugar = fp |> #fp_ctr_sugar |> #ctr_sugar
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   361
    val ctrs = map (Ctr_Sugar.mk_ctr typs) (#ctrs ctr_sugar);
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   362
    val qty_ctrs = map (Ctr_Sugar.mk_ctr qty_typs) (#ctrs ctr_sugar);
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   363
    val ctr_Tss = map (dest_Const #> snd #> binder_types) ctrs;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   364
    val qty_ctr_Tss = map (dest_Const #> snd #> binder_types) qty_ctrs;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   365
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   366
    val n = length ctrs;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   367
    val ks = 1 upto n;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   368
    val (xss, _) = mk_Freess "x" ctr_Tss lthy;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   369
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   370
    fun sel_retT (rty' as Type (s, rtys'), qty' as Type (s', qtys')) =
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   371
        if (rty', qty') = (rty, qty) then qty_isom else (if s = s'
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   372
          then Type (s, map sel_retT (rtys' ~~ qtys')) else qty')
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   373
      | sel_retT (_, qty') = qty';
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   374
60233
89d500d7e3eb better precomputing
kuncar
parents: 60232
diff changeset
   375
    val sel_retTs = map2 (map2 (sel_retT oo pair)) ctr_Tss qty_ctr_Tss
89d500d7e3eb better precomputing
kuncar
parents: 60232
diff changeset
   376
89d500d7e3eb better precomputing
kuncar
parents: 60232
diff changeset
   377
    fun lazy_prove_code_dt (rty, qty) lthy =
89d500d7e3eb better precomputing
kuncar
parents: 60232
diff changeset
   378
      if is_none (code_dt_of lthy (rty, qty)) then prove_code_dt (rty, qty) lthy |> fst else lthy;
89d500d7e3eb better precomputing
kuncar
parents: 60232
diff changeset
   379
89d500d7e3eb better precomputing
kuncar
parents: 60232
diff changeset
   380
    val lthy = fold2 (fold2 (lazy_prove_code_dt oo pair)) ctr_Tss sel_retTs lthy
89d500d7e3eb better precomputing
kuncar
parents: 60232
diff changeset
   381
89d500d7e3eb better precomputing
kuncar
parents: 60232
diff changeset
   382
    val sel_argss = @{map 4} (fn k => fn xs => @{map 2} (fn x => fn qty_ret => 
89d500d7e3eb better precomputing
kuncar
parents: 60232
diff changeset
   383
      (k, qty_ret, (xs, x)))) ks xss xss sel_retTs;
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   384
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   385
    fun mk_sel_casex (_, _, (_, x)) = Ctr_Sugar.mk_case typs (x |> dest_Free |> snd) (#casex ctr_sugar);
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   386
    val dis_casex = Ctr_Sugar.mk_case typs HOLogic.boolT (#casex ctr_sugar);
60233
89d500d7e3eb better precomputing
kuncar
parents: 60232
diff changeset
   387
    fun mk_sel_case_args lthy ctr_Tss ks (k, qty_ret, (xs, x)) =
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   388
      let
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   389
        val T = x |> dest_Free |> snd;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   390
        fun gen_undef_wit Ts wits =
60233
89d500d7e3eb better precomputing
kuncar
parents: 60232
diff changeset
   391
          case code_dt_of lthy (T, qty_ret) of
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   392
            SOME code_dt =>
60233
89d500d7e3eb better precomputing
kuncar
parents: 60232
diff changeset
   393
              (fold_rev (Term.lambda o curry Free Name.uu) Ts (mk_witness_of_code_dt qty_ret code_dt),
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   394
                wit_thm_of_code_dt code_dt :: wits)
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   395
            | NONE => (fold_rev (Term.lambda o curry Free Name.uu) Ts (mk_undefined T), wits)
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   396
      in
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   397
        @{fold_map 2} (fn Ts => fn k' => fn wits =>
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   398
          (if k = k' then (fold_rev Term.lambda xs x, wits) else gen_undef_wit Ts wits)) ctr_Tss ks []
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   399
      end;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   400
    fun mk_sel_rhs arg =
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   401
      let val (sel_rhs, wits) = mk_sel_case_args lthy ctr_Tss ks arg
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   402
      in (arg |> #2, wits, list_comb (mk_sel_casex arg, sel_rhs)) end;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   403
    fun mk_dis_case_args args k  = map (fn (k', arg) => (if k = k'
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   404
      then fold_rev Term.lambda arg @{const True} else fold_rev Term.lambda arg @{const False})) args;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   405
    val sel_rhs = map (map mk_sel_rhs) sel_argss
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   406
    val dis_rhs = map (fn k => list_comb (dis_casex, mk_dis_case_args (ks ~~ xss) k)) ks
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   407
    val dis_qty = qty_isom --> HOLogic.boolT;
63003
bf5fcc65586b clarified signature;
wenzelm
parents: 62969
diff changeset
   408
    val dis_names = map (fn k => Binding.qualify_name true uTname ("dis" ^ string_of_int k)) ks;
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   409
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   410
    val (diss, lthy) = @{fold_map 2} (fn b => fn rhs => fn lthy =>
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   411
      lift_def ld_no_notes (b, NoSyn) dis_qty rhs (K all_tac) [] lthy
66017
57acac0fd29b avoid Local_Theory.reset in application space
haftmann
parents: 63352
diff changeset
   412
      |>> mk_lift_const_of_lift_def dis_qty) dis_names dis_rhs lthy
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   413
60231
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   414
    val unfold_lift_sel_rsp = @{lemma "(\<And>x. P1 x \<Longrightarrow> P2 (f x)) \<Longrightarrow> (rel_fun (eq_onp P1) (eq_onp P2)) f f"
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   415
      by (simp add: eq_onp_same_args rel_fun_eq_onp_rel)}
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   416
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   417
    fun lift_sel_tac exhaust_rule dt_rules wits ctxt i =
61841
4d3527b94f2a more general types Proof.method / context_tactic;
wenzelm
parents: 61466
diff changeset
   418
        (Method.insert_tac ctxt wits THEN' 
60231
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   419
         eq_onp_to_top_tac ctxt THEN' (* normalize *)
60728
26ffdb966759 {r,e,d,f}tac with proper context in BNF
traytel
parents: 60669
diff changeset
   420
         rtac ctxt unfold_lift_sel_rsp THEN'
60231
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   421
         case_tac exhaust_rule ctxt THEN_ALL_NEW (
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   422
        EVERY' [hyp_subst_tac ctxt, (* does not kill wits because = was rewritten to eq_onp top *)
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   423
        Raw_Simplifier.rewrite_goal_tac ctxt (map safe_mk_meta_eq dt_rules), 
60752
b48830b670a1 prefer tactics with explicit context;
wenzelm
parents: 60728
diff changeset
   424
        REPEAT_DETERM o etac ctxt conjE, assume_tac ctxt])) i
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   425
    val pred_simps = Transfer.lookup_pred_data lthy (Tname rty) |> the |> Transfer.pred_simps
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   426
    val sel_tac = lift_sel_tac (#exhaust ctr_sugar) (#case_thms ctr_sugar @ pred_simps)
63003
bf5fcc65586b clarified signature;
wenzelm
parents: 62969
diff changeset
   427
    val sel_names =
bf5fcc65586b clarified signature;
wenzelm
parents: 62969
diff changeset
   428
      map (fn (k, xs) =>
bf5fcc65586b clarified signature;
wenzelm
parents: 62969
diff changeset
   429
        map (fn k' => Binding.qualify_name true uTname ("sel" ^ string_of_int k ^ string_of_int k'))
bf5fcc65586b clarified signature;
wenzelm
parents: 62969
diff changeset
   430
          (1 upto length xs)) (ks ~~ ctr_Tss);
60233
89d500d7e3eb better precomputing
kuncar
parents: 60232
diff changeset
   431
    val (selss, lthy) = @{fold_map 2} (@{fold_map 2} (fn b => fn (qty_ret, wits, rhs) => fn lthy =>
60231
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   432
        lift_def_code_dt { code_dt = true, lift_config = ld_no_notes }
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   433
        (b, NoSyn) (qty_isom --> qty_ret) rhs (HEADGOAL o sel_tac wits) [] lthy
66017
57acac0fd29b avoid Local_Theory.reset in application space
haftmann
parents: 63352
diff changeset
   434
      |>> mk_lift_const_of_lift_def (qty_isom --> qty_ret))) sel_names sel_rhs lthy
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   435
60235
3cab6f891c2f reorder some steps in the construction to support mutual datatypes
kuncar
parents: 60234
diff changeset
   436
    (* now we can execute the qty qty_isom isomorphism *)
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   437
    fun mk_type_definition newT oldT RepC AbsC A =
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   438
      let
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   439
        val typedefC =
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 67713
diff changeset
   440
          Const (\<^const_name>\<open>type_definition\<close>,
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   441
            (newT --> oldT) --> (oldT --> newT) --> HOLogic.mk_setT oldT --> HOLogic.boolT);
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   442
      in typedefC $ RepC $ AbsC $ A end;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   443
    val typedef_goal = mk_type_definition qty_isom qty rep_isom abs_isom (HOLogic.mk_UNIV qty) |>
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   444
      HOLogic.mk_Trueprop;
60231
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   445
    fun typ_isom_tac ctxt i =
63170
eae6549dbea2 tuned proofs, to allow unfold_abs_def;
wenzelm
parents: 63003
diff changeset
   446
      EVERY' [ SELECT_GOAL (Local_Defs.unfold0_tac ctxt @{thms type_definition_def}),
60231
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   447
        DETERM o Transfer.transfer_tac true ctxt,
63170
eae6549dbea2 tuned proofs, to allow unfold_abs_def;
wenzelm
parents: 63003
diff changeset
   448
          SELECT_GOAL (Local_Defs.unfold0_tac ctxt @{thms eq_onp_top_eq_eq}) (* normalize *), 
60231
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   449
          Raw_Simplifier.rewrite_goal_tac ctxt 
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   450
          (map safe_mk_meta_eq @{thms id_apply simp_thms Ball_def}),
60728
26ffdb966759 {r,e,d,f}tac with proper context in BNF
traytel
parents: 60669
diff changeset
   451
         rtac ctxt TrueI] i;
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   452
63352
4eaf35781b23 tuned signature;
wenzelm
parents: 63342
diff changeset
   453
    val (_, transfer_lthy) =
67713
041898537c19 tuned signature;
wenzelm
parents: 66017
diff changeset
   454
      Proof_Context.note_thms ""
041898537c19 tuned signature;
wenzelm
parents: 66017
diff changeset
   455
        (Binding.empty_atts,
041898537c19 tuned signature;
wenzelm
parents: 66017
diff changeset
   456
          [(@{thms right_total_UNIV_transfer}, [Transfer.transfer_add]),
041898537c19 tuned signature;
wenzelm
parents: 66017
diff changeset
   457
           (@{thms Domain_eq_top}, [Transfer.transfer_domain_add])]) lthy;
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   458
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   459
    val quot_thm_isom = Goal.prove_sorry transfer_lthy [] [] typedef_goal
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   460
      (fn {context = ctxt, prems = _} => typ_isom_tac ctxt 1)
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   461
      |> Thm.close_derivation
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   462
      |> singleton (Variable.export transfer_lthy lthy)
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   463
      |> (fn thm => @{thm UNIV_typedef_to_Quotient} OF [thm, @{thm reflexive}])
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   464
    val qty_isom_name = Tname qty_isom;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   465
    val quot_isom_rep =
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   466
      let
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   467
        val (quotients : Lifting_Term.quotients) = Symtab.insert (Lifting_Info.quotient_eq) (qty_isom_name,
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   468
          {quot_thm = quot_thm_isom, pcr_info = NONE}) Symtab.empty
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   469
        val id_actions = { constr = K I, lift = K I, comp_lift = K I }
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   470
      in
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   471
        fn ctxt => fn (rty, qty) => Lifting_Term.prove_schematic_quot_thm id_actions quotients
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   472
          ctxt (rty, qty) () |> fst |> Lifting_Term.force_qty_type ctxt qty
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   473
          |> quot_thm_rep
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   474
      end;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   475
    val x_lthy = lthy
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   476
    val (x, lthy) = yield_singleton (mk_Frees "x") qty_isom lthy;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   477
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   478
    fun mk_ctr ctr ctr_Ts sels =
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   479
      let
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   480
        val sel_ret_Ts = map (dest_Const #> snd #> body_type) sels;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   481
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   482
        fun rep_isom lthy t (rty, qty) =
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   483
          let
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   484
            val rep = quot_isom_rep lthy (rty, qty)
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   485
          in
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 67713
diff changeset
   486
            if is_Const rep andalso (rep |> dest_Const |> fst) = \<^const_name>\<open>id\<close> then
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   487
              t else rep $ t
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   488
          end;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   489
      in
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   490
        @{fold 3} (fn sel => fn ctr_T => fn sel_ret_T => fn ctr =>
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   491
          ctr $ rep_isom lthy (sel $ x) (ctr_T, sel_ret_T)) sels ctr_Ts sel_ret_Ts ctr
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   492
      end;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   493
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   494
    (* stolen from Metis *)
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   495
    exception BREAK_LIST
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   496
    fun break_list (x :: xs) = (x, xs)
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   497
      | break_list _ = raise BREAK_LIST
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   498
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   499
    val (ctr, ctrs) = qty_ctrs |> rev |> break_list;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   500
    val (ctr_Ts, ctr_Tss) = qty_ctr_Tss |> rev |> break_list;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   501
    val (sel, rselss) = selss |> rev |> break_list;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   502
    val rdiss = rev diss |> tl;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   503
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   504
    val first_ctr = mk_ctr ctr ctr_Ts sel;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   505
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   506
    fun mk_If_ctr dis ctr ctr_Ts sel elsex = mk_If (dis$x) (mk_ctr ctr ctr_Ts sel) elsex;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   507
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   508
    val rhs = @{fold 4} mk_If_ctr rdiss ctrs ctr_Tss rselss first_ctr;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   509
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   510
    val rep_isom_code_goal = HOLogic.mk_Trueprop (HOLogic.mk_eq (rep_isom$x, rhs));
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   511
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   512
    local
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   513
      val rep_isom_code_tac_rules = map safe_mk_meta_eq @{thms refl id_apply if_splits simp_thms}
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   514
    in
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   515
      fun rep_isom_code_tac (ctr_sugar:Ctr_Sugar.ctr_sugar) ctxt i =
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   516
        let
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   517
          val exhaust = ctr_sugar |> #exhaust
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   518
          val cases = ctr_sugar |> #case_thms
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   519
          val map_ids = fp |> #fp_nesting_bnfs |> map BNF_Def.map_id0_of_bnf
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   520
          val simp_rules = map safe_mk_meta_eq (cases @ map_ids) @ rep_isom_code_tac_rules
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   521
        in
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   522
          EVERY' [Transfer.gen_frees_tac [] ctxt, DETERM o (Transfer.transfer_tac true ctxt),
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   523
            case_tac exhaust ctxt THEN_ALL_NEW EVERY' [hyp_subst_tac ctxt,
60728
26ffdb966759 {r,e,d,f}tac with proper context in BNF
traytel
parents: 60669
diff changeset
   524
              Raw_Simplifier.rewrite_goal_tac ctxt simp_rules, rtac ctxt TrueI ]] i
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   525
        end
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   526
    end
60234
17ff843807cb more readable error message if some types do not correspond to sort constraints of the datatype
kuncar
parents: 60233
diff changeset
   527
    
17ff843807cb more readable error message if some types do not correspond to sort constraints of the datatype
kuncar
parents: 60233
diff changeset
   528
    (* stolen from bnf_fp_n2m.ML *)
17ff843807cb more readable error message if some types do not correspond to sort constraints of the datatype
kuncar
parents: 60233
diff changeset
   529
    fun force_typ ctxt T =
17ff843807cb more readable error message if some types do not correspond to sort constraints of the datatype
kuncar
parents: 60233
diff changeset
   530
      Term.map_types Type_Infer.paramify_vars
17ff843807cb more readable error message if some types do not correspond to sort constraints of the datatype
kuncar
parents: 60233
diff changeset
   531
      #> Type.constraint T
17ff843807cb more readable error message if some types do not correspond to sort constraints of the datatype
kuncar
parents: 60233
diff changeset
   532
      #> singleton (Type_Infer_Context.infer_types ctxt);
17ff843807cb more readable error message if some types do not correspond to sort constraints of the datatype
kuncar
parents: 60233
diff changeset
   533
17ff843807cb more readable error message if some types do not correspond to sort constraints of the datatype
kuncar
parents: 60233
diff changeset
   534
    (* The following tests that types in rty have corresponding arities imposed by constraints of
17ff843807cb more readable error message if some types do not correspond to sort constraints of the datatype
kuncar
parents: 60233
diff changeset
   535
       the datatype fp. Otherwise rep_isom_code_tac could fail (especially transfer in it) is such
17ff843807cb more readable error message if some types do not correspond to sort constraints of the datatype
kuncar
parents: 60233
diff changeset
   536
       a way that it is not easy to infer the problem with sorts.
17ff843807cb more readable error message if some types do not correspond to sort constraints of the datatype
kuncar
parents: 60233
diff changeset
   537
    *)
17ff843807cb more readable error message if some types do not correspond to sort constraints of the datatype
kuncar
parents: 60233
diff changeset
   538
    val _ = yield_singleton (mk_Frees "x") (#T fp) lthy |> fst |> force_typ lthy qty
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   539
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   540
    val rep_isom_code = Goal.prove_sorry lthy [] [] rep_isom_code_goal
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   541
      (fn {context = ctxt, prems = _} => rep_isom_code_tac ctr_sugar ctxt 1)
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   542
      |> Thm.close_derivation
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   543
      |> singleton(Variable.export lthy x_lthy)
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   544
    val lthy = x_lthy
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   545
    val lthy =
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   546
      lthy
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   547
      |> snd o Local_Theory.note ((Binding.empty, @{attributes [code]}), [rep_isom_code])
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   548
      |> Lifting_Setup.lifting_forget pointer
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   549
  in
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   550
    ((selss, diss, rep_isom_code), lthy)
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   551
  end
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   552
and constr qty (quot_thm, (lthy, rel_eq_onps)) =
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   553
  let
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   554
    val quot_thm = Lifting_Term.force_qty_type lthy qty quot_thm
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   555
    val (rty, qty) = quot_thm_rty_qty quot_thm
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   556
    val rty_name = Tname rty;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   557
    val pred_data = Transfer.lookup_pred_data lthy rty_name
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   558
    val pred_data = if is_some pred_data then the pred_data
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   559
      else error ("code_dt: " ^ quote rty_name ^ " is not a datatype.")
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   560
    val rel_eq_onp = safe_mk_meta_eq (Transfer.rel_eq_onp pred_data);
60231
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   561
    val rel_eq_onps = insert Thm.eq_thm rel_eq_onp rel_eq_onps
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   562
    val R_conv = Transfer.top_sweep_rewr_conv @{thms eq_onp_top_eq_eq[symmetric, THEN eq_reflection]}
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   563
      then_conv Conv.rewr_conv rel_eq_onp
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   564
    val quot_thm = Conv.fconv_rule(HOLogic.Trueprop_conv (Quotient_R_conv R_conv)) quot_thm;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   565
  in
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   566
    if is_none (code_dt_of lthy (rty, qty)) then
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   567
      let
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   568
        val non_empty_pred = quot_thm RS @{thm type_definition_Quotient_not_empty}
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   569
        val pred = quot_thm_rel quot_thm |> dest_comb |> snd;
66017
57acac0fd29b avoid Local_Theory.reset in application space
haftmann
parents: 63352
diff changeset
   570
        val (pred, lthy) =
57acac0fd29b avoid Local_Theory.reset in application space
haftmann
parents: 63352
diff changeset
   571
          lthy
57acac0fd29b avoid Local_Theory.reset in application space
haftmann
parents: 63352
diff changeset
   572
          |> Local_Theory.open_target |> snd
57acac0fd29b avoid Local_Theory.reset in application space
haftmann
parents: 63352
diff changeset
   573
          |> yield_singleton (Variable.import_terms true) pred;
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   574
        val TFrees = Term.add_tfreesT qty []
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   575
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   576
        fun non_empty_typedef_tac non_empty_pred ctxt i =
61841
4d3527b94f2a more general types Proof.method / context_tactic;
wenzelm
parents: 61466
diff changeset
   577
          (Method.insert_tac ctxt [non_empty_pred] THEN' 
63170
eae6549dbea2 tuned proofs, to allow unfold_abs_def;
wenzelm
parents: 63003
diff changeset
   578
            SELECT_GOAL (Local_Defs.unfold0_tac ctxt [mem_Collect_eq]) THEN' assume_tac ctxt) i
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   579
        val uTname = unique_Tname (rty, qty)
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   580
        val Tdef_set = HOLogic.mk_Collect ("x", rty, pred $ Free("x", rty));
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   581
        val ((_, tcode_dt), lthy) = conceal_naming_result (typedef (Binding.concealed uTname, TFrees, NoSyn)
60231
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   582
          Tdef_set NONE (fn lthy => HEADGOAL (non_empty_typedef_tac non_empty_pred lthy))) lthy;
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   583
        val type_definition_thm = tcode_dt |> snd |> #type_definition;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   584
        val qty_isom = tcode_dt |> fst |> #abs_type;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   585
66017
57acac0fd29b avoid Local_Theory.reset in application space
haftmann
parents: 63352
diff changeset
   586
        val (binding, lthy) =
57acac0fd29b avoid Local_Theory.reset in application space
haftmann
parents: 63352
diff changeset
   587
          lthy
57acac0fd29b avoid Local_Theory.reset in application space
haftmann
parents: 63352
diff changeset
   588
          |> conceal_naming_result (Lifting_Setup.setup_by_typedef_thm
57acac0fd29b avoid Local_Theory.reset in application space
haftmann
parents: 63352
diff changeset
   589
              { notes = false } type_definition_thm)
57acac0fd29b avoid Local_Theory.reset in application space
haftmann
parents: 63352
diff changeset
   590
          ||> Local_Theory.close_target
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   591
        val (wit, wit_thm) = mk_witness quot_thm;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   592
        val code_dt = mk_code_dt rty qty wit wit_thm NONE;
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   593
        val lthy = lthy
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   594
          |> update_code_dt code_dt
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   595
          |> mk_rep_isom binding (rty, qty, qty_isom) |> snd
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   596
      in
60231
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   597
        (quot_thm, (lthy, rel_eq_onps))
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   598
      end
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   599
    else
60231
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   600
      (quot_thm, (lthy, rel_eq_onps))
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   601
  end
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   602
and lift_def_code_dt config var qty rhs tac par_thms lthy = gen_lift_def (add_lift_def_code_dt config)
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   603
  var qty rhs tac par_thms lthy
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   604
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   605
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   606
(** from parsed parameters to the config record **)
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   607
60239
755e11e2e15d make SML/NJ more happy;
wenzelm
parents: 60236
diff changeset
   608
fun map_config_code_dt f1 f2 ({code_dt = code_dt, lift_config = lift_config}: config_code_dt) =
755e11e2e15d make SML/NJ more happy;
wenzelm
parents: 60236
diff changeset
   609
  {code_dt = f1 code_dt, lift_config = f2 lift_config}
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   610
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   611
fun update_config_code_dt nval = map_config_code_dt (K nval) I
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   612
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   613
val config_flags = [("code_dt", update_config_code_dt true)]
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   614
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   615
fun evaluate_params params =
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   616
  let
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   617
    fun eval_param param config =
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   618
      case AList.lookup (op =) config_flags param of
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   619
        SOME update => update config
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   620
        | NONE => error ("Unknown parameter: " ^ (quote param))
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   621
  in
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   622
    fold eval_param params default_config_code_dt
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   623
  end
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   624
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   625
(**
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   626
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   627
  lift_definition command. It opens a proof of a corresponding respectfulness
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   628
  theorem in a user-friendly, readable form. Then add_lift_def_code_dt is called internally.
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   629
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   630
**)
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   631
60231
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   632
local
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 67713
diff changeset
   633
  val eq_onp_assms_tac_fixed_rules = map (Transfer.prep_transfer_domain_thm \<^context>)
60231
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   634
    [@{thm pcr_Domainp_total}, @{thm pcr_Domainp_par_left_total}, @{thm pcr_Domainp_par}, 
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   635
      @{thm pcr_Domainp}]
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   636
in
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   637
fun mk_readable_rsp_thm_eq tm lthy =
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   638
  let
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   639
    val ctm = Thm.cterm_of lthy tm
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   640
    
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   641
    fun assms_rewr_conv tactic rule ct =
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   642
      let
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   643
        fun prove_extra_assms thm =
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   644
          let
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   645
            val assms = cprems_of thm
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   646
            fun finish thm = if Thm.no_prems thm then SOME (Goal.conclude thm) else NONE
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   647
            fun prove ctm = Option.mapPartial finish (SINGLE tactic (Goal.init ctm))
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   648
          in
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   649
            map_interrupt prove assms
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   650
          end
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   651
    
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   652
        fun cconl_of thm = Drule.strip_imp_concl (Thm.cprop_of thm)
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   653
        fun lhs_of thm = fst (Thm.dest_equals (cconl_of thm))
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   654
        fun rhs_of thm = snd (Thm.dest_equals (cconl_of thm))
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   655
        val rule1 = Thm.incr_indexes (Thm.maxidx_of_cterm ct + 1) rule;
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   656
        val lhs = lhs_of rule1;
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   657
        val rule2 = Thm.rename_boundvars (Thm.term_of lhs) (Thm.term_of ct) rule1;
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   658
        val rule3 =
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   659
          Thm.instantiate (Thm.match (lhs, ct)) rule2
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   660
            handle Pattern.MATCH => raise CTERM ("assms_rewr_conv", [lhs, ct]);
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   661
        val proved_assms = prove_extra_assms rule3
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   662
      in
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   663
        case proved_assms of
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   664
          SOME proved_assms =>
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   665
            let
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   666
              val rule3 = proved_assms MRSL rule3
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   667
              val rule4 =
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   668
                if lhs_of rule3 aconvc ct then rule3
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   669
                else
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   670
                  let val ceq = Thm.dest_fun2 (Thm.cprop_of rule3)
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   671
                  in rule3 COMP Thm.trivial (Thm.mk_binop ceq ct (rhs_of rule3)) end
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   672
            in Thm.transitive rule4 (Thm.beta_conversion true (rhs_of rule4)) end
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   673
          | NONE => Conv.no_conv ct
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   674
      end
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   675
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   676
    fun assms_rewrs_conv tactic rules = Conv.first_conv (map (assms_rewr_conv tactic) rules)
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   677
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   678
    fun simp_arrows_conv ctm =
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   679
      let
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   680
        val unfold_conv = Conv.rewrs_conv 
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   681
          [@{thm rel_fun_eq_eq_onp[THEN eq_reflection]}, 
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   682
            @{thm rel_fun_eq_onp_rel[THEN eq_reflection]},
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   683
            @{thm rel_fun_eq[THEN eq_reflection]},
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   684
            @{thm rel_fun_eq_rel[THEN eq_reflection]}, 
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   685
            @{thm rel_fun_def[THEN eq_reflection]}]
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   686
        fun binop_conv2 cv1 cv2 = Conv.combination_conv (Conv.arg_conv cv1) cv2
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   687
        val eq_onp_assms_tac_rules = @{thm left_unique_OO} :: 
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   688
            eq_onp_assms_tac_fixed_rules @ (Transfer.get_transfer_raw lthy)
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   689
        val intro_top_rule = @{thm eq_onp_top_eq_eq[symmetric, THEN eq_reflection]}
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   690
        val kill_tops = Transfer.top_sweep_rewr_conv [@{thm eq_onp_top_eq_eq[THEN eq_reflection]}]
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   691
        val eq_onp_assms_tac = (CONVERSION kill_tops THEN' 
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   692
          TRY o REPEAT_ALL_NEW (resolve_tac lthy eq_onp_assms_tac_rules) 
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   693
          THEN_ALL_NEW (DETERM o Transfer.eq_tac lthy)) 1
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   694
        val relator_eq_onp_conv = Conv.bottom_conv
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   695
          (K (Conv.try_conv (assms_rewrs_conv eq_onp_assms_tac
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   696
            (intro_top_rule :: Lifting_Info.get_relator_eq_onp_rules lthy)))) lthy
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   697
          then_conv kill_tops
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   698
        val relator_eq_conv = Conv.bottom_conv
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   699
          (K (Conv.try_conv (Conv.rewrs_conv (Transfer.get_relator_eq lthy)))) lthy
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   700
      in
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   701
        case (Thm.term_of ctm) of
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 67713
diff changeset
   702
          Const (\<^const_name>\<open>rel_fun\<close>, _) $ _ $ _ => 
60231
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   703
            (binop_conv2 simp_arrows_conv simp_arrows_conv then_conv unfold_conv) ctm
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   704
          | _ => (relator_eq_onp_conv then_conv relator_eq_conv) ctm
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   705
      end
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   706
    
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   707
    val unfold_ret_val_invs = Conv.bottom_conv 
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   708
      (K (Conv.try_conv (Conv.rewr_conv @{thm eq_onp_same_args[THEN eq_reflection]}))) lthy
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   709
    val unfold_inv_conv = 
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   710
      Conv.top_sweep_conv (K (Conv.rewr_conv @{thm eq_onp_def[THEN eq_reflection]})) lthy
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   711
    val simp_conv = HOLogic.Trueprop_conv (Conv.fun2_conv simp_arrows_conv)
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   712
    val univq_conv = Conv.rewr_conv @{thm HOL.all_simps(6)[symmetric, THEN eq_reflection]}
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   713
    val univq_prenex_conv = Conv.top_conv (K (Conv.try_conv univq_conv)) lthy
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   714
    val beta_conv = Thm.beta_conversion true
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   715
    val eq_thm = 
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   716
      (simp_conv then_conv univq_prenex_conv then_conv beta_conv then_conv unfold_ret_val_invs
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   717
         then_conv unfold_inv_conv) ctm
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   718
  in
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   719
    Object_Logic.rulify lthy (eq_thm RS Drule.equal_elim_rule2)
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   720
  end
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   721
end
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   722
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   723
fun rename_to_tnames ctxt term =
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   724
  let
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 67713
diff changeset
   725
    fun all_typs (Const (\<^const_name>\<open>Pure.all\<close>, _) $ Abs (_, T, t)) = T :: all_typs t
60231
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   726
      | all_typs _ = []
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   727
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 67713
diff changeset
   728
    fun rename (Const (\<^const_name>\<open>Pure.all\<close>, T1) $ Abs (_, T2, t)) (new_name :: names) = 
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 67713
diff changeset
   729
        (Const (\<^const_name>\<open>Pure.all\<close>, T1) $ Abs (new_name, T2, rename t names)) 
60231
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   730
      | rename t _ = t
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   731
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   732
    val (fixed_def_t, _) = yield_singleton (Variable.importT_terms) term ctxt
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   733
    val new_names = Old_Datatype_Prop.make_tnames (all_typs fixed_def_t)
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   734
  in
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   735
    rename term new_names
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   736
  end
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   737
60235
3cab6f891c2f reorder some steps in the construction to support mutual datatypes
kuncar
parents: 60234
diff changeset
   738
fun quot_thm_err ctxt (rty, qty) pretty_msg =
3cab6f891c2f reorder some steps in the construction to support mutual datatypes
kuncar
parents: 60234
diff changeset
   739
  let
3cab6f891c2f reorder some steps in the construction to support mutual datatypes
kuncar
parents: 60234
diff changeset
   740
    val error_msg = cat_lines
3cab6f891c2f reorder some steps in the construction to support mutual datatypes
kuncar
parents: 60234
diff changeset
   741
       ["Lifting failed for the following types:",
3cab6f891c2f reorder some steps in the construction to support mutual datatypes
kuncar
parents: 60234
diff changeset
   742
        Pretty.string_of (Pretty.block
3cab6f891c2f reorder some steps in the construction to support mutual datatypes
kuncar
parents: 60234
diff changeset
   743
         [Pretty.str "Raw type:", Pretty.brk 2, Syntax.pretty_typ ctxt rty]),
3cab6f891c2f reorder some steps in the construction to support mutual datatypes
kuncar
parents: 60234
diff changeset
   744
        Pretty.string_of (Pretty.block
3cab6f891c2f reorder some steps in the construction to support mutual datatypes
kuncar
parents: 60234
diff changeset
   745
         [Pretty.str "Abstract type:", Pretty.brk 2, Syntax.pretty_typ ctxt qty]),
3cab6f891c2f reorder some steps in the construction to support mutual datatypes
kuncar
parents: 60234
diff changeset
   746
        "",
3cab6f891c2f reorder some steps in the construction to support mutual datatypes
kuncar
parents: 60234
diff changeset
   747
        (Pretty.string_of (Pretty.block
3cab6f891c2f reorder some steps in the construction to support mutual datatypes
kuncar
parents: 60234
diff changeset
   748
         [Pretty.str "Reason:", Pretty.brk 2, pretty_msg]))]
3cab6f891c2f reorder some steps in the construction to support mutual datatypes
kuncar
parents: 60234
diff changeset
   749
  in
3cab6f891c2f reorder some steps in the construction to support mutual datatypes
kuncar
parents: 60234
diff changeset
   750
    error error_msg
3cab6f891c2f reorder some steps in the construction to support mutual datatypes
kuncar
parents: 60234
diff changeset
   751
  end
3cab6f891c2f reorder some steps in the construction to support mutual datatypes
kuncar
parents: 60234
diff changeset
   752
60236
865741686926 handle error messages also in after_qed
kuncar
parents: 60235
diff changeset
   753
fun check_rty_err ctxt (rty_schematic, rty_forced) (raw_var, rhs_raw) =
865741686926 handle error messages also in after_qed
kuncar
parents: 60235
diff changeset
   754
  let
60379
51d9dcd71ad7 tuned signature;
wenzelm
parents: 60239
diff changeset
   755
    val (_, ctxt') = Proof_Context.read_var raw_var ctxt
60485
wenzelm
parents: 60379
diff changeset
   756
    val rhs = Syntax.read_term ctxt' rhs_raw
60236
865741686926 handle error messages also in after_qed
kuncar
parents: 60235
diff changeset
   757
    val error_msg = cat_lines
865741686926 handle error messages also in after_qed
kuncar
parents: 60235
diff changeset
   758
       ["Lifting failed for the following term:",
865741686926 handle error messages also in after_qed
kuncar
parents: 60235
diff changeset
   759
        Pretty.string_of (Pretty.block
865741686926 handle error messages also in after_qed
kuncar
parents: 60235
diff changeset
   760
         [Pretty.str "Term:", Pretty.brk 2, Syntax.pretty_term ctxt rhs]),
865741686926 handle error messages also in after_qed
kuncar
parents: 60235
diff changeset
   761
        Pretty.string_of (Pretty.block
865741686926 handle error messages also in after_qed
kuncar
parents: 60235
diff changeset
   762
         [Pretty.str "Type:", Pretty.brk 2, Syntax.pretty_typ ctxt rty_schematic]),
865741686926 handle error messages also in after_qed
kuncar
parents: 60235
diff changeset
   763
        "",
865741686926 handle error messages also in after_qed
kuncar
parents: 60235
diff changeset
   764
        (Pretty.string_of (Pretty.block
865741686926 handle error messages also in after_qed
kuncar
parents: 60235
diff changeset
   765
         [Pretty.str "Reason:",
865741686926 handle error messages also in after_qed
kuncar
parents: 60235
diff changeset
   766
          Pretty.brk 2,
865741686926 handle error messages also in after_qed
kuncar
parents: 60235
diff changeset
   767
          Pretty.str "The type of the term cannot be instantiated to",
865741686926 handle error messages also in after_qed
kuncar
parents: 60235
diff changeset
   768
          Pretty.brk 1,
865741686926 handle error messages also in after_qed
kuncar
parents: 60235
diff changeset
   769
          Pretty.quote (Syntax.pretty_typ ctxt rty_forced),
865741686926 handle error messages also in after_qed
kuncar
parents: 60235
diff changeset
   770
          Pretty.str "."]))]
865741686926 handle error messages also in after_qed
kuncar
parents: 60235
diff changeset
   771
    in
865741686926 handle error messages also in after_qed
kuncar
parents: 60235
diff changeset
   772
      error error_msg
865741686926 handle error messages also in after_qed
kuncar
parents: 60235
diff changeset
   773
    end
865741686926 handle error messages also in after_qed
kuncar
parents: 60235
diff changeset
   774
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   775
fun lift_def_cmd (params, raw_var, rhs_raw, par_xthms) lthy =
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   776
  let
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   777
    val config = evaluate_params params
60379
51d9dcd71ad7 tuned signature;
wenzelm
parents: 60239
diff changeset
   778
    val ((binding, SOME qty, mx), lthy) = Proof_Context.read_var raw_var lthy
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   779
    val var = (binding, mx)
60485
wenzelm
parents: 60379
diff changeset
   780
    val rhs = Syntax.read_term lthy rhs_raw
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   781
    val par_thms = Attrib.eval_thms lthy par_xthms
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   782
    val (goal, after_qed) = prepare_lift_def (add_lift_def_code_dt config) var qty rhs par_thms lthy
60231
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   783
    val (goal, after_qed) =
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   784
      case goal of
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   785
        NONE => (goal, K (after_qed Drule.dummy_thm))
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   786
        | SOME prsp_tm =>
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   787
          let
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   788
            val readable_rsp_thm_eq = mk_readable_rsp_thm_eq prsp_tm lthy
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   789
            val (readable_rsp_tm, _) = Logic.dest_implies (Thm.prop_of readable_rsp_thm_eq)
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   790
            val readable_rsp_tm_tnames = rename_to_tnames lthy readable_rsp_tm
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   791
        
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   792
            fun after_qed' [[thm]] lthy = 
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   793
              let
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   794
                val internal_rsp_thm = Goal.prove lthy [] [] prsp_tm 
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   795
                    (fn {context = ctxt, ...} =>
60728
26ffdb966759 {r,e,d,f}tac with proper context in BNF
traytel
parents: 60669
diff changeset
   796
                      rtac ctxt readable_rsp_thm_eq 1 THEN Proof_Context.fact_tac ctxt [thm] 1)
60231
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   797
              in
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   798
                after_qed internal_rsp_thm lthy
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   799
              end
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   800
          in
0daab758e087 don't use the human-readable version of the rsp thm as a goal in the ML interface (there is no formal definition of its statement); make tactics more robust wrt. predicates in predicators; tuned
kuncar
parents: 60230
diff changeset
   801
            (SOME readable_rsp_tm_tnames, after_qed')
60236
865741686926 handle error messages also in after_qed
kuncar
parents: 60235
diff changeset
   802
          end
865741686926 handle error messages also in after_qed
kuncar
parents: 60235
diff changeset
   803
    fun after_qed_with_err_handling thmss ctxt = (after_qed thmss ctxt
865741686926 handle error messages also in after_qed
kuncar
parents: 60235
diff changeset
   804
      handle Lifting_Term.QUOT_THM (rty, qty, msg) => quot_thm_err lthy (rty, qty) msg)
865741686926 handle error messages also in after_qed
kuncar
parents: 60235
diff changeset
   805
      handle Lifting_Term.CHECK_RTY (rty_schematic, rty_forced) =>
865741686926 handle error messages also in after_qed
kuncar
parents: 60235
diff changeset
   806
        check_rty_err lthy (rty_schematic, rty_forced) (raw_var, rhs_raw);
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   807
  in
60236
865741686926 handle error messages also in after_qed
kuncar
parents: 60235
diff changeset
   808
    Proof.theorem NONE (snd oo after_qed_with_err_handling) [map (rpair []) (the_list goal)] lthy
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   809
  end
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   810
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   811
fun lift_def_cmd_with_err_handling (params, (raw_var, rhs_raw, par_xthms)) lthy =
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   812
  (lift_def_cmd (params, raw_var, rhs_raw, par_xthms) lthy
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   813
    handle Lifting_Term.QUOT_THM (rty, qty, msg) => quot_thm_err lthy (rty, qty) msg)
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   814
    handle Lifting_Term.CHECK_RTY (rty_schematic, rty_forced) =>
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   815
      check_rty_err lthy (rty_schematic, rty_forced) (raw_var, rhs_raw);
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   816
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   817
val parse_param = Parse.name
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   818
val parse_params = Scan.optional (Args.parens (Parse.list parse_param)) [];
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   819
60669
wenzelm
parents: 60485
diff changeset
   820
wenzelm
parents: 60485
diff changeset
   821
(* command syntax *)
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   822
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   823
val _ =
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 67713
diff changeset
   824
  Outer_Syntax.local_theory_to_proof \<^command_keyword>\<open>lift_definition\<close>
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   825
    "definition for constants over the quotient type"
60669
wenzelm
parents: 60485
diff changeset
   826
    (parse_params --
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 67713
diff changeset
   827
      (((Parse.binding -- (\<^keyword>\<open>::\<close> |-- (Parse.typ >> SOME) -- Parse.opt_mixfix')
61466
9a468c3a1fa1 tuned signature;
wenzelm
parents: 60784
diff changeset
   828
          >> Scan.triple2) --
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 67713
diff changeset
   829
        (\<^keyword>\<open>is\<close> |-- Parse.term) --
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 67713
diff changeset
   830
        Scan.optional (\<^keyword>\<open>parametric\<close> |-- Parse.!!! Parse.thms1) []) >> Scan.triple1)
60669
wenzelm
parents: 60485
diff changeset
   831
     >> lift_def_cmd_with_err_handling);
60229
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   832
4cd6462c1fda Workaround that allows us to execute lifted constants that have as a return type a datatype containing a subtype
kuncar
parents:
diff changeset
   833
end