1

我试图了解 Data.Data 包的 Constr 类型。考虑下面的会话。dataTypeConstrs返回ConstrMaybe 的零参数和单参数构造函数的列表。由于明显的类型错误,尝试重新创建列表失败。它是 GHC 关于 Consr 值的特殊行为吗?

$ ghci
GHCi, version 7.10.2: http://www.haskell.org/ghc/  :? for help
Prelude> :set -XScopedTypeVariables
Prelude> :module +Data.Data
Prelude Data.Data> dataTypeConstrs (dataTypeOf (Nothing :: Maybe ()))
[Nothing,Just]
Prelude Data.Data> :i it
it :: [Constr]  -- Defined at <interactive>:4:1

Prelude Data.Data> let i2 :: [Constr] = [Nothing,Just]

<interactive>:6:23:
    Couldn't match expected type ‘Constr’ with actual type ‘Maybe a0’
    In the expression: Nothing
    In the expression: [Nothing, Just]
4

1 回答 1

0

那不是实际构造函数的列表,而是构造函数表示的列表。它的Show实例使用快速而松散的输出,这使它看起来像别的东西。

只是假装它是

[ Constr{ name = "Nothing", args = 0, ... }
, Constr{ name = "Just", args = 1, ... }
]

除了它以松散的方式显示。

更准确地说,这是内部的、不透明的构造函数表示。使用constr*观察者检查类型的值Constr

于 2015-11-24T17:59:40.910 回答