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 describe_unification_error t ue
=
117 UnifyPred (p1
, p2
) =>
118 (print
"Reason: Incompatible contexts.\n";
119 preface ("Have:", p_pred p1
);
120 preface ("Need:", p_pred p2
))
121 |
UnifyTyp (t1
, t2
) =>
125 (print
"Reason: Incompatible types.\n";
126 preface ("Have:", p_typ t1
);
127 preface ("Need:", p_typ t2
))
128 |
UnifyOccurs (name
, t
') =>
132 (print
"Reason: Occurs check failed for ";
137 fun describe_type_error loc te
=
139 WrongType (place
, e
, t1
, t2
, ueo
) =>
140 (ErrorMsg
.error (SOME loc
) (place ^
" has wrong type.");
141 preface (" Expression:", p_exp e
);
142 preface ("Actual type:", p_typ t1
);
143 preface ("Needed type:", p_typ t2
);
144 Option
.app (describe_unification_error t1
) ueo
)
145 |
WrongForm (place
, form
, e
, t
, ueo
) =>
146 (ErrorMsg
.error (SOME loc
) (place ^
" has a non-" ^ form ^
" type.");
147 preface ("Expression:", p_exp e
);
148 preface (" Type:", p_typ t
);
149 Option
.app (describe_unification_error t
) ueo
)
150 | UnboundVariable name
=>
151 ErrorMsg
.error (SOME loc
) ("Unbound variable " ^ name ^
".\n")
152 |
WrongPred (place
, p1
, p2
) =>
153 (ErrorMsg
.error (SOME loc
) ("Context incompatibility for " ^ place ^
".");
154 preface ("Have:", p_pred p1
);
155 preface ("Need:", p_pred p2
))
157 fun predImplies (p1All
as (p1
, _
), p2All
as (p2
, _
)) =
159 (_
, CAnd (p1
, p2
)) => predImplies (p1All
, p1
) andalso predImplies (p1All
, p2
)
160 |
(CAnd (p1
, p2
), _
) => predImplies (p1
, p2All
) orelse predImplies (p2
, p2All
)
162 |
(_
, CPrefix (CRoot
, _
)) => true
163 |
(CNot (CPrefix (CRoot
, _
), _
), _
) => true
165 |
(CRoot
, CRoot
) => true
167 |
(CConst s1
, CConst s2
) => s1
= s2
169 |
(CPrefix p1
, CPrefix p2
) => predImplies (p1
, p2
)
170 |
(_
, CPrefix p2
) => predImplies (p1All
, p2
)
172 |
(CNot p1
, CNot p2
) => predImplies (p2
, p1
)
176 fun predSimpl (pAll
as (p
, loc
)) =
180 | CPrefix p
=> (CPrefix (predSimpl p
), loc
)
181 | CNot p
=> (CNot (predSimpl p
), loc
)
184 val p1
' = predSimpl p1
185 val p2
' = predSimpl p2
188 (CAnd (c1
, c2
), _
) => predSimpl (CAnd (c1
, (CAnd (c2
, p2
'), loc
)), loc
)
189 | _
=> if predImplies (p2
', p1
') then
191 else if predImplies (p1
', p2
') then
194 (CAnd (p1
', p2
'), loc
)
197 fun subPred (p1
, p2
) =
198 if predImplies (p1
, p2
) then
201 raise (Unify (UnifyPred (p1
, p2
)))
203 fun subRecord
f (r1
, r2
) =
204 SM
.appi (fn (k
, v2
) =>
205 case SM
.find (r1
, k
) of
206 NONE
=> raise UnequalDomains
207 | SOME v1
=> f (v1
, v2
)) r2
209 fun occurs
u (t
, _
) =
212 | TList t
=> occurs u t
213 |
TArrow (d
, r
) => occurs u d
orelse occurs u r
214 |
TAction (_
, d
, r
) =>
215 List.exists (occurs u
) (SM
.listItems d
)
216 orelse List.exists (occurs u
) (SM
.listItems r
)
217 |
TNested (_
, t
) => occurs u t
219 |
TUnif (_
, ref (SOME t
)) => occurs u t
220 |
TUnif (_
, u
') => u
= u
'
222 fun subTyp (t1All
as (t1
, _
), t2All
as (t2
, _
)) =
224 (TBase s1
, TBase s2
) =>
228 raise Unify (UnifyTyp (t1All
, t2All
))
229 |
(TList t1
, TList t2
) => subTyp (t1
, t2
)
230 |
(TArrow (d1
, r1
), TArrow (d2
, r2
)) =>
234 |
(TAction (p1
, d1
, r1
), TAction (p2
, d2
, r2
)) =>
236 subRecord
subTyp (d2
, d1
);
237 subRecord
subTyp (r1
, r2
);
238 subRecord
subTyp (r2
, r1
))
239 handle UnequalDomains
=> raise Unify (UnifyTyp (t1All
, t2All
)))
241 |
(TNested (d1
, r1
), TNested (d2
, r2
)) =>
245 |
(TUnif (_
, ref (SOME t1
)), _
) => subTyp (t1
, t2All
)
246 |
(_
, TUnif (_
, ref (SOME t2
))) => subTyp (t1All
, t2
)
248 |
(TUnif (_
, r1
), TUnif (_
, r2
)) =>
254 |
(TUnif (name
, r
), _
) =>
255 if occurs r t2All
then
256 raise (Unify (UnifyOccurs (name
, t2All
)))
260 |
(_
, TUnif (name
, r
)) =>
261 if occurs r t1All
then
262 raise (Unify (UnifyOccurs (name
, t1All
)))
269 | _
=> raise Unify (UnifyTyp (t1All
, t2All
))
276 fun whnorm (tAll
as (t
, loc
)) =
278 TUnif (_
, ref (SOME tAll
)) => whnorm tAll
281 fun baseCondition t
=
283 (TBase name
, _
) => typeRule name
285 (case baseCondition t
of
287 | SOME f
=> SOME (fn (EList ls
, _
) => List.all f ls
291 fun hasTyp (e
, t1
, t2
) =
292 if (case baseCondition t2
of
294 | SOME rule
=> rule e
) then
299 fun checkTyp
G (tAll
as (t
, loc
)) =
301 val err
= ErrorMsg
.error (SOME loc
)
305 if lookupType G name
then
308 (err ("Unbound type name " ^ name
);
310 | TList t
=> (TList (checkTyp G t
), loc
)
311 |
TArrow (d
, r
) => (TArrow (checkTyp G d
, checkTyp G r
), loc
)
312 |
TAction (p
, d
, r
) => (TAction (p
, SM
.map (checkTyp G
) d
,
313 SM
.map (checkTyp G
) r
), loc
)
314 |
TNested (p
, t
) => (TNested (p
, checkTyp G t
), loc
)
315 | TError
=> raise Fail
"TError in parser-generated type"
316 | TUnif _
=> raise Fail
"TUnif in parser-generated type"
319 fun checkExp
G (eAll
as (e
, loc
)) =
321 val dte
= describe_type_error loc
324 EInt _
=> (TBase
"int", loc
)
325 | EString _
=> (TBase
"string", loc
)
328 val t
= (newUnif (), loc
)
330 foldl (fn (e
', ret
) =>
332 val t
' = checkExp G e
'
334 (hasTyp (eAll
, t
', t
);
336 (TList (TError
, loc
), loc
)
340 (dte (WrongType ("List element",
346 end) (TList t
, loc
) es
353 NONE
=> (newUnif (), loc
)
354 | SOME t
=> checkTyp G t
356 val G
' = bindVal
G (x
, t
, NONE
)
357 val t
' = checkExp G
' e
359 (TArrow (t
, t
'), loc
)
362 (case lookupVal G x
of
363 NONE
=> (dte (UnboundVariable x
);
366 |
EApp (func
, arg
) =>
368 val dom
= (newUnif (), loc
)
369 val ran
= (newUnif (), loc
)
371 val tf
= checkExp G func
372 val ta
= checkExp G arg
374 (hasTyp (func
, tf
, (TArrow (dom
, ran
), loc
));
375 hasTyp (arg
, ta
, dom
)
377 dte (WrongType ("Function argument",
384 (dte (WrongForm ("Function to be applied",
396 (TAction ((CPrefix (CRoot
, loc
), loc
),
398 SM
.insert (SM
.empty
, evar
, t
)),
401 |
EGet (x
, evar
, rest
) =>
403 val xt
= (newUnif (), loc
)
404 val G
' = bindVal
G (x
, xt
, NONE
)
406 val rt
= whnorm (checkExp G
' rest
)
409 (TAction (p
, d
, r
), _
) =>
410 (case SM
.find (d
, evar
) of
411 NONE
=> (TAction (p
, SM
.insert (d
, evar
, xt
), r
), loc
)
415 dte (WrongType ("Retrieved environment variable",
422 | _
=> (dte (WrongForm ("Body of environment variable read",
430 | ESeq
[] => raise Fail
"Empty ESeq"
431 | ESeq
[e1
] => checkExp G e1
432 |
ESeq (e1
:: rest
) =>
434 val e2
= (ESeq rest
, loc
)
436 val t1
= whnorm (checkExp G e1
)
437 val t2
= whnorm (checkExp G e2
)
440 (TAction (p1
, d1
, r1
), _
) =>
442 (TAction (p2
, d2
, r2
), _
) =>
444 val p
' = predSimpl (CAnd (p1
, p2
), loc
)
446 val d
' = SM
.foldli (fn (name
, t
, d
') =>
447 case SM
.find (r1
, name
) of
449 (case SM
.find (d
', name
) of
450 NONE
=> SM
.insert (d
', name
, t
)
454 dte (WrongType ("Shared environment variable",
463 dte (WrongType ("Shared environment variable",
471 val r
' = SM
.foldli (fn (name
, t
, r
') => SM
.insert (r
', name
, t
))
474 (TAction (p
', d
', r
'), loc
)
477 | _
=> (dte (WrongForm ("Action to be sequenced",
484 | _
=> (dte (WrongForm ("Action to be sequenced",
494 val t1
= whnorm (checkExp G e1
)
495 val t2
= whnorm (checkExp G e2
)
498 (TAction (p1
, d1
, r1
), _
) =>
500 (TAction (p2
, d2
, r2
), _
) =>
502 val p
' = predSimpl (CAnd (p1
, p2
), loc
)
504 val d
' = SM
.foldli (fn (name
, t
, d
') =>
505 case SM
.find (r1
, name
) of
507 (case SM
.find (d
', name
) of
508 NONE
=> SM
.insert (d
', name
, t
)
512 dte (WrongType ("Shared environment variable",
521 dte (WrongType ("Shared environment variable",
529 (TAction (p
', d
', r2
), loc
)
532 | _
=> (dte (WrongForm ("Action to be sequenced",
539 | _
=> (dte (WrongForm ("Action to be sequenced",
550 val t1
= whnorm (checkExp G e1
)
551 val t2
= whnorm (checkExp G e2
)
554 (TNested (pd
, (TAction (pr
, d1
, r1
), _
)), _
) =>
556 (TAction (p
, d
, r
), _
) =>
557 if predImplies (pd
, p
) then
560 SM
.unionWithi (fn (name
, t1
, t2
) =>
563 dte (WrongType ("Environment variable",
570 (TAction (pr
, combineRecs (d
, d1
),
571 combineRecs (r
, r1
)), loc
)
574 (dte (WrongPred ("nested action",
580 (dte (WrongForm ("Body of nested action",
588 (dte (WrongForm ("Container of nested action",
596 | ESkip
=> (TAction ((CPrefix (CRoot
, loc
), loc
),
597 SM
.empty
, SM
.empty
), loc
)
602 fun ununif (tAll
as (t
, loc
)) =
605 | TList t
=> (TList (ununif t
), loc
)
606 |
TArrow (d
, r
) => (TArrow (ununif d
, ununif r
), loc
)
607 |
TAction (p
, d
, r
) => (TAction (p
, SM
.map ununif d
, SM
.map ununif r
), loc
)
608 |
TUnif (_
, ref (SOME t
)) => ununif t
612 |
TUnif (_
, ref NONE
) => raise Ununif
614 fun hasError (t
, _
) =
617 | TList t
=> hasError t
618 |
TArrow (d
, r
) => hasError d
orelse hasError r
619 |
TAction (p
, d
, r
) => List.exists
hasError (SM
.listItems d
)
620 orelse List.exists
hasError (SM
.listItems r
)
623 |
TUnif (_
, ref (SOME t
)) => hasError t
624 |
TUnif (_
, ref NONE
) => false
627 fun checkUnit
G (eAll
as (_
, loc
)) =
630 val t
= checkExp G eAll
637 (ErrorMsg
.error (SOME loc
) "Unification variables remain in type:";
642 fun checkDecl
G (d
, _
, loc
) =
644 DExternType name
=> bindType G name
645 |
DExternVal (name
, t
) => bindVal
G (name
, checkTyp G t
, NONE
)
646 |
DVal (name
, to
, e
) =>
650 NONE
=> (newUnif (), loc
)
651 | SOME to
=> checkTyp G to
657 describe_type_error loc
658 (WrongType ("Bound value",
663 bindVal
G (name
, to
, SOME e
)
666 fun checkFile G
tInit (ds
, eo
) =
668 val G
' = foldl (fn (d
, G
) => checkDecl G d
) G ds
672 |
SOME (e
as (_
, loc
)) =>
674 val t
= checkExp G
' e
678 (ErrorMsg
.error (SOME loc
) "Bad type for final expression of source file.";
679 preface ("Actual:", p_typ t
);
680 preface ("Needed:", p_typ tInit
))