从Idris到Scala的通用加法器?

从Idris到Scala的通用加法器?,scala,shapeless,idris,Scala,Shapeless,Idris,介绍了以下通用加法器方法: AdderType : (numArgs : Nat) -> Type AdderType Z = Int AdderType (S k) = (next : Int) -> AdderType k adder : (n : Nat) -> (acc : Int) -> AdderType n adder Z acc = acc adder (S k) acc = \x => (adder k (x+acc)) 例如:

介绍了以下通用加法器方法:

AdderType : (numArgs : Nat) -> Type
AdderType Z     = Int
AdderType (S k) = (next : Int) -> AdderType k

adder : (n : Nat) -> (acc : Int) -> AdderType n
adder Z acc     = acc
adder (S k) acc = \x => (adder k (x+acc))
例如:

-- expects 3 Int's to add, with a starting value of 0
*Work> :t (adder 3 0) 
adder 3 0 : Int -> Int -> Int -> Int

-- 0 (initial) + 3 + 3 + 3 == 9
*Work> (adder 3 0) 3 3 3
9 : Int
我猜它可以处理上述通用的
加法器
函数


如何在Scala中编写它,有无变形?

更新:我将在下面保留我的原始实现,但这里有一个更直接的实现:

import shapeless._

trait AdderType[N <: Nat] extends DepFn1[Int]

object AdderType {
  type Aux[N <: Nat, Out0] = AdderType[N] { type Out = Out0 }
  def apply[N <: Nat](base: Int)(implicit at: AdderType[N]): at.Out = at(base)

  implicit val adderTypeZero: Aux[Nat._0, Int] = new AdderType[Nat._0] {
    type Out = Int
    def apply(x: Int): Int = x
  }

  implicit def adderTypeSucc[N <: Nat](implicit
    atN: AdderType[N]
  ): Aux[Succ[N], Int => atN.Out] = new AdderType[Succ[N]] {
    type Out = Int => atN.Out
    def apply(x: Int): Int => atN.Out = i => atN(x + i)
  }
}

它更为冗长,而且表示形式也有点不同,让Scala语法来解决我们的“基本情况”本质上是一个
Int=>Int
而不是
Int
,因为否则我看不到避免编写
apply
()
到处都是,但基本思想完全相同。

如果你在长途旅行中,手上没有不成形的东西,下面是在纯Scala中如何做到这一点的方法。对于那些不熟悉shapeless和出于某种原因不使用shapeless的人来说,它可能很有用

首先,我们需要某种方法来迭代类型,即在类型中表示自然数。您可以使用任何嵌套类型,也可以定义一个新类型,其中包含一些数字别名:

sealed trait Nat

trait Zero extends Nat
trait Succ[N <: Nat] extends Nat

// enough for examples:
type _0 = Zero
type _1 = Succ[_0]
type _2 = Succ[_1]
type _3 = Succ[_2]
type _4 = Succ[_3]
// etc...
现在,为了构造一个
AdderType
的实例并应用它,我们编写了一个函数,该函数取
N
import shapeless._

trait AdderType[N <: Nat] extends DepFn1[Int] {
  protected def plus(x: Int): AdderType.Aux[N, Out]
}

object AdderType {
  type Aux[N <: Nat, Out0] = AdderType[N] { type Out = Out0 }

  def apply[N <: Nat](base: Int)(implicit at: AdderType[N]): Aux[N, at.Out] =
    at.plus(base)

  private[this] case class AdderTypeZero(acc: Int) extends AdderType[Nat._1] {
    type Out = Int
    def apply(x: Int): Int = acc + x
    protected def plus(x: Int): Aux[Nat._1, Int] = copy(acc = acc + x)
  }

  private[this] case class AdderTypeSucc[N <: Nat, Out0](
    atN: Aux[N, Out0],
    acc: Int
  ) extends AdderType[Succ[N]] {
    type Out = Aux[N, Out0]
    def apply(x: Int): Aux[N, Out0] = atN.plus(acc + x)
    protected def plus(x: Int): Aux[Succ[N], Aux[N, Out0]] = copy(acc = acc + x)
  }

  implicit val adderTypeZero: Aux[Nat._1, Int] = AdderTypeZero(0)
  implicit def adderTypeSucc[N <: Nat](implicit
    atN: AdderType[N]
  ): Aux[Succ[N], Aux[N, atN.Out]] = AdderTypeSucc(atN, 0)
}
scala> val at3 = AdderType[Nat._3](0)
at3: AdderType[shapeless.Succ[shapeless.Succ[shapeless.Succ[shapeless._0]]]] { ...

scala> at3(3)(3)(3)
res0: Int = 9
sealed trait Nat

trait Zero extends Nat
trait Succ[N <: Nat] extends Nat

// enough for examples:
type _0 = Zero
type _1 = Succ[_0]
type _2 = Succ[_1]
type _3 = Succ[_2]
type _4 = Succ[_3]
// etc...
// first we define the type which take a Nat type argument
trait AdderType[N <: Nat] {

  type Out
  def apply(i: Int): Out
}

// then we inductively construct its values using implicits
case object AdderType {

  // base case: N = _0
  implicit def zero:
      AdderType[_0] { type Out = Int } =
  new AdderType[_0] {

    type Out = Int
    def apply(i: Int): Out = i
  }

  // induction step: N -> Succ[N]
  implicit def succ[N <: Nat, NOut](
    implicit prev: AdderType[N] { type Out = NOut }
  ):  AdderType[Succ[N]] { type Out = Int => NOut } =
  new AdderType[Succ[N]] {

    type Out = Int => NOut
    def apply(i: Int): Out = k => prev(i + k)
  }
}
def adder[N <: Nat](initial: Int)(
  implicit adderFunction: AdderType[N]
): adderFunction.Out = adderFunction(initial)
scala> val add3Numbers = adder_[_3](0)
add3Numbers: Int => (Int => (Int => Int)) = <function1>

scala> add3Numbers(1)(2)(3)
res0: Int = 6
def adder[N <: Nat, NOut](n: NatVal[N])(initial: Int)(
  implicit adderFunction: AdderType[N] { type Out = NOut }
): NOut = adderFunction(initial)
// constructing "values" to derive its type arg
case class NatVal[N <: Nat]()

// just a convenience function
def nat[N <: Nat]: NatVal[N] = NatVal[N]()
scala> val add3Numbers = adder(nat[_3])(0)
add3Numbers: this.Out = <function1>

scala> add3Numbers(1)(2)(3)
res1: this.Out = 6
def foo[AOut]()(implicit
  a: A { type Out = AOut},
  b: B { type In = AOut }
) ...
def foo()(implicit
  a: A,
  b: B { type In = a.Out }
) ...