164 *) |
165 *) |
165 |
166 |
166 datatype type_sig = |
167 datatype type_sig = |
167 TySg of { |
168 TySg of { |
168 classes: class list, |
169 classes: class list, |
169 classrel: (class * class list) list, |
170 classrel: Sorts.classrel, |
170 default: sort, |
171 default: sort, |
171 tycons: (string * int) list, |
172 tycons: int Symtab.table, |
172 abbrs: (string * (string list * typ)) list, |
173 abbrs: (string list * typ) Symtab.table, |
173 arities: (string * (class * sort list) list) list}; |
174 arities: Sorts.arities}; |
174 |
175 |
175 fun rep_tsig (TySg comps) = comps; |
176 fun rep_tsig (TySg comps) = comps; |
176 |
177 |
177 fun defaultS (TySg {default, ...}) = default; |
178 fun defaultS (TySg {default, ...}) = default; |
178 |
179 |
179 fun logical_types (TySg {classrel, arities, tycons, ...}) = |
180 fun logical_types (TySg {classrel, arities, tycons, ...}) = |
180 let |
181 let |
181 fun log_class c = Sorts.class_le classrel (c, logicC); |
182 fun log_class c = Sorts.class_le classrel (c, logicC); |
182 fun log_type t = exists (log_class o #1) (assocs arities t); |
183 fun log_type t = exists (log_class o #1) (Symtab.lookup_multi (arities, t)); |
183 in |
184 in filter log_type (Symtab.keys tycons) end; |
184 filter log_type (map #1 tycons) |
|
185 end; |
|
186 |
185 |
187 |
186 |
188 (* sorts *) |
187 (* sorts *) |
189 |
188 |
190 (* FIXME declared!? *) |
189 (* FIXME declared!? *) |
264 (*Instantiation of type variables in terms *) |
263 (*Instantiation of type variables in terms *) |
265 fun inst_term_tvars (_,[]) t = t |
264 fun inst_term_tvars (_,[]) t = t |
266 | inst_term_tvars arg t = map_term_types (inst_typ_tvars arg) t; |
265 | inst_term_tvars arg t = map_term_types (inst_typ_tvars arg) t; |
267 |
266 |
268 |
267 |
269 (* norm_typ *) |
268 (* norm_typ, norm_term *) |
270 |
269 |
271 (*expand abbreviations and normalize sorts*) |
270 (*expand abbreviations and normalize sorts*) |
272 fun norm_typ (tsig as TySg {abbrs, ...}) ty = |
271 fun norm_typ (tsig as TySg {abbrs, ...}) ty = |
273 let |
272 let |
274 val idx = maxidx_of_typ ty + 1; |
273 val idx = maxidx_of_typ ty + 1; |
275 |
274 |
276 fun norm (Type (a, Ts)) = |
275 fun norm (Type (a, Ts)) = |
277 (case assoc (abbrs, a) of |
276 (case Symtab.lookup (abbrs, a) of |
278 Some (vs, U) => norm (inst_typ (map (rpair idx) vs ~~ Ts) (incr_tvar idx U)) |
277 Some (vs, U) => norm (inst_typ (map (rpair idx) vs ~~ Ts) (incr_tvar idx U)) |
279 | None => Type (a, map norm Ts)) |
278 | None => Type (a, map norm Ts)) |
280 | norm (TFree (x, S)) = TFree (x, norm_sort tsig S) |
279 | norm (TFree (x, S)) = TFree (x, norm_sort tsig S) |
281 | norm (TVar (xi, S)) = TVar (xi, norm_sort tsig S); |
280 | norm (TVar (xi, S)) = TVar (xi, norm_sort tsig S); |
282 in |
281 |
283 norm ty |
282 val ty' = norm ty; |
284 end; |
283 in if ty = ty' then ty else ty' end; (*dumb tuning to avoid copying*) |
285 |
284 |
286 |
285 fun norm_term tsig t = |
|
286 let val t' = map_term_types (norm_typ tsig) t |
|
287 in if t = t' then t else t' end; (*dumb tuning to avoid copying*) |
287 |
288 |
288 |
289 |
289 |
290 |
290 (** build type signatures **) |
291 (** build type signatures **) |
291 |
292 |
292 fun make_tsig (classes, classrel, default, tycons, abbrs, arities) = |
293 fun make_tsig (classes, classrel, default, tycons, abbrs, arities) = |
293 TySg {classes = classes, classrel = classrel, default = default, |
294 TySg {classes = classes, classrel = classrel, default = default, |
294 tycons = tycons, abbrs = abbrs, arities = arities}; |
295 tycons = tycons, abbrs = abbrs, arities = arities}; |
295 |
296 |
296 val tsig0 = make_tsig ([], [], [], [], [], []); |
297 val tsig0 = make_tsig ([], Symtab.empty, [], Symtab.empty, Symtab.empty, Symtab.empty); |
297 |
298 |
298 |
299 |
299 |
300 |
300 |
301 |
301 (* typ_errors *) |
302 (* typ_errors *) |
302 |
303 |
303 (*check validity of (not necessarily normal) type; accumulate error messages*) |
304 (*check validity of (not necessarily normal) type; accumulate error messages*) |
304 |
305 |
305 fun typ_errors tsig (typ, errors) = |
306 fun typ_errors tsig (typ, errors) = |
306 let |
307 let |
307 val TySg {classes, tycons, abbrs, ...} = tsig; |
308 val {classes, tycons, abbrs, ...} = rep_tsig tsig; |
308 |
309 |
309 fun class_err (errs, c) = |
310 fun class_err (errs, c) = |
310 if c mem_string classes then errs |
311 if c mem_string classes then errs |
311 else undcl_class c ins_string errs; |
312 else undcl_class c ins_string errs; |
312 |
313 |
313 val sort_err = foldl class_err; |
314 val sort_err = foldl class_err; |
314 |
315 |
315 fun typ_errs (Type (c, Us), errs) = |
316 fun typ_errs (errs, Type (c, Us)) = |
316 let |
317 let |
317 val errs' = foldr typ_errs (Us, errs); |
318 val errs' = foldl typ_errs (errs, Us); |
318 fun nargs n = |
319 fun nargs n = |
319 if n = length Us then errs' |
320 if n = length Us then errs' |
320 else ("Wrong number of arguments: " ^ quote c) ins_string errs'; |
321 else ("Wrong number of arguments: " ^ quote c) ins_string errs'; |
321 in |
322 in |
322 (case assoc (tycons, c) of |
323 (case Symtab.lookup (tycons, c) of |
323 Some n => nargs n |
324 Some n => nargs n |
324 | None => |
325 | None => |
325 (case assoc (abbrs, c) of |
326 (case Symtab.lookup (abbrs, c) of |
326 Some (vs, _) => nargs (length vs) |
327 Some (vs, _) => nargs (length vs) |
327 | None => undcl_type c ins_string errs)) |
328 | None => undcl_type c ins_string errs)) |
328 end |
329 end |
329 | typ_errs (TFree (_, S), errs) = sort_err (errs, S) |
330 | typ_errs (errs, TFree (_, S)) = sort_err (errs, S) |
330 | typ_errs (TVar ((x, i), S), errs) = |
331 | typ_errs (errs, TVar ((x, i), S)) = |
331 if i < 0 then |
332 if i < 0 then |
332 ("Negative index for TVar " ^ quote x) ins_string sort_err (errs, S) |
333 ("Negative index for TVar " ^ quote x) ins_string sort_err (errs, S) |
333 else sort_err (errs, S); |
334 else sort_err (errs, S); |
334 in |
335 in typ_errs (errors, typ) end; |
335 typ_errs (typ, errors) |
|
336 end; |
|
337 |
336 |
338 |
337 |
339 (* cert_typ *) |
338 (* cert_typ *) |
340 |
339 |
341 (*check and normalize typ wrt. tsig*) (*exception TYPE*) |
340 (*check and normalize typ wrt. tsig*) (*exception TYPE*) |
408 |
408 |
409 (* 'merge_arities' builds the union of two 'arities' lists; |
409 (* 'merge_arities' builds the union of two 'arities' lists; |
410 it only checks the two restriction conditions and inserts afterwards |
410 it only checks the two restriction conditions and inserts afterwards |
411 all elements of the second list into the first one *) |
411 all elements of the second list into the first one *) |
412 |
412 |
413 fun merge_arities classrel = |
413 fun merge_arities_aux classrel = |
414 let fun test_ar t (ars1, sw) = add_arity classrel ars1 (t,sw); |
414 let fun test_ar t (ars1, sw) = add_arity classrel ars1 (t,sw); |
415 |
415 |
416 fun merge_c (arities1, (c as (t, ars2))) = case assoc (arities1, t) of |
416 fun merge_c (arities1, (c as (t, ars2))) = case assoc (arities1, t) of |
417 Some(ars1) => |
417 Some(ars1) => |
418 let val ars = foldl (test_ar t) (ars1, ars2) |
418 let val ars = foldl (test_ar t) (ars1, ars2) |
419 in overwrite (arities1, (t,ars)) end |
419 in overwrite (arities1, (t,ars)) end |
420 | None => c::arities1 |
420 | None => c::arities1 |
421 in foldl merge_c end; |
421 in foldl merge_c end; |
422 |
422 |
|
423 fun merge_arities classrel (a1, a2) = |
|
424 Symtab.make (merge_arities_aux classrel (Symtab.dest a1, Symtab.dest a2)); |
|
425 |
423 fun add_tycons (tycons, tn as (t,n)) = |
426 fun add_tycons (tycons, tn as (t,n)) = |
424 (case assoc (tycons, t) of |
427 (case Symtab.lookup (tycons, t) of |
425 Some m => if m = n then tycons else varying_decls t |
428 Some m => if m = n then tycons else varying_decls t |
426 | None => tn :: tycons); |
429 | None => Symtab.update (tn, tycons)); |
427 |
430 |
428 fun merge_abbrs (abbrs1, abbrs2) = |
431 fun merge_abbrs abbrs = |
429 let val abbrs = abbrs1 union abbrs2 in |
432 Symtab.merge (op =) abbrs handle Symtab.DUPS dups => raise TERM (dup_tyabbrs dups, []); |
430 (case gen_duplicates eq_fst abbrs of |
|
431 [] => abbrs |
|
432 | dups => raise TERM (dup_tyabbrs (map fst dups), [])) |
|
433 end; |
|
434 |
433 |
435 |
434 |
436 (* 'merge_tsigs' takes the above declared functions to merge two type |
435 (* 'merge_tsigs' takes the above declared functions to merge two type |
437 signatures *) |
436 signatures *) |
438 |
437 |
440 tycons=tycons1, arities=arities1, abbrs=abbrs1}, |
439 tycons=tycons1, arities=arities1, abbrs=abbrs1}, |
441 TySg{classes=classes2, default=default2, classrel=classrel2, |
440 TySg{classes=classes2, default=default2, classrel=classrel2, |
442 tycons=tycons2, arities=arities2, abbrs=abbrs2}) = |
441 tycons=tycons2, arities=arities2, abbrs=abbrs2}) = |
443 let val classes' = classes1 union_string classes2; |
442 let val classes' = classes1 union_string classes2; |
444 val classrel' = merge_classrel (classrel1, classrel2); |
443 val classrel' = merge_classrel (classrel1, classrel2); |
445 val tycons' = foldl add_tycons (tycons1, tycons2) |
444 val tycons' = foldl add_tycons (tycons1, Symtab.dest tycons2) |
446 val arities' = merge_arities classrel' (arities1, arities2); |
445 val arities' = merge_arities classrel' (arities1, arities2); |
447 val default' = Sorts.norm_sort classrel' (default1 @ default2); |
446 val default' = Sorts.norm_sort classrel' (default1 @ default2); |
448 val abbrs' = merge_abbrs(abbrs1, abbrs2); |
447 val abbrs' = merge_abbrs(abbrs1, abbrs2); |
449 in make_tsig(classes', classrel', default', tycons', abbrs', arities') end; |
448 in make_tsig(classes', classrel', default', tycons', abbrs', arities') end; |
450 |
449 |
469 |
468 |
470 fun add_classrel classes (classrel, (s, ges)) = |
469 fun add_classrel classes (classrel, (s, ges)) = |
471 let |
470 let |
472 fun upd (classrel, s') = |
471 fun upd (classrel, s') = |
473 if s' mem_string classes then |
472 if s' mem_string classes then |
474 let val ges' = the (assoc (classrel, s)) |
473 let val ges' = the (Symtab.lookup (classrel, s)) |
475 in case assoc (classrel, s') of |
474 in case Symtab.lookup (classrel, s') of |
476 Some sups => if s mem_string sups |
475 Some sups => if s mem_string sups |
477 then error(" Cycle :" ^ s^" <= "^ s'^" <= "^ s ) |
476 then error(" Cycle :" ^ s^" <= "^ s'^" <= "^ s ) |
478 else overwrite |
477 else Symtab.update ((s, sups union_string ges'), classrel) |
479 (classrel, (s, sups union_string ges')) |
|
480 | None => classrel |
478 | None => classrel |
481 end |
479 end |
482 else err_undcl_class s' |
480 else err_undcl_class s' |
483 in foldl upd (classrel @ [(s, ges)], ges) end; |
481 in foldl upd (Symtab.update ((s, ges), classrel), ges) end; |
484 |
482 |
485 |
483 |
486 (* 'extend_classes' inserts all new classes into the corresponding |
484 (* 'extend_classes' inserts all new classes into the corresponding |
487 lists ('classes', 'classrel') if possible *) |
485 lists ('classes', 'classrel') if possible *) |
488 |
486 |
529 |
527 |
530 fun ext_tsig_types (TySg {classes, classrel, default, tycons, abbrs, arities}) ts = |
528 fun ext_tsig_types (TySg {classes, classrel, default, tycons, abbrs, arities}) ts = |
531 let |
529 let |
532 fun check_type (c, n) = |
530 fun check_type (c, n) = |
533 if n < 0 then err_neg_args c |
531 if n < 0 then err_neg_args c |
534 else if is_some (assoc (tycons, c)) then err_dup_tycon c |
532 else if is_some (Symtab.lookup (tycons, c)) then err_dup_tycon c |
535 else if is_some (assoc (abbrs, c)) then error (ty_confl c) |
533 else if is_some (Symtab.lookup (abbrs, c)) then error (ty_confl c) |
536 else (); |
534 else (); |
537 in |
535 in |
538 seq check_type ts; |
536 seq check_type ts; |
539 make_tsig (classes, classrel, default, ts @ tycons, abbrs, |
537 make_tsig (classes, classrel, default, Symtab.extend (tycons, ts), abbrs, |
540 map (rpair [] o #1) ts @ arities) |
538 Symtab.extend (arities, map (rpair [] o #1) ts)) |
541 end; |
539 end; |
542 |
540 |
543 |
541 |
544 |
542 |
545 (** add type abbreviations **) |
543 (** add type abbreviations **) |
605 the 'arities' association list of the given type signatrure *) |
601 the 'arities' association list of the given type signatrure *) |
606 |
602 |
607 fun coregular (classes, classrel, tycons) = |
603 fun coregular (classes, classrel, tycons) = |
608 let fun ex C = if C mem_string classes then () else err_undcl_class(C); |
604 let fun ex C = if C mem_string classes then () else err_undcl_class(C); |
609 |
605 |
610 fun addar(arities, (t, (w, C))) = case assoc(tycons, t) of |
606 fun addar(arities, (t, (w, C))) = case Symtab.lookup (tycons, t) of |
611 Some(n) => if n <> length w then varying_decls(t) else |
607 Some(n) => if n <> length w then varying_decls(t) else |
612 ((seq o seq) ex w; ex C; |
608 ((seq o seq) ex w; ex C; |
613 let val ars = the (assoc(arities, t)) |
609 let val ars = the (Symtab.lookup (arities, t)) |
614 val ars' = add_arity classrel ars (t,(C,w)) |
610 val ars' = add_arity classrel ars (t,(C,w)) |
615 in overwrite(arities, (t,ars')) end) |
611 in Symtab.update ((t,ars'), arities) end) |
616 | None => error (undcl_type t); |
612 | None => error (undcl_type t); |
617 |
613 |
618 in addar end; |
614 in addar end; |
619 |
615 |
620 |
616 |