author | haftmann |
Fri, 09 Mar 2007 08:45:53 +0100 | |
changeset 22423 | c1836b14c63a |
parent 21708 | 45e7491bea47 |
child 22484 | 25dfebd7b4c8 |
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 |
21 |
val get_typecopy_info: theory -> string -> info option |
|
20835 | 22 |
type hook = string * info -> theory -> theory |
23 |
val add_hook: hook -> theory -> theory |
|
24 |
val get_spec: theory -> string -> (string * sort) list * (string * typ list) list |
|
25 |
val get_eq: theory -> string -> thm |
|
20426 | 26 |
val print: theory -> unit |
27 |
val setup: theory -> theory |
|
28 |
end; |
|
29 |
||
30 |
structure TypecopyPackage: TYPECOPY_PACKAGE = |
|
31 |
struct |
|
32 |
||
33 |
(* theory data *) |
|
34 |
||
35 |
type info = { |
|
36 |
vs: (string * sort) list, |
|
37 |
constr: string, |
|
38 |
typ: typ, |
|
39 |
inject: thm, |
|
40 |
proj: string * typ, |
|
41 |
proj_def: thm |
|
42 |
}; |
|
43 |
||
44 |
type hook = string * info -> theory -> theory; |
|
45 |
||
46 |
structure TypecopyData = TheoryDataFun |
|
47 |
(struct |
|
48 |
val name = "HOL/typecopy_package"; |
|
49 |
type T = info Symtab.table * (serial * hook) list; |
|
50 |
val empty = (Symtab.empty, []); |
|
51 |
val copy = I; |
|
52 |
val extend = I; |
|
53 |
fun merge _ ((tab1, hooks1), (tab2, hooks2) : T) = |
|
54 |
(Symtab.merge (K true) (tab1, tab2), AList.merge (op =) (K true) (hooks1, hooks2)); |
|
55 |
fun print thy (tab, _) = |
|
56 |
let |
|
57 |
fun mk (tyco, { vs, constr, typ, proj = (proj, _), ... } : info) = |
|
58 |
(Pretty.block o Pretty.breaks) [ |
|
59 |
Sign.pretty_typ thy (Type (tyco, map TFree vs)), |
|
60 |
Pretty.str "=", |
|
61 |
(Pretty.str o Sign.extern_const thy) constr, |
|
62 |
Sign.pretty_typ thy typ, |
|
63 |
Pretty.block [Pretty.str "(", (Pretty.str o Sign.extern_const thy) proj, Pretty.str ")"] |
|
64 |
]; |
|
65 |
in |
|
66 |
(Pretty.writeln o Pretty.block o Pretty.fbreaks) ( |
|
67 |
Pretty.str "type copies:" |
|
68 |
:: map mk (Symtab.dest tab) |
|
69 |
) |
|
70 |
end; |
|
71 |
end); |
|
72 |
||
73 |
val print = TypecopyData.print; |
|
74 |
val get_typecopies = Symtab.keys o fst o TypecopyData.get; |
|
75 |
val get_typecopy_info = Symtab.lookup o fst o TypecopyData.get; |
|
76 |
||
77 |
||
78 |
(* hook management *) |
|
79 |
||
80 |
fun add_hook hook = |
|
81 |
(TypecopyData.map o apsnd o cons) (serial (), hook); |
|
82 |
||
83 |
fun invoke_hooks tyco_info thy = |
|
84 |
fold (fn (_, f) => f tyco_info) ((snd o TypecopyData.get) thy) thy; |
|
85 |
||
86 |
||
87 |
(* add a type copy *) |
|
88 |
||
89 |
local |
|
90 |
||
20483
04aa552a83bc
TypedefPackage.add_typedef_* now yields name of introduced type constructor
haftmann
parents:
20426
diff
changeset
|
91 |
fun gen_add_typecopy prep_typ (raw_tyco, raw_vs) raw_ty constr_proj thy = |
20426 | 92 |
let |
93 |
val ty = prep_typ thy raw_ty; |
|
94 |
val vs = AList.make (the_default HOLogic.typeS o AList.lookup (op =) (typ_tfrees ty)) raw_vs; |
|
95 |
val tac = Tactic.rtac UNIV_witness 1; |
|
20483
04aa552a83bc
TypedefPackage.add_typedef_* now yields name of introduced type constructor
haftmann
parents:
20426
diff
changeset
|
96 |
fun add_info tyco ( { abs_type = ty_abs, rep_type = ty_rep, Abs_name = c_abs, |
20426 | 97 |
Rep_name = c_rep, Abs_inject = inject, |
21708 | 98 |
Abs_inverse = inverse, ... } : TypedefPackage.info ) thy = |
20426 | 99 |
let |
100 |
val exists_thm = |
|
101 |
UNIV_I |
|
102 |
|> Drule.instantiate' [SOME (ctyp_of thy (Logic.varifyT ty_rep))] []; |
|
103 |
val inject' = inject OF [exists_thm, exists_thm]; |
|
104 |
val proj_def = inverse OF [exists_thm]; |
|
105 |
val info = { |
|
106 |
vs = vs, |
|
107 |
constr = c_abs, |
|
108 |
typ = ty_rep, |
|
109 |
inject = inject', |
|
110 |
proj = (c_rep, ty_abs --> ty_rep), |
|
111 |
proj_def = proj_def |
|
112 |
}; |
|
113 |
in |
|
114 |
thy |
|
20483
04aa552a83bc
TypedefPackage.add_typedef_* now yields name of introduced type constructor
haftmann
parents:
20426
diff
changeset
|
115 |
|> (TypecopyData.map o apfst o Symtab.update_new) (tyco, info) |
04aa552a83bc
TypedefPackage.add_typedef_* now yields name of introduced type constructor
haftmann
parents:
20426
diff
changeset
|
116 |
|> invoke_hooks (tyco, info) |
04aa552a83bc
TypedefPackage.add_typedef_* now yields name of introduced type constructor
haftmann
parents:
20426
diff
changeset
|
117 |
|> pair (tyco, info) |
20426 | 118 |
end |
119 |
in |
|
120 |
thy |
|
20483
04aa552a83bc
TypedefPackage.add_typedef_* now yields name of introduced type constructor
haftmann
parents:
20426
diff
changeset
|
121 |
|> setmp TypedefPackage.quiet_mode true |
04aa552a83bc
TypedefPackage.add_typedef_* now yields name of introduced type constructor
haftmann
parents:
20426
diff
changeset
|
122 |
(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
|
123 |
(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
|
124 |
|-> (fn (tyco, info) => add_info tyco info) |
20426 | 125 |
end; |
126 |
||
127 |
in |
|
128 |
||
129 |
val add_typecopy = gen_add_typecopy Sign.certify_typ; |
|
130 |
||
131 |
end; (*local*) |
|
132 |
||
133 |
||
22423 | 134 |
(* equality function equation and datatype specification *) |
20835 | 135 |
|
136 |
fun get_eq thy tyco = |
|
137 |
(#inject o the o get_typecopy_info thy) tyco; |
|
138 |
||
139 |
fun get_spec thy tyco = |
|
140 |
let |
|
141 |
val SOME { vs, constr, typ, ... } = get_typecopy_info thy tyco |
|
142 |
in (vs, [(constr, [typ])]) end; |
|
143 |
||
144 |
||
145 |
(* hook for projection function code *) |
|
20426 | 146 |
|
21708 | 147 |
fun add_project (_ , {proj_def, ...} : info) = |
20597 | 148 |
CodegenData.add_func proj_def; |
20426 | 149 |
|
150 |
val setup = |
|
151 |
TypecopyData.init |
|
20597 | 152 |
#> add_hook add_project; |
20426 | 153 |
|
154 |
end; (*struct*) |