[Haskell-cafe] Type signatures in FFI exported functions

Alfonso Acosta alfonso.acosta at gmail.com
Sat Nov 18 12:45:35 EST 2006


The FFI doesn't allow using type signatures in exported functions.

Imagine this simplified example

class Foo a where
 action1 :: a -> IO()
 action2 :: a -> IO()

exportable :: Foo a => StablePtr a -> IO()
exportable ptr = do deref <-  deRefStablePtr ptr
                            action1 deref

Currently the FFI doesn't allow to export exportable due to the type
signature. But I don't really understand why isn't it feisable.

In my code I solved the problem by quantifying existentially

data CFoo = forall a.Foo a => CFoo a

instance Foo CFoo where
 action1 (CFoo a) = action1 a
 action2 (CFoo a) = action2 a

exportable' :: StablePtr CFoo -> IO()
exportable' ptr = do deref <- deRefStablePtr ptr
                             action1 a

Another way to fix it would be, unlike standard Haskell98, allowing
dictionaries to be packaged in data constructors

data CFoo' a = Foo a => CFoo' a

instance Foo (CFoo' a) where
 action1 (CFoo' x) = action1 x
 action2 (CFoo' x) = action2 x

exportable'' :: StablePtr (CFoo' a) -> IO()
exportable'' ptr = do deref <- deRefStablePtr ptr
                             action1 deref

As far as I know, the later option has only been recently made
available for GADTs in GHC, does anyone know away of doing it with
normal ADT?

Does anyone know a better workaround?

Thanks in advance,

Alfonso Acosta

More information about the Haskell-Cafe mailing list