| author | wenzelm |
| Sat, 29 May 2004 15:07:42 +0200 | |
| changeset 14838 | b12855d44c97 |
| parent 14830 | faa4865ba1ce |
| child 14906 | 2da524f3d785 |
| permissions | -rw-r--r-- |
| 256 | 1 |
(* Title: Pure/type.ML |
| 0 | 2 |
ID: $Id$ |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
3 |
Author: Tobias Nipkow, Lawrence C Paulson, and Markus Wenzel |
| 0 | 4 |
|
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
5 |
Type signatures and certified types, special treatment of type vars, |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
6 |
matching and unification of types, extend and merge type signatures. |
| 0 | 7 |
*) |
8 |
||
9 |
signature TYPE = |
|
| 2964 | 10 |
sig |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
11 |
(*type signatures and certified types*) |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
12 |
datatype decl = |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
13 |
LogicalType of int | |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
14 |
Abbreviation of string list * typ | |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
15 |
Nonterminal |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
16 |
type tsig |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
17 |
val rep_tsig: tsig -> |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
18 |
{classes: Sorts.classes,
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
19 |
default: sort, |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
20 |
types: (decl * stamp) Symtab.table, |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
21 |
arities: Sorts.arities, |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
22 |
log_types: string list, |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
23 |
witness: (typ * sort) option} |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
24 |
val empty_tsig: tsig |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
25 |
val classes: tsig -> class list |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
26 |
val defaultS: tsig -> sort |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
27 |
val logical_types: tsig -> string list |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
28 |
val universal_witness: tsig -> (typ * sort) option |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
29 |
val eq_sort: tsig -> sort * sort -> bool |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
30 |
val subsort: tsig -> sort * sort -> bool |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
31 |
val of_sort: tsig -> typ * sort -> bool |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
32 |
val cert_class: tsig -> class -> class |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
33 |
val cert_sort: tsig -> sort -> sort |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
34 |
val witness_sorts: tsig -> sort list -> sort list -> (typ * sort) list |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
35 |
val cert_typ: tsig -> typ -> typ |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
36 |
val cert_typ_syntax: tsig -> typ -> typ |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
37 |
val cert_typ_raw: tsig -> typ -> typ |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
38 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
39 |
(*special treatment of type vars*) |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
40 |
val strip_sorts: typ -> typ |
| 621 | 41 |
val no_tvars: typ -> typ |
42 |
val varifyT: typ -> typ |
|
43 |
val unvarifyT: typ -> typ |
|
| 12501 | 44 |
val varify: term * string list -> term * (string * indexname) list |
| 10495 | 45 |
val freeze_thaw_type : typ -> typ * (typ -> typ) |
|
3411
163f8f4a42d7
Removal of freeze_vars and thaw_vars. New freeze_thaw
paulson
parents:
3175
diff
changeset
|
46 |
val freeze_thaw : term -> term * (term -> term) |
| 2964 | 47 |
|
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
48 |
(*matching and unification*) |
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
49 |
val inst_typ_tvars: Pretty.pp -> tsig -> (indexname * typ) list -> typ -> typ |
|
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
50 |
val inst_term_tvars: Pretty.pp -> tsig -> (indexname * typ) list -> term -> term |
| 2964 | 51 |
exception TYPE_MATCH |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
52 |
val typ_match: tsig -> typ Vartab.table * (typ * typ) -> typ Vartab.table |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
53 |
val typ_instance: tsig -> typ * typ -> bool |
| 2964 | 54 |
exception TUNIFY |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
55 |
val unify: tsig -> typ Vartab.table * int -> typ * typ -> typ Vartab.table * int |
| 450 | 56 |
val raw_unify: typ * typ -> bool |
| 0 | 57 |
|
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
58 |
(*extend and merge type signatures*) |
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
59 |
val add_classes: Pretty.pp -> (class * class list) list -> tsig -> tsig |
|
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
60 |
val add_classrel: Pretty.pp -> (class * class) list -> tsig -> tsig |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
61 |
val set_defsort: sort -> tsig -> tsig |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
62 |
val add_types: (string * int) list -> tsig -> tsig |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
63 |
val add_abbrs: (string * string list * typ) list -> tsig -> tsig |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
64 |
val add_nonterminals: string list -> tsig -> tsig |
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
65 |
val add_arities: Pretty.pp -> arity list -> tsig -> tsig |
|
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
66 |
val merge_tsigs: Pretty.pp -> tsig * tsig -> tsig |
| 2964 | 67 |
end; |
68 |
||
69 |
structure Type: TYPE = |
|
| 0 | 70 |
struct |
71 |
||
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
72 |
(** type signatures and certified types **) |
| 2964 | 73 |
|
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
74 |
(* type declarations *) |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
75 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
76 |
datatype decl = |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
77 |
LogicalType of int | |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
78 |
Abbreviation of string list * typ | |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
79 |
Nonterminal; |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
80 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
81 |
fun str_of_decl (LogicalType _) = "logical type constructor" |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
82 |
| str_of_decl (Abbreviation _) = "type abbreviation" |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
83 |
| str_of_decl Nonterminal = "syntactic type"; |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
84 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
85 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
86 |
(* type tsig *) |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
87 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
88 |
datatype tsig = |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
89 |
TSig of {
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
90 |
classes: Sorts.classes, (*declared classes with proper subclass relation*) |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
91 |
default: sort, (*default sort on input*) |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
92 |
types: (decl * stamp) Symtab.table, (*declared types*) |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
93 |
arities: Sorts.arities, (*image specification of types wrt. sorts*) |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
94 |
log_types: string list, (*logical types sorted by number of arguments*) |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
95 |
witness: (typ * sort) option}; (*witness for non-emptiness of strictest sort*) |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
96 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
97 |
fun rep_tsig (TSig comps) = comps; |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
98 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
99 |
fun make_tsig (classes, default, types, arities, log_types, witness) = |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
100 |
TSig {classes = classes, default = default, types = types, arities = arities,
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
101 |
log_types = log_types, witness = witness}; |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
102 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
103 |
fun map_tsig f (TSig {classes, default, types, arities, log_types, witness}) =
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
104 |
make_tsig (f (classes, default, types, arities, log_types, witness)); |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
105 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
106 |
fun build_tsig (classes, default, types, arities) = |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
107 |
let |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
108 |
fun add_log_type (ts, (c, (LogicalType n, _))) = (c, n) :: ts |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
109 |
| add_log_type (ts, _) = ts; |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
110 |
val log_types = |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
111 |
Symtab.foldl add_log_type ([], types) |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
112 |
|> Library.sort (Library.int_ord o pairself #2) |> map #1; |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
113 |
val witness = |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
114 |
(case Sorts.witness_sorts (classes, arities) log_types [] [Graph.keys classes] of |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
115 |
[w] => Some w | _ => None); |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
116 |
in make_tsig (classes, default, types, arities, log_types, witness) end; |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
117 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
118 |
fun change_tsig f (TSig {classes, default, types, arities, log_types = _, witness = _}) =
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
119 |
build_tsig (f (classes, default, types, arities)); |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
120 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
121 |
val empty_tsig = build_tsig (Graph.empty, [], Symtab.empty, Symtab.empty); |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
122 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
123 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
124 |
(* classes and sorts *) |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
125 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
126 |
fun classes (TSig {classes = C, ...}) = Graph.keys C;
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
127 |
fun defaultS (TSig {default, ...}) = default;
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
128 |
fun logical_types (TSig {log_types, ...}) = log_types;
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
129 |
fun universal_witness (TSig {witness, ...}) = witness;
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
130 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
131 |
fun eq_sort (TSig {classes, ...}) = Sorts.sort_eq classes;
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
132 |
fun subsort (TSig {classes, ...}) = Sorts.sort_le classes;
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
133 |
fun of_sort (TSig {classes, arities, ...}) = Sorts.of_sort (classes, arities);
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
134 |
fun norm_sort (TSig {classes, ...}) = Sorts.norm_sort classes;
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
135 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
136 |
fun cert_class (TSig {classes, ...}) c =
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
137 |
if can (Graph.get_node classes) c then c |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
138 |
else raise TYPE ("Undeclared class: " ^ quote c, [], []);
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
139 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
140 |
fun cert_sort tsig = norm_sort tsig o map (cert_class tsig); |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
141 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
142 |
fun witness_sorts (tsig as TSig {classes, arities, log_types, ...}) =
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
143 |
Sorts.witness_sorts (classes, arities) log_types; |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
144 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
145 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
146 |
(* certified types *) |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
147 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
148 |
fun bad_nargs t = "Bad number of arguments for type constructor: " ^ quote t; |
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
149 |
fun undecl_type c = "Undeclared type constructor: " ^ quote c; |
|
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
150 |
|
|
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
151 |
local |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
152 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
153 |
fun inst_typ tye = |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
154 |
let |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
155 |
fun inst (var as (v, _)) = |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
156 |
(case assoc_string_int (tye, v) of |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
157 |
Some U => inst_typ tye U |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
158 |
| None => TVar var); |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
159 |
in map_type_tvar inst end; |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
160 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
161 |
fun norm_typ (tsig as TSig {types, ...}) ty =
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
162 |
let |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
163 |
val idx = Term.maxidx_of_typ ty + 1; |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
164 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
165 |
fun norm (Type (a, Ts)) = |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
166 |
(case Symtab.lookup (types, a) of |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
167 |
Some (Abbreviation (vs, U), _) => |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
168 |
norm (inst_typ (map (rpair idx) vs ~~ Ts) (incr_tvar idx U)) |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
169 |
| _ => Type (a, map norm Ts)) |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
170 |
| norm (TFree (x, S)) = TFree (x, norm_sort tsig S) |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
171 |
| norm (TVar (xi, S)) = TVar (xi, norm_sort tsig S); |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
172 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
173 |
val ty' = norm ty; |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
174 |
in if ty = ty' then ty else ty' end; (*avoid copying of already normal type*) |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
175 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
176 |
fun certify_typ normalize syntax tsig ty = |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
177 |
let |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
178 |
val TSig {types, ...} = tsig;
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
179 |
fun err msg = raise TYPE (msg, [ty], []); |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
180 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
181 |
fun check_sort S = (map (cert_class tsig) S; ()); |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
182 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
183 |
fun check_typ (Type (c, Ts)) = |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
184 |
let fun nargs n = if length Ts <> n then err (bad_nargs c) else () in |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
185 |
(case Symtab.lookup (types, c) of |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
186 |
Some (LogicalType n, _) => nargs n |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
187 |
| Some (Abbreviation (vs, _), _) => nargs (length vs) |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
188 |
| Some (Nonterminal, _) => nargs 0 |
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
189 |
| None => err (undecl_type c)); |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
190 |
seq check_typ Ts |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
191 |
end |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
192 |
| check_typ (TFree (_, S)) = check_sort S |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
193 |
| check_typ (TVar ((x, i), S)) = |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
194 |
if i < 0 then err ("Malformed type variable: " ^ quote (Term.string_of_vname (x, i)))
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
195 |
else check_sort S; |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
196 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
197 |
fun no_syntax (Type (c, Ts)) = |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
198 |
(case Symtab.lookup (types, c) of |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
199 |
Some (Nonterminal, _) => |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
200 |
err ("Illegal occurrence of syntactic type: " ^ quote c)
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
201 |
| _ => seq no_syntax Ts) |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
202 |
| no_syntax _ = (); |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
203 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
204 |
val _ = check_typ ty; |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
205 |
val ty' = if normalize orelse not syntax then norm_typ tsig ty else ty; |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
206 |
val _ = if not syntax then no_syntax ty' else (); |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
207 |
in ty' end; |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
208 |
|
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
209 |
in |
|
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
210 |
|
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
211 |
val cert_typ = certify_typ true false; |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
212 |
val cert_typ_syntax = certify_typ true true; |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
213 |
val cert_typ_raw = certify_typ false true; |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
214 |
|
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
215 |
end; |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
216 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
217 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
218 |
(** special treatment of type vars **) |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
219 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
220 |
(* strip_sorts *) |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
221 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
222 |
fun strip_sorts (Type (a, Ts)) = Type (a, map strip_sorts Ts) |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
223 |
| strip_sorts (TFree (x, _)) = TFree (x, []) |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
224 |
| strip_sorts (TVar (xi, _)) = TVar (xi, []); |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
225 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
226 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
227 |
(* no_tvars *) |
| 621 | 228 |
|
229 |
fun no_tvars T = |
|
| 12501 | 230 |
(case typ_tvars T of [] => T |
231 |
| vs => raise TYPE ("Illegal schematic type variable(s): " ^
|
|
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
232 |
commas_quote (map (Term.string_of_vname o #1) vs), [T], [])); |
| 621 | 233 |
|
| 7641 | 234 |
|
| 2964 | 235 |
(* varify, unvarify *) |
| 621 | 236 |
|
| 2964 | 237 |
val varifyT = map_type_tfree (fn (a, S) => TVar ((a, 0), S)); |
238 |
||
| 621 | 239 |
fun unvarifyT (Type (a, Ts)) = Type (a, map unvarifyT Ts) |
240 |
| unvarifyT (TVar ((a, 0), S)) = TFree (a, S) |
|
241 |
| unvarifyT T = T; |
|
242 |
||
243 |
fun varify (t, fixed) = |
|
244 |
let |
|
245 |
val fs = add_term_tfree_names (t, []) \\ fixed; |
|
246 |
val ixns = add_term_tvar_ixns (t, []); |
|
| 12501 | 247 |
val fmap = fs ~~ map (rpair 0) (variantlist (fs, map #1 ixns)) |
| 2964 | 248 |
fun thaw (f as (a, S)) = |
249 |
(case assoc (fmap, a) of |
|
250 |
None => TFree f |
|
| 12501 | 251 |
| Some b => TVar (b, S)); |
252 |
in (map_term_types (map_type_tfree thaw) t, fmap) end; |
|
| 2964 | 253 |
|
254 |
||
| 7641 | 255 |
(* freeze_thaw: freeze TVars in a term; return the "thaw" inverse *) |
|
3411
163f8f4a42d7
Removal of freeze_vars and thaw_vars. New freeze_thaw
paulson
parents:
3175
diff
changeset
|
256 |
|
| 7641 | 257 |
local |
258 |
||
259 |
fun new_name (ix, (pairs,used)) = |
|
|
3411
163f8f4a42d7
Removal of freeze_vars and thaw_vars. New freeze_thaw
paulson
parents:
3175
diff
changeset
|
260 |
let val v = variant used (string_of_indexname ix) |
|
163f8f4a42d7
Removal of freeze_vars and thaw_vars. New freeze_thaw
paulson
parents:
3175
diff
changeset
|
261 |
in ((ix,v)::pairs, v::used) end; |
| 621 | 262 |
|
| 7641 | 263 |
fun freeze_one alist (ix,sort) = |
| 3790 | 264 |
TFree (the (assoc (alist, ix)), sort) |
| 4142 | 265 |
handle OPTION => |
| 3790 | 266 |
raise TYPE ("Failure during freezing of ?" ^ string_of_indexname ix, [], []);
|
| 2964 | 267 |
|
| 7641 | 268 |
fun thaw_one alist (a,sort) = TVar (the (assoc (alist,a)), sort) |
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
269 |
handle OPTION => TFree(a, sort); |
| 416 | 270 |
|
| 10495 | 271 |
in |
272 |
||
273 |
(*this sort of code could replace unvarifyT*) |
|
| 7641 | 274 |
fun freeze_thaw_type T = |
275 |
let |
|
276 |
val used = add_typ_tfree_names (T, []) |
|
277 |
and tvars = map #1 (add_typ_tvars (T, [])); |
|
278 |
val (alist, _) = foldr new_name (tvars, ([], used)); |
|
279 |
in (map_type_tvar (freeze_one alist) T, map_type_tfree (thaw_one (map swap alist))) end; |
|
280 |
||
|
3411
163f8f4a42d7
Removal of freeze_vars and thaw_vars. New freeze_thaw
paulson
parents:
3175
diff
changeset
|
281 |
fun freeze_thaw t = |
| 7641 | 282 |
let |
283 |
val used = it_term_types add_typ_tfree_names (t, []) |
|
284 |
and tvars = map #1 (it_term_types add_typ_tvars (t, [])); |
|
285 |
val (alist, _) = foldr new_name (tvars, ([], used)); |
|
286 |
in |
|
287 |
(case alist of |
|
288 |
[] => (t, fn x => x) (*nothing to do!*) |
|
289 |
| _ => (map_term_types (map_type_tvar (freeze_one alist)) t, |
|
290 |
map_term_types (map_type_tfree (thaw_one (map swap alist))))) |
|
291 |
end; |
|
292 |
||
293 |
end; |
|
294 |
||
| 256 | 295 |
|
296 |
||
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
297 |
(** matching and unification of types **) |
| 8899 | 298 |
|
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
299 |
(* instantiation *) |
| 256 | 300 |
|
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
301 |
fun type_of_sort pp tsig (T, S) = |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
302 |
if of_sort tsig (T, S) then T |
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
303 |
else raise TYPE ("Type not of sort " ^ Pretty.string_of_sort pp S, [T], []);
|
| 0 | 304 |
|
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
305 |
fun inst_typ_tvars pp tsig tye = |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
306 |
let |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
307 |
fun inst (var as (v, S)) = |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
308 |
(case assoc_string_int (tye, v) of |
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
309 |
Some U => type_of_sort pp tsig (U, S) |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
310 |
| None => TVar var); |
|
949
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
311 |
in map_type_tvar inst end; |
| 0 | 312 |
|
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
313 |
fun inst_term_tvars _ _ [] t = t |
|
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
314 |
| inst_term_tvars pp tsig tye t = map_term_types (inst_typ_tvars pp tsig tye) t; |
| 0 | 315 |
|
316 |
||
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
317 |
(* matching *) |
| 0 | 318 |
|
| 2964 | 319 |
exception TYPE_MATCH; |
| 0 | 320 |
|
| 2964 | 321 |
fun typ_match tsig = |
322 |
let |
|
323 |
fun match (subs, (TVar (v, S), T)) = |
|
|
8406
a217b0cd304d
Type.unify and Type.typ_match now use Vartab instead of association lists.
berghofe
parents:
7641
diff
changeset
|
324 |
(case Vartab.lookup (subs, v) of |
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
325 |
None => (Vartab.update_new ((v, type_of_sort Pretty.pp_undef tsig (T, S)), subs) |
| 2964 | 326 |
handle TYPE _ => raise TYPE_MATCH) |
327 |
| Some U => if U = T then subs else raise TYPE_MATCH) |
|
328 |
| match (subs, (Type (a, Ts), Type (b, Us))) = |
|
329 |
if a <> b then raise TYPE_MATCH |
|
330 |
else foldl match (subs, Ts ~~ Us) |
|
331 |
| match (subs, (TFree x, TFree y)) = |
|
332 |
if x = y then subs else raise TYPE_MATCH |
|
333 |
| match _ = raise TYPE_MATCH; |
|
334 |
in match end; |
|
| 0 | 335 |
|
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
336 |
fun typ_instance tsig (T, U) = |
|
8406
a217b0cd304d
Type.unify and Type.typ_match now use Vartab instead of association lists.
berghofe
parents:
7641
diff
changeset
|
337 |
(typ_match tsig (Vartab.empty, (U, T)); true) handle TYPE_MATCH => false; |
| 2964 | 338 |
|
| 0 | 339 |
|
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
340 |
(* unification *) |
| 2964 | 341 |
|
| 0 | 342 |
exception TUNIFY; |
343 |
||
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
344 |
(*occurs_check*) |
| 2964 | 345 |
fun occurs v tye = |
346 |
let |
|
347 |
fun occ (Type (_, Ts)) = exists occ Ts |
|
348 |
| occ (TFree _) = false |
|
349 |
| occ (TVar (w, _)) = |
|
350 |
eq_ix (v, w) orelse |
|
|
8406
a217b0cd304d
Type.unify and Type.typ_match now use Vartab instead of association lists.
berghofe
parents:
7641
diff
changeset
|
351 |
(case Vartab.lookup (tye, w) of |
| 2964 | 352 |
None => false |
353 |
| Some U => occ U); |
|
| 0 | 354 |
in occ end; |
355 |
||
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
356 |
(*chase variable assignments; if devar returns a type var then it must be unassigned*) |
| 2964 | 357 |
fun devar (T as TVar (v, _), tye) = |
|
8406
a217b0cd304d
Type.unify and Type.typ_match now use Vartab instead of association lists.
berghofe
parents:
7641
diff
changeset
|
358 |
(case Vartab.lookup (tye, v) of |
| 2964 | 359 |
Some U => devar (U, tye) |
360 |
| None => T) |
|
| 256 | 361 |
| devar (T, tye) = T; |
| 0 | 362 |
|
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
363 |
fun unify (tsig as TSig {classes, arities, ...}) (tyenv, maxidx) TU =
|
| 2964 | 364 |
let |
365 |
val tyvar_count = ref maxidx; |
|
366 |
fun gen_tyvar S = TVar (("'a", inc tyvar_count), S);
|
|
367 |
||
368 |
fun mg_domain a S = |
|
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
369 |
Sorts.mg_domain (classes, arities) a S handle Sorts.DOMAIN _ => raise TUNIFY; |
| 2964 | 370 |
|
371 |
fun meet ((_, []), tye) = tye |
|
372 |
| meet ((TVar (xi, S'), S), tye) = |
|
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
373 |
if Sorts.sort_le classes (S', S) then tye |
|
13666
a2730043029b
Removed add_env because Vartab.map was too slow for large environments.
berghofe
parents:
12726
diff
changeset
|
374 |
else Vartab.update_new ((xi, |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
375 |
gen_tyvar (Sorts.inter_sort classes (S', S))), tye) |
| 2964 | 376 |
| meet ((TFree (_, S'), S), tye) = |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
377 |
if Sorts.sort_le classes (S', S) then tye |
| 2964 | 378 |
else raise TUNIFY |
379 |
| meet ((Type (a, Ts), S), tye) = meets ((Ts, mg_domain a S), tye) |
|
380 |
and meets (([], []), tye) = tye |
|
381 |
| meets ((T :: Ts, S :: Ss), tye) = |
|
382 |
meets ((Ts, Ss), meet ((devar (T, tye), S), tye)) |
|
383 |
| meets _ = sys_error "meets"; |
|
384 |
||
385 |
fun unif ((ty1, ty2), tye) = |
|
386 |
(case (devar (ty1, tye), devar (ty2, tye)) of |
|
387 |
(T as TVar (v, S1), U as TVar (w, S2)) => |
|
388 |
if eq_ix (v, w) then tye |
|
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
389 |
else if Sorts.sort_le classes (S1, S2) then |
|
13666
a2730043029b
Removed add_env because Vartab.map was too slow for large environments.
berghofe
parents:
12726
diff
changeset
|
390 |
Vartab.update_new ((w, T), tye) |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
391 |
else if Sorts.sort_le classes (S2, S1) then |
|
13666
a2730043029b
Removed add_env because Vartab.map was too slow for large environments.
berghofe
parents:
12726
diff
changeset
|
392 |
Vartab.update_new ((v, U), tye) |
| 2964 | 393 |
else |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
394 |
let val S = gen_tyvar (Sorts.inter_sort classes (S1, S2)) in |
|
13666
a2730043029b
Removed add_env because Vartab.map was too slow for large environments.
berghofe
parents:
12726
diff
changeset
|
395 |
Vartab.update_new ((v, S), Vartab.update_new ((w, S), tye)) |
| 2964 | 396 |
end |
397 |
| (TVar (v, S), T) => |
|
398 |
if occurs v tye T then raise TUNIFY |
|
|
13666
a2730043029b
Removed add_env because Vartab.map was too slow for large environments.
berghofe
parents:
12726
diff
changeset
|
399 |
else meet ((T, S), Vartab.update_new ((v, T), tye)) |
| 2964 | 400 |
| (T, TVar (v, S)) => |
401 |
if occurs v tye T then raise TUNIFY |
|
|
13666
a2730043029b
Removed add_env because Vartab.map was too slow for large environments.
berghofe
parents:
12726
diff
changeset
|
402 |
else meet ((T, S), Vartab.update_new ((v, T), tye)) |
| 2964 | 403 |
| (Type (a, Ts), Type (b, Us)) => |
404 |
if a <> b then raise TUNIFY |
|
405 |
else foldr unif (Ts ~~ Us, tye) |
|
406 |
| (T, U) => if T = U then tye else raise TUNIFY); |
|
| 12528 | 407 |
in (unif (TU, tyenv), ! tyvar_count) end; |
| 0 | 408 |
|
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
409 |
(*purely structural unification *) |
| 450 | 410 |
fun raw_unify (ty1, ty2) = |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
411 |
(unify empty_tsig (Vartab.empty, 0) (strip_sorts ty1, strip_sorts ty2); true) |
| 450 | 412 |
handle TUNIFY => false; |
413 |
||
414 |
||
| 0 | 415 |
|
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
416 |
(** extend and merge type signatures **) |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
417 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
418 |
(* arities *) |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
419 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
420 |
local |
|
1435
aefcd255ed4a
Removed bug in type unification. Negative indexes are not used any longer.
nipkow
parents:
1392
diff
changeset
|
421 |
|
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
422 |
fun err_decl t decl = error ("Illegal " ^ str_of_decl decl ^ ": " ^ quote t);
|
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
423 |
|
|
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
424 |
fun for_classes _ None = "" |
|
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
425 |
| for_classes pp (Some (c1, c2)) = |
|
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
426 |
" for classes " ^ Pretty.string_of_classrel pp [c1, c2]; |
| 3790 | 427 |
|
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
428 |
fun err_conflict pp t cc (c, Ss) (c', Ss') = |
|
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
429 |
error ("Conflict of type arities" ^ for_classes pp cc ^ ":\n " ^
|
|
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
430 |
Pretty.string_of_arity pp (t, Ss, [c]) ^ " and\n " ^ |
|
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
431 |
Pretty.string_of_arity pp (t, Ss', [c'])); |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
432 |
|
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
433 |
fun coregular pp C t (c, Ss) ars = |
| 3790 | 434 |
let |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
435 |
fun conflict (c', Ss') = |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
436 |
if Sorts.class_le C (c, c') andalso not (Sorts.sorts_le C (Ss, Ss')) then |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
437 |
Some ((c, c'), (c', Ss')) |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
438 |
else if Sorts.class_le C (c', c) andalso not (Sorts.sorts_le C (Ss', Ss)) then |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
439 |
Some ((c', c), (c', Ss')) |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
440 |
else None; |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
441 |
in |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
442 |
(case Library.get_first conflict ars of |
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
443 |
Some ((c1, c2), (c', Ss')) => err_conflict pp t (Some (c1, c2)) (c, Ss) (c', Ss') |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
444 |
| None => (c, Ss) :: ars) |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
445 |
end; |
| 3790 | 446 |
|
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
447 |
fun insert pp C t ((c, Ss), ars) = |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
448 |
(case assoc_string (ars, c) of |
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
449 |
None => coregular pp C t (c, Ss) ars |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
450 |
| Some Ss' => |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
451 |
if Sorts.sorts_le C (Ss, Ss') then ars |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
452 |
else if Sorts.sorts_le C (Ss', Ss) |
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
453 |
then coregular pp C t (c, Ss) (ars \ (c, Ss')) |
|
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
454 |
else err_conflict pp t None (c, Ss) (c, Ss')); |
| 2587 | 455 |
|
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
456 |
fun complete C (c, Ss) = map (rpair Ss) (Graph.all_succs C [c]); |
| 3790 | 457 |
|
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
458 |
fun insert_arities pp classes (arities, (t, ars)) = |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
459 |
let val ars' = |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
460 |
Symtab.lookup_multi (arities, t) |
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
461 |
|> curry (foldr (insert pp classes t)) (flat (map (complete classes) ars)) |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
462 |
in Symtab.update ((t, ars'), arities) end; |
| 3790 | 463 |
|
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
464 |
fun insert_table pp classes = Symtab.foldl (fn (arities, (t, ars)) => |
|
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
465 |
insert_arities pp classes (arities, (t, map (apsnd (map (Sorts.norm_sort classes))) ars))); |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
466 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
467 |
in |
| 2587 | 468 |
|
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
469 |
fun add_arities pp decls tsig = tsig |> change_tsig (fn (classes, default, types, arities) => |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
470 |
let |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
471 |
fun prep (t, Ss, S) = |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
472 |
(case Symtab.lookup (types, t) of |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
473 |
Some (LogicalType n, _) => |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
474 |
if length Ss = n then |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
475 |
(t, map (cert_sort tsig) Ss, cert_sort tsig S) |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
476 |
handle TYPE (msg, _, _) => error msg |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
477 |
else error (bad_nargs t) |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
478 |
| Some (decl, _) => err_decl t decl |
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
479 |
| None => error (undecl_type t)); |
| 4603 | 480 |
|
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
481 |
val ars = decls |> map ((fn (t, Ss, S) => (t, map (fn c => (c, Ss)) S)) o prep); |
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
482 |
val arities' = foldl (insert_arities pp classes) (arities, ars); |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
483 |
in (classes, default, types, arities') end); |
| 12528 | 484 |
|
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
485 |
fun rebuild_arities pp classes arities = |
|
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
486 |
insert_table pp classes (Symtab.empty, arities); |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
487 |
|
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
488 |
fun merge_arities pp classes (arities1, arities2) = |
|
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
489 |
insert_table pp classes (insert_table pp classes (Symtab.empty, arities1), arities2); |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
490 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
491 |
end; |
| 4603 | 492 |
|
493 |
||
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
494 |
(* classes *) |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
495 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
496 |
local |
| 0 | 497 |
|
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
498 |
fun err_dup_classes cs = |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
499 |
error ("Duplicate declaration of class(es): " ^ commas_quote cs);
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
500 |
|
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
501 |
fun err_cyclic_classes pp css = |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
502 |
error (cat_lines (map (fn cs => |
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
503 |
"Cycle in class relation: " ^ Pretty.string_of_classrel pp cs) css)); |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
504 |
|
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
505 |
fun add_class pp (c, cs) tsig = tsig |> change_tsig (fn (classes, default, types, arities) => |
| 2964 | 506 |
let |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
507 |
val cs' = map (cert_class tsig) cs |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
508 |
handle TYPE (msg, _, _) => error msg; |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
509 |
val classes' = classes |> Graph.new_node (c, stamp ()) |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
510 |
handle Graph.DUP d => err_dup_classes [d]; |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
511 |
val classes'' = classes' |> fold Graph.add_edge_trans_acyclic (map (pair c) cs') |
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
512 |
handle Graph.CYCLES css => err_cyclic_classes pp css; |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
513 |
in (classes'', default, types, arities) end); |
| 2964 | 514 |
|
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
515 |
in |
| 0 | 516 |
|
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
517 |
val add_classes = fold o add_class; |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
518 |
|
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
519 |
fun add_classrel pp ps tsig = tsig |> change_tsig (fn (classes, default, types, arities) => |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
520 |
let |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
521 |
val ps' = map (pairself (cert_class tsig)) ps |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
522 |
handle TYPE (msg, _, _) => error msg; |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
523 |
val classes' = classes |> fold Graph.add_edge_trans_acyclic ps' |
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
524 |
handle Graph.CYCLES css => err_cyclic_classes pp css; |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
525 |
val default' = default |> Sorts.norm_sort classes'; |
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
526 |
val arities' = arities |> rebuild_arities pp classes'; |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
527 |
in (classes', default', types, arities') end); |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
528 |
|
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
529 |
fun merge_classes pp CC = Graph.merge_trans_acyclic (op =) CC |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
530 |
handle Graph.DUPS cs => err_dup_classes cs |
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
531 |
| Graph.CYCLES css => err_cyclic_classes pp css; |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
532 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
533 |
end; |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
534 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
535 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
536 |
(* default sort *) |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
537 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
538 |
fun set_defsort S tsig = tsig |> change_tsig (fn (classes, _, types, arities) => |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
539 |
(classes, cert_sort tsig S handle TYPE (msg, _, _) => error msg, types, arities)); |
| 0 | 540 |
|
541 |
||
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
542 |
(* types *) |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
543 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
544 |
local |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
545 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
546 |
fun err_neg_args c = |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
547 |
error ("Negative number of arguments in type constructor declaration: " ^ quote c);
|
|
949
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
548 |
|
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
549 |
fun err_in_decls c decl decl' = |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
550 |
let |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
551 |
val s = str_of_decl decl; |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
552 |
val s' = str_of_decl decl'; |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
553 |
in |
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
554 |
if s = s' then |
|
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
555 |
error ("Duplicate declaration of " ^ s ^ ": " ^ quote c)
|
|
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
556 |
else error ("Conflict of " ^ s ^ " with " ^ s' ^ ": " ^ quote c)
|
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
557 |
end; |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
558 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
559 |
fun new_decl (c, decl) types = |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
560 |
(case Symtab.lookup (types, c) of |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
561 |
Some (decl', _) => err_in_decls c decl decl' |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
562 |
| None => Symtab.update ((c, (decl, stamp ())), types)); |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
563 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
564 |
fun the_decl types c = fst (the (Symtab.lookup (types, c))); |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
565 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
566 |
fun change_types f = change_tsig (fn (classes, default, types, arities) => |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
567 |
(classes, default, f types, arities)); |
| 2964 | 568 |
|
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
569 |
fun add_abbr (a, vs, rhs) tsig = tsig |> change_types (fn types => |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
570 |
let |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
571 |
fun err msg = |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
572 |
error (msg ^ "\nThe error(s) above occurred in type abbreviation " ^ quote a); |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
573 |
val rhs' = strip_sorts (varifyT (no_tvars (cert_typ_syntax tsig rhs))) |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
574 |
handle TYPE (msg, _, _) => err msg; |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
575 |
in |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
576 |
(case duplicates vs of |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
577 |
[] => [] |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
578 |
| dups => err ("Duplicate variables on lhs: " ^ commas_quote dups));
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
579 |
(case gen_rems (op =) (map (#1 o #1) (typ_tvars rhs'), vs) of |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
580 |
[] => [] |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
581 |
| extras => err ("Extra variables on rhs: " ^ commas_quote extras));
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
582 |
types |> new_decl (a, Abbreviation (vs, rhs')) |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
583 |
end); |
| 0 | 584 |
|
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
585 |
in |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
586 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
587 |
fun add_types ps = change_types (fold new_decl (ps |> map (fn (c, n) => |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
588 |
if n < 0 then err_neg_args c else (c, LogicalType n)))); |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
589 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
590 |
val add_abbrs = fold add_abbr; |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
591 |
val add_nonterminals = change_types o fold new_decl o map (rpair Nonterminal); |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
592 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
593 |
fun merge_types (types1, types2) = |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
594 |
Symtab.merge Library.eq_snd (types1, types2) handle Symtab.DUPS (d :: _) => |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
595 |
err_in_decls d (the_decl types1 d) (the_decl types2 d); |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
596 |
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
597 |
end; |
| 0 | 598 |
|
| 3790 | 599 |
|
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
600 |
(* merge type signatures *) |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
601 |
|
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
602 |
fun merge_tsigs pp (tsig1, tsig2) = |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
603 |
let |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
604 |
val (TSig {classes = classes1, default = default1, types = types1, arities = arities1,
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
605 |
log_types = _, witness = _}) = tsig1; |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
606 |
val (TSig {classes = classes2, default = default2, types = types2, arities = arities2,
|
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
607 |
log_types = _, witness = _}) = tsig2; |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
608 |
|
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
609 |
val classes' = merge_classes pp (classes1, classes2); |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
610 |
val default' = Sorts.inter_sort classes' (default1, default2); |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
611 |
val types' = merge_types (types1, types2); |
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
612 |
val arities' = merge_arities pp classes' (arities1, arities2); |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
613 |
in build_tsig (classes', default', types', arities') end; |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
614 |
|
| 0 | 615 |
end; |