gpt4 book ai didi

haskell - 为什么 GHC 认为类型变量对于具有默认实现的类是不明确的?

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

在下面的示例中,我希望 GHC 能够约束变量 a输入 A因为这就是我在 instance EAmbiguous A 中所说的.但是,当将其加载到 ghci 中时,我得到:

Ambiguous type variable ‘a0’ arising from a use of ‘Main.$dmcount’

有问题的代码:

{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}

import GHC.Generics (Rep, Generic)
import GHC.Base (Type)

class EAmbiguous a where
count :: Int
default count :: (Generic a, GenericAmbiguous (Rep a)) => Int
count = genericCount @(Rep a)

class GenericAmbiguous (a :: Type -> Type) where
genericCount :: Int

instance GenericAmbiguous (f p) where
genericCount = 10

data A = A deriving Generic

-- The error happens when defining a body-less instance:
-- • Ambiguous type variable ‘a0’ arising from a use of ‘Main.$dmcount’
-- prevents the constraint ‘(Generic a0)’ from being solved.
instance EAmbiguous A

我已经研究了好一阵子了,我相信这应该行得通,但我显然遗漏了一些东西。所以我的问题是:为什么 GHC 不能解决 aA

最佳答案

这原来是 GHC 错误,已在 GHC 8.0.2 中修复:https://ghc.haskell.org/trac/ghc/ticket/12220

关于haskell - 为什么 GHC 认为类型变量对于具有默认实现的类是不明确的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41661571/

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