Skip to content

Commit

Permalink
Reuse VarImpl.toIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
chengluyu committed Nov 22, 2023
1 parent 6f12bf4 commit 0c65802
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 20 deletions.
8 changes: 1 addition & 7 deletions shared/src/main/scala/mlscript/TypeSimplifier.scala
Original file line number Diff line number Diff line change
Expand Up @@ -411,13 +411,7 @@ trait TypeSimplifier { self: Typer =>
val arity = fs.size
val (componentFields, rcdFields) = rcd.fields
.filterNot(traitPrefixes contains _._1.name.takeWhile(_ =/= '#'))
.partitionMap(f =>
if (f._1.name.forall(_.isDigit)) {
val index = f._1.name.toInt
if (0 <= index && index < arity) L(index -> f._2)
else R(f)
} else R(f)
)
.partitionMap(f => f._1.toIndexOption.filter((0 until arity).contains).map(_ -> f._2).toLeft(f))
val componentFieldsMap = componentFields.toMap
val tupleComponents = fs.iterator.zipWithIndex.map { case ((nme, ty), i) =>
nme -> (ty && componentFieldsMap.getOrElse(i, TopType.toUpper(noProv))).update(go(_, pol.map(!_)), go(_, pol))
Expand Down
14 changes: 1 addition & 13 deletions shared/src/main/scala/mlscript/TyperHelpers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -604,19 +604,7 @@ abstract class TyperHelpers { Typer: Typer =>
case t @ RecordType(fs) => RecordType(fs.filter(nt => !names(nt._1)))(t.prov)
case t @ TupleType(fs) =>
val relevantNames = names.filter(n =>
!n.name.isEmpty && n.name.forall(_.isDigit) && {
val index = n.name.toInt
0 <= index && index < fs.length
})
// // With old tuple field names:
// n.name.startsWith("_")
// && {
// val t = n.name.tail
// t.forall(_.isDigit) && {
// val n = t.toInt
// 1 <= n && n <= fs.length
// }
// })
n.toIndexOption.exists((0 until fs.length).contains))
if (relevantNames.isEmpty) t
else {
val rcd = t.toRecord
Expand Down
2 changes: 2 additions & 0 deletions shared/src/main/scala/mlscript/helpers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,8 @@ trait VarImpl { self: Var =>
case S(_) => name.forall(_.isDigit)
case N => false
}
/** Get the integer if it's a valid index. */
def toIndexOption: Opt[Int] = if (isIndex) name.toIntOption else N
def isPatVar: Bool =
(name.head.isLetter && name.head.isLower || name.head === '_' || name.head === '$') && name =/= "true" && name =/= "false"
def toVar: Var = this
Expand Down

0 comments on commit 0c65802

Please sign in to comment.