[Haskell-cafe] Typeclass Show Question

Kai Zhang kai at kzhang.org
Sun Nov 30 23:59:22 UTC 2014


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> wrote:

> Hi,
>
>
>
> 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
>
>
>
> <interactive>:3: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:
>
> conmath.hs:10:24:
>
>     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 ?
>
>
>
> Regards,
>
> Yasu
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20141130/17596d0e/attachment-0001.html>


More information about the Haskell-Cafe mailing list