Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  showing user defined data types (Dan Lior)
   2. Re:  showing user defined data types (Sean Bartell)


----------------------------------------------------------------------

Message: 1
Date: Fri, 5 Apr 2013 15:15:26 -0500
From: Dan Lior <[email protected]>
Subject: [Haskell-beginners] showing user defined data types
To: "[email protected]" <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

Hello, 

I defined a type Pixel :

data Pixel = Black | White deriving (Eq, Show)

and would like to "show" it as 

show :: Pixel -> String
show Black = "#"
show White = "."

Apparently, ghc finds this ambiguous. That seems strange to me since Pixel is 
my own custom type and I don't imagine that ghc has another definition for 
showing it. Isn't this what "overloading" is supposed to enable?

I can get around the problem by :

import Prelude hiding(show)

but that seems to drastic since it hides all the various overloaded 
incarnations of "show". I only need it to hide one of them (the one for Pixel). 

I'm obviously misunderstanding something. 

Is there a simple way to define my own show function for my own user defined 
data type? 

Thanks in advance. 

dan




------------------------------

Message: 2
Date: Fri, 5 Apr 2013 16:31:44 -0400
From: Sean Bartell <[email protected]>
Subject: Re: [Haskell-beginners] showing user defined data types
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

Hello,

Dan Lior on 2013-04-05:
> data Pixel = Black | White deriving (Eq, Show)
> 
> and would like to "show" it as 
> 
> show :: Pixel -> String
> show Black = "#"
> show White = "."
> 
> Apparently, ghc finds this ambiguous. That seems strange to me since Pixel is 
> my own custom type and I don't imagine that ghc has another definition for 
> showing it. Isn't this what "overloading" is supposed to enable?

The normal show function is part of the Show typeclass, which is what
allows you to have different definitions of it for different types.
You're defining a new, separate "show" that only works on Pixels, and
when you try to call it later GHC doesn't know which "show" you mean.

Instead, you want to extend the normal "show" to work with Pixel by
making Pixel an instance of Show, like this:

instance Show Pixel where
    show Black = "#"
    show White = "."

You also need to remove "Show" from the "deriving" clause--it's causing
GHC to automatically make Pixel a Show instance by returning "Black" or
"White", and you want "#" or "." instead.

Sean Bartell



------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 58, Issue 16
*****************************************

Reply via email to