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

Hi Miguel,
That works. but it gives just a single solution [1,2,3] when there are supposed to be two [[1,2,3],[1,4,3]]. Of course the code in YAHT may be in error.
Also, how the heck does Haskell decide which "success", "failure", "augment", and "combine" to use in function "searchAll", since there are five possibilities.
MIchael
--- On Sat, 5/30/09, Miguel Mitrofanov
The following code is from Section 8.4.2, pgs. 111-112 (PDF paging) of YAHT.
It compiles fine, but upon trying it I get the following error message.
It seems to be trying to 'Show' the Computation class but I'm not sure where to put the 'Deriving'.
Michael
============
Loading package ghc-prim ... linking ... done. Loading package integer ... linking ... done. Loading package base ... linking ... done. [1 of 1] Compiling Main ( graph4.hs, interpreted ) Ok, modules loaded: Main. *Main> let g = Graph [(1,'a'),(2,'b'),(3,'c'),(4,'d')] [(1,2,'p'),(2,3,'q'),(1,4,'r'),(4,3,'s')] *Main> searchAll g 1 3
<interactive>:1:0: No instance for (Show (c [Int])) arising from a use of `print' at <interactive>:1:0-14 Possible fix: add an instance declaration for (Show (c [Int])) In a stmt of a 'do' expression: print it
============================
data Failable a = Success a | Fail String deriving (Show)
data Graph v e = Graph [(Int,v)] [(Int,Int,e)]
class Computation c where success :: a -> c a failure :: String -> c a augment :: c a -> (a -> c b) -> c b combine :: c a -> c a -> c a
instance Computation Maybe where success = Just failure = const Nothing augment (Just x) f = f x augment Nothing _ = Nothing combine Nothing y = y combine x _ = x
instance Computation Failable where success = Success failure = Fail augment (Success x) f = f x augment (Fail s) _ = Fail s combine (Fail _) y = y combine x _ = x
instance Computation [] where success a = [a] failure = const [] augment l f = concat (map f l) combine = (++)
searchAll g@(Graph vl el) src dst | src == dst = success [src] | otherwise = search' el where search' [] = failure "no path" search' ((u,v,_):es) | src == u = (searchAll g v dst `augment` (success . (u:))) `combine` search' es | otherwise = search' es
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Sat, May 30, 2009 at 9:00 PM, michael rice
That works. but it gives just a single solution [1,2,3] when there are supposed to be two [[1,2,3],[1,4,3]]. Of course the code in YAHT may be in error.
Works for me. *Main> searchAll g 1 3 :: [[Int]] [[1,2,3],[1,4,3]] *Main> searchAll g 1 3 :: Maybe [Int] Just [1,2,3] *Main> searchAll g 1 3 :: Failable [Int] Success [1,2,3]
Also, how the heck does Haskell decide which "success", "failure", "augment", and "combine" to use in function "searchAll", since there are five possibilities.
*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.
--
Dave Menendez

On Sat, May 30, 2009 at 6:33 PM, David Menendez
*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 (3)
-
David Menendez
-
michael rice
-
Ryan Ingram