type inference & instance extensions
A fragment of an attempt to make pairs serve as complex numbers, using ghc/hugs extensions: instance Num a => Num (a,a) where (x,y) * (u,v) = (x*u-y*v, x*v+y*u) Unfortunately, type inference isn't strong enough to cope with (1,1)*(1,1) Why shouldn't it be strengthened to do so? Or is there a declarative trick (perhaps with dependent classes) that will accomplish the goal? Doug McIlroy
Doug McIlroy <doug@cs.dartmouth.edu> wrote:
instance Num a => Num (a,a) where (x,y) * (u,v) = (x*u-y*v, x*v+y*u)
Unfortunately, type inference isn't strong enough to cope with
(1,1)*(1,1)
I'm guessing it is because (fromInteger 1, fromInteger 1) :: (Num a, Num b) => (a,b) So you want to force the two components to be the same type: let x = 1 in (x,x)*(x,x) Or for differing component values: let x = 1 in (x,2`asTypeOf`x)*(3`asTypeOf`x,4`asTypeOf`x) Regards, Malcolm
Doug McIlroy wrote:
A fragment of an attempt to make pairs serve as complex numbers, using ghc/hugs extensions:
instance Num a => Num (a,a) where (x,y) * (u,v) = (x*u-y*v, x*v+y*u)
Unfortunately, type inference isn't strong enough to cope with
(1,1)*(1,1)
Why shouldn't it be strengthened to do so?
The problem is that type classes are an "open" system. Although it's obvious that your instance is the only one in this code that can be used to type-check (1,1), that doesn't preclude new code adding an instance that could make it behave differently. I had hoped that the code below (GHC 6.10+) would work, but it just sends GHC into a loop when you actually try to typecheck (1,1). I don't know if that's a bug in GHC or a misunderstanding on my part of how the typechecking should work. {-# LANGUAGE FlexibleInstances, TypeFamilies #-} instance (a~b, Num a) => Num (a, b) where fromInteger k = (fromInteger k, fromInteger 0) (x,y) * (u,v) = (x*u-y*v, x*v+y*u) Ganesh ============================================================================== Please access the attached hyperlink for an important electronic communications disclaimer: http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html ==============================================================================
A loop without turning on a flag to allow it must be a bug. -- Lennart On Mon, Jan 19, 2009 at 2:04 PM, Sittampalam, Ganesh <ganesh.sittampalam@credit-suisse.com> wrote:
Doug McIlroy wrote:
A fragment of an attempt to make pairs serve as complex numbers, using ghc/hugs extensions:
instance Num a => Num (a,a) where (x,y) * (u,v) = (x*u-y*v, x*v+y*u)
Unfortunately, type inference isn't strong enough to cope with
(1,1)*(1,1)
Why shouldn't it be strengthened to do so?
The problem is that type classes are an "open" system. Although it's obvious that your instance is the only one in this code that can be used to type-check (1,1), that doesn't preclude new code adding an instance that could make it behave differently.
I had hoped that the code below (GHC 6.10+) would work, but it just sends GHC into a loop when you actually try to typecheck (1,1). I don't know if that's a bug in GHC or a misunderstanding on my part of how the typechecking should work.
{-# LANGUAGE FlexibleInstances, TypeFamilies #-}
instance (a~b, Num a) => Num (a, b) where fromInteger k = (fromInteger k, fromInteger 0) (x,y) * (u,v) = (x*u-y*v, x*v+y*u)
Ganesh
============================================================================== Please access the attached hyperlink for an important electronic communications disclaimer:
http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html ==============================================================================
_______________________________________________ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell
Lennart Augustsson wrote:
I had hoped that the code below (GHC 6.10+) would work, but it just sends GHC into a loop when you actually try to typecheck (1,1). I don't know if that's a bug in GHC or a misunderstanding on my part of how the typechecking should work.
A loop without turning on a flag to allow it must be a bug.
Sorry, I didn't mean to say "typecheck (1,1)". I meant to say "evaluate (1,1)*(1,1)" - I had a suspicion that some kind of infinite polymorphic recursion through the Num instance is happening and accidentally turned that thought into a completely different statement. Ganesh ============================================================================== Please access the attached hyperlink for an important electronic communications disclaimer: http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html ==============================================================================
participants (4)
-
Doug McIlroy -
Lennart Augustsson -
Malcolm Wallace -
Sittampalam, Ganesh