
Hello the following program fails to load in Hugs2005 with all Haskell extensions enabled: {-# OPTIONS_GHC -fglasgow-exts -fallow-undecidable-instances #-} class (Monad m) => Stream m h where class (Stream m h) => CharStream m h where vGetChar :: h -> m Char vGetContents :: h -> m String vGetContents h = func where func = do vGetChar h return "" class (Stream m h) => ByteStream m h where vGetByte :: h -> m () instance (CharStream m h) => ByteStream m h where vGetByte h = undefined instance (ByteStream m h) => CharStream m h where vGetChar = undefined message printed is: ERROR file:.\SystemStreamClasses.hs - *** The type checker has reached the cutoff limit while trying to *** determine whether: *** CharStream a b *** can be deduced from: *** () *** This may indicate that the problem is undecidable. However, *** you may still try to increase the cutoff limit using the -c *** option and then try again. (The current setting is -c40) this program compiles ok in GHC 6.4.1. if call to `func` is replaced with its body, then program loads ok: vGetContents h = do vGetChar h return "" can problem with loading my code in Hugs be resolved by giving to `func` some type signmature? i can't replace call with its body because code in real program is much more complicated: vGetContents h = let loop = let func = do c <- vGetChar h next <- loop c `seq` return (c : next) handler e = if isEOFError e then return [] else mError e in mCatch func handler in do firstchar <- vGetChar h rest <- loop return (firstchar : rest) class (Monad m) => MonadHelper m where mError :: IOError -> m a mCatch :: m a -> (IOError -> m a) -> m a class (Show h, MonadHelper m) => Stream m h | h -> m where .... -- Best regards, Bulat mailto:bulatz@HotPOP.com

On Fri, Jan 06, 2006 at 01:52:23PM +0300, Bulat Ziganshin wrote:
the following program fails to load in Hugs2005 with all Haskell extensions enabled:
instance (CharStream m h) => ByteStream m h where ... instance (ByteStream m h) => CharStream m h where ...
Looks circular to me, i.e. there's no justification for inferring instances of either class. However recent GHC's spot the recursion in such cases and create mutually recursive instances.

Hello Ross, Friday, January 06, 2006, 2:16:24 PM, you wrote:
the following program fails to load in Hugs2005 with all Haskell extensions enabled:
instance (CharStream m h) => ByteStream m h where ... instance (ByteStream m h) => CharStream m h where ...
RP> Looks circular to me, i.e. there's no justification for inferring RP> instances of either class. However recent GHC's spot the recursion RP> in such cases and create mutually recursive instances. the following definition, which is a pure H98, also looks circular: class Something x where a::Int b::Int a = b+1 b = a-1 nevertheless, its semantics are well-known and obvious - instance need to define either a or b, and another function will be automatically derived. my instances definitions are just the same - you can define either functions in one class or in another, and then derive definitions for another class. i can't use single-class solution, because in actual code there are 4 different classes and in ideal someone can give explicit definition only for functions in one class and all other definitions must be derived automatically: class ByteStream s where getByte::Int class CharStream s where getChar::Char class BlockStream s where getBuf::Ptr a class MemoryStream s where receiveBuffer::Ptr a instance (ByteStream s) => CharStream s where getChar = chr getByte .... instance BlockStream Handle where .... instance CharStream StringBuffer where .... instance MemoryStream MemoryBuffer where .... -- Best regards, Bulat mailto:bulatz@HotPOP.com
participants (2)
-
Bulat Ziganshin
-
Ross Paterson