Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plug new FFI method into GHC

Tags:

haskell

ffi

Is there a way to plug a Haskell function of type

myFFI :: (C a) => String -> IO a

(where C is some typeclass describing the types of variables I can import) into GHC as an FFI scheme so that I can write in my Haskell program stuff like

foreign import myFFI "foo" foo :: T1 -> T2

that gets compiled into a call to foo = unsafePerformIO $ myFFI "foo" :: T1 -> T2?

I imagine this could be done by modifying GHC, but is there a way to do it via a plugin I can write without touching the GHC codebase proper?

like image 450
Cactus Avatar asked Mar 24 '12 16:03

Cactus


1 Answers

To answer the question in the comments (since the main question is answered with "use TH"), you can use TH as well to collect a list of all the names you've thus bound. Then, at startup, an init call can walk through that and force them.

like image 124
sclv Avatar answered Nov 12 '22 05:11

sclv