[Haskell-cafe] Messing around with types [newbie]

Roberto Zunino zunino at di.unipi.it
Thu Jun 21 13:33:37 EDT 2007

Cristiano Paris wrote:
> class FooOp a b where
>  foo :: a -> b -> IO ()
> instance FooOp Int Double where
>  foo x y = putStrLn $ (show x) ++ " Double " ++ (show y)
> partialFoo = foo (10::Int)
> bar = partialFoo (5.0::Double)

The Haskell type classes system works in an "open world assumption": 
while the compiler can see the class instances in your code, it does not 
assume there are not others elsewhere (e.g. in another module).

In your example, the compiler can not prove that the only instance matching

   prog = partialFoo 5.0 10

is the one you wrote, unless you restrict the numeric constants to 
specific types as you did. Indeed, assume that in another module there 
is the instance

   instance FooOp Double Double where
     foo _ _ = putStrLn "DD"

what should then be the result of your program prog? 10 might be a 
Double after all, and "DD" could be as good a result as "5.0 Double 10". 
Being ambiguous, the program is rejected. Due to the open world 
assumption, your program is rejected as well.


More information about the Haskell-Cafe mailing list