[Haskell-beginners] fonction in a typeclass that does not mention the type variable

Peter Hall peter.hall at memorphic.com
Wed Aug 21 19:05:42 CEST 2013


Maybe I don't fully understand what you are trying to achieve, but I don't
think what you are trying to do makes sense. infixLevel and
symbolToConstructor will have different implementations for each instance
of Foo, and the compiler cannot possibly know which implementations you
mean. If the implementation of symbolToInfixLevel is independent of the
implementations of infixLevel and symbolToConstructor then it shouldn't
need them in its definition.

Peter



On 21 August 2013 17:39, TP <paratribulations at free.fr> wrote:

> Hi,
>
> I have a simple test case containing a typeclass Foo with one type variable
> a. The goal is to write once and for all a function symbolToInfixLevel
> function that combines two other functions defined in the typeclass:
>
> -----------------------
> class Foo a where
>
>     symbolToConstructor :: String -> ( a -> a -> a )
>     infixLevel :: a -> Int
>
>     symbolToInfixLevel :: String -> Int
>     symbolToInfixLevel s = infixLevel $ (symbolToConstructor s) undefined
> undefined
> -----------------------
>
> This yields an error because there is no "a" in the type signature for
> symbolToInfixLevel:
>
> $ runghc test_typeclass_without_typevariable.hs
> test_typeclass_without_typevariable.hs:1:1:
>     The class method `symbolToInfixLevel'
>     mentions none of the type variables of the class Foo a
>     When checking the class method: symbolToInfixLevel :: String -> Int
>     In the class declaration for `Foo'
>
> Now, if I define symbolToInfixLevel out of the typeclass:
>
> -----------------------
> class Foo a where
>
>     symbolToConstructor :: String -> ( a -> a -> a )
>     infixLevel :: a -> Int
>
> symbolToInfixLevel :: String -> Int
> symbolToInfixLevel s = infixLevel $ (symbolToConstructor s) undefined
> undefined
> -----------------------
>
> Now, I obtain:
>
> $ runghc test_typeclass_without_typevariable.hs
> test_typeclass_without_typevariable.hs:7:24:
>     No instance for (Foo a0) arising from a use of `infixLevel'
>     In the expression: infixLevel
>     In the expression:
>       infixLevel $ (symbolToConstructor s) undefined undefined
>     In an equation for `symbolToInfixLevel':
>         symbolToInfixLevel s
>           = infixLevel $ (symbolToConstructor s) undefined undefined
>
> How to get rid from this situation?
>
> Thanks in advance,
>
> TP
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20130821/2c4fbc2e/attachment.htm>


More information about the Beginners mailing list