Why I need instance for `Show (Maybe String)`?

Currently I’m studying type parameters. In the book Learn You a Haskell For Great Good the author gives an example in section Type Parameters :

data Maybe a = Nothing | Just a

When I try to implement this Maybe a, I have to add deriving (Show), otherwise when I call Just "Hey", Haskell warns we that "No instance for Show (Maybe String).

I’m curious why there’s no deriving (Show) in author’s example. In previous sections, the author emphasized the need of deriving (Show) when we want to define a new type and show it in the prompt. Maybe since String is of typeclass Show so we don’t need to add deriving (Show)? I feel confused!

deriving (Show) creates some generated code.

For a concrete data type (no parameters), it generates code based on the type constructors.

For a data type with one parameter it generates something like the following (in spirit):

instance (Show a) => Show (Maybe a) where
  show Nothing = "Nothing"
  show (Just x) = "Just (" ++ show x ++ ")"

Hopefully that helps.

I would guess the author left it off either accidentally, or intentionally to reduce clutter once it had been shown a few times.

1 Like

This looks clear! Thanks!