Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uncurry typelambdas to fix the recent implicit generation change #239

Merged
merged 2 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.virtuslab.inkuire.engine.api

import org.virtuslab.inkuire.engine.impl.model._
import org.virtuslab.inkuire.engine.impl.utils.Monoid
import org.virtuslab.inkuire.engine.impl.service.TypeNormalizationOps

import com.softwaremill.quicklens._

Expand Down Expand Up @@ -73,9 +74,12 @@ case class InkuireDb(
}
}

object InkuireDb {
object InkuireDb extends TypeNormalizationOps {
def empty: InkuireDb = InkuireDb(Seq.empty, Map.empty, Seq.empty, Map.empty)

def withNormalizedFunctions(db: InkuireDb): InkuireDb =
db.modify(_.functions.each.signature).using(uncurrySignature)

def combine(x: InkuireDb, y: InkuireDb): InkuireDb =
InkuireDb(
functions = (x.functions ++ y.functions).distinct,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package org.virtuslab.inkuire.engine.impl.model

import com.softwaremill.quicklens._

case class Signature(
receiver: Option[Contravariance],
arguments: Seq[Contravariance],
result: Covariance,
context: SignatureContext
) {
def typesWithVariances: Seq[Variance] = receiver.toSeq ++ arguments :+ result
def modifyAllTypes(f: TypeLike => TypeLike): Signature =
this.modifyAll(_.receiver.each.typ, _.arguments.each.typ, _.result.typ).using(f)
}

object Signature {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.virtuslab.inkuire.engine.impl.model._

import scala.util.Random

trait MatchingOps {
trait MatchingOps extends TypeNormalizationOps {

protected def genDummyTypes(n: Int): IndexedSeq[Type] =
1.to(n).map { i =>
Expand Down Expand Up @@ -56,7 +56,7 @@ trait MatchingOps {
.flatMap(identity)
.zip(concreteType.params.map(_.typ))
.toMap
parents.map(substituteBindings(_, bindings))
parents.map(substituteBindings(_, bindings)).map(uncurryTypes)
}

implicit class TypeMatchingOps(typ: TypeLike) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ScalaAnnotatedSignaturePrettifier extends BaseSignaturePrettifier {
args.map(_.typ).map(prettifyType).mkString(sep)

def prettifyType(t: TypeLike): String = t match {
case t: Type if t.isStarProjection => "*"
case t: Type if t.isStarProjection => "?"
case t: Type if t.isGeneric && !t.isVariable && t.name.name.matches("Function.*") =>
s"(${prettifyArgs(t.params, " => ")})"
case t: Type if t.isGeneric && !t.isVariable && t.name.name.matches("Tuple.*") =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.virtuslab.inkuire.engine.api._
import org.virtuslab.inkuire.engine.impl.model._
import org.virtuslab.inkuire.engine.impl.utils.Monoid._
import org.virtuslab.inkuire.engine.impl.utils.State
import scala.util.chaining._

class SubstitutionMatchService(val inkuireDb: InkuireDb) extends BaseMatchService with MatchingOps {

Expand All @@ -21,8 +22,7 @@ class SubstitutionMatchService(val inkuireDb: InkuireDb) extends BaseMatchServic
)
.flatMap { okTypes =>
State.get[TypingState].map { typingState =>
if (okTypes)
checkBindings(typingState.variableBindings)
if (okTypes) checkBindings(typingState.variableBindings)
else false
}
}
Expand All @@ -41,12 +41,14 @@ class SubstitutionMatchService(val inkuireDb: InkuireDb) extends BaseMatchServic
}

override def findMatches(resolveResult: ResolveResult): Seq[(AnnotatedSignature, Signature)] = {
val actualSignatures = resolveResult.signatures.foldLeft(resolveResult.signatures) {
case (acc, against) =>
acc.filter { sgn =>
sgn == against || !sgn.canSubstituteFor(against) || against.canSubstituteFor(sgn)
}
}
val actualSignatures = resolveResult.signatures
.foldLeft(resolveResult.signatures) {
case (acc, against) =>
acc.filter { sgn =>
sgn == against || !sgn.canSubstituteFor(against) || against.canSubstituteFor(sgn)
}
}
.map(uncurrySignature)
val actualSignaturesSize = actualSignatures.headOption.map(_.typesWithVariances.size)
val actualResolveResult = resolveResult.modify(_.signatures).setTo(actualSignatures)
resolveResult.filters
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.virtuslab.inkuire.engine.impl.service

import org.virtuslab.inkuire.engine.impl.model._
import com.softwaremill.quicklens._

trait TypeNormalizationOps {
def uncurryTypes(tpe: TypeLike): TypeLike = tpe match {
case TypeLambda(args, t: Type) if args.zip(t.params).forall { case (a, p) => a == p.typ } =>
uncurryTypes(t.modify(_.params).setTo(List.empty))
case t: Type =>
t.modify(_.params.each.typ).using(uncurryTypes)
case t: OrType =>
t.modifyAll(_.left, _.right).using(uncurryTypes)
case t: AndType =>
t.modifyAll(_.left, _.right).using(uncurryTypes)
case t: TypeLambda =>
t.modify(_.result).using(uncurryTypes)
}

def uncurrySignature(sgn: Signature): Signature =
sgn.modifyAllTypes(uncurryTypes)
}
Loading
Loading