scala - 隐式解析失败?

标签 scala shapeless type-level-computation

我一直在研究 Okasaki's dense binary number system 的“无形风格”实现.它只是一个类型级别的位链表;一种 HList二进制 Digit s。我已经完成了我的操作的初稿,其中包括您对自然数所期望的标准数学运算。直到现在我才意识到我的编码存在一个大问题。如何修复我的 Induction 中的隐式分辨率例子?随意将整个片段粘贴到 REPL 中。在这个例子中,对 shapeless 的唯一依赖是 DepFn1 , 和 DepFn2 .

import shapeless.{ DepFn1, DepFn2 }

sealed trait Digit
case object Zero extends Digit
case object One extends Digit

sealed trait Dense { type N <: Dense }

final case class ::[+H <: Digit, +T <: Dense](digit: H, tail: T) extends Dense {
  type N = digit.type :: tail.N
}

sealed trait DNil extends Dense {
  type N = DNil
}

case object DNil extends DNil

/* ops */
trait IsDCons[N <: Dense] {
  type H <: Digit
  type T <: Dense

  def digit(n: N): H
  def tail(n: N): T
}

object IsDCons {
  type Aux[N <: Dense, H0 <: Digit, T0 <: Dense] = IsDCons[N] {
    type H = H0
    type T = T0
  }

  def apply[N <: Dense](implicit ev: IsDCons[N]): Aux[N, ev.H, ev.T] = ev

  implicit def isDCons[H0 <: Digit, T0 <: Dense]: Aux[H0 :: T0, H0, T0] =
    new IsDCons[H0 :: T0] {
      type H = H0
      type T = T0

      def digit(n: H0 :: T0): H = n.digit
      def tail(n: H0 :: T0): T = n.tail
    }
}

// Disallows Leading Zeros
trait SafeCons[H <: Digit, T <: Dense] extends DepFn2[H, T] { type Out <: Dense }

trait LowPrioritySafeCons {
  type Aux[H <: Digit, T <: Dense, Out0 <: Dense] = SafeCons[H, T] { type Out = Out0 }

  implicit def sc1[H <: Digit, T <: Dense]: Aux[H, T, H :: T] =
    new SafeCons[H, T] {
      type Out = H :: T
      def apply(h: H, t: T) = h :: t
  }
}

object SafeCons extends LowPrioritySafeCons {
  implicit val sc0: Aux[Zero.type, DNil, DNil] =
    new SafeCons[Zero.type, DNil] {
      type Out = DNil
      def apply(h: Zero.type, t: DNil) = DNil
  }
}

trait ShiftLeft[N <: Dense] extends DepFn1[N] { type Out <: Dense }

object ShiftLeft {
  type Aux[N <: Dense, Out0 <: Dense] = ShiftLeft[N] { type Out = Out0 }

  implicit def sl1[T <: Dense](implicit sc: SafeCons[Zero.type, T]): Aux[T, sc.Out] =
    new ShiftLeft[T] {
      type Out = sc.Out
      def apply(n: T) = Zero safe_:: n
    }
}

trait Succ[N <: Dense] extends DepFn1[N] { type Out <: Dense }

object Succ {
  type Aux[N <: Dense, Out0 <: Dense] = Succ[N] { type Out = Out0 }

  def apply[N <: Dense](implicit succ: Succ[N]): Aux[N, succ.Out] = succ

  implicit val succ0: Aux[DNil, One.type :: DNil] =
    new Succ[DNil] {
      type Out = One.type :: DNil
      def apply(DNil: DNil) = One :: DNil
    }

  implicit def succ1[T <: Dense]: Aux[Zero.type :: T, One.type :: T] =
    new Succ[Zero.type :: T] {
      type Out = One.type :: T
      def apply(n: Zero.type :: T) = One :: n.tail
  }

  implicit def succ2[T <: Dense, S <: Dense]
    (implicit ev: Aux[T, S], sl: ShiftLeft[S]): Aux[One.type :: T, sl.Out] =
      new Succ[One.type :: T] {
        type Out = sl.Out
        def apply(n: One.type :: T) = n.tail.succ.shiftLeft
      }
}

