[Haskell-cafe] Typeclass Show Question

Yasuaki Kudo yasu at yasuaki.com
Mon Dec 1 02:10:04 UTC 2014

Thank you so much; this worked! :)


From: Kai Zhang [mailto:kai at kzhang.org] 
Sent: Monday, December 1, 2014 08:59
To: Yasuaki Kudo
Cc: Haskell Cafe
Subject: Re: [Haskell-cafe] Typeclass Show Question


The type "a" in S a b must be an instance of "Show" in order to use "show". So you need:


instance Show a => Show (S a b) where

        show (S x y) = show x


On Sun, Nov 30, 2014 at 3:55 PM, Yasuaki Kudo <yasu at yasuaki.com <mailto:yasu at yasuaki.com> > wrote:



When I launch GHCI with the option –XGADTs and load the following program,


f = \x y  -> (1, x,y)


data S a b where 

        S :: (Num a, Num b) => a -> b -> S a b  



I encounter this dialog:

*Main> S 1 1 



    No instance for (Show (S a0 b0))

      arising from a use of `print'

    Possible fix: add an instance declaration for (Show (S a0 b0))

    In a stmt of an interactive GHCi command: print it



I tried to follow the recommendation but still could not figure out how.   For example, after adding:


instance Show (S a b) where

        show (S x y) = show(x)

I still get:


    Could not deduce (Show a) arising from a use of `show'

    from the context (Num a, Num b)

      bound by a pattern with constructor

                 S :: forall a b. (Num a, Num b) => a -> b -> S a b,

               in an equation for `show'

      at conmath.hs:10:15-19

    Possible fix:

      add (Show a) to the context of

        the data constructor `S'

        or the instance declaration

    In the expression: show (x)

    In an equation for `show': show (S x y) = show (x)

    In the instance declaration for `Show (S a b)'

Failed, modules loaded: none.


What should I do to show S 1 1 ?




Haskell-Cafe mailing list
Haskell-Cafe at haskell.org <mailto:Haskell-Cafe at haskell.org> 


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20141201/c294bd4c/attachment.html>

More information about the Haskell-Cafe mailing list