[Haskell] GHC Error question

Chris Kuklewicz haskell at list.mightyreason.com
Fri Dec 8 13:35:29 EST 2006


Rene de Visser wrote:
> There may be a heuristic that would help more programs to go through... but 
> I prefer asking the programmer to make the desired behaviour explicit.
> 
> Simon
> 
> How can the user make this explicit?
> 
> With the
> 
> class C a b where
>     op :: a -> a
> instance C Int Int where
>     op a = -a
> 
> test d = op d
> 
> example,
> 
> I have been unable to figure out what type annotations I need to add to 
> 'test', for exampl,e to define test to be for the C Int Int instance.
> 
> Or is it simply impossible in GHC to give test a type signature and to use 
> it as a useful function?
> 
> Example 

I can't quite do that but I have a trick: In GHC 6.6 with -fglasgow-exts this works:

> module Foo where
> 
> data CInst a b
> 
> class C a b where
>   cInst :: CInst a b
>   cInst = undefined
>   op :: a -> a
>   op' :: CInst a b -> a -> a
>  
> instance C Int Int where
>   op a = -a
>   op' _ a = (-a)
> 
> intInst :: CInst Int Int
> intInst = cInst
> 
> test x = (op' intInst) x

And test is inferred to have type:

> *Foo> :i test
> test :: Int -> Int 	-- Defined at /tmp/test.hs:18:0

Essentially the trick used in this case is to reify the instance (C Int Int) as
the value (undefined :: CInst Int Int).  Then you can specify to "op"

-- 
Chris


More information about the Glasgow-haskell-users mailing list