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
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 datatype unification_error
=
107 UnifyPred
of pred
* pred
108 | UnifyTyp
of typ
* typ
109 | UnifyOccurs
of string * typ
111 exception Unify
of unification_error
113 datatype type_error
=
114 WrongType
of string * exp
* typ
* typ
* unification_error option
115 | WrongForm
of string * string * exp
* typ
* unification_error option
116 | UnboundVariable
of string
117 | WrongPred
of string * pred
* pred
119 fun describe_unification_error t ue
=
121 UnifyPred (p1
, p2
) =>
122 (print
"Reason: Incompatible contexts.\n";
123 preface ("Have:", p_pred p1
);
124 preface ("Need:", p_pred p2
))
125 |
UnifyTyp (t1
, t2
) =>
129 (print
"Reason: Incompatible types.\n";
130 preface ("Have:", p_typ t1
);
131 preface ("Need:", p_typ t2
))
132 |
UnifyOccurs (name
, t
') =>
136 (print
"Reason: Occurs check failed for ";
141 fun describe_type_error loc te
=
143 WrongType (place
, e
, t1
, t2
, ueo
) =>
144 (ErrorMsg
.error (SOME loc
) (place ^
" has wrong type.");
145 preface (" Expression:", p_exp e
);
146 preface ("Actual type:", p_typ t1
);
147 preface ("Needed type:", p_typ t2
);
148 Option
.app (describe_unification_error t1
) ueo
)
149 |
WrongForm (place
, form
, e
, t
, ueo
) =>
150 (ErrorMsg
.error (SOME loc
) (place ^
" has a non-" ^ form ^
" type.");
151 preface ("Expression:", p_exp e
);
152 preface (" Type:", p_typ t
);
153 Option
.app (describe_unification_error t
) ueo
)
154 | UnboundVariable name
=>
155 ErrorMsg
.error (SOME loc
) ("Unbound variable " ^ name ^
".\n")
156 |
WrongPred (place
, p1
, p2
) =>
157 (ErrorMsg
.error (SOME loc
) ("Context incompatibility for " ^ place ^
".");
158 preface ("Have:", p_pred p1
);
159 preface ("Need:", p_pred p2
))
161 fun predImplies (p1All
as (p1
, _
), p2All
as (p2
, _
)) =
163 (_
, CAnd (p1
, p2
)) => predImplies (p1All
, p1
) andalso predImplies (p1All
, p2
)
164 |
(CAnd (p1
, p2
), _
) => predImplies (p1
, p2All
) orelse predImplies (p2
, p2All
)
166 |
(_
, CPrefix (CRoot
, _
)) => true
167 |
(CNot (CPrefix (CRoot
, _
), _
), _
) => true
169 |
(CRoot
, CRoot
) => true
171 |
(CConst s1
, CConst s2
) => s1
= s2
173 |
(CPrefix p1
, CPrefix p2
) => predImplies (p1
, p2
)
174 |
(_
, CPrefix p2
) => predImplies (p1All
, p2
)
176 |
(CNot p1
, CNot p2
) => predImplies (p2
, p1
)
177 |
(CRoot
, CNot (CConst _
, _
)) => true
178 |
(CConst s1
, CNot (CConst s2
, _
)) => s1
<> s2
182 fun predSimpl (pAll
as (p
, loc
)) =
186 | CPrefix p
=> (CPrefix (predSimpl p
), loc
)
187 | CNot p
=> (CNot (predSimpl p
), loc
)
190 val p1
' = predSimpl p1
191 val p2
' = predSimpl p2
194 (CAnd (c1
, c2
), _
) => predSimpl (CAnd (c1
, (CAnd (c2
, p2
'), loc
)), loc
)
195 | _
=> if predImplies (p2
', p1
') then
197 else if predImplies (p1
', p2
') then
200 (CAnd (p1
', p2
'), loc
)
203 fun subPred (p1
, p2
) =
204 if predImplies (p1
, p2
) then
207 raise (Unify (UnifyPred (p1
, p2
)))
209 fun subRecord
f (r1
, r2
) =
210 SM
.appi (fn (k
, v2
) =>
211 case SM
.find (r1
, k
) of
212 NONE
=> raise UnequalDomains
213 | SOME v1
=> f (v1
, v2
)) r2
215 fun occurs
u (t
, _
) =
218 | TList t
=> occurs u t
219 |
TArrow (d
, r
) => occurs u d
orelse occurs u r
220 |
TAction (_
, d
, r
) =>
221 List.exists (occurs u
) (SM
.listItems d
)
222 orelse List.exists (occurs u
) (SM
.listItems r
)
223 |
TNested (_
, t
) => occurs u t
225 |
TUnif (_
, ref (SOME t
)) => occurs u t
226 |
TUnif (_
, u
') => u
= u
'
228 fun subTyp (t1All
as (t1
, _
), t2All
as (t2
, _
)) =
230 (TBase s1
, TBase s2
) =>
234 raise Unify (UnifyTyp (t1All
, t2All
))
235 |
(TList t1
, TList t2
) => subTyp (t1
, t2
)
236 |
(TArrow (d1
, r1
), TArrow (d2
, r2
)) =>
240 |
(TAction (p1
, d1
, r1
), TAction (p2
, d2
, r2
)) =>
242 subRecord
subTyp (d2
, d1
);
243 subRecord
subTyp (r1
, r2
);
244 subRecord
subTyp (r2
, r1
))
245 handle UnequalDomains
=> raise Unify (UnifyTyp (t1All
, t2All
)))
247 |
(TNested (d1
, r1
), TNested (d2
, r2
)) =>
251 |
(TUnif (_
, ref (SOME t1
)), _
) => subTyp (t1
, t2All
)
252 |
(_
, TUnif (_
, ref (SOME t2
))) => subTyp (t1All
, t2
)
254 |
(TUnif (_
, r1
), TUnif (_
, r2
)) =>
260 |
(TUnif (name
, r
), _
) =>
261 if occurs r t2All
then
262 raise (Unify (UnifyOccurs (name
, t2All
)))
266 |
(_
, TUnif (name
, r
)) =>
267 if occurs r t1All
then
268 raise (Unify (UnifyOccurs (name
, t1All
)))
275 | _
=> raise Unify (UnifyTyp (t1All
, t2All
))
282 fun whnorm (tAll
as (t
, loc
)) =
284 TUnif (_
, ref (SOME tAll
)) => whnorm tAll
287 fun baseCondition t
=
289 (TBase name
, _
) => typeRule name
291 (case baseCondition t
of
293 | SOME f
=> SOME (fn (EList ls
, _
) => List.all f ls
297 fun hasTyp (e
, t1
, t2
) =
298 if (case baseCondition t2
of
300 | SOME rule
=> rule e
) then
305 fun checkPred
G (p
, loc
) =
307 val err
= ErrorMsg
.error (SOME loc
)
312 if lookupContext G s
then
315 err ("Unbound context " ^ s
)
316 | CPrefix p
=> checkPred G p
317 | CNot p
=> checkPred G p
318 |
CAnd (p1
, p2
) => (checkPred G p1
; checkPred G p2
)
321 fun checkTyp
G (tAll
as (t
, loc
)) =
323 val err
= ErrorMsg
.error (SOME loc
)
327 if lookupType G name
then
330 (err ("Unbound type name " ^ name
);
332 | TList t
=> (TList (checkTyp G t
), loc
)
333 |
TArrow (d
, r
) => (TArrow (checkTyp G d
, checkTyp G r
), loc
)
334 |
TAction (p
, d
, r
) => (checkPred G p
;
335 (TAction (p
, SM
.map (checkTyp G
) d
,
336 SM
.map (checkTyp G
) r
), loc
))
337 |
TNested (p
, t
) => (checkPred G p
;
338 (TNested (p
, checkTyp G t
), loc
))
339 | TError
=> raise Fail
"TError in parser-generated type"
340 | TUnif _
=> raise Fail
"TUnif in parser-generated type"
343 fun envVarSetFrom
v (e
, _
) =
350 |
EGet (_
, _
, e
) => envVarSetFrom v e
351 | ESeq es
=> foldr (fn (e
, found
) =>
354 | NONE
=> envVarSetFrom v e
)
356 |
ELocal (_
, e
) => envVarSetFrom v e
360 fun checkExp
G (eAll
as (e
, loc
)) =
362 val dte
= describe_type_error loc
365 EInt _
=> (TBase
"int", loc
)
366 | EString _
=> (TBase
"string", loc
)
369 val t
= (newUnif (), loc
)
371 foldl (fn (e
', ret
) =>
373 val t
' = checkExp G e
'
375 (hasTyp (eAll
, t
', t
);
377 (TList (TError
, loc
), loc
)
381 (dte (WrongType ("List element",
387 end) (TList t
, loc
) es
394 NONE
=> (newUnif (), loc
)
395 | SOME t
=> checkTyp G t
397 val G
' = bindVal
G (x
, t
, NONE
)
398 val t
' = checkExp G
' e
400 (TArrow (t
, t
'), loc
)
403 (case lookupVal G x
of
404 NONE
=> (dte (UnboundVariable x
);
407 |
EApp (func
, arg
) =>
409 val dom
= (newUnif (), loc
)
410 val ran
= (newUnif (), loc
)
412 val tf
= checkExp G func
413 val ta
= checkExp G arg
415 (hasTyp (func
, tf
, (TArrow (dom
, ran
), loc
));
416 hasTyp (arg
, ta
, dom
)
418 dte (WrongType ("Function argument",
425 (dte (WrongForm ("Function to be applied",
435 val p
' = checkPred G p
437 val G
' = bindVal
G (x
, (TAction (p
, SM
.empty
, SM
.empty
), loc
), NONE
)
438 val t
' = whnorm (checkExp G
' e
)
441 (TAction _
, _
) => (TNested (p
, t
'), loc
)
442 | _
=> (dte (WrongForm ("Body of nested configuration 'fn'",
454 (TAction ((CPrefix (CRoot
, loc
), loc
),
456 SM
.insert (SM
.empty
, evar
, t
)),
459 |
EGet (x
, evar
, rest
) =>
461 val xt
= (newUnif (), loc
)
462 val G
' = bindVal
G (x
, xt
, NONE
)
464 val rt
= whnorm (checkExp G
' rest
)
467 (TAction (p
, d
, r
), _
) =>
468 (case SM
.find (d
, evar
) of
469 NONE
=> (TAction (p
, SM
.insert (d
, evar
, xt
), r
), loc
)
473 dte (WrongType ("Retrieved environment variable",
480 | _
=> (dte (WrongForm ("Body of environment variable read",
488 | ESeq
[] => raise Fail
"Empty ESeq"
489 | ESeq
[e1
] => checkExp G e1
490 |
ESeq (e1
:: rest
) =>
492 val e2
= (ESeq rest
, loc
)
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
)
510 ((case envVarSetFrom name e1
of
511 NONE
=> subTyp (t
, t
')
512 | SOME e
=> hasTyp (e
, t
, t
'))
514 dte (WrongType ("Shared environment variable",
521 ((case envVarSetFrom name e1
of
522 NONE
=> subTyp (t
, t
')
523 | SOME e
=> hasTyp (e
, t
, t
'))
525 dte (WrongType ("Shared environment variable",
533 val r
' = SM
.foldli (fn (name
, t
, r
') => SM
.insert (r
', name
, t
))
536 (TAction (p
', d
', r
'), loc
)
539 | _
=> (dte (WrongForm ("Action to be sequenced",
546 | _
=> (dte (WrongForm ("Action to be sequenced",
556 val t1
= whnorm (checkExp G e1
)
557 val t2
= whnorm (checkExp G e2
)
560 (TAction (p1
, d1
, r1
), _
) =>
562 (TAction (p2
, d2
, r2
), _
) =>
564 val p
' = predSimpl (CAnd (p1
, p2
), loc
)
566 val d
' = SM
.foldli (fn (name
, t
, d
') =>
567 case SM
.find (r1
, name
) of
569 (case SM
.find (d
', name
) of
570 NONE
=> SM
.insert (d
', name
, t
)
572 ((case envVarSetFrom name e1
of
573 NONE
=> subTyp (t
', t
)
574 | SOME e
=> hasTyp (e
, t
', t
))
576 dte (WrongType ("Shared environment variable",
583 ((case envVarSetFrom name e1
of
584 NONE
=> subTyp (t
', t
)
585 | SOME e
=> hasTyp (e
, t
', t
))
587 dte (WrongType ("Shared environment variable",
595 (TAction (p
', d
', r2
), loc
)
598 | _
=> (dte (WrongForm ("Action to be sequenced",
605 | _
=> (dte (WrongForm ("Action to be sequenced",
616 val t1
= whnorm (checkExp G e1
)
617 val t2
= whnorm (checkExp G e2
)
620 (TNested (pd
, (TAction (pr
, d1
, r1
), _
)), _
) =>
622 (TAction (p
, d
, r
), _
) =>
623 if predImplies (pd
, p
) then
626 SM
.unionWithi (fn (name
, t1
, t2
) =>
629 dte (WrongType ("Environment variable",
636 (TAction (pr
, combineRecs (d
, d1
),
637 combineRecs (r
, r1
)), loc
)
640 (dte (WrongPred ("nested action",
646 (dte (WrongForm ("Body of nested action",
654 (dte (WrongForm ("Container of nested action",
662 | ESkip
=> (TAction ((CPrefix (CRoot
, loc
), loc
),
663 SM
.empty
, SM
.empty
), loc
)
668 fun ununif (tAll
as (t
, loc
)) =
671 | TList t
=> (TList (ununif t
), loc
)
672 |
TArrow (d
, r
) => (TArrow (ununif d
, ununif r
), loc
)
673 |
TAction (p
, d
, r
) => (TAction (p
, SM
.map ununif d
, SM
.map ununif r
), loc
)
674 |
TUnif (_
, ref (SOME t
)) => ununif t
678 |
TUnif (_
, ref NONE
) => raise Ununif
680 fun hasError (t
, _
) =
683 | TList t
=> hasError t
684 |
TArrow (d
, r
) => hasError d
orelse hasError r
685 |
TAction (p
, d
, r
) => List.exists
hasError (SM
.listItems d
)
686 orelse List.exists
hasError (SM
.listItems r
)
689 |
TUnif (_
, ref (SOME t
)) => hasError t
690 |
TUnif (_
, ref NONE
) => false
693 fun checkUnit
G (eAll
as (_
, loc
)) =
696 val t
= checkExp G eAll
703 (ErrorMsg
.error (SOME loc
) "Unification variables remain in type:";
708 fun checkDecl
G (d
, _
, loc
) =
714 (ErrorMsg
.error (SOME loc
) "'extern type' not allowed in untrusted code";
716 |
DExternVal (name
, t
) =>
718 bindVal
G (name
, checkTyp G t
, NONE
)
720 (ErrorMsg
.error (SOME loc
) "'extern val' not allowed in untrusted code";
722 |
DVal (name
, to
, e
) =>
726 NONE
=> (newUnif (), loc
)
727 | SOME to
=> checkTyp G to
733 describe_type_error loc
734 (WrongType ("Bound value",
739 bindVal
G (name
, to
, SOME e
)
741 | DContext name
=> bindContext G name
743 fun checkFile G
tInit (_
, ds
, eo
) =
745 val G
' = foldl (fn (d
, G
) => checkDecl G d
) G ds
749 |
SOME (e
as (_
, loc
)) =>
751 val t
= checkExp G
' e
755 (ErrorMsg
.error (SOME loc
) "Bad type for final expression of source file.";
756 preface ("Actual:", p_typ t
);
757 preface ("Needed:", p_typ tInit
))