Skip to content

Commit 052dc9c

Browse files
committed
Fix to drop unused metavariables
Sometimes type inference uses metavariables that end up not being used at all and that causes generalization to insert unnecessary implicit functions. This changes `lift*` to drop unused metavariables.
1 parent 7825b41 commit 052dc9c

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

elab.ml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ let elab_eff env eff =
177177

178178
let rec elab_typ env typ l =
179179
Trace.elab (lazy ("[elab_typ] " ^ EL.label_of_typ typ));
180+
let lift_warn = lift_warn (level ()) in
180181
(fun (s, zs) -> typ.at, env, s, zs, None, EL.label_of_typ typ) <<<
181182
match typ.it with
182183
| EL.PathT(exp) ->
@@ -314,6 +315,7 @@ and elab_dec env dec l =
314315

315316
and elab_pathexp env exp l =
316317
Trace.elab (lazy ("[elab_pathexp] " ^ EL.label_of_exp exp));
318+
let lift_warn = lift_warn (level ()) in
317319
let ExT(aks, t), p, zs, _ = elab_instexp env exp l in
318320
Trace.debug (lazy ("[ExpP] s = " ^ string_of_norm_extyp (ExT(aks, t))));
319321
if p = Impure then
@@ -362,6 +364,8 @@ and elab_const = function
362364

363365
and elab_exp env exp l =
364366
Trace.elab (lazy ("[elab_exp] " ^ EL.label_of_exp exp));
367+
let lift = lift (level ()) in
368+
let lift_warn = lift_warn (level ()) in
365369
(fun (s, p, zs, e) -> exp.at, env, s, zs, Some e, EL.label_of_exp exp) <<<
366370
match exp.it with
367371
| EL.VarE(var) ->
@@ -395,7 +399,7 @@ Trace.debug (lazy ("[FunE] env =" ^ VarSet.fold (fun a s -> s ^ " " ^ a) (domain
395399
| Pure, f -> f
396400
| _ -> error impl.at "impure function cannot be implicit" in
397401
ExT([], FunT(aks, t, s, p')), Pure,
398-
lift (* TODO: _warn exp.at (FunT(aks, t, s, p')) *) env (zs1 @ zs2),
402+
lift (* TODO: _warn exp.at *) (FunT(aks, t, s, p')) env (zs1 @ zs2),
399403
IL.genE(erase_bind aks, IL.LamE(var.it, erase_typ t, e2))
400404

401405
| EL.WrapE(var, typ) ->
@@ -606,6 +610,7 @@ t = !b:*. [= b] -> {t : [= t4.t b], u : !a:*. [= a] => [= (a, t4.u b a)]}
606610

607611
and elab_bind env bind l =
608612
Trace.elab (lazy ("[elab_bind] " ^ EL.label_of_bind bind));
613+
let lift_warn = lift_warn (level ()) in
609614
(fun (s, p, zs, e) -> bind.at, env, s, zs, Some e, EL.label_of_bind bind) <<<
610615
match bind.it with
611616
| EL.VarB(var, exp) ->
@@ -680,7 +685,7 @@ Trace.debug (lazy ("[GenE] a1 = " ^ string_of_typ (VarT(a1, BaseK))));
680685
match !z with
681686
| Undet u -> u.level >= level
682687
| Det _ -> assert false
683-
) (lift (add_typs aks env) zs) in
688+
) (lift level t (add_typs aks env) zs) in
684689
if p = Impure || zs1 = [] then
685690
s, p, zs1 @ zs2, e
686691
else begin

sub.ml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,19 +112,21 @@ let rec materialize_typ = function
112112

113113
(* Lifting *)
114114

115-
let lift env zs =
115+
let lift bottom t env zs =
116116
let dom = domain_typ env in
117117
List.filter (fun z ->
118118
match !z with
119119
| Det _ -> false
120-
| Undet u -> u.vars <- VarSet.inter u.vars dom; true
120+
| Undet u ->
121+
u.vars <- VarSet.inter u.vars dom;
122+
u.level <= bottom || occurs_typ u t
121123
) zs
122124

123125

124126
module IdSet = Set.Make(struct type t = int let compare = compare end)
125127
let warned_already = ref IdSet.empty
126128

127-
let lift_warn at t env zs =
129+
let lift_warn bottom at t env zs =
128130
let dom = domain_typ env in
129131
List.filter (fun z ->
130132
match !z with
@@ -142,7 +144,7 @@ let lift_warn at t env zs =
142144
);
143145
warned_already := IdSet.add u.id !warned_already
144146
);
145-
true
147+
u.level <= bottom || occurs_typ u t
146148
) zs
147149

148150

@@ -172,6 +174,7 @@ let rec psubst p t =
172174
| _ -> assert false
173175

174176
let rec sub_typ infer_binds env t1 t2 ps =
177+
let lift = lift (level ()) t1 in
175178
Trace.sub (lazy ("[sub_typ] t1 = " ^ string_of_norm_typ t1));
176179
Trace.sub (lazy ("[sub_typ] t2 = " ^ string_of_norm_typ t2));
177180
Trace.sub (lazy ("[sub_typ] ps = " ^
@@ -348,6 +351,7 @@ and sub_extyp infer_binds env s1 s2 ps =
348351
| [], [] ->
349352
sub_typ infer_binds env t1 t2 ps
350353
| _ ->
354+
let lift = lift (level ()) t1 in
351355
let ts, zs, f =
352356
sub_typ infer_binds (add_typs aks1 env) t1 t2 (varTs aks2) in
353357
[], lift env zs,

sub.mli

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ val materialize_typ : Types.typ -> Fomega.exp
99

1010
(* Lifting *)
1111

12-
val lift : Env.env -> Types.infer ref list -> Types.infer ref list
12+
val lift :
13+
int -> Types.typ -> Env.env -> Types.infer ref list -> Types.infer ref list
1314
val lift_warn :
14-
Source.region -> Types.typ -> Env.env -> Types.infer ref list ->
15+
int -> Source.region -> Types.typ -> Env.env -> Types.infer ref list ->
1516
Types.infer ref list
1617

1718

0 commit comments

Comments
 (0)