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 checkTyp
G (tAll
as (t
, loc
)) =
286 val err
= ErrorMsg
.error (SOME loc
)
290 if lookupType G name
then
293 (err ("Unbound type name " ^ name
);
295 | TList t
=> (TList (checkTyp G t
), loc
)
296 |
TArrow (d
, r
) => (TArrow (checkTyp G d
, checkTyp G r
), loc
)
297 |
TAction (p
, d
, r
) => (TAction (p
, SM
.map (checkTyp G
) d
,
298 SM
.map (checkTyp G
) r
), loc
)
299 |
TNested (p
, t
) => (TNested (p
, checkTyp G t
), loc
)
300 | TError
=> raise Fail
"TError in parser-generated type"
301 | TUnif _
=> raise Fail
"TUnif in parser-generated type"
304 fun checkExp
G (eAll
as (e
, loc
)) =
306 val dte
= describe_type_error loc
309 EInt _
=> (TBase
"int", loc
)
310 | EString _
=> (TBase
"string", loc
)
313 val t
= (newUnif (), loc
)
315 foldl (fn (e
', ret
) =>
317 val t
' = checkExp G e
'
321 (TList (TError
, loc
), loc
)
325 (dte (WrongType ("List element",
331 end) (TList t
, loc
) es
338 NONE
=> (newUnif (), loc
)
339 | SOME t
=> checkTyp G t
341 val G
' = bindVal
G (x
, t
, NONE
)
342 val t
' = checkExp G
' e
344 (TArrow (t
, t
'), loc
)
347 (case lookupVal G x
of
348 NONE
=> (dte (UnboundVariable x
);
351 |
EApp (func
, arg
) =>
353 val dom
= (newUnif (), loc
)
354 val ran
= (newUnif (), loc
)
356 val tf
= checkExp G func
357 val ta
= checkExp G arg
359 (subTyp (tf
, (TArrow (dom
, ran
), loc
));
362 dte (WrongType ("Function argument",
369 (dte (WrongForm ("Function to be applied",
381 (TAction ((CPrefix (CRoot
, loc
), loc
),
383 SM
.insert (SM
.empty
, evar
, t
)),
386 |
EGet (x
, evar
, rest
) =>
388 val xt
= (newUnif (), loc
)
389 val G
' = bindVal
G (x
, xt
, NONE
)
391 val rt
= whnorm (checkExp G
' rest
)
394 (TAction (p
, d
, r
), _
) =>
395 (case SM
.find (d
, evar
) of
396 NONE
=> (TAction (p
, SM
.insert (d
, evar
, xt
), r
), loc
)
400 dte (WrongType ("Retrieved environment variable",
407 | _
=> (dte (WrongForm ("Body of environment variable read",
415 | ESeq
[] => raise Fail
"Empty ESeq"
416 | ESeq
[e1
] => checkExp G e1
417 |
ESeq (e1
:: rest
) =>
419 val e2
= (ESeq rest
, loc
)
421 val t1
= whnorm (checkExp G e1
)
422 val t2
= whnorm (checkExp G e2
)
425 (TAction (p1
, d1
, r1
), _
) =>
427 (TAction (p2
, d2
, r2
), _
) =>
429 val p
' = predSimpl (CAnd (p1
, p2
), loc
)
431 val d
' = SM
.foldli (fn (name
, t
, d
') =>
432 case SM
.find (r1
, name
) of
434 (case SM
.find (d
', name
) of
435 NONE
=> SM
.insert (d
', name
, t
)
439 dte (WrongType ("Shared environment variable",
448 dte (WrongType ("Shared environment variable",
456 val r
' = SM
.foldli (fn (name
, t
, r
') => SM
.insert (r
', name
, t
))
459 (TAction (p
', d
', r
'), loc
)
462 | _
=> (dte (WrongForm ("Action to be sequenced",
469 | _
=> (dte (WrongForm ("Action to be sequenced",
479 val t1
= whnorm (checkExp G e1
)
480 val t2
= whnorm (checkExp G e2
)
483 (TAction (p1
, d1
, r1
), _
) =>
485 (TAction (p2
, d2
, r2
), _
) =>
487 val p
' = predSimpl (CAnd (p1
, p2
), loc
)
489 val d
' = SM
.foldli (fn (name
, t
, d
') =>
490 case SM
.find (r1
, name
) of
492 (case SM
.find (d
', name
) of
493 NONE
=> SM
.insert (d
', name
, t
)
497 dte (WrongType ("Shared environment variable",
506 dte (WrongType ("Shared environment variable",
514 (TAction (p
', d
', r2
), loc
)
517 | _
=> (dte (WrongForm ("Action to be sequenced",
524 | _
=> (dte (WrongForm ("Action to be sequenced",
535 val t1
= whnorm (checkExp G e1
)
536 val t2
= whnorm (checkExp G e2
)
539 (TNested (pd
, (TAction (pr
, d1
, r1
), _
)), _
) =>
541 (TAction (p
, d
, r
), _
) =>
542 if predImplies (pd
, p
) then
545 SM
.unionWithi (fn (name
, t1
, t2
) =>
548 dte (WrongType ("Environment variable",
555 (TAction (pr
, combineRecs (d
, d1
),
556 combineRecs (r
, r1
)), loc
)
559 (dte (WrongPred ("nested action",
565 (dte (WrongForm ("Body of nested action",
573 (dte (WrongForm ("Container of nested action",
581 | ESkip
=> (TAction ((CPrefix (CRoot
, loc
), loc
),
582 SM
.empty
, SM
.empty
), loc
)
587 fun ununif (tAll
as (t
, loc
)) =
590 | TList t
=> (TList (ununif t
), loc
)
591 |
TArrow (d
, r
) => (TArrow (ununif d
, ununif r
), loc
)
592 |
TAction (p
, d
, r
) => (TAction (p
, SM
.map ununif d
, SM
.map ununif r
), loc
)
593 |
TUnif (_
, ref (SOME t
)) => ununif t
597 |
TUnif (_
, ref NONE
) => raise Ununif
599 fun hasError (t
, _
) =
602 | TList t
=> hasError t
603 |
TArrow (d
, r
) => hasError d
orelse hasError r
604 |
TAction (p
, d
, r
) => List.exists
hasError (SM
.listItems d
)
605 orelse List.exists
hasError (SM
.listItems r
)
608 |
TUnif (_
, ref (SOME t
)) => hasError t
609 |
TUnif (_
, ref NONE
) => false
612 fun checkUnit
G (eAll
as (_
, loc
)) =
615 val t
= checkExp G eAll
622 (ErrorMsg
.error (SOME loc
) "Unification variables remain in type:";
627 fun checkDecl
G (d
, _
, loc
) =
629 DExternType name
=> bindType G name
630 |
DExternVal (name
, t
) => bindVal
G (name
, checkTyp G t
, NONE
)
631 |
DVal (name
, to
, e
) =>
635 NONE
=> (newUnif (), loc
)
636 | SOME to
=> checkTyp G to
642 describe_type_error loc
643 (WrongType ("Bound value",
648 bindVal
G (name
, to
, SOME e
)
651 fun checkFile G
tInit (ds
, eo
) =
653 val G
' = foldl (fn (d
, G
) => checkDecl G d
) G ds
657 |
SOME (e
as (_
, loc
)) =>
659 val t
= checkExp G
' e
663 (ErrorMsg
.error (SOME loc
) "Bad type for final expression of source file.";
664 preface ("Actual:", p_typ t
);
665 preface ("Needed:", p_typ tInit
))