Skip to content

Commit

Permalink
use iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
auht committed Aug 29, 2024
1 parent e1b7fba commit 9cf43a4
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 29 deletions.
26 changes: 14 additions & 12 deletions shared/src/main/scala/mlscript/ConstraintSolver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -845,18 +845,20 @@ class ConstraintSolver extends NormalForms { self: Typer =>
}
}
}
val u = lhs.tsc.filter(_._1.constraints.sizeCompare(1) === 0)
u.foreachEntry { case (k, _) =>
k.tvs.mapValues(_.unwrapProxies).foreach {
val u = lhs.tsc.keysIterator.filter(_.constraints.sizeCompare(1)===0).duplicate
u._1.foreach { k =>
k.tvs.mapValuesIter(_.unwrapProxies).foreach {
case (_,tv: TV) => tv.tsc.remove(k)
case _ => ()
}
}
u.foreachEntry { case (k, _) =>
k.constraints.head.zip(k.tvs).foreach {
u._2.foreach { k =>
println(s"tag5: ${k.tvs}")
k.constraints.head.iterator.zip(k.tvs).foreach {
case (c, (pol, t)) => if (pol) rec(t, c, false) else rec(c, t, false)
}
}
println(s"tag2: $u")
lhs.lowerBounds.foreach(rec(_, rhs, true)) // propagate from the bound

case (lhs, rhs: TypeVariable) if lhs.level <= rhs.level =>
Expand All @@ -872,15 +874,15 @@ class ConstraintSolver extends NormalForms { self: Typer =>
}
}
}
val u = rhs.tsc.filter(_._1.constraints.sizeCompare(1) === 0)
u.foreachEntry { case (k, _) =>
k.tvs.mapValues(_.unwrapProxies).foreach {
val u = rhs.tsc.keysIterator.filter(_.constraints.sizeCompare(1)===0).duplicate
u._1.foreach { k =>
k.tvs.mapValuesIter(_.unwrapProxies).foreach {
case (_,tv: TV) => tv.tsc.remove(k)
case _ => ()
}
}
u.foreachEntry { case (k, _) =>
k.constraints.head.zip(k.tvs).foreach {
u._2.foreach { k =>
k.constraints.head.iterator.zip(k.tvs).foreach {
case (c, (pol, t)) => if (pol) rec(t, c, false) else rec(c, t, false)
}
}
Expand Down Expand Up @@ -1608,7 +1610,7 @@ class ConstraintSolver extends NormalForms { self: Typer =>
lvl
})
val freshentsc = tv.tsc.flatMap { case (tsc,_) =>
if (tsc.tvs.map(_._2.unwrapProxies).forall {
if (tsc.tvs.values.map(_.unwrapProxies).forall {
case tv: TV => !freshened.contains(tv)
case _ => true
}) S(tsc) else N
Expand All @@ -1620,7 +1622,7 @@ class ConstraintSolver extends NormalForms { self: Typer =>
val t = new TupleSetConstraints(tsc.constraints, tsc.tvs)
t.constraints = t.constraints.map(_.map(freshen))
t.tvs = t.tvs.map(x => (x._1,freshen(x._2)))
t.tvs.map(_._2.unwrapProxies).zipWithIndex.foreach {
t.tvs.values.map(_.unwrapProxies).zipWithIndex.foreach {
case (tv: TV, i) => tv.tsc.updateWith(t)(_.map(_ + i).orElse(S(Set(i))))
case _ => ()
}
Expand Down
5 changes: 2 additions & 3 deletions shared/src/main/scala/mlscript/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,7 @@ class Typer(var dbg: Boolean, var verbose: Bool, var explainErrors: Bool, val ne
if (p) (b, tv) else (tv, b) }
}.toList, innerTy)

val ambiguous = innerTy.getVars.toList.flatMap(_.tsc)
.flatMap(_._1.tvs).distinct
val ambiguous = innerTy.getVars.unsorted.flatMap(_.tsc.keys.flatMap(_.tvs))
.groupBy(_._2)
.filter { case (v,pvs) => pvs.sizeIs > 1 }
if (ambiguous.nonEmpty) raise(ErrorReport(
Expand Down Expand Up @@ -687,7 +686,7 @@ class Typer(var dbg: Boolean, var verbose: Bool, var explainErrors: Bool, val ne
tscs.foreach { case (typevars, constrs) =>
val tvs = typevars.map(x => (x._1, rec(x._2)))
val tsc = new TupleSetConstraints(constrs.map(_.map(rec)), tvs)
tvs.map(_._2.unwrapProxies).zipWithIndex.foreach {
tvs.values.map(_.unwrapProxies).zipWithIndex.foreach {
case (tv: TV, i) => tv.tsc.updateWith(tsc)(_.map(_ + i).orElse(S(Set(i))))
case _ => ()
}
Expand Down
12 changes: 6 additions & 6 deletions shared/src/main/scala/mlscript/TyperDatatypes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -683,14 +683,14 @@ abstract class TyperDatatypes extends TyperHelpers { Typer: Typer =>
(u,l.reduce((x,y) => ComposedType(!p,x,y)(noProv)))
}
}
tvs.map(_._2.unwrapProxies).foreach {
tvs.values.map(_.unwrapProxies).foreach {
case tv: TV => tv.tsc += this -> Set.empty
case _ => ()
}
if (!u.isEmpty) {
tvs = u.flatMap(_.keys).distinct
constraints = tvs.map(x => u.map(_.getOrElse(x,if (x._1) TopType else BotType))).transpose
tvs.map(_._2.unwrapProxies).zipWithIndex.foreach {
tvs.values.map(_.unwrapProxies).zipWithIndex.foreach {
case (tv: TV, i) => tv.tsc.updateWith(this)(_.map(_ + i).orElse(S(Set(i))))
case _ => ()
}
Expand Down Expand Up @@ -769,7 +769,7 @@ abstract class TyperDatatypes extends TyperHelpers { Typer: Typer =>
}
}
def mk(ov: Overload, f: FT)(implicit raise: Raise, ctx: Ctx): Opt[TupleSetConstraints] = {
val u = ov.alts.map(lcgFunction(false, f, _)).flatten.map { x =>
val u = ov.alts.flatMap(lcgFunction(false, f, _)).map { x =>
x.groupMap(_._1)(_._2).map { case (u@(p,_),l) =>
(u,l.reduce((x,y) => ComposedType(!p,x,y)(noProv)))
}
Expand All @@ -778,7 +778,7 @@ abstract class TyperDatatypes extends TyperHelpers { Typer: Typer =>
val tvs = u.flatMap(_.keys).distinct
val m = tvs.map(x => u.map(_.getOrElse(x,if (x._1) TopType else BotType)))
val tsc = new TupleSetConstraints(m.transpose, tvs)
tvs.mapValues(_.unwrapProxies).zipWithIndex.foreach {
tvs.mapValuesIter(_.unwrapProxies).zipWithIndex.foreach {
case ((true, tv: TV), i) =>
tv.tsc.updateWith(tsc)(_.map(_ + i).orElse(S(Set(i))))
tv.lowerBounds.foreach(tsc.updateImpl(i, _))
Expand All @@ -789,11 +789,11 @@ abstract class TyperDatatypes extends TyperHelpers { Typer: Typer =>
}
println(s"TSC mk: ${tsc.tvs} in ${tsc.constraints}")
if (tsc.constraints.sizeCompare(1) === 0) {
tvs.map(_._2.unwrapProxies).foreach {
tvs.values.map(_.unwrapProxies).foreach {
case tv: TV => tv.tsc.remove(tsc)
case _ => ()
}
tsc.constraints.head.zip(tvs).foreach {
tsc.constraints.head.iterator.zip(tvs).foreach {
case (c, (pol, t)) =>
if (!pol) constrain(c, t)(raise, ov.prov, ctx)
if (pol) constrain(t, c)(raise, ov.prov, ctx)
Expand Down
6 changes: 3 additions & 3 deletions shared/src/main/scala/mlscript/TyperHelpers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -759,8 +759,8 @@ abstract class TyperHelpers { Typer: Typer =>
case tv: TypeVariable =>
val poltv = pol(tv)
(if (poltv =/= S(false)) tv.lowerBounds.map(pol.at(tv.level, true) -> _) else Nil) :::
(if (poltv =/= S(true)) tv.upperBounds.map(pol.at(tv.level, false) -> _) else Nil) :::
tv.tsc.keys.flatMap(_.tvs).map(u => pol.at(tv.level,u._1) -> u._2).toList
(if (poltv =/= S(true)) tv.upperBounds.map(pol.at(tv.level, false) -> _) else Nil) ++
tv.tsc.keys.flatMap(_.tvs).map(u => pol.at(tv.level,u._1) -> u._2)
case FunctionType(l, r) => pol.contravar -> l :: pol.covar -> r :: Nil
case Overload(as) => as.map(pol -> _)
case ComposedType(_, l, r) => pol -> l :: pol -> r :: Nil
Expand Down Expand Up @@ -917,7 +917,7 @@ abstract class TyperHelpers { Typer: Typer =>
}
def children(includeBounds: Bool): List[SimpleType] = this match {
case tv @ AssignedVariable(ty) => if (includeBounds) ty :: Nil else Nil
case tv: TypeVariable => if (includeBounds) tv.lowerBounds ::: tv.upperBounds ++ tv.tsc.keys.flatMap(_.tvs.map(_._2)) else Nil
case tv: TypeVariable => if (includeBounds) tv.lowerBounds ::: tv.upperBounds ++ tv.tsc.keys.flatMap(_.tvs.values) else Nil
case FunctionType(l, r) => l :: r :: Nil
case Overload(as) => as
case ComposedType(_, l, r) => l :: r :: Nil
Expand Down
10 changes: 5 additions & 5 deletions shared/src/test/diff/nu/HeungTung.mls
Original file line number Diff line number Diff line change
Expand Up @@ -409,15 +409,15 @@ x => q(x)
//│ q is not implemented

:e
(x => q(x))(1):int
(x => q(x))(1):Int
//│ ╔══[ERROR] Type mismatch in type ascription:
//│ ║ l.412: (x => q(x))(1):int
//│ ║ l.412: (x => q(x))(1):Int
//│ ║ ^^^^^^^^^^^^^^
//│ ╟── application of type `?a` does not match type `int`
//│ ╟── application of type `?a` does not match type `Int`
//│ ╟── Note: constraint arises from type reference:
//│ ║ l.412: (x => q(x))(1):int
//│ ║ l.412: (x => q(x))(1):Int
//│ ╙── ^^^
//│ int
//│ Int
//│ res
//│ = <no result>
//│ q is not implemented
Expand Down

0 comments on commit 9cf43a4

Please sign in to comment.