author | wenzelm |
Tue, 14 Mar 2000 11:33:14 +0100 | |
changeset 8450 | dc44d6533f0f |
parent 8406 | a217b0cd304d |
child 8610 | f0f7600b2605 |
permissions | -rw-r--r-- |
256 | 1 |
(* Title: Pure/type.ML |
0 | 2 |
ID: $Id$ |
416 | 3 |
Author: Tobias Nipkow & Lawrence C Paulson |
0 | 4 |
|
2964 | 5 |
Type signatures, unification of types, interface to type inference. |
0 | 6 |
*) |
7 |
||
8 |
signature TYPE = |
|
2964 | 9 |
sig |
7641 | 10 |
(*TFrees and TVars*) |
621 | 11 |
val no_tvars: typ -> typ |
12 |
val varifyT: typ -> typ |
|
13 |
val unvarifyT: typ -> typ |
|
14 |
val varify: term * string list -> term |
|
3411
163f8f4a42d7
Removal of freeze_vars and thaw_vars. New freeze_thaw
paulson
parents:
3175
diff
changeset
|
15 |
val freeze_thaw : term -> term * (term -> term) |
2964 | 16 |
|
17 |
(*type signatures*) |
|
0 | 18 |
type type_sig |
200 | 19 |
val rep_tsig: type_sig -> |
256 | 20 |
{classes: class list, |
7069 | 21 |
classrel: Sorts.classrel, |
256 | 22 |
default: sort, |
7069 | 23 |
tycons: int Symtab.table, |
7641 | 24 |
log_types: string list, |
25 |
univ_witness: (typ * sort) option, |
|
7069 | 26 |
abbrs: (string list * typ) Symtab.table, |
27 |
arities: Sorts.arities} |
|
7641 | 28 |
val classes: type_sig -> class list |
0 | 29 |
val defaultS: type_sig -> sort |
2964 | 30 |
val logical_types: type_sig -> string list |
7641 | 31 |
val univ_witness: type_sig -> (typ * sort) option |
2964 | 32 |
val subsort: type_sig -> sort * sort -> bool |
33 |
val eq_sort: type_sig -> sort * sort -> bool |
|
34 |
val norm_sort: type_sig -> sort -> sort |
|
7641 | 35 |
val witness_sorts: type_sig -> sort list -> sort list -> (typ * sort) list |
2964 | 36 |
val rem_sorts: typ -> typ |
416 | 37 |
val tsig0: type_sig |
621 | 38 |
val ext_tsig_classes: type_sig -> (class * class list) list -> type_sig |
2964 | 39 |
val ext_tsig_classrel: type_sig -> (class * class) list -> type_sig |
422 | 40 |
val ext_tsig_defsort: type_sig -> sort -> type_sig |
582 | 41 |
val ext_tsig_types: type_sig -> (string * int) list -> type_sig |
621 | 42 |
val ext_tsig_abbrs: type_sig -> (string * string list * typ) list -> type_sig |
963 | 43 |
val ext_tsig_arities: type_sig -> (string * sort list * sort)list -> type_sig |
256 | 44 |
val merge_tsigs: type_sig * type_sig -> type_sig |
2964 | 45 |
val typ_errors: type_sig -> typ * string list -> string list |
256 | 46 |
val cert_typ: type_sig -> typ -> typ |
47 |
val norm_typ: type_sig -> typ -> typ |
|
7069 | 48 |
val norm_term: type_sig -> term -> term |
256 | 49 |
val inst_term_tvars: type_sig * (indexname * typ) list -> term -> term |
8406
a217b0cd304d
Type.unify and Type.typ_match now use Vartab instead of association lists.
berghofe
parents:
7641
diff
changeset
|
50 |
val inst_typ_tvars: type_sig * (indexname * typ) list -> typ -> typ |
2964 | 51 |
|
52 |
(*type matching*) |
|
53 |
exception TYPE_MATCH |
|
8406
a217b0cd304d
Type.unify and Type.typ_match now use Vartab instead of association lists.
berghofe
parents:
7641
diff
changeset
|
54 |
val typ_match: type_sig -> typ Vartab.table * (typ * typ) |
a217b0cd304d
Type.unify and Type.typ_match now use Vartab instead of association lists.
berghofe
parents:
7641
diff
changeset
|
55 |
-> typ Vartab.table |
2964 | 56 |
val typ_instance: type_sig * typ * typ -> bool |
3175 | 57 |
val of_sort: type_sig -> typ * sort -> bool |
2964 | 58 |
|
59 |
(*type unification*) |
|
60 |
exception TUNIFY |
|
8406
a217b0cd304d
Type.unify and Type.typ_match now use Vartab instead of association lists.
berghofe
parents:
7641
diff
changeset
|
61 |
val unify: type_sig -> int -> typ Vartab.table -> (typ * typ) |
a217b0cd304d
Type.unify and Type.typ_match now use Vartab instead of association lists.
berghofe
parents:
7641
diff
changeset
|
62 |
-> typ Vartab.table * int |
450 | 63 |
val raw_unify: typ * typ -> bool |
0 | 64 |
|
2964 | 65 |
(*type inference*) |
3790 | 66 |
val get_sort: type_sig -> (indexname -> sort option) -> (sort -> sort) |
67 |
-> (indexname * sort) list -> indexname -> sort |
|
2964 | 68 |
val constrain: term -> typ -> term |
4603 | 69 |
val param: string list -> string * sort -> typ |
2979 | 70 |
val infer_types: (term -> Pretty.T) -> (typ -> Pretty.T) |
71 |
-> type_sig -> (string -> typ option) -> (indexname -> typ option) |
|
3790 | 72 |
-> (indexname -> sort option) -> (string -> string) -> (typ -> typ) |
73 |
-> (sort -> sort) -> string list -> bool -> typ list -> term list |
|
2964 | 74 |
-> term list * (indexname * typ) list |
75 |
end; |
|
76 |
||
77 |
structure Type: TYPE = |
|
0 | 78 |
struct |
79 |
||
2964 | 80 |
|
7641 | 81 |
(*** TFrees and TVars ***) |
621 | 82 |
|
83 |
fun no_tvars T = |
|
84 |
if null (typ_tvars T) then T |
|
3790 | 85 |
else raise TYPE ("Illegal schematic type variable(s)", [T], []); |
621 | 86 |
|
7641 | 87 |
|
2964 | 88 |
(* varify, unvarify *) |
621 | 89 |
|
2964 | 90 |
val varifyT = map_type_tfree (fn (a, S) => TVar ((a, 0), S)); |
91 |
||
621 | 92 |
fun unvarifyT (Type (a, Ts)) = Type (a, map unvarifyT Ts) |
93 |
| unvarifyT (TVar ((a, 0), S)) = TFree (a, S) |
|
94 |
| unvarifyT T = T; |
|
95 |
||
96 |
fun varify (t, fixed) = |
|
97 |
let |
|
98 |
val fs = add_term_tfree_names (t, []) \\ fixed; |
|
99 |
val ixns = add_term_tvar_ixns (t, []); |
|
100 |
val fmap = fs ~~ variantlist (fs, map #1 ixns) |
|
2964 | 101 |
fun thaw (f as (a, S)) = |
102 |
(case assoc (fmap, a) of |
|
103 |
None => TFree f |
|
104 |
| Some b => TVar ((b, 0), S)); |
|
7641 | 105 |
in map_term_types (map_type_tfree thaw) t end; |
2964 | 106 |
|
107 |
||
7641 | 108 |
(* 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
|
109 |
|
7641 | 110 |
local |
111 |
||
112 |
fun new_name (ix, (pairs,used)) = |
|
3411
163f8f4a42d7
Removal of freeze_vars and thaw_vars. New freeze_thaw
paulson
parents:
3175
diff
changeset
|
113 |
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
|
114 |
in ((ix,v)::pairs, v::used) end; |
621 | 115 |
|
7641 | 116 |
fun freeze_one alist (ix,sort) = |
3790 | 117 |
TFree (the (assoc (alist, ix)), sort) |
4142 | 118 |
handle OPTION => |
3790 | 119 |
raise TYPE ("Failure during freezing of ?" ^ string_of_indexname ix, [], []); |
2964 | 120 |
|
7641 | 121 |
fun thaw_one alist (a,sort) = TVar (the (assoc (alist,a)), sort) |
4142 | 122 |
handle OPTION => TFree(a,sort); |
416 | 123 |
|
7641 | 124 |
(*this sort of code could replace unvarifyT (?) -- currently unused*) |
125 |
fun freeze_thaw_type T = |
|
126 |
let |
|
127 |
val used = add_typ_tfree_names (T, []) |
|
128 |
and tvars = map #1 (add_typ_tvars (T, [])); |
|
129 |
val (alist, _) = foldr new_name (tvars, ([], used)); |
|
130 |
in (map_type_tvar (freeze_one alist) T, map_type_tfree (thaw_one (map swap alist))) end; |
|
131 |
||
132 |
in |
|
416 | 133 |
|
3411
163f8f4a42d7
Removal of freeze_vars and thaw_vars. New freeze_thaw
paulson
parents:
3175
diff
changeset
|
134 |
fun freeze_thaw t = |
7641 | 135 |
let |
136 |
val used = it_term_types add_typ_tfree_names (t, []) |
|
137 |
and tvars = map #1 (it_term_types add_typ_tvars (t, [])); |
|
138 |
val (alist, _) = foldr new_name (tvars, ([], used)); |
|
139 |
in |
|
140 |
(case alist of |
|
141 |
[] => (t, fn x => x) (*nothing to do!*) |
|
142 |
| _ => (map_term_types (map_type_tvar (freeze_one alist)) t, |
|
143 |
map_term_types (map_type_tfree (thaw_one (map swap alist))))) |
|
144 |
end; |
|
145 |
||
146 |
end; |
|
147 |
||
256 | 148 |
|
149 |
||
416 | 150 |
(*** type signatures ***) |
256 | 151 |
|
2964 | 152 |
(* type type_sig *) |
153 |
||
256 | 154 |
(* |
7641 | 155 |
classes: list of all declared classes; |
156 |
classrel: (see Pure/sorts.ML) |
|
157 |
default: default sort attached to all unconstrained type vars; |
|
158 |
tycons: table of all declared types with the number of their arguments; |
|
159 |
log_types: list of logical type constructors sorted by number of arguments; |
|
160 |
univ_witness: type witnessing non-emptiness of least sort |
|
161 |
abbrs: table of type abbreviations; |
|
162 |
arities: (see Pure/sorts.ML) |
|
0 | 163 |
*) |
164 |
||
256 | 165 |
datatype type_sig = |
166 |
TySg of { |
|
167 |
classes: class list, |
|
7069 | 168 |
classrel: Sorts.classrel, |
256 | 169 |
default: sort, |
7069 | 170 |
tycons: int Symtab.table, |
7641 | 171 |
log_types: string list, |
172 |
univ_witness: (typ * sort) option, |
|
7069 | 173 |
abbrs: (string list * typ) Symtab.table, |
174 |
arities: Sorts.arities}; |
|
256 | 175 |
|
189 | 176 |
fun rep_tsig (TySg comps) = comps; |
0 | 177 |
|
7641 | 178 |
fun classes (TySg {classes = cs, ...}) = cs; |
256 | 179 |
fun defaultS (TySg {default, ...}) = default; |
7641 | 180 |
fun logical_types (TySg {log_types, ...}) = log_types; |
181 |
fun univ_witness (TySg {univ_witness, ...}) = univ_witness; |
|
2964 | 182 |
|
183 |
||
184 |
(* sorts *) |
|
185 |
||
186 |
fun subsort (TySg {classrel, ...}) = Sorts.sort_le classrel; |
|
187 |
fun eq_sort (TySg {classrel, ...}) = Sorts.sort_eq classrel; |
|
188 |
fun norm_sort (TySg {classrel, ...}) = Sorts.norm_sort classrel; |
|
189 |
||
7641 | 190 |
fun witness_sorts (tsig as TySg {classrel, arities, log_types, ...}) = |
191 |
Sorts.witness_sorts (classrel, arities, log_types); |
|
2964 | 192 |
|
193 |
fun rem_sorts (Type (a, tys)) = Type (a, map rem_sorts tys) |
|
194 |
| rem_sorts (TFree (x, _)) = TFree (x, []) |
|
195 |
| rem_sorts (TVar (xi, _)) = TVar (xi, []); |
|
196 |
||
256 | 197 |
|
582 | 198 |
(* error messages *) |
256 | 199 |
|
416 | 200 |
fun undcl_class c = "Undeclared class " ^ quote c; |
2233
f919bdd5f9b6
Removed or eta-expanded some declarations that are illegal under value
paulson
parents:
2182
diff
changeset
|
201 |
fun err_undcl_class s = error (undcl_class s); |
0 | 202 |
|
422 | 203 |
fun err_dup_classes cs = |
204 |
error ("Duplicate declaration of class(es) " ^ commas_quote cs); |
|
416 | 205 |
|
206 |
fun undcl_type c = "Undeclared type constructor " ^ quote c; |
|
256 | 207 |
|
582 | 208 |
fun err_neg_args c = |
209 |
error ("Negative number of arguments of type constructor " ^ quote c); |
|
210 |
||
416 | 211 |
fun err_dup_tycon c = |
212 |
error ("Duplicate declaration of type constructor " ^ quote c); |
|
213 |
||
621 | 214 |
fun dup_tyabbrs ts = |
215 |
"Duplicate declaration of type abbreviation(s) " ^ commas_quote ts; |
|
416 | 216 |
|
217 |
fun ty_confl c = "Conflicting type constructor and abbreviation " ^ quote c; |
|
0 | 218 |
|
219 |
||
2964 | 220 |
(* FIXME err_undcl_class! *) |
0 | 221 |
(* 'leq' checks the partial order on classes according to the |
7641 | 222 |
statements in classrel 'a' |
0 | 223 |
*) |
224 |
||
7069 | 225 |
fun less a (C, D) = case Symtab.lookup (a, C) of |
2182
29e56f003599
Removal of polymorphic equality via mem, subset, eq_set, etc
paulson
parents:
2148
diff
changeset
|
226 |
Some ss => D mem_string ss |
621 | 227 |
| None => err_undcl_class C; |
0 | 228 |
|
256 | 229 |
fun leq a (C, D) = C = D orelse less a (C, D); |
0 | 230 |
|
231 |
||
232 |
||
2964 | 233 |
(* FIXME *) |
200 | 234 |
(*Instantiation of type variables in types*) |
235 |
(*Pre: instantiations obey restrictions! *) |
|
236 |
fun inst_typ tye = |
|
949
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
237 |
let fun inst(var as (v, _)) = case assoc(tye, v) of |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
238 |
Some U => inst_typ tye U |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
239 |
| None => TVar(var) |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
240 |
in map_type_tvar inst end; |
0 | 241 |
|
242 |
||
3175 | 243 |
|
7641 | 244 |
fun of_sort (TySg {classrel, arities, ...}) = Sorts.of_sort (classrel, arities); |
3175 | 245 |
|
246 |
fun check_has_sort (tsig, T, S) = |
|
247 |
if of_sort tsig (T, S) then () |
|
3790 | 248 |
else raise TYPE ("Type not of sort " ^ Sorts.str_of_sort S, [T], []); |
0 | 249 |
|
250 |
||
251 |
(*Instantiation of type variables in types *) |
|
256 | 252 |
fun inst_typ_tvars(tsig, tye) = |
949
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
253 |
let fun inst(var as (v, S)) = case assoc(tye, v) of |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
254 |
Some U => (check_has_sort(tsig, U, S); U) |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
255 |
| None => TVar(var) |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
256 |
in map_type_tvar inst end; |
0 | 257 |
|
258 |
(*Instantiation of type variables in terms *) |
|
2617 | 259 |
fun inst_term_tvars (_,[]) t = t |
260 |
| inst_term_tvars arg t = map_term_types (inst_typ_tvars arg) t; |
|
200 | 261 |
|
262 |
||
7069 | 263 |
(* norm_typ, norm_term *) |
200 | 264 |
|
2991 | 265 |
(*expand abbreviations and normalize sorts*) |
266 |
fun norm_typ (tsig as TySg {abbrs, ...}) ty = |
|
256 | 267 |
let |
621 | 268 |
val idx = maxidx_of_typ ty + 1; |
269 |
||
2991 | 270 |
fun norm (Type (a, Ts)) = |
7069 | 271 |
(case Symtab.lookup (abbrs, a) of |
2991 | 272 |
Some (vs, U) => norm (inst_typ (map (rpair idx) vs ~~ Ts) (incr_tvar idx U)) |
273 |
| None => Type (a, map norm Ts)) |
|
274 |
| norm (TFree (x, S)) = TFree (x, norm_sort tsig S) |
|
275 |
| norm (TVar (xi, S)) = TVar (xi, norm_sort tsig S); |
|
7069 | 276 |
|
277 |
val ty' = norm ty; |
|
278 |
in if ty = ty' then ty else ty' end; (*dumb tuning to avoid copying*) |
|
256 | 279 |
|
7069 | 280 |
fun norm_term tsig t = |
281 |
let val t' = map_term_types (norm_typ tsig) t |
|
282 |
in if t = t' then t else t' end; (*dumb tuning to avoid copying*) |
|
256 | 283 |
|
284 |
||
285 |
||
286 |
(** build type signatures **) |
|
287 |
||
7641 | 288 |
fun make_tsig (classes, classrel, default, tycons, log_types, univ_witness, abbrs, arities) = |
289 |
TySg {classes = classes, classrel = classrel, default = default, tycons = tycons, |
|
290 |
log_types = log_types, univ_witness = univ_witness, abbrs = abbrs, arities = arities}; |
|
291 |
||
292 |
fun rebuild_tsig (TySg {classes, classrel, default, tycons, log_types = _, univ_witness = _, abbrs, arities}) = |
|
293 |
let |
|
294 |
fun log_class c = Sorts.class_le classrel (c, logicC); |
|
295 |
fun log_type (t, _) = exists (log_class o #1) (Symtab.lookup_multi (arities, t)); |
|
296 |
val ts = filter log_type (Symtab.dest tycons); |
|
416 | 297 |
|
7641 | 298 |
val log_types = map #1 (Library.sort (Library.int_ord o pairself #2) ts); |
299 |
val univ_witness = |
|
300 |
(case Sorts.witness_sorts (classrel, arities, log_types) [] [classes] of |
|
301 |
[w] => Some w | _ => None); |
|
302 |
in make_tsig (classes, classrel, default, tycons, log_types, univ_witness, abbrs, arities) end; |
|
256 | 303 |
|
7641 | 304 |
val tsig0 = |
305 |
make_tsig ([], Symtab.empty, [], Symtab.empty, [], None, Symtab.empty, Symtab.empty) |
|
306 |
|> rebuild_tsig; |
|
1215
a206f722bef9
added nonempty_sort (a somewhat braindead version!);
wenzelm
parents:
963
diff
changeset
|
307 |
|
a206f722bef9
added nonempty_sort (a somewhat braindead version!);
wenzelm
parents:
963
diff
changeset
|
308 |
|
416 | 309 |
(* typ_errors *) |
256 | 310 |
|
416 | 311 |
(*check validity of (not necessarily normal) type; accumulate error messages*) |
256 | 312 |
|
416 | 313 |
fun typ_errors tsig (typ, errors) = |
256 | 314 |
let |
7069 | 315 |
val {classes, tycons, abbrs, ...} = rep_tsig tsig; |
416 | 316 |
|
317 |
fun class_err (errs, c) = |
|
2182
29e56f003599
Removal of polymorphic equality via mem, subset, eq_set, etc
paulson
parents:
2148
diff
changeset
|
318 |
if c mem_string classes then errs |
29e56f003599
Removal of polymorphic equality via mem, subset, eq_set, etc
paulson
parents:
2148
diff
changeset
|
319 |
else undcl_class c ins_string errs; |
256 | 320 |
|
321 |
val sort_err = foldl class_err; |
|
0 | 322 |
|
7069 | 323 |
fun typ_errs (errs, Type (c, Us)) = |
256 | 324 |
let |
7069 | 325 |
val errs' = foldl typ_errs (errs, Us); |
256 | 326 |
fun nargs n = |
327 |
if n = length Us then errs' |
|
2182
29e56f003599
Removal of polymorphic equality via mem, subset, eq_set, etc
paulson
parents:
2148
diff
changeset
|
328 |
else ("Wrong number of arguments: " ^ quote c) ins_string errs'; |
256 | 329 |
in |
7069 | 330 |
(case Symtab.lookup (tycons, c) of |
256 | 331 |
Some n => nargs n |
332 |
| None => |
|
7069 | 333 |
(case Symtab.lookup (abbrs, c) of |
256 | 334 |
Some (vs, _) => nargs (length vs) |
2182
29e56f003599
Removal of polymorphic equality via mem, subset, eq_set, etc
paulson
parents:
2148
diff
changeset
|
335 |
| None => undcl_type c ins_string errs)) |
256 | 336 |
end |
7069 | 337 |
| typ_errs (errs, TFree (_, S)) = sort_err (errs, S) |
338 |
| typ_errs (errs, TVar ((x, i), S)) = |
|
416 | 339 |
if i < 0 then |
2182
29e56f003599
Removal of polymorphic equality via mem, subset, eq_set, etc
paulson
parents:
2148
diff
changeset
|
340 |
("Negative index for TVar " ^ quote x) ins_string sort_err (errs, S) |
416 | 341 |
else sort_err (errs, S); |
7069 | 342 |
in typ_errs (errors, typ) end; |
256 | 343 |
|
344 |
||
345 |
(* cert_typ *) |
|
346 |
||
2964 | 347 |
(*check and normalize typ wrt. tsig*) (*exception TYPE*) |
348 |
fun cert_typ tsig T = |
|
349 |
(case typ_errors tsig (T, []) of |
|
350 |
[] => norm_typ tsig T |
|
3790 | 351 |
| errs => raise TYPE (cat_lines errs, [T], [])); |
256 | 352 |
|
353 |
||
354 |
||
422 | 355 |
(** merge type signatures **) |
256 | 356 |
|
7641 | 357 |
(* merge classrel *) |
0 | 358 |
|
422 | 359 |
fun assoc_union (as1, []) = as1 |
360 |
| assoc_union (as1, (key, l2) :: as2) = |
|
2182
29e56f003599
Removal of polymorphic equality via mem, subset, eq_set, etc
paulson
parents:
2148
diff
changeset
|
361 |
(case assoc_string (as1, key) of |
7641 | 362 |
Some l1 => assoc_union (overwrite (as1, (key, l1 union_string l2)), as2) |
422 | 363 |
| None => assoc_union ((key, l2) :: as1, as2)); |
0 | 364 |
|
2964 | 365 |
fun merge_classrel (classrel1, classrel2) = |
7069 | 366 |
let |
367 |
val classrel = transitive_closure (assoc_union (Symtab.dest classrel1, Symtab.dest classrel2)) |
|
2182
29e56f003599
Removal of polymorphic equality via mem, subset, eq_set, etc
paulson
parents:
2148
diff
changeset
|
368 |
in |
2964 | 369 |
if exists (op mem_string) classrel then |
422 | 370 |
error ("Cyclic class structure!") (* FIXME improve msg, raise TERM *) |
7069 | 371 |
else Symtab.make classrel |
416 | 372 |
end; |
373 |
||
374 |
||
422 | 375 |
(* coregularity *) |
0 | 376 |
|
7641 | 377 |
local |
378 |
||
0 | 379 |
(* 'is_unique_decl' checks if there exists just one declaration t:(Ss)C *) |
380 |
||
963 | 381 |
fun is_unique_decl ars (t,(C,w)) = case assoc (ars, C) of |
0 | 382 |
Some(w1) => if w = w1 then () else |
256 | 383 |
error("There are two declarations\n" ^ |
2964 | 384 |
Sorts.str_of_arity(t, w, [C]) ^ " and\n" ^ |
385 |
Sorts.str_of_arity(t, w1, [C]) ^ "\n" ^ |
|
0 | 386 |
"with the same result class.") |
387 |
| None => (); |
|
388 |
||
963 | 389 |
(* 'coreg' checks if there are two declarations t:(Ss1)C1 and t:(Ss2)C2 |
0 | 390 |
such that C1 >= C2 then Ss1 >= Ss2 (elementwise) *) |
391 |
||
963 | 392 |
fun coreg_err(t, (C1,w1), (C2,w2)) = |
2964 | 393 |
error("Declarations " ^ Sorts.str_of_arity(t, w1, [C1]) ^ " and " |
394 |
^ Sorts.str_of_arity(t, w2, [C2]) ^ " are in conflict"); |
|
0 | 395 |
|
2964 | 396 |
fun coreg classrel (t, Cw1) = |
397 |
let |
|
398 |
fun check1(Cw1 as (C1,w1), Cw2 as (C2,w2)) = |
|
399 |
if leq classrel (C1,C2) then |
|
400 |
if Sorts.sorts_le classrel (w1,w2) then () |
|
401 |
else coreg_err(t, Cw1, Cw2) |
|
402 |
else () |
|
403 |
fun check(Cw2) = (check1(Cw1,Cw2); check1(Cw2,Cw1)) |
|
963 | 404 |
in seq check end; |
0 | 405 |
|
7641 | 406 |
in |
407 |
||
2964 | 408 |
fun add_arity classrel ars (tCw as (_,Cw)) = |
409 |
(is_unique_decl ars tCw; coreg classrel tCw ars; Cw ins ars); |
|
0 | 410 |
|
7641 | 411 |
end; |
0 | 412 |
|
413 |
||
963 | 414 |
(* 'merge_arities' builds the union of two 'arities' lists; |
422 | 415 |
it only checks the two restriction conditions and inserts afterwards |
416 |
all elements of the second list into the first one *) |
|
417 |
||
7641 | 418 |
local |
419 |
||
7069 | 420 |
fun merge_arities_aux classrel = |
2964 | 421 |
let fun test_ar t (ars1, sw) = add_arity classrel ars1 (t,sw); |
422 | 422 |
|
963 | 423 |
fun merge_c (arities1, (c as (t, ars2))) = case assoc (arities1, t) of |
424 |
Some(ars1) => |
|
425 |
let val ars = foldl (test_ar t) (ars1, ars2) |
|
426 |
in overwrite (arities1, (t,ars)) end |
|
427 |
| None => c::arities1 |
|
422 | 428 |
in foldl merge_c end; |
429 |
||
7641 | 430 |
in |
431 |
||
7069 | 432 |
fun merge_arities classrel (a1, a2) = |
433 |
Symtab.make (merge_arities_aux classrel (Symtab.dest a1, Symtab.dest a2)); |
|
422 | 434 |
|
7641 | 435 |
end; |
436 |
||
437 |
||
438 |
(* tycons *) |
|
439 |
||
440 |
fun varying_decls t = |
|
441 |
error ("Type constructor " ^ quote t ^ " has varying number of arguments"); |
|
442 |
||
7069 | 443 |
fun add_tycons (tycons, tn as (t,n)) = |
444 |
(case Symtab.lookup (tycons, t) of |
|
445 |
Some m => if m = n then tycons else varying_decls t |
|
446 |
| None => Symtab.update (tn, tycons)); |
|
447 |
||
7641 | 448 |
|
449 |
(* merge_abbrs *) |
|
450 |
||
7069 | 451 |
fun merge_abbrs abbrs = |
452 |
Symtab.merge (op =) abbrs handle Symtab.DUPS dups => raise TERM (dup_tyabbrs dups, []); |
|
422 | 453 |
|
454 |
||
7641 | 455 |
(* merge_tsigs *) |
422 | 456 |
|
7641 | 457 |
fun merge_tsigs |
458 |
(TySg {classes = classes1, default = default1, classrel = classrel1, tycons = tycons1, |
|
459 |
log_types = _, univ_witness = _, arities = arities1, abbrs = abbrs1}, |
|
460 |
TySg {classes = classes2, default = default2, classrel = classrel2, tycons = tycons2, |
|
461 |
log_types = _, univ_witness = _, arities = arities2, abbrs = abbrs2}) = |
|
462 |
let |
|
463 |
val classes' = classes1 union_string classes2; |
|
464 |
val classrel' = merge_classrel (classrel1, classrel2); |
|
465 |
val arities' = merge_arities classrel' (arities1, arities2); |
|
466 |
val tycons' = foldl add_tycons (tycons1, Symtab.dest tycons2); |
|
467 |
val default' = Sorts.norm_sort classrel' (default1 @ default2); |
|
468 |
val abbrs' = merge_abbrs (abbrs1, abbrs2); |
|
469 |
in |
|
470 |
make_tsig (classes', classrel', default', tycons', [], None, abbrs', arities') |
|
471 |
|> rebuild_tsig |
|
472 |
end; |
|
422 | 473 |
|
474 |
||
475 |
||
476 |
(*** extend type signatures ***) |
|
477 |
||
2964 | 478 |
(** add classes and classrel relations **) |
422 | 479 |
|
480 |
fun add_classes classes cs = |
|
2182
29e56f003599
Removal of polymorphic equality via mem, subset, eq_set, etc
paulson
parents:
2148
diff
changeset
|
481 |
(case cs inter_string classes of |
422 | 482 |
[] => cs @ classes |
483 |
| dups => err_dup_classes cs); |
|
484 |
||
485 |
||
2964 | 486 |
(*'add_classrel' adds a tuple consisting of a new class (the new class has |
422 | 487 |
already been inserted into the 'classes' list) and its superclasses (they |
2964 | 488 |
must be declared in 'classes' too) to the 'classrel' list of the given type |
422 | 489 |
signature; furthermore all inherited superclasses according to the |
490 |
superclasses brought with are inserted and there is a check that there are |
|
491 |
no cycles (i.e. C <= D <= C, with C <> D);*) |
|
492 |
||
2964 | 493 |
fun add_classrel classes (classrel, (s, ges)) = |
621 | 494 |
let |
2964 | 495 |
fun upd (classrel, s') = |
2182
29e56f003599
Removal of polymorphic equality via mem, subset, eq_set, etc
paulson
parents:
2148
diff
changeset
|
496 |
if s' mem_string classes then |
7069 | 497 |
let val ges' = the (Symtab.lookup (classrel, s)) |
498 |
in case Symtab.lookup (classrel, s') of |
|
2182
29e56f003599
Removal of polymorphic equality via mem, subset, eq_set, etc
paulson
parents:
2148
diff
changeset
|
499 |
Some sups => if s mem_string sups |
422 | 500 |
then error(" Cycle :" ^ s^" <= "^ s'^" <= "^ s ) |
7069 | 501 |
else Symtab.update ((s, sups union_string ges'), classrel) |
2964 | 502 |
| None => classrel |
621 | 503 |
end |
504 |
else err_undcl_class s' |
|
7069 | 505 |
in foldl upd (Symtab.update ((s, ges), classrel), ges) end; |
422 | 506 |
|
507 |
||
508 |
(* 'extend_classes' inserts all new classes into the corresponding |
|
2964 | 509 |
lists ('classes', 'classrel') if possible *) |
422 | 510 |
|
2964 | 511 |
fun extend_classes (classes, classrel, new_classes) = |
621 | 512 |
let |
513 |
val classes' = add_classes classes (map fst new_classes); |
|
2964 | 514 |
val classrel' = foldl (add_classrel classes') (classrel, new_classes); |
515 |
in (classes', classrel') end; |
|
422 | 516 |
|
517 |
||
621 | 518 |
(* ext_tsig_classes *) |
519 |
||
520 |
fun ext_tsig_classes tsig new_classes = |
|
521 |
let |
|
7641 | 522 |
val TySg {classes, classrel, default, tycons, log_types, univ_witness, abbrs, arities} = tsig; |
2964 | 523 |
val (classes',classrel') = extend_classes (classes,classrel,new_classes); |
7641 | 524 |
in make_tsig (classes', classrel', default, tycons, log_types, univ_witness, abbrs, arities) end; |
621 | 525 |
|
526 |
||
2964 | 527 |
(* ext_tsig_classrel *) |
422 | 528 |
|
2964 | 529 |
fun ext_tsig_classrel tsig pairs = |
422 | 530 |
let |
7641 | 531 |
val TySg {classes, classrel, default, tycons, log_types, univ_witness, abbrs, arities} = tsig; |
422 | 532 |
|
533 |
(* FIXME clean! *) |
|
2964 | 534 |
val classrel' = |
7069 | 535 |
merge_classrel (classrel, Symtab.make (map (fn (c1, c2) => (c1, [c2])) pairs)); |
422 | 536 |
in |
7641 | 537 |
make_tsig (classes, classrel', default, tycons, log_types, univ_witness, abbrs, arities) |
538 |
|> rebuild_tsig |
|
422 | 539 |
end; |
540 |
||
541 |
||
542 |
(* ext_tsig_defsort *) |
|
543 |
||
7641 | 544 |
fun ext_tsig_defsort |
545 |
(TySg {classes, classrel, default = _, tycons, log_types, univ_witness, abbrs, arities, ...}) default = |
|
546 |
make_tsig (classes, classrel, default, tycons, log_types, univ_witness, abbrs, arities); |
|
422 | 547 |
|
548 |
||
549 |
||
621 | 550 |
(** add types **) |
582 | 551 |
|
7641 | 552 |
fun ext_tsig_types (TySg {classes, classrel, default, tycons, log_types, univ_witness, abbrs, arities}) ts = |
582 | 553 |
let |
554 |
fun check_type (c, n) = |
|
555 |
if n < 0 then err_neg_args c |
|
7069 | 556 |
else if is_some (Symtab.lookup (tycons, c)) then err_dup_tycon c |
557 |
else if is_some (Symtab.lookup (abbrs, c)) then error (ty_confl c) |
|
582 | 558 |
else (); |
7641 | 559 |
val _ = seq check_type ts; |
560 |
val tycons' = Symtab.extend (tycons, ts); |
|
561 |
val arities' = Symtab.extend (arities, map (rpair [] o #1) ts); |
|
562 |
in make_tsig (classes, classrel, default, tycons', log_types, univ_witness, abbrs, arities') end; |
|
582 | 563 |
|
564 |
||
565 |
||
566 |
(** add type abbreviations **) |
|
567 |
||
568 |
fun abbr_errors tsig (a, (lhs_vs, rhs)) = |
|
569 |
let |
|
963 | 570 |
val TySg {tycons, abbrs, ...} = tsig; |
621 | 571 |
val rhs_vs = map (#1 o #1) (typ_tvars rhs); |
582 | 572 |
|
573 |
val dup_lhs_vars = |
|
574 |
(case duplicates lhs_vs of |
|
575 |
[] => [] |
|
621 | 576 |
| vs => ["Duplicate variables on lhs: " ^ commas_quote vs]); |
582 | 577 |
|
578 |
val extra_rhs_vars = |
|
579 |
(case gen_rems (op =) (rhs_vs, lhs_vs) of |
|
580 |
[] => [] |
|
621 | 581 |
| vs => ["Extra variables on rhs: " ^ commas_quote vs]); |
582 | 582 |
|
583 |
val tycon_confl = |
|
7069 | 584 |
if is_none (Symtab.lookup (tycons, a)) then [] |
582 | 585 |
else [ty_confl a]; |
586 |
||
587 |
val dup_abbr = |
|
7069 | 588 |
if is_none (Symtab.lookup (abbrs, a)) then [] |
582 | 589 |
else ["Duplicate declaration of abbreviation"]; |
590 |
in |
|
591 |
dup_lhs_vars @ extra_rhs_vars @ tycon_confl @ dup_abbr @ |
|
592 |
typ_errors tsig (rhs, []) |
|
593 |
end; |
|
594 |
||
621 | 595 |
fun prep_abbr tsig (a, vs, raw_rhs) = |
596 |
let |
|
4974 | 597 |
fun err msgs = (seq error_msg msgs; |
621 | 598 |
error ("The error(s) above occurred in type abbreviation " ^ quote a)); |
599 |
||
600 |
val rhs = rem_sorts (varifyT (no_tvars raw_rhs)) |
|
601 |
handle TYPE (msg, _, _) => err [msg]; |
|
602 |
val abbr = (a, (vs, rhs)); |
|
603 |
in |
|
582 | 604 |
(case abbr_errors tsig abbr of |
621 | 605 |
[] => abbr |
606 |
| msgs => err msgs) |
|
582 | 607 |
end; |
608 |
||
7641 | 609 |
fun add_abbr |
610 |
(tsig as TySg {classes, classrel, default, tycons, log_types, univ_witness, arities, abbrs}, abbr) = |
|
611 |
make_tsig (classes, classrel, default, tycons, log_types, univ_witness, |
|
612 |
Symtab.update (prep_abbr tsig abbr, abbrs), arities); |
|
621 | 613 |
|
614 |
fun ext_tsig_abbrs tsig raw_abbrs = foldl add_abbr (tsig, raw_abbrs); |
|
582 | 615 |
|
616 |
||
617 |
||
422 | 618 |
(** add arities **) |
619 |
||
0 | 620 |
(* 'coregular' checks |
963 | 621 |
- the two restrictions 'is_unique_decl' and 'coreg' |
256 | 622 |
- if the classes in the new type declarations are known in the |
0 | 623 |
given type signature |
624 |
- if one type constructor has always the same number of arguments; |
|
256 | 625 |
if one type declaration has passed all checks it is inserted into |
963 | 626 |
the 'arities' association list of the given type signatrure *) |
0 | 627 |
|
2964 | 628 |
fun coregular (classes, classrel, tycons) = |
2182
29e56f003599
Removal of polymorphic equality via mem, subset, eq_set, etc
paulson
parents:
2148
diff
changeset
|
629 |
let fun ex C = if C mem_string classes then () else err_undcl_class(C); |
0 | 630 |
|
7069 | 631 |
fun addar(arities, (t, (w, C))) = case Symtab.lookup (tycons, t) of |
0 | 632 |
Some(n) => if n <> length w then varying_decls(t) else |
963 | 633 |
((seq o seq) ex w; ex C; |
7069 | 634 |
let val ars = the (Symtab.lookup (arities, t)) |
2964 | 635 |
val ars' = add_arity classrel ars (t,(C,w)) |
7069 | 636 |
in Symtab.update ((t,ars'), arities) end) |
2233
f919bdd5f9b6
Removed or eta-expanded some declarations that are illegal under value
paulson
parents:
2182
diff
changeset
|
637 |
| None => error (undcl_type t); |
0 | 638 |
|
963 | 639 |
in addar end; |
0 | 640 |
|
641 |
||
963 | 642 |
(* 'close' extends the 'arities' association list after all new type |
0 | 643 |
declarations have been inserted successfully: |
644 |
for every declaration t:(Ss)C , for all classses D with C <= D: |
|
645 |
if there is no declaration t:(Ss')C' with C < C' and C' <= D |
|
963 | 646 |
then insert the declaration t:(Ss)D into 'arities' |
0 | 647 |
this means, if there exists a declaration t:(Ss)C and there is |
648 |
no declaration t:(Ss')D with C <=D then the declaration holds |
|
256 | 649 |
for all range classes more general than C *) |
650 |
||
2964 | 651 |
fun close classrel arities = |
7069 | 652 |
let fun check sl (l, (s, dom)) = case Symtab.lookup (classrel, s) of |
621 | 653 |
Some sups => |
256 | 654 |
let fun close_sup (l, sup) = |
2964 | 655 |
if exists (fn s'' => less classrel (s, s'') andalso |
656 |
leq classrel (s'', sup)) sl |
|
0 | 657 |
then l |
256 | 658 |
else (sup, dom)::l |
659 |
in foldl close_sup (l, sups) end |
|
0 | 660 |
| None => l; |
256 | 661 |
fun ext (s, l) = (s, foldl (check (map #1 l)) (l, l)); |
963 | 662 |
in map ext arities end; |
0 | 663 |
|
422 | 664 |
|
621 | 665 |
(* ext_tsig_arities *) |
256 | 666 |
|
2964 | 667 |
fun norm_domain classrel = |
668 |
let fun one_min (f, (doms, ran)) = (f, (map (Sorts.norm_sort classrel) doms, ran)) |
|
669 |
in map one_min end; |
|
670 |
||
621 | 671 |
fun ext_tsig_arities tsig sarities = |
416 | 672 |
let |
7641 | 673 |
val TySg {classes, classrel, default, tycons, log_types, univ_witness, arities, abbrs} = tsig; |
963 | 674 |
val arities1 = |
7641 | 675 |
flat (map (fn (t, ss, cs) => map (fn c => (t, (ss, c))) cs) sarities); |
676 |
val arities2 = |
|
677 |
foldl (coregular (classes, classrel, tycons)) (arities, norm_domain classrel arities1) |
|
7069 | 678 |
|> Symtab.dest |> close classrel |> Symtab.make; |
416 | 679 |
in |
7641 | 680 |
make_tsig (classes, classrel, default, tycons, log_types, univ_witness, abbrs, arities2) |
681 |
|> rebuild_tsig |
|
416 | 682 |
end; |
0 | 683 |
|
684 |
||
416 | 685 |
|
2964 | 686 |
(*** type unification and friends ***) |
0 | 687 |
|
2964 | 688 |
(** matching **) |
0 | 689 |
|
2964 | 690 |
exception TYPE_MATCH; |
0 | 691 |
|
2964 | 692 |
fun typ_match tsig = |
693 |
let |
|
694 |
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
|
695 |
(case Vartab.lookup (subs, v) of |
a217b0cd304d
Type.unify and Type.typ_match now use Vartab instead of association lists.
berghofe
parents:
7641
diff
changeset
|
696 |
None => (Vartab.update_new ((v, (check_has_sort (tsig, T, S); T)), subs) |
2964 | 697 |
handle TYPE _ => raise TYPE_MATCH) |
698 |
| Some U => if U = T then subs else raise TYPE_MATCH) |
|
699 |
| match (subs, (Type (a, Ts), Type (b, Us))) = |
|
700 |
if a <> b then raise TYPE_MATCH |
|
701 |
else foldl match (subs, Ts ~~ Us) |
|
702 |
| match (subs, (TFree x, TFree y)) = |
|
703 |
if x = y then subs else raise TYPE_MATCH |
|
704 |
| match _ = raise TYPE_MATCH; |
|
705 |
in match end; |
|
0 | 706 |
|
2964 | 707 |
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
|
708 |
(typ_match tsig (Vartab.empty, (U, T)); true) handle TYPE_MATCH => false; |
2964 | 709 |
|
0 | 710 |
|
2964 | 711 |
|
712 |
(** unification **) |
|
713 |
||
0 | 714 |
exception TUNIFY; |
715 |
||
716 |
||
2964 | 717 |
(* occurs check *) |
0 | 718 |
|
2964 | 719 |
fun occurs v tye = |
720 |
let |
|
721 |
fun occ (Type (_, Ts)) = exists occ Ts |
|
722 |
| occ (TFree _) = false |
|
723 |
| occ (TVar (w, _)) = |
|
724 |
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
|
725 |
(case Vartab.lookup (tye, w) of |
2964 | 726 |
None => false |
727 |
| Some U => occ U); |
|
0 | 728 |
in occ end; |
729 |
||
2964 | 730 |
|
731 |
(* chase variable assignments *) |
|
732 |
||
733 |
(*if devar returns a type var then it must be unassigned*) |
|
734 |
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
|
735 |
(case Vartab.lookup (tye, v) of |
2964 | 736 |
Some U => devar (U, tye) |
737 |
| None => T) |
|
256 | 738 |
| devar (T, tye) = T; |
0 | 739 |
|
1627 | 740 |
|
2964 | 741 |
(* add_env *) |
0 | 742 |
|
2964 | 743 |
(*avoids chains 'a |-> 'b |-> 'c ...*) |
8406
a217b0cd304d
Type.unify and Type.typ_match now use Vartab instead of association lists.
berghofe
parents:
7641
diff
changeset
|
744 |
fun add_env (vT as (v, T), tab) = Vartab.update_new (vT, Vartab.map |
a217b0cd304d
Type.unify and Type.typ_match now use Vartab instead of association lists.
berghofe
parents:
7641
diff
changeset
|
745 |
(fn (U as (TVar (w, S))) => if eq_ix (v, w) then T else U | U => U) tab); |
0 | 746 |
|
2964 | 747 |
(* unify *) |
748 |
||
749 |
fun unify (tsig as TySg {classrel, arities, ...}) maxidx tyenv TU = |
|
750 |
let |
|
751 |
val tyvar_count = ref maxidx; |
|
752 |
fun gen_tyvar S = TVar (("'a", inc tyvar_count), S); |
|
753 |
||
754 |
fun mg_domain a S = |
|
7641 | 755 |
Sorts.mg_domain (classrel, arities) a S handle Sorts.DOMAIN _ => raise TUNIFY; |
2964 | 756 |
|
757 |
fun meet ((_, []), tye) = tye |
|
758 |
| meet ((TVar (xi, S'), S), tye) = |
|
759 |
if Sorts.sort_le classrel (S', S) then tye |
|
760 |
else add_env ((xi, gen_tyvar (Sorts.inter_sort classrel (S', S))), tye) |
|
761 |
| meet ((TFree (_, S'), S), tye) = |
|
762 |
if Sorts.sort_le classrel (S', S) then tye |
|
763 |
else raise TUNIFY |
|
764 |
| meet ((Type (a, Ts), S), tye) = meets ((Ts, mg_domain a S), tye) |
|
765 |
and meets (([], []), tye) = tye |
|
766 |
| meets ((T :: Ts, S :: Ss), tye) = |
|
767 |
meets ((Ts, Ss), meet ((devar (T, tye), S), tye)) |
|
768 |
| meets _ = sys_error "meets"; |
|
769 |
||
770 |
fun unif ((ty1, ty2), tye) = |
|
771 |
(case (devar (ty1, tye), devar (ty2, tye)) of |
|
772 |
(T as TVar (v, S1), U as TVar (w, S2)) => |
|
773 |
if eq_ix (v, w) then tye |
|
774 |
else if Sorts.sort_le classrel (S1, S2) then add_env ((w, T), tye) |
|
775 |
else if Sorts.sort_le classrel (S2, S1) then add_env ((v, U), tye) |
|
776 |
else |
|
777 |
let val S = gen_tyvar (Sorts.inter_sort classrel (S1, S2)) in |
|
778 |
add_env ((v, S), add_env ((w, S), tye)) |
|
779 |
end |
|
780 |
| (TVar (v, S), T) => |
|
781 |
if occurs v tye T then raise TUNIFY |
|
782 |
else meet ((T, S), add_env ((v, T), tye)) |
|
783 |
| (T, TVar (v, S)) => |
|
784 |
if occurs v tye T then raise TUNIFY |
|
785 |
else meet ((T, S), add_env ((v, T), tye)) |
|
786 |
| (Type (a, Ts), Type (b, Us)) => |
|
787 |
if a <> b then raise TUNIFY |
|
788 |
else foldr unif (Ts ~~ Us, tye) |
|
789 |
| (T, U) => if T = U then tye else raise TUNIFY); |
|
790 |
in |
|
791 |
(unif (TU, tyenv), ! tyvar_count) |
|
792 |
end; |
|
0 | 793 |
|
794 |
||
2964 | 795 |
(* raw_unify *) |
0 | 796 |
|
2964 | 797 |
(*purely structural unification -- ignores sorts*) |
450 | 798 |
fun raw_unify (ty1, ty2) = |
8406
a217b0cd304d
Type.unify and Type.typ_match now use Vartab instead of association lists.
berghofe
parents:
7641
diff
changeset
|
799 |
(unify tsig0 0 Vartab.empty (rem_sorts ty1, rem_sorts ty2); true) |
450 | 800 |
handle TUNIFY => false; |
801 |
||
802 |
||
0 | 803 |
|
2964 | 804 |
(** type inference **) |
1435
aefcd255ed4a
Removed bug in type unification. Negative indexes are not used any longer.
nipkow
parents:
1392
diff
changeset
|
805 |
|
3790 | 806 |
(* sort constraints *) |
807 |
||
808 |
fun get_sort tsig def_sort map_sort raw_env = |
|
809 |
let |
|
810 |
fun eq ((xi, S), (xi', S')) = |
|
811 |
xi = xi' andalso eq_sort tsig (S, S'); |
|
812 |
||
813 |
val env = gen_distinct eq (map (apsnd map_sort) raw_env); |
|
814 |
val _ = |
|
815 |
(case gen_duplicates eq_fst env of |
|
816 |
[] => () |
|
817 |
| dups => error ("Inconsistent sort constraints for type variable(s) " ^ |
|
818 |
commas (map (quote o Syntax.string_of_vname' o fst) dups))); |
|
2587 | 819 |
|
3790 | 820 |
fun get xi = |
821 |
(case (assoc (env, xi), def_sort xi) of |
|
822 |
(None, None) => defaultS tsig |
|
823 |
| (None, Some S) => S |
|
824 |
| (Some S, None) => S |
|
825 |
| (Some S, Some S') => |
|
3804 | 826 |
if eq_sort tsig (S, S') then S' |
3790 | 827 |
else error ("Sort constraint inconsistent with default for type variable " ^ |
828 |
quote (Syntax.string_of_vname' xi))); |
|
829 |
in get end; |
|
830 |
||
831 |
||
832 |
(* type constraints *) |
|
2587 | 833 |
|
2964 | 834 |
fun constrain t T = |
835 |
if T = dummyT then t |
|
836 |
else Const ("_type_constraint_", T) $ t; |
|
0 | 837 |
|
838 |
||
4603 | 839 |
(* user parameters *) |
840 |
||
841 |
fun is_param (x, _) = size x > 0 andalso ord x = ord "?"; |
|
5345 | 842 |
fun param used (x, S) = TVar ((variant used ("?" ^ x), 0), S); |
4603 | 843 |
|
844 |
||
2964 | 845 |
(* decode_types *) |
0 | 846 |
|
3804 | 847 |
(*transform parse tree into raw term*) |
3790 | 848 |
fun decode_types tsig is_const def_type def_sort map_const map_type map_sort tm = |
2964 | 849 |
let |
850 |
fun get_type xi = if_none (def_type xi) dummyT; |
|
5080
ce093ff0880e
defaults for free variables hide consts of same name;
wenzelm
parents:
4974
diff
changeset
|
851 |
fun is_free x = is_some (def_type (x, ~1)); |
3790 | 852 |
val raw_env = Syntax.raw_term_sorts tm; |
853 |
val sort_of = get_sort tsig def_sort map_sort raw_env; |
|
2964 | 854 |
|
3804 | 855 |
val certT = cert_typ tsig o map_type; |
856 |
fun decodeT t = certT (Syntax.typ_of_term sort_of t); |
|
0 | 857 |
|
2964 | 858 |
fun decode (Const ("_constrain", _) $ t $ typ) = |
859 |
constrain (decode t) (decodeT typ) |
|
3804 | 860 |
| decode (Const ("_constrainAbs", _) $ (Abs (x, T, t)) $ typ) = |
2964 | 861 |
if T = dummyT then Abs (x, decodeT typ, decode t) |
3804 | 862 |
else constrain (Abs (x, certT T, decode t)) (decodeT typ --> dummyT) |
863 |
| decode (Abs (x, T, t)) = Abs (x, certT T, decode t) |
|
2964 | 864 |
| decode (t $ u) = decode t $ decode u |
3804 | 865 |
| decode (Free (x, T)) = |
3790 | 866 |
let val c = map_const x in |
5080
ce093ff0880e
defaults for free variables hide consts of same name;
wenzelm
parents:
4974
diff
changeset
|
867 |
if not (is_free x) andalso (is_const c orelse NameSpace.qualified c) then |
ce093ff0880e
defaults for free variables hide consts of same name;
wenzelm
parents:
4974
diff
changeset
|
868 |
Const (c, certT T) |
3790 | 869 |
else if T = dummyT then Free (x, get_type (x, ~1)) |
3804 | 870 |
else constrain (Free (x, certT T)) (get_type (x, ~1)) |
3790 | 871 |
end |
3804 | 872 |
| decode (Var (xi, T)) = |
2964 | 873 |
if T = dummyT then Var (xi, get_type xi) |
3804 | 874 |
else constrain (Var (xi, certT T)) (get_type xi) |
2964 | 875 |
| decode (t as Bound _) = t |
3804 | 876 |
| decode (Const (c, T)) = Const (map_const c, certT T); |
2964 | 877 |
in |
878 |
decode tm |
|
879 |
end; |
|
0 | 880 |
|
881 |
||
2964 | 882 |
(* infer_types *) |
949
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
883 |
|
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
884 |
(* |
2964 | 885 |
Given [T1,...,Tn] and [t1,...,tn], ensure that the type of ti |
886 |
unifies with Ti (for i=1,...,n). |
|
887 |
||
888 |
tsig: type signature |
|
3790 | 889 |
const_type: name mapping and signature lookup |
2964 | 890 |
def_type: partial map from indexnames to types (constrains Frees, Vars) |
891 |
def_sort: partial map from indexnames to sorts (constrains TFrees, TVars) |
|
892 |
used: list of already used type variables |
|
893 |
freeze: if true then generated parameters are turned into TFrees, else TVars |
|
949
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
894 |
*) |
0 | 895 |
|
3790 | 896 |
fun infer_types prt prT tsig const_type def_type def_sort |
897 |
map_const map_type map_sort used freeze pat_Ts raw_ts = |
|
565 | 898 |
let |
2964 | 899 |
val TySg {classrel, arities, ...} = tsig; |
900 |
val pat_Ts' = map (cert_typ tsig) pat_Ts; |
|
3790 | 901 |
val is_const = is_some o const_type; |
2964 | 902 |
val raw_ts' = |
3790 | 903 |
map (decode_types tsig is_const def_type def_sort map_const map_type map_sort) raw_ts; |
2964 | 904 |
val (ts, Ts, unifier) = |
2979 | 905 |
TypeInfer.infer_types prt prT const_type classrel arities used freeze |
4603 | 906 |
is_param raw_ts' pat_Ts'; |
7641 | 907 |
in (ts, unifier) end; |
0 | 908 |
|
3790 | 909 |
|
0 | 910 |
end; |