[Fwd: Instance resolving strangeness]

Sven Moritz Hallberg pesco at gmx.de
Tue Dec 7 12:20:14 EST 2004


I just posted the message below to glasgow-haskell-users. However, after 
discovering that

     test = foo (id 1) (id 2) :: ()

also works (see below), this to me looks definately like a bug. A valid 
program should still be valid after replacing any occurence of (id x) 
with x. Right?

-SMH


-------- Original Message --------
Subject: Instance resolving strangeness
Date: Tue, 07 Dec 2004 15:56:14 +0100
From: Sven Moritz Hallberg <pesco at gmx.de>
To: glasgow-haskell-users at haskell.org

Hello,

while trying to implement a certain polyvariadic function, using the
trick posted by Oleg Kiselyov to the Haskell ML some time ago, I
stumbled across some strange behaviour by GHC.

In particular, I'd like a function with a variable number of arguments
which are either Strings or Ints. I'd like to save the user from giving
explicit signatures to the Ints, so she can write

      foo 1 2 3

instead of

      foo (1::Int) (2::Int) (3::Int)    .


To demonstrate, consider the following declarations:

  >   class Foo a where
  >       foo :: a
  >   instance Foo () where
  >       foo = ()
  >   instance (Foo b, Integral a) => Foo (a -> b) where
  >       foo = \x -> foo

Given the above, GHC accepts the following line

  >   test = foo (1 :: Num a => a) (2 :: Num a => a) :: ()

and

  >   main = print test

prints "()" as expected. This also works in Hugs and requires no extensions.

If, however, I remove the explicit Num type signature from one (or both)
of the integer literals above, for instance

      test = foo (1 :: Num a => a) 2 :: ()    ,

GHC rejects the program with the following message.

Regex.hs:9:
      No instance for (Foo (t -> ()))
        arising from use of `foo' at Regex.hs:9
      When checking the type signature of the expression:
	  foo (1 :: forall a. (Num a) => a) 2 :: ()
      In the definition of `test':
	test = foo (1 :: forall a. (Num a) => a) 2 :: ()

Hugs still accepts and prints the expected result.

Can somebody enlighten me as to the reason for GHC's behaviour here? My
understanding is that the 2 above is taken as (fromIntegral 2) by the
compiler, having type (Num a => a). So restating that type explicitly
should have no effect...


Greetings,
Sven Moritz Hallberg

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 256 bytes
Desc: OpenPGP digital signature
Url : http://www.haskell.org/pipermail/glasgow-haskell-bugs/attachments/20041207/fd3f87aa/signature.bin


More information about the Glasgow-haskell-bugs mailing list