# Monadic function fails as Nothing in Maybe context but as Exception in Either context.

11 messages
Open this post in threaded view
|

## Monadic function fails as Nothing in Maybe context but as Exception in Either context.

 I've written a monadic function which in a Maybe context produces a Nothing when it fails (as intended), but in an Either context produces an Exception rather than a Left.Here's a tiny demonstration. "tinyGraph" below has one Node, 0, with the label "dog". If I try to change the label at Node 0 to "cat", it works. If I try to change the label at Node 1 to "cat", it fails, because Node 1 is not in the graph.    type MyGraph = Gr String String    tinyGraph = mkGraph [(0, "dog")] [] :: MyGraph    maybeSucceed = replaceStringAtNodeM tinyGraph 0 "cat" :: Maybe MyGraph      -- == Just (mkGraph [(0,"cat")] [])    maybeFail = replaceStringAtNodeM tinyGraph 1 "cat" :: Maybe MyGraph      -- == Nothing    eitherSucceed = replaceStringAtNodeM tinyGraph 0 "cat" :: Either String MyGraph      -- ==  Right (mkGraph [(0,"cat")] [])    eitherFail = replaceStringAtNodeM tinyGraph 1 "cat" :: Either String MyGraph      -- *** Exception: Node not in GraphHere's the code:    import Data.Graph.Inductive -- FGL, the Functional Graph Library    gelemM :: (Monad m) => MyGraph -> Node -> m ()    gelemM g n = if gelem n g       -- FGL's gelem function returns      then return ()                  -- True if the node is in the graph      else fail "Node not in Graph"   -- False otherwise    replaceStringAtNode :: MyGraph -> Node -> String -> MyGraph    replaceStringAtNode g n e = let (Just (a,b,c,d),g') = match n g      in (a,b,e,d) & g'    replaceStringAtNodeM :: (Monad m) => MyGraph -> Node -> String -> m MyGraph    replaceStringAtNodeM g n s = do      gelemM g n      return \$ replaceStringAtNode g n s        -- if evaluated, the pattern match in replaceStringAtNode must succeed,        -- because gelemM catches the case where n is not in the graph-- Jeffrey Benjamin Brown _______________________________________________ Haskell-Cafe mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Open this post in threaded view
|

## Re: Monadic function fails as Nothing in Maybe context but as Exception in Either context.

Open this post in threaded view
|

## Re: Monadic function fails as Nothing in Maybe context but as Exception in Either context.

 In reply to this post by Jeffrey Brown On Mon, 30 Nov 2015 14:25:49 -0800 Jeffrey Brown <[hidden email]> wrote: > I've written a monadic function which in a Maybe context produces a > Nothing when it fails (as intended), but in an Either context > produces an Exception rather than a Left. The Monad instance for (Either e) uses the default definition of 'fail', which is to throw an exception: instance Monad (Either e) where     return = Right     Left  l >>= _ = Left l     Right r >>= k = k r In general, 'Left' can't be used because that would only type check for (Either String), but the Monad instance is more general than that. -- Michael Walker (http://www.barrucadu.co.uk) _______________________________________________ Haskell-Cafe mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Open this post in threaded view
|

## Re: Monadic function fails as Nothing in Maybe context but as Exception in Either context.

Open this post in threaded view
|

## Re: Monadic function fails as Nothing in Maybe context but as Exception in Either context.

Open this post in threaded view
|

## Re: Monadic function fails as Nothing in Maybe context but as Exception in Either context.

Open this post in threaded view
|

## Re: Monadic function fails as Nothing in Maybe context but as Exception in Either context.

Open this post in threaded view
|

## Re: Monadic function fails as Nothing in Maybe context but as Exception in Either context.

Open this post in threaded view
|