[Haskell-cafe] Missing a "Deriving"?
michael rice
nowgate at yahoo.com
Sat May 30 21:00:05 EDT 2009
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 <miguelimo38 at yandex.ru> wrote:
From: Miguel Mitrofanov <miguelimo38 at yandex.ru>
Subject: Re: [Haskell-cafe] Missing a "Deriving"?
To: "michael rice" <nowgate at yahoo.com>
Cc: haskell-cafe at haskell.org
Date: Saturday, May 30, 2009, 5:36 PM
It's trying to 'Show' the 'c [Int]' type, but doesn't know which 'c' to use; so it's trying to find a generic instance, which doesn't exist. You can't fix this with 'deriving' or anything like this; instead, provide the type annotation like this:
*Main> searchAll g 1 3 :: Maybe [Int]
On 31 May 2009, at 00:50, michael rice wrote:
> 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 at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090530/d0bc5c37/attachment.html
More information about the Haskell-Cafe
mailing list