Proposal #2: marshalling utilites

Marcin 'Qrczak' Kowalczyk qrczak at
Sat Dec 2 16:19:38 EST 2000

Sat, 02 Dec 2000 13:49:52 +1100, Manuel M. T. Chakravarty <chak at> pisze:

> And because the greatest fun of all is to argue about names,
> I propose to rename `throwIf_EINTR', `throwIfNull_EINTR',
> and `throwIfMinus1_EINTR' to `throwIfEINTR',
> `throwIfNullEINTR', and `throwIfMinus1EINTR', respectively.

Perhaps testing for (== -1) and (== nullPtr) is not worth their own
functions and throwIf (== -1) is enough to keep the interface smaller.

It must be stressed that these functions translate errno - this fact
is not visible in their interface. They can easily be misused if a
library function just returns something in case of an error but does
not set errno at all.

Anyway, I think that the following functions are more important :-) than
throwIfMinus1 and throwIfNull (perhaps under better names):

sequenceCont:: [(a -> z) -> z] -> ([a] -> z) -> z
mapCont:: (a -> (b -> z) -> z) -> [a] -> ([b] -> z) -> z

Example of usage:

execv:: [String] -> IO a
execv args = do
    mapCont withCString args $ \argPtrs@(arg0Ptr:_) ->
        withArray0 nullPtr argPtrs $ \argsPtr ->
            execvC arg0Ptr argsPtr
    throwErrno "execv"
foreign import "execv" unsafe
    execvC :: Ptr CChar -> Ptr (Ptr CChar) -> IO CInt

Without them one has to write a recursive helper function.
Unfortunately plain mapM does not work for continuation-style

 __("<  Marcin Kowalczyk * qrczak at
  ^^                      SYGNATURA ZASTÊPCZA

More information about the FFI mailing list