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 SS
= StringSet
26 structure SM
= StringMap
28 type env
= SS
.set
* typ SM
.map
29 val empty
: env
= (SS
.add (SS
.singleton
"int", "string"),
32 fun lookupType (ts
, _
) name
= SS
.member (ts
, name
)
33 fun lookupVal (_
, vs
) name
= SM
.find (vs
, name
)
35 fun bindType (ts
, vs
) name
= (SS
.add (ts
, name
), vs
)
36 fun bindVal (ts
, vs
) (name
, t
) = (ts
, SM
.insert (vs
, name
, t
))
41 fun resetUnif () = unifCount
:= 0
48 str (chr (ord #
"A" + c
))
50 "UNIF" ^
Int.toString (c
- 26)
53 TUnif (name
, ref NONE
)
57 exception UnequalDomains
59 fun eqRecord
f (r1
, r2
) =
60 (SM
.appi (fn (k
, v1
) =>
61 case SM
.find (r2
, k
) of
62 NONE
=> raise UnequalDomains
67 raise UnequalDomains
) r1
;
68 SM
.appi (fn (k
, v2
) =>
69 case SM
.find (r1
, k
) of
70 NONE
=> raise UnequalDomains
75 raise UnequalDomains
) r2
;
77 handle UnequalDomains
=> false
79 fun eqPred ((p1
, _
), (p2
, _
)) =
81 (CRoot
, CRoot
) => true
82 |
(CConst s1
, CConst s2
) => s1
= s2
83 |
(CPrefix p1
, CPrefix p2
) => eqPred (p1
, p2
)
84 |
(CNot p1
, CNot p2
) => eqPred (p1
, p2
)
85 |
(CAnd (p1
, q1
), CAnd (p2
, q2
)) =>
86 eqPred (p1
, p2
) andalso eqPred (q1
, q2
)
90 fun eqTy (t1All
as (t1
, _
), t2All
as (t2
, _
)) =
92 (TBase s1
, TBase s2
) => s1
= s2
93 |
(TList t1
, TList t2
) => eqTy (t1
, t2
)
94 |
(TArrow (d1
, r1
), TArrow (d2
, r2
)) =>
95 eqTy (d1
, d2
) andalso eqTy (r1
, r2
)
97 |
(TAction (p1
, d1
, r1
), TAction (p2
, d2
, r2
)) =>
98 eqPred (p1
, p2
) andalso eqRecord
eqTy (d1
, d2
)
99 andalso eqRecord
eqTy (r1
, r2
)
101 |
(TNested (p1
, q1
), TNested (p2
, q2
)) =>
102 eqPred (p1
, p2
) andalso eqTy (q1
, q2
)
104 |
(TUnif (_
, ref (SOME t1
)), _
) => eqTy (t1
, t2All
)
105 |
(_
, TUnif (_
, ref (SOME t2
))) => eqTy (t1All
, t2
)
107 |
(TUnif (_
, r1
), TUnif (_
, r2
)) => r1
= r2
109 |
(TError
, TError
) => true
113 datatype unification_error
=
114 UnifyPred
of pred
* pred
115 | UnifyTyp
of typ
* typ
116 | UnifyOccurs
of string * typ
118 exception Unify
of unification_error
120 datatype type_error
=
121 WrongType
of string * exp
* typ
* typ
* unification_error option
122 | WrongForm
of string * string * exp
* typ
* unification_error option
123 | UnboundVariable
of string
124 | WrongPred
of string * pred
* pred
126 fun preface (s
, d
) = printd (PD
.hovBox (PD
.PPS
.Rel
0,
127 [PD
.string s
, PD
.space
1, d
]))
129 fun describe_unification_error t ue
=
131 UnifyPred (p1
, p2
) =>
132 (print
"Reason: Incompatible contexts.\n";
133 preface ("Have:", p_pred p1
);
134 preface ("Need:", p_pred p2
))
135 |
UnifyTyp (t1
, t2
) =>
139 (print
"Reason: Incompatible types.\n";
140 preface ("Have:", p_typ t1
);
141 preface ("Need:", p_typ t2
))
142 |
UnifyOccurs (name
, t
') =>
146 (print
"Reason: Occurs check failed for ";
151 fun describe_type_error loc te
=
153 WrongType (place
, e
, t1
, t2
, ueo
) =>
154 (ErrorMsg
.error (SOME loc
) (place ^
" has wrong type.");
155 preface (" Expression:", p_exp e
);
156 preface ("Actual type:", p_typ t1
);
157 preface ("Needed type:", p_typ t2
);
158 Option
.app (describe_unification_error t1
) ueo
)
159 |
WrongForm (place
, form
, e
, t
, ueo
) =>
160 (ErrorMsg
.error (SOME loc
) (place ^
" has a non-" ^ form ^
" type.");
161 preface ("Expression:", p_exp e
);
162 preface (" Type:", p_typ t
);
163 Option
.app (describe_unification_error t
) ueo
)
164 | UnboundVariable name
=>
165 ErrorMsg
.error (SOME loc
) ("Unbound variable " ^ name ^
".\n")
166 |
WrongPred (place
, p1
, p2
) =>
167 (ErrorMsg
.error (SOME loc
) ("Context incompatibility for " ^ place ^
".");
168 preface ("Have:", p_pred p1
);
169 preface ("Need:", p_pred p2
))
171 fun predImplies (p1All
as (p1
, _
), p2All
as (p2
, _
)) =
173 (_
, CAnd (p1
, p2
)) => predImplies (p1All
, p1
) andalso predImplies (p1All
, p2
)
174 |
(CAnd (p1
, p2
), _
) => predImplies (p1
, p2All
) orelse predImplies (p2
, p2All
)
176 |
(_
, CPrefix (CRoot
, _
)) => true
177 |
(CNot (CPrefix (CRoot
, _
), _
), _
) => true
179 |
(CRoot
, CRoot
) => true
181 |
(CConst s1
, CConst s2
) => s1
= s2
183 |
(CPrefix p1
, CPrefix p2
) => predImplies (p1
, p2
)
184 |
(_
, CPrefix p2
) => predImplies (p1All
, p2
)
186 |
(CNot p1
, CNot p2
) => predImplies (p2
, p1
)
190 fun predSimpl (pAll
as (p
, loc
)) =
194 | CPrefix p
=> (CPrefix (predSimpl p
), loc
)
195 | CNot p
=> (CNot (predSimpl p
), loc
)
198 val p1
' = predSimpl p1
199 val p2
' = predSimpl p2
202 (CAnd (c1
, c2
), _
) => predSimpl (CAnd (c1
, (CAnd (c2
, p2
'), loc
)), loc
)
203 | _
=> if predImplies (p2
', p1
') then
205 else if predImplies (p1
', p2
') then
208 (CAnd (p1
', p2
'), loc
)
211 fun subPred (p1
, p2
) =
212 if predImplies (p1
, p2
) then
215 raise (Unify (UnifyPred (p1
, p2
)))
217 fun subRecord
f (r1
, r2
) =
218 SM
.appi (fn (k
, v2
) =>
219 case SM
.find (r1
, k
) of
220 NONE
=> raise UnequalDomains
221 | SOME v1
=> f (v1
, v2
)) r2
223 fun occurs
u (t
, _
) =
226 | TList t
=> occurs u t
227 |
TArrow (d
, r
) => occurs u d
orelse occurs u r
228 |
TAction (_
, d
, r
) =>
229 List.exists (occurs u
) (SM
.listItems d
)
230 orelse List.exists (occurs u
) (SM
.listItems r
)
231 |
TNested (_
, t
) => occurs u t
233 |
TUnif (_
, ref (SOME t
)) => occurs u t
234 |
TUnif (_
, u
') => u
= u
'
236 fun subTyp (t1All
as (t1
, _
), t2All
as (t2
, _
)) =
238 (TBase s1
, TBase s2
) =>
242 raise Unify (UnifyTyp (t1All
, t2All
))
243 |
(TList t1
, TList t2
) => subTyp (t1
, t2
)
244 |
(TArrow (d1
, r1
), TArrow (d2
, r2
)) =>
248 |
(TAction (p1
, d1
, r1
), TAction (p2
, d2
, r2
)) =>
250 subRecord
subTyp (d2
, d1
);
251 subRecord
subTyp (r1
, r2
);
252 subRecord
subTyp (r2
, r1
))
253 handle UnequalDomains
=> raise Unify (UnifyTyp (t1All
, t2All
)))
255 |
(TNested (d1
, r1
), TNested (d2
, r2
)) =>
259 |
(TUnif (_
, ref (SOME t1
)), _
) => subTyp (t1
, t2All
)
260 |
(_
, TUnif (_
, ref (SOME t2
))) => subTyp (t1All
, t2
)
262 |
(TUnif (_
, r1
), TUnif (_
, r2
)) =>
268 |
(TUnif (name
, r
), _
) =>
269 if occurs r t2All
then
270 raise (Unify (UnifyOccurs (name
, t2All
)))
274 |
(_
, TUnif (name
, r
)) =>
275 if occurs r t1All
then
276 raise (Unify (UnifyOccurs (name
, t1All
)))
283 | _
=> raise Unify (UnifyTyp (t1All
, t2All
))
290 fun whnorm (tAll
as (t
, loc
)) =
292 TUnif (_
, ref (SOME tAll
)) => whnorm tAll
295 fun checkTyp
G (tAll
as (t
, loc
)) =
297 val err
= ErrorMsg
.error (SOME loc
)
301 if lookupType G name
then
304 (err ("Unbound type name " ^ name
);
306 | TList t
=> (TList (checkTyp G t
), loc
)
307 |
TArrow (d
, r
) => (TArrow (checkTyp G d
, checkTyp G r
), loc
)
308 |
TAction (p
, d
, r
) => (TAction (p
, SM
.map (checkTyp G
) d
,
309 SM
.map (checkTyp G
) r
), loc
)
310 |
TNested (p
, t
) => (TNested (p
, checkTyp G t
), loc
)
311 | TError
=> raise Fail
"TError in parser-generated type"
312 | TUnif _
=> raise Fail
"TUnif in parser-generated type"
315 fun checkExp
G (eAll
as (e
, loc
)) =
317 val dte
= describe_type_error loc
320 EInt _
=> (TBase
"int", loc
)
321 | EString _
=> (TBase
"string", loc
)
324 val t
= (newUnif (), loc
)
326 foldl (fn (e
', ret
) =>
328 val t
' = checkExp G e
'
332 (TList (TError
, loc
), loc
)
336 (dte (WrongType ("List element",
342 end) (TList t
, loc
) es
349 NONE
=> (newUnif (), loc
)
350 | SOME t
=> checkTyp G t
352 val G
' = bindVal
G (x
, t
)
353 val t
' = checkExp G
' e
355 (TArrow (t
, t
'), loc
)
358 (case lookupVal G x
of
359 NONE
=> (dte (UnboundVariable x
);
362 |
EApp (func
, arg
) =>
364 val dom
= (newUnif (), loc
)
365 val ran
= (newUnif (), loc
)
367 val tf
= checkExp G func
368 val ta
= checkExp G arg
370 (subTyp (tf
, (TArrow (dom
, ran
), loc
));
373 dte (WrongType ("Function argument",
380 (dte (WrongForm ("Function to be applied",
392 (TAction ((CPrefix (CRoot
, loc
), loc
),
394 SM
.insert (SM
.empty
, evar
, t
)),
397 |
EGet (x
, evar
, rest
) =>
399 val xt
= (newUnif (), loc
)
400 val G
' = bindVal
G (x
, xt
)
402 val rt
= whnorm (checkExp G
' rest
)
405 (TAction (p
, d
, r
), _
) =>
406 (case SM
.find (d
, evar
) of
407 NONE
=> (TAction (p
, SM
.insert (d
, evar
, xt
), r
), loc
)
411 dte (WrongType ("Retrieved environment variable",
418 | _
=> (dte (WrongForm ("Body of environment variable read",
426 | ESeq
[] => raise Fail
"Empty ESeq"
427 | ESeq
[e1
] => checkExp G e1
428 |
ESeq (e1
:: rest
) =>
430 val e2
= (ESeq rest
, loc
)
432 val t1
= whnorm (checkExp G e1
)
433 val t2
= whnorm (checkExp G e2
)
436 (TAction (p1
, d1
, r1
), _
) =>
438 (TAction (p2
, d2
, r2
), _
) =>
440 val p
' = predSimpl (CAnd (p1
, p2
), loc
)
442 val d
' = SM
.foldli (fn (name
, t
, d
') =>
443 case SM
.find (r1
, name
) of
445 (case SM
.find (d
', name
) of
446 NONE
=> SM
.insert (d
', name
, t
)
450 dte (WrongType ("Shared environment variable",
459 dte (WrongType ("Shared environment variable",
467 val r
' = SM
.foldli (fn (name
, t
, r
') => SM
.insert (r
', name
, t
))
470 (TAction (p
', d
', r
'), loc
)
473 | _
=> (dte (WrongForm ("Action to be sequenced",
480 | _
=> (dte (WrongForm ("Action to be sequenced",
490 val t1
= whnorm (checkExp G e1
)
491 val t2
= whnorm (checkExp G e2
)
494 (TAction (p1
, d1
, r1
), _
) =>
496 (TAction (p2
, d2
, r2
), _
) =>
498 val p
' = predSimpl (CAnd (p1
, p2
), loc
)
500 val d
' = SM
.foldli (fn (name
, t
, d
') =>
501 case SM
.find (r1
, name
) of
503 (case SM
.find (d
', name
) of
504 NONE
=> SM
.insert (d
', name
, t
)
508 dte (WrongType ("Shared environment variable",
517 dte (WrongType ("Shared environment variable",
525 (TAction (p
', d
', r2
), loc
)
528 | _
=> (dte (WrongForm ("Action to be sequenced",
535 | _
=> (dte (WrongForm ("Action to be sequenced",
546 val t1
= whnorm (checkExp G e1
)
547 val t2
= whnorm (checkExp G e2
)
550 (TNested (pd
, (TAction (pr
, d1
, r1
), _
)), _
) =>
552 (TAction (p
, d
, r
), _
) =>
553 if predImplies (pd
, p
) then
556 SM
.unionWithi (fn (name
, t1
, t2
) =>
559 dte (WrongType ("Environment variable",
566 (TAction (pr
, combineRecs (d
, d1
),
567 combineRecs (r
, r1
)), loc
)
570 (dte (WrongPred ("nested action",
576 (dte (WrongForm ("Body of nested action",
584 (dte (WrongForm ("Container of nested action",
592 | ESkip
=> (TAction ((CPrefix (CRoot
, loc
), loc
),
593 SM
.empty
, SM
.empty
), loc
)
598 fun ununif (tAll
as (t
, loc
)) =
601 | TList t
=> (TList (ununif t
), loc
)
602 |
TArrow (d
, r
) => (TArrow (ununif d
, ununif r
), loc
)
603 |
TAction (p
, d
, r
) => (TAction (p
, SM
.map ununif d
, SM
.map ununif r
), loc
)
604 |
TUnif (_
, ref (SOME t
)) => ununif t
608 |
TUnif (_
, ref NONE
) => raise Ununif
610 fun hasError (t
, _
) =
613 | TList t
=> hasError t
614 |
TArrow (d
, r
) => hasError d
orelse hasError r
615 |
TAction (p
, d
, r
) => List.exists
hasError (SM
.listItems d
)
616 orelse List.exists
hasError (SM
.listItems r
)
619 |
TUnif (_
, ref (SOME t
)) => hasError t
620 |
TUnif (_
, ref NONE
) => false
623 fun checkUnit
G (eAll
as (_
, loc
)) =
626 val t
= checkExp G eAll
633 (ErrorMsg
.error (SOME loc
) "Unification variables remain in type:";
638 fun checkDecl
G (d
, _
, loc
) =
640 DExternType name
=> bindType G name
641 |
DExternVal (name
, t
) => bindVal
G (name
, checkTyp G t
)
643 fun checkFile G
tInit (ds
, eo
) =
645 val G
' = foldl (fn (d
, G
) => checkDecl G d
) G ds
649 |
SOME (e
as (_
, loc
)) =>
651 val t
= checkExp G
' e
655 (ErrorMsg
.error (SOME loc
) "Bad type for final expression of source file.";
656 preface ("Actual:", p_typ t
);
657 preface ("Needed:", p_typ tInit
))