[Haskell-cafe] Typeclass Show Question

Yasuaki Kudo yasu at yasuaki.com
Sun Nov 30 23:55:18 UTC 2014



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 ?




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

More information about the Haskell-Cafe mailing list