1

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]
Grwlf
  • 896
  • 9
  • 21
  • 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 i2 – Michael Welch Nov 24 '15 at 13:55

1 Answers1

0

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.

chi
  • 111,837
  • 3
  • 133
  • 218
  • Indeed, I've missed the Show instance for Constr `instance Show Constr where show = constring` which shows the name of a constructor. Just a good old poor Show design problem, Thanks! – Grwlf Nov 25 '15 at 09:58