1 (* HCoop
Domtool (http
://hcoop
.sourceforge
.net
/)
2 * Copyright (c
) 2006-2007, 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
27 val externFlag
= ref
false
28 fun allowExterns () = externFlag
:= true
29 fun disallowExterns () = externFlag
:= false
34 fun resetUnif () = unifCount
:= 0
41 str (chr (ord #
"A" + c
))
43 "UNIF" ^
Int.toString (c
- 26)
46 TUnif (name
, ref NONE
)
50 exception UnequalDomains
52 fun eqRecord
f (r1
, r2
) =
53 (SM
.appi (fn (k
, v1
) =>
54 case SM
.find (r2
, k
) of
55 NONE
=> raise UnequalDomains
60 raise UnequalDomains
) r1
;
61 SM
.appi (fn (k
, v2
) =>
62 case SM
.find (r1
, k
) of
63 NONE
=> raise UnequalDomains
68 raise UnequalDomains
) r2
;
70 handle UnequalDomains
=> false
72 fun eqPred ((p1
, _
), (p2
, _
)) =
74 (CRoot
, CRoot
) => true
75 |
(CConst s1
, CConst s2
) => s1
= s2
76 |
(CPrefix p1
, CPrefix p2
) => eqPred (p1
, p2
)
77 |
(CNot p1
, CNot p2
) => eqPred (p1
, p2
)
78 |
(CAnd (p1
, q1
), CAnd (p2
, q2
)) =>
79 eqPred (p1
, p2
) andalso eqPred (q1
, q2
)
83 fun eqTy (t1All
as (t1
, _
), t2All
as (t2
, _
)) =
85 (TBase s1
, TBase s2
) => s1
= s2
86 |
(TList t1
, TList t2
) => eqTy (t1
, t2
)
87 |
(TArrow (d1
, r1
), TArrow (d2
, r2
)) =>
88 eqTy (d1
, d2
) andalso eqTy (r1
, r2
)
90 |
(TAction (p1
, d1
, r1
), TAction (p2
, d2
, r2
)) =>
91 eqPred (p1
, p2
) andalso eqRecord
eqTy (d1
, d2
)
92 andalso eqRecord
eqTy (r1
, r2
)
94 |
(TNested (p1
, q1
), TNested (p2
, q2
)) =>
95 eqPred (p1
, p2
) andalso eqTy (q1
, q2
)
97 |
(TUnif (_
, ref (SOME t1
)), _
) => eqTy (t1
, t2All
)
98 |
(_
, TUnif (_
, ref (SOME t2
))) => eqTy (t1All
, t2
)
100 |
(TUnif (_
, r1
), TUnif (_
, r2
)) => r1
= r2
102 |
(TError
, TError
) => true
106 fun predImplies (p1All
as (p1
, _
), p2All
as (p2
, _
)) =
108 (_
, CAnd (p1
, p2
)) => predImplies (p1All
, p1
) andalso predImplies (p1All
, p2
)
109 |
(CAnd (p1
, p2
), _
) => predImplies (p1
, p2All
) orelse predImplies (p2
, p2All
)
111 |
(_
, CPrefix (CRoot
, _
)) => true
112 |
(CNot (CPrefix (CRoot
, _
), _
), _
) => true
114 |
(CRoot
, CRoot
) => true
116 |
(CConst s1
, CConst s2
) => s1
= s2
118 |
(CPrefix p1
, CPrefix p2
) => predImplies (p1
, p2
)
119 |
(_
, CPrefix p2
) => predImplies (p1All
, p2
)
121 |
(CNot p1
, CNot p2
) => predImplies (p2
, p1
)
122 |
(CRoot
, CNot (CConst _
, _
)) => true
123 |
(CConst s1
, CNot (CConst s2
, _
)) => s1
<> s2
127 fun predSimpl (pAll
as (p
, loc
)) =
131 | CPrefix p
=> (CPrefix (predSimpl p
), loc
)
132 | CNot p
=> (CNot (predSimpl p
), loc
)
135 val p1
' = predSimpl p1
136 val p2
' = predSimpl p2
139 (CAnd (c1
, c2
), _
) => predSimpl (CAnd (c1
, (CAnd (c2
, p2
'), loc
)), loc
)
140 | _
=> if predImplies (p2
', p1
') then
142 else if predImplies (p1
', p2
') then
145 (CAnd (p1
', p2
'), loc
)
148 fun subPred (p1
, p2
) =
149 if predImplies (p1
, p2
) then
152 raise (Unify (UnifyPred (p1
, p2
)))
154 fun subRecord
f (r1
, r2
) =
155 SM
.appi (fn (k
, v2
) =>
156 case SM
.find (r1
, k
) of
157 NONE
=> raise UnequalDomains
158 | SOME v1
=> f (v1
, v2
)) r2
160 fun occurs
u (t
, _
) =
163 | TList t
=> occurs u t
164 |
TArrow (d
, r
) => occurs u d
orelse occurs u r
165 |
TAction (_
, d
, r
) =>
166 List.exists (occurs u
) (SM
.listItems d
)
167 orelse List.exists (occurs u
) (SM
.listItems r
)
168 |
TNested (_
, t
) => occurs u t
170 |
TUnif (_
, ref (SOME t
)) => occurs u t
171 |
TUnif (_
, u
') => u
= u
'
173 fun subTyp (t1All
as (t1
, _
), t2All
as (t2
, _
)) =
175 (TBase s1
, TBase s2
) =>
179 raise Unify (UnifyTyp (t1All
, t2All
))
180 |
(TList t1
, TList t2
) => subTyp (t1
, t2
)
181 |
(TArrow (d1
, r1
), TArrow (d2
, r2
)) =>
185 |
(TAction (p1
, d1
, r1
), TAction (p2
, d2
, r2
)) =>
187 subRecord
subTyp (d2
, d1
);
188 subRecord
subTyp (r1
, r2
);
189 subRecord
subTyp (r2
, r1
))
190 handle UnequalDomains
=> raise Unify (UnifyTyp (t1All
, t2All
)))
192 |
(TNested (d1
, r1
), TNested (d2
, r2
)) =>
196 |
(TUnif (_
, ref (SOME t1
)), _
) => subTyp (t1
, t2All
)
197 |
(_
, TUnif (_
, ref (SOME t2
))) => subTyp (t1All
, t2
)
199 |
(TUnif (_
, r1
), TUnif (_
, r2
)) =>
205 |
(TUnif (name
, r
), _
) =>
206 if occurs r t2All
then
207 raise (Unify (UnifyOccurs (name
, t2All
)))
211 |
(_
, TUnif (name
, r
)) =>
212 if occurs r t1All
then
213 raise (Unify (UnifyOccurs (name
, t1All
)))
220 | _
=> raise Unify (UnifyTyp (t1All
, t2All
))
227 fun whnorm (tAll
as (t
, loc
)) =
229 TUnif (_
, ref (SOME tAll
)) => whnorm tAll
232 fun baseCondition t
=
234 (TBase name
, _
) => typeRule name
236 (case baseCondition t
of
238 | SOME f
=> SOME (fn (EList ls
, _
) => List.all f ls
242 fun hasTyp (e
, t1
, t2
) =
243 if (case baseCondition t2
of
245 | SOME rule
=> rule e
) then
250 fun checkPred
G (p
, loc
) =
252 val err
= ErrorMsg
.error (SOME loc
)
257 if lookupContext G s
then
260 err ("Unbound context " ^ s
)
261 | CPrefix p
=> checkPred G p
262 | CNot p
=> checkPred G p
263 |
CAnd (p1
, p2
) => (checkPred G p1
; checkPred G p2
)
266 fun checkTyp
G (tAll
as (t
, loc
)) =
268 val err
= ErrorMsg
.error (SOME loc
)
272 if lookupType G name
then
275 (err ("Unbound type name " ^ name
);
277 | TList t
=> (TList (checkTyp G t
), loc
)
278 |
TArrow (d
, r
) => (TArrow (checkTyp G d
, checkTyp G r
), loc
)
279 |
TAction (p
, d
, r
) => (checkPred G p
;
280 (TAction (p
, SM
.map (checkTyp G
) d
,
281 SM
.map (checkTyp G
) r
), loc
))
282 |
TNested (p
, t
) => (checkPred G p
;
283 (TNested (p
, checkTyp G t
), loc
))
284 | TError
=> raise Fail
"TError in parser-generated type"
285 | TUnif _
=> raise Fail
"TUnif in parser-generated type"
288 fun envVarSetFrom
v (e
, _
) =
295 |
EGet (_
, _
, e
) => envVarSetFrom v e
296 | ESeq es
=> foldr (fn (e
, found
) =>
299 | NONE
=> envVarSetFrom v e
)
301 |
ELocal (_
, e
) => envVarSetFrom v e
305 fun checkExp
G (eAll
as (e
, loc
)) =
307 val dte
= Describe
.describe_type_error loc
310 EInt _
=> (TBase
"int", loc
)
311 | EString _
=> (TBase
"string", loc
)
314 val t
= (newUnif (), loc
)
316 foldl (fn (e
', ret
) =>
318 val t
' = checkExp G e
'
320 (hasTyp (eAll
, t
', t
);
322 (TList (TError
, loc
), loc
)
326 (dte (WrongType ("List element",
332 end) (TList t
, loc
) es
339 NONE
=> (newUnif (), loc
)
340 | SOME t
=> checkTyp G t
342 val G
' = bindVal
G (x
, t
, NONE
)
343 val t
' = checkExp G
' e
345 (TArrow (t
, t
'), loc
)
348 (case lookupVal G x
of
349 NONE
=> (dte (UnboundVariable x
);
352 |
EApp (func
, arg
) =>
354 val dom
= (newUnif (), loc
)
355 val ran
= (newUnif (), loc
)
357 val tf
= checkExp G func
358 val ta
= checkExp G arg
360 (hasTyp (func
, tf
, (TArrow (dom
, ran
), loc
));
361 hasTyp (arg
, ta
, dom
)
363 dte (WrongType ("Function argument",
370 (dte (WrongForm ("Function to be applied",
380 val p
' = checkPred G p
382 val G
' = bindVal
G (x
, (TAction (p
, SM
.empty
, SM
.empty
), loc
), NONE
)
383 val t
' = whnorm (checkExp G
' e
)
386 (TAction _
, _
) => (TNested (p
, t
'), loc
)
387 | _
=> (dte (WrongForm ("Body of nested configuration 'fn'",
399 (TAction ((CPrefix (CRoot
, loc
), loc
),
401 SM
.insert (SM
.empty
, evar
, t
)),
404 |
EGet (x
, evar
, rest
) =>
406 val xt
= (newUnif (), loc
)
407 val G
' = bindVal
G (x
, xt
, NONE
)
409 val rt
= whnorm (checkExp G
' rest
)
412 (TAction (p
, d
, r
), _
) =>
413 (case SM
.find (d
, evar
) of
414 NONE
=> (TAction (p
, SM
.insert (d
, evar
, xt
), r
), loc
)
418 dte (WrongType ("Retrieved environment variable",
425 | _
=> (dte (WrongForm ("Body of environment variable read",
433 | ESeq
[] => raise Fail
"Empty ESeq"
434 | ESeq
[e1
] => checkExp G e1
435 |
ESeq (e1
:: rest
) =>
437 val e2
= (ESeq rest
, loc
)
439 val t1
= whnorm (checkExp G e1
)
440 val t2
= whnorm (checkExp G e2
)
443 (TAction (p1
, d1
, r1
), _
) =>
445 (TAction (p2
, d2
, r2
), _
) =>
447 val p
' = predSimpl (CAnd (p1
, p2
), loc
)
449 val d
' = SM
.foldli (fn (name
, t
, d
') =>
450 case SM
.find (r1
, name
) of
452 (case SM
.find (d
', name
) of
453 NONE
=> SM
.insert (d
', name
, t
)
455 ((case envVarSetFrom name e1
of
456 NONE
=> subTyp (t
, t
')
457 | SOME e
=> hasTyp (e
, t
, t
'))
459 dte (WrongType ("Shared environment variable",
466 ((case envVarSetFrom name e1
of
467 NONE
=> subTyp (t
, t
')
468 | SOME e
=> hasTyp (e
, t
, t
'))
470 dte (WrongType ("Shared environment variable",
478 val r
' = SM
.foldli (fn (name
, t
, r
') => SM
.insert (r
', name
, t
))
481 (TAction (p
', d
', r
'), loc
)
484 | _
=> (dte (WrongForm ("Action to be sequenced",
491 | _
=> (dte (WrongForm ("Action to be sequenced",
501 val t1
= whnorm (checkExp G e1
)
502 val t2
= whnorm (checkExp G e2
)
505 (TAction (p1
, d1
, r1
), _
) =>
507 (TAction (p2
, d2
, r2
), _
) =>
509 val p
' = predSimpl (CAnd (p1
, p2
), loc
)
511 val d
' = SM
.foldli (fn (name
, t
, d
') =>
512 case SM
.find (r1
, name
) of
514 (case SM
.find (d
', name
) of
515 NONE
=> SM
.insert (d
', name
, t
)
517 ((case envVarSetFrom name e1
of
518 NONE
=> subTyp (t
', t
)
519 | SOME e
=> hasTyp (e
, t
', t
))
521 dte (WrongType ("Shared environment variable",
528 ((case envVarSetFrom name e1
of
529 NONE
=> subTyp (t
', t
)
530 | SOME e
=> hasTyp (e
, t
', t
))
532 dte (WrongType ("Shared environment variable",
540 (TAction (p
', d
', r2
), loc
)
543 | _
=> (dte (WrongForm ("Action to be sequenced",
550 | _
=> (dte (WrongForm ("Action to be sequenced",
561 val t1
= whnorm (checkExp G e1
)
562 val t2
= whnorm (checkExp G e2
)
565 (TNested (pd
, (TAction (pr
, d1
, r1
), _
)), _
) =>
567 (TAction (p
, d
, r
), _
) =>
568 if predImplies (pd
, p
) then
571 SM
.unionWithi (fn (name
, t1
, t2
) =>
574 dte (WrongType ("Environment variable",
581 (TAction (pr
, combineRecs (d
, d1
),
582 combineRecs (r
, r1
)), loc
)
585 (dte (WrongPred ("nested action",
591 (dte (WrongForm ("Body of nested action",
599 (dte (WrongForm ("Container of nested action",
607 | ESkip
=> (TAction ((CPrefix (CRoot
, loc
), loc
),
608 SM
.empty
, SM
.empty
), loc
)
613 fun ununif (tAll
as (t
, loc
)) =
616 | TList t
=> (TList (ununif t
), loc
)
617 |
TArrow (d
, r
) => (TArrow (ununif d
, ununif r
), loc
)
618 |
TAction (p
, d
, r
) => (TAction (p
, SM
.map ununif d
, SM
.map ununif r
), loc
)
619 |
TUnif (_
, ref (SOME t
)) => ununif t
623 |
TUnif (_
, ref NONE
) => raise Ununif
625 fun hasError (t
, _
) =
628 | TList t
=> hasError t
629 |
TArrow (d
, r
) => hasError d
orelse hasError r
630 |
TAction (p
, d
, r
) => List.exists
hasError (SM
.listItems d
)
631 orelse List.exists
hasError (SM
.listItems r
)
634 |
TUnif (_
, ref (SOME t
)) => hasError t
635 |
TUnif (_
, ref NONE
) => false
638 fun checkUnit
G (eAll
as (_
, loc
)) =
641 val t
= checkExp G eAll
648 (ErrorMsg
.error (SOME loc
) "Unification variables remain in type:";
653 fun checkDecl
G (d
, _
, loc
) =
659 (ErrorMsg
.error (SOME loc
) "'extern type' not allowed in untrusted code";
661 |
DExternVal (name
, t
) =>
663 bindVal
G (name
, checkTyp G t
, NONE
)
665 (ErrorMsg
.error (SOME loc
) "'extern val' not allowed in untrusted code";
667 |
DVal (name
, to
, e
) =>
671 NONE
=> (newUnif (), loc
)
672 | SOME to
=> checkTyp G to
678 Describe
.describe_type_error loc
679 (WrongType ("Bound value",
684 bindVal
G (name
, to
, SOME e
)
686 | DContext name
=> bindContext G name
688 fun checkFile G
tInit (_
, ds
, eo
) =
690 val G
' = foldl (fn (d
, G
) => checkDecl G d
) G ds
694 |
SOME (e
as (_
, loc
)) =>
696 val t
= checkExp G
' e
700 (ErrorMsg
.error (SOME loc
) "Bad type for final expression of source file.";
701 preface ("Actual:", p_typ t
);
702 preface ("Needed:", p_typ tInit
))