Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 530
lanskey Avatar asked Jul 12 '16 21:07

lanskey


1 Answers

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.

like image 144
Tikhon Jelvis Avatar answered Oct 07 '22 11:10

Tikhon Jelvis