[Haskell-cafe] A Very Simple Type Class Question

Alejandro Serrano Mena trupill at gmail.com
Tue Nov 11 21:11:08 UTC 2014


The problem is that the type of f is

f :: (A a, A b) => a -> b

This means that given an 'a', you need to create a function which works for
*any* b in A.

However, the function you implement is of type `f :: String -> String`, not
of type `f :: A b => String -> b`, as needed. If you were to implement the
function in that way, you could use:

class A a where
  f :: a -> a

2014-11-11 21:59 GMT+01:00 Larry Lee <llee454 at gmail.com>:

> Hi
>
> I have a very simple problem.
> I have a class and want to define a function in that class that returns a
> different instance of the same class.
>
> I tried accomplishing this as follows:
>
>     class A a where
>       f :: A b => a -> b
>
>
> This fails however when I try to instantiate it. For example:
>
>     instance A String where
>       f x = x
>
>
> I get an error message that makes absolutely no sense to me:
>
>     src/CSVTree.hs:12:9:
>         Could not deduce (b ~ [Char])
>         from the context (A b)
>           bound by the type signature for f :: A b => String -> b
>           at src/CSVTree.hs:12:3-9
>           `b' is a rigid type variable bound by
>               the type signature for f :: A b => String -> b
>               at src/CSVTree.hs:12:3
>         In the expression: x
>         In an equation for `f': f x = x
>         In the instance declaration for `A String'
>     make: *** [compile] Error 1
>
> Can someone please explain: how I can achieve my goal; and why my code is
> failing; simply and in plain English.
>
> Thanks,
> Larry
> _______________________________________________
> 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/20141111/227a0147/attachment-0001.html>


More information about the Haskell-Cafe mailing list