author | wenzelm |
Sat, 28 Jan 2006 17:29:00 +0100 | |
changeset 18826 | 2a805b3dd9f0 |
parent 17804 | 4deae4b33d97 |
child 18928 | 042608ffa2ec |
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 | |
14989 | 14 |
Abbreviation of string list * typ * bool | |
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
|
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 -> |
16370
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
18 |
{classes: NameSpace.T * Sorts.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
|
19 |
default: sort, |
16370
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
20 |
types: (decl * stamp) NameSpace.table, |
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
|
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 |
14993 | 35 |
val cert_typ: tsig -> typ -> typ |
36 |
val cert_typ_syntax: tsig -> typ -> typ |
|
16340 | 37 |
val cert_typ_abbrev: tsig -> typ -> typ |
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
|
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 |
|
15797 | 44 |
val varify: term * (string * sort) list -> term * ((string * sort) * indexname) list |
16289 | 45 |
val freeze_thaw_type: typ -> typ * (typ -> typ) |
46 |
val freeze_type: typ -> typ |
|
47 |
val freeze_thaw: term -> term * (term -> term) |
|
48 |
val freeze: term -> term |
|
2964 | 49 |
|
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
|
50 |
(*matching and unification*) |
2964 | 51 |
exception TYPE_MATCH |
15797 | 52 |
type tyenv |
53 |
val lookup: tyenv * (indexname * sort) -> typ option |
|
16946 | 54 |
val typ_match: tsig -> typ * typ -> tyenv -> tyenv |
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 typ_instance: tsig -> typ * typ -> bool |
16946 | 56 |
val raw_match: typ * typ -> tyenv -> tyenv |
57 |
val raw_instance: typ * typ -> bool |
|
2964 | 58 |
exception TUNIFY |
16946 | 59 |
val unify: tsig -> typ * typ -> tyenv * int -> tyenv * int |
60 |
val raw_unify: typ * typ -> tyenv -> tyenv |
|
17804 | 61 |
val could_unify: typ * typ -> bool |
16650 | 62 |
val eq_type: tyenv -> typ * typ -> bool |
0 | 63 |
|
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
|
64 |
(*extend and merge type signatures*) |
16370
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
65 |
val add_classes: Pretty.pp -> NameSpace.naming -> (bstring * class list) list -> tsig -> tsig |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
66 |
val hide_classes: bool -> string list -> tsig -> tsig |
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
67 |
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
|
68 |
val set_defsort: sort -> tsig -> tsig |
16370
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
69 |
val add_types: NameSpace.naming -> (bstring * int) list -> tsig -> tsig |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
70 |
val add_abbrevs: NameSpace.naming -> (string * string list * typ) list -> tsig -> tsig |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
71 |
val add_nonterminals: NameSpace.naming -> string list -> tsig -> tsig |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
72 |
val hide_types: bool -> string list -> tsig -> tsig |
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
73 |
val add_arities: Pretty.pp -> arity list -> tsig -> tsig |
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
74 |
val merge_tsigs: Pretty.pp -> tsig * tsig -> tsig |
2964 | 75 |
end; |
76 |
||
77 |
structure Type: TYPE = |
|
0 | 78 |
struct |
79 |
||
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
|
80 |
(** type signatures and certified types **) |
2964 | 81 |
|
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
|
82 |
(* 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
|
83 |
|
0d984ee030a1
major cleanup of tsig datastructures 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 |
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
|
85 |
LogicalType of int | |
14989 | 86 |
Abbreviation of string list * typ * bool | |
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
|
87 |
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
|
88 |
|
0d984ee030a1
major cleanup of tsig datastructures 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 |
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
|
90 |
| 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
|
91 |
| 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
|
92 |
|
0d984ee030a1
major cleanup of tsig datastructures 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 |
|
0d984ee030a1
major cleanup of tsig datastructures 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 |
(* 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
|
95 |
|
0d984ee030a1
major cleanup of tsig datastructures 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 |
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
|
97 |
TSig of { |
16370
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
98 |
classes: NameSpace.T * Sorts.classes, (*declared classes with proper subclass relation*) |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
99 |
default: sort, (*default sort on input*) |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
100 |
types: (decl * stamp) NameSpace.table, (*declared types*) |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
101 |
arities: Sorts.arities, (*image specification of types wrt. sorts*) |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
102 |
log_types: string list, (*logical types sorted by number of arguments*) |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
103 |
witness: (typ * sort) option}; (*witness for non-emptiness of strictest sort*) |
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
|
104 |
|
0d984ee030a1
major cleanup of tsig datastructures 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 |
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
|
106 |
|
0d984ee030a1
major cleanup of tsig datastructures 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 |
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
|
108 |
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
|
109 |
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
|
110 |
|
0d984ee030a1
major cleanup of tsig datastructures 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 |
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
|
112 |
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
|
113 |
val log_types = |
17756 | 114 |
Symtab.fold (fn (c, (LogicalType n, _)) => cons (c, n) | _ => I) (snd types) [] |
115 |
|> Library.sort (Library.int_ord o pairself snd) |> map fst; |
|
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
|
116 |
val witness = |
17756 | 117 |
(case Sorts.witness_sorts (snd classes, arities) log_types [] [Graph.keys (snd classes)] of |
15531 | 118 |
[w] => SOME w | _ => NONE); |
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
|
119 |
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
|
120 |
|
16370
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
121 |
fun map_tsig f (TSig {classes, default, types, arities, log_types = _, witness = _}) = |
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
|
122 |
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
|
123 |
|
16370
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
124 |
val empty_tsig = |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
125 |
build_tsig ((NameSpace.empty, Graph.empty), [], NameSpace.empty_table, Symtab.empty); |
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
|
126 |
|
0d984ee030a1
major cleanup of tsig datastructures 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 |
|
0d984ee030a1
major cleanup of tsig datastructures 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 |
(* 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
|
129 |
|
16370
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
130 |
fun classes (TSig {classes = (_, C), ...}) = Graph.keys 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
|
131 |
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
|
132 |
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
|
133 |
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
|
134 |
|
16370
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
135 |
fun eq_sort (TSig {classes, ...}) = Sorts.sort_eq (#2 classes); |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
136 |
fun subsort (TSig {classes, ...}) = Sorts.sort_le (#2 classes); |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
137 |
fun of_sort (TSig {classes, arities, ...}) = Sorts.of_sort (#2 classes, 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
|
138 |
|
16370
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
139 |
fun cert_class (TSig {classes, ...}) = Sorts.certify_class (#2 classes); |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
140 |
fun cert_sort (TSig {classes, ...}) = Sorts.certify_sort (#2 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
|
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, ...}) = |
16370
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
143 |
Sorts.witness_sorts (#2 classes, arities) log_types; |
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
|
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 |
|
14998 | 151 |
local |
152 |
||
153 |
fun inst_typ env (Type (c, Ts)) = Type (c, map (inst_typ env) Ts) |
|
17184 | 154 |
| inst_typ env (T as TFree (x, _)) = if_none (AList.lookup (op =) env x) T |
14998 | 155 |
| inst_typ _ T = T; |
156 |
||
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
|
157 |
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
|
158 |
let |
16370
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
159 |
val TSig {classes = (_, classes), types = (_, types), ...} = 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
|
160 |
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
|
161 |
|
14989 | 162 |
val check_syntax = |
163 |
if syntax then K () |
|
164 |
else fn c => err ("Illegal occurrence of syntactic type: " ^ quote c); |
|
165 |
||
166 |
fun cert (T as Type (c, Ts)) = |
|
167 |
let |
|
168 |
val Ts' = map cert Ts; |
|
169 |
fun nargs n = if length Ts <> n then err (bad_nargs c) else (); |
|
170 |
in |
|
17412 | 171 |
(case Symtab.lookup types c of |
15531 | 172 |
SOME (LogicalType n, _) => (nargs n; Type (c, Ts')) |
173 |
| SOME (Abbreviation (vs, U, syn), _) => (nargs (length vs); |
|
14989 | 174 |
if syn then check_syntax c else (); |
14993 | 175 |
if normalize then inst_typ (vs ~~ Ts') U |
14989 | 176 |
else Type (c, Ts')) |
15531 | 177 |
| SOME (Nonterminal, _) => (nargs 0; check_syntax c; T) |
178 |
| 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
|
179 |
end |
14989 | 180 |
| cert (TFree (x, S)) = TFree (x, Sorts.certify_sort classes S) |
181 |
| cert (TVar (xi as (_, i), S)) = |
|
14993 | 182 |
if i < 0 then |
183 |
err ("Malformed type variable: " ^ quote (Term.string_of_vname xi)) |
|
14989 | 184 |
else TVar (xi, Sorts.certify_sort classes 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
|
185 |
|
14989 | 186 |
val ty' = cert ty; |
14993 | 187 |
in if ty = ty' then ty else ty' end; (*avoid copying of already normal type*) |
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
188 |
|
14998 | 189 |
in |
190 |
||
16340 | 191 |
val cert_typ = certify_typ true false; |
192 |
val cert_typ_syntax = certify_typ true true; |
|
193 |
val cert_typ_abbrev = certify_typ false true; |
|
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
|
194 |
|
14998 | 195 |
end; |
196 |
||
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
|
197 |
|
0d984ee030a1
major cleanup of tsig datastructures 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 |
|
0d984ee030a1
major cleanup of tsig datastructures 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 |
(** 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
|
200 |
|
0d984ee030a1
major cleanup of tsig datastructures 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 |
(* 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
|
202 |
|
0d984ee030a1
major cleanup of tsig datastructures 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 |
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
|
204 |
| 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
|
205 |
| 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
|
206 |
|
0d984ee030a1
major cleanup of tsig datastructures 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 |
|
0d984ee030a1
major cleanup of tsig datastructures 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 |
(* no_tvars *) |
621 | 209 |
|
210 |
fun no_tvars T = |
|
12501 | 211 |
(case typ_tvars T of [] => T |
212 |
| vs => raise TYPE ("Illegal schematic type variable(s): " ^ |
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
213 |
commas_quote (map (Term.string_of_vname o #1) vs), [T], [])); |
621 | 214 |
|
7641 | 215 |
|
2964 | 216 |
(* varify, unvarify *) |
621 | 217 |
|
2964 | 218 |
val varifyT = map_type_tfree (fn (a, S) => TVar ((a, 0), S)); |
16946 | 219 |
val unvarifyT = map_type_tvar (fn ((a, 0), S) => TFree (a, S) | v => TVar v); |
621 | 220 |
|
221 |
fun varify (t, fixed) = |
|
222 |
let |
|
15797 | 223 |
val fs = add_term_tfrees (t, []) \\ fixed; |
621 | 224 |
val ixns = add_term_tvar_ixns (t, []); |
15797 | 225 |
val fmap = fs ~~ map (rpair 0) (variantlist (map fst fs, map #1 ixns)) |
2964 | 226 |
fun thaw (f as (a, S)) = |
17184 | 227 |
(case AList.lookup (op =) fmap f of |
15531 | 228 |
NONE => TFree f |
16946 | 229 |
| SOME xi => TVar (xi, S)); |
12501 | 230 |
in (map_term_types (map_type_tfree thaw) t, fmap) end; |
2964 | 231 |
|
232 |
||
7641 | 233 |
(* 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
|
234 |
|
7641 | 235 |
local |
236 |
||
16289 | 237 |
fun new_name (ix, (pairs, used)) = |
238 |
let val v = variant used (string_of_indexname ix) |
|
239 |
in ((ix, v) :: pairs, v :: used) end; |
|
621 | 240 |
|
16289 | 241 |
fun freeze_one alist (ix, sort) = |
17184 | 242 |
TFree (the (AList.lookup (op =) alist ix), sort) |
15531 | 243 |
handle Option => |
3790 | 244 |
raise TYPE ("Failure during freezing of ?" ^ string_of_indexname ix, [], []); |
2964 | 245 |
|
17184 | 246 |
fun thaw_one alist (a, sort) = TVar (the (AList.lookup (op =) alist a), sort) |
16289 | 247 |
handle Option => TFree (a, sort); |
416 | 248 |
|
10495 | 249 |
in |
250 |
||
251 |
(*this sort of code could replace unvarifyT*) |
|
7641 | 252 |
fun freeze_thaw_type T = |
253 |
let |
|
254 |
val used = add_typ_tfree_names (T, []) |
|
255 |
and tvars = map #1 (add_typ_tvars (T, [])); |
|
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
256 |
val (alist, _) = foldr new_name ([], used) tvars; |
7641 | 257 |
in (map_type_tvar (freeze_one alist) T, map_type_tfree (thaw_one (map swap alist))) end; |
258 |
||
16289 | 259 |
val freeze_type = #1 o freeze_thaw_type; |
260 |
||
3411
163f8f4a42d7
Removal of freeze_vars and thaw_vars. New freeze_thaw
paulson
parents:
3175
diff
changeset
|
261 |
fun freeze_thaw t = |
7641 | 262 |
let |
263 |
val used = it_term_types add_typ_tfree_names (t, []) |
|
264 |
and tvars = map #1 (it_term_types add_typ_tvars (t, [])); |
|
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
265 |
val (alist, _) = foldr new_name ([], used) tvars; |
7641 | 266 |
in |
267 |
(case alist of |
|
268 |
[] => (t, fn x => x) (*nothing to do!*) |
|
269 |
| _ => (map_term_types (map_type_tvar (freeze_one alist)) t, |
|
270 |
map_term_types (map_type_tfree (thaw_one (map swap alist))))) |
|
271 |
end; |
|
272 |
||
16289 | 273 |
val freeze = #1 o freeze_thaw; |
274 |
||
7641 | 275 |
end; |
276 |
||
256 | 277 |
|
278 |
||
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
|
279 |
(** matching and unification of types **) |
8899 | 280 |
|
15797 | 281 |
type tyenv = (sort * typ) Vartab.table; |
256 | 282 |
|
15797 | 283 |
fun tvar_clash ixn S S' = raise TYPE ("Type variable " ^ |
284 |
quote (Term.string_of_vname ixn) ^ " has two distinct sorts", |
|
285 |
[TVar (ixn, S), TVar (ixn, S')], []); |
|
0 | 286 |
|
16289 | 287 |
fun lookup (tye, (ixn, S)) = |
17412 | 288 |
(case Vartab.lookup tye ixn of |
15797 | 289 |
NONE => NONE |
16289 | 290 |
| SOME (S', T) => if S = S' then SOME T else tvar_clash ixn S S'); |
291 |
||
0 | 292 |
|
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
|
293 |
(* matching *) |
0 | 294 |
|
2964 | 295 |
exception TYPE_MATCH; |
0 | 296 |
|
16946 | 297 |
fun typ_match tsig = |
2964 | 298 |
let |
16340 | 299 |
fun match (TVar (v, S), T) subs = |
15797 | 300 |
(case lookup (subs, (v, S)) of |
15531 | 301 |
NONE => |
17412 | 302 |
if of_sort tsig (T, S) then Vartab.update_new (v, (S, T)) subs |
14993 | 303 |
else raise TYPE_MATCH |
15531 | 304 |
| SOME U => if U = T then subs else raise TYPE_MATCH) |
16340 | 305 |
| match (Type (a, Ts), Type (b, Us)) subs = |
2964 | 306 |
if a <> b then raise TYPE_MATCH |
16885 | 307 |
else matches (Ts, Us) subs |
16340 | 308 |
| match (TFree x, TFree y) subs = |
2964 | 309 |
if x = y then subs else raise TYPE_MATCH |
16885 | 310 |
| match _ _ = raise TYPE_MATCH |
311 |
and matches (T :: Ts, U :: Us) subs = matches (Ts, Us) (match (T, U) subs) |
|
312 |
| matches _ subs = subs; |
|
16946 | 313 |
in match end; |
0 | 314 |
|
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
|
315 |
fun typ_instance tsig (T, U) = |
16946 | 316 |
(typ_match tsig (U, T) Vartab.empty; true) handle TYPE_MATCH => false; |
317 |
||
318 |
(*purely structural matching*) |
|
319 |
fun raw_match (TVar (v, S), T) subs = |
|
320 |
(case lookup (subs, (v, S)) of |
|
17412 | 321 |
NONE => Vartab.update_new (v, (S, T)) subs |
16946 | 322 |
| SOME U => if U = T then subs else raise TYPE_MATCH) |
323 |
| raw_match (Type (a, Ts), Type (b, Us)) subs = |
|
324 |
if a <> b then raise TYPE_MATCH |
|
325 |
else raw_matches (Ts, Us) subs |
|
326 |
| raw_match (TFree x, TFree y) subs = |
|
327 |
if x = y then subs else raise TYPE_MATCH |
|
328 |
| raw_match _ _ = raise TYPE_MATCH |
|
329 |
and raw_matches (T :: Ts, U :: Us) subs = raw_matches (Ts, Us) (raw_match (T, U) subs) |
|
330 |
| raw_matches _ subs = subs; |
|
331 |
||
332 |
fun raw_instance (T, U) = |
|
333 |
(raw_match (U, T) Vartab.empty; true) handle TYPE_MATCH => false; |
|
2964 | 334 |
|
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 |
(* unification *) |
2964 | 337 |
|
0 | 338 |
exception TUNIFY; |
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 |
(*occurs_check*) |
2964 | 341 |
fun occurs v tye = |
342 |
let |
|
343 |
fun occ (Type (_, Ts)) = exists occ Ts |
|
344 |
| occ (TFree _) = false |
|
15797 | 345 |
| occ (TVar (w, S)) = |
2964 | 346 |
eq_ix (v, w) orelse |
15797 | 347 |
(case lookup (tye, (w, S)) of |
15531 | 348 |
NONE => false |
349 |
| SOME U => occ U); |
|
0 | 350 |
in occ end; |
351 |
||
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
|
352 |
(*chase variable assignments; if devar returns a type var then it must be unassigned*) |
16885 | 353 |
fun devar tye (T as TVar v) = |
354 |
(case lookup (tye, v) of |
|
355 |
SOME U => devar tye U |
|
15531 | 356 |
| NONE => T) |
16885 | 357 |
| devar tye T = T; |
0 | 358 |
|
17804 | 359 |
(*order-sorted unification*) |
16946 | 360 |
fun unify (tsig as TSig {classes = (_, classes), arities, ...}) TU (tyenv, maxidx) = |
2964 | 361 |
let |
362 |
val tyvar_count = ref maxidx; |
|
363 |
fun gen_tyvar S = TVar (("'a", inc tyvar_count), S); |
|
364 |
||
365 |
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
|
366 |
Sorts.mg_domain (classes, arities) a S handle Sorts.DOMAIN _ => raise TUNIFY; |
2964 | 367 |
|
16885 | 368 |
fun meet (_, []) tye = tye |
369 |
| 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
|
370 |
if Sorts.sort_le classes (S', S) then tye |
17412 | 371 |
else Vartab.update_new |
17221 | 372 |
(xi, (S', gen_tyvar (Sorts.inter_sort classes (S', S)))) tye |
16885 | 373 |
| 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
|
374 |
if Sorts.sort_le classes (S', S) then tye |
2964 | 375 |
else raise TUNIFY |
16885 | 376 |
| meet (Type (a, Ts), S) tye = meets (Ts, mg_domain a S) tye |
377 |
and meets (T :: Ts, S :: Ss) tye = meets (Ts, Ss) (meet (devar tye T, S) tye) |
|
378 |
| meets _ tye = tye; |
|
2964 | 379 |
|
16885 | 380 |
fun unif (ty1, ty2) tye = |
381 |
(case (devar tye ty1, devar tye ty2) of |
|
2964 | 382 |
(T as TVar (v, S1), U as TVar (w, S2)) => |
15797 | 383 |
if eq_ix (v, w) then |
384 |
if S1 = S2 then tye else tvar_clash v S1 S2 |
|
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
|
385 |
else if Sorts.sort_le classes (S1, S2) then |
17412 | 386 |
Vartab.update_new (w, (S2, 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
|
387 |
else if Sorts.sort_le classes (S2, S1) then |
17412 | 388 |
Vartab.update_new (v, (S1, U)) tye |
2964 | 389 |
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
|
390 |
let val S = gen_tyvar (Sorts.inter_sort classes (S1, S2)) in |
17412 | 391 |
Vartab.update_new (v, (S1, S)) (Vartab.update_new (w, (S2, S)) tye) |
2964 | 392 |
end |
393 |
| (TVar (v, S), T) => |
|
394 |
if occurs v tye T then raise TUNIFY |
|
17412 | 395 |
else meet (T, S) (Vartab.update_new (v, (S, T)) tye) |
2964 | 396 |
| (T, TVar (v, S)) => |
397 |
if occurs v tye T then raise TUNIFY |
|
17412 | 398 |
else meet (T, S) (Vartab.update_new (v, (S, T)) tye) |
2964 | 399 |
| (Type (a, Ts), Type (b, Us)) => |
400 |
if a <> b then raise TUNIFY |
|
16885 | 401 |
else unifs (Ts, Us) tye |
402 |
| (T, U) => if T = U then tye else raise TUNIFY) |
|
403 |
and unifs (T :: Ts, U :: Us) tye = unifs (Ts, Us) (unif (T, U) tye) |
|
404 |
| unifs _ tye = tye; |
|
405 |
in (unif TU tyenv, ! tyvar_count) end; |
|
0 | 406 |
|
16946 | 407 |
(*purely structural unification*) |
408 |
fun raw_unify (ty1, ty2) tye = |
|
409 |
(case (devar tye ty1, devar tye ty2) of |
|
410 |
(T as TVar (v, S1), U as TVar (w, S2)) => |
|
411 |
if eq_ix (v, w) then |
|
412 |
if S1 = S2 then tye else tvar_clash v S1 S2 |
|
17412 | 413 |
else Vartab.update_new (w, (S2, T)) tye |
16946 | 414 |
| (TVar (v, S), T) => |
415 |
if occurs v tye T then raise TUNIFY |
|
17412 | 416 |
else Vartab.update_new (v, (S, T)) tye |
16946 | 417 |
| (T, TVar (v, S)) => |
418 |
if occurs v tye T then raise TUNIFY |
|
17412 | 419 |
else Vartab.update_new (v, (S, T)) tye |
16946 | 420 |
| (Type (a, Ts), Type (b, Us)) => |
421 |
if a <> b then raise TUNIFY |
|
422 |
else raw_unifys (Ts, Us) tye |
|
423 |
| (T, U) => if T = U then tye else raise TUNIFY) |
|
424 |
and raw_unifys (T :: Ts, U :: Us) tye = raw_unifys (Ts, Us) (raw_unify (T, U) tye) |
|
425 |
| raw_unifys _ tye = tye; |
|
426 |
||
17804 | 427 |
(*fast unification filter*) |
428 |
fun could_unify (Type (a, Ts), Type (b, Us)) = a = b andalso could_unifys (Ts, Us) |
|
429 |
| could_unify (TFree (a, _), TFree (b, _)) = a = b |
|
430 |
| could_unify (TVar _, _) = true |
|
431 |
| could_unify (_, TVar _) = true |
|
432 |
| could_unify _ = false |
|
433 |
and could_unifys (T :: Ts, U :: Us) = could_unify (T, U) andalso could_unifys (Ts, Us) |
|
434 |
| could_unifys _ = true; |
|
450 | 435 |
|
17804 | 436 |
|
437 |
(*equality with respect to a type environment*) |
|
16650 | 438 |
fun eq_type tye (T, T') = |
16885 | 439 |
(case (devar tye T, devar tye T') of |
16650 | 440 |
(Type (s, Ts), Type (s', Ts')) => |
441 |
s = s' andalso ListPair.all (eq_type tye) (Ts, Ts') |
|
442 |
| (U, U') => U = U'); |
|
443 |
||
450 | 444 |
|
0 | 445 |
|
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
|
446 |
(** 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
|
447 |
|
0d984ee030a1
major cleanup of tsig datastructures 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 |
(* 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
|
449 |
|
0d984ee030a1
major cleanup of tsig datastructures 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 |
local |
1435
aefcd255ed4a
Removed bug in type unification. Negative indexes are not used any longer.
nipkow
parents:
1392
diff
changeset
|
451 |
|
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
|
452 |
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
|
453 |
|
15531 | 454 |
fun for_classes _ NONE = "" |
455 |
| for_classes pp (SOME (c1, c2)) = |
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
456 |
" for classes " ^ Pretty.string_of_classrel pp [c1, c2]; |
3790 | 457 |
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
458 |
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
|
459 |
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
|
460 |
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
|
461 |
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
|
462 |
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
463 |
fun coregular pp C t (c, Ss) ars = |
3790 | 464 |
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
|
465 |
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
|
466 |
if Sorts.class_le C (c, c') andalso not (Sorts.sorts_le C (Ss, Ss')) then |
15531 | 467 |
SOME ((c, c'), (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
|
468 |
else if Sorts.class_le C (c', c) andalso not (Sorts.sorts_le C (Ss', Ss)) then |
15531 | 469 |
SOME ((c', c), (c', Ss')) |
470 |
else NONE; |
|
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
|
471 |
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
|
472 |
(case Library.get_first conflict ars of |
15531 | 473 |
SOME ((c1, c2), (c', Ss')) => err_conflict pp t (SOME (c1, c2)) (c, Ss) (c', Ss') |
474 |
| NONE => (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
|
475 |
end; |
3790 | 476 |
|
16340 | 477 |
fun insert pp C t (c, Ss) ars = |
17184 | 478 |
(case AList.lookup (op =) ars c of |
15531 | 479 |
NONE => coregular pp C t (c, Ss) ars |
480 |
| SOME 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
|
481 |
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
|
482 |
else if Sorts.sorts_le C (Ss', Ss) |
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
483 |
then coregular pp C t (c, Ss) (ars \ (c, Ss')) |
15531 | 484 |
else err_conflict pp t NONE (c, Ss) (c, Ss')); |
2587 | 485 |
|
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
|
486 |
fun complete C (c, Ss) = map (rpair Ss) (Graph.all_succs C [c]); |
3790 | 487 |
|
16340 | 488 |
fun insert_arities pp classes (t, ars) 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
|
489 |
let val ars' = |
17412 | 490 |
Symtab.lookup_multi arities t |
16340 | 491 |
|> fold_rev (fold_rev (insert pp classes t)) (map (complete classes) ars) |
17412 | 492 |
in Symtab.update (t, ars') arities end; |
3790 | 493 |
|
16444 | 494 |
fun insert_table pp classes = Symtab.fold (fn (t, ars) => |
495 |
insert_arities pp classes (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
|
496 |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
497 |
in |
2587 | 498 |
|
16370
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
499 |
fun add_arities pp decls tsig = tsig |> map_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
|
500 |
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
|
501 |
fun prep (t, Ss, S) = |
17756 | 502 |
(case Symtab.lookup (snd types) t of |
15531 | 503 |
SOME (LogicalType n, _) => |
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 |
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
|
505 |
(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
|
506 |
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
|
507 |
else error (bad_nargs t) |
15531 | 508 |
| SOME (decl, _) => err_decl t decl |
509 |
| NONE => error (undecl_type t)); |
|
4603 | 510 |
|
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
|
511 |
val ars = decls |> map ((fn (t, Ss, S) => (t, map (fn c => (c, Ss)) S)) o prep); |
17756 | 512 |
val arities' = fold (insert_arities pp (snd classes)) ars 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
|
513 |
in (classes, default, types, arities') end); |
12528 | 514 |
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
515 |
fun rebuild_arities pp classes arities = |
16444 | 516 |
Symtab.empty |
517 |
|> insert_table pp classes 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
|
518 |
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
519 |
fun merge_arities pp classes (arities1, arities2) = |
16444 | 520 |
Symtab.empty |
521 |
|> insert_table pp classes arities1 |
|
522 |
|> insert_table pp classes 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
|
523 |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
524 |
end; |
4603 | 525 |
|
526 |
||
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 |
(* 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
|
528 |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
529 |
local |
0 | 530 |
|
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
|
531 |
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
|
532 |
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
|
533 |
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
534 |
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
|
535 |
error (cat_lines (map (fn cs => |
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
536 |
"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
|
537 |
|
16370
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
538 |
fun add_class pp naming (c, cs) tsig = |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
539 |
tsig |> map_tsig (fn ((space, classes), default, types, arities) => |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
540 |
let |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
541 |
val c' = NameSpace.full naming c; |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
542 |
val cs' = map (cert_class tsig) cs |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
543 |
handle TYPE (msg, _, _) => error msg; |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
544 |
val space' = space |> NameSpace.declare naming c'; |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
545 |
val classes' = classes |> Graph.new_node (c', stamp ()) |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
546 |
handle Graph.DUP dup => err_dup_classes [dup]; |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
547 |
val classes'' = classes' |> fold Graph.add_edge_trans_acyclic (map (pair c') cs') |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
548 |
handle Graph.CYCLES css => err_cyclic_classes pp css; |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
549 |
in ((space', classes''), default, types, arities) end); |
2964 | 550 |
|
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
|
551 |
in |
0 | 552 |
|
16370
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
553 |
val add_classes = fold oo 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
|
554 |
|
16370
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
555 |
fun add_classrel pp ps tsig = |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
556 |
tsig |> map_tsig (fn ((space, classes), default, types, arities) => |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
557 |
let |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
558 |
val ps' = map (pairself (cert_class tsig)) ps |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
559 |
handle TYPE (msg, _, _) => error msg; |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
560 |
val classes' = classes |> fold Graph.add_edge_trans_acyclic ps' |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
561 |
handle Graph.CYCLES css => err_cyclic_classes pp css; |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
562 |
val default' = default |> Sorts.norm_sort classes'; |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
563 |
val arities' = arities |> rebuild_arities pp classes'; |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
564 |
in ((space, classes'), default', types, arities') end); |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
565 |
|
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
566 |
fun merge_classes pp ((space1, classes1), (space2, 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
|
567 |
let |
16370
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
568 |
val space = NameSpace.merge (space1, space2); |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
569 |
val classes = |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
570 |
Graph.merge_trans_acyclic (op =) (classes1, classes2) |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
571 |
handle Graph.DUPS cs => err_dup_classes cs |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
572 |
| Graph.CYCLES css => err_cyclic_classes pp css; |
16946 | 573 |
in (space, classes) 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
|
574 |
|
0d984ee030a1
major cleanup of tsig datastructures 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 |
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
|
576 |
|
16370
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
577 |
fun hide_classes fully cs = map_tsig (fn ((space, classes), default, types, arities) => |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
578 |
((fold (NameSpace.hide fully) cs space, classes), default, types, arities)); |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
579 |
|
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
|
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 |
(* 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
|
582 |
|
16370
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
583 |
fun set_defsort S tsig = tsig |> map_tsig (fn (classes, _, 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
|
584 |
(classes, cert_sort tsig S handle TYPE (msg, _, _) => error msg, types, arities)); |
0 | 585 |
|
586 |
||
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
|
587 |
(* 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
|
588 |
|
0d984ee030a1
major cleanup of tsig datastructures 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 |
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
|
590 |
|
0d984ee030a1
major cleanup of tsig datastructures 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 |
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
|
592 |
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
|
593 |
|
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
|
594 |
fun err_in_decls c decl decl' = |
14906 | 595 |
let val s = str_of_decl decl and s' = str_of_decl decl' in |
596 |
if s = s' then error ("Duplicate declaration of " ^ s ^ ": " ^ quote c) |
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
597 |
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
|
598 |
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
|
599 |
|
16370
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
600 |
fun new_decl naming (c, decl) (space, types) = |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
601 |
let |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
602 |
val c' = NameSpace.full naming c; |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
603 |
val space' = NameSpace.declare naming c' space; |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
604 |
val types' = |
17412 | 605 |
(case Symtab.lookup types c' of |
16370
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
606 |
SOME (decl', _) => err_in_decls c' decl decl' |
17412 | 607 |
| NONE => Symtab.update (c', (decl, stamp ())) types); |
16370
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
608 |
in (space', types') 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
|
609 |
|
17412 | 610 |
fun the_decl (_, types) = fst o the o Symtab.lookup types; |
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
|
611 |
|
16370
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
612 |
fun change_types f = map_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
|
613 |
(classes, default, f types, arities)); |
2964 | 614 |
|
14989 | 615 |
fun syntactic types (Type (c, Ts)) = |
17412 | 616 |
(case Symtab.lookup types c of SOME (Nonterminal, _) => true | _ => false) |
14989 | 617 |
orelse exists (syntactic types) Ts |
618 |
| syntactic _ _ = false; |
|
619 |
||
16370
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
620 |
fun add_abbrev naming (a, vs, rhs) tsig = tsig |> change_types (fn types => |
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
|
621 |
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
|
622 |
fun err msg = |
14906 | 623 |
error (msg ^ "\nThe error(s) above occurred in type abbreviation: " ^ quote a); |
16946 | 624 |
val rhs' = strip_sorts (no_tvars (cert_typ_syntax tsig rhs)) |
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
|
625 |
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
|
626 |
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
|
627 |
(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
|
628 |
[] => [] |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
629 |
| 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
|
630 |
(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
|
631 |
[] => [] |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
632 |
| extras => err ("Extra variables on rhs: " ^ commas_quote extras)); |
16370
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
633 |
types |> new_decl naming (a, Abbreviation (vs, rhs', syntactic (#2 types) rhs')) |
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
|
634 |
end); |
0 | 635 |
|
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
|
636 |
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
|
637 |
|
16370
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
638 |
fun add_types naming ps = change_types (fold (new_decl naming) (ps |> map (fn (c, n) => |
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
|
639 |
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
|
640 |
|
16370
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
641 |
val add_abbrevs = fold o add_abbrev; |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
642 |
|
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
643 |
fun add_nonterminals naming = change_types o fold (new_decl naming) o map (rpair Nonterminal); |
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
|
644 |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
645 |
fun merge_types (types1, types2) = |
17496 | 646 |
NameSpace.merge_tables (Library.eq_snd (op =)) (types1, types2) handle Symtab.DUPS (d :: _) => |
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
|
647 |
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
|
648 |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
649 |
end; |
0 | 650 |
|
16370
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
651 |
fun hide_types fully cs = map_tsig (fn (classes, default, (space, types), arities) => |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
652 |
(classes, default, (fold (NameSpace.hide fully) cs space, types), arities)); |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
653 |
|
3790 | 654 |
|
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
|
655 |
(* 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
|
656 |
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
657 |
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
|
658 |
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
|
659 |
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
|
660 |
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
|
661 |
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
|
662 |
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
|
663 |
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
664 |
val classes' = merge_classes pp (classes1, classes2); |
16370
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
665 |
val default' = Sorts.inter_sort (#2 classes') (default1, default2); |
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
|
666 |
val types' = merge_types (types1, types2); |
16370
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
667 |
val arities' = merge_arities pp (#2 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
|
668 |
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
|
669 |
|
0 | 670 |
end; |