haskell - GHC 9 中量化约束的行为变化
问题描述
以前,为了对像这样的类型类使用量化约束Ord
,您必须在实例中包含超类,如下所示:
newtype A f = A (f Int)
deriving instance (forall a. Eq a => Eq (f a)) => Eq (A f)
deriving instance (forall a. Eq a => Eq (f a), forall a. Ord a => Ord (f a)) => Ord (A f)
(这实际上正是这个问题中给出的解决方案)。
然而,在 GHC 9 中,上述代码不起作用。它失败并出现以下错误:
• Could not deduce (Eq (f a))
from the context: (forall a. Eq a => Eq (f a),
forall a. Ord a => Ord (f a))
bound by a stand-alone deriving instance declaration:
forall (f :: * -> *).
(forall a. Eq a => Eq (f a), forall a. Ord a => Ord (f a)) =>
Ord (A f)
or from: Eq a
bound by a quantified context
• In the ambiguity check for a stand-alone deriving instance declaration
To defer the ambiguity check to use sites, enable AllowAmbiguousTypes
In the stand-alone deriving instance for
‘(forall a. Eq a => Eq (f a), forall a. Ord a => Ord (f a)) =>
Ord (A f)’
不幸的是,这个AllowAmbiguousTypes
建议不起作用。(你得到相同的错误,然后是类上的每个方法的相同错误)
有谁知道这个的解决方法?
解决方案
解决它的一种简单方法是将第二个派生子句更改为:
deriving instance (Eq (A f), forall a. Ord a => Ord (f a)) => Ord (A f)
我还没有很好的解释为什么会发生这种情况。
推荐阅读
- python - 更新序列化对象 Django
- java - 使用枚举或字符串的 switch 语句
- r - 我希望在 R 中将 0/1 变量重新编码为 YES/NO?
- kubernetes - 在特定的内部 IP 上运行 GKE pod
- kotlin-multiplatform - SQLDelight:如何动态创建表?
- javascript - 单元测试快速中间件
- sql - 将Oracle表数据转换为日常数据(sql)
- bots - 测量真人流量的机器人安全方法
- microsoft-graph-api - 在美国夏令时期间错误地为 CalendarView 计算 UTC,但尚未在欧洲
- flutter - 在 Flutter/dart 代码中访问 C# Nuget 包