Why is the bracket function called bracket?
I assume it has to do with the type signature syntax; brackets are used in the type signature to denote a function. Lets see the type signature of function map:
map :: (a -> b) -> [a] -> [b]
The first parameter of map is a function.
Now, let's have a look at bracket on Hackage.
bracket:: IO a -> (a -> IO b) -> (a -> IO c) -> IO c
Bracket: https://hackage.haskell.org/package/base-4.16.0.0/docs/Control-Exception.html#v:bracket
Okay I get it, the parameters of bracket are functions with IO actions.
Examples on Hackage use bracket like this:
bracket
(openFile "filename" ReadMode)
(hClose)
(\fileHandle -> do { ... })
After contemplating for a while I considered the name "bracket" not favorable.
I would say "performResourceSafely" would be a better fit.
So I ask again: Why is the bracket function called bracket?
CodePudding user response:
bracket v. To bound on both sides, to surround, as enclosing with brackets
— wiktionary
The thing that bracket does is it wraps some operation that uses a resource with the actions that allocate and deallocate that resource (with some care taken to ensure that deallocation happens even when exceptions are thrown). So the (de)allocation actions surround – or bracket – the main operation.
The file example you mention is especially suggestive. Like "open bracket" for [ or ( and "close bracket" for ] or ), the bracketing actions are "open file" and "close file" (well, "close handle"). This parallel is fairly common; e.g. with databases, one opens and closes a connection, with network stuff, one opens and closes a session, with subprocesses, one opens and closes a program, etc.
