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