gpt4 book ai didi

scala - Haskell 如何区分不同的可能类型类实例

转载 作者:行者123 更新时间:2023-12-04 21:57:33 25 4
gpt4 key购买 nike

如果我对事物使用错误的名称,我深表歉意。我的问题来自对比 Scala 和 Haskell 语法。考虑:

class Monoid a where
mempty :: a
mappend :: a -> a -> a

instance Monoid Int where
mempty = 0
mappend a b = a + b

sigma :: (Monoid a) => Int -> Int -> (Int -> Int) -> (Int -> a) -> a
sigma a b inc comp =
if a > b then mempty else mappend (comp a) (sigma (inc a) b inc comp)

在 Scala 中,它可能是这样的:
trait Monoid[A] {
def mempty: A
def mappend(a1: A, a2: A): A
}

class IntMonoid extends Monoid[Int] {
def mempty = 0
def mappend(a: Int, b: Int) = a + b
}

def sigma[A](a: Int, b: Int, inc: Int => Int, comp: Int => a)
(implicit m: Monoid[A]): A =
if (a > b) m.mempty else m.append(comp(a), sigma(inc(a), b, inc, comp))

现在,Int 既可以是具有 0 和加法的 Monoid,也可以是具有 1 和乘法的 Monoid,因此我们可以提供 2 个类型类,每个实现一个。在 Scala 中,如果两个实现在作用域内都是隐式的并且具有相同的优先级,这将导致编译错误。在这种情况下,我们可以简单地手动传递正确的实例,错误将得到解决。

这种情况下 Haskell 的等价物是什么?如果 Int 有两个实例是 Monoid,那么如何选择使用哪个实现?

最佳答案

对于实际上是 Num 实例的任何类型,Haskell 仅具有两个 newtype 包装器。 (包括 Int 类型)类型类:SumProduct .所以,Sum是加法下的幺半群且 Product type 是乘法下的幺半群。取自实际来源:

newtype Sum a = Sum { getSum :: a }
deriving (Eq, Ord, Read, Show, Bounded, Generic, Generic1, Num)

instance Num a => Monoid (Sum a) where
mempty = Sum 0
Sum x `mappend` Sum y = Sum (x + y)

newtype Product a = Product { getProduct :: a }
deriving (Eq, Ord, Read, Show, Bounded, Generic, Generic1, Num)

instance Num a => Monoid (Product a) where
mempty = Product 1
Product x `mappend` Product y = Product (x * y)

关于scala - Haskell 如何区分不同的可能类型类实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23523098/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com