Skip to content

Commit caebe29

Browse files
committed
Recursively dependent signatures
1 parent 02f56cb commit caebe29

6 files changed

Lines changed: 194 additions & 33 deletions

File tree

elab.ml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,18 @@ Trace.debug (lazy ("[WithT] aks12 = " ^ string_of_norm_extyp (ExT(aks12, StrT []
270270
ExT(aks11, subst_typ (subst aks12 ts) t1),
271271
lift_warn typ.at t1 (add_typs aks11 env) (zs1 @ zs2 @ zs3)
272272

273+
| EL.RecT(var, typ1, typ2) ->
274+
let ExT(aks1, t1), zs1 = elab_typ env typ1 l in
275+
let env1 = add_val var.it t1 (add_typs aks1 env) in
276+
let ExT(aks2, t2), zs2 = elab_typ env1 typ2 l in
277+
let ts, zs3, e =
278+
try sub_typ env1 t2 t1 (varTs aks1) with Sub e -> error typ.at
279+
("recursive type does not match annotation: " ^ Sub.string_of_error e)
280+
in
281+
let aks' = freshen_vars env aks1 in
282+
let t3 = subst_typ (subst aks1 (varTs aks')) t2 in
283+
ExT(aks', t3), lift_warn typ.at t3 env (zs1 @ zs2 @ zs3)
284+
273285
and elab_dec env dec l =
274286
Trace.elab (lazy ("[elab_dec] " ^ EL.label_of_dec dec));
275287
(fun (s, zs) -> dec.at, env, s, zs, None, EL.label_of_dec dec) <<<

regression.1ml

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
type RDS = rec (R: let type T'1 = {type t _} in {A: T'1; B: T'1}) => let
2+
type ONE (type this _) (type that _) = {
3+
t = this;
4+
aThis 'a: this a;
5+
ofThat 'a: that a -> this a;
6+
};
7+
in {
8+
A: ONE R.A.t R.B.t;
9+
B: ONE R.B.t R.A.t;
10+
};
11+
12+
Rds :> RDS = let
13+
One = {
14+
type t _ = ();
15+
aThis = ();
16+
ofThat _ = ();
17+
};
18+
in {
19+
A = One;
20+
B = One;
21+
};
22+
23+
;;
24+
125
Equivalence: {
226
type t a b;
327

@@ -60,13 +84,29 @@ type_error rec (R: {}) => {
6084
kaboom () = R;
6185
};
6286

63-
Kaboom = rec (R: rec R => {kaboom: () -> R}) => @(= R) {
64-
kaboom () = R;
65-
};
87+
;;Kaboom = rec (R: rec R => {kaboom: () -> R}) => @(= R) {
88+
;; kaboom () = R;
89+
;;};
6690

6791
;;
6892

69-
Mutually = let
93+
type MUTUALLY = rec (R: {
94+
Even: {type t _};
95+
Odd: {type t _};
96+
}) => {
97+
Even: {
98+
t = R.Even.t;
99+
make 'x: x => R.Odd.t x => R.Even.t x;
100+
size 'x: R.Even.t x -> int;
101+
};
102+
Odd: {
103+
t = R.Odd.t;
104+
make 'x: opt (R.Even.t x) => R.Odd.t x;
105+
size 'x: R.Odd.t x -> int;
106+
};
107+
};
108+
109+
Mutually :> MUTUALLY = let
70110
T = rec (R: {
71111
Even: {type t _};
72112
Odd: {type t _};
@@ -109,7 +149,7 @@ in {
109149
;;
110150

111151
Hungry = {
112-
type eat a = rec eat_a => a -> eat_a;
152+
...rec {type eat a} => {type eat a = a -> eat a};
113153

114154
eater 'a: eat a = rec (eater: eat a) => @(eat a) (fun a => eater);
115155

@@ -119,7 +159,7 @@ Hungry = {
119159
};
120160

121161
PolyRec = {
122-
type l a = rec (type t) => alt a t;
162+
...rec {type l a} => {type l a = alt a (l a)};
123163
...rec {type t a} => {type t a = alt a (t (type (a, a)))};
124164

125165
t_int = t int;

sub.ml

Lines changed: 117 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,25 @@ let lift_warn at t env zs =
9191

9292
(* Subtyping *)
9393

94+
let rec has_typs_typ ps = function
95+
| VarT(_) -> false
96+
| PrimT(_) -> false
97+
| StrT(tr) -> List.exists (fun (_, t) -> has_typs_typ ps t) tr
98+
| FunT(aks, td, ExT(_, tr), e) ->
99+
(match e with
100+
| Implicit | Explicit Pure ->
101+
has_typs_typ (List.map (fun p -> AppT(p, varTs aks)) ps) tr
102+
| Explicit Impure -> false)
103+
| TypT(ExT(_, p)) -> List.mem p ps
104+
| WrapT(_) -> false
105+
| LamT(_) -> false
106+
| AppT(_) -> false
107+
| TupT(_) -> false
108+
| DotT(_) -> false
109+
| RecT(_) -> false
110+
| InferT(_) -> false
111+
112+
94113
let resolve_typ z t =
95114
Trace.sub (lazy ("[resolve_typ] z = " ^ string_of_norm_typ (InferT(z))));
96115
Trace.sub (lazy ("[resolve_typ] t = " ^ string_of_norm_typ t));
@@ -101,12 +120,36 @@ let unify_typ t1 t2 =
101120
Trace.sub (lazy ("[unify_typ] t2 = " ^ string_of_norm_typ t2));
102121
unify_typ t1 t2
103122

123+
let rec psubst p t =
124+
match p with
125+
| VarT(a, k) -> a, t
126+
| AppT(p', ts) -> psubst p' (LamT(List.map unvarT ts, t))
127+
| _ -> assert false
128+
104129

105130
let rec sub_typ env t1 t2 ps =
106131
Trace.sub (lazy ("[sub_typ] t1 = " ^ string_of_norm_typ t1));
107132
Trace.sub (lazy ("[sub_typ] t2 = " ^ string_of_norm_typ t2));
108133
Trace.sub (lazy ("[sub_typ] ps = " ^
109134
String.concat ", " (List.map string_of_norm_typ ps)));
135+
let ts', zs', t2, ps =
136+
if ps <> [] then
137+
let su, zs' = match_typ env t1 t2 ps in
138+
Trace.sub (lazy ("[sub_typ] su = " ^
139+
String.concat ", " (List.map (fun (p, t) -> Printf.sprintf "[= %s] - [= %s]\n" (string_of_typ p) (string_of_typ t)) su)));
140+
let t2 = subst_typ (List.map (fun (p, t) -> psubst p t) su) t2 in
141+
let ps' = List.filter (fun p -> List.mem_assoc p su) ps in
142+
let ts' = List.map (fun p -> List.assoc p su) ps' in
143+
let ps = List.filter (fun p -> not (List.mem_assoc p su)) ps in
144+
if ps <> [] then begin
145+
Trace.sub (lazy ("[sub_typ] unmatched ps = " ^
146+
String.concat ", " (List.map string_of_norm_typ ps)));
147+
raise Not_found
148+
end;
149+
ts', zs', t2, ps
150+
else
151+
[], [], t2, ps
152+
in
110153
let e1 = IL.VarE("x") in
111154
let ts, zs, e =
112155
match norm_typ t1, freshen_typ env (norm_typ t2) with
@@ -267,7 +310,7 @@ let rec sub_typ env t1 t2 ps =
267310
Trace.sub (lazy ("[sub_typ] done ts = " ^
268311
String.concat ", " (List.map string_of_norm_typ ts)));
269312
Trace.sub (lazy ("[sub_typ] done x -> " ^ IL.string_of_exp e));
270-
ts, zs, IL.LamE("x", erase_typ t1, e)
313+
ts' @ ts, zs' @ zs, IL.LamE("x", erase_typ t1, e)
271314

272315
and sub_extyp env s1 s2 ps =
273316
Trace.sub (lazy ("[sub_extyp] s1 = " ^ string_of_norm_extyp s1));
@@ -290,18 +333,11 @@ and sub_row env tr1 tr2 ps =
290333
| [] ->
291334
[], [], []
292335
| (l, t2)::tr2' ->
293-
Trace.sub (lazy ("[sub_row] l = " ^ l));
294336
let ts1, zs1, f =
295337
try sub_typ env (List.assoc l tr1) t2 ps with
296338
| Not_found -> raise (Sub (Struct(l, Missing)))
297339
| Sub e -> raise (Sub (Struct(l, e)))
298340
in
299-
let rec psubst p t =
300-
match p with
301-
| VarT(a, k) -> a, t
302-
| AppT(p', ts) -> psubst p' (LamT(List.map unvarT ts, t))
303-
| _ -> assert false
304-
in
305341
let su = List.map2 psubst (Lib.List.take (List.length ts1) ps) ts1 in
306342
let ps' = Lib.List.drop (List.length ts1) ps in
307343
let ts2, zs2, fs = sub_row env tr1 (subst_row su tr2') ps' in
@@ -331,3 +367,76 @@ and equal_row env tr1 tr2 ps =
331367
let _, zs2, _ =
332368
try sub_row env tr2 tr1 ps with Sub e -> raise (Sub (Right e)) in
333369
zs1 @ zs2
370+
371+
and match_typ env t1 t2 ps =
372+
let t2 = norm_typ t2 in
373+
Trace.sub (lazy ("[match_typ] t1 = " ^ string_of_norm_typ t1));
374+
Trace.sub (lazy ("[match_typ] t2 = " ^ string_of_typ t2));
375+
Trace.sub (lazy ("[match_typ] ps = " ^
376+
String.concat ", " (List.map string_of_norm_typ ps)));
377+
if not (has_typs_typ ps t2) then [], [] else
378+
match norm_typ t1, freshen_typ env t2 with
379+
| t1, FunT(aks21, t21, ExT(aks22, t22), Implicit) ->
380+
assert (aks22 = []);
381+
let su, zs = match_typ (add_typs aks21 env) t1 t22 ps in
382+
List.map (fun (p, t) -> (p, LamT(aks21, t))) su, lift env zs
383+
384+
| FunT(aks11, t11, ExT(aks12, t12), Implicit), t2 ->
385+
assert (aks12 = []);
386+
let ts1, zs1 = guess_typs (Env.domain_typ env) aks11 in
387+
let t1' = subst_typ (subst aks11 ts1) t12 in
388+
let su, zs2 = match_typ env t1' t2 ps in
389+
su, zs1 @ zs2
390+
391+
| TypT(s1), TypT(s2) ->
392+
(match s1, s2 with
393+
| ExT(aks1, t), ExT([], p) when List.mem p ps ->
394+
if aks1 <> [] || not (!undecidable_flag || is_small_typ t) then
395+
raise (Sub (Mismatch (t1, t2)));
396+
[(p, t)], []
397+
| _ ->
398+
[], [])
399+
400+
| StrT(tr1), StrT(tr2) ->
401+
match_row env tr1 tr2 ps
402+
403+
| FunT(aks1, t11, s1, Explicit p1), FunT(aks2, t21, s2, Explicit p2) ->
404+
if p1 = Impure && p2 = Pure then raise (Sub (FunEffect(p1, p2)));
405+
if p1 <> Pure || p2 <> Pure then
406+
[], []
407+
else
408+
let env' = add_typs aks2 env in
409+
let ts1, zs1, f1 =
410+
try sub_typ env' t21 t11 (varTs aks1) with Sub e ->
411+
raise (Sub (FunParam e)) in
412+
let ps' = List.map (fun p -> AppT(p, varTs aks2)) ps in
413+
let su, zs2 =
414+
try match_extyp env' (subst_extyp (subst aks1 ts1) s1) s2 ps'
415+
with Sub e -> raise (Sub (FunResult e)) in
416+
List.map (function (AppT(p, _), t) -> (p, LamT(aks2, t))
417+
| _ -> assert false) su, lift env (zs1 @ zs2)
418+
419+
| _ ->
420+
[], []
421+
422+
and match_extyp env s1 s2 ps =
423+
let ExT(aks2, t2) = freshen_extyp env s2 in
424+
let ExT(aks1, t1) = freshen_extyp (add_typs aks2 env) s1 in
425+
match aks1, aks2 with
426+
| [], [] ->
427+
match_typ env t1 t2 ps
428+
| _ ->
429+
[], []
430+
431+
and match_row env tr1 tr2 ps =
432+
match tr2 with
433+
| [] ->
434+
[], []
435+
| (l, t2)::tr2' ->
436+
let su1, zs1 =
437+
try match_typ env (List.assoc l tr1) t2 ps with
438+
| Not_found -> raise (Sub (Struct(l, Missing)))
439+
| Sub e -> raise (Sub (Struct(l, e)))
440+
in
441+
let su2, zs2 = match_row env tr1 (subst_row (List.map (fun (p, t) -> psubst p t) su1) tr2') ps in
442+
su1 @ su2, zs1 @ zs2

syntax.ml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ and typ' =
2828
| EqT of exp
2929
| AsT of typ * typ
3030
| WithT of typ * var list * exp
31+
| RecT of var * typ * typ
3132

3233
and dec = (dec', unit) phrase
3334
and dec' =
@@ -218,13 +219,10 @@ let dotopE(x) =
218219

219220
let recT(p, t2) =
220221
let b, t1 = p.it in
221-
let e = TypE(t2)@@t2.at in
222-
let e' =
223-
match b.it with
224-
| VarB(x, _) -> RecE(x, t1, e)
225-
| EmptyB -> RecE("_"@@b.at, t1, e)
226-
| _ -> RecE("$"@@b.at, t1, letE(b, e)@@span[b.at; e.at])
227-
in PathT(e'@@span[p.at; t2.at])
222+
match b.it with
223+
| VarB(x, _) -> RecT(x, t1, t2)
224+
| EmptyB -> RecT("_"@@b.at, t1, t2)
225+
| _ -> RecT("$"@@b.at, t1, letT(b, t2)@@span[b.at; t2.at])
228226

229227
let recE(p, e) =
230228
let b, t = p.it in
@@ -355,6 +353,7 @@ let label_of_typ t =
355353
| EqT _ -> "EqT"
356354
| AsT _ -> "AsT"
357355
| WithT _ -> "WithT"
356+
| RecT _ -> "RecT"
358357

359358
let label_of_dec d =
360359
match d.it with
@@ -409,6 +408,8 @@ let rec string_of_typ t =
409408
| AsT(t1, t2) -> node' [string_of_typ t1; string_of_typ t2]
410409
| WithT(t, xs, e) ->
411410
node' ([string_of_typ t] @ List.map string_of_var xs @ [string_of_exp e])
411+
| RecT(x, b, t) ->
412+
node' [string_of_var x; string_of_typ b; string_of_typ t]
412413

413414
and string_of_dec d =
414415
let node' = node (label_of_dec d) in

talk.1ml

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,24 +107,23 @@ do map :
107107

108108
;; Objects
109109

110-
type point' self =
111-
{
112-
getX : () -> int;
113-
getY : () -> int;
114-
move : int -> int -> self;
110+
...rec {type point} => {
111+
type point = {
112+
getX : () -> int;
113+
getY : () -> int;
114+
move : int -> int -> point;
115+
}
115116
};
116-
type recpoint = rec (x : type) => point' x;
117-
type point = point' recpoint;
118117

119118
newpoint = rec (newpoint : int -> int -> point) => fun x y =>
120-
{
119+
@point {
121120
getX () = x;
122121
getY () = y;
123-
move dx dy = @recpoint (newpoint (x + dx) (y + dy));
122+
move dx dy = newpoint (x + dx) (y + dy);
124123
};
125124

126125
p = newpoint 3 4;
127-
p' = (p.move 1 2).@recpoint;
126+
p' = (p.@point.move 1 2).@point;
128127
do Int.print (p'.getX ());
129128
do print " ";
130129
do Int.print (p'.getY ());

test.1ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,11 +295,11 @@ in {
295295

296296

297297
type int = primitive "int";
298-
type t = rec a => int;
298+
...rec {type t} => {type t = int};
299299
v = @t 3;
300300
n = v.@t;
301301
@t m = v;
302-
type u a = rec _ => a;
302+
u (type a) = rec (_: type) => a;
303303
w = @(u int) 4;
304304
@(u int) x = w;
305305
type p a b = (a,b);

0 commit comments

Comments
 (0)