src/HOL/Tools/typecopy_package.ML
author wenzelm
Tue Sep 25 15:34:35 2007 +0200 (2007-09-25)
changeset 24711 e8bba7723858
parent 24626 85eceef2edc7
child 25489 5b0625f6e324
permissions -rw-r--r--
simplified interpretation setup;
haftmann@20426
     1
(*  Title:      HOL/Tools/typecopy_package.ML
haftmann@20426
     2
    ID:         $Id$
haftmann@20426
     3
    Author:     Florian Haftmann, TU Muenchen
haftmann@20426
     4
haftmann@20855
     5
Introducing copies of types using trivial typedefs; datatype-like abstraction.
haftmann@20426
     6
*)
haftmann@20426
     7
haftmann@20426
     8
signature TYPECOPY_PACKAGE =
haftmann@20426
     9
sig
haftmann@20426
    10
  type info = {
haftmann@20426
    11
    vs: (string * sort) list,
haftmann@20426
    12
    constr: string,
haftmann@20426
    13
    typ: typ,
haftmann@20426
    14
    inject: thm,
haftmann@20426
    15
    proj: string * typ,
haftmann@20426
    16
    proj_def: thm
haftmann@20426
    17
  }
haftmann@20426
    18
  val add_typecopy: bstring * string list -> typ -> (bstring * bstring) option
haftmann@20483
    19
    -> theory -> (string * info) * theory
haftmann@20426
    20
  val get_typecopies: theory -> string list
haftmann@20426
    21
  val get_typecopy_info: theory -> string -> info option
wenzelm@24711
    22
  val interpretation: (string * info -> theory -> theory) -> theory -> theory
haftmann@20835
    23
  val get_spec: theory -> string -> (string * sort) list * (string * typ list) list
haftmann@20835
    24
  val get_eq: theory -> string -> thm
wenzelm@22846
    25
  val print_typecopies: theory -> unit
haftmann@20426
    26
  val setup: theory -> theory
haftmann@20426
    27
end;
haftmann@20426
    28
haftmann@20426
    29
structure TypecopyPackage: TYPECOPY_PACKAGE =
haftmann@20426
    30
struct
haftmann@20426
    31
haftmann@20426
    32
(* theory data *)
haftmann@20426
    33
haftmann@20426
    34
type info = {
haftmann@20426
    35
  vs: (string * sort) list,
haftmann@20426
    36
  constr: string,
haftmann@20426
    37
  typ: typ,
haftmann@20426
    38
  inject: thm,
haftmann@20426
    39
  proj: string * typ,
haftmann@20426
    40
  proj_def: thm
haftmann@20426
    41
};
haftmann@20426
    42
haftmann@20426
    43
structure TypecopyData = TheoryDataFun
wenzelm@22846
    44
(
haftmann@24626
    45
  type T = info Symtab.table;
haftmann@24626
    46
  val empty = Symtab.empty;
haftmann@20426
    47
  val copy = I;
haftmann@20426
    48
  val extend = I;
haftmann@24626
    49
  fun merge _ = Symtab.merge (K true);
wenzelm@22846
    50
);
wenzelm@22846
    51
wenzelm@22846
    52
fun print_typecopies thy =
wenzelm@22846
    53
  let
haftmann@24626
    54
    val tab = TypecopyData.get thy;
wenzelm@22846
    55
    fun mk (tyco, { vs, constr, typ, proj = (proj, _), ... } : info) =
wenzelm@22846
    56
      (Pretty.block o Pretty.breaks) [
wenzelm@22846
    57
        Sign.pretty_typ thy (Type (tyco, map TFree vs)),
wenzelm@22846
    58
        Pretty.str "=",
wenzelm@22846
    59
        (Pretty.str o Sign.extern_const thy) constr,
wenzelm@22846
    60
        Sign.pretty_typ thy typ,
wenzelm@22846
    61
        Pretty.block [Pretty.str "(", (Pretty.str o Sign.extern_const thy) proj, Pretty.str  ")"]];
haftmann@20426
    62
    in
wenzelm@22846
    63
      (Pretty.writeln o Pretty.block o Pretty.fbreaks)
wenzelm@22846
    64
        (Pretty.str "type copies:" :: map mk (Symtab.dest tab))
haftmann@20426
    65
    end;
haftmann@20426
    66
haftmann@24626
    67
val get_typecopies = Symtab.keys o TypecopyData.get;
haftmann@24626
    68
val get_typecopy_info = Symtab.lookup o TypecopyData.get;
haftmann@20426
    69
haftmann@20426
    70
