<div dir="ltr"><br><br><div class="gmail_quote">On Tue, Oct 20, 2009 at 11:04 AM, Nicolas Pouillard <span dir="ltr">&lt;<a href="mailto:nicolas.pouillard@gmail.com">nicolas.pouillard@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="im">On Sun, Oct 18, 2009 at 9:45 PM, Michael Snoyman &lt;<a href="mailto:michael@snoyman.com">michael@snoyman.com</a>&gt; wrote:<br>
&gt; While working on the next release of data-object, I wanted to represent some<br>
&gt; operations that might fail. The typical candidates were:<br>
&gt;<br>
&gt; 1) Maybe<br>
&gt; 2) Either<br>
&gt; 3) Monad<br>
&gt;<br>
&gt; Monad is always iffy because of the often times poorly defined fail. Maybe<br>
&gt; doesn&#39;t provide any means of reporting what the problem was. Either is not<br>
&gt; defined as a Monad in the base library. Also, the usual candidate of Either<br>
&gt; String only provides for an error message. It would be nice to have more<br>
&gt; in-depth information available.<br>
&gt;<br>
&gt; So I&#39;ve put together a package I call &quot;attempt&quot;. It defines a data type<br>
&gt; called (surprise) Attempt with a Success and Failure constructor. The trick<br>
&gt; here is that it using the new extensible exceptions under the surface, so<br>
&gt; you can report any kind of exception you want. It also provides a<br>
&gt; &quot;FromAttempt&quot; type class for possible conversion targets from an Attempt,<br>
&gt; provides an attempt function with some helpers for performing the equivalent<br>
&gt; of Control.Exception.catches, and provides two samples of functions I would<br>
&gt; want to implement with this library: attemptJoin and attemptLookup.<br>
&gt;<br>
&gt; My questions for the list are:<br>
&gt;<br>
&gt; 1) Is the overall approach sound, or do people have better ideas?<br>
<br>
</div>I think that there is a place for such a package. Given the Monad/fail issue,<br>
the Maybe/no-message issue and the Either/not-a-monad-in-base issue.<br>
<br>
About the name Attempt, I think that &#39;Outcome&#39; would be a better name.<br></blockquote><div><br>The problem with a name like &quot;Outcome&quot; is that it doesn&#39;t really imply the possibility of failure, simply that we are wrapping of the result for some reason. <br>
</div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="im"><br>
&gt; 2) Are there any other FromAttempt instances I should provide out of the<br>
&gt; box?<br>
<br>
</div>None that I see.<br>
<div class="im"><br>
&gt; 3) I was considering adding specialized versions of the fromAttempt<br>
&gt; function, ie ioFromAttempt, maybeFromAttempt. Thoughts?<br>
<br>
</div>It is a bit long to spell.<br></blockquote><div><br>True, but it can be more convenient than giving explicit type signatures. The main reason this might come up would be regarding the (Either String) and (Either SomeException) instances.<br>
 </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="im"><br>
&gt; 4) Should I follow the naming scheme attemptJoin, attemptLookup, etc, or<br>
&gt; just call the functions join, lookup and force people to import the module<br>
&gt; qualified?<br>
<br>
</div>A nice alternative would be to make a Data.Attempt.Extra (or a better<br>
name) which<br>
would be imported qualified.<br></blockquote><div><br>I like. The only other name I can think of right now is Data.Attempt.Helper, but that&#39;s just painting the bike shed.<br> </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">

<div class="im"><br>
&gt; 5) Any other suggestions for attempt functions? I&#39;ve considered<br>
&gt; head/tail/etc.<br>
<br>
</div>Why not, maybe the &#39;safe&#39; package should be of some inspiration.<br></blockquote><div><br>Once they&#39;re all in their own module, I don&#39;t see a problem adding as many as possible.<br> </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">

<br>
&gt; 6) Include ToAttempt?<br>
<br>
IMHO, no.<br>
<div class="im"><br>
&gt; The code is available on github at<br>
<br>
</div>I think that the &#39;attempt&#39; function should follow functions like<br>
&#39;maybe&#39; and &#39;either&#39;.<br>
<br>
attempt :: (forall e. Exception e -&gt; b) -&gt; (a -&gt; b) -&gt; Attempt a -&gt; b<br><br></blockquote><div>I was trying to more model it after fromMaybe, but instead just ended up with something ugly. I&#39;m going to rewrite attempt using this type signature and then add fromAttempt as well.<br>
<br>Michael <br></div></div></div>