0

我正在尝试将 Haskell 转换Semigroup为 Scala。Haskell 代码运行良好,但我无法用 Scala 编写

哈斯克尔:

import Data.Semigroup

newtype Combine a b = Combine { unCombine :: (a -> b) }

instance Semigroup b => Semigroup (Combine a b) where  
    Combine f <> Combine g = Combine (f <> g)

f = Combine $ \n -> Sum (n + 1)
g = Combine $ \n -> Sum (n - 1)

print (unCombine (f <> g) $ 0)   -- Sum 0
print (unCombine (f <> g) $ 10)  -- Sum 20

斯卡拉代码

import cats.Semigroup
import cats.instances.all._

trait Combine[A, B] {
    def unCombine(a: A): B
}

val f = new Combine[Int, Int] {
  override def unCombine(n: Int): Int = n + 1
}

val g = new Combine[Int, Int] {
  override def unCombine(n: Int): Int = n - 1
}


implicit val mySemigroup: Semigroup[Combine[Int, Int]] = new Semigroup[Combine[Int, Int]] {
  def combine(x: Combine[Int, Int], y: Combine[Int, Int]): Combine[Int, Int] = (x,y) match {
    // ???
  }
}
4

2 回答 2

3

除了@KartikSabharwal 的回答,因为SemigroupCombine都是函数式接口,从 Scala 2.12 开始,您可以像这样定义特定情况:

implicit val mySemigroup: Semigroup[Combine[Int, Int]] =
  (x, y) => a => x.unCombine(a) + y.unCombine(a)

@KartikSabharwal 提到的通用案例在 Scala 2.12 中看起来像这样:

// Don't forget to NOT import `cats.instances.all._` together with this import
import cats.implicits._ 

implicit def combineSemigroup[A, B](
  implicit ev: Semigroup[B]
): Semigroup[Combine[A, B]] =
  (x, y) => a => x.unCombine(a) combine y.unCombine(a)

在 Scala 2.11 中就像这样:

import cats.implicits._ 

implicit def combineSemigroup[A, B](
  implicit ev: Semigroup[B]
): Semigroup[Combine[A, B]] =
  new Semigroup[Combine[A, B]] {
    override def combine(x: Combine[A, B], y: Combine[A, B]): Combine[A, B] =
      new Combine[A, B] {
        override def unCombine(a: A): B = x.unCombine(a) combine y.unCombine(a)
      }
  }
于 2018-09-14T17:46:05.380 回答
2

这是回答您的特定问题的代码。

import cats.Semigroup
import cats.instances.all._

object Main extends App {

  trait Combine[A, B] {
    def unCombine(a: A): B
  }

  override def main(args: Array[String]): Unit = {
    implicit val mySemigroup: Semigroup[Combine[Int, Int]] =
      new Semigroup[Combine[Int, Int]] {
        def combine(x: Combine[Int, Int], y: Combine[Int, Int]): Combine[Int, Int] =
          new Combine[Int, Int] {
            override def unCombine(n: Int): Int =
              Semigroup[Int].combine(x.unCombine(n), y.unCombine(n))
          }
        }

    val f = new Combine[Int, Int] {
      override def unCombine(n: Int): Int = n + 1
    }

    val g = new Combine[Int, Int] {
      override def unCombine(n: Int): Int = n - 1
    }

    val example = Semigroup[Combine[Int, Int]].combine(f, g).unCombine(10)

    printf("%d\n", example)
  }
}

理想情况下,我想在精神上复制 Haskell 代码并实现某种形式

// 'a' can be any type
// Semigroup[b] must exist
implicit val mySemigroup: Semigroup[Combine[a, b]] =
  def combine(x: Combine[a, b], y: Combine[a, b]): Combine[a, b] =
    new Combine[a, b] {
      override def unCombine(n: a): b =
        Semigroup[b].combine(x.unCombine(n), y.unCombine(n))
    }

但我对 Scala 了解的不够多,无法完成它。当我弄清楚时,我会更新答案,或者其他人可以来编辑这个答案/发布一个更好的答案。

于 2018-09-14T17:17:07.903 回答