author | haftmann |
Tue, 06 Feb 2007 11:40:53 +0100 | |
changeset 22248 | 74ea64617c89 |
parent 20854 | f9cf9e62d11c |
child 22678 | 23963361278c |
permissions | -rw-r--r-- |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
1 |
(* Title: Pure/type_infer.ML |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
2 |
ID: $Id$ |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
3 |
Author: Stefan Berghofer and Markus Wenzel, TU Muenchen |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
4 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
5 |
Type inference. |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
6 |
*) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
7 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
8 |
signature TYPE_INFER = |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
9 |
sig |
8087 | 10 |
val anyT: sort -> typ |
11 |
val logicT: typ |
|
18339 | 12 |
val mixfixT: Syntax.mixfix -> typ |
8611 | 13 |
val polymorphicT: typ -> typ |
14828 | 14 |
val appl_error: Pretty.pp -> string -> term -> typ -> term -> typ -> string list |
14788
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
15 |
val constrain: term -> typ -> term |
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
16 |
val param: int -> string * sort -> typ |
18339 | 17 |
val paramify_dummies: typ -> int -> typ * int |
14788
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
18 |
val get_sort: Type.tsig -> (indexname -> sort option) -> (sort -> sort) |
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
19 |
-> (indexname * sort) list -> indexname -> sort |
14828 | 20 |
val infer_types: Pretty.pp |
14788
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
21 |
-> Type.tsig -> (string -> typ option) -> (indexname -> typ option) |
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
22 |
-> (indexname -> sort option) -> (string -> string) -> (typ -> typ) |
20161 | 23 |
-> (sort -> sort) -> Name.context -> bool -> typ list -> term list |
14788
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
24 |
-> term list * (indexname * typ) list |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
25 |
end; |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
26 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
27 |
structure TypeInfer: TYPE_INFER = |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
28 |
struct |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
29 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
30 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
31 |
(** term encodings **) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
32 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
33 |
(* |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
34 |
Flavours of term encodings: |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
35 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
36 |
parse trees (type term): |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
37 |
A very complicated structure produced by the syntax module's |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
38 |
read functions. Encodes types and sorts as terms; may contain |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
39 |
explicit constraints and partial typing information (where |
8087 | 40 |
dummies serve as wildcards). |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
41 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
42 |
Parse trees are INTERNAL! Users should never encounter them, |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
43 |
except in parse / print translation functions. |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
44 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
45 |
raw terms (type term): |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
46 |
Provide the user interface to type inferences. They may contain |
8087 | 47 |
partial type information (dummies are wildcards) or explicit |
48 |
type constraints (introduced via constrain: term -> typ -> |
|
49 |
term). |
|
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
50 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
51 |
The type inference function also lets users specify a certain |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
52 |
subset of TVars to be treated as non-rigid inference parameters. |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
53 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
54 |
preterms (type preterm): |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
55 |
The internal representation for type inference. |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
56 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
57 |
well-typed term (type term): |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
58 |
Fully typed lambda terms to be accepted by appropriate |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
59 |
certification functions. |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
60 |
*) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
61 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
62 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
63 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
64 |
(** pretyps and preterms **) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
65 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
66 |
(*links to parameters may get instantiated, anything else is rigid*) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
67 |
datatype pretyp = |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
68 |
PType of string * pretyp list | |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
69 |
PTFree of string * sort | |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
70 |
PTVar of indexname * sort | |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
71 |
Param of sort | |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
72 |
Link of pretyp ref; |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
73 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
74 |
datatype preterm = |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
75 |
PConst of string * pretyp | |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
76 |
PFree of string * pretyp | |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
77 |
PVar of indexname * pretyp | |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
78 |
PBound of int | |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
79 |
PAbs of string * pretyp * preterm | |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
80 |
PAppl of preterm * preterm | |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
81 |
Constraint of preterm * pretyp; |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
82 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
83 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
84 |
(* utils *) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
85 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
86 |
val mk_param = Link o ref o Param; |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
87 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
88 |
fun deref (T as Link (ref (Param _))) = T |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
89 |
| deref (Link (ref T)) = deref T |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
90 |
| deref T = T; |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
91 |
|
16195 | 92 |
fun fold_pretyps f (PConst (_, T)) x = f T x |
93 |
| fold_pretyps f (PFree (_, T)) x = f T x |
|
94 |
| fold_pretyps f (PVar (_, T)) x = f T x |
|
95 |
| fold_pretyps _ (PBound _) x = x |
|
96 |
| fold_pretyps f (PAbs (_, T, t)) x = fold_pretyps f t (f T x) |
|
97 |
| fold_pretyps f (PAppl (t, u)) x = fold_pretyps f u (fold_pretyps f t x) |
|
98 |
| fold_pretyps f (Constraint (t, T)) x = f T (fold_pretyps f t x); |
|
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
99 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
100 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
101 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
102 |
(** raw typs/terms to pretyps/preterms **) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
103 |
|
20668 | 104 |
(* pretyp_of *) |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
105 |
|
8087 | 106 |
fun anyT S = TFree ("'_dummy_", S); |
14854 | 107 |
val logicT = anyT []; |
8087 | 108 |
|
18339 | 109 |
fun mixfixT (Binder _) = (logicT --> logicT) --> logicT |
110 |
| mixfixT mx = replicate (Syntax.mixfix_args mx) logicT ---> logicT; |
|
111 |
||
112 |
||
8611 | 113 |
(*indicate polymorphic Vars*) |
114 |
fun polymorphicT T = Type ("_polymorphic_", [T]); |
|
115 |
||
20668 | 116 |
fun pretyp_of is_param typ params = |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
117 |
let |
20668 | 118 |
val params' = fold_atyps |
119 |
(fn TVar (xi as (x, _), S) => |
|
120 |
(fn ps => |
|
20735 | 121 |
if is_param xi andalso not (Vartab.defined ps xi) |
122 |
then Vartab.update (xi, mk_param S) ps else ps) |
|
20668 | 123 |
| _ => I) typ params; |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
124 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
125 |
fun pre_of (TVar (v as (xi, _))) = |
20735 | 126 |
(case Vartab.lookup params' xi of |
15531 | 127 |
NONE => PTVar v |
128 |
| SOME p => p) |
|
8087 | 129 |
| pre_of (TFree ("'_dummy_", S)) = mk_param S |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
130 |
| pre_of (TFree v) = PTFree v |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
131 |
| pre_of (T as Type (a, Ts)) = |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
132 |
if T = dummyT then mk_param [] |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
133 |
else PType (a, map pre_of Ts); |
20668 | 134 |
in (pre_of typ, params') end; |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
135 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
136 |
|
20668 | 137 |
(* preterm_of *) |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
138 |
|
20668 | 139 |
fun preterm_of const_type is_param tm (vparams, params) = |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
140 |
let |
20668 | 141 |
fun add_vparm xi ps = |
20735 | 142 |
if not (Vartab.defined ps xi) then |
143 |
Vartab.update (xi, mk_param []) ps |
|
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
144 |
else ps; |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
145 |
|
20668 | 146 |
val vparams' = fold_aterms |
147 |
(fn Var (_, Type ("_polymorphic_", _)) => I |
|
148 |
| Var (xi, _) => add_vparm xi |
|
149 |
| Free (x, _) => add_vparm (x, ~1) |
|
150 |
| _ => I) |
|
151 |
tm vparams; |
|
20735 | 152 |
fun var_param xi = the (Vartab.lookup vparams' xi); |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
153 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
154 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
155 |
val preT_of = pretyp_of is_param; |
20735 | 156 |
fun polyT_of T = fst (pretyp_of (K true) T Vartab.empty); |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
157 |
|
20668 | 158 |
fun constrain T t ps = |
159 |
if T = dummyT then (t, ps) |
|
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
160 |
else |
20668 | 161 |
let val (T', ps') = preT_of T ps |
162 |
in (Constraint (t, T'), ps') end; |
|
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
163 |
|
20668 | 164 |
fun pre_of (Const (c, T)) ps = |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
165 |
(case const_type c of |
20735 | 166 |
SOME U => constrain T (PConst (c, polyT_of U)) ps |
15531 | 167 |
| NONE => raise TYPE ("No such constant: " ^ quote c, [], [])) |
20735 | 168 |
| pre_of (Var (xi, Type ("_polymorphic_", [T]))) ps = (PVar (xi, polyT_of T), ps) |
20668 | 169 |
| pre_of (Var (xi, T)) ps = constrain T (PVar (xi, var_param xi)) ps |
170 |
| pre_of (Free (x, T)) ps = constrain T (PFree (x, var_param (x, ~1))) ps |
|
171 |
| pre_of (Const ("_type_constraint_", Type ("fun", [T, _])) $ t) ps = |
|
172 |
uncurry (constrain T) (pre_of t ps) |
|
173 |
| pre_of (Bound i) ps = (PBound i, ps) |
|
174 |
| pre_of (Abs (x, T, t)) ps = |
|
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
175 |
let |
20668 | 176 |
val (T', ps') = preT_of T ps; |
177 |
val (t', ps'') = pre_of t ps'; |
|
178 |
in (PAbs (x, T', t'), ps'') end |
|
179 |
| pre_of (t $ u) ps = |
|
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
180 |
let |
20668 | 181 |
val (t', ps') = pre_of t ps; |
182 |
val (u', ps'') = pre_of u ps'; |
|
183 |
in (PAppl (t', u'), ps'') end; |
|
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
184 |
|
20668 | 185 |
val (tm', params') = pre_of tm params; |
186 |
in (tm', (vparams', params')) end; |
|
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
187 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
188 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
189 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
190 |
(** pretyps/terms to typs/terms **) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
191 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
192 |
(* add_parms *) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
193 |
|
16195 | 194 |
fun add_parmsT (PType (_, Ts)) rs = fold add_parmsT Ts rs |
20854 | 195 |
| add_parmsT (Link (r as ref (Param _))) rs = insert (op =) r rs |
16195 | 196 |
| add_parmsT (Link (ref T)) rs = add_parmsT T rs |
197 |
| add_parmsT _ rs = rs; |
|
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
198 |
|
16195 | 199 |
val add_parms = fold_pretyps add_parmsT; |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
200 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
201 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
202 |
(* add_names *) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
203 |
|
20161 | 204 |
fun add_namesT (PType (_, Ts)) = fold add_namesT Ts |
205 |
| add_namesT (PTFree (x, _)) = Name.declare x |
|
206 |
| add_namesT (PTVar ((x, _), _)) = Name.declare x |
|
207 |
| add_namesT (Link (ref T)) = add_namesT T |
|
208 |
| add_namesT (Param _) = I; |
|
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
209 |
|
16195 | 210 |
val add_names = fold_pretyps add_namesT; |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
211 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
212 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
213 |
(* simple_typ/term_of *) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
214 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
215 |
(*deref links, fail on params*) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
216 |
fun simple_typ_of (PType (a, Ts)) = Type (a, map simple_typ_of Ts) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
217 |
| simple_typ_of (PTFree v) = TFree v |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
218 |
| simple_typ_of (PTVar v) = TVar v |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
219 |
| simple_typ_of (Link (ref T)) = simple_typ_of T |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
220 |
| simple_typ_of (Param _) = sys_error "simple_typ_of: illegal Param"; |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
221 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
222 |
(*convert types, drop constraints*) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
223 |
fun simple_term_of (PConst (c, T)) = Const (c, simple_typ_of T) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
224 |
| simple_term_of (PFree (x, T)) = Free (x, simple_typ_of T) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
225 |
| simple_term_of (PVar (xi, T)) = Var (xi, simple_typ_of T) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
226 |
| simple_term_of (PBound i) = Bound i |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
227 |
| simple_term_of (PAbs (x, T, t)) = Abs (x, simple_typ_of T, simple_term_of t) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
228 |
| simple_term_of (PAppl (t, u)) = simple_term_of t $ simple_term_of u |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
229 |
| simple_term_of (Constraint (t, _)) = simple_term_of t; |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
230 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
231 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
232 |
(* typs_terms_of *) (*DESTRUCTIVE*) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
233 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
234 |
fun typs_terms_of used mk_var prfx (Ts, ts) = |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
235 |
let |
4957
30c49821e61f
remove seq2, scan (use seq2, foldl_map from library.ML);
wenzelm
parents:
3784
diff
changeset
|
236 |
fun elim (r as ref (Param S), x) = r := mk_var (x, S) |
30c49821e61f
remove seq2, scan (use seq2, foldl_map from library.ML);
wenzelm
parents:
3784
diff
changeset
|
237 |
| elim _ = (); |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
238 |
|
16195 | 239 |
val used' = fold add_names ts (fold add_namesT Ts used); |
240 |
val parms = rev (fold add_parms ts (fold add_parmsT Ts [])); |
|
20161 | 241 |
val names = Name.invents used' (prfx ^ "'a") (length parms); |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
242 |
in |
16195 | 243 |
ListPair.app elim (parms, names); |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
244 |
(map simple_typ_of Ts, map simple_term_of ts) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
245 |
end; |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
246 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
247 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
248 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
249 |
(** order-sorted unification of types **) (*DESTRUCTIVE*) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
250 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
251 |
exception NO_UNIFIER of string; |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
252 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
253 |
|
19465 | 254 |
fun unify pp tsig = |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
255 |
let |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
256 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
257 |
(* adjust sorts of parameters *) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
258 |
|
19465 | 259 |
fun not_of_sort x S' S = |
14828 | 260 |
"Variable " ^ x ^ "::" ^ Pretty.string_of_sort pp S' ^ " not of sort " ^ |
261 |
Pretty.string_of_sort pp S; |
|
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
262 |
|
4957
30c49821e61f
remove seq2, scan (use seq2, foldl_map from library.ML);
wenzelm
parents:
3784
diff
changeset
|
263 |
fun meet (_, []) = () |
30c49821e61f
remove seq2, scan (use seq2, foldl_map from library.ML);
wenzelm
parents:
3784
diff
changeset
|
264 |
| meet (Link (r as (ref (Param S'))), S) = |
19465 | 265 |
if Type.subsort tsig (S', S) then () |
266 |
else r := mk_param (Type.inter_sort tsig (S', S)) |
|
4957
30c49821e61f
remove seq2, scan (use seq2, foldl_map from library.ML);
wenzelm
parents:
3784
diff
changeset
|
267 |
| meet (Link (ref T), S) = meet (T, S) |
30c49821e61f
remove seq2, scan (use seq2, foldl_map from library.ML);
wenzelm
parents:
3784
diff
changeset
|
268 |
| meet (PType (a, Ts), S) = |
19465 | 269 |
ListPair.app meet (Ts, Type.arity_sorts pp tsig a S |
270 |
handle ERROR msg => raise NO_UNIFIER msg) |
|
4957
30c49821e61f
remove seq2, scan (use seq2, foldl_map from library.ML);
wenzelm
parents:
3784
diff
changeset
|
271 |
| meet (PTFree (x, S'), S) = |
19465 | 272 |
if Type.subsort tsig (S', S) then () |
273 |
else raise NO_UNIFIER (not_of_sort x S' S) |
|
4957
30c49821e61f
remove seq2, scan (use seq2, foldl_map from library.ML);
wenzelm
parents:
3784
diff
changeset
|
274 |
| meet (PTVar (xi, S'), S) = |
19465 | 275 |
if Type.subsort tsig (S', S) then () |
276 |
else raise NO_UNIFIER (not_of_sort (Syntax.string_of_vname xi) S' S) |
|
4957
30c49821e61f
remove seq2, scan (use seq2, foldl_map from library.ML);
wenzelm
parents:
3784
diff
changeset
|
277 |
| meet (Param _, _) = sys_error "meet"; |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
278 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
279 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
280 |
(* occurs check and assigment *) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
281 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
282 |
fun occurs_check r (Link (r' as ref T)) = |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
283 |
if r = r' then raise NO_UNIFIER "Occurs check!" |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
284 |
else occurs_check r T |
15570 | 285 |
| occurs_check r (PType (_, Ts)) = List.app (occurs_check r) Ts |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
286 |
| occurs_check _ _ = (); |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
287 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
288 |
fun assign r T S = |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
289 |
(case deref T of |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
290 |
T' as Link (r' as ref (Param _)) => |
8087 | 291 |
if r = r' then () else (meet (T', S); r := T') |
292 |
| T' => (occurs_check r T'; meet (T', S); r := T')); |
|
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
293 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
294 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
295 |
(* unification *) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
296 |
|
4957
30c49821e61f
remove seq2, scan (use seq2, foldl_map from library.ML);
wenzelm
parents:
3784
diff
changeset
|
297 |
fun unif (Link (r as ref (Param S)), T) = assign r T S |
30c49821e61f
remove seq2, scan (use seq2, foldl_map from library.ML);
wenzelm
parents:
3784
diff
changeset
|
298 |
| unif (T, Link (r as ref (Param S))) = assign r T S |
30c49821e61f
remove seq2, scan (use seq2, foldl_map from library.ML);
wenzelm
parents:
3784
diff
changeset
|
299 |
| unif (Link (ref T), U) = unif (T, U) |
30c49821e61f
remove seq2, scan (use seq2, foldl_map from library.ML);
wenzelm
parents:
3784
diff
changeset
|
300 |
| unif (T, Link (ref U)) = unif (T, U) |
30c49821e61f
remove seq2, scan (use seq2, foldl_map from library.ML);
wenzelm
parents:
3784
diff
changeset
|
301 |
| unif (PType (a, Ts), PType (b, Us)) = |
2979 | 302 |
if a <> b then |
14828 | 303 |
raise NO_UNIFIER ("Clash of types " ^ quote a ^ " and " ^ quote b) |
16195 | 304 |
else ListPair.app unif (Ts, Us) |
4957
30c49821e61f
remove seq2, scan (use seq2, foldl_map from library.ML);
wenzelm
parents:
3784
diff
changeset
|
305 |
| unif (T, U) = if T = U then () else raise NO_UNIFIER ""; |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
306 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
307 |
in unif end; |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
308 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
309 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
310 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
311 |
(** type inference **) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
312 |
|
14828 | 313 |
fun appl_error pp why t T u U = |
8087 | 314 |
["Type error in application: " ^ why, |
315 |
"", |
|
316 |
Pretty.string_of (Pretty.block |
|
14828 | 317 |
[Pretty.str "Operator:", Pretty.brk 2, Pretty.term pp t, |
318 |
Pretty.str " ::", Pretty.brk 1, Pretty.typ pp T]), |
|
8087 | 319 |
Pretty.string_of (Pretty.block |
14828 | 320 |
[Pretty.str "Operand:", Pretty.brk 3, Pretty.term pp u, |
321 |
Pretty.str " ::", Pretty.brk 1, Pretty.typ pp U]), |
|
8087 | 322 |
""]; |
323 |
||
5635 | 324 |
|
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
325 |
(* infer *) (*DESTRUCTIVE*) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
326 |
|
19465 | 327 |
fun infer pp tsig = |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
328 |
let |
2979 | 329 |
(* errors *) |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
330 |
|
2979 | 331 |
fun unif_failed msg = |
14828 | 332 |
"Type unification failed" ^ (if msg = "" then "" else ": " ^ msg) ^ "\n"; |
2979 | 333 |
|
334 |
fun prep_output bs ts Ts = |
|
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
335 |
let |
20161 | 336 |
val (Ts_bTs', ts') = typs_terms_of Name.context PTFree "??" (Ts @ map snd bs, ts); |
19012 | 337 |
val (Ts', Ts'') = chop (length Ts) Ts_bTs'; |
13629 | 338 |
val xs = map Free (map fst bs ~~ Ts''); |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
339 |
val ts'' = map (fn t => subst_bounds (xs, t)) ts'; |
2979 | 340 |
in (ts'', Ts') end; |
341 |
||
342 |
fun err_loose i = |
|
3784 | 343 |
raise TYPE ("Loose bound variable: B." ^ string_of_int i, [], []); |
2979 | 344 |
|
3510 | 345 |
fun err_appl msg bs t T u U = |
2979 | 346 |
let |
3510 | 347 |
val ([t', u'], [T', U']) = prep_output bs [t, u] [T, U]; |
348 |
val why = |
|
349 |
(case T' of |
|
14828 | 350 |
Type ("fun", _) => "Incompatible operand type" |
351 |
| _ => "Operator not of function type"); |
|
352 |
val text = unif_failed msg ^ cat_lines (appl_error pp why t' T' u' U'); |
|
3784 | 353 |
in raise TYPE (text, [T', U'], [t', u']) end; |
2979 | 354 |
|
355 |
fun err_constraint msg bs t T U = |
|
356 |
let |
|
357 |
val ([t'], [T', U']) = prep_output bs [t] [T, U]; |
|
358 |
val text = cat_lines |
|
359 |
[unif_failed msg, |
|
5635 | 360 |
"Cannot meet type constraint:", "", |
14828 | 361 |
Pretty.string_of (Pretty.block |
362 |
[Pretty.str "Term:", Pretty.brk 2, Pretty.term pp t', |
|
363 |
Pretty.str " ::", Pretty.brk 1, Pretty.typ pp T']), |
|
364 |
Pretty.string_of (Pretty.block |
|
365 |
[Pretty.str "Type:", Pretty.brk 2, Pretty.typ pp U']), ""]; |
|
3784 | 366 |
in raise TYPE (text, [T', U'], [t']) end; |
2979 | 367 |
|
368 |
||
369 |
(* main *) |
|
370 |
||
19465 | 371 |
val unif = unify pp tsig; |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
372 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
373 |
fun inf _ (PConst (_, T)) = T |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
374 |
| inf _ (PFree (_, T)) = T |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
375 |
| inf _ (PVar (_, T)) = T |
15570 | 376 |
| inf bs (PBound i) = snd (List.nth (bs, i) handle Subscript => err_loose i) |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
377 |
| inf bs (PAbs (x, T, t)) = PType ("fun", [T, inf ((x, T) :: bs) t]) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
378 |
| inf bs (PAppl (t, u)) = |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
379 |
let |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
380 |
val T = inf bs t; |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
381 |
val U = inf bs u; |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
382 |
val V = mk_param []; |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
383 |
val U_to_V = PType ("fun", [U, V]); |
4957
30c49821e61f
remove seq2, scan (use seq2, foldl_map from library.ML);
wenzelm
parents:
3784
diff
changeset
|
384 |
val _ = unif (U_to_V, T) handle NO_UNIFIER msg => err_appl msg bs t T u U; |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
385 |
in V end |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
386 |
| inf bs (Constraint (t, U)) = |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
387 |
let val T = inf bs t in |
4957
30c49821e61f
remove seq2, scan (use seq2, foldl_map from library.ML);
wenzelm
parents:
3784
diff
changeset
|
388 |
unif (T, U) handle NO_UNIFIER msg => err_constraint msg bs t T U; |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
389 |
T |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
390 |
end; |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
391 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
392 |
in inf [] end; |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
393 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
394 |
|
14788
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
395 |
(* basic_infer_types *) |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
396 |
|
19465 | 397 |
fun basic_infer_types pp tsig const_type used freeze is_param ts Ts = |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
398 |
let |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
399 |
(*convert to preterms/typs*) |
20735 | 400 |
val (Ts', Tps) = fold_map (pretyp_of (K true)) Ts Vartab.empty; |
401 |
val (ts', (vps, ps)) = fold_map (preterm_of const_type is_param) ts (Vartab.empty, Tps); |
|
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
402 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
403 |
(*run type inference*) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
404 |
val tTs' = ListPair.map Constraint (ts', Ts'); |
19465 | 405 |
val _ = List.app (fn t => (infer pp tsig t; ())) tTs'; |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
406 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
407 |
(*collect result unifier*) |
15531 | 408 |
fun ch_var (xi, Link (r as ref (Param S))) = (r := PTVar (xi, S); NONE) |
409 |
| ch_var xi_T = SOME xi_T; |
|
20735 | 410 |
val env = map_filter ch_var (Vartab.dest Tps); |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
411 |
|
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
412 |
(*convert back to terms/typs*) |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
413 |
val mk_var = |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
414 |
if freeze then PTFree |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
415 |
else (fn (x, S) => PTVar ((x, 0), S)); |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
416 |
val (final_Ts, final_ts) = typs_terms_of used mk_var "" (Ts', ts'); |
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
417 |
val final_env = map (apsnd simple_typ_of) env; |
14788
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
418 |
in (final_ts, final_Ts, final_env) end; |
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
419 |
|
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
420 |
|
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
421 |
|
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
422 |
(** type inference **) |
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
423 |
|
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
424 |
(* user constraints *) |
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
425 |
|
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
426 |
fun constrain t T = |
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
427 |
if T = dummyT then t |
19577 | 428 |
else Const ("_type_constraint_", T --> T) $ t; |
14788
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
429 |
|
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
430 |
|
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
431 |
(* user parameters *) |
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
432 |
|
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
433 |
fun is_param (x, _) = size x > 0 andalso ord x = ord "?"; |
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
434 |
fun param i (x, S) = TVar (("?" ^ x, i), S); |
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
435 |
|
18339 | 436 |
val paramify_dummies = |
437 |
let |
|
438 |
fun dummy S maxidx = (param (maxidx + 1) ("'dummy", S), maxidx + 1); |
|
439 |
||
440 |
fun paramify (TFree ("'_dummy_", S)) maxidx = dummy S maxidx |
|
441 |
| paramify (Type ("dummy", _)) maxidx = dummy [] maxidx |
|
442 |
| paramify (Type (a, Ts)) maxidx = |
|
443 |
let val (Ts', maxidx') = fold_map paramify Ts maxidx |
|
444 |
in (Type (a, Ts'), maxidx') end |
|
445 |
| paramify T maxidx = (T, maxidx); |
|
446 |
in paramify end; |
|
14788
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
447 |
|
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
448 |
|
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
449 |
(* decode sort constraints *) |
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
450 |
|
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
451 |
fun get_sort tsig def_sort map_sort raw_env = |
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
452 |
let |
20668 | 453 |
fun eq ((xi, S), (xi', S')) = |
454 |
Term.eq_ix (xi, xi') andalso Type.eq_sort tsig (S, S'); |
|
14788
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
455 |
|
19046
bc5c6c9b114e
removed distinct, renamed gen_distinct to distinct;
wenzelm
parents:
19012
diff
changeset
|
456 |
val env = distinct eq (map (apsnd map_sort) raw_env); |
18964 | 457 |
val _ = (case duplicates (eq_fst (op =)) env of [] => () |
14828 | 458 |
| dups => error ("Inconsistent sort constraints for type variable(s) " |
459 |
^ commas_quote (map (Syntax.string_of_vname' o fst) dups))); |
|
14788
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
460 |
|
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
461 |
fun get xi = |
17271 | 462 |
(case (AList.lookup (op =) env xi, def_sort xi) of |
15531 | 463 |
(NONE, NONE) => Type.defaultS tsig |
464 |
| (NONE, SOME S) => S |
|
465 |
| (SOME S, NONE) => S |
|
466 |
| (SOME S, SOME S') => |
|
14788
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
467 |
if Type.eq_sort tsig (S, S') then S' |
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
468 |
else error ("Sort constraint inconsistent with default for type variable " ^ |
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
469 |
quote (Syntax.string_of_vname' xi))); |
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
470 |
in get end; |
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
471 |
|
4957
30c49821e61f
remove seq2, scan (use seq2, foldl_map from library.ML);
wenzelm
parents:
3784
diff
changeset
|
472 |
|
14788
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
473 |
(* decode_types -- transform parse tree into raw term *) |
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
474 |
|
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
475 |
fun decode_types tsig is_const def_type def_sort map_const map_type map_sort tm = |
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
476 |
let |
18939 | 477 |
fun get_type xi = the_default dummyT (def_type xi); |
16195 | 478 |
fun is_free x = is_some (def_type (x, ~1)); |
14788
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
479 |
val raw_env = Syntax.raw_term_sorts tm; |
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
480 |
val sort_of = get_sort tsig def_sort map_sort raw_env; |
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
481 |
|
14993 | 482 |
val certT = Type.cert_typ tsig o map_type; |
14788
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
483 |
fun decodeT t = certT (Syntax.typ_of_term sort_of map_sort t); |
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
484 |
|
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
485 |
fun decode (Const ("_constrain", _) $ t $ typ) = |
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
486 |
constrain (decode t) (decodeT typ) |
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
487 |
| decode (Const ("_constrainAbs", _) $ (Abs (x, T, t)) $ typ) = |
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
488 |
if T = dummyT then Abs (x, decodeT typ, decode t) |
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
489 |
else constrain (Abs (x, certT T, decode t)) (decodeT typ --> dummyT) |
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
490 |
| decode (Abs (x, T, t)) = Abs (x, certT T, decode t) |
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
491 |
| decode (t $ u) = decode t $ decode u |
18996 | 492 |
| decode (Const (x, T)) = |
493 |
let val c = (case try (unprefix Syntax.constN) x of SOME c => c | NONE => map_const x) |
|
494 |
in Const (c, certT T) end |
|
14788
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
495 |
| decode (Free (x, T)) = |
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
496 |
let val c = map_const x in |
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
497 |
if not (is_free x) andalso (is_const c orelse NameSpace.is_qualified c) then |
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
498 |
Const (c, certT T) |
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
499 |
else if T = dummyT then Free (x, get_type (x, ~1)) |
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
500 |
else constrain (Free (x, certT T)) (get_type (x, ~1)) |
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
501 |
end |
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
502 |
| decode (Var (xi, T)) = |
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
503 |
if T = dummyT then Var (xi, get_type xi) |
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
504 |
else constrain (Var (xi, certT T)) (get_type xi) |
18996 | 505 |
| decode (t as Bound _) = t; |
14788
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
506 |
in decode tm end; |
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
507 |
|
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
508 |
|
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
509 |
(* infer_types *) |
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
510 |
|
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
511 |
(*Given [T1,...,Tn] and [t1,...,tn], ensure that the type of ti |
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
512 |
unifies with Ti (for i=1,...,n). |
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
513 |
|
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
514 |
tsig: type signature |
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
515 |
const_type: name mapping and signature lookup |
16195 | 516 |
def_type: partial map from indexnames to types (constrains Frees and Vars) |
517 |
def_sort: partial map from indexnames to sorts (constrains TFrees and TVars) |
|
20161 | 518 |
used: context of already used type variables |
14788
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
519 |
freeze: if true then generated parameters are turned into TFrees, else TVars*) |
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
520 |
|
14828 | 521 |
fun infer_types pp tsig const_type def_type def_sort |
14788
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
522 |
map_const map_type map_sort used freeze pat_Ts raw_ts = |
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
523 |
let |
14993 | 524 |
val pat_Ts' = map (Type.cert_typ tsig) pat_Ts; |
16195 | 525 |
val is_const = is_some o const_type; |
14788
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
526 |
val raw_ts' = |
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
527 |
map (decode_types tsig is_const def_type def_sort map_const map_type map_sort) raw_ts; |
19465 | 528 |
val (ts, Ts, unifier) = |
529 |
basic_infer_types pp tsig const_type used freeze is_param raw_ts' pat_Ts'; |
|
14788
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
530 |
in (ts, unifier) end; |
9776f0c747c8
incorporate type inference interface from type.ML;
wenzelm
parents:
14695
diff
changeset
|
531 |
|
2957
d35fca99b3be
Type inference (isolated from type.ML, completely reimplemented).
wenzelm
parents:
diff
changeset
|
532 |
end; |