Data.Matrix.prettyMatrix is a strange Show instance
Opened this issue · 0 comments
Data.Matrix.prettyMatrix
produces a String that looks nice on its own, but it doesn't have some properties that a good Show instance should have.
Firstly, when embedded inside other String
s obtained from Show
, the results can be very strange. In my code, I have some datatypes that contain matrices, and I am logging the results to a file after using a general pretty-printer (http://hackage.haskell.org/package/pretty-show-1.10/docs/Text-Show-Pretty.html). When the pretty printer encounters matrices, it gets completely confused because they do not look like normal Haskell expressions.
Secondly, derived instances of Show
have the following property, according to the documentation: "The result of show
is a syntactically correct Haskell expression containing only constants, given the fixity declarations in force at the point where the type is declared. It contains only the constructor names defined in the data type, parentheses, and spaces. When labelled constructor fields are used, braces, commas, field names, and equal signs are also used." Several datatypes try to mimic this property in their Show instances. For example, show
applied to a Map
gives something like fromList [(1,2), (3,4)]
.
I propose that the Show
instance for a matrix should use toLists
, so that, for example, show
-ing the 2x2 identity matrix gives either
-- This is in the same spirit as Data.Map, and gives an expression
-- that can be copied and pasted into ghci to give a matrix
fromLists [[1,0],[0,1]]
or just
-- This is in the spirit of Data.Vector. It gives an expression that cannot
-- precisely be copied into ghci, but it does follow Haskell syntax and is
-- shorter/simpler than the above
[[1,0],[0,1]]