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
33 fun resetUnif () = unifCount
:= 0
40 str (chr (ord #
"A" + c
))
42 "UNIF" ^
Int.toString (c
- 26)
45 TUnif (name
, ref NONE
)
49 exception UnequalDomains
51 fun eqRecord
f (r1
, r2
) =
52 (SM
.appi (fn (k
, v1
) =>
53 case SM
.find (r2
, k
) of
54 NONE
=> raise UnequalDomains
59 raise UnequalDomains
) r1
;
60 SM
.appi (fn (k
, v2
) =>
61 case SM
.find (r1
, k
) of
62 NONE
=> raise UnequalDomains
67 raise UnequalDomains
) r2
;
69 handle UnequalDomains
=> false
71 fun eqPred ((p1
, _
), (p2
, _
)) =
73 (CRoot
, CRoot
) => true
74 |
(CConst s1
, CConst s2
) => s1
= s2
75 |
(CPrefix p1
, CPrefix p2
) => eqPred (p1
, p2
)
76 |
(CNot p1
, CNot p2
) => eqPred (p1
, p2
)
77 |
(CAnd (p1
, q1
), CAnd (p2
, q2
)) =>
78 eqPred (p1
, p2
) andalso eqPred (q1
, q2
)
82 fun eqTy (t1All
as (t1
, _
), t2All
as (t2
, _
)) =
84 (TBase s1
, TBase s2
) => s1
= s2
85 |
(TList t1
, TList t2
) => eqTy (t1
, t2
)
86 |
(TArrow (d1
, r1
), TArrow (d2
, r2
)) =>
87 eqTy (d1
, d2
) andalso eqTy (r1
, r2
)
89 |
(TAction (p1
, d1
, r1
), TAction (p2
, d2
, r2
)) =>
90 eqPred (p1
, p2
) andalso eqRecord
eqTy (d1
, d2
)
91 andalso eqRecord
eqTy (r1
, r2
)
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
114 fun preface (s
, d
) = printd (PD
.hovBox (PD
.PPS
.Rel
0,
115 [PD
.string s
, PD
.space
1, d
]))
117 fun describe_unification_error t ue
=
119 UnifyPred (p1
, p2
) =>
120 (print
"Reason: Incompatible predicates.\n";
121 preface ("Have:", p_pred p1
);
122 preface ("Need:", p_pred p2
))
123 |
UnifyTyp (t1
, t2
) =>
127 (print
"Reason: Incompatible types.\n";
128 preface ("Have:", p_typ t1
);
129 preface ("Need:", p_typ t2
))
130 |
UnifyOccurs (name
, t
') =>
134 (print
"Reason: Occurs check failed for ";
139 fun describe_type_error loc te
=
141 WrongType (place
, e
, t1
, t2
, ueo
) =>
142 (ErrorMsg
.error (SOME loc
) (place ^
" has wrong type.");
143 preface (" Expression:", p_exp e
);
144 preface ("Actual type:", p_typ t1
);
145 preface ("Needed type:", p_typ t2
);
146 Option
.app (describe_unification_error t1
) ueo
)
147 |
WrongForm (place
, form
, e
, t
, ueo
) =>
148 (ErrorMsg
.error (SOME loc
) (place ^
" has a non-" ^ form ^
" type.");
149 preface ("Expression:", p_exp e
);
150 preface (" Type:", p_typ t
);
151 Option
.app (describe_unification_error t
) ueo
)
152 | UnboundVariable name
=>
153 ErrorMsg
.error (SOME loc
) ("Unbound variable " ^ name ^
".\n")
155 fun predImplies (p1All
as (p1
, _
), p2All
as (p2
, _
)) =
157 (_
, CPrefix (CRoot
, _
)) => true
158 |
(CNot (CPrefix (CRoot
, _
), _
), _
) => true
160 |
(CRoot
, CRoot
) => true
162 |
(CConst s1
, CConst s2
) => s1
= s2
164 |
(CPrefix p1
, CPrefix p2
) => predImplies (p1
, p2
)
166 |
(CNot p1
, CNot p2
) => predImplies (p2
, p1
)
168 |
(_
, CAnd (p1
, p2
)) => predImplies (p1All
, p1
) andalso predImplies (p1All
, p2
)
169 |
(CAnd (p1
, p2
), _
) => predImplies (p1
, p2All
) orelse predImplies (p2
, p2All
)
173 fun predSimpl (pAll
as (p
, loc
)) =
177 | CPrefix p
=> (CPrefix (predSimpl p
), loc
)
178 | CNot p
=> (CNot (predSimpl p
), loc
)
181 val p1
' = predSimpl p1
182 val p2
' = predSimpl p2
185 (CAnd (c1
, c2
), _
) => predSimpl (CAnd (c1
, (CAnd (c2
, p2
'), loc
)), loc
)
186 | _
=> if predImplies (p2
', p1
') then
189 (CAnd (p1
', p2
'), loc
)
192 fun unifyPred (p1
, p2
) =
193 if predImplies (p1
, p2
) then
196 raise (Unify (UnifyPred (p1
, p2
)))
198 fun unifyRecord
f (r1
, r2
) =
199 (SM
.appi (fn (k
, v1
) =>
200 case SM
.find (r2
, k
) of
201 NONE
=> raise UnequalDomains
202 | SOME v2
=> f (v1
, v2
)) r1
;
203 SM
.appi (fn (k
, v2
) =>
204 case SM
.find (r1
, k
) of
205 NONE
=> raise UnequalDomains
206 | SOME v1
=> f (v1
, v2
)) r2
)
208 fun occurs
u (t
, _
) =
211 | TList t
=> occurs u t
212 |
TArrow (d
, r
) => occurs u d
orelse occurs u r
213 |
TAction (_
, d
, r
) =>
214 List.exists (occurs u
) (SM
.listItems d
)
215 orelse List.exists (occurs u
) (SM
.listItems r
)
217 |
TUnif (_
, ref (SOME t
)) => occurs u t
218 |
TUnif (_
, u
') => u
= u
'
220 fun unify (t1All
as (t1
, _
), t2All
as (t2
, _
)) =
222 (TBase s1
, TBase s2
) =>
226 raise Unify (UnifyTyp (t1All
, t2All
))
227 |
(TList t1
, TList t2
) => unify (t1
, t2
)
228 |
(TArrow (d1
, r1
), TArrow (d2
, r2
)) =>
232 |
(TAction (p1
, d1
, r1
), TAction (p2
, d2
, r2
)) =>
233 ((unifyPred (p1
, p2
);
234 unifyRecord
unify (d1
, d2
);
235 unifyRecord
unify (r1
, r2
))
236 handle UnequalDomains
=> raise Unify (UnifyTyp (t1All
, t2All
)))
238 |
(TUnif (_
, ref (SOME t1
)), _
) => unify (t1
, t2All
)
239 |
(_
, TUnif (_
, ref (SOME t2
))) => unify (t1All
, t2
)
241 |
(TUnif (_
, r1
), TUnif (_
, r2
)) =>
247 |
(TUnif (name
, r
), _
) =>
248 if occurs r t2All
then
249 raise (Unify (UnifyOccurs (name
, t2All
)))
253 |
(_
, TUnif (name
, r
)) =>
254 if occurs r t1All
then
255 raise (Unify (UnifyOccurs (name
, t1All
)))
262 | _
=> raise Unify (UnifyTyp (t1All
, t2All
))
269 fun whnorm (tAll
as (t
, loc
)) =
271 TUnif (_
, ref (SOME tAll
)) => whnorm tAll
274 fun checkExp
G (eAll
as (e
, loc
)) =
276 val dte
= describe_type_error loc
279 EInt _
=> (TBase
"int", loc
)
280 | EString _
=> (TBase
"string", loc
)
283 val t
= (newUnif (), loc
)
285 foldl (fn (e
', ret
) =>
287 val t
' = checkExp G e
'
291 (TList (TError
, loc
), loc
)
295 (dte (WrongType ("List element",
301 end) (TList t
, loc
) es
308 NONE
=> (newUnif (), loc
)
311 val G
' = SM
.insert (G
, x
, t
)
312 val t
' = checkExp G
' e
314 (TArrow (t
, t
'), loc
)
317 (case SM
.find (G
, x
) of
318 NONE
=> (dte (UnboundVariable x
);
321 |
EApp (func
, arg
) =>
323 val dom
= (newUnif (), loc
)
324 val ran
= (newUnif (), loc
)
326 val tf
= checkExp G func
327 val ta
= checkExp G arg
329 (unify (tf
, (TArrow (dom
, ran
), loc
));
332 dte (WrongType ("Function argument",
339 (dte (WrongForm ("Function to be applied",
351 (TAction ((CPrefix (CRoot
, loc
), loc
),
353 SM
.insert (SM
.empty
, evar
, t
)),
356 |
EGet (x
, evar
, rest
) =>
358 val xt
= (newUnif (), loc
)
359 val G
' = SM
.insert (G
, x
, xt
)
361 val rt
= whnorm (checkExp G
' rest
)
364 (TAction (p
, d
, r
), _
) =>
365 (case SM
.find (d
, evar
) of
366 NONE
=> (TAction (p
, SM
.insert (d
, evar
, xt
), r
), loc
)
370 dte (WrongType ("Retrieved environment variable",
376 | _
=> (dte (WrongForm ("Body of environment variable read",
384 | ESeq
[] => raise Fail
"Empty ESeq"
385 | ESeq
[e1
] => checkExp G e1
386 |
ESeq (e1
:: rest
) =>
388 val e2
= (ESeq rest
, loc
)
390 val t1
= whnorm (checkExp G e1
)
391 val t2
= whnorm (checkExp G e2
)
394 (TAction (p1
, d1
, r1
), _
) =>
396 (TAction (p2
, d2
, r2
), _
) =>
398 val p
' = predSimpl (CAnd (p1
, p2
), loc
)
400 val d
' = SM
.foldli (fn (name
, t
, d
') =>
401 case SM
.find (r1
, name
) of
403 (case SM
.find (d
', name
) of
404 NONE
=> SM
.insert (d
', name
, t
)
408 dte (WrongType ("Shared environment variable",
417 dte (WrongType ("Shared environment variable",
425 val r
' = SM
.foldli (fn (name
, t
, r
') => SM
.insert (r
', name
, t
))
428 (TAction (p
', d
', r
'), loc
)
430 | _
=> (dte (WrongForm ("Action to be sequenced",
436 | _
=> (dte (WrongForm ("Action to be sequenced",
446 val rt
= whnorm (checkExp G e
)
449 (TAction (p
, d
, _
), _
) =>
450 (TAction (p
, d
, SM
.empty
), loc
)
451 | _
=> (dte (WrongForm ("Body of local action",
462 fun ununif (tAll
as (t
, loc
)) =
465 | TList t
=> (TList (ununif t
), loc
)
466 |
TArrow (d
, r
) => (TArrow (ununif d
, ununif r
), loc
)
467 |
TAction (p
, d
, r
) => (TAction (p
, SM
.map ununif d
, SM
.map ununif r
), loc
)
468 |
TUnif (_
, ref (SOME t
)) => ununif t
471 |
TUnif (_
, ref NONE
) => raise Ununif
473 fun hasError (t
, _
) =
476 | TList t
=> hasError t
477 |
TArrow (d
, r
) => hasError d
orelse hasError r
478 |
TAction (p
, d
, r
) => List.exists
hasError (SM
.listItems d
)
479 orelse List.exists
hasError (SM
.listItems r
)
481 |
TUnif (_
, ref (SOME t
)) => hasError t
482 |
TUnif (_
, ref NONE
) => false
485 fun checkUnit
G (eAll
as (_
, loc
)) =
488 val t
= checkExp G eAll
495 (ErrorMsg
.error (SOME loc
) "Unification variables remain in type:";