[Haskell-beginners] fonction in a typeclass that does not mention the type variable
TP
paratribulations at free.fr
Wed Aug 21 18:39:33 CEST 2013
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
More information about the Beginners
mailing list