Skip to content

Commit 4d89224

Browse files
committed
WIP Recursively dependent signatures
1 parent db6923a commit 4d89224

3 files changed

Lines changed: 39 additions & 8 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) <<<

sub.ml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,23 @@ let lift_warn at t env zs =
9191

9292
(* Subtyping *)
9393

94+
let rec is_tycon_typ = function
95+
| VarT(_) -> false
96+
| PrimT(_) -> false
97+
| StrT(_) -> false
98+
| FunT(_, _, s, Explicit e) -> e = Pure && is_tycon_extyp s
99+
| FunT(_, _, _, Implicit) -> false
100+
| TypT(_) -> true
101+
| WrapT(_) -> false
102+
| LamT(_) -> false
103+
| AppT(_) -> false
104+
| TupT(_) -> false
105+
| DotT(_) -> false
106+
| RecT(_) -> false
107+
| InferT(_) -> false
108+
109+
and is_tycon_extyp (ExT(_, t)) = is_tycon_typ t
110+
94111
let resolve_typ z t =
95112
Trace.sub (lazy ("[resolve_typ] z = " ^ string_of_norm_typ (InferT(z))));
96113
Trace.sub (lazy ("[resolve_typ] t = " ^ string_of_norm_typ t));
@@ -290,7 +307,8 @@ and sub_row env tr1 tr2 ps =
290307
| [] ->
291308
[], [], []
292309
| (l, t2)::tr2' ->
293-
Trace.sub (lazy ("[sub_row] l = " ^ l));
310+
Trace.sub (lazy (let k = if is_tycon_typ t2 then "type" else "val" in
311+
"[sub_row] l = " ^ l ^ " [" ^ k ^ "]"));
294312
let ts1, zs1, f =
295313
try sub_typ env (List.assoc l tr1) t2 ps with
296314
| Not_found -> raise (Sub (Struct(l, Missing)))

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' =
@@ -210,13 +211,10 @@ let dotopE(x) =
210211

211212
let recT(p, t2) =
212213
let b, t1 = p.it in
213-
let e = TypE(t2)@@t2.at in
214-
let e' =
215-
match b.it with
216-
| VarB(x, _) -> RecE(x, t1, e)
217-
| EmptyB -> RecE("_"@@b.at, t1, e)
218-
| _ -> RecE("$"@@b.at, t1, letE(b, e)@@span[b.at; e.at])
219-
in PathT(e'@@span[p.at; t2.at])
214+
match b.it with
215+
| VarB(x, _) -> RecT(x, t1, t2)
216+
| EmptyB -> RecT("_"@@b.at, t1, t2)
217+
| _ -> RecT("$"@@b.at, t1, letT(b, t2)@@span[b.at; t2.at])
220218

221219
let recE(p, e) =
222220
let b, t = p.it in
@@ -344,6 +342,7 @@ let label_of_typ t =
344342
| EqT _ -> "EqT"
345343
| AsT _ -> "AsT"
346344
| WithT _ -> "WithT"
345+
| RecT _ -> "RecT"
347346

348347
let label_of_dec d =
349348
match d.it with
@@ -398,6 +397,8 @@ let rec string_of_typ t =
398397
| AsT(t1, t2) -> node' [string_of_typ t1; string_of_typ t2]
399398
| WithT(t, xs, e) ->
400399
node' ([string_of_typ t] @ List.map string_of_var xs @ [string_of_exp e])
400+
| RecT(x, b, t) ->
401+
node' [string_of_var x; string_of_typ b; string_of_typ t]
401402

402403
and string_of_dec d =
403404
let node' = node (label_of_dec d) in

0 commit comments

Comments
 (0)