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
)
51 fun predImplies (p1All
as (p1
, _
), p2All
as (p2
, _
)) =
53 (_
, CAnd (p1
, p2
)) => predImplies (p1All
, p1
) andalso predImplies (p1All
, p2
)
54 |
(CAnd (p1
, p2
), _
) => predImplies (p1
, p2All
) orelse predImplies (p2
, p2All
)
56 |
(_
, CPrefix (CRoot
, _
)) => true
57 |
(CNot (CPrefix (CRoot
, _
), _
), _
) => true
59 |
(CRoot
, CRoot
) => true
61 |
(CConst s1
, CConst s2
) => s1
= s2
63 |
(CPrefix p1
, CPrefix p2
) => predImplies (p1
, p2
)
64 |
(_
, CPrefix p2
) => predImplies (p1All
, p2
)
66 |
(CNot p1
, CNot p2
) => predImplies (p2
, p1
)
67 |
(CRoot
, CNot (CConst _
, _
)) => true
68 |
(CConst s1
, CNot (CConst s2
, _
)) => s1
<> s2
72 fun predSimpl (pAll
as (p
, loc
)) =
76 | CPrefix p
=> (CPrefix (predSimpl p
), loc
)
77 | CNot p
=> (CNot (predSimpl p
), loc
)
80 val p1
' = predSimpl p1
81 val p2
' = predSimpl p2
84 (CAnd (c1
, c2
), _
) => predSimpl (CAnd (c1
, (CAnd (c2
, p2
'), loc
)), loc
)
85 | _
=> if predImplies (p2
', p1
') then
87 else if predImplies (p1
', p2
') then
90 (CAnd (p1
', p2
'), loc
)
93 fun subPred (p1
, p2
) =
94 if predImplies (p1
, p2
) then
97 raise (Unify (UnifyPred (p1
, p2
)))
99 fun subRecord
f (r1
, r2
) =
100 SM
.appi (fn (k
, v2
) =>
101 case SM
.find (r1
, k
) of
102 NONE
=> raise Describe
.UnequalDomains
103 | SOME v1
=> f (v1
, v2
)) r2
105 fun occurs
u (t
, _
) =
108 | TList t
=> occurs u t
109 |
TArrow (d
, r
) => occurs u d
orelse occurs u r
110 |
TAction (_
, d
, r
) =>
111 List.exists (occurs u
) (SM
.listItems d
)
112 orelse List.exists (occurs u
) (SM
.listItems r
)
113 |
TNested (_
, t
) => occurs u t
115 |
TUnif (_
, ref (SOME t
)) => occurs u t
116 |
TUnif (_
, u
') => u
= u
'
118 fun subTyp (t1All
as (t1
, _
), t2All
as (t2
, _
)) =
120 (TBase s1
, TBase s2
) =>
124 raise Unify (UnifyTyp (t1All
, t2All
))
125 |
(TList t1
, TList t2
) => subTyp (t1
, t2
)
126 |
(TArrow (d1
, r1
), TArrow (d2
, r2
)) =>
130 |
(TAction (p1
, d1
, r1
), TAction (p2
, d2
, r2
)) =>
132 subRecord
subTyp (d2
, d1
);
133 subRecord
subTyp (r1
, r2
);
134 subRecord
subTyp (r2
, r1
))
135 handle UnequalDomains
=> raise Unify (UnifyTyp (t1All
, t2All
)))
137 |
(TNested (d1
, r1
), TNested (d2
, r2
)) =>
141 |
(TUnif (_
, ref (SOME t1
)), _
) => subTyp (t1
, t2All
)
142 |
(_
, TUnif (_
, ref (SOME t2
))) => subTyp (t1All
, t2
)
144 |
(TUnif (_
, r1
), TUnif (_
, r2
)) =>
150 |
(TUnif (name
, r
), _
) =>
151 if occurs r t2All
then
152 raise (Unify (UnifyOccurs (name
, t2All
)))
156 |
(_
, TUnif (name
, r
)) =>
157 if occurs r t1All
then
158 raise (Unify (UnifyOccurs (name
, t1All
)))
165 | _
=> raise Unify (UnifyTyp (t1All
, t2All
))
172 fun whnorm (tAll
as (t
, loc
)) =
174 TUnif (_
, ref (SOME tAll
)) => whnorm tAll
177 fun baseCondition t
=
179 (TBase name
, _
) => typeRule name
181 (case baseCondition t
of
183 | SOME f
=> SOME (fn (EList ls
, _
) => List.all f ls
187 fun simplifyKindOf e
=
189 (EApp ((EVar s
, _
), e
'), _
) =>
190 (case Env
.function s
of
198 fun hasTyp (e
, t1
, t2
) =
199 if (case baseCondition t2
of
201 | SOME rule
=> rule (simplifyKindOf e
)) then
206 fun checkPred
G (p
, loc
) =
208 val err
= ErrorMsg
.error (SOME loc
)
213 if lookupContext G s
then
216 err ("Unbound context " ^ s
)
217 | CPrefix p
=> checkPred G p
218 | CNot p
=> checkPred G p
219 |
CAnd (p1
, p2
) => (checkPred G p1
; checkPred G p2
)
222 fun checkTyp
G (tAll
as (t
, loc
)) =
224 val err
= ErrorMsg
.error (SOME loc
)
228 if lookupType G name
then
231 (err ("Unbound type name " ^ name
);
233 | TList t
=> (TList (checkTyp G t
), loc
)
234 |
TArrow (d
, r
) => (TArrow (checkTyp G d
, checkTyp G r
), loc
)
235 |
TAction (p
, d
, r
) => (checkPred G p
;
236 (TAction (p
, SM
.map (checkTyp G
) d
,
237 SM
.map (checkTyp G
) r
), loc
))
238 |
TNested (p
, t
) => (checkPred G p
;
239 (TNested (p
, checkTyp G t
), loc
))
240 | TError
=> raise Fail
"TError in parser-generated type"
241 | TUnif _
=> raise Fail
"TUnif in parser-generated type"
244 fun envVarSetFrom
v (e
, _
) =
251 |
EGet (_
, _
, _
, e
) => envVarSetFrom v e
252 | ESeq es
=> foldr (fn (e
, found
) =>
255 | NONE
=> envVarSetFrom v e
)
257 |
ELocal (_
, e
) => envVarSetFrom v e
261 val ununify
= Describe
.ununify
263 fun checkExp
G (eAll
as (e
, loc
)) =
265 val dte
= Describe
.describe_type_error loc
268 EInt _
=> (TBase
"int", loc
)
269 | EString _
=> (TBase
"string", loc
)
272 val t
= (newUnif (), loc
)
274 foldl (fn (e
', ret
) =>
276 val t
' = checkExp G e
'
278 (hasTyp (eAll
, t
', t
);
280 (TList (TError
, loc
), loc
)
284 (dte (WrongType ("List element",
290 end) (TList t
, loc
) es
297 NONE
=> (newUnif (), loc
)
298 | SOME t
=> checkTyp G t
300 val G
' = bindVal
G (x
, t
, NONE
)
301 val t
' = checkExp G
' e
303 (TArrow (t
, t
'), loc
)
306 (case lookupVal G x
of
307 NONE
=> (dte (UnboundVariable x
);
310 |
EApp (func
, arg
) =>
312 val dom
= (newUnif (), loc
)
313 val ran
= (newUnif (), loc
)
315 val tf
= checkExp G func
316 val ta
= checkExp G arg
318 (hasTyp (func
, tf
, (TArrow (dom
, ran
), loc
));
319 hasTyp (arg
, ta
, dom
)
321 dte (WrongType ("Function argument",
328 (dte (WrongForm ("Function to be applied",
338 val p
' = checkPred G p
340 val G
' = bindVal
G (x
, (TAction (p
, SM
.empty
, SM
.empty
), loc
), NONE
)
341 val t
' = whnorm (checkExp G
' e
)
344 (TAction _
, _
) => (TNested (p
, t
'), loc
)
345 | _
=> (dte (WrongForm ("Body of nested configuration 'fn'",
357 (TAction ((CPrefix (CRoot
, loc
), loc
),
359 SM
.insert (SM
.empty
, evar
, t
)),
362 |
EGet (x
, topt
, evar
, rest
) =>
364 val xt
= (newUnif (), loc
)
365 val G
' = bindVal
G (x
, xt
, NONE
)
367 val rt
= whnorm (checkExp G
' rest
)
371 | SOME t
=> subTyp (xt
, checkTyp G t
);
374 (TAction (p
, d
, r
), _
) =>
375 (case SM
.find (d
, evar
) of
376 NONE
=> (TAction (p
, SM
.insert (d
, evar
, xt
), r
), loc
)
380 dte (WrongType ("Retrieved environment variable",
387 | _
=> (dte (WrongForm ("Body of environment variable read",
395 | ESeq
[] => raise Fail
"Empty ESeq"
396 | ESeq
[e1
] => checkExp G e1
397 |
ESeq (e1
:: rest
) =>
399 val e2
= (ESeq rest
, loc
)
401 val t1
= whnorm (checkExp G e1
)
402 val t2
= whnorm (checkExp G e2
)
405 (TAction (p1
, d1
, r1
), _
) =>
407 (TAction (p2
, d2
, r2
), _
) =>
409 val p
' = predSimpl (CAnd (p1
, p2
), loc
)
411 val d
' = SM
.foldli (fn (name
, t
, d
') =>
412 case SM
.find (r1
, name
) of
414 (case SM
.find (d
', name
) of
415 NONE
=> SM
.insert (d
', name
, t
)
417 ((case envVarSetFrom name e1
of
418 NONE
=> subTyp (t
, t
')
419 | SOME e
=> hasTyp (e
, t
, t
'))
421 dte (WrongType ("Shared environment variable",
428 ((case envVarSetFrom name e1
of
429 NONE
=> subTyp (t
, t
')
430 | SOME e
=> hasTyp (e
, t
, t
'))
432 dte (WrongType ("Shared environment variable",
440 val r
' = SM
.foldli (fn (name
, t
, r
') => SM
.insert (r
', name
, t
))
443 (TAction (p
', d
', r
'), loc
)
446 | _
=> (dte (WrongForm ("First action to be sequenced",
453 | _
=> (dte (WrongForm ("Second action to be sequenced",
463 val t1
= whnorm (checkExp G e1
)
464 val t2
= whnorm (checkExp G e2
)
467 (TAction (p1
, d1
, r1
), _
) =>
469 (TAction (p2
, d2
, r2
), _
) =>
471 val p
' = predSimpl (CAnd (p1
, p2
), loc
)
473 val d
' = SM
.foldli (fn (name
, t
, d
') =>
474 case SM
.find (r1
, name
) of
476 (case SM
.find (d
', name
) of
477 NONE
=> SM
.insert (d
', name
, t
)
479 ((case envVarSetFrom name e1
of
480 NONE
=> subTyp (t
', t
)
481 | SOME e
=> hasTyp (e
, t
', t
))
483 dte (WrongType ("Shared environment variable",
490 ((case envVarSetFrom name e1
of
491 NONE
=> subTyp (t
', t
)
492 | SOME e
=> hasTyp (e
, t
', t
))
494 dte (WrongType ("Shared environment variable",
502 (TAction (p
', d
', r2
), loc
)
505 | _
=> (dte (WrongForm ("Body of local settings",
512 | _
=> (dte (WrongForm ("Local settings",
523 val t1
= whnorm (checkExp G e1
)
524 val t2
= whnorm (checkExp G e2
)
527 (TNested (pd
, (TAction (pr
, d1
, r1
), _
)), _
) =>
529 (TAction (p
, d
, r
), _
) =>
530 if predImplies (pd
, p
) then
533 SM
.unionWithi (fn (name
, t1
, t2
) =>
536 dte (WrongType ("Environment variable",
543 (TAction (pr
, combineRecs (d
, d1
),
544 combineRecs (r
, r1
)), loc
)
547 (dte (WrongPred ("nested action",
553 (dte (WrongForm ("Body of nested action",
561 (dte (WrongForm ("Container of nested action",
569 | ESkip
=> (TAction ((CPrefix (CRoot
, loc
), loc
),
570 SM
.empty
, SM
.empty
), loc
)
572 |
EIf (e1
, e2
, e3
) =>
574 val t1
= checkExp G e1
575 val t2
= checkExp G e2
576 val t3
= checkExp G e3
577 val bool = (TBase
"bool", loc
)
581 dte (WrongType ("\"if\" test",
586 (subTyp (t2
, t3
); t3
)
588 ((subTyp (t3
, t2
); t2
)
590 (dte (WrongType ("\"else\" case",
601 fun ununif (tAll
as (t
, loc
)) =
604 | TList t
=> (TList (ununif t
), loc
)
605 |
TArrow (d
, r
) => (TArrow (ununif d
, ununif r
), loc
)
606 |
TAction (p
, d
, r
) => (TAction (p
, SM
.map ununif d
, SM
.map ununif r
), loc
)
607 |
TUnif (_
, ref (SOME t
)) => ununif t
611 |
TUnif (_
, ref NONE
) => raise Ununif
613 fun hasError (t
, _
) =
616 | TList t
=> hasError t
617 |
TArrow (d
, r
) => hasError d
orelse hasError r
618 |
TAction (p
, d
, r
) => List.exists
hasError (SM
.listItems d
)
619 orelse List.exists
hasError (SM
.listItems r
)
622 |
TUnif (_
, ref (SOME t
)) => hasError t
623 |
TUnif (_
, ref NONE
) => false
626 fun checkUnit
G (eAll
as (_
, loc
)) =
629 val t
= checkExp G eAll
636 (ErrorMsg
.error (SOME loc
) "Unification variables remain in type:";
641 fun checkDecl
G (d
, _
, loc
) =
647 (ErrorMsg
.error (SOME loc
) "'extern type' not allowed in untrusted code";
649 |
DExternVal (name
, t
) =>
651 bindVal
G (name
, checkTyp G t
, NONE
)
653 (ErrorMsg
.error (SOME loc
) "'extern val' not allowed in untrusted code";
655 |
DVal (name
, to
, e
) =>
659 NONE
=> (newUnif (), loc
)
660 | SOME to
=> checkTyp G to
666 Describe
.describe_type_error loc
667 (WrongType ("Bound value",
672 bindVal
G (name
, to
, SOME e
)
674 | DContext name
=> bindContext G name
676 fun printActionDiffs
{have
, need
} =
677 case (ununif have
, ununif need
) of
678 ((TAction (p1
, in1
, out1
), loc
), (TAction (p2
, in2
, out2
), _
)) =>
681 if predImplies (p1
, p2
) then
684 (ErrorMsg
.error (SOME loc
) "Files provides the wrong kind of configuration.";
685 preface ("Have:", p_pred p1
);
686 preface ("Need:", p_pred p2
))
689 SM
.appi (fn (name
, t
) =>
690 case SM
.find (in2
, name
) of
691 NONE
=> (ErrorMsg
.error (SOME loc
) "An unavailable environment variable is used.";
692 print ("Name: " ^ name ^
"\n");
693 preface ("Type:", p_typ t
))
697 (ErrorMsg
.error (SOME loc
) "Wrong type for environment variable.";
698 print (" Name: " ^ name ^
"\n");
699 preface (" Has type:", p_typ t
');
700 preface ("Used with type:", p_typ t
)))
704 SM
.appi (fn (name
, t
) =>
705 case SM
.find (out1
, name
) of
706 NONE
=> (ErrorMsg
.error (SOME loc
) "Missing an output environment variable.";
707 print ("Name: " ^ name ^
"\n");
708 preface ("Type:", p_typ t
))
712 (ErrorMsg
.error (SOME loc
) "Wrong type for output environment variable.";
713 print (" Name: " ^ name ^
"\n");
714 preface (" Has type:", p_typ t
');
715 preface ("Need type:", p_typ t
)))
726 fun checkFile G
tInit (_
, ds
, eo
) =
728 val G
' = foldl (fn (d
, G
) => checkDecl G d
) G ds
732 |
SOME (e
as (_
, loc
)) =>
734 val t
= checkExp G
' e
738 if printActionDiffs
{have
= t
, need
= tInit
} then
741 ErrorMsg
.error (SOME loc
) "File ends in something that isn't a directive."