haskell - 使用常量值退化类型类实例声明

标签 haskell typeclass

我已将所有内容简化为基本内容,因此如果下面的示例代码是人为设计的,请耐心等待。假设我们有:

class Foo a where
    foo :: a

data Type a = Type a

instance (Foo a) => Foo (Type a) where
    foo = Type foo

现在,假设我想制作 Type a例如,Show 的一个实例每当 aFoo 的一个实例和 Show (选择 Show 是为了避免定义另一个类型类)。那么我们想要怎样Type a成为 Show 的一个实例?好吧,除非我们疯了,我们当然希望它像
instance (Foo a, Show a) => Show (Type a) where
    show (Type x) = show x

或者可能
instance (Foo a, Show a) => Show (Type a) where
    show (Type x) = "Blabla " ++ (show x)

这一切都很好,而且效果很好。出于某种莫名其妙的原因,我们想show输出任何 foo :: a看起来/看起来像!在我们人为设置的环境中,我无法想象我们为什么要那样做,但假设我们这样做了。不应该
instance (Foo a, Show a) => Show (Type a) where
    show _ = show foo

做的伎俩?

唉,GHC 说

Ambiguous type variable 'a' in the constraints: 'Foo a' [...] 'Show a'



也许 GHC 无法弄清楚是哪个 foo我正在谈论。我的意思是 foo :: Type afoo :: a ?将上一个片段更改为
instance (Foo a, Show a) => Show (Type a) where
    show _ = show (foo :: a)

给我

Could not deduce (Foo a1) from the context () arising from a use of 'foo' at [...] Possible fix: add (Foo a1) to the context of an expression type signature In the first argument of 'show', namely '(foo :: a)' In the expression: show (foo :: a)



在这一点上,我开始认为我误解了一些基本的东西。然而,我有一种奇怪的感觉,过去类似的结构对我有用。

最佳答案

我认为问题在于类型变量不限于定义。也就是说,在

instance (Foo a, Show a) => Show (Type a) where
    show _ = show (foo :: a)
a第二行不同于 a在第一行,这就是为什么它显示为 a1在错误消息中。见 http://www.haskell.org/haskellwiki/Scoped_type_variables .如果这是问题,这应该可以工作(我在这台机器上没有 GHC):
asTypeOf :: a -> a -> a
asTypeOf a b = a

instance (Foo a, Show a) => Show (Type a) where
    show (Type x) = show (foo `asTypeOf` x)

关于haskell - 使用常量值退化类型类实例声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4041111/

相关文章:

haskell - 可以确保在安装 GHC 7.8 时安装分析库吗?

haskell - 为什么函数组合需要括号?

haskell - 将 fmap 与 Parallel.Strategies 结合使用会出现 NFData 错误

Haskell 类型错误 : Could not deduce (Show a) arising from a use of `show' from the context (Num a)

变量类型签名的 Haskell 困难

haskell - 使用折叠实现 takeWhile

haskell - 在 Haskell 中从互联网下载大文件

haskell - 为什么这个实现是可折叠类型类的错误实例?

scala - Scala 中高级类型的类型类实例中的两个参数函数,如何将这个简单的 Haskell 代码转换为 Scala?

c# - Haskell类型类和C++模板类