8000 dramatically simplify `ProductZipper` by arainko · Pull Request #130 · arainko/ducktape · GitHub
[go: up one dir, main page]

Skip to content
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
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ ThisBuild / startYear := Some(2023)
ThisBuild / licenses := Seq(License.Apache2)
ThisBuild / developers := List(tlGitHubDev("arainko", "Aleksander Rainko"))
ThisBuild / tlSonatypeUseLegacyHost := false
ThisBuild / scalaVersion := "3.3.1"
ThisBuild / scalaVersion := "3.3.2"

ThisBuild / semanticdbEnabled := true
ThisBuild / semanticdbVersion := scalafixSemanticdb.revision
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,21 @@ object FalliblePlanInterpreter {
}

private enum Value[F[+x]] {
final def wrapped[A](F: TransformationMode[F], tpe: Type[A])(using Quotes, Type[F]): Expr[F[A]] =
final def wrapped[A](F: TransformationMode[F], tpe: Type[A])(using Quotes, Type[F]): Expr[F[A]] = {
given Type[A] = tpe

this match
case Unwrapped(value) =>
'{ ${ F.value }.pure[A](${ value.asExprOf[A] }) }
case Wrapped(value) =>
value.asExprOf[F[A]]
}

case Wrapped(value) => value.asExprOf[F[A]]
final def asFieldValue(name: String, tpe: Type[?]): Either[FieldValue.Unwrapped, FieldValue.Wrapped[F]] =
this match {
case unw: Unwrapped[F] => Left(new FieldValue.Unwrapped(name, tpe, unw.value))
case wrapped: Wrapped[F] => Right(new FieldValue.Wrapped(name, tpe, wrapped.value))
}

case Unwrapped(value: Expr[Any])
case Wrapped(value: Expr[F[Any]])
Expand All @@ -189,17 +196,12 @@ object FalliblePlanInterpreter {
import quotes.reflect.*

val (unwrapped, wrapped) =
fieldPlans.map {
fieldPlans.partitionMap {
case (fieldName, p: Plan.Configured[Fallible]) =>
(fieldName, p.dest.tpe, recurse(p, value, F))
recurse(p, value, F).asFieldValue(fieldName, p.dest.tpe)
case (fieldName, plan) =>
val fieldValue = value.accessFieldByName(fieldName).asExpr
(fieldName, plan.dest.tpe, recurse(plan, fieldValue, F))
}.partitionMap {
case (fieldName, tpe, Value.Unwrapped(value)) =>
Left(ProductZipper.Field.Unwrapped(ProductZipper.Field(fieldName, tpe), value))
case (fieldName, tpe, Value.Wrapped(value)) =>
Right(ProductZipper.Field.Wrapped(ProductZipper.Field(fieldName, tpe), value))
recurse(plan, fieldValue, F).asFieldValue(fieldName, plan.dest.tpe)
}

plan.dest.tpe match {
Expand All @@ -214,7 +216,7 @@ object FalliblePlanInterpreter {
}
}
.getOrElse {
val fields = unwrapped.map(field => field.field. 8000 name -> field.value).toMap
val fields = unwrapped.map(field => field.name -> field.value).toMap
Value.Unwrapped(construct(fields))
}
case TransformationMode.FailFast(f) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.github.arainko.ducktape.internal

import scala.quoted.*

private[ducktape] enum FieldValue {
def name: String
def tpe: Type[?]

case Wrapped[F[+x]](name: String, tpe: Type[?], value: Expr[F[Any]])
case Unwrapped(name: String, tpe: Type[?], value: Expr[Any])
}

object FieldValue {
extension [F[+x]](wrapped: FieldValue.Wrapped[F]) {
def unwrapped(value: Expr[Any]): FieldValue.Unwrapped = FieldValue.Unwrapped(wrapped.name, wrapped.tpe, value)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private[ducktape] object NonEmptyList {
private[ducktape] given [A: Debug]: Debug[NonEmptyList[A]] = Debug.collection[A, List]

extension [A](self: NonEmptyList[A]) {
export toList.{ foldLeft, reduceLeft, head, tail, exists, filter, collect }
export toList.{ foldLeft, reduceLeft, head, tail, exists, filter, collect, toVector }

private[ducktape] def toList: ::[A] = self

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ private[ducktape] object ProductBinder {

def nestFlatMapsAndConstruct[F[+x]: Type, Dest: Type](
F: Expr[Mode.FailFast[F]],
unwrappedFields: List[ProductZipper.Field.Unwrapped],
wrappedFields: List[ProductZipper.Field.Wrapped[F]],
unwrappedFields: List[FieldValue.Unwrapped],
wrappedFields: List[FieldValue.Wrapped[F]],
construct: ProductConstructor
)(using Quotes): Expr[F[Dest]] = {

def recurse(
leftoverFields: List[ProductZipper.Field.Wrapped[F]],
collectedUnwrappedFields: List[ProductZipper.Field.Unwrapped]
leftoverFields: List[FieldValue.Wrapped[F]],
collectedUnwrappedFields: List[FieldValue.Unwrapped]
)(using Quotes): Expr[F[Dest]] =
leftoverFields match {
case ProductZipper.Field.Wrapped(field, wrappedValue) :: Nil =>
case (field @ FieldValue.Wrapped(_, _, wrappedValue)) :: Nil =>
field.tpe match {
case '[destField] =>
val value = wrappedValue.asExprOf[F[destField]]
Expand All @@ -35,7 +35,7 @@ private[ducktape] object ProductBinder {
// so there are Exprs being constructed in a wrong way somewhere (this only occurts when falling back to the non-fallible PlanInterpreter)
val fields =
((field.name -> unwrappedValue) :: collectedUnwrappedFields.map(f =>
f.field.name -> alignOwner(f.value)
f.name -> alignOwner(f.value)
)).toMap
construct(fields).asExprOf[Dest]
)
Expand All @@ -44,7 +44,7 @@ private[ducktape] object ProductBinder {
}
}

case ProductZipper.Field.Wrapped(field, wrappedValue) :: next =>
case (field @ FieldValue.Wrapped(_, _, wrappedValue)) :: next =>
field.tpe match {
case '[destField] =>
val value = wrappedValue.asExprOf[F[destField]]
Expand All @@ -54,17 +54,15 @@ private[ducktape] object ProductBinder {
${
generateLambda[F, destField, Dest](
field,
q ?=>
unwrappedValue =>
recurse(next, ProductZipper.Field.Unwrapped(field, unwrappedValue) :: collectedUnwrappedFields)(using q)
q ?=> unwrappedValue => recurse(next, field.unwrapped(unwrappedValue) :: collectedUnwrappedFields)(using q)
)
}
)
}
}

case Nil =>
val fields = collectedUnwrappedFields.map(f => f.field.name -> f.value).toMap
val fields = collectedUnwrappedFields.map(f => f.name -> f.value).toMap
def constructedValue(using Quotes) = construct(fields).asExprOf[Dest]
'{ $F.pure[Dest]($constructedValue) }
}
Expand All @@ -81,7 +79,7 @@ private[ducktape] object ProductBinder {
// this probably warrants a crash report?
@nowarn // todo: use @unchecked?
private def generateLambda[F[+x]: Type, A: Type, B: Type](
field: ProductZipper.Field,
field: FieldValue,
f: (Quotes) ?=> Expr[A] => Expr[F[B]]
)(using Quotes) = {
import quotes.reflect.*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,10 @@ import io.github.arainko.ducktape.Mode

import scala.quoted.*

object ProductZipper {
case class Field(name: String, tpe: Type[?])

object Field {
final case class Wrapped[F[+x]](field: Field, value: Expr[F[Any]])
final case class Unwrapped(field: Field, value: Expr[Any])
}

import Field.*
private[ducktape] object ProductZipper {

/**
* 'Zips' a product using Transformer.Accumulating.Support[F]#product into a nested Tuple2 then
* 'Zips' a product using Mode.Accumulating[F]#product into a nested Tuple2 then
* unpacks that said tuple and allows the caller to use the unpacked fields to construct a tree of type Dest.
*
* @param wrappedFields fields wrapped in F
Expand All @@ -24,152 +16,77 @@ object ProductZipper {
*/
def zipAndConstruct[F[+x]: Type, Dest: Type](
F: Expr[Mode.Accumulating[F]],
wrappedFields: NonEmptyList[Field.Wrapped[F]],
unwrappedFields: List[Field.Unwrapped]
wrappedFields: NonEmptyList[FieldValue.Wrapped[F]],
unwrappedFields: List[FieldValue.Unwrapped]
)(construct: ProductConstructor)(using Quotes): Expr[F[Dest]] = {
zipFields[F](F, wrappedFields) match {
// ducktape 0.2.x changed the zipping and unzipping algorithm, to emulate the 0.1 runtime behavior we need to zip those fields in reverse
val reorderedFields = wrappedFields.reverse

zipFields[F](F, reorderedFields) match {
case '{ $zipped: F[a] } =>
'{
$F.map(
$zipped,
value => ${ unzipAndConstruct[Dest](wrappedFields, unwrappedFields, 'value, construct) }
value => ${ unzipAndConstruct[Dest](reorderedFields, unwrappedFields, 'value, construct) }
)
}
}
}

private def zipFields[F[+x]: Type](
F: Expr[Mode.Accumulating[F]],
wrappedFields: NonEmptyList[Field.Wrapped[F]]
wrappedFields: NonEmptyList[FieldValue.Wrapped[F]]
)(using Quotes): Expr[F[Any]] =
wrappedFields.map(_.value).reduceLeft { (accumulated, current) =>
(accumulated -> current) match {
case '{ $accumulated: F[a] } -> '{ $current: F[b] } =>
'{ $F.product[`a`, `b`]($accumulated, $current) }
'{ $F.product[`b`, `a`]($current, $accumulated) }
}
}

private def unzipAndConstruct[Dest: Type](
wrappedFields: NonEmptyList[Field.Wrapped[?]],
unwrappedFields: List[Field.Unwrapped],
wrappedFields: NonEmptyList[FieldValue.Wrapped[?]],
unwrappedFields: List[FieldValue.Unwrapped],
nestedPairs: Expr[Any],
construct: ProductConstructor
)(using Quotes) = {
import quotes.reflect.*

ProductZipper.unzip(nestedPairs, wrappedFields) match {
case (bind: Bind, unzippedFields) =>
// TODO: Come back to this at some point, owners of the collected unwrapped defs need to be aligned to Symbol.spliceOwner
// so there are Exprs being constructed in a wrong way somewhere (this only occurts when falling back to the non-fallible PlanInterpreter)
val fields = (unzippedFields ::: unwrappedFields).map(field => field.field.name -> alignOwner(field.value)).toMap
Match(
nestedPairs.asTerm,
CaseDef(
bind,
None,
construct(fields).asTerm
) :: Nil
).asExprOf[Dest]

case (pattern: Unapply, unzippedFields) =>
// workaround for https://github.com/lampepfl/dotty/issues/16784
val matchErrorBind = Symbol.newBind(Symbol.spliceOwner, "x", Flags.EmptyFlags, TypeRepr.of[Any])
val wronglyMatchedReference = Ref(matchErrorBind).asExpr
val matchErrorCase =
CaseDef(Bind(matchErrorBind, Wildcard()), None, '{ throw new MatchError($wronglyMatchedReference) }.asTerm)

// TODO: Come back to this at some point, owners of the collected unwrapped defs need to be aligned to Symbol.spliceOwner
// so there are Exprs being constructed in a wrong way somewhere (this only occurts when falling back to the non-fallible PlanInterpreter)
val fields = (unzippedFields ::: unwrappedFields).map(field => field.field.name -> alignOwner(field.value)).toMap

Match(
nestedPairs.asTerm,
CaseDef(
pattern,
None,
construct(fields).asTerm
) :: matchErrorCase :: Nil
).asExprOf[Dest]
}
val unzippedFields = ProductZipper.unzip(nestedPairs, wrappedFields)
val fields = (unzippedFields ::: unwrappedFields).map(field => field.name -> alignOwner(field.value)).toMap
construct(fields).asExprOf[Dest]
}

private def alignOwner(expr: Expr[Any])(using Quotes) = {
import quotes.reflect.*
expr.asTerm.changeOwner(Symbol.spliceOwner).asExpr
}

/**
* Imagine you have a value of type: Tuple2[Tuple2[Tuple2[Int, Double], Float], String], eg.
* val whatever: Tuple2[Tuple2[Tuple2[Int, Double], Float], String] = 1 -> 1d -> 1f -> "4"
*
* and fields that were 'zipped' into this shape with `PartialTransformer.Accumulating.Support#product`
* by applying this operation left to right eg.
* (field1: Int).product(field2: Double).product(field3: Float)
*
* The fields need to be provided in THE EXACT SAME ORDER they were zipped so for the operation above we'd expect
* List(field1, field2, field3, field4).
*
* This method will generate a tree that unpacks such an associated tuple, eg. for the example above we'd get a tree that corresponds to
* a pattern match:
* case Tuple2(Tuple2(Tuple(field1, field2), field3), field4)
*
* and a list of unwrapped fields that allow you to operate on the bound values of the pattern match.
*/
private def unzip(
nestedPairs: Expr[Any],
fields: NonEmptyList[Field.Wrapped[?]]
_fields: NonEmptyList[FieldValue.Wrapped[?]]
)(using Quotes) = {
import quotes.reflect.*

def recurse(
tpe: Type[?],
leftoverFields: List[Field.Wrapped[?]]
)(using Quotes): (quotes.reflect.Unapply | quotes.reflect.Bind, List[Field.Unwrapped]) = {
import quotes.reflect.*

(tpe -> leftoverFields) match {
case ('[Tuple2[first, second]], Field.Wrapped(firstField, _) :: Field.Wrapped(secondField, _) :: Nil) =>
val firstTpe = TypeRepr.of[second]
val secondTpe = TypeRepr.of[first]
val firstBind = Symbol.newBind(Symbol.spliceOwner, firstField.name, Flags.Case, firstTpe)
val secondBind = Symbol.newBind(Symbol.spliceOwner, secondField.name, Flags.Case, secondTpe)
val fields =
List(Field.Unwrapped(secondField, Ref(secondBind).asExpr), Field.Unwrapped(firstField, Ref(firstBind).asExpr))
val extractor =
Unapply(Tuple2Extractor(secondTpe, firstTpe), Nil, Bind(secondBind, Wildcard()) :: Bind(firstBind, Wildcard()) :: Nil)
extractor -> fields

case ('[tpe], Field.Wrapped(field, _) :: Nil) =>
val tpe = TypeRepr.of(using field.tpe)
val bind = Symbol.newBind(Symbol.spliceOwner, field.name, Flags.Case, tpe)
Bind(bind, Wildcard()) -> (Field.Unwrapped(field, Ref(bind).asExpr) :: Nil)

case ('[Tuple2[rest, current]], Field.Wrapped(field, _) :: tail) =>
val restTpe = TypeRepr.of[rest]
val currentTpe = TypeRepr.of[current]
val pairExtractor = Tuple2Extractor(restTpe, currentTpe)
val bind = Symbol.newBind(Symbol.spliceOwner, field.name, Flags.Case, currentTpe)
val (pattern, unzippedFields) = recurse(summon[Type[rest]], tail)
val extractor = Unapply(pairExtractor, Nil, pattern :: Bind(bind, Wildcard()) :: Nil)
val fields = Field.Unwrapped(field, Ref(bind).asExpr) :: unzippedFields
extractor -> fields

case (tpe, fields) =>
val printedType = TypeRepr.of(using tpe).show
report.errorAndAbort(s"Unexpected state reached while unzipping a product, tpe: $printedType, fields: ${fields}")

}
val fields = _fields.toVector
val size = fields.size

if size == 1 then {
fields.head.unwrapped(nestedPairs) :: Nil
} else {
fields.indices.map { idx =>
if idx == 0 then fields(idx).unwrapped(unpackRight(nestedPairs, size - 1).asExpr)
else fields(idx).unwrapped(unpackLeft(unpackRight(nestedPairs, size - 1 - idx).asExpr).asExpr)
}.toList
}

recurse(nestedPairs.asTerm.tpe.asType, fields.reverse.toList)
}

private def Tuple2Extractor(using Quotes)(A: quotes.reflect.TypeRepr, B: quotes.reflect.TypeRepr) = {
private def unpackRight(expr: Expr[Any], times: Int)(using Quotes): quotes.reflect.Term = {
import quotes.reflect.*

Select
.unique('{ Tuple2 }.asTerm, "unapply")
.appliedToTypes(A :: B :: Nil)
if (times == 0) expr.asTerm
else Select.unique(unpackRight(expr, times - 1), "_2")
}

private def unpackLeft(expr: Expr[Any])(using Quotes): quotes.reflect.Term = {
import quotes.reflect.*
Select.unique(expr.asTerm, "_1")
}
}
0