Wed, 26 Jul 2000 15:46:16 +0100, Claus Reinke <[EMAIL PROTECTED]> pisze:

> class CallN sigF sig where 
>   callN :: IO sigF -> sig

AAA! Thank you, it works now!

Having IO in the argument here, together with doing the work inside
the argument of the recursive call, was The Lacking Idea.

------------------------------------------------------------------------
class ForeignArg f h where
    convertArg :: (f -> IO a) -> h -> IO a

class ForeignRes f h where
    convertRes :: IO f -> IO h

class Call f h where
    callIO :: IO f -> h

instance ForeignRes fr hr => Call (IO fr) (IO hr) where
    callIO f = f >>= convertRes

instance (ForeignArg fa ha, Call ff hf) => Call (fa -> ff) (ha -> hf) where
    callIO f = callIO . convertArg (\fa -> f >>= return . ($fa))

call:: Call f h => f -> h
call = callIO . return
------------------------------------------------------------------------

Example of usage:

openFd:: String -> Int -> Int -> IO Fd
openFd = call openC
foreign import "open" unsafe openC :: CString -> CInt -> CMode -> IO (ThrowIfMinus1 Fd)

> PS. Even if this should do what you want, I'm not sure how
>     much type-class machinery and partial applications you'll
>     want to introduce at the ground level of a foreign function
>     interface. Unless compilers are very good at getting all
>     this machinery out of the way, the word overhead comes
>     to mind.

I hope that compilers will be able to reasonably inline it, eliminating
the overhead. At least theorethically is should be possible.

Unfortunately I guess that GHC does not unfold any recursion.
Maybe it won't be hard to make it possible to add a hint:
   {-# UNFOLD 6 callIO #-}
that the programmer politely asks to try to inline and unfold the
recursion up to 6 times at call sites to see if it finishes?

If tuples were changed to be defined recursively - I think it would
work, be compatible and be a good idea - then it would be useful to
have (==) on small tuples with class dispatching optimized away.

-- 
 __("<  Marcin Kowalczyk * [EMAIL PROTECTED] http://qrczak.ids.net.pl/
 \__/            GCS/M d- s+:-- a23 C+++$ UL++>++++$ P+++ L++>++++$ E-
  ^^                W++ N+++ o? K? w(---) O? M- V? PS-- PE++ Y? PGP+ t
QRCZAK                5? X- R tv-- b+>++ DI D- G+ e>++++ h! r--%>++ y-


Reply via email to