Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one override show for a newtype?

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...

like image 245
gatoatigrado Avatar asked Apr 18 '10 01:04

gatoatigrado


2 Answers

If you want your own Show instance for A, then just don't derive it and make your own instance:

newtype A = A Int deriving (Eq, Num)

instance Show A where
  show (A a) = show a

Then you can write something like:

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

Of course, if you are defining your own + operator like that, then I don't think your problem requires the newtype A declaration:

module Main where

import Prelude hiding ((+))

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

aSum = 3 + 4

main :: IO ()
main = putStrLn aSum
like image 51
MtnViewMark Avatar answered Sep 29 '22 14:09

MtnViewMark


override the default integer constructors in Haskell so they produce strings

So this is done by defining a Num instance for String. Then (+) can be used as String -> String -> String.

A super quick example:

{-# LANGUAGE TypeSynonymInstances #-}

module A where

instance Num String where (+) = (++)

{-

*A> "hello" + "world"
"helloworld"

-}

Write a fromIntegral method to get functions from integer literals to strings (e.g. 1 --> "1").

For a more general, more disciplined approach to lifting lists of Num values to Num, see the Hinze approach to streams as Num, http://hackage.haskell.org/package/hinze-streams

like image 33
Don Stewart Avatar answered Sep 29 '22 13:09

Don Stewart