[Haskell-cafe] "Casting" newtype to base type?

Tom Ellis tom-lists-haskell-cafe-2013 at jaguarpaw.co.uk
Mon Jul 1 17:24:08 CEST 2013


On Mon, Jul 01, 2013 at 05:07:00PM +0200, Vlatko Basic wrote:
> Hello Cafe!
> 
> I had a (simplified) record
> 
>   data P = P {
>     a :: String,
>     b :: String,
>     c :: IO String
>     } deriving (Show, Eq)
> 
> but to get automatic deriving of 'Show' and 'Eq' for 'data P' I have
> created 'newtype IOS' and its 'Show' and 'Eq' instances
> 
>   newtype IOS = IO String
>   instance Show (IOS) where show _ = "(IO String) function"
>   instance Eq   (IOS) where _ == _ = True

An Eq instance for something containing IO is bound to lead to puzzlement
somewhere down the line.  I think you're better off defining something like

    data P_lesser = P_lesser {
        a_lesser :: String,
        b_lesser :: String
    } deriving (Show, Eq)

    to_lesser p = P_lesser (a p) (b p)

and just factoring everything through "to_lesser" when you want to compare
or show.

Tom



More information about the Haskell-Cafe mailing list