Skip to content

Commit

Permalink
Revert changes to using clauses which break downstream code
Browse files Browse the repository at this point in the history
  • Loading branch information
propensive committed Jun 16, 2024
1 parent 18169ff commit a59c3f2
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 49 deletions.
63 changes: 34 additions & 29 deletions src/core/wisteria.ProductDerivationMethods.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import scala.deriving.*
import scala.compiletime.*

trait ProductDerivationMethods[TypeclassType[_]]:
protected transparent inline def construct[DerivationType <: Product: ProductReflection]
(using requirement: ContextRequirement)
protected transparent inline def construct[DerivationType <: Product]
(using reflection: ProductReflection[DerivationType], requirement: ContextRequirement)
(inline lambda: [FieldType] =>
requirement.Optionality[TypeclassType[FieldType]] =>
(typeclass: requirement.Optionality[TypeclassType[FieldType]],
Expand All @@ -35,17 +35,18 @@ trait ProductDerivationMethods[TypeclassType[_]]:
FieldType)
: DerivationType =

type Fields = DerivationType.MirroredElemTypes
type Labels = DerivationType.MirroredElemLabels
type Fields = reflection.MirroredElemTypes
type Labels = reflection.MirroredElemLabels

DerivationType.fromProduct:
reflection.fromProduct:
fold[DerivationType, Fields, Labels, Tuple](EmptyTuple, 0): accumulator =>
[FieldType] => context ?=> lambda[FieldType](context) *: accumulator
.reverse

protected transparent inline def constructWith[ConstructorType[_]]
(using requirement: ContextRequirement)
[DerivationType <: Product: ProductReflection]
[DerivationType <: Product]
(using reflection: ProductReflection[DerivationType])
(inline bind: [InputType, OutputType] => ConstructorType[InputType] =>
(InputType => ConstructorType[OutputType]) =>
ConstructorType[OutputType],
Expand All @@ -59,8 +60,8 @@ trait ProductDerivationMethods[TypeclassType[_]]:
ConstructorType[FieldType])
: ConstructorType[DerivationType] =

type Fields = DerivationType.MirroredElemTypes
type Labels = DerivationType.MirroredElemLabels
type Fields = reflection.MirroredElemTypes
type Labels = reflection.MirroredElemLabels

val tuple: ConstructorType[Tuple] =
fold[DerivationType, Fields, Labels, ConstructorType[Tuple]](pure(EmptyTuple), 0):
Expand All @@ -71,10 +72,10 @@ trait ProductDerivationMethods[TypeclassType[_]]:
pure(result *: accumulator2)

bind(tuple): tuple =>
pure(DerivationType.fromProduct(tuple.reverse))
pure(reflection.fromProduct(tuple.reverse))

protected transparent inline def contexts[DerivationType <: Product: ProductReflection]
(using requirement: ContextRequirement)
protected transparent inline def contexts[DerivationType <: Product]
(using reflection: ProductReflection[DerivationType], requirement: ContextRequirement)
[ResultType]
(inline lambda: [FieldType] =>
requirement.Optionality[TypeclassType[FieldType]] =>
Expand All @@ -86,42 +87,46 @@ trait ProductDerivationMethods[TypeclassType[_]]:
ResultType)
: IArray[ResultType] =

type Fields = DerivationType.MirroredElemTypes
type Labels = DerivationType.MirroredElemLabels
type Fields = reflection.MirroredElemTypes
type Labels = reflection.MirroredElemLabels

summonInline[ClassTag[ResultType]].give:
IArray.create[ResultType](valueOf[Tuple.Size[Fields]]): array =>
fold[DerivationType, Fields, Labels, Unit]((), 0): accumulator =>
[FieldType] => context ?=> array(index) = lambda[FieldType](context)

inline def typeName[DerivationType: Reflection]: Text = valueOf[DerivationType.MirroredLabel].tt
inline def typeName[DerivationType](using reflection: Reflection[DerivationType]): Text =
valueOf[reflection.MirroredLabel].tt

inline def tuple[DerivationType: Reflection]: Boolean = compiletime.summonFrom:
case given (DerivationType.MirroredMonoType <:< Tuple) => true
case _ => false
inline def tuple[DerivationType](using reflection: Reflection[DerivationType]): Boolean =
compiletime.summonFrom:
case given (reflection.MirroredMonoType <:< Tuple) => true
case _ => false

inline def singleton[DerivationType: Reflection]: Boolean = compiletime.summonFrom:
case given (DerivationType.MirroredMonoType <:< Singleton) => true
case _ => false
inline def singleton[DerivationType](using reflection: Reflection[DerivationType]): Boolean =
compiletime.summonFrom:
case given (reflection.MirroredMonoType <:< Singleton) => true
case _ => false

protected transparent inline def complement
[DerivationType <: Product: ProductReflection, FieldType]
protected transparent inline def complement[DerivationType <: Product, FieldType]
(product: DerivationType)
(using fieldIndex: Int & FieldIndex[FieldType], requirement: ContextRequirement)
(using fieldIndex: Int & FieldIndex[FieldType],
reflection: ProductReflection[DerivationType],
requirement: ContextRequirement)
: FieldType =

type Labels = DerivationType.MirroredElemLabels
type Fields = DerivationType.MirroredElemTypes
type Labels = reflection.MirroredElemLabels
type Fields = reflection.MirroredElemTypes
val tuple: Fields = Tuple.fromProductTyped(product)

fold[DerivationType, Fields, Labels, Optional[FieldType]](tuple, Unset, 0):
accumulator => [FieldType2] => field =>
if index == fieldIndex then field.asInstanceOf[FieldType] else accumulator
.vouch(using Unsafe)

protected transparent inline def fields[DerivationType <: Product: ProductReflection]
(inline product: DerivationType)
protected transparent inline def fields[DerivationType <: Product](inline product: DerivationType)
(using requirement: ContextRequirement)
(using reflection: ProductReflection[DerivationType])
[ResultType]
(inline lambda: [FieldType] =>
FieldType =>
Expand All @@ -133,8 +138,8 @@ trait ProductDerivationMethods[TypeclassType[_]]:
: IArray[ResultType] =

summonInline[ClassTag[ResultType]].give:
type Labels = DerivationType.MirroredElemLabels
type Fields = DerivationType.MirroredElemTypes
type Labels = reflection.MirroredElemLabels
type Fields = reflection.MirroredElemTypes
val tuple: Fields = Tuple.fromProductTyped(product)

IArray.create[ResultType](tuple.size): array =>
Expand Down
41 changes: 21 additions & 20 deletions src/core/wisteria.SumDerivationMethods.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,24 @@ trait SumDerivationMethods[TypeclassType[_]]:
case _: (variantType *: variantsType) => all[variantType, variantsType]
case _ => false

protected transparent inline def complement[DerivationType: SumReflection, VariantType]
(sum: DerivationType)
(using variantIndex: Int & VariantIndex[VariantType])
protected transparent inline def complement[DerivationType, VariantType](sum: DerivationType)
(using variantIndex: Int & VariantIndex[VariantType],
reflection: SumReflection[DerivationType])
: Optional[VariantType] =

type Labels = DerivationType.MirroredElemLabels
type Variants = DerivationType.MirroredElemTypes
val size: Int = valueOf[Tuple.Size[DerivationType.MirroredElemTypes]]
type Labels = reflection.MirroredElemLabels
type Variants = reflection.MirroredElemTypes
val size: Int = valueOf[Tuple.Size[reflection.MirroredElemTypes]]

fold[DerivationType, Variants, Labels](sum, size, 0, false)(index == DerivationType.ordinal(sum)):
fold[DerivationType, Variants, Labels](sum, size, 0, false)(index == reflection.ordinal(sum)):
[VariantType2 <: DerivationType] => field =>
if index == variantIndex then field.asInstanceOf[VariantType] else Unset

protected inline def variantLabels[DerivationType: SumReflection]: List[Text] =
protected inline def variantLabels[DerivationType]
(using reflection: SumReflection[DerivationType])
: List[Text] =

constValueTuple[DerivationType.MirroredElemLabels].toList.map(_.toString.tt)
constValueTuple[reflection.MirroredElemLabels].toList.map(_.toString.tt)

protected transparent inline def delegate[DerivationType](label: Text)
(using reflection: SumReflection[DerivationType], requirement: ContextRequirement)
Expand All @@ -76,8 +78,8 @@ trait SumDerivationMethods[TypeclassType[_]]:
[VariantType <: DerivationType] => context => lambda[VariantType](context)
.vouch(using Unsafe)

protected transparent inline def variant[DerivationType: SumReflection](sum: DerivationType)
(using requirement: ContextRequirement)
protected transparent inline def variant[DerivationType](sum: DerivationType)
(using reflection: SumReflection[DerivationType], requirement: ContextRequirement)
[ResultType]
(inline lambda: [VariantType <: DerivationType] =>
VariantType =>
Expand All @@ -87,19 +89,18 @@ trait SumDerivationMethods[TypeclassType[_]]:
ResultType)
: ResultType =

type Labels = DerivationType.MirroredElemLabels
type Variants = DerivationType.MirroredElemTypes
type Labels = reflection.MirroredElemLabels
type Variants = reflection.MirroredElemTypes

val size: Int = valueOf[Tuple.Size[DerivationType.MirroredElemTypes]]
val size: Int = valueOf[Tuple.Size[reflection.MirroredElemTypes]]

fold[DerivationType, Variants, Labels](sum, size, 0, false)(index == DerivationType.ordinal(sum)):
fold[DerivationType, Variants, Labels](sum, size, 0, false)(index == reflection.ordinal(sum)):
[VariantType <: DerivationType] => variant => lambda[VariantType](variant)
.vouch(using Unsafe)

private transparent inline def fold
[DerivationType: SumReflection, VariantsType <: Tuple, LabelsType <: Tuple]
private transparent inline def fold[DerivationType, VariantsType <: Tuple, LabelsType <: Tuple]
(inline inputLabel: Text, size: Int, index: Int, fallible: Boolean)
(using requirement: ContextRequirement)
(using reflection: SumReflection[DerivationType], requirement: ContextRequirement)
(inline predicate: (label: Text, index: Int & VariantIndex[DerivationType]) ?=> Boolean)
[ResultType]
(inline lambda: [VariantType <: DerivationType] =>
Expand Down Expand Up @@ -136,9 +137,9 @@ trait SumDerivationMethods[TypeclassType[_]]:
then raise(VariantError[DerivationType](inputLabel))(Unset)(using summonInline[Errant[VariantError]])
else throw Panic(msg"Should be unreachable")

private transparent inline def fold[DerivationType: SumReflection, VariantsType <: Tuple, LabelsType <: Tuple]
private transparent inline def fold[DerivationType, VariantsType <: Tuple, LabelsType <: Tuple]
(inline sum: DerivationType, size: Int, index: Int, fallible: Boolean)
(using requirement: ContextRequirement)
(using reflection: SumReflection[DerivationType], requirement: ContextRequirement)
(inline predicate: (label: Text, index: Int & VariantIndex[DerivationType]) ?=> Boolean)
[ResultType]
(inline lambda: [VariantType <: DerivationType] =>
Expand Down

0 comments on commit a59c3f2

Please sign in to comment.