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@25506
|
21 |
val get_info: theory -> string -> info option
|
haftmann@25489
|
22 |
val interpretation: (string -> theory -> theory) -> theory -> theory
|
wenzelm@22846
|
23 |
val print_typecopies: theory -> unit
|
haftmann@20426
|
24 |
val setup: theory -> theory
|
haftmann@20426
|
25 |
end;
|
haftmann@20426
|
26 |
|
haftmann@20426
|
27 |
structure TypecopyPackage: TYPECOPY_PACKAGE =
|
haftmann@20426
|
28 |
struct
|
haftmann@20426
|
29 |
|
haftmann@20426
|
30 |
(* theory data *)
|
haftmann@20426
|
31 |
|
haftmann@20426
|
32 |
type info = {
|
haftmann@20426
|
33 |
vs: (string * sort) list,
|
haftmann@20426
|
34 |
constr: string,
|
haftmann@20426
|
35 |
typ: typ,
|
haftmann@20426
|
36 |
inject: thm,
|
haftmann@20426
|
37 |
proj: string * typ,
|
haftmann@20426
|
38 |
proj_def: thm
|
haftmann@20426
|
39 |
};
|
haftmann@20426
|
40 |
|
haftmann@20426
|
41 |
structure TypecopyData = TheoryDataFun
|
wenzelm@22846
|
42 |
(
|
haftmann@24626
|
43 |
type T = info Symtab.table;
|
haftmann@24626
|
44 |
val empty = Symtab.empty;
|
haftmann@20426
|
45 |
val copy = I;
|
haftmann@20426
|
46 |
val extend = I;
|
haftmann@24626
|
47 |
fun merge _ = Symtab.merge (K true);
|
wenzelm@22846
|
48 |
);
|
wenzelm@22846
|
49 |
|
wenzelm@22846
|
50 |
fun print_typecopies thy =
|
wenzelm@22846
|
51 |
let
|
haftmann@24626
|
52 |
val tab = TypecopyData.get thy;
|
wenzelm@22846
|
53 |
fun mk (tyco, { vs, constr, typ, proj = (proj, _), ... } : info) =
|
wenzelm@22846
|
54 |
(Pretty.block o Pretty.breaks) [
|
wenzelm@26939
|
55 |
Syntax.pretty_typ_global thy (Type (tyco, map TFree vs)),
|
wenzelm@22846
|
56 |
Pretty.str "=",
|
wenzelm@22846
|
57 |
(Pretty.str o Sign.extern_const thy) constr,
|
wenzelm@26939
|
58 |
Syntax.pretty_typ_global thy typ,
|
wenzelm@22846
|
59 |
Pretty.block [Pretty.str "(", (Pretty.str o Sign.extern_const thy) proj, Pretty.str ")"]];
|
haftmann@20426
|
60 |
in
|
wenzelm@22846
|
61 |
(Pretty.writeln o Pretty.block o Pretty.fbreaks)
|
wenzelm@22846
|
62 |
(Pretty.str "type copies:" :: map mk (Symtab.dest tab))
|
haftmann@20426
|
63 |
end;
|
haftmann@20426
|
64 |
|
haftmann@24626
|
65 |
val get_typecopies = Symtab.keys o TypecopyData.get;
|
haftmann@25506
|
66 |
val get_info = Symtab.lookup o TypecopyData.get;
|
haftmann@20426
|
67 |
|
haftmann@20426
|
68 |
|
haftmann@25489
|
69 |
(* interpretation of type copies *)
|
haftmann@20426
|
70 |
|
wenzelm@24711
|
71 |
structure TypecopyInterpretation = InterpretationFun(type T = string val eq = op =);
|
haftmann@25489
|
72 |
val interpretation = TypecopyInterpretation.interpretation;
|
haftmann@20426
|
73 |
|
haftmann@20426
|
74 |
|
haftmann@20426
|
75 |
(* add a type copy *)
|
haftmann@20426
|
76 |
|
haftmann@20426
|
77 |
local
|
haftmann@20426
|
78 |
|
haftmann@20483
|
79 |
fun gen_add_typecopy prep_typ (raw_tyco, raw_vs) raw_ty constr_proj thy =
|
haftmann@20426
|
80 |
let
|
haftmann@20426
|
81 |
val ty = prep_typ thy raw_ty;
|
haftmann@20426
|
82 |
val vs = AList.make (the_default HOLogic.typeS o AList.lookup (op =) (typ_tfrees ty)) raw_vs;
|
haftmann@20426
|
83 |
val tac = Tactic.rtac UNIV_witness 1;
|
haftmann@20483
|
84 |
fun add_info tyco ( { abs_type = ty_abs, rep_type = ty_rep, Abs_name = c_abs,
|
haftmann@20426
|
85 |
Rep_name = c_rep, Abs_inject = inject,
|
wenzelm@21708
|
86 |
Abs_inverse = inverse, ... } : TypedefPackage.info ) thy =
|
haftmann@20426
|
87 |
let
|
haftmann@20426
|
88 |
val exists_thm =
|
haftmann@20426
|
89 |
UNIV_I
|
haftmann@20426
|
90 |
|> Drule.instantiate' [SOME (ctyp_of thy (Logic.varifyT ty_rep))] [];
|
haftmann@20426
|
91 |
val inject' = inject OF [exists_thm, exists_thm];
|
haftmann@20426
|
92 |
val proj_def = inverse OF [exists_thm];
|
haftmann@20426
|
93 |
val info = {
|
haftmann@20426
|
94 |
vs = vs,
|
haftmann@20426
|
95 |
constr = c_abs,
|
haftmann@20426
|
96 |
typ = ty_rep,
|
haftmann@20426
|
97 |
inject = inject',
|
haftmann@20426
|
98 |
proj = (c_rep, ty_abs --> ty_rep),
|
haftmann@20426
|
99 |
proj_def = proj_def
|
haftmann@20426
|
100 |
};
|
haftmann@20426
|
101 |
in
|
haftmann@20426
|
102 |
thy
|
haftmann@24626
|
103 |
|> (TypecopyData.map o Symtab.update_new) (tyco, info)
|
wenzelm@24711
|
104 |
|> TypecopyInterpretation.data tyco
|
haftmann@20483
|
105 |
|> pair (tyco, info)
|
haftmann@20426
|
106 |
end
|
haftmann@20426
|
107 |
in
|
haftmann@20426
|
108 |
thy
|
wenzelm@26475
|
109 |
|> TypedefPackage.add_typedef_i false (SOME raw_tyco) (raw_tyco, map fst vs, NoSyn)
|
wenzelm@26475
|
110 |
(HOLogic.mk_UNIV ty) (Option.map swap constr_proj) tac
|
haftmann@20483
|
111 |
|-> (fn (tyco, info) => add_info tyco info)
|
haftmann@20426
|
112 |
end;
|
haftmann@20426
|
113 |
|
haftmann@20426
|
114 |
in
|
haftmann@20426
|
115 |
|
haftmann@20426
|
116 |
val add_typecopy = gen_add_typecopy Sign.certify_typ;
|
haftmann@20426
|
117 |
|
wenzelm@22846
|
118 |
end;
|
haftmann@20426
|
119 |
|
haftmann@20426
|
120 |
|
haftmann@25489
|
121 |
(* code generator setup *)
|
haftmann@20835
|
122 |
|
haftmann@25489
|
123 |
fun add_typecopy_spec tyco thy =
|
haftmann@25489
|
124 |
let
|
haftmann@25534
|
125 |
val SOME { constr, proj_def, inject, vs, ... } = get_info thy tyco;
|
haftmann@25864
|
126 |
val vs' = (map o apsnd) (curry (Sorts.inter_sort (Sign.classes_of thy)) [HOLogic.class_eq]) vs;
|
haftmann@25506
|
127 |
val ty = Logic.unvarifyT (Sign.the_const_type thy constr);
|
haftmann@26513
|
128 |
fun add_def tyco lthy =
|
haftmann@26513
|
129 |
let
|
haftmann@26513
|
130 |
val ty = Type (tyco, map TFree vs');
|
haftmann@26513
|
131 |
fun mk_side const_name = Const (const_name, ty --> ty --> HOLogic.boolT)
|
haftmann@26513
|
132 |
$ Free ("x", ty) $ Free ("y", ty);
|
haftmann@26513
|
133 |
val def = HOLogic.mk_Trueprop (HOLogic.mk_eq
|
haftmann@26732
|
134 |
(mk_side @{const_name eq_class.eq}, mk_side @{const_name "op ="}));
|
haftmann@26513
|
135 |
val def' = Syntax.check_term lthy def;
|
haftmann@26513
|
136 |
val ((_, (_, thm)), lthy') = Specification.definition
|
haftmann@26513
|
137 |
(NONE, (("", []), def')) lthy;
|
haftmann@26513
|
138 |
val ctxt_thy = ProofContext.init (ProofContext.theory_of lthy);
|
haftmann@26513
|
139 |
val thm' = singleton (ProofContext.export lthy' ctxt_thy) thm;
|
haftmann@26513
|
140 |
in (thm', lthy') end;
|
haftmann@26513
|
141 |
fun tac thms = Class.intro_classes_tac []
|
haftmann@26513
|
142 |
THEN ALLGOALS (ProofContext.fact_tac thms);
|
haftmann@26513
|
143 |
fun add_eq_thm thy =
|
haftmann@26513
|
144 |
let
|
haftmann@26513
|
145 |
val eq = inject
|
haftmann@28054
|
146 |
|> Code_Unit.constrain_thm [HOLogic.class_eq]
|
haftmann@26513
|
147 |
|> Simpdata.mk_eq
|
haftmann@26513
|
148 |
|> MetaSimplifier.rewrite_rule [Thm.transfer thy @{thm equals_eq}];
|
haftmann@26513
|
149 |
in Code.add_func eq thy end;
|
haftmann@25489
|
150 |
in
|
haftmann@25506
|
151 |
thy
|
haftmann@25506
|
152 |
|> Code.add_datatype [(constr, ty)]
|
haftmann@25506
|
153 |
|> Code.add_func proj_def
|
haftmann@25864
|
154 |
|> TheoryTarget.instantiation ([tyco], vs', [HOLogic.class_eq])
|
haftmann@26513
|
155 |
|> add_def tyco
|
haftmann@26513
|
156 |
|-> (fn thm => Class.prove_instantiation_instance (K (tac [thm]))
|
haftmann@26513
|
157 |
#> LocalTheory.exit
|
haftmann@26513
|
158 |
#> ProofContext.theory_of
|
haftmann@26513
|
159 |
#> Code.del_func thm
|
haftmann@26513
|
160 |
#> add_eq_thm)
|
haftmann@25489
|
161 |
end;
|
haftmann@20426
|
162 |
|
haftmann@25489
|
163 |
val setup =
|
haftmann@25489
|
164 |
TypecopyInterpretation.init
|
haftmann@25489
|
165 |
#> interpretation add_typecopy_spec
|
haftmann@20426
|
166 |
|
wenzelm@22846
|
167 |
end;
|