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