the Haskell "with" "pattern"?

Hello, 1) Is there a common assumed semantics across all of the Haskell "with" things? withString? withData? 2) If the answer to 1) is yes, then does this imply some kind of polymorphism? Couldn't "with" be "reduced" to a higher order function then ?? Kind regards, Vasili

At Wed, 6 Aug 2008 00:43:55 -0500, Galchin, Vasili wrote:
[1
] [1.1 ] Hello, 1) Is there a common assumed semantics across all of the Haskell "with" things? withString? withData?
A vague semantic is that some resource is acquired, used, and then released.
2) If the answer to 1) is yes, then does this imply some kind of polymorphism? Couldn't "with" be "reduced" to a higher order function then ??
I think Control.Exception.bracket is used to implement many, but not all, of the with* functions.
newThing :: IO a newThing = ...
destroyThing :: a -> IO b destroyThing ...
withThing :: (Thing -> IO a) -> IO a withThing useThing = bracket newThing destroyThing useThing
I have not fully answered either of your questions, but hopefully this is a start. j.

hmmm .. Jeremy ....
http://notes-on-haskell.blogspot.com/2007/03/design-patterns-in-haskell-brac.......
thx,
vasili
On Wed, Aug 6, 2008 at 2:25 AM, Jeremy Shaw
At Wed, 6 Aug 2008 00:43:55 -0500, Galchin, Vasili wrote:
[1
] [1.1 ] Hello, 1) Is there a common assumed semantics across all of the Haskell
"with"
things? withString? withData?
A vague semantic is that some resource is acquired, used, and then released.
2) If the answer to 1) is yes, then does this imply some kind of polymorphism? Couldn't "with" be "reduced" to a higher order function
then
??
I think Control.Exception.bracket is used to implement many, but not all, of the with* functions.
newThing :: IO a newThing = ...
destroyThing :: a -> IO b destroyThing ...
withThing :: (Thing -> IO a) -> IO a withThing useThing = bracket newThing destroyThing useThing
I have not fully answered either of your questions, but hopefully this is a start.
j.

Hi Jeremy and the haskell community,
I think now my question is why isn't there a higher-order "with"
function in the Haskell Prelude?
Regards, Vasili
On Wed, Aug 6, 2008 at 2:25 AM, Jeremy Shaw
At Wed, 6 Aug 2008 00:43:55 -0500, Galchin, Vasili wrote:
[1
] [1.1 ] Hello, 1) Is there a common assumed semantics across all of the Haskell
"with"
things? withString? withData?
A vague semantic is that some resource is acquired, used, and then released.
2) If the answer to 1) is yes, then does this imply some kind of polymorphism? Couldn't "with" be "reduced" to a higher order function
then
??
I think Control.Exception.bracket is used to implement many, but not all, of the with* functions.
newThing :: IO a newThing = ...
destroyThing :: a -> IO b destroyThing ...
withThing :: (Thing -> IO a) -> IO a withThing useThing = bracket newThing destroyThing useThing
I have not fully answered either of your questions, but hopefully this is a start.
j.

2008/8/6 Galchin, Vasili
Hi Jeremy and the haskell community,
I think now my question is why isn't there a higher-order "with" function in the Haskell Prelude?
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. However, you could presumably abstract this more using typeclasses with fundeps or associated types. class Resource a where type Constr a acquire :: Constr a -> IO a release :: a -> IO () with :: (Resource a) => Constr a -> (a -> IO b) -> IO b with constr = bracket (acquire constr) release I haven't calculated how many of the existing with* functions would fit into this abstraction. Luke
Regards, Vasili
On Wed, Aug 6, 2008 at 2:25 AM, Jeremy Shaw
wrote: At Wed, 6 Aug 2008 00:43:55 -0500, Galchin, Vasili wrote:
[1
] [1.1 ] Hello, 1) Is there a common assumed semantics across all of the Haskell
"with"
things? withString? withData?
A vague semantic is that some resource is acquired, used, and then released.
2) If the answer to 1) is yes, then does this imply some kind of polymorphism? Couldn't "with" be "reduced" to a higher order function
then
??
I think Control.Exception.bracket is used to implement many, but not all, of the with* functions.
newThing :: IO a newThing = ...
destroyThing :: a -> IO b destroyThing ...
withThing :: (Thing -> IO a) -> IO a withThing useThing = bracket newThing destroyThing useThing
I have not fully answered either of your questions, but hopefully this is a start.
j.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

