Skip to content

Commit c46b74f

Browse files
committed
minor changes
1 parent b8f3a3a commit c46b74f

File tree

3 files changed

+37
-15
lines changed

3 files changed

+37
-15
lines changed

shared/src/main/scala/mlscript/ConstraintSolver.scala

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -825,12 +825,13 @@ class ConstraintSolver extends NormalForms { self: Typer =>
825825
val newBound = (cctx._1 ::: cctx._2.reverse).foldRight(rhs)((c, ty) =>
826826
if (c.prov is noProv) ty else mkProxy(ty, c.prov))
827827
lhs.upperBounds ::= newBound // update the bound
828-
lhs.tsc.toList.flatMap { case (k,v)=>v.map((k,_)) }.foreach {
829-
case (tsc, i) =>
828+
lhs.tsc.foreachEntry { (tsc, v) =>
829+
v.foreach { i =>
830830
if (!tsc.tvs(i)._1) {
831831
tsc.updateOn(i, rhs)
832832
if (tsc.constraints.isEmpty) reportError()
833833
}
834+
}
834835
}
835836
val u = lhs.tsc.filter(_._1.constraints.sizeCompare(1) === 0)
836837
u.foreachEntry { case (k, _) => lhs.tsc.remove(k) }
@@ -846,12 +847,13 @@ class ConstraintSolver extends NormalForms { self: Typer =>
846847
val newBound = (cctx._1 ::: cctx._2.reverse).foldLeft(lhs)((ty, c) =>
847848
if (c.prov is noProv) ty else mkProxy(ty, c.prov))
848849
rhs.lowerBounds ::= newBound // update the bound
849-
rhs.tsc.toList.flatMap { case (k,v)=>v.map((k,_)) }.foreach {
850-
case (tsc, i) =>
850+
rhs.tsc.foreachEntry { (tsc, v) =>
851+
v.foreach { i =>
851852
if(tsc.tvs(i)._1) {
852853
tsc.updateOn(i, lhs)
853854
if (tsc.constraints.isEmpty) reportError()
854855
}
856+
}
855857
}
856858
val u = rhs.tsc.filter(_._1.constraints.sizeCompare(1) === 0)
857859
u.foreachEntry { case (k, _) => rhs.tsc.remove(k) }

shared/src/main/scala/mlscript/Typer.scala

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,14 @@ class Typer(var dbg: Boolean, var verbose: Bool, var explainErrors: Bool, val ne
113113
if (p) (b, tv) else (tv, b) }
114114
}.toList, innerTy)
115115

116-
val ambiguous: Bool = innerTy.getVars.toList.flatMap(_.tsc)
116+
val ambiguous = innerTy.getVars.toList.flatMap(_.tsc)
117117
.flatMap(_._1.tvs).distinct
118118
.groupBy(_._2)
119-
.exists { case (v,pvs) => pvs.sizeIs > 1 }
120-
if (ambiguous) raise(ErrorReport(Ls(fromStr("ambiguous") -> N), true))
119+
.filter { case (v,pvs) => pvs.sizeIs > 1 }
120+
if (ambiguous.nonEmpty) raise(ErrorReport(
121+
msg"ambiguous" -> N ::
122+
ambiguous.map { case (v,_) => msg"cannot determine satisfiability of type ${v.expPos}" -> v.prov.loco }.toList
123+
, true))
121124

122125
println(s"Inferred poly constr: $cty —— where ${cty.showBounds}")
123126

shared/src/test/diff/nu/HeungTung.mls

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,9 @@ fun r: Int -> Int & Bool -> Bool
423423
:e
424424
x => r(r(x))
425425
//│ ╔══[ERROR] ambiguous
426-
//│ ╙──
426+
//│ ╟── cannot determine satisfiability of type ?a
427+
//│ ║ l.424: x => r(r(x))
428+
//│ ╙── ^^^^
427429
//│ forall 'a 'b. 'a -> 'b
428430
//│ where
429431
//│ [+'c, -'b] in {[Int, Int], [Bool, Bool]}
@@ -468,7 +470,9 @@ fun s: Str -> Str & AA -> AA
468470
:e
469471
let g = x => s(r(x))
470472
//│ ╔══[ERROR] ambiguous
471-
//│ ╙──
473+
//│ ╟── cannot determine satisfiability of type ?a
474+
//│ ║ l.471: let g = x => s(r(x))
475+
//│ ╙── ^^^^
472476
//│ let g: forall 'a 'b. 'a -> 'b
473477
//│ where
474478
//│ [+'c, -'b] in {[Str, Str], [AA, AA]}
@@ -480,13 +484,13 @@ let g = x => s(r(x))
480484
:e
481485
g(0)
482486
//│ ╔══[ERROR] Type mismatch in application:
483-
//│ ║ l.481: g(0)
487+
//│ ║ l.485: g(0)
484488
//│ ║ ^^^^
485489
//│ ╟── integer literal of type `Int` does not match type `?a`
486-
//│ ║ l.481: g(0)
490+
//│ ║ l.485: g(0)
487491
//│ ║ ^
488492
//│ ╟── Note: constraint arises from application:
489-
//│ ║ l.469: let g = x => s(r(x))
493+
//│ ║ l.471: let g = x => s(r(x))
490494
//│ ╙── ^^^^
491495
//│ error
492496
//│ res
@@ -514,18 +518,31 @@ fun app2: ('a -> 'a -> 'a) -> 'a -> 'a
514518
fun snd: A -> Int -> Int & Str -> Str -> Str
515519
//│ fun snd: A -> Int -> Int & Str -> Str -> Str
516520

521+
517522
:e
518523
x => app2(snd)(x):Int
519524
//│ ╔══[ERROR] Type mismatch in type ascription:
520-
//│ ║ l.518: x => app2(snd)(x):Int
525+
//│ ║ l.523: x => app2(snd)(x):Int
521526
//│ ║ ^^^^^^^^^^^^
522527
//│ ╟── application of type `'a` does not match type `nothing`
523528
//│ ╟── Note: constraint arises from type reference:
524-
//│ ║ l.518: x => app2(snd)(x):Int
529+
//│ ║ l.523: x => app2(snd)(x):Int
525530
//│ ╙── ^^^
526531
//│ ╔══[ERROR] ambiguous
527-
//│ ╙──
532+
//│ ╙── cannot determine satisfiability of type 'a
528533
//│ nothing -> Int
529534
//│ res
530535
//│ = <no result>
531536
//│ app2 is not implemented
537+
538+
fun app2_ (f:'a -> 'a -> 'a)(x) = f(x)(x)
539+
//│ fun app2_: forall 'a. (f: 'a -> 'a -> 'a) -> 'a -> 'a
540+
541+
app2_(snd)
542+
//│ 'a -> 'b
543+
//│ where
544+
//│ 'a <: 'b
545+
//│ [+'a, -'a, -'b] in {[A & Int, Int, Int], [Str, Str, Str]}
546+
//│ res
547+
//│ = <no result>
548+
//│ snd is not implemented

0 commit comments

Comments
 (0)