<div dir="ltr">2008/8/6 Galchin, Vasili <span dir="ltr"><<a href="mailto:vigalchin@gmail.com">vigalchin@gmail.com</a>></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> I think now my question is why isn't there a higher-order "with" function in the Haskell Prelude?<br></div></blockquote><div><br>There is. Well, not in the prelude, but in the base package at least. It's called "bracket". But I think the reason that not all "with" functions are implemented with bracket is because "with" 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> type Constr a<br> acquire :: Constr a -> IO a<br> release :: a -> IO ()<br>
<br>with :: (Resource a) => Constr a -> (a -> IO b) -> IO b<br>with constr = bracket (acquire constr) release<br><br>I haven'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"><<a href="mailto:jeremy@n-heptane.com" target="_blank">jeremy@n-heptane.com</a>></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>
><br>
> [1 <multipart/alternative (7bit)>]<br>
> [1.1 <text/plain; ISO-8859-1 (7bit)>]<br>
<div>> Hello,<br>
><br>
> 1) Is there a common assumed semantics across all of the Haskell "with"<br>
> things? withString? withData?<br>
<br>
</div>A vague semantic is that some resource is acquired, used, and then released.<br>
<div><br>
> 2) If the answer to 1) is yes, then does this imply some kind of<br>
> polymorphism? Couldn't "with" be "reduced" to a higher order function then<br>
> ??<br>
<br>
</div>I think Control.Exception.bracket is used to implement many, but not<br>
all, of the with* functions.<br>
<br>
> newThing :: IO a<br>
> newThing = ...<br>
<br>
> destroyThing :: a -> IO b<br>
> destroyThing ...<br>
<br>
> withThing :: (Thing -> IO a) -> IO a<br>
> 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>