Skip to content

Commit 2dd3333

Browse files
committed
Fix unexpected generalizations
1 parent b6b9a0a commit 2dd3333

File tree

3 files changed

+32
-16
lines changed

3 files changed

+32
-16
lines changed

hkmc2/shared/src/main/scala/hkmc2/bbml/bbML.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ final case class BbCtx(
4646
def getRegEnv: Type = outVar match
4747
case S(v) => v | outRegAcc
4848
case N => outRegAcc
49+
def isTopLevel = parent.isEmpty
4950

5051

5152
given (using ctx: BbCtx): Raise = ctx.raise
@@ -283,7 +284,8 @@ class BBTyper(using elState: Elaborator.State, tl: TL):
283284
val funTy = tryMkMono(res, lam)
284285
given CCtx = CCtx.init(lam, N)
285286
constrain(funTy, funTyV)(using ctx)
286-
pctx += sym -> PolyType.generalize(funTy, S(outer), 1)
287+
if pctx.isTopLevel then // only generalize top-level definitions
288+
pctx += sym -> PolyType.generalize(funTy, S(outer), 1)
287289
case _ => error(msg"Function definition shape not yet supported for ${sym.nme}" -> lam.toLoc :: Nil)
288290

289291
private def typeSplit

hkmc2/shared/src/test/mlscript/bbml/bbExtrude.mls

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,23 @@ f(g)(bar)
9191
//│ ╟── because: cannot constrain 'B <: ¬{Int}
9292
//│ ╙── because: cannot constrain ⊤ <: ¬{Int}
9393
//│ Type: Int
94+
95+
fun k(x) =
96+
fun f(y) = x(y)
97+
f(0) + 0
98+
k
99+
//│ Type: ['eff] -> (Int ->{'eff} Int) ->{'eff} Int
100+
101+
:e
102+
k(_ => "")
103+
//│ ╔══[ERROR] Type error in function literal with expected type 'x
104+
//│ ║ l.102: k(_ => "")
105+
//│ ║ ^^
106+
//│ ╟── because: cannot constrain ('_) ->{⊥} (Str) <: 'x
107+
//│ ╟── because: cannot constrain ('_) ->{⊥} (Str) <: 'x
108+
//│ ╟── because: cannot constrain ('_) ->{⊥} (Str) <: (Int) ->{'eff} (Int)
109+
//│ ╙── because: cannot constrain Str <: Int
110+
//│ Type: Int
111+
112+
k(_ => 42)
113+
//│ Type: Int

hkmc2/shared/src/test/mlscript/bbml/bbGetters.mls

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,14 @@ test1()
3131
//│ Type: ⊥
3232

3333

34-
:todo // it feels like this should work
3534
fun test2() =
3635
fun funny = case
3736
0 then 0
3837
n then funny(n - 1) + 1
3938
funny
40-
//│ ╔══[ERROR] Expected a monomorphic type or an instantiable type here, but () ->{⊥} [outer, 'caseScrut, 'eff] -> 'caseScrut ->{'eff} Int found
41-
//│ ║ l.37: 0 then 0
42-
//│ ║ ^
43-
//│ ║ l.38: n then funny(n - 1) + 1
44-
//│ ║ ^^^^^^^^^^^^^^^^^^^^^^^^^^^
45-
//│ ║ l.39: funny
46-
//│ ╙── ^^^^^^^
47-
//│ Type: ⊤
39+
test2
40+
//│ = [function test2]
41+
//│ Type: () -> (Int -> Int)
4842

4943
fun test2() =
5044
fun funny = case
@@ -82,7 +76,7 @@ fun test2() =
8276
case n then funny(n - 1) + 1
8377
funny
8478
//│ ╔══[WARNING] Pure expression in statement position
85-
//│ ║ l.81: case 0 then 0
79+
//│ ║ l.75: case 0 then 0
8680
//│ ╙── ^
8781
//│ JS (unsanitized):
8882
//│ let test22;
@@ -104,15 +98,15 @@ fun test2() =
10498
//│ return tmp1
10599
//│ };
106100
//│ ╔══[WARNING] Pure expression in statement position
107-
//│ ║ l.81: case 0 then 0
101+
//│ ║ l.75: case 0 then 0
108102
//│ ╙── ^
109103
//│ ╔══[ERROR] Function definition shape not yet supported for funny
110-
//│ ║ l.81: case 0 then 0
104+
//│ ║ l.75: case 0 then 0
111105
//│ ║ ^
112-
//│ ║ l.82: case n then funny(n - 1) + 1
106+
//│ ║ l.76: case n then funny(n - 1) + 1
113107
//│ ╙── ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
114108
//│ ╔══[ERROR] Variable not found: funny
115-
//│ ║ l.83: funny
109+
//│ ║ l.77: funny
116110
//│ ╙── ^^^^^
117111
//│ Type: ⊤
118112

@@ -121,7 +115,7 @@ fun test2() =
121115
fun test3 =
122116
print("Hi")
123117
//│ ╔══[ERROR] Function definition shape not yet supported for test3
124-
//│ ║ l.122: print("Hi")
118+
//│ ║ l.116: print("Hi")
125119
//│ ╙── ^^^^^^^^^^^
126120
//│ Type: ⊤
127121

0 commit comments

Comments
 (0)