2008/8/6 Galchin, Vasili
Hello,
1) Is there a common assumed semantics across all of the Haskell "with" things? withString? withData?
You probably knew this already but there's nothing in the "with idiom" that prevents the resource to escape. Oleg recently wrote about (lightweight) monadic regions and how they can statically assure that this doesn't happen. Cheers, Johan

Johan Tibell wrote:
2008/8/6 Galchin, Vasili
: Hello,
1) Is there a common assumed semantics across all of the Haskell "with" things? withString? withData?
You probably knew this already but there's nothing in the "with idiom" that prevents the resource to escape.
And, it doesn't always matter. Some withs are more unsafe that others :) if the 'with' constructs a nice ordinary heap allocated haskell structure from the external resource then it may not matter one jot if it escapes: it may be out-of-date, perhaps, but still a useful data value. On the other hand sometimes it does matter....
Oleg recently wrote about (lightweight) monadic regions and how they can statically assure that this doesn't happen.
Which is clever.

On Wed, Aug 6, 2008 at 5:44 PM, Jules Bean
Johan Tibell wrote:
You probably knew this already but there's nothing in the "with idiom" that prevents the resource to escape.
And, it doesn't always matter. Some withs are more unsafe that others :)
if the 'with' constructs a nice ordinary heap allocated haskell structure from the external resource then it may not matter one jot if it escapes: it may be out-of-date, perhaps, but still a useful data value.
I'm sure you're right but at the moment I can't really think of any such structure. Perhaps you could give an example? Why would you need the "with" in the first place for such a structure? Cheers, Johan

On Wed, 2008-08-06 at 18:21 +0200, Johan Tibell wrote:
On Wed, Aug 6, 2008 at 5:44 PM, Jules Bean
wrote: Johan Tibell wrote:
You probably knew this already but there's nothing in the "with idiom" that prevents the resource to escape.
And, it doesn't always matter. Some withs are more unsafe that others :)
if the 'with' constructs a nice ordinary heap allocated haskell structure from the external resource then it may not matter one jot if it escapes: it may be out-of-date, perhaps, but still a useful data value.
I'm sure you're right but at the moment I can't really think of any such structure. Perhaps you could give an example? Why would you need the "with" in the first place for such a structure?
http://www.haskell.org/ghc/docs/latest/html/libraries/base/Control-Concurren... withMVar :: MVar a -> (a -> IO b) -> IO b (useful for synchronization, among other things). modifyMVar :: MVar a -> (a -> IO (a, b)) -> IO b although that isn't quite within the boundaries of what `bracket' can do. jcc

On Wed, 6 Aug 2008, Galchin, Vasili wrote:
1) Is there a common assumed semantics across all of the Haskell "with" things? withString? withData?
2) If the answer to 1) is yes, then does this imply some kind of polymorphism? Couldn't "with" be "reduced" to a higher order function then ??
The Continuation monad abstracts the idea of 'with': http://www.haskell.org/pipermail/haskell-cafe/2008-February/038963.html

Henning, Cool ... thx, vasili On Thu, Aug 7, 2008 at 2:13 AM, Henning Thielemann < lemming@henning-thielemann.de> wrote:
On Wed, 6 Aug 2008, Galchin, Vasili wrote:
1) Is there a common assumed semantics across all of the Haskell
things? withString? withData?
2) If the answer to 1) is yes, then does this imply some kind of polymorphism? Couldn't "with" be "reduced" to a higher order function
"with" then
??
The Continuation monad abstracts the idea of 'with': http://www.haskell.org/pipermail/haskell-cafe/2008-February/038963.html
participants (7)
-
Galchin, Vasili
-
Henning Thielemann
-
Jeremy Shaw
-
Johan Tibell
-
Jonathan Cast
-
Jules Bean
-
Luke Palmer