如何在GHCi中列出应用了类型参数的类型的实例

3

我注意到我不知道如何让GHCi打印关于复合类型的信息。让我们考虑一个例子:

data X a = X (a Int)
type XList = X []

instance Show XList where show (X l) = "X (" ++ show l ++ ")"

我希望看到"X []"实现Show。
尝试1
λ :i (X [])

<interactive>:1:2: error: parse error on input ‘X’

尝试2-打印列表的实例但不包括 (X [])

λ :i X []

第三次尝试 - 与实例无关

λ :i XList
type XList = X []   -- Defined at <interactive>:20:1

然而,当适用时,展示实例是有效的。

λ show (X [1,2,3])
"X ([1,2,3])"

λ show (X ['1'])

<interactive>:31:18: error:
    • Couldn't match expected type ‘Int’ with actual type ‘Char’
1个回答

5

:info(缩写为:i)只能用于名称,而不能用于表达式。要获取表达式的实例,请使用:instances命令:

λ :instances (X [])
instance [safe] Show XList -- Defined at <interactive>:6:10
instance [safe] Show XList -- Defined at <interactive>:6:10

1
从哪个GHC版本开始提供instances命令?没有8.8.3这样的版本,所有的开发活动都是几年前的,并且只提到了一个我需要应用的补丁。 - Daniil Iaitskov
@DaneelS.Yaitskov 这是 GHC 8.10.1 的新功能。 - Joseph Sible-Reinstate Monica

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接