/* syntax */
val Cons = ::
implicit class DenseOps[N <: Dense](val n: N) extends AnyVal {
  def ::[H <: Digit](h: H): H :: N = Cons(h, n)

  def safe_::[H <: Digit](h: H)(implicit sc: SafeCons[H, N]): sc.Out = sc(h, n)

  def succ(implicit s: Succ[N]): s.Out = s(n)

  def digit(implicit c: IsDCons[N]): c.H = c.digit(n)

  def tail(implicit c: IsDCons[N]): c.T = c.tail(n)

  def shiftLeft(implicit sl: ShiftLeft[N]): sl.Out = sl(n)
}

/* aliases */
type _0 = DNil
val _0: _0 = DNil

val _1 = _0.succ
type _1 = _1.N

val _2 = _1.succ
type _2 = _2.N

/* test */
trait Induction[A <: Dense]

object Induction{
  def apply[A <: Dense](a: A)(implicit r: Induction[A]) = r
  implicit val r0 = new Induction[_0] {}
  implicit def r1[A <: Dense](implicit r: Induction[A], s: Succ[A]) = 
    new Induction[s.Out]{}
}

Induction(_0)
Induction(_1)
Induction(_2) // <- Could not find implicit value for parameter r...

This is a link to the question's follow up

最佳答案

这是一个有点不完整的答案,但希望它能让你摆脱困境......

我认为你的问题是 r1 的定义这里,

object Induction{
  def apply[A <: Dense](a: A)(implicit r: Induction[A]) = r
  implicit val r0 = new Induction[_0] {}
  implicit def r1[A <: Dense](implicit r: Induction[A], s: Succ[A]) = 
    new Induction[s.Out]{}
}

当您询问 Induction(_2)你希望 r1适用和s.Out固定为 _2这将插入 r1 中从右到左的推理过程s 隐式参数块。

不幸的是,这不会发生。一、s.Out不会固定为 _2因为它不是类型变量。因此,您至少必须将其重写为,
implicit def r1[A <: Dense, SO <: Dense]
  (implicit r: Induction[A], s: Succ.Aux[A, SO]): Induction[SO] = 
    new Induction[SO]{}

r1甚至适用。然而,这不会让你走得更远,因为 SO仅被限制为等于类型成员 Outs ...它在Succ的选择中没有作用s 的实例.我们无法从另一端取得任何进展,因为此时 A就类型检查器而言,这是完全不确定的。

所以恐怕你得重新考虑一下。我认为你最好的方法是定义一个 Pred运算符,它允许您按照这些方式定义某些内容,
implicit def r1[S <: Dense, PO <: Dense]
  (implicit p: Pred.Aux[S, PO], r: Induction[PO]): Induction[S] = 
    new Induction[S]{}

现在当你要求 Induction(_2) S将立即解决为 _2 , Pred _2 的实例将得到解决,产生 _1 的解决方案为 PO这为类型检查器提供了解决归纳下一步所需的信息。

请注意,一般策略是从结果类型 ( Induction[S] ) 开始以修复初始类型变量,然后从左到右遍历隐式参数列表。

另请注意,我已向隐式定义添加了显式结果类型:您几乎应该总是这样做(此规则很少有异常(exception))。

关于scala - 隐式解析失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31768203/

相关文章:

scala - 用于无形 Hlist 的通用 Poly2 文件夹案例

scala - 为什么类体会影响隐式解析?

haskell - 我可以让 GHC 推断出超过 GADT 模式匹配的约束吗?

api - 为什么 Play 2.0.2 中有 Form 的 fold 方法?

python - Scala 相当于 python itertools 'cycle'

scala - sbt中依赖的增量版本号什么时候可以省略?

scala - Shapeless:使用 Coproduct 拥有自己的 HList 约束

haskell - 如何为递归单例类型定义 NFData 实例?

scala - 用于检查正在使用哪些内存选项的 SBT 命令