1 (* HCoop
Domtool (http
://hcoop
.sourceforge
.net
/)
2 * Copyright (c
) 2006, Adam Chlipala
4 * This program is free software
; you can redistribute it
and/or
5 * modify it under the terms
of the GNU General Public License
6 * as published by the Free Software Foundation
; either version
2
7 * of the License
, or (at your option
) any later version
.
9 * This program is distributed
in the hope that it will be useful
,
10 * but WITHOUT ANY WARRANTY
; without even the implied warranty
of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
. See the
12 * GNU General Public License for more details
.
14 * You should have received a copy
of the GNU General Public License
15 * along
with this program
; if not
, write to the Free Software
16 * Foundation
, Inc
., 51 Franklin Street
, Fifth Floor
, Boston
, MA
02110-1301, USA
.
19 (* Domtool configuration language
type checking
*)
21 structure Tycheck
:> TYCHECK
= struct
25 structure SM
= StringMap
30 fun resetUnif () = unifCount
:= 0
37 str (chr (ord #
"A" + c
))
39 "UNIF" ^
Int.toString (c
- 26)
42 TUnif (name
, ref NONE
)
46 exception UnequalDomains
48 fun eqRecord
f (r1
, r2
) =
49 (SM
.appi (fn (k
, v1
) =>
50 case SM
.find (r2
, k
) of
51 NONE
=> raise UnequalDomains
56 raise UnequalDomains
) r1
;
57 SM
.appi (fn (k
, v2
) =>
58 case SM
.find (r1
, k
) of
59 NONE
=> raise UnequalDomains
64 raise UnequalDomains
) r2
;
66 handle UnequalDomains
=> false
68 fun eqPred ((p1
, _
), (p2
, _
)) =
70 (CRoot
, CRoot
) => true
71 |
(CConst s1
, CConst s2
) => s1
= s2
72 |
(CPrefix p1
, CPrefix p2
) => eqPred (p1
, p2
)
73 |
(CNot p1
, CNot p2
) => eqPred (p1
, p2
)
74 |
(CAnd (p1
, q1
), CAnd (p2
, q2
)) =>
75 eqPred (p1
, p2
) andalso eqPred (q1
, q2
)
79 fun eqTy (t1All
as (t1
, _
), t2All
as (t2
, _
)) =
81 (TBase s1
, TBase s2
) => s1
= s2
82 |
(TList t1
, TList t2
) => eqTy (t1
, t2
)
83 |
(TArrow (d1
, r1
), TArrow (d2
, r2
)) =>
84 eqTy (d1
, d2
) andalso eqTy (r1
, r2
)
86 |
(TAction (p1
, d1
, r1
), TAction (p2
, d2
, r2
)) =>
87 eqPred (p1
, p2
) andalso eqRecord
eqTy (d1
, d2
)
88 andalso eqRecord
eqTy (r1
, r2
)
90 |
(TNested (p1
, q1
), TNested (p2
, q2
)) =>
91 eqPred (p1
, p2
) andalso eqTy (q1
, q2
)
93 |
(TUnif (_
, ref (SOME t1
)), _
) => eqTy (t1
, t2All
)
94 |
(_
, TUnif (_
, ref (SOME t2
))) => eqTy (t1All
, t2
)
96 |
(TUnif (_
, r1
), TUnif (_
, r2
)) => r1
= r2
98 |
(TError
, TError
) => true
102 datatype unification_error
=
103 UnifyPred
of pred
* pred
104 | UnifyTyp
of typ
* typ
105 | UnifyOccurs
of string * typ
107 exception Unify
of unification_error
109 datatype type_error
=
110 WrongType
of string * exp
* typ
* typ
* unification_error option
111 | WrongForm
of string * string * exp
* typ
* unification_error option
112 | UnboundVariable
of string
113 | WrongPred
of string * pred
* pred
115 fun preface (s
, d
) = printd (PD
.hovBox (PD
.PPS
.Rel
0,
116 [PD
.string s
, PD
.space
1, d
]))
118 fun describe_unification_error t ue
=
120 UnifyPred (p1
, p2
) =>
121 (print
"Reason: Incompatible contexts.\n";
122 preface ("Have:", p_pred p1
);
123 preface ("Need:", p_pred p2
))
124 |
UnifyTyp (t1
, t2
) =>
128 (print
"Reason: Incompatible types.\n";
129 preface ("Have:", p_typ t1
);
130 preface ("Need:", p_typ t2
))
131 |
UnifyOccurs (name
, t
') =>
135 (print
"Reason: Occurs check failed for ";
140 fun describe_type_error loc te
=
142 WrongType (place
, e
, t1
, t2
, ueo
) =>
143 (ErrorMsg
.error (SOME loc
) (place ^
" has wrong type.");
144 preface (" Expression:", p_exp e
);
145 preface ("Actual type:", p_typ t1
);
146 preface ("Needed type:", p_typ t2
);
147 Option
.app (describe_unification_error t1
) ueo
)
148 |
WrongForm (place
, form
, e
, t
, ueo
) =>
149 (ErrorMsg
.error (SOME loc
) (place ^
" has a non-" ^ form ^
" type.");
150 preface ("Expression:", p_exp e
);
151 preface (" Type:", p_typ t
);
152 Option
.app (describe_unification_error t
) ueo
)
153 | UnboundVariable name
=>
154 ErrorMsg
.error (SOME loc
) ("Unbound variable " ^ name ^
".\n")
155 |
WrongPred (place
, p1
, p2
) =>
156 (ErrorMsg
.error (SOME loc
) ("Context incompatibility for " ^ place ^
".");
157 preface ("Have:", p_pred p1
);
158 preface ("Need:", p_pred p2
))
160 fun predImplies (p1All
as (p1
, _
), p2All
as (p2
, _
)) =
162 (_
, CAnd (p1
, p2
)) => predImplies (p1All
, p1
) andalso predImplies (p1All
, p2
)
163 |
(CAnd (p1
, p2
), _
) => predImplies (p1
, p2All
) orelse predImplies (p2
, p2All
)
165 |
(_
, CPrefix (CRoot
, _
)) => true
166 |
(CNot (CPrefix (CRoot
, _
), _
), _
) => true
168 |
(CRoot
, CRoot
) => true
170 |
(CConst s1
, CConst s2
) => s1
= s2
172 |
(CPrefix p1
, CPrefix p2
) => predImplies (p1
, p2
)
173 |
(_
, CPrefix p2
) => predImplies (p1All
, p2
)
175 |
(CNot p1
, CNot p2
) => predImplies (p2
, p1
)
179 fun predSimpl (pAll
as (p
, loc
)) =
183 | CPrefix p
=> (CPrefix (predSimpl p
), loc
)
184 | CNot p
=> (CNot (predSimpl p
), loc
)
187 val p1
' = predSimpl p1
188 val p2
' = predSimpl p2
191 (CAnd (c1
, c2
), _
) => predSimpl (CAnd (c1
, (CAnd (c2
, p2
'), loc
)), loc
)
192 | _
=> if predImplies (p2
', p1
') then
194 else if predImplies (p1
', p2
') then
197 (CAnd (p1
', p2
'), loc
)
200 fun subPred (p1
, p2
) =
201 if predImplies (p1
, p2
) then
204 raise (Unify (UnifyPred (p1
, p2
)))
206 fun subRecord
f (r1
, r2
) =
207 SM
.appi (fn (k
, v2
) =>
208 case SM
.find (r1
, k
) of
209 NONE
=> raise UnequalDomains
210 | SOME v1
=> f (v1
, v2
)) r2
212 fun occurs
u (t
, _
) =
215 | TList t
=> occurs u t
216 |
TArrow (d
, r
) => occurs u d
orelse occurs u r
217 |
TAction (_
, d
, r
) =>
218 List.exists (occurs u
) (SM
.listItems d
)
219 orelse List.exists (occurs u
) (SM
.listItems r
)
220 |
TNested (_
, t
) => occurs u t
222 |
TUnif (_
, ref (SOME t
)) => occurs u t
223 |
TUnif (_
, u
') => u
= u
'
225 fun subTyp (t1All
as (t1
, _
), t2All
as (t2
, _
)) =
227 (TBase s1
, TBase s2
) =>
231 raise Unify (UnifyTyp (t1All
, t2All
))
232 |
(TList t1
, TList t2
) => subTyp (t1
, t2
)
233 |
(TArrow (d1
, r1
), TArrow (d2
, r2
)) =>
237 |
(TAction (p1
, d1
, r1
), TAction (p2
, d2
, r2
)) =>
239 subRecord
subTyp (d2
, d1
);
240 subRecord
subTyp (r1
, r2
);
241 subRecord
subTyp (r2
, r1
))
242 handle UnequalDomains
=> raise Unify (UnifyTyp (t1All
, t2All
)))
244 |
(TNested (d1
, r1
), TNested (d2
, r2
)) =>
248 |
(TUnif (_
, ref (SOME t1
)), _
) => subTyp (t1
, t2All
)
249 |
(_
, TUnif (_
, ref (SOME t2
))) => subTyp (t1All
, t2
)
251 |
(TUnif (_
, r1
), TUnif (_
, r2
)) =>
257 |
(TUnif (name
, r
), _
) =>
258 if occurs r t2All
then
259 raise (Unify (UnifyOccurs (name
, t2All
)))
263 |
(_
, TUnif (name
, r
)) =>
264 if occurs r t1All
then
265 raise (Unify (UnifyOccurs (name
, t1All
)))
272 | _
=> raise Unify (UnifyTyp (t1All
, t2All
))
279 fun whnorm (tAll
as (t
, loc
)) =
281 TUnif (_
, ref (SOME tAll
)) => whnorm tAll
284 fun hasTyp (e
, t1
, t2
) =
287 (case typeRule name
of
288 NONE
=> subTyp (t1
, t2
)
294 | _
=> subTyp (t1
, t2
)
296 fun checkTyp
G (tAll
as (t
, loc
)) =
298 val err
= ErrorMsg
.error (SOME loc
)
302 if lookupType G name
then
305 (err ("Unbound type name " ^ name
);
307 | TList t
=> (TList (checkTyp G t
), loc
)
308 |
TArrow (d
, r
) => (TArrow (checkTyp G d
, checkTyp G r
), loc
)
309 |
TAction (p
, d
, r
) => (TAction (p
, SM
.map (checkTyp G
) d
,
310 SM
.map (checkTyp G
) r
), loc
)
311 |
TNested (p
, t
) => (TNested (p
, checkTyp G t
), loc
)
312 | TError
=> raise Fail
"TError in parser-generated type"
313 | TUnif _
=> raise Fail
"TUnif in parser-generated type"
316 fun checkExp
G (eAll
as (e
, loc
)) =
318 val dte
= describe_type_error loc
321 EInt _
=> (TBase
"int", loc
)
322 | EString _
=> (TBase
"string", loc
)
325 val t
= (newUnif (), loc
)
327 foldl (fn (e
', ret
) =>
329 val t
' = checkExp G e
'
331 (hasTyp (eAll
, t
', t
);
333 (TList (TError
, loc
), loc
)
337 (dte (WrongType ("List element",
343 end) (TList t
, loc
) es
350 NONE
=> (newUnif (), loc
)
351 | SOME t
=> checkTyp G t
353 val G
' = bindVal
G (x
, t
, NONE
)
354 val t
' = checkExp G
' e
356 (TArrow (t
, t
'), loc
)
359 (case lookupVal G x
of
360 NONE
=> (dte (UnboundVariable x
);
363 |
EApp (func
, arg
) =>
365 val dom
= (newUnif (), loc
)
366 val ran
= (newUnif (), loc
)
368 val tf
= checkExp G func
369 val ta
= checkExp G arg
371 (hasTyp (func
, tf
, (TArrow (dom
, ran
), loc
));
372 hasTyp (arg
, ta
, dom
)
374 dte (WrongType ("Function argument",
381 (dte (WrongForm ("Function to be applied",
393 (TAction ((CPrefix (CRoot
, loc
), loc
),
395 SM
.insert (SM
.empty
, evar
, t
)),
398 |
EGet (x
, evar
, rest
) =>
400 val xt
= (newUnif (), loc
)
401 val G
' = bindVal
G (x
, xt
, NONE
)
403 val rt
= whnorm (checkExp G
' rest
)
406 (TAction (p
, d
, r
), _
) =>
407 (case SM
.find (d
, evar
) of
408 NONE
=> (TAction (p
, SM
.insert (d
, evar
, xt
), r
), loc
)
412 dte (WrongType ("Retrieved environment variable",
419 | _
=> (dte (WrongForm ("Body of environment variable read",
427 | ESeq
[] => raise Fail
"Empty ESeq"
428 | ESeq
[e1
] => checkExp G e1
429 |
ESeq (e1
:: rest
) =>
431 val e2
= (ESeq rest
, loc
)
433 val t1
= whnorm (checkExp G e1
)
434 val t2
= whnorm (checkExp G e2
)
437 (TAction (p1
, d1
, r1
), _
) =>
439 (TAction (p2
, d2
, r2
), _
) =>
441 val p
' = predSimpl (CAnd (p1
, p2
), loc
)
443 val d
' = SM
.foldli (fn (name
, t
, d
') =>
444 case SM
.find (r1
, name
) of
446 (case SM
.find (d
', name
) of
447 NONE
=> SM
.insert (d
', name
, t
)
451 dte (WrongType ("Shared environment variable",
460 dte (WrongType ("Shared environment variable",
468 val r
' = SM
.foldli (fn (name
, t
, r
') => SM
.insert (r
', name
, t
))
471 (TAction (p
', d
', r
'), loc
)
474 | _
=> (dte (WrongForm ("Action to be sequenced",
481 | _
=> (dte (WrongForm ("Action to be sequenced",
491 val t1
= whnorm (checkExp G e1
)
492 val t2
= whnorm (checkExp G e2
)
495 (TAction (p1
, d1
, r1
), _
) =>
497 (TAction (p2
, d2
, r2
), _
) =>
499 val p
' = predSimpl (CAnd (p1
, p2
), loc
)
501 val d
' = SM
.foldli (fn (name
, t
, d
') =>
502 case SM
.find (r1
, name
) of
504 (case SM
.find (d
', name
) of
505 NONE
=> SM
.insert (d
', name
, t
)
509 dte (WrongType ("Shared environment variable",
518 dte (WrongType ("Shared environment variable",
526 (TAction (p
', d
', r2
), loc
)
529 | _
=> (dte (WrongForm ("Action to be sequenced",
536 | _
=> (dte (WrongForm ("Action to be sequenced",
547 val t1
= whnorm (checkExp G e1
)
548 val t2
= whnorm (checkExp G e2
)
551 (TNested (pd
, (TAction (pr
, d1
, r1
), _
)), _
) =>
553 (TAction (p
, d
, r
), _
) =>
554 if predImplies (pd
, p
) then
557 SM
.unionWithi (fn (name
, t1
, t2
) =>
560 dte (WrongType ("Environment variable",
567 (TAction (pr
, combineRecs (d
, d1
),
568 combineRecs (r
, r1
)), loc
)
571 (dte (WrongPred ("nested action",
577 (dte (WrongForm ("Body of nested action",
585 (dte (WrongForm ("Container of nested action",
593 | ESkip
=> (TAction ((CPrefix (CRoot
, loc
), loc
),
594 SM
.empty
, SM
.empty
), loc
)
599 fun ununif (tAll
as (t
, loc
)) =
602 | TList t
=> (TList (ununif t
), loc
)
603 |
TArrow (d
, r
) => (TArrow (ununif d
, ununif r
), loc
)
604 |
TAction (p
, d
, r
) => (TAction (p
, SM
.map ununif d
, SM
.map ununif r
), loc
)
605 |
TUnif (_
, ref (SOME t
)) => ununif t
609 |
TUnif (_
, ref NONE
) => raise Ununif
611 fun hasError (t
, _
) =
614 | TList t
=> hasError t
615 |
TArrow (d
, r
) => hasError d
orelse hasError r
616 |
TAction (p
, d
, r
) => List.exists
hasError (SM
.listItems d
)
617 orelse List.exists
hasError (SM
.listItems r
)
620 |
TUnif (_
, ref (SOME t
)) => hasError t
621 |
TUnif (_
, ref NONE
) => false
624 fun checkUnit
G (eAll
as (_
, loc
)) =
627 val t
= checkExp G eAll
634 (ErrorMsg
.error (SOME loc
) "Unification variables remain in type:";
639 fun checkDecl
G (d
, _
, loc
) =
641 DExternType name
=> bindType G name
642 |
DExternVal (name
, t
) => bindVal
G (name
, checkTyp G t
, NONE
)
643 |
DVal (name
, to
, e
) =>
647 NONE
=> (newUnif (), loc
)
648 | SOME to
=> checkTyp G to
654 describe_type_error loc
655 (WrongType ("Bound value",
660 bindVal
G (name
, to
, SOME e
)
663 fun checkFile G
tInit (ds
, eo
) =
665 val G
' = foldl (fn (d
, G
) => checkDecl G d
) G ds
669 |
SOME (e
as (_
, loc
)) =>
671 val t
= checkExp G
' e
675 (ErrorMsg
.error (SOME loc
) "Bad type for final expression of source file.";
676 preface ("Actual:", p_typ t
);
677 preface ("Needed:", p_typ tInit
))