newbie question -- how does one override show for a newtype?

Posted by gatoatigrado on Stack Overflow See other posts from Stack Overflow or by gatoatigrado
Published on 2010-04-18T01:46:44Z Indexed on 2010/04/18 1:53 UTC
Read the original article Hit count: 480

Filed under:
|

I want to override the default integer constructors in Haskell so they produce strings (mostly for curiosity, but temporarily to make a nice input alternative for LaTeX's \frac{}{} inconvenience).

I wanted to be able to use the language itself, instead of a special parser, but I guess that's probably not going to work out...

module Main where

import Prelude hiding ((+))

newtype A = A Int deriving (Eq, Show, Num)
default (A)

(+) :: A -> (A -> String)
(A a) + (A b) = (show a) ++ " + " ++ (show b)

main2 = 3+4

main :: IO ()
main = putStrLn main2

The problem with the above is that the + function only works for (A, A) instead of (A, String), etc. If one simply leaves out the pattern match "(A a)" and writes "a" instead, then the show() function prepends "A " so "3" becomes "A 3" instead of just "3".

I want to override Show for A, but it seems to be quite a headache...

© Stack Overflow or respective owner

Related posts about haskell

Related posts about beginner