Skip to content

Commit

Permalink
Uncurry typelambdas to fix the recent implicit generation change
Browse files Browse the repository at this point in the history
  • Loading branch information
KacperFKorban committed Sep 19, 2024
1 parent dbf2606 commit cb6cda8
Show file tree
Hide file tree
Showing 13 changed files with 47,582 additions and 12 deletions.
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,8 @@ 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 +57,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 @@ -46,11 +46,12 @@ class SubstitutionMatchService(val inkuireDb: InkuireDb) extends BaseMatchServic
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
.filterFrom(inkuireDb.functions)
// .filter(fun => fun.name.contains("contains") && fun.signature.toString.contains("SeqOps"))
.filter(fun => Some(fun.signature.typesWithVariances.size) == actualSignaturesSize)
.map(fun => fun -> isMatch(actualResolveResult)(fun))
.collect {
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)
}
File renamed without changes.
47,527 changes: 47,527 additions & 0 deletions engine/src/test/resources/stdlib-3.6.0-RC1.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ trait BaseEndToEndEngineTest {
var testService: InkuireTestService = null
def apply() = testService
override def beforeAll(): Unit = {
val file = new File(filePath)
val url = Thread.currentThread().getContextClassLoader().getResource(filePath)
val file = new File(url.getPath())
testService = new InkuireTestService(file.toURI.toURL.toString())
}
override def afterAll(): Unit = {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.virtuslab.inkuire.engine

class EndToEndEngineTest extends munit.FunSuite with BaseEndToEndEngineTest {

override val filePath: String = "./engine/src/test/resources/stdlib.json"
abstract class EndToEndEngineTest extends munit.FunSuite with BaseEndToEndEngineTest {

testFunctionFound("List[A] => (A => B) => List[B]", "map")

Expand Down Expand Up @@ -49,3 +47,11 @@ class EndToEndEngineTest extends munit.FunSuite with BaseEndToEndEngineTest {

testFunctionFound("Seq[A] => A => Boolean", "contains")
}

class EndToEndEngineTest360RC1 extends EndToEndEngineTest {
override val filePath: String = "stdlib-3.6.0-RC1.json"
}

class EndToEndEngineTest312 extends EndToEndEngineTest {
override val filePath: String = "stdlib-3.1.2.json"
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class InkuireTestService(path: String) {
}
.pipe(Monoid.combineAll[InkuireDb])
.withOrphanTypes
.pipe(InkuireDb.withNormalizedFunctions)

val matchService = new SubstitutionMatchService(db)
val resolver = new DefaultSignatureResolver(db)
Expand Down
1 change: 1 addition & 0 deletions http/src/main/scala/org/virtuslab/inkuire/http/Cli.scala
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class Cli extends InputHandler with OutputHandler {
}
.pipe(InkuireDb.combineAll)
.withOrphanTypes
.pipe(InkuireDb.withNormalizedFunctions)
}
.pipe(Future.apply(_))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import org.virtuslab.inkuire.engine.api.FutureExcept
import org.virtuslab.inkuire.engine.api.InkuireDb
import org.virtuslab.inkuire.engine.api.InputHandler
import org.virtuslab.inkuire.engine.impl.service.EngineModelSerializers
import org.virtuslab.inkuire.engine.impl.service.TypeNormalizationOps
import org.virtuslab.inkuire.js.model.JsConfig

import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import scala.util.chaining._

class JSInputHandler(private val scriptPath: String) extends InputHandler {
class JSInputHandler(private val scriptPath: String) extends InputHandler with TypeNormalizationOps {

private def tryGetURLContent(url: String)(implicit ec: ExecutionContext): FutureExcept[String] =
Ajax
Expand Down Expand Up @@ -52,6 +53,7 @@ class JSInputHandler(private val scriptPath: String) extends InputHandler {
}
.pipe(InkuireDb.combineAll)
.withOrphanTypes
.pipe(InkuireDb.withNormalizedFunctions)
}
.pipe(FutureExcept.fromFuture)
}.value
Expand Down

0 comments on commit cb6cda8

Please sign in to comment.