[Haskell-cafe] Missing a "Deriving"?
ryani.spam at gmail.com
Sun May 31 00:45:58 EDT 2009
On Sat, May 30, 2009 at 6:33 PM, David Menendez <dave at zednenem.com> wrote:
> *Main> :t searchAll
> searchAll :: (Computation c) => Graph t t1 -> Int -> Int -> c [Int]
> The way searchAll is written, the choice of which functions to use
> depends on the type variable c. That's determined by the calling
> context of searchAll, which is why you need to provide a type
> signature when using it at the GHCi command line.
This is actually one of the most interesting and important things to
"get" about typeclasses; it's not *just* like an interface, because
the instance type can appear in the result of a function and *not* in
the arguments at all.
In contrast, in Java/C++, the method to use is always chosen by the
object being called; one could argue that all of COM is an attempt to
get around this problem.
> fromInteger :: Num a => Integer -> a
> fromDynamic :: Typeable a => Dynamic -> Maybe a
In both of these cases, the choice of which instance to use is made by
the caller, and often automatically by type inference:
> test x = case fromDynamic x of
> Just s -> s == "hello"
> Nothing -> False
Now (test $ toDynamic "hello") = True, but (test $ toDynamic 'a') =
False. Notice that I never directly specified what type "fromDynamic"
should return; but the case statement forces it to return Maybe
String, since I compare (s == "hello")
More information about the Haskell-Cafe