Skip to content

Commit db9ed1e

Browse files
committed
Recursively dependent signatures
1 parent 3fa6d08 commit db9ed1e

6 files changed

Lines changed: 179 additions & 15 deletions

File tree

elab.ml

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

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

readme.1ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ p = f (fun x => x)
4646

4747

4848

49-
type stream = rec t => {hd : int, tl : () ~> opt t} ;; creates rec type
49+
...rec {type stream} => {type stream = {hd : int, tl : () ~> opt stream}} ;; creates rec type
5050
single x :@ stream = {hd = x, tl = fun () => none} ;; b :@ t rolls value into t
5151
({hd = n} :@ stream) = single 5 ;; p :@ t pattern matches on rec value
5252
do Int.print n ;; or:

regression.1ml

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,30 @@ type DEC_PUNNING = {int, list}
3333

3434
;;
3535

36+
type RDS = rec (R: let type T'1 = {type t _} in {A: T'1, B: T'1}) => let
37+
type ONE (type this _) (type that _) = {
38+
t = this
39+
aThis 'a: this a
40+
ofThat 'a: that a -> this a
41+
}
42+
in {
43+
A: ONE R.A.t R.B.t
44+
B: ONE R.B.t R.A.t
45+
}
46+
47+
Rds :> RDS = let
48+
One = {
49+
type t _ = ()
50+
aThis = ()
51+
ofThat _ = ()
52+
}
53+
in {
54+
A = One
55+
B = One
56+
}
57+
58+
;;
59+
3660
Equivalence: {
3761
type t a b
3862

@@ -220,11 +244,27 @@ type_error rec (R: {}) => {
220244
kaboom () = R
221245
}
222246

223-
Kaboom = rec (R: rec R => {kaboom: () ~> R}) => {kaboom () = R} :@ (= R)
247+
;;Kaboom = rec (R: rec R => {kaboom: () ~> R}) => {kaboom () = R} :@ (= R)
224248

225249
;;
226250

227-
Mutually = let
251+
type MUTUALLY = rec (R: {
252+
Even: {type t _}
253+
Odd: {type t _}
254+
}) => {
255+
Even: {
256+
t = R.Even.t
257+
make 'x: x -> R.Odd.t x -> R.Even.t x
258+
size 'x: R.Even.t x ~> int
259+
}
260+
Odd: {
261+
t = R.Odd.t
262+
make 'x: opt (R.Even.t x) -> R.Odd.t x
263+
size 'x: R.Odd.t x ~> int
264+
}
265+
}
266+
267+
Mutually :> MUTUALLY = let
228268
T = rec (R: {
229269
Even: {type t _}
230270
Odd: {type t _}
@@ -285,7 +325,7 @@ type (a `>>` b) c = c
285325
;;
286326

287327
Hungry = {
288-
type eat a = rec eat_a => a ~> eat_a
328+
...rec {type eat a} => {type eat a = a ~> eat a}
289329

290330
eater 'a = rec (eater: eat a) => (fun a => eater) :@ eat a
291331

@@ -295,7 +335,7 @@ Hungry = {
295335
}
296336

297337
PolyRec = {
298-
type l a = rec (type t) => a | t
338+
...rec {type l a} => {type l a = a | l a}
299339
...rec {type t a} => {type t a = a | t (a, a)}
300340

301341
t_int = t int

sub.ml

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,24 @@ let extract_bind infer_binds env tr1 l t2 =
153153
| None -> raise (Sub (Struct(l, Missing)))
154154
| Some (t, zs, e) -> t, zs, fun f _ _ -> IL.AppE(f, e)
155155

156+
let rec has_typs_typ ps = function
157+
| StrT(tr) -> List.exists (fun (_, t) -> has_typs_typ ps t) tr
158+
| FunT(aks, td, ExT(_, tr), e) ->
159+
(match e with
160+
| Implicit | Explicit Pure ->
161+
has_typs_typ (List.map (fun p -> AppT(p, varTs aks)) ps) tr
162+
| Explicit Impure -> false)
163+
| TypT(ExT(_, p)) -> List.mem p ps
164+
| VarT(_)
165+
| PrimT(_)
166+
| WrapT(_)
167+
| LamT(_)
168+
| AppT(_)
169+
| TupT(_)
170+
| DotT(_)
171+
| RecT(_)
172+
| InferT(_) -> false
173+
156174
let resolve_typ z t =
157175
Trace.sub (lazy ("[resolve_typ] z = " ^ string_of_norm_typ (InferT(z))));
158176
Trace.sub (lazy ("[resolve_typ] t = " ^ string_of_norm_typ t));
@@ -174,6 +192,24 @@ let rec sub_typ infer_binds env t1 t2 ps =
174192
Trace.sub (lazy ("[sub_typ] t2 = " ^ string_of_norm_typ t2));
175193
Trace.sub (lazy ("[sub_typ] ps = " ^
176194
String.concat ", " (List.map string_of_norm_typ ps)));
195+
let ts', zs', t2, ps =
196+
if ps <> [] then
197+
let su, zs' = match_typ env t1 t2 ps in
198+
Trace.sub (lazy ("[sub_typ] su = " ^
199+
String.concat ", " (List.map (fun (p, t) -> Printf.sprintf "[= %s] - [= %s]\n" (string_of_typ p) (string_of_typ t)) su)));
200+
let t2 = subst_typ (List.map (fun (p, t) -> psubst p t) su) t2 in
201+
let ps' = List.filter (fun p -> List.mem_assoc p su) ps in
202+
let ts' = List.map (fun p -> List.assoc p su) ps' in
203+
let ps = List.filter (fun p -> not (List.mem_assoc p su)) ps in
204+
if ps <> [] then begin
205+
Trace.sub (lazy ("[sub_typ] unmatched ps = " ^
206+
String.concat ", " (List.map string_of_norm_typ ps)));
207+
raise Not_found
208+
end;
209+
ts', zs', t2, ps
210+
else
211+
[], [], t2, ps
212+
in
177213
let e1 = IL.VarE("x") in
178214
let ts, zs, e =
179215
match norm_typ t1, freshen_typ env (norm_typ t2) with
@@ -335,7 +371,7 @@ let rec sub_typ infer_binds env t1 t2 ps =
335371
Trace.sub (lazy ("[sub_typ] done ts = " ^
336372
String.concat ", " (List.map string_of_norm_typ ts)));
337373
Trace.sub (lazy ("[sub_typ] done x -> " ^ IL.string_of_exp e));
338-
ts, zs, IL.LamE("x", erase_typ t1, e)
374+
ts' @ ts, zs' @ zs, IL.LamE("x", erase_typ t1, e)
339375

340376
and sub_extyp infer_binds env s1 s2 ps =
341377
Trace.sub (lazy ("[sub_extyp] s1 = " ^ string_of_norm_extyp s1));
@@ -394,5 +430,79 @@ and equal_row env tr1 tr2 ps =
394430
try sub_row false env tr2 tr1 ps with Sub e -> raise (Sub (Right e)) in
395431
zs1 @ zs2
396432

433+
and match_typ env t1 t2 ps =
434+
let t2 = norm_typ t2 in
435+
Trace.sub (lazy ("[match_typ] t1 = " ^ string_of_norm_typ t1));
436+
Trace.sub (lazy ("[match_typ] t2 = " ^ string_of_typ t2));
437+
Trace.sub (lazy ("[match_typ] ps = " ^
438+
String.concat ", " (List.map string_of_norm_typ ps)));
439+
if not (has_typs_typ ps t2) then [], [] else
440+
match norm_typ t1, freshen_typ env t2 with
441+
| t1, FunT(aks21, t21, ExT(aks22, t22), Implicit) ->
442+
assert (aks22 = []);
443+
let su, zs = match_typ (add_typs aks21 env) t1 t22 ps in
444+
List.map (fun (p, t) -> (p, LamT(aks21, t))) su, lift env zs
445+
446+
| FunT(aks11, t11, ExT(aks12, t12), Implicit), t2 ->
447+
assert (aks12 = []);
448+
let ts1, zs1 = guess_typs (Env.domain_typ env) aks11 in
449+
let t1' = subst_typ (subst aks11 ts1) t12 in
450+
let su, zs2 = match_typ env t1' t2 ps in
451+
su, zs1 @ zs2
452+
453+
| TypT(s1), TypT(s2) ->
454+
(match s1, s2 with
455+
| ExT(aks1, t), ExT([], p) when List.mem p ps ->
456+
if aks1 <> [] || not (!undecidable_flag || is_small_typ t) then
457+
raise (Sub (Mismatch (t1, t2)));
458+
[(p, t)], []
459+
| _ ->
460+
[], [])
461+
462+
| StrT(tr1), StrT(tr2) ->
463+
match_row env tr1 tr2 ps
464+
465+
| FunT(aks1, t11, s1, Explicit p1), FunT(aks2, t21, s2, Explicit p2) ->
466+
if p1 = Impure && p2 = Pure then raise (Sub (FunEffect(p1, p2)));
467+
if p1 <> Pure || p2 <> Pure then
468+
[], []
469+
else
470+
let env' = add_typs aks2 env in
471+
let ts1, zs1, f1 =
472+
try sub_typ true env' t21 t11 (varTs aks1) with Sub e ->
473+
raise (Sub (FunParam e)) in
474+
let ps' = List.map (fun p -> AppT(p, varTs aks2)) ps in
475+
let su, zs2 =
476+
try match_extyp env' (subst_extyp (subst aks1 ts1) s1) s2 ps'
477+
with Sub e -> raise (Sub (FunResult e)) in
478+
List.map (function (AppT(p, _), t) -> (p, LamT(aks2, t))
479+
| _ -> assert false) su, lift env (zs1 @ zs2)
480+
481+
| _ ->
482+
[], []
483+
484+
and match_extyp env s1 s2 ps =
485+
let ExT(aks2, t2) = freshen_extyp env s2 in
486+
let ExT(aks1, t1) = freshen_extyp (add_typs aks2 env) s1 in
487+
match aks1, aks2 with
488+
| [], [] ->
489+
match_typ env t1 t2 ps
490+
| _ ->
491+
[], []
492+
493+
and match_row env tr1 tr2 ps =
494+
match tr2 with
495+
| [] ->
496+
[], []
497+
| (l, t2)::tr2' ->
498+
let t1, zs, app = extract_bind true env tr1 l t2 in
499+
let su1, zs1 =
500+
try match_typ env t1 t2 ps with
501+
| Sub e -> raise (Sub (Struct(l, e))) in
502+
let su2, zs2 =
503+
match_row env tr1
504+
(subst_row (List.map (fun (p, t) -> psubst p t) su1) tr2') ps in
505+
su1 @ su2, zs @ zs1 @ zs2
506+
397507
let sub_typ = sub_typ true
398508
let sub_extyp = sub_extyp true

syntax.ml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ and typ' =
2929
| EqT of exp
3030
| AsT of typ * typ
3131
| WithT of typ * var list * exp
32+
| RecT of var * typ * typ
3233

3334
and dec = (dec', unit) phrase
3435
and dec' =
@@ -228,13 +229,10 @@ type pat = {bind: bind; infer: typ option; annot: typ option}
228229

229230
let recT(p, t2) =
230231
let b, t1 = p.it in
231-
let e = typE(t2)@@t2.at in
232-
let e' =
233-
match b.it with
234-
| VarB(x, {it = VarE({it = "$"})}) -> RecE(x, t1, e)
235-
| EmptyB -> RecE("_"@@b.at, t1, e)
236-
| _ -> RecE("$"@@b.at, t1, letE(b, e)@@span[b.at; e.at])
237-
in PathT(e'@@span[p.at; t2.at])
232+
match b.it with
233+
| VarB(x, {it = VarE({it = "$"})}) -> RecT(x, t1, t2)
234+
| EmptyB -> RecT("_"@@b.at, t1, t2)
235+
| _ -> RecT("$"@@b.at, t1, letT(b, t2)@@span[b.at; t2.at])
238236

239237
let recE(p, e) =
240238
let b, t = p.it in
@@ -366,6 +364,7 @@ let label_of_typ t =
366364
| EqT _ -> "EqT"
367365
| AsT _ -> "AsT"
368366
| WithT _ -> "WithT"
367+
| RecT _ -> "RecT"
369368

370369
let label_of_dec d =
371370
match d.it with
@@ -422,6 +421,8 @@ let rec string_of_typ t =
422421
| AsT(t1, t2) -> node' [string_of_typ t1; string_of_typ t2]
423422
| WithT(t, xs, e) ->
424423
node' ([string_of_typ t] @ List.map string_of_var xs @ [string_of_exp e])
424+
| RecT(x, b, t) ->
425+
node' [string_of_var x; string_of_typ b; string_of_typ t]
425426

426427
and string_of_dec d =
427428
let node' = node (label_of_dec d) in
@@ -475,6 +476,7 @@ let rec imports_typ typ =
475476
| EqT exp -> imports_exp exp
476477
| AsT(typ1, typ2) -> imports_typ typ1 @ imports_typ typ2
477478
| WithT(typ, _, exp) -> imports_typ typ @ imports_exp exp
479+
| RecT(_, typ1, typ2) -> imports_typ typ1 @ imports_typ typ2
478480

479481
and imports_dec dec =
480482
match dec.it with

test.1ml

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

276276

277277
type int = primitive "int"
278-
type t = rec a => int
278+
...rec {type t} => {type t = int}
279279
v = 3 :@ t
280280
n = v @: t
281281
(m :@ t) = v
282-
type u a = rec _ => a
282+
...rec {type u a} => {type u a = a}
283283
w = 4 :@ u int
284284
(x :@ u int) = w
285285
((x :@ t, y) :@ u (_, _)) = (6 :@ t, 7) :@ u (_, _)

0 commit comments

Comments
 (0)