wenzelm@24711
    71
(* interpretation *)
haftmann@20426
    72
wenzelm@24711
    73
structure TypecopyInterpretation = InterpretationFun(type T = string val eq = op =);
haftmann@20426
    74
wenzelm@24711
    75
fun interpretation interp = TypecopyInterpretation.interpretation
wenzelm@24711
    76
  (fn tyco => fn thy => interp (tyco, (the o get_typecopy_info thy) tyco) thy);
haftmann@20426
    77
haftmann@20426
    78
haftmann@20426
    79
(* add a type copy *)
haftmann@20426
    80
haftmann@20426
    81
local
haftmann@20426
    82
haftmann@20483
    83
fun gen_add_typecopy prep_typ (raw_tyco, raw_vs) raw_ty constr_proj thy =
haftmann@20426
    84
  let
haftmann@20426
    85
    val ty = prep_typ thy raw_ty;
haftmann@20426
    86
    val vs = AList.make (the_default HOLogic.typeS o AList.lookup (op =) (typ_tfrees ty)) raw_vs;
haftmann@20426
    87
    val tac = Tactic.rtac UNIV_witness 1;
haftmann@20483
    88
    fun add_info tyco ( { abs_type = ty_abs, rep_type = ty_rep, Abs_name = c_abs,
haftmann@20426
    89
      Rep_name = c_rep, Abs_inject = inject,
wenzelm@21708
    90
      Abs_inverse = inverse, ... } : TypedefPackage.info ) thy =
haftmann@20426
    91
        let
haftmann@20426
    92
          val exists_thm =
haftmann@20426
    93
            UNIV_I
haftmann@20426
    94
            |> Drule.instantiate' [SOME (ctyp_of thy (Logic.varifyT ty_rep))] [];
haftmann@20426
    95
          val inject' = inject OF [exists_thm, exists_thm];
haftmann@20426
    96
          val proj_def = inverse OF [exists_thm];
haftmann@20426
    97
          val info = {
haftmann@20426
    98
            vs = vs,
haftmann@20426
    99
            constr = c_abs,
haftmann@20426
   100
            typ = ty_rep,
haftmann@20426
   101
            inject = inject',
haftmann@20426
   102
            proj = (c_rep, ty_abs --> ty_rep),
haftmann@20426
   103
            proj_def = proj_def
haftmann@20426
   104
          };
haftmann@20426
   105
        in
haftmann@20426
   106
          thy
haftmann@24626
   107
          |> (TypecopyData.map o Symtab.update_new) (tyco, info)
wenzelm@24711
   108
          |> TypecopyInterpretation.data tyco
haftmann@20483
   109
          |> pair (tyco, info)
haftmann@20426
   110
        end
haftmann@20426
   111
  in
haftmann@20426
   112
    thy
haftmann@20483
   113
    |> setmp TypedefPackage.quiet_mode true
haftmann@20483
   114
        (TypedefPackage.add_typedef_i false (SOME raw_tyco) (raw_tyco, map fst vs, NoSyn)
haftmann@20483
   115
          (HOLogic.mk_UNIV ty) (Option.map swap constr_proj)) tac
haftmann@20483
   116
    |-> (fn (tyco, info) => add_info tyco info)
haftmann@20426
   117
  end;
haftmann@20426
   118
haftmann@20426
   119
in
haftmann@20426
   120
haftmann@20426
   121
val add_typecopy = gen_add_typecopy Sign.certify_typ;
haftmann@20426
   122
wenzelm@22846
   123
end;
haftmann@20426
   124
haftmann@20426
   125
haftmann@22423
   126
(* equality function equation and datatype specification *)
haftmann@20835
   127
haftmann@20835
   128
fun get_eq thy tyco =
haftmann@20835
   129
  (#inject o the o get_typecopy_info thy) tyco;
haftmann@20835
   130
haftmann@20835
   131
fun get_spec thy tyco =
haftmann@20835
   132
  let
haftmann@20835
   133
    val SOME { vs, constr, typ, ... } = get_typecopy_info thy tyco
haftmann@20835
   134
  in (vs, [(constr, [typ])]) end;
haftmann@20835
   135
haftmann@20835
   136
wenzelm@24711
   137
(* interpretation for projection function code *)
haftmann@20426
   138
wenzelm@24711
   139
fun add_project (_, {proj_def, ...} : info) = Code.add_default_func proj_def;
haftmann@20426
   140
wenzelm@24711
   141
val setup = TypecopyInterpretation.init #> interpretation add_project;
haftmann@20426
   142
wenzelm@22846
   143
end;