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.  No instance for (Show a) (Ovidiu Deac)
   2. Re:  No instance for (Show a) (Daniel Fischer)


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

Message: 1
Date: Sun, 31 Jul 2011 12:19:36 +0300
From: Ovidiu Deac <[email protected]>
Subject: [Haskell-beginners] No instance for (Show a)
To: beginners <[email protected]>
Message-ID:
        <CAKVsE7uQnESvYgkm=Hw3zeemGRvAJRjAXoSaLKaHJMHwLnn1=w...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

For some reason ghc complains about not being able to call show on an
Integer (?!?!?)

Please enlighten me!

ovidiu

This is the hspec:
    it "shows one element"
        ( show (push 1 EmptyStack) ? "Stack(1)")

...this is the code:

data Stack a =
    EmptyStack |
    StackEntry a (Stack a)
    deriving(Eq)


instance Show (Stack a) where
    show s =
        "Stack(" ? (showImpl s) ? ")"
        where
            showImpl EmptyStack = ""
            showImpl (StackEntry x _) = show x

...and this is the error:

src/Stack.hs:12:22:
    No instance for (Show a)
      arising from a use of `showImpl'
    In the first argument of `(++)', namely `(showImpl s)'
    In the second argument of `(++)', namely `(showImpl s) ++ ")"'
    In the expression: "Stack(" ++ (showImpl s) ++ ")"



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

Message: 2
Date: Sun, 31 Jul 2011 11:37:57 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] No instance for (Show a)
To: [email protected]
Message-ID: <[email protected]>
Content-Type: Text/Plain;  charset="utf-8"

On Sunday 31 July 2011, 11:19:36, Ovidiu Deac wrote:
> For some reason ghc complains about not being able to call show on an
> Integer (?!?!?)

No, it complains about not being able to call show on a value of arbitrary 
type. Your

instance Show (Stack a) where

says you can show stacks of arbitrary type, so nothing can be assumed about 
the element type, but in the implementation you try to call show on the top 
element.
To make it work, you have to tell GHC that the elements can be shown,

instance (Show a) => Show (Stack a) where
   -- what you have

would work. The "(Show a) =>" part says roughly "if elements of the stack 
can be shown, then the stack can be shown".

> 
> Please enlighten me!
> 
> ovidiu
> 
> This is the hspec:
>     it "shows one element"
>         ( show (push 1 EmptyStack) ? "Stack(1)")
> 
> ...this is the code:
> 
> data Stack a =
>     EmptyStack |
>     StackEntry a (Stack a)
>     deriving(Eq)
> 
> 
> instance Show (Stack a) where
>     show s =
>         "Stack(" ? (showImpl s) ? ")"
>         where
>             showImpl EmptyStack = ""
>             showImpl (StackEntry x _) = show x
> 
> ...and this is the error:
> 
> src/Stack.hs:12:22:
>     No instance for (Show a)
>       arising from a use of `showImpl'
>     In the first argument of `(++)', namely `(showImpl s)'
>     In the second argument of `(++)', namely `(showImpl s) ++ ")"'
>     In the expression: "Stack(" ++ (showImpl s) ++ ")"
> 



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

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


End of Beginners Digest, Vol 37, Issue 69
*****************************************

Reply via email to