Given this:
data Foo = Bar { name :: String } | Baz { nickname :: String }
Both the functions name and nickname seem to be of type Foo -> String:
:t name
name :: Foo -> String
:t nickname
nickname :: Foo -> String
However, the definitions are incomplete since both of the following expressions will raise pattern match errors:
name $ Baz { nickname = "Bob" }
nickname $ Bar { name = "Fred" }
Is it possible to complete the definitions of name and nickname, i.e. something like:
name Baz { nickname = n } = ...
nickname Bar { name = n } = ...
Trying this in hugs yields errors like “Multiple declarations for variable name”.
No, it’s not possible. The field names are top-level functions with respect to scope and thus cannot be redefined or extended. It’s as impossible to make them total functions as it is to make
headone.