Laws for Show and Read?

Niklas Hambüchen mail at nh2.me
Wed Jun 5 14:18:17 UTC 2019


> Shouldn't it be laws that "show" must be injective and "read" must be surjective?
My opinion (and that of many others) is that 

* Show and Read should be used only for debugging purposes or things that directly relate to Haskell data types when written as literals
* whenever possible, use only the GHC-derived instances for those
* for all other use cases, different explicit parsers or pretty-printers should be used.

The GHC-derived instances also automatically make your desired laws mostly hold.

Injectivity seems a bit off, given that e.g.

  (read "3" :: Int) == (read "  3  " :: Int)

so whitespace stripping, parentheses etc. allow different inputs to map to same outputs.

Niklas


More information about the Libraries mailing list