Circular Instance declarations
oleg@pobox.com
oleg@pobox.com
Mon, 8 Sep 2003 03:59:53 GMT
Ashley Yakeley wrote:
> Would it be reasonable for the compiler to check back through the
> stack and allow the circularity? It will just create an ordinary
> recursive function.
The following works.
Flags: -fglasgow-exts -fallow-undecidable-instances
data D r = ZeroD | SuccD (r (D r))
instance (Eq (r (D r))) => Eq (D r) where
ZeroD == ZeroD = True
(SuccD a) == (SuccD b) = a == b
_ == _ = False
newtype C a = MkC a
instance Eq (C (D C)) where
(MkC ZeroD) == (MkC ZeroD) = True
(MkC (SuccD a)) == (MkC (SuccD b)) = a == b
_ == _ = False
equalDC :: D C -> D C -> Bool
equalDC = (==)
c2 = SuccD (MkC (SuccD (MkC ZeroD)))
c1 = SuccD (MkC (ZeroD))
*Main> equalDC c2 c2
True
*Main> equalDC c2 c1
False
*Main> equalDC ZeroD c1
False
*Main> equalDC ZeroD ZeroD
True
*Main>