Shapeless 中什么时候需要依赖类型?

When are dependent types needed in Shapeless?

据我了解,依赖类型允许您不指定输出类型:

例如,如果您有一个类型 class:

trait Last[In] {
  type Out
}

那么你可以在不指定输出类型的情况下召唤一个实例:

implicitly(Last[String :: Int :: HNil]) // output type calculated as Int

并且 Aux 模式允许您再次指定输出类型:

implicitly(Last.Aux[String :: Int :: HNil, Int])

为了对输出类型 () 做一些有用的事情,您需要在隐式参数列表中使用它。

但是如果你总是需要指定(或分配一个类型参数)输出类型,为什么要首先使用依赖类型(然后是 Aux)呢?

我尝试从 Shapeless 的 src 复制 Last 类型 class,用特征中的附加类型参数替换 type Out 并删除 Aux。它仍然有效。

我真正需要它们的时候是什么情况?

I get that Sum[A, B] is not the same as Sum[A, B] { type Out = C } or Sum.Aux[A, B, C]. I'm asking why do I need type Out at all rather than just Sum[A, B, C].

区别在于部分应用。对于 trait MyTrait { type A; type B; type C } 你可以指定一些类型而不指定其他类型(期望编译器推断它们)。但是对于 trait MyTrait[A, B, C] ,您只能指定全部或不指定其中任何一个。 对于 Sum[A, B] { type Out },您更愿意指定 AB 而不是指定 Out(期望编译器根据作用域中存在的隐式推断其值)。类似地,对于 trait Last[In] { type Out },您更愿意指定 In 而不是指定 Out(期望编译器推断其值)。 所以类型参数更像是输入,类型成员更像是输出。

https://www.youtube.com/watch?v=R8GksuRw3VI

Abstract types versus type parameters 和相关问题


But when exactly, would I prefer to specify In and not specify Out?

让我们考虑以下示例。是自然数加法的类型class:

sealed trait Nat
case object Zero extends Nat
type Zero = Zero.type
case class Succ[N <: Nat](n: N) extends Nat

type One = Succ[Zero]
type Two = Succ[One]
type Three = Succ[Two]
type Four = Succ[Three]
type Five = Succ[Four]

val one: One = Succ(Zero)
val two: Two = Succ(one)
val three: Three = Succ(two)
val four: Four = Succ(three)
val five: Five = Succ(four)

trait Add[N <: Nat, M <: Nat] {
  type Out <: Nat
  def apply(n: N, m: M): Out
}

object Add {
  type Aux[N <: Nat, M <: Nat, Out0 <: Nat] = Add[N, M] { type Out = Out0 }
  def instance[N <: Nat, M <: Nat, Out0 <: Nat](f: (N, M) => Out0): Aux[N, M, Out0] = new Add[N, M] {
    override type Out = Out0
    override def apply(n: N, m: M): Out = f(n, m)
  }

  implicit def zeroAdd[M <: Nat]: Aux[Zero, M, M] = instance((_, m) => m)
  implicit def succAdd[N <: Nat, M <: Nat, N_addM <: Nat](implicit add: Aux[N, M, N_addM]): Aux[Succ[N], M, Succ[N_addM]] =
    instance((succN, m) => Succ(add(succN.n, m)))
}

此类型 class 在类型级别上均有效

implicitly[Add.Aux[Two, Three, Five]]

和价值水平

println(implicitly[Add[Two, Three]].apply(two, three))//Succ(Succ(Succ(Succ(Succ(Zero)))))
assert(implicitly[Add[Two, Three]].apply(two, three) == five)//ok

现在让我们用类型参数而不是类型成员重写它:

trait Add[N <: Nat, M <: Nat, Out <: Nat] {
  def apply(n: N, m: M): Out
}

object Add {
  implicit def zeroAdd[M <: Nat]: Add[Zero, M, M] = (_, m) => m
  implicit def succAdd[N <: Nat, M <: Nat, N_addM <: Nat](implicit add: Add[N, M, N_addM]): Add[Succ[N], M, Succ[N_addM]] =
    (succN, m) => Succ(add(succN.n, m))
}

在类型级别上它的工作方式类似

implicitly[Add[Two, Three, Five]]

但在值级别上,现在您必须指定类型 Five,而在前一种情况下,它是由编译器推断的。

println(implicitly[Add[Two, Three, Five]].apply(two, three))//Succ(Succ(Succ(Succ(Succ(Zero)))))
assert(implicitly[Add[Two, Three, Five]].apply(two, three) == five)//ok

所以区别在于部分应用。


But if you add a + syntax sugar as you normally would to make it practical (shapeless also does it for everything), the dependent type doesn't seem to matter

语法并不总是有帮助。例如,让我们考虑一个类型 class,它接受一个类型(但不接受该类型的值)并生成一个类型和该类型的值:

trait MyTrait {
  type T
}

object Object1 extends MyTrait
object Object2 extends MyTrait

trait TypeClass[In] {
  type Out
  def apply(): Out
}

object TypeClass {
  type Aux[In, Out0] = TypeClass[In] { type Out = Out0 }
  def instance[In, Out0](x: Out0): Aux[In, Out0] = new TypeClass[In] {
    override type Out = Out0
    override def apply(): Out = x
  }

  def apply[In](implicit tc: TypeClass[In]): Aux[In, tc.Out] = tc

  implicit val makeInstance1: Aux[Object1.T, Int] = instance(1)
  implicit val makeInstance2: Aux[Object2.T, String] = instance("a")
}

println(TypeClass[Object1.T].apply())//1
println(TypeClass[Object2.T].apply())//a

但是如果我们将 Out 设为类型参数,那么在调用时我们将必须指定 Out 并且无法定义扩展方法并从元素推断类型参数 In类型,因为没有 Object1.TObject2.T.

类型的元素