gpt4 book ai didi

scala - 来自对余积进行运算的单态函数的多函数

转载 作者:行者123 更新时间:2023-12-01 15:00:11 25 4
gpt4 key购买 nike

我正在开发一个 API,该 API 应该能够从对某些副产品类型进行操作的标准单态函数动态构建无形状的 Poly1 函数。

目标是公开一个接收函数的简单方法:

type FooCoproduct = Foo :+: Bar :+: CNil
def addF[E](f: E => E)(implicit ev: Inject[FooCoproduct, E]) = ???

并累积这些函数以构建一个涵盖余积中所有类型的总 Poly1 函数。此处的证据 ev 强制类型参数 E 是余积中的类型。

在测试了几种方法(包括类型类的泛型派生)之后,最有前途的方法使我在 HList 中积累了这些单态函数,并尝试通过 解析适用的方法>选择器。这可能通过示例更好地理解:

object CoproductSample extends App {

import shapeless.{ :+:, CNil, Coproduct, HList, HNil, Poly1 }
import shapeless.ops.coproduct.Inject
import shapeless.ops.hlist.Selector

class Builder[A <: Coproduct] {

def accum[B](f: B => B, hl: HList)(implicit ev: Inject[A, B]) = f :: hl

class PolyBuilder[L <: HList](hl: L) extends Poly1 {
implicit def run[T](implicit ev: Selector[L, T => T]) =
at[T](hl.select[T => T])
}

}

type Cop = Int :+: String :+: CNil

val builder = new Builder[Cop]

val hl1 = builder.accum((i: Int) => i + 1, HNil)
val hl2 = builder.accum((s: String) => s + "one", hl1)

object pf extends builder.PolyBuilder(hl2)

val rInt = Coproduct[Cop](10).fold(pf)
val rStr = Coproduct[Cop]("ten").fold(pf)
}

这段代码没有编译消息:

could not find implicit value for parameter folder:     
shapeless.ops.coproduct.Folder[CoproductSample.pf.type, CoproductSample.Cop]

我想我需要提供一个 Selector[L, T => T],其中 L 是累积的 HList 的类型但我想不出这样做的方法。另一方面,我觉得我的问题必须有更简单的解决方案。

如有任何帮助,我们将不胜感激。

更新

在进行更多研究后,我想出了一个几乎可行的解决方案。不幸的是,我无法正确跟踪结果类型。

object CoproductSample {
import shapeless.{ CNil, Coproduct, HList, HNil, Inl, Inr, Poly2, ::, :+: }

// Accumulates ordinary functions from A => A in an HList
def accum[A, L <: HList](f: A => A, hl: L): (A => A) :: L = f :: hl

// A poly2 function that evaluates some monomorphic function present in
// an HList for certain value that satifies the signature of this function
object PolyEval extends Poly2 {
implicit def hnilCase[A]: Case.Aux[A, HNil, Option[A]] =
at[A, HNil]((a, l) => None)

implicit def hheadCaseSuccess[A, T <: HList]: Case.Aux[A, (A => A) :: T, Option[A]] =
at[A, (A => A) :: T]((a: A, l: (A => A) :: T) => Option(l.head(a)))

implicit def hheadCaseFail[A, H, T <: HList](
implicit tail: Case.Aux[A, T, Option[A]]
): Case.Aux[A, (H => H) :: T, Option[A]] =
at[A, (H => H) :: T]((a: A, l: (H => H) :: T) => PolyEval(a, l.tail))
}

// A poly2 function that uses `PolyEval` for evaluating a value present in
// a coproduct against an HList of monomorphic functions
object PolyEvalCop extends Poly2 {
implicit def cnilCase[A <: CNil, L <: HList]: Case.Aux[A, L, Option[A]] =
at[A, L]((a, l) => sys.error("Impossible!"))

implicit def cconsCase[H, T <: Coproduct, L <: HList](
implicit head: PolyEval.Case.Aux[H, L, Option[H]],
tail: Case[T, L]) // What is the return type here???)
= at[H :+: T, L]((c, l) =>
c match {
case Inl(h) => PolyEval(h, l)
case Inr(t) => PolyEvalCop(t, l)
})
}
}

控制台 session :

scala> import shapeless._, CoproductSample._
import shapeless._
import CoproductSample._

scala> case class Foo(i: Int); case class Bar(s: String)
defined class Foo
defined class Bar

scala> val f = (foo: Foo) => foo.copy(i = foo.i * 2)
f: Foo => Foo = <function1>

scala> val g = (bar: Bar) => bar.copy(s = bar.s + "_changed!")
g: Bar => Bar = <function1>

scala> val hl = accum(g, accum(f, HNil))
hl: shapeless.::[Bar => Bar,shapeless.::[Foo => Foo,shapeless.HNil.type]] = <function1> :: <function1> :: HNil

scala> type C = Foo :+: Bar :+: CNil
defined type alias C

scala> PolyEvalCop(Coproduct[C](Foo(10)), hl)
res1: Any = Some(Foo(20))

scala> PolyEvalCop(Coproduct[C](Bar("bar")), hl)
res2: Any = Some(Bar(bar_changed!))

未正确跟踪结果类型,它被解析为 Any

最佳答案

addF 的签名来看,您似乎想要映射到 Coproduct 上,即修改它的值并保留在一个副产品上,即 def add[ E](e:E):E,如果是这种情况,这将有效:

@ {
trait Add[E]{
def add(e:E):E
}
object Add{
def apply[E:Add]:Add[E] = implicitly[Add[E]]
implicit object cnil extends Add[CNil] {
def add(e:CNil) = throw new RuntimeException("Impossible")
}
implicit def coproduct[H, T <: Coproduct](
implicit
addH: Add[H],
addT:Add[T],
basis: ops.coproduct.Basis[H :+: T,T]
):Add[H :+: T] = new Add[H :+: T]{
def add(e: H :+: T) = e match {
case Inl(h) => Coproduct[H :+: T](addH.add(h)) // to stay in the Coproduct
case Inr(t) => addT.add(t).embed[H :+: T] // to stay in the coproduct
}
}
}
}
defined trait Add
defined object Add
@ implicit def addString = new Add[String] {
def add(e:String) = e + "-ah"
}
defined function addString
@ implicit def addInt = new Add[Int] {
def add(e:Int) = e + 1
}
defined function addInt
@ type C = Int :+: String :+: CNil
defined type C
@ val i = Coproduct[C](1)
i: C = 1
@ Add[C].add(i)
res24: C = 2 // notice that the return type is C
@ val s = Coproduct[C]("a")
s: C = a
@ Add[C].add(s)
res26: C = a-ah // notice that the return type is C

它显然适用于“普通”类型:

@ Add[Int].add(1)
res38: Int = 2

以上等同于map;但是如果你想要一个 fold,即 def add[E](e:E):Int,你只需修改这两行:

case Inl(h) => addH.add(h)
case Inr(t) => addT.add(t)

关于scala - 来自对余积进行运算的单态函数的多函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39489047/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com