【问题标题】:|+| is a semigroup, why it needs a monoid implicit resolution|+|是一个半群,为什么它需要一个幺半群隐式解析
【发布时间】:2014-08-24 02:06:05
【问题描述】:

Semigroup 的目标是确保关联性和封闭性 monoid 的目标是基于 Semigroup 并提供额外的 Identity。 当我使用 |+| semigroup appender,为什么我定义隐式幺半群而不是隐式半群

这是我使用 reduceLeft 的代码,不需要初始值

    val result1 = List(Staff("John", 36), Staff("Andrew", 30))
    val result2 = List(Staff("John", 40), Staff("Danny", 30))
    val result3 = List(Staff("Andrew", 30))
    val result4: List[Staff] = List()

    implicit val staffListSemigroup = new Monoid[List[Staff]] {

      override def zero: List[Staff] = Nil

      override def append(f1: List[Staff], f2: => List[Staff]): List[Staff] = {

        val mapSemigroup = f1.map(t => (t.name, t.numberOfTasks)).toMap |+| f2.map(t => (t.name, t.numberOfTasks)).toMap

        mapSemigroup.map(t => Staff(t._1, t._2)).toList

      }
    }

    val result = List(result1, result2, result3, result4).reduceLeft(_ |+| _)

    assert(result.size == 3)

如果 staffListSemigroup 是 Semigroup[List[Staff]],则编译错误是 value |+|不是 List[SemigroupSpec.this.Staff] 的成员

另外,|+| 的定义在半群内

final class SemigroupOps[F] private[syntax](val self: F)(implicit val F: Semigroup[F]) extends Ops[F] {
  ////
  final def |+|(other: => F): F = F.append(self, other)
  final def mappend(other: => F): F = F.append(self, other)
  final def ⊹(other: => F): F = F.append(self, other)
  ////
}

在此先感谢

编辑

接下来是@Travis 的回答,我认为这是不正确的。对于隐式值,特定值将始终覆盖通用值。这是我刚刚写的代码示例:

case class Foo(i : Int, s : String)
class Test[T] {
  def print = "print test"
}

implicit val test = new Test[Any]
implicit val testMoreSpecific = new Test[Foo] {
  override def print = "print Foo"
}

def doStuff[A](implicit test: Test[A]) = {
  test.print
}

doStuff[Foo] //it successfully print out print Foo
doStuff //compilation error, ambiguous implicit value found

是不是因为在 Scalaz 中,没有指定像 Foo 这样的方法中指定的类型。

【问题讨论】:

    标签: scala functional-programming scalaz scalaz7


    【解决方案1】:

    问题是对于任何A 已经有一个Semigroup 用于List[A]。您为List[Staff] 定义了一个更具体的实例,这会导致歧义,正如您通过请求实例所看到的那样:

    scala> Semigroup[List[Staff]]
    <console>:17: error: ambiguous implicit values:
     both method listMonoid in trait ListInstances of type [A]=> scalaz.Monoid[List[A]]
     and value staffListSemigroup of type => scalaz.Semigroup[List[Staff]]
     match expected type scalaz.Semigroup[List[Staff]]
                  Semigroup[List[Staff]]
                           ^
    

    您可以跳过一些麻烦并尝试将 Scalaz 提供的实例保持在范围之外,但请不要这样做!- 这可能会让其他用户感到非常困惑,并且违反了some basic good principles for working with type classes。相反,您可以为 List[Staff] 编写一个包装器(一个简单的案例类就可以了),然后为该类型提供一个实例。


    为了完整起见,值得注意的是带有Monoid 的版本可以编译,因为Monoid 比Semigroup 更具体(请参阅language specification 的第6.26.3 节了解适用于此处的规则,但是请注意,它们有点乱)。

    【讨论】:

    • 嘿 Travis,我不认为这是因为模棱两可,因为隐式值特定类型可以覆盖通用类型,我添加一个示例来澄清这一点。
    • @Cloudtech:但是listMonoid 没有提供Monoid[List[Any]]——它为任何A 提供了一个通用的Monoid[List[A]]。
    • 这是否意味着我们永远不应该为 List 定义隐式半群?
    • 这取决于您所说的“应该”是什么意思。 可以,如果您将 Scalaz 的 listMonoid 保持在范围之外,这可能对您来说很好。不过,总的来说,这是一个坏主意。
    • @Cloudtech:要明确一点:问题不仅在于“看起来很奇怪”的解决方法——您不一定总是可以控制范围内的实例,以及混合和匹配实例具有不同的行为可能会导致非常混乱的错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多