Skip to content

Commit 4d0e03f

Browse files
committed
Change to disallow rec type aliases
For example, rec {type t} => {type t = t} is no longer allowed. The check is overly restrictive.
1 parent ffec606 commit 4d0e03f

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

elab.ml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,10 +563,16 @@ Trace.debug (lazy ("[UnwrapE] s2 = " ^ string_of_norm_extyp s2));
563563
let t2, zs2 = elab_pathexp env1 exp1 l in
564564
Trace.debug (lazy ("[RecT] s1 = " ^ string_of_norm_extyp s1));
565565
Trace.debug (lazy ("[RecT] t2 = " ^ string_of_norm_typ t2));
566+
let vts1 = varTs aks1 in
566567
let ts, zs3, e =
567-
try sub_typ env1 t2 t1 (varTs aks1) with Sub e -> error typ.at
568+
try sub_typ env1 t2 t1 vts1 with Sub e -> error typ.at
568569
("recursive type does not match annotation: " ^ Sub.string_of_error e)
569570
in
571+
ts
572+
|> List.iter (fun t ->
573+
let t = norm_typ t in
574+
if List.exists (fun t' -> t' = t) vts1 then
575+
error typ.at "illegal recursive type alias");
570576
Trace.debug (lazy ("[RecT] ts = " ^ String.concat ", " (List.map string_of_norm_typ ts)));
571577
let t3, k3 = try make_rec_typ t1 with Recursive ->
572578
error typ.at "illegal type for recursive expression" in

regression.1ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,12 @@ Hungry = {
298298
do eater <+ 1 <+ 2
299299
}
300300

301+
type_error rec {type t _, type u _} => {
302+
type t x = u x
303+
type u x = int
304+
}
305+
type_error type rec t => t
306+
301307
PolyRec = {
302308
type l a = rec (type t) => a | t
303309
...rec {type t a} => {type t a = a | t (a, a)}

types.ml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,14 @@ let rec norm_typ = function
335335
| FunT(aks, t, s, f) -> FunT(aks, norm_typ t, norm_extyp s, f)
336336
| TypT(s) -> TypT(norm_extyp s)
337337
| WrapT(s) -> WrapT(norm_extyp s)
338-
| LamT(aks, t) -> LamT(aks, norm_typ t)
338+
| LamT(aks, t) ->
339+
(match norm_typ t with
340+
| AppT(f, ts)
341+
when List.for_all2 (fun (a, k) t -> t = VarT (a, k)) aks ts &&
342+
List.for_all (fun (a, k) -> not (contains_typ a f)) aks ->
343+
f
344+
| t -> LamT(aks, t)
345+
)
339346
| AppT(t, ts) ->
340347
(match norm_typ t with
341348
| LamT(aks, t') -> norm_typ (subst_typ (subst aks ts) t')

0 commit comments

Comments
 (0)