<div dir="ltr"><div><div>I spoke too soon; I'm seeing the same problem with MonadError.<br></div><div><br></div><div><font face="monospace, monospace">    Prelude> :set -XFlexibleContexts</font></div><div><font face="monospace, monospace">    Prelude> import Control.Monad.Except</font></div><div><font face="monospace, monospace">    Prelude Control.Monad.Except> let f = (fail "be Left!" :: (MonadError String m) => m ())</font></div><div><font face="monospace, monospace">    Loading package transformers-0.4.2.0 ... linking ... done.</font></div><div><font face="monospace, monospace">    Loading package mtl-2.2.1 ... linking ... done.</font></div><div><font face="monospace, monospace">    Prelude Control.Monad.Except> f :: Either String ()</font></div><div><font face="monospace, monospace">    *** Exception: be Left!</font></div><div><font face="monospace, monospace">    Prelude Control.Monad.Except></font> </div></div><div><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Nov 30, 2015 at 6:18 PM, Jeffrey Brown <span dir="ltr"><<a href="mailto:jeffbrown.the@gmail.com" target="_blank">jeffbrown.the@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Oleg's suggestion works! I just had to add these two lines in order to use it:<div><div><br></div><div><font face="monospace, monospace">    {-# LANGUAGE FlexibleContexts #-}</font></div><div><font face="monospace, monospace">    import Control.Monad.Except -- mtl library</font></div></div><div><br></div><div>Thanks, everybody!</div></div><div class="gmail_extra"><div><div class="h5"><br><div class="gmail_quote">On Mon, Nov 30, 2015 at 2:38 PM, Oleg Grenrus <span dir="ltr"><<a href="mailto:oleg.grenrus@iki.fi" target="_blank">oleg.grenrus@iki.fi</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><br><div><span><blockquote type="cite"><div>On 01 Dec 2015, at 00:34, Oleg Grenrus <<a href="mailto:oleg.grenrus@iki.fi" target="_blank">oleg.grenrus@iki.fi</a>> wrote:</div><br><div><div style="word-wrap:break-word">Hi, Jeffrey<div><br></div><div>in short: `fail` of `Either e` throws an exception (i.e. is not overriden, default implementation is `fail s = error s`) [1, 2]</div><div><br></div><div>For `Maybe`, fail is defined as `fail _ = Nothing`; which is good default. [3] </div><div><br></div><div>You probably want to use for example `throwError from `mtl` package [4]:</div><div><br></div><div><div dir="ltr"><div></div></div></div></div></div></blockquote><div><br></div></span><div>I haven’t still tested it, but less wrong context is `MonadError String m`:</div><br><div><div style="word-wrap:break-word"><div><div dir="ltr"><div><font face="monospace, monospace">    gelemM :: (MonadError String m) => MyGraph -> Node -> m ()</font></div><div><div><div><font face="monospace, monospace">    gelemM g n = if gelem n g       -- FGL's gelem function returns</font></div><div><font face="monospace, monospace">      then return ()                        -- True if the node is in the graph</font></div><div><font face="monospace, monospace">      else throwError "Node not in Graph"   -- False otherwise</font></div></div></div></div></div></div></div><div><div><blockquote type="cite"><div><div style="word-wrap:break-word"><div><br></div><div>[1] <a href="https://hackage.haskell.org/package/base-4.8.1.0/docs/src/Data.Either.html#line-137" target="_blank">https://hackage.haskell.org/package/base-4.8.1.0/docs/src/Data.Either.html#line-137</a></div><div>[2] <a href="https://hackage.haskell.org/package/base-4.8.1.0/docs/src/GHC.Base.html#Monad" target="_blank">https://hackage.haskell.org/package/base-4.8.1.0/docs/src/GHC.Base.html#Monad</a></div><div>[3] <a href="https://hackage.haskell.org/package/base-4.8.1.0/docs/src/GHC.Base.html#line-642" target="_blank">https://hackage.haskell.org/package/base-4.8.1.0/docs/src/GHC.Base.html#line-642</a></div><div>[4] <a href="http://hackage.haskell.org/package/mtl-2.2.1/docs/Control-Monad-Except.html#v:throwError" target="_blank">http://hackage.haskell.org/package/mtl-2.2.1/docs/Control-Monad-Except.html#v:throwError</a></div><div><br></div><div>- Oleg</div><div><br><div><div><blockquote type="cite"><div>On 01 Dec 2015, at 00:25, Jeffrey Brown <<a href="mailto:jeffbrown.the@gmail.com" target="_blank">jeffbrown.the@gmail.com</a>> wrote:</div><br><div><div dir="ltr"><div>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.</div><div><br></div><div>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.</div><div><br></div><div><font face="monospace, monospace">    type MyGraph = Gr String String</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">    tinyGraph = mkGraph [(0, "dog")] [] :: MyGraph</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">    maybeSucceed = replaceStringAtNodeM tinyGraph 0 "cat" :: Maybe MyGraph</font></div><div><font face="monospace, monospace">      -- == Just (mkGraph [(0,"cat")] [])</font></div><div><font face="monospace, monospace">    maybeFail = replaceStringAtNodeM tinyGraph 1 "cat" :: Maybe MyGraph</font></div><div><font face="monospace, monospace">      -- == Nothing</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">    eitherSucceed = replaceStringAtNodeM tinyGraph 0 "cat" :: Either String MyGraph</font></div><div><font face="monospace, monospace">      -- ==  Right (mkGraph [(0,"cat")] [])</font></div><div><font face="monospace, monospace">    eitherFail = replaceStringAtNodeM tinyGraph 1 "cat" :: Either String MyGraph</font></div><div><font face="monospace, monospace">      -- *** Exception: Node not in Graph</font></div><div><br></div><div>Here's the code:</div><div><br></div><div><font face="monospace, monospace">    import Data.Graph.Inductive -- FGL, the Functional Graph Library</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">    gelemM :: (Monad m) => MyGraph -> Node -> m ()</font></div><div><font face="monospace, monospace">    gelemM g n = if gelem n g       -- FGL's gelem function returns</font></div><div><font face="monospace, monospace">      then return ()                  -- True if the node is in the graph</font></div><div><font face="monospace, monospace">      else fail "Node not in Graph"   -- False otherwise</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">    replaceStringAtNode :: MyGraph -> Node -> String -> MyGraph</font></div><div><font face="monospace, monospace">    replaceStringAtNode g n e = let (Just (a,b,c,d),g') = match n g</font></div><div><font face="monospace, monospace">      in (a,b,e,d) & g'</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">    replaceStringAtNodeM :: (Monad m) => MyGraph -> Node -> String -> m MyGraph</font></div><div><font face="monospace, monospace">    replaceStringAtNodeM g n s = do</font></div><div><font face="monospace, monospace">      gelemM g n</font></div><div><font face="monospace, monospace">      return $ replaceStringAtNode g n s</font></div><div><font face="monospace, monospace">        -- if evaluated, the pattern match in </font><span style="font-family:monospace,monospace">replaceStringAtNode must succeed,</span></div><div><span style="font-family:monospace,monospace">        -- because gelemM catches the case where n is not in the graph</span></div><div><br></div><div>[1] <a href="https://github.com/JeffreyBenjaminBrown/digraphs-with-text/blob/master/test/monad_fail_problems.hs" target="_blank">https://github.com/JeffreyBenjaminBrown/digraphs-with-text/blob/master/test/monad_fail_problems.hs</a></div><div><br></div><div><br></div>-- <br><div><div dir="ltr">Jeffrey Benjamin Brown</div></div>
</div>
_______________________________________________<br>Haskell-Cafe mailing list<br><a href="mailto:Haskell-Cafe@haskell.org" target="_blank">Haskell-Cafe@haskell.org</a><br><a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><br></div></blockquote></div><br></div></div></div>_______________________________________________<br>Haskell-Cafe mailing list<br><a href="mailto:Haskell-Cafe@haskell.org" target="_blank">Haskell-Cafe@haskell.org</a><br><a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><br></div></blockquote></div></div></div><br></div></blockquote></div><br><br clear="all"><div><br></div></div></div><span class="HOEnZb"><font color="#888888">-- <br><div><div dir="ltr">Jeffrey Benjamin Brown</div></div>
</font></span></div>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr">Jeffrey Benjamin Brown</div></div>
</div>