I am currently working with Haskell bindings for the HDF5 C library. Like many C libraries, its function calls use a lot of pointers.
Haskell's usual “best practice” function for allocating and allocating C resources follows a bracket , such as alloca , withArray , etc. When using them, I often enter several nested brackets. For example, here is a short excerpt for HDF5 bindings:
selectHyperslab rID dName = withDataset rID dName $ \dID -> do v <- withDataspace 10 $ \dstDS -> do srcDS <- c'H5Dget_space dID dat <- alloca3 (0, 1, 10) $ \(start, stride, count) -> do err <- c'H5Sselect_hyperslab srcDS c'H5S_SELECT_SET start stride count nullPtr -- do some work ... return value alloca3 (a, b, c) action = alloca $ \aP -> do poke aP a alloca $ \bP -> do poke bP b alloca $ \cP -> do poke cP c action (aP, bP, cP)
In the above code, the enclosed brackets are the brackets that I wrote withDataset , withDataspace and alloca3 that I wrote to prevent nesting of brackets from three levels in the code. For C libraries with a lot of resource collection calls and pointer arguments, encoding with standard parenthesis primitives can become unmanageable (which is why I wrote alloca3 to reduce nesting.)
As a rule, are there any best practices or coding methods that help reduce the enclosure of brackets when you need to allocate and release many resources (for example, using C calls)? The only alternative I found is a ResourceT transformer, which from the tutorial looks like it is designed to make access / and not simplify the bracket pattern.
haskell
fluffynukeit
source share