Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data.Matrix.prettyMatrix is a strange Show instance #4

Open
davidsd opened this issue Feb 26, 2020 · 0 comments
Open

Data.Matrix.prettyMatrix is a strange Show instance #4

davidsd opened this issue Feb 26, 2020 · 0 comments

Comments

@davidsd
Copy link
Contributor

davidsd commented Feb 26, 2020

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 Strings 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]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant