Re: [Haskell-cafe] Missing a "Deriving"?

Hi Ryan,
Is there something missing or mislabeled in your post, because I don't see any definition of toDynamic.
Michael
--- On Sun, 5/31/09, Ryan Ingram
*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. Some examples:
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") -- ryan

Oops, it's called toDyn; from Data.Dynamic [1]
toDyn :: Typeable a => a -> Dynamic
-- ryan
[1] http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Dynamic.html
On Sat, May 30, 2009 at 10:18 PM,
Hi Ryan,
Is there something missing or mislabeled in your post, because I don't see any definition of toDynamic.
Michael
--- On Sun, 5/31/09, Ryan Ingram
wrote: From: Ryan Ingram
Subject: Re: [Haskell-cafe] Missing a "Deriving"? To: "David Menendez" Cc: "michael rice" , haskell-cafe@haskell.org, "Miguel Mitrofanov" Date: Sunday, May 31, 2009, 12:45 AM On Sat, May 30, 2009 at 6:33 PM, David Menendez
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.
Some examples:
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")
-- ryan
participants (2)
-
nowgate@yahoo.com
-
Ryan Ingram