[Haskell-cafe] weird behavior with FFI

Lukas Mai l.mai at web.de
Sat Feb 2 17:41:05 EST 2008

Am Samstag, 2. Februar 2008 schrieb Tim Newsham:
> I am working on haskell bindings to C functions using FFI.  I have
> a callback function that returns "IO ()".  When I pass in the callback
> function:
>      foo = printf "foo"
> it crashes (is there an easy way to debug this exception using ghc6.8.2?)
>      bot: SilcClient_d1al: uncaught exception
> however, if I pass in the callback function:
>      foo = do
>          printf "foo"
>          return ()
> it does not crash.

I suspect this has to do with printf returning 'undefined' if you use it
with an IO type. Something in the code looks at the result and crashes.
Your second version is equivalent to printf "foo" >>= \_ -> return (),
i.e. it never evaluates printf's result but returns () instead.
Fixing the first version would require some changes to Text.Printf,
restricting the 'a' in PrintfType (IO a).

HTH, Lukas

More information about the Haskell-Cafe mailing list