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