<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Random Hacks: 8 ways to report errors in Haskell</title>
    <link>http://www.randomhacks.net/articles/2007/03/10/haskell-8-ways-to-report-errors</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Technology and Other Fun Stuff</description>
    <item>
      <title>"8 ways to report errors in Haskell" by andrew u frank</title>
      <description>&lt;p&gt;Using ghc 6.10 (with base 4.0.0.0) the module Control.Exception becomes easier to use and seem to be quite flexible.&lt;/p&gt;


	&lt;p&gt;The only limitation I can see is that the exception raised in pure code (this is possible) cannot be caught in pure code; catching exceptions is only possible in the IO monade. I wonder, if this is a serious restriction and how difficult it would be to ovecome.
The same example with division by zero becomes (if properly formated):&lt;/p&gt;


	&lt;p&gt;import qualified Control.Exception as E&lt;/p&gt;


	&lt;p&gt;import Data.Typeable&lt;/p&gt;


	&lt;p&gt;data MyException = DivByZero | ThisEx | ThatEx String deriving (Show, Eq, Typeable)&lt;/p&gt;


	&lt;p&gt;instance E.Exception MyException&lt;/p&gt;


	&lt;p&gt;myDiv9 :: (Monad m) =&gt; Float &amp;rarr; Float &amp;rarr; m Float&lt;/p&gt;


	&lt;p&gt;myDiv9 a 0 = E.throw DivByZero&lt;/p&gt;


	&lt;p&gt;myDiv9 a 99 = E.throw (ThatEx &amp;#8220;Fortran style ending&amp;#8221;)&lt;/p&gt;


	&lt;p&gt;myDiv9 a b = return (a / b)&lt;/p&gt;


	&lt;p&gt;isDivByZero (DivByZero) = True&lt;/p&gt;


	&lt;p&gt;isDivByZero _ = False&lt;/p&gt;


	&lt;p&gt;example9 :: Float &amp;rarr; Float &amp;rarr; &lt;span class="caps"&gt;IO &lt;/span&gt;()&lt;/p&gt;


	&lt;p&gt;example9 x y =
    E.catchJust (\e &amp;rarr; if isDivByZero e then Just (e) else Nothing)&lt;/p&gt;


	&lt;p&gt;(do 
              q &lt;- myDiv9 x y
              putStrLn (show q)
              E.throw ThisEx
           )&lt;/p&gt;


	&lt;p&gt;(\err &amp;rarr; 
                do 
                  putStrLn (&amp;#8220;my DivByZero &amp;#8221; ++ show (err:: MyException))
                  return ()
            )&lt;/p&gt;</description>
      <pubDate>Sat, 06 Jun 2009 07:02:54 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:9ec15195-c21b-4122-9432-973c3dc5ef9a</guid>
      <link>http://www.randomhacks.net/articles/2007/03/10/haskell-8-ways-to-report-errors#comment-637</link>
    </item>
    <item>
      <title>"8 ways to report errors in Haskell" by andrew u frank</title>
      <description>&lt;p&gt;moving from ghc 6.8.2 to 6.10 I cannot recompile the examples given here (especially 1 and 4). What has changed in base 4.0.0.0? The error I get is&lt;/p&gt;


	&lt;p&gt;Ambiguous type variable `e&amp;#8217; in the constraint:
      `Exception e&amp;#8217;
        arising from a use of `E.catch&amp;#8217;&lt;/p&gt;


	&lt;p&gt;anybody can help?&lt;/p&gt;</description>
      <pubDate>Thu, 04 Jun 2009 11:44:07 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:0c895db1-df07-4094-becd-6aa4d0f59013</guid>
      <link>http://www.randomhacks.net/articles/2007/03/10/haskell-8-ways-to-report-errors#comment-636</link>
    </item>
    <item>
      <title>"8 ways to report errors in Haskell" by andrew u frank</title>
      <description>&lt;p&gt;very useful post. i have then selected version 4 and it works.
thanks for shedding light on this issue, which is confusing due to the incompatible but same name functions.&lt;/p&gt;


	&lt;p&gt;andrew&lt;/p&gt;</description>
      <pubDate>Sat, 11 Apr 2009 08:15:10 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:53c26e1f-3e9e-4c59-9bf8-05745819a040</guid>
      <link>http://www.randomhacks.net/articles/2007/03/10/haskell-8-ways-to-report-errors#comment-608</link>
    </item>
    <item>
      <title>"8 ways to report errors in Haskell" by tphyahoo</title>
      <description>&lt;p&gt;right, uh, with whitespace :)&lt;/p&gt;


	&lt;p&gt;example4a x y = maybe ( putStrLn &amp;#8220;Division by zero&amp;#8221; ) ( putStrLn . show ) ( myDiv4 x y)&lt;/p&gt;


	&lt;p&gt;example4b x y = either putStrLn ( putStrLn . show ) ( myDiv4 x y )&lt;/p&gt;</description>
      <pubDate>Wed, 09 Jan 2008 10:46:48 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:40a56fb7-62d3-443b-9a1d-89ab2101380f</guid>
      <link>http://www.randomhacks.net/articles/2007/03/10/haskell-8-ways-to-report-errors#comment-548</link>
    </item>
    <item>
      <title>"8 ways to report errors in Haskell" by tphyahoo</title>
      <description>&lt;p&gt;uh, sorry, what I meant was&lt;/p&gt;


	&lt;p&gt;example4a x y = maybe ( putStrLn &amp;#8220;Division by zero&amp;#8221; ) ( putStrLn . show ) ( myDiv4 x y)
example4b x y = either putStrLn ( putStrLn . show ) ( myDiv4 x y )&lt;/p&gt;</description>
      <pubDate>Wed, 09 Jan 2008 10:46:11 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:f6f166d8-06ed-4c2f-9c19-3fa93f0e7c48</guid>
      <link>http://www.randomhacks.net/articles/2007/03/10/haskell-8-ways-to-report-errors#comment-547</link>
    </item>
    <item>
      <title>"8 ways to report errors in Haskell" by tphyahoo</title>
      <description>&lt;p&gt;Or (wrt what I just said) you could do what Luis Cabellos suggested above.&lt;/p&gt;


	&lt;p&gt;Also, examples 4b and 4c could be rewritten without the case statements by using the maybe/either functions.&lt;/p&gt;


	&lt;p&gt;example4a x y = maybe
    ( putStrLn &amp;#8220;Division by zero&amp;#8221; )
    ( putStrLn . show )&lt;/p&gt;


	&lt;p&gt;example4b x y = either
    putStrLn
    ( putStrLn . show )&lt;/p&gt;


	&lt;p&gt;I suspect there&amp;#8217;s an idiom for doing this with IO as well but I don&amp;#8217;t know it.&lt;/p&gt;</description>
      <pubDate>Wed, 09 Jan 2008 10:43:30 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:b91dba8e-3746-41fe-b5d3-5f2595967d52</guid>
      <link>http://www.randomhacks.net/articles/2007/03/10/haskell-8-ways-to-report-errors#comment-546</link>
    </item>
    <item>
      <title>"8 ways to report errors in Haskell" by tphyahoo</title>
      <description>&lt;p&gt;You need to import Control.Monad.Error for the Either stuff to work in example 4b or you get &amp;#8220;No instance for (Monad (Either String))&amp;#8221;&lt;/p&gt;


	&lt;p&gt;I think possibly this used to work, but stopped working at some point due to a reorganization of the haskell standard libs.&lt;/p&gt;


	&lt;p&gt;Thanks for putting this great post together.&lt;/p&gt;</description>
      <pubDate>Wed, 09 Jan 2008 10:35:16 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:82f9dedb-17a7-40d5-9aa0-ad9e41e0cd7b</guid>
      <link>http://www.randomhacks.net/articles/2007/03/10/haskell-8-ways-to-report-errors#comment-545</link>
    </item>
    <item>
      <title>"8 ways to report errors in Haskell" by Luis Cabellos</title>
      <description>&lt;p&gt;Great Post!&lt;/p&gt;


	&lt;p&gt;I found one code error. To test #3 way we need to add this:&lt;/p&gt;


&lt;pre&gt;instance Monad (Either a) where
  (Right x) &amp;gt;&amp;gt;= k = k x
  (Left err) &amp;gt;&amp;gt;= k = Left err
  return = Right
&lt;/pre&gt;</description>
      <pubDate>Tue, 23 Oct 2007 06:27:03 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:cdaebc43-552a-42f2-a762-63524f93ed3d</guid>
      <link>http://www.randomhacks.net/articles/2007/03/10/haskell-8-ways-to-report-errors#comment-523</link>
    </item>
    <item>
      <title>"8 ways to report errors in Haskell" by xml security geek</title>
      <description>&lt;p&gt;Yea, Haskell is nice to play with, but such inconsistencies still make it less appealing for serious development(((&lt;/p&gt;</description>
      <pubDate>Sat, 31 Mar 2007 23:03:46 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:98f8739f-dae2-4afa-91c0-3b8e60bd4e04</guid>
      <link>http://www.randomhacks.net/articles/2007/03/10/haskell-8-ways-to-report-errors#comment-384</link>
    </item>
    <item>
      <title>"8 ways to report errors in Haskell" by David Waern</title>
      <description>&lt;p&gt;Hi,&lt;/p&gt;


	&lt;p&gt;Nice post! I&amp;#8217;ve been bugged by these issues myself. It would be nice to have standards/conventions for this.&lt;/p&gt;</description>
      <pubDate>Sun, 11 Mar 2007 13:06:42 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:f38589eb-2fc6-4193-afb5-4cdd7616dc29</guid>
      <link>http://www.randomhacks.net/articles/2007/03/10/haskell-8-ways-to-report-errors#comment-348</link>
    </item>
    <item>
      <title>8 ways to report errors in Haskell</title>
      <description>&lt;p&gt;Haskell is a marvellous language, but there are some things I don&amp;#8217;t like about it.  My least favorite: Haskell has no fewer than 8
different APIs for reporting errors.&lt;/p&gt;

&lt;p&gt;To make a bad situation worse, the choice of API varies between 
popular libraries. To give a particularly unfortunate example,
&lt;a href="http://haskell.org/ghc/docs/latest/html/libraries/network/Network-URI.html"&gt;Network.URI.parseURI&lt;/a&gt; and &lt;a href="http://www.haskell.org/http/api/Network-HTTP.html"&gt;Network.HTTP.simpleHTTP&lt;/a&gt; report
errors in entirely different ways, turning a &amp;#8220;download this URL&amp;#8221; program into a &lt;a href="http://darcs.haskell.org/http/test/get.hs"&gt;page of code&lt;/a&gt;,
nearly half of which is devoted to dealing with various kinds of errors. (The rest is boilerplate that could be refactored into a nice wrapper.)&lt;/p&gt;

&lt;p&gt;Let&amp;#8217;s begin with a toy function, the simplest possible program that could
actually fail:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='hs-definition'&gt;myDiv&lt;/span&gt; &lt;span class='hs-varid'&gt;x&lt;/span&gt; &lt;span class='hs-varid'&gt;y&lt;/span&gt; &lt;span class='hs-keyglyph'&gt;=&lt;/span&gt; &lt;span class='hs-varid'&gt;x&lt;/span&gt; &lt;span class='hs-varop'&gt;/&lt;/span&gt; &lt;span class='hs-varid'&gt;y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;As every algebra student knows, we can&amp;#8217;t divide by zero.  Using this
function as our example, let&amp;#8217;s take a look at all the different ways we
can implement error-reporting in Haskell.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.randomhacks.net/articles/2007/03/10/haskell-8-ways-to-report-errors"&gt;Read More&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Sat, 10 Mar 2007 19:05:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:226356a7-21bf-493b-aa23-59dccc766054</guid>
      <author>Eric Kidd</author>
      <link>http://www.randomhacks.net/articles/2007/03/10/haskell-8-ways-to-report-errors</link>
      <category>Haskell</category>
      <trackback:ping>http://www.randomhacks.net/articles/trackback/347</trackback:ping>
    </item>
  </channel>
</rss>
