[Haskell] Newbie help with type-classes
Bas van Dijk
v.dijk.bas at gmail.com
Fri May 11 06:07:09 EDT 2007
Maybe this is not what you want, but you can also put the 'convl'
function in the 'ConvertToInt' class.
class ConvertToInt a where
conv :: a -> Int
convl :: [a] -> [Int]
With this approach you don't need any language extension.
regards,
Bas van Dijk
On 5/11/07, Ryan Ingram <ryani.spam at gmail.com> wrote:
> Here's a test case for the problem I'm having; I'm using runhaskell from ghc
> v6.6.
>
> Problem #1) Without -fallow-undecidable-instances, I get the following
> error:
> Constraint is no smaller than the instance head
> in the constraint: ConvertToInt a
> (Use -fallow-undecidable-instances to permit this)
> In the instance declaration for `ConvertToIntList a'
>
> Problem #2) With -fallow-undecidable-instances, I get this error instead:
> Overlapping instances for ConvertToIntList ()
> arising from use of `convl' at testcase.hs:28:6-15
> Matching instances:
> instance (ConvertToInt a) => ConvertToIntList a
> -- Defined at testcase.hs:15:0
> instance ConvertToIntList () -- Defined at testcase.hs:18:0
> In the expression: convl [()]
> In the definition of `xl2': xl2 = convl [()]
>
> I don't understand why there is an overlapping instances error; () is not an
> instance of ConvertToInt so how could that instance ever apply?
>
> Is there something basic about type-classes that I'm not understanding here?
> My actual problem is more complicated than this, but this test-case covers
> the basic issue; something being an instance of class A means that I can
> derive an instance of class B for it, but I want to implement other
> instances of class B as well.
>
> Code below:
>
>
> {-# OPTIONS -fglasgow-exts -fallow-undecidable-instances #-}
>
> module TestCase
> where
> class ConvertToInt a where
> conv :: a -> Int
>
> class ConvertToIntList a where
> convl :: [a] -> [Int]
>
> instance ConvertToInt Int where
> conv = id
>
> instance ConvertToInt a => ConvertToIntList a where
> convl = map conv
>
> instance ConvertToIntList () where
> convl x = []
>
> x :: Int
> x = 5
>
> xl :: [Int]
> xl = convl [x]
>
> xl2 :: [Int]
> xl2 = convl [()]
> _______________________________________________
> Haskell mailing list
> Haskell at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell
>
>
More information about the Haskell
mailing list