1
votes

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]
1
Interesting question. In looking at Data.Data I see there are functions for create Constr instances. Like mkConstr. I'm guessing that the instance of Show for Constr doesn't give you enough detail to actually construct a Constr as you are attempting with i2Michael Welch

1 Answers

0
votes

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.