Thanks for your solutions. I modified the code slightly below.
Here is my current understanding
1) In 'instance Eq1 a => Eq1 [a]' the equality predicate == needs to be defined for the second equation to be evaluated correctly.
2) If the empty list is annotated with a type then the first equation in 'instance Eq1 a => Eq1 [a]' should be work with that annotated type.
Equation 2 does not rely on any definition of ==.
3) If we replaced equation 1 in 'instance Eq1 a => Eq1 [a]' with
eq [] [] = ([] == [])
It not work because there is no concrete type in the instance
Even adding 'instance (Eq a, Eq1 a) => Eq1 [a]' will not help.
Is my understanding correct?
Thanks,
Pat
On Sat, Nov 30, 2013 at 10:40:59PM +0000, Patrick Browne wrote:
> Hi,
> I am trying to write my own simplified class and instances for equality.
> I have trouble with the equality equation for the empty list.
> Even though I can use the [] == [] at the GHCi prompt I cannot use it in
> my equality test.
> How can I make my eq test handle empty lists while staying within the
> context of my current code?.
> Thanks,
> Pat
>
> class Eq1 a where
> eq :: a -> a -> Bool
>
> instance Eq1 Int where
> eq a b = a == b
>
> instance Eq1 a => Eq1 [a] where
> -- This line compiles but gives an run time error.
> eq [] [] = True
This code does not give a run time error. The error you are getting
is a type inference error, which has nothing to do with this code.
> eq [] []
You simply need to give a type annotation on one of the empty lists.
eq ([] :: [Int]) []
The standard Eq class has some built-in magic support by GHCI
(extended default rules) which means this does not apply in the case
of using == directly.
-Brent
_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners