Search code examples
haskellshowpretty-printghci

Pretty-printing in ghci


Is there a way to make ghci use a custom pretty-printing function instead of show for certain types? A more general question: what are the general guidelines to make a library as usable as possible in interactive mode? Thanks.


Solution

  • You can specify a custom pretty-printing function using the --interactive-print flag and naming any function in scope with the type C a => a -> IO () for any constraint C. (See Section 2.4.9 of the docs for details.)

    ghci --interactive-print=MyModule.prettyPrint
    

    This means you can specify your own function from your own typeclass. There's no way to do this just for a specific type, but your custom class can always include a fallback instance like

    instance Show a => PrettyPrint a where prettyPrint = show
    

    This will require at least OverlappingInstances to work.