|
1 (* Title: HOL/Tools/typecopy_package.ML |
|
2 ID: $Id$ |
|
3 Author: Florian Haftmann, TU Muenchen |
|
4 |
|
5 Introducing copies of types using trivial typedefs. |
|
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 |
|
19 -> theory -> info * theory |
|
20 val get_typecopies: theory -> string list |
|
21 val get_typecopy_info: theory -> string -> info option |
|
22 type hook = string * info -> theory -> theory; |
|
23 val add_hook: hook -> theory -> theory; |
|
24 val typecopy_fun_extr: theory -> string * typ -> thm list option |
|
25 val typecopy_type_extr: theory -> string |
|
26 -> (((string * sort) list * (string * typ list) list) * tactic) option |
|
27 val print: theory -> unit |
|
28 val setup: theory -> theory |
|
29 end; |
|
30 |
|
31 structure TypecopyPackage: TYPECOPY_PACKAGE = |
|
32 struct |
|
33 |
|
34 (* theory context reference *) |
|
35 |
|
36 val univ_witness = thm "Set.UNIV_witness" |
|
37 |
|
38 |
|
39 (* theory data *) |
|
40 |
|
41 type info = { |
|
42 vs: (string * sort) list, |
|
43 constr: string, |
|
44 typ: typ, |
|
45 inject: thm, |
|
46 proj: string * typ, |
|
47 proj_def: thm |
|
48 }; |
|
49 |
|
50 type hook = string * info -> theory -> theory; |
|
51 |
|
52 structure TypecopyData = TheoryDataFun |
|
53 (struct |
|
54 val name = "HOL/typecopy_package"; |
|
55 type T = info Symtab.table * (serial * hook) list; |
|
56 val empty = (Symtab.empty, []); |
|
57 val copy = I; |
|
58 val extend = I; |
|
59 fun merge _ ((tab1, hooks1), (tab2, hooks2) : T) = |
|
60 (Symtab.merge (K true) (tab1, tab2), AList.merge (op =) (K true) (hooks1, hooks2)); |
|
61 fun print thy (tab, _) = |
|
62 let |
|
63 fun mk (tyco, { vs, constr, typ, proj = (proj, _), ... } : info) = |
|
64 (Pretty.block o Pretty.breaks) [ |
|
65 Sign.pretty_typ thy (Type (tyco, map TFree vs)), |
|
66 Pretty.str "=", |
|
67 (Pretty.str o Sign.extern_const thy) constr, |
|
68 Sign.pretty_typ thy typ, |
|
69 Pretty.block [Pretty.str "(", (Pretty.str o Sign.extern_const thy) proj, Pretty.str ")"] |
|
70 ]; |
|
71 in |
|
72 (Pretty.writeln o Pretty.block o Pretty.fbreaks) ( |
|
73 Pretty.str "type copies:" |
|
74 :: map mk (Symtab.dest tab) |
|
75 ) |
|
76 end; |
|
77 end); |
|
78 |
|
79 val print = TypecopyData.print; |
|
80 val get_typecopies = Symtab.keys o fst o TypecopyData.get; |
|
81 val get_typecopy_info = Symtab.lookup o fst o TypecopyData.get; |
|
82 |
|
83 |
|
84 (* hook management *) |
|
85 |
|
86 fun add_hook hook = |
|
87 (TypecopyData.map o apsnd o cons) (serial (), hook); |
|
88 |
|
89 fun invoke_hooks tyco_info thy = |
|
90 fold (fn (_, f) => f tyco_info) ((snd o TypecopyData.get) thy) thy; |
|
91 |
|
92 |
|
93 (* add a type copy *) |
|
94 |
|
95 local |
|
96 |
|
97 fun gen_add_typecopy prep_typ (tyco, raw_vs) raw_ty constr_proj thy = |
|
98 let |
|
99 val ty = prep_typ thy raw_ty; |
|
100 val vs = AList.make (the_default HOLogic.typeS o AList.lookup (op =) (typ_tfrees ty)) raw_vs; |
|
101 val tac = Tactic.rtac UNIV_witness 1; |
|
102 fun add_info ( { abs_type = ty_abs, rep_type = ty_rep, Abs_name = c_abs, |
|
103 Rep_name = c_rep, Abs_inject = inject, |
|
104 Abs_inverse = inverse, ... } : TypedefPackage.info ) thy = |
|
105 let |
|
106 val Type (tyco', _) = ty_abs; |
|
107 val exists_thm = |
|
108 UNIV_I |
|
109 |> Drule.instantiate' [SOME (ctyp_of thy (Logic.varifyT ty_rep))] []; |
|
110 val inject' = inject OF [exists_thm, exists_thm]; |
|
111 val proj_def = inverse OF [exists_thm]; |
|
112 val info = { |
|
113 vs = vs, |
|
114 constr = c_abs, |
|
115 typ = ty_rep, |
|
116 inject = inject', |
|
117 proj = (c_rep, ty_abs --> ty_rep), |
|
118 proj_def = proj_def |
|
119 }; |
|
120 in |
|
121 thy |
|
122 |> (TypecopyData.map o apfst o Symtab.update_new) (tyco', info) |
|
123 |> invoke_hooks (tyco', info) |
|
124 |> pair info |
|
125 end |
|
126 in |
|
127 thy |
|
128 |> TypedefPackage.add_typedef_i false (SOME tyco) (tyco, map fst vs, NoSyn) |
|
129 (HOLogic.mk_UNIV ty) (Option.map swap constr_proj) tac |
|
130 |-> (fn info => add_info info) |
|
131 end; |
|
132 |
|
133 in |
|
134 |
|
135 val add_typecopy = gen_add_typecopy Sign.certify_typ; |
|
136 |
|
137 end; (*local*) |
|
138 |
|
139 |
|
140 (* theory setup *) |
|
141 |
|
142 fun typecopy_type_extr thy tyco = |
|
143 case get_typecopy_info thy tyco |
|
144 of SOME { vs, constr, typ, inject, ... } => SOME ((vs, [(constr, [typ])]), |
|
145 (ALLGOALS o match_tac) [eq_reflection] |
|
146 THEN (ALLGOALS o match_tac) [inject]) |
|
147 | NONE => NONE; |
|
148 |
|
149 fun typecopy_fun_extr thy (c, ty) = |
|
150 case (fst o strip_type) ty |
|
151 of Type (tyco, _) :: _ => |
|
152 (case get_typecopy_info thy tyco |
|
153 of SOME { proj_def, proj as (c', _), ... } => |
|
154 if c = c' then SOME [proj_def] else NONE |
|
155 | NONE => NONE) |
|
156 | _ => NONE; |
|
157 |
|
158 val setup = |
|
159 TypecopyData.init |
|
160 #> CodegenTheorems.add_fun_extr (these oo typecopy_fun_extr) |
|
161 #> CodegenTheorems.add_datatype_extr typecopy_type_extr; |
|
162 |
|
163 end; (*struct*) |