4454
|
1 |
(* Title: HOL/record.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Wolfgang Naraschewski and Markus Wenzel, TU Muenchen
|
|
4 |
|
|
5 |
Internal interface for records.
|
|
6 |
*)
|
|
7 |
|
|
8 |
|
|
9 |
signature RECORD =
|
|
10 |
sig
|
|
11 |
val add_record:
|
|
12 |
(string list * bstring) -> string option ->
|
|
13 |
(bstring * string) list -> theory -> theory
|
|
14 |
val add_record_i:
|
|
15 |
(string list * bstring) -> (typ list * string) option ->
|
|
16 |
(bstring * typ) list -> theory -> theory
|
|
17 |
end;
|
|
18 |
|
|
19 |
structure Record: RECORD =
|
|
20 |
struct
|
|
21 |
|
|
22 |
val base = Sign.base_name;
|
|
23 |
|
|
24 |
(* FIXME move to Pure/library.ML *)
|
|
25 |
fun set_minus xs [] = xs
|
|
26 |
| set_minus xs (y::ys) = set_minus (xs \ y) ys;
|
|
27 |
(* FIXME *)
|
|
28 |
|
|
29 |
(* FIXME move to Pure/sign.ML *)
|
|
30 |
fun of_sort sg =
|
|
31 |
Sorts.of_sort
|
|
32 |
(#classrel (Type.rep_tsig (#tsig (Sign.rep_sg sg))))
|
|
33 |
(#arities (Type.rep_tsig (#tsig (Sign.rep_sg sg))));
|
|
34 |
(* FIXME *)
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
(** record info **)
|
|
39 |
|
|
40 |
fun get_record thy name =
|
|
41 |
let val table = ThyData.get_records thy
|
|
42 |
in
|
|
43 |
Symtab.lookup (table, name)
|
|
44 |
end;
|
|
45 |
|
|
46 |
fun put_record thy name info =
|
|
47 |
let val table = ThyData.get_records thy
|
|
48 |
in
|
|
49 |
ThyData.put_records (Symtab.update ((name, info), table))
|
|
50 |
end;
|
|
51 |
|
|
52 |
local
|
|
53 |
fun record_infos thy None = []
|
|
54 |
| record_infos thy (Some info) =
|
|
55 |
case #parent info of
|
|
56 |
None => [info]
|
|
57 |
| Some (_, parent) => record_infos thy (get_record thy parent) @ [info];
|
|
58 |
fun record_parass thy info =
|
|
59 |
(map (map (fn (str, _) => (str, 0)) o #args) (record_infos thy (Some info)))
|
|
60 |
: indexname list list;
|
|
61 |
fun record_argss thy info =
|
|
62 |
map (fst o the) (tl (map #parent (record_infos thy (Some info)))) @
|
|
63 |
[map TFree (#args info)];
|
|
64 |
in
|
|
65 |
fun record_field_names thy info =
|
|
66 |
flat (map (map fst o #fields) (record_infos thy (Some info)));
|
|
67 |
fun record_field_types thy info =
|
|
68 |
let
|
|
69 |
val paras_args = map (op ~~) (record_parass thy info ~~ record_argss thy info);
|
|
70 |
val raw_substs = map typ_subst_TVars paras_args;
|
|
71 |
fun make_substs [] = []
|
|
72 |
| make_substs (x::xs) = (foldr1 (op o) (x::xs)) :: make_substs xs;
|
|
73 |
val free_TFree = map (map (map_type_tfree (fn (str, s) => TVar((str, 0), s))));
|
|
74 |
val raw_record_field_types = map (map snd o #fields) (record_infos thy (Some info))
|
|
75 |
in
|
|
76 |
flat (ListPair.map (fn (s, ts) => map s ts)
|
|
77 |
(make_substs raw_substs, free_TFree raw_record_field_types))
|
|
78 |
end;
|
|
79 |
end;
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
(** abstract syntax **)
|
|
84 |
|
|
85 |
(* tuples *)
|
|
86 |
|
|
87 |
val unitT = Type ("unit",[]);
|
|
88 |
val unit = Const ("()",unitT);
|
|
89 |
fun mk_prodT (T1, T2) = Type ("*", [T1,T2]);
|
|
90 |
fun mk_Pair (t1, t2) =
|
|
91 |
let val T1 = fastype_of t1
|
|
92 |
and T2 = fastype_of t2
|
|
93 |
in Const ("Pair", [T1, T2] ---> mk_prodT (T1,T2)) $ t1 $ t2 end;
|
|
94 |
|
|
95 |
fun dest_prodT (Type ("*", [T1, T2])) = (T1, T2)
|
|
96 |
| dest_prodT T = raise TYPE ("dest_prodT: product type expected", [T], []);
|
|
97 |
|
|
98 |
fun mk_fst pT = Const ("fst", pT --> fst (dest_prodT pT));
|
|
99 |
fun mk_snd pT = Const ("snd", pT --> snd (dest_prodT pT));
|
|
100 |
|
|
101 |
fun ap_fst t = mk_fst (fastype_of t) $ t;
|
|
102 |
fun ap_snd t = mk_snd (fastype_of t) $ t;
|
|
103 |
|
|
104 |
|
|
105 |
(* records *)
|
|
106 |
|
|
107 |
fun selT T recT = recT --> T;
|
|
108 |
fun updateT T recT = T --> recT --> recT;
|
|
109 |
val base_free = Free o apfst base;
|
|
110 |
val make_scheme_name = "make_scheme";
|
|
111 |
val make_name = "make";
|
|
112 |
val update_suffix = "_update";
|
|
113 |
fun make_schemeC full make_schemeT = Const (full make_scheme_name, make_schemeT);
|
|
114 |
fun makeC full makeT = Const (full make_name, makeT);
|
|
115 |
fun selC s T recT = Const (s, selT T recT);
|
|
116 |
fun updateC full s T recT = Const (full (base s ^ update_suffix), updateT T recT);
|
|
117 |
fun frees xs = foldr add_typ_tfree_names (xs, []);
|
|
118 |
|
|
119 |
|
|
120 |
|
|
121 |
(** constants, definitions, axioms **)
|
|
122 |
|
|
123 |
(* constant declarations for make, selectors and update *)
|
|
124 |
|
|
125 |
fun decls make_schemeT makeT selT updateT recT current_fields =
|
|
126 |
let val make_scheme_decl = (make_scheme_name, make_schemeT, NoSyn);
|
|
127 |
val make_decl = (make_name, makeT, NoSyn);
|
|
128 |
val sel_decls = map (fn (c, T) => (c, selT T recT, NoSyn)) current_fields;
|
|
129 |
val update_decls =
|
|
130 |
map (fn (c, T) => (c ^ update_suffix, updateT T recT, NoSyn)) current_fields
|
|
131 |
in
|
|
132 |
make_scheme_decl :: make_decl :: sel_decls @ update_decls
|
|
133 |
end;
|
|
134 |
|
|
135 |
|
|
136 |
(* definitions for make, selectors and update *)
|
|
137 |
|
|
138 |
(*make*)
|
|
139 |
fun make_defs make_schemeT makeT base_frees z thy =
|
|
140 |
let
|
|
141 |
val sign = sign_of thy;
|
|
142 |
val full = Sign.full_name sign;
|
|
143 |
val make_args_scheme = list_comb (make_schemeC full make_schemeT, base_frees) $ z;
|
|
144 |
val make_args = list_comb (makeC full makeT, base_frees);
|
|
145 |
val make_scheme_def =
|
|
146 |
mk_defpair (make_args_scheme, foldr mk_Pair (base_frees, z));
|
|
147 |
val make_def =
|
|
148 |
mk_defpair (make_args, foldr mk_Pair (base_frees, unit))
|
|
149 |
in
|
|
150 |
make_scheme_def :: [make_def]
|
|
151 |
end;
|
|
152 |
|
|
153 |
(*selectors*)
|
|
154 |
fun sel_defs recT r all_fields current_fullfields =
|
|
155 |
let
|
|
156 |
val prefix_len = length all_fields - length current_fullfields;
|
|
157 |
val sel_terms =
|
|
158 |
map (fn k => ap_fst o funpow k ap_snd)
|
|
159 |
(prefix_len upto length all_fields - 1)
|
|
160 |
in
|
|
161 |
ListPair.map
|
|
162 |
(fn ((s, T), t) => mk_defpair (selC s T recT $ r, t r))
|
|
163 |
(current_fullfields, sel_terms)
|
|
164 |
end;
|
|
165 |
|
|
166 |
(*update*)
|
|
167 |
fun update_defs recT r all_fields current_fullfields thy =
|
|
168 |
let
|
|
169 |
val sign = sign_of thy;
|
|
170 |
val full = Sign.full_name sign;
|
|
171 |
val len_all_fields = length all_fields;
|
|
172 |
fun sel_last r = funpow len_all_fields ap_snd r;
|
|
173 |
fun update_def_s (s, T) =
|
|
174 |
let val updates = map (fn (s', T') =>
|
|
175 |
if s = s' then base_free (s, T) else selC s' T' recT $ r)
|
|
176 |
all_fields
|
|
177 |
in
|
|
178 |
mk_defpair (updateC full s T recT $ base_free (s, T) $ r,
|
|
179 |
foldr mk_Pair (updates, sel_last r))
|
|
180 |
end;
|
|
181 |
in
|
|
182 |
map update_def_s current_fullfields
|
|
183 |
end
|
|
184 |
|
|
185 |
|
|
186 |
|
|
187 |
(** errors **)
|
|
188 |
|
|
189 |
fun check_duplicate_fields all_field_names =
|
|
190 |
let val has_dupl = findrep all_field_names
|
|
191 |
in
|
|
192 |
if null has_dupl then []
|
|
193 |
else ["Duplicate field declaration: " ^ quote (hd has_dupl) ^
|
|
194 |
" (Double might be in ancestor)"]
|
|
195 |
end;
|
|
196 |
|
|
197 |
fun check_parent None name thy = []
|
|
198 |
| check_parent (Some (args, parent)) name thy =
|
|
199 |
let
|
|
200 |
val opt_info = get_record thy parent;
|
|
201 |
val sign = sign_of thy;
|
|
202 |
fun check_sorts [] [] = []
|
|
203 |
| check_sorts ((str, sort)::xs) (y::ys) =
|
|
204 |
if of_sort sign (y, sort)
|
|
205 |
then check_sorts xs ys
|
|
206 |
else ["Sort of " ^
|
|
207 |
quote (Pretty.string_of (Sign.pretty_typ sign y)) ^
|
|
208 |
" does not match parent declaration"]
|
|
209 |
in
|
|
210 |
case opt_info of
|
|
211 |
None => ["Parent " ^ quote parent ^" not defined"]
|
|
212 |
| Some {args = pargs, parent = pparent,
|
|
213 |
fields = pfields, sign_ref = psign_ref} =>
|
|
214 |
if (length args = length pargs)
|
|
215 |
then check_sorts pargs args
|
|
216 |
else ["Mismatching arities for parent " ^ quote (base parent)]
|
|
217 |
end;
|
|
218 |
|
|
219 |
fun check_duplicate_records thy full_name =
|
|
220 |
if is_none (get_record thy full_name) then []
|
|
221 |
else ["Duplicate record declaration"];
|
|
222 |
|
|
223 |
fun check_duplicate_args raw_args =
|
|
224 |
let val has_dupl = findrep raw_args
|
|
225 |
in
|
|
226 |
if null has_dupl then []
|
|
227 |
else ["Duplicate parameter: " ^ quote (hd has_dupl)]
|
|
228 |
end;
|
|
229 |
|
|
230 |
fun check_raw_args raw_args free_vars thy =
|
|
231 |
let
|
|
232 |
val free_vars_names = map fst free_vars;
|
|
233 |
val diff_set = set_minus free_vars_names raw_args;
|
|
234 |
val default_sort = Type.defaultS ((#tsig o Sign.rep_sg) (sign_of thy));
|
|
235 |
val assign_sorts =
|
|
236 |
map (fn x => case assoc (free_vars, x) of
|
|
237 |
None => (x, default_sort)
|
|
238 |
| Some sort => (x, sort)) raw_args
|
|
239 |
in
|
|
240 |
if free_vars_names subset raw_args
|
|
241 |
then ([], assign_sorts)
|
|
242 |
else (["Free type variable(s): " ^
|
|
243 |
(foldr1 (fn (s, s') => s ^ ", " ^ s') (map quote diff_set))],
|
|
244 |
[])
|
|
245 |
end;
|
|
246 |
|
|
247 |
|
|
248 |
|
|
249 |
(** ext_record **)
|
|
250 |
|
|
251 |
fun ext_record (args, name) opt_parent current_fields thy =
|
|
252 |
let
|
|
253 |
val full_name = Sign.full_name (sign_of thy) name;
|
|
254 |
val thy = thy |> Theory.add_path name;
|
|
255 |
val sign = sign_of thy;
|
|
256 |
val full = Sign.full_name sign;
|
|
257 |
|
|
258 |
val current_fullfields = map (apfst full) current_fields;
|
|
259 |
val info = {args = args, fields = current_fullfields,
|
|
260 |
parent = opt_parent, sign_ref = Sign.self_ref sign};
|
|
261 |
val thy = thy |> put_record thy full_name info;
|
|
262 |
val all_types = record_field_types thy info;
|
|
263 |
val all_fields = record_field_names thy info ~~ all_types;
|
|
264 |
val base_frees = map base_free all_fields;
|
|
265 |
|
|
266 |
val tfrees = frees all_types;
|
|
267 |
val zeta = variant tfrees "'z";
|
|
268 |
val zetaT = TFree (zeta, HOLogic.termS);
|
|
269 |
val z = base_free ("z", zetaT);
|
|
270 |
val recT = foldr mk_prodT (all_types, zetaT);
|
|
271 |
val recT_unitT = foldr mk_prodT (all_types, unitT);
|
|
272 |
val make_schemeT = (all_types @ [zetaT]) ---> recT;
|
|
273 |
val makeT = all_types ---> recT_unitT;
|
|
274 |
val r = base_free ("r", recT);
|
|
275 |
|
|
276 |
val errors = check_duplicate_fields (map base (record_field_names thy info))
|
|
277 |
in
|
|
278 |
if not (null errors)
|
|
279 |
then error (cat_lines errors) else
|
|
280 |
thy |> Theory.add_path ".."
|
|
281 |
|> Theory.add_tyabbrs_i [(name, tfrees @ [zeta], recT, NoSyn)]
|
|
282 |
|> Theory.add_path name
|
|
283 |
|> Theory.add_consts_i (decls make_schemeT makeT selT updateT recT current_fields)
|
|
284 |
|> Theory.add_defs_i
|
|
285 |
((make_defs make_schemeT makeT base_frees z thy) @
|
|
286 |
(sel_defs recT r all_fields current_fullfields) @
|
|
287 |
(update_defs recT r all_fields current_fullfields thy))
|
|
288 |
|> Theory.add_path ".."
|
|
289 |
end;
|
|
290 |
|
|
291 |
|
|
292 |
|
|
293 |
(** external interfaces **)
|
|
294 |
|
|
295 |
(* add_record *)
|
|
296 |
|
|
297 |
fun add_record_aux prep_typ prep_parent (raw_args, name) raw_parent raw_fields thy =
|
|
298 |
let
|
|
299 |
val _ = require_thy thy "Prod" "record definitions";
|
|
300 |
val sign = sign_of thy;
|
|
301 |
val full_name = Sign.full_name sign name;
|
|
302 |
val make_assocs = map (fn (a, b) => ((a, ~1), b));
|
|
303 |
|
|
304 |
val parent = apsome (prep_parent sign) raw_parent;
|
|
305 |
val parent_args = if_none (apsome fst parent) [];
|
|
306 |
val parent_assoc = make_assocs (foldr (op union) ((map typ_tfrees parent_args), []));
|
|
307 |
|
|
308 |
fun prepare_fields ass [] = []
|
|
309 |
| prepare_fields ass ((name, str)::xs) =
|
|
310 |
let val type_of_name = prep_typ sign ass str
|
|
311 |
in (name, type_of_name)::
|
|
312 |
(prepare_fields (ass union (make_assocs (typ_tfrees type_of_name))) xs)
|
|
313 |
end;
|
|
314 |
val fields = prepare_fields (parent_assoc) raw_fields;
|
|
315 |
val fields_types = map snd fields;
|
|
316 |
val free_vars = foldr (op union) ((map typ_tfrees fields_types), []);
|
|
317 |
|
|
318 |
val check_args = check_raw_args raw_args free_vars thy;
|
|
319 |
val args = snd check_args;
|
|
320 |
|
|
321 |
val errors = (check_parent parent name thy) @
|
|
322 |
(check_duplicate_records thy full_name) @
|
|
323 |
(check_duplicate_args raw_args) @
|
|
324 |
(fst check_args)
|
|
325 |
in
|
|
326 |
if not (null errors)
|
|
327 |
then error (cat_lines errors)
|
|
328 |
else ext_record (args, name) parent fields thy
|
|
329 |
end
|
|
330 |
handle ERROR =>
|
|
331 |
error ("The error(s) above occurred in record declaration " ^ quote name);
|
|
332 |
|
|
333 |
|
|
334 |
(* internalization methods *)
|
|
335 |
|
|
336 |
fun read_parent sign name =
|
|
337 |
(case Sign.read_raw_typ (sign, K None) name of
|
|
338 |
Type (name, Ts) => (Ts, name)
|
|
339 |
| _ => error ("Malformed parent specification: " ^ name));
|
|
340 |
|
|
341 |
fun read_typ sign ass name =
|
|
342 |
Sign.read_typ (sign, curry assoc ass) name handle TYPE (msg, _, _) => error msg;
|
|
343 |
|
|
344 |
fun cert_typ sign ass T =
|
|
345 |
Sign.certify_typ sign T handle TYPE (msg, _, _) => error msg;
|
|
346 |
|
|
347 |
|
|
348 |
(* add_record(_i) *)
|
|
349 |
|
|
350 |
val add_record = add_record_aux read_typ read_parent;
|
|
351 |
val add_record_i = add_record_aux cert_typ (K I);
|
|
352 |
|
|
353 |
|
|
354 |
end;
|