How to show anything

Contents
  • Standalone deriving
  • When you can’t derive
  • Printing in GHCi with :force
  • Printing with generics

Suppose we have some value that we want to print for debugging.

data Person = Human { name :: String, age :: Int }
            | Dog { goodPupper :: Bool }
λ> thePerson = Dog True

λ> thePerson

error:
    • No instance for (Show Person) arising from a use of ‘print’
    • In a stmt of an interactive GHCi command: print it

Oh no! – The error message

No instance for (Show Person)

is telling us that the author of the Person type didn’t give it a Show instance, so GHCi doesn’t know how to print it for us.

So what can we do?

There is no “universal way to print anything” in Haskell, but there is usually something you can do in this situation. Here we discuss a variety of ways to print a value if there isn’t a Show instance available.

Sign up for access to the full page, plus the complete archive and all the latest content.