<div dir="ltr">2008/8/6 Galchin, Vasili <span dir="ltr">&lt;<a href="mailto:vigalchin@gmail.com">vigalchin@gmail.com</a>&gt;</span><br><div class="gmail_quote"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div dir="ltr">Hi Jeremy and the haskell community,<br><br>&nbsp;&nbsp;&nbsp; I think now my question is why isn&#39;t there a higher-order &quot;with&quot; function in the Haskell Prelude?<br></div></blockquote><div><br>There is.&nbsp; Well, not in the prelude, but in the base package at least.&nbsp; It&#39;s called &quot;bracket&quot;.&nbsp; But I think the reason that not all &quot;with&quot; functions are implemented with bracket is because &quot;with&quot; is not as well defined as a function; it is an idiom, and varies in too many ways to have a well-defined semantics.<br>
<br>However, you could presumably abstract this more using typeclasses with fundeps or associated types.<br><br>class Resource a where<br>&nbsp;&nbsp;&nbsp; type Constr a<br>&nbsp;&nbsp;&nbsp; acquire :: Constr a -&gt; IO a<br>&nbsp;&nbsp;&nbsp; release :: a -&gt; IO ()<br>
<br>with :: (Resource a) =&gt; Constr a -&gt; (a -&gt; IO b) -&gt; IO b<br>with constr = bracket (acquire constr) release<br><br>I haven&#39;t calculated how many of the existing with* functions would fit into this abstraction.<br>
<br>Luke<br><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 dir="ltr"><br>Regards, Vasili<br><br><div class="gmail_quote"><div class="Ih2E3d">

On Wed, Aug 6, 2008 at 2:25 AM, Jeremy Shaw <span dir="ltr">&lt;<a href="mailto:jeremy@n-heptane.com" target="_blank">jeremy@n-heptane.com</a>&gt;</span> wrote:<br></div><div class="Ih2E3d"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">

At Wed, 6 Aug 2008 00:43:55 -0500,<br>
Galchin, Vasili wrote:<br>
&gt;<br>
&gt; [1 &nbsp;&lt;multipart/alternative (7bit)&gt;]<br>
&gt; [1.1 &nbsp;&lt;text/plain; ISO-8859-1 (7bit)&gt;]<br>
<div>&gt; Hello,<br>
&gt;<br>
&gt; &nbsp; &nbsp; 1) Is there a common assumed semantics across all of the Haskell &quot;with&quot;<br>
&gt; things? withString? withData?<br>
<br>
</div>A vague semantic is that some resource is acquired, used, and then released.<br>
<div><br>
&gt; &nbsp; &nbsp; 2) If the answer to 1) is yes, then does this imply some kind of<br>
&gt; polymorphism? Couldn&#39;t &quot;with&quot; be &quot;reduced&quot; to a higher order function then<br>
&gt; ??<br>
<br>
</div>I think Control.Exception.bracket is used to implement many, but not<br>
all, of the with* functions.<br>
<br>
&gt; newThing :: IO a<br>
&gt; newThing = ...<br>
<br>
&gt; destroyThing :: a -&gt; IO b<br>
&gt; destroyThing ...<br>
<br>
&gt; withThing :: (Thing -&gt; IO a) -&gt; IO a<br>
&gt; withThing useThing = bracket newThing destroyThing useThing<br>
<br>
I have not fully answered either of your questions, but hopefully this<br>
is a start.<br>
<font color="#888888"><br>
j.<br>
</font></blockquote></div></div><br></div>
<br>_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
<br></blockquote></div><br></div>