gpt4 book ai didi

haskell - Eq1/Ord1 类型类的目的和语义

转载 作者:行者123 更新时间:2023-12-01 03:24:39 25 4
gpt4 key购买 nike

考虑到以下类型,我应该如何实现 Eq1实例?

data Foo a = Bar String | Baz a [a]

instance Eq1 Foo where
liftEq _ (Bar a) (Bar b) = a == b -- can't use f
liftEq f (Baz a aa) (Baz b bb) = f a b && liftEq f aa bb -- instance for lists
liftEq _ _ _ = False

上面的例子正确吗?我应该在 GHC 8.0.2 中手动实现它吗?有 deriving-compat使用 TH 的库,但为什么叫它 -compat ?

最佳答案

Eq1点和 friend 提供的约束旨在与采用类型构造函数的类型构造函数一起使用——例如,monad 转换器,或 Fix (cf. How to derive instances in recursion schemes )——以一种更整洁的方式,不需要像 FlexibleContexts 这样的扩展名或 UndecidableInstances .

Is the instance above correct?



是的。

Am I supposed to implement it by hand in GHC 8.0.2?



目前,没有 GHC 提供 Eq1 的推导.这是一个 libraries mailing list discussion of that matter .

There is deriving-compat library that uses TH, but why is it called -compat?



我相信“兼容”的存在仅仅是因为该库还提供了最近 GHC 确实为旧 GHC 提供的实例的推导。

关于haskell - Eq1/Ord1 类型类的目的和语义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42449584/

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