简体   繁体   中英

Understanding Constr type of Data.Data package of Haskell

I am trying to understand the Constr type of Data.Data package. Consider the session below. dataTypeConstrs returns a list of Constr , both zero- and one-argument constructors of Maybe. Attempting to re-create the list fails due to obvious type error. Is it a special behavior of GHC regarding to Constr value?

$ 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]

That is not a list of actual constructors, but a list of constructor representations . Its Show instance uses a fast and loose output which makes it seem something else.

Just pretend it's something as

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

except it's being displayed in a loose way.

More precisely, that is the internal , opaque constructor representation. Use the constr* observer to inspect a value of type Constr .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM