Anonymous FFI calls

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
19 messages Options
Reply | Threaded
Open this post in threaded view
|

Anonymous FFI calls

Francesco Mazzoli
Hi,

I am in a situation where it would be very useful to call C functions
without an explicit FFI import.  For example, I'd like to be able to do

    (foreign import ccall "cadd" :: CInt -> CInt -> CInt) 1 2

instead of declaring the foreign import explicitely at the top level.

Is there a way to do this or to achieve similar results in some other
way?

If not, I imagine it would be easy to implement such a facility in GHC,
given that the code implementing calling to C functions must already be
present to implement "proper" FFI imports.  I think such an addition
would be useful in many cases.

Thanks,
Francesco

Reply | Threaded
Open this post in threaded view
|

[Haskell-cafe] Anonymous FFI calls

Simon Peyton Jones
This may be relevant.  http://www.cse.unsw.edu.au/~chak/papers/CMCK14.html
Manuel gave a talk about something like this at the Haskell Symposium.

Simon

|  -----Original Message-----
|  From: Haskell-Cafe [mailto:haskell-cafe-bounces at haskell.org] On Behalf
|  Of Francesco Mazzoli
|  Sent: 11 February 2015 10:26
|  To: haskell; ghc-devs at haskell.org
|  Subject: [Haskell-cafe] Anonymous FFI calls
|  
|  Hi,
|  
|  I am in a situation where it would be very useful to call C functions
|  without an explicit FFI import.  For example, I'd like to be able to
|  do
|  
|      (foreign import ccall "cadd" :: CInt -> CInt -> CInt) 1 2
|  
|  instead of declaring the foreign import explicitely at the top level.
|  
|  Is there a way to do this or to achieve similar results in some other
|  way?
|  
|  If not, I imagine it would be easy to implement such a facility in
|  GHC, given that the code implementing calling to C functions must
|  already be present to implement "proper" FFI imports.  I think such an
|  addition would be useful in many cases.
|  
|  Thanks,
|  Francesco
|  _______________________________________________
|  Haskell-Cafe mailing list
|  Haskell-Cafe at haskell.org
|  http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply | Threaded
Open this post in threaded view
|

[Haskell-cafe] Anonymous FFI calls

Francesco Mazzoli
Hi Simon,

Thanks for the reply.

The work on accelerated is indeed relevant: in accelerate-cuda, CUDA
kernels (which are composed of C code) get compiled to a binary file
invoking an external compiler, and then (this is the relevant part)
the compiled binary is executed used the CUDA library.

The problem is that while CUDA kernels are executable through a
libraries, compiled functions using the C calling convention aren't.
So I can't really call compiled C functions like accelerate-cuda
invokes CUDA kernels.

Relatedly, if I have some function pointer known at runtime that
addresses a C function that takes some arguments, I have no way to
invoke it from Haskell, since all FFI imports must be declared at
compile-time, and I don't know the address of the symbol I want to
execute at compile time.

I am exploring the possibility of adding such capabilities to GHC,
since as I mentioned I don't see any particular roadblock.

Francesco

On 11 February 2015 at 11:40, Simon Peyton Jones <simonpj at microsoft.com> wrote:

> This may be relevant.  http://www.cse.unsw.edu.au/~chak/papers/CMCK14.html
> Manuel gave a talk about something like this at the Haskell Symposium.
>
> Simon
>
> |  -----Original Message-----
> |  From: Haskell-Cafe [mailto:haskell-cafe-bounces at haskell.org] On Behalf
> |  Of Francesco Mazzoli
> |  Sent: 11 February 2015 10:26
> |  To: haskell; ghc-devs at haskell.org
> |  Subject: [Haskell-cafe] Anonymous FFI calls
> |
> |  Hi,
> |
> |  I am in a situation where it would be very useful to call C functions
> |  without an explicit FFI import.  For example, I'd like to be able to
> |  do
> |
> |      (foreign import ccall "cadd" :: CInt -> CInt -> CInt) 1 2
> |
> |  instead of declaring the foreign import explicitely at the top level.
> |
> |  Is there a way to do this or to achieve similar results in some other
> |  way?
> |
> |  If not, I imagine it would be easy to implement such a facility in
> |  GHC, given that the code implementing calling to C functions must
> |  already be present to implement "proper" FFI imports.  I think such an
> |  addition would be useful in many cases.
> |
> |  Thanks,
> |  Francesco
> |  _______________________________________________
> |  Haskell-Cafe mailing list
> |  Haskell-Cafe at haskell.org
> |  http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply | Threaded
Open this post in threaded view
|

[Haskell-cafe] Anonymous FFI calls

Sylvain HENRY
2015-02-11 16:18 GMT+01:00 Francesco Mazzoli <f at mazzo.li>:

>
> Relatedly, if I have some function pointer known at runtime that
> addresses a C function that takes some arguments, I have no way to
> invoke it from Haskell, since all FFI imports must be declared at
> compile-time, and I don't know the address of the symbol I want to
> execute at compile time.
>

You can use FunPtr and wrapper imports to convert a FunPtr into a Haskell
function.
https://hackage.haskell.org/package/base-4.7.0.2/docs/Foreign-Ptr.html#g:2

You may be interested in my dynamic-linker-template package [1] to avoid
having to write boilerplate wrappers. For now it only works with dynamic
linking from System.Posix.DynamicLinker, but it could be easily extended to
support other platforms. It automatically generates wrappers for all the
functions in a record as well as the code to load symbol addresses and to
convert them into Haskell functions (examples [2,3]).

Sylvain

[1] https://hackage.haskell.org/package/dynamic-linker-template
[2]
https://github.com/hsyl20/dynamic-linker-template/blob/master/Tests/Test.hs
[3]
https://github.com/hsyl20/ViperVM/blob/master/src/lib/ViperVM/Arch/OpenCL/Library.hs
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/ghc-devs/attachments/20150211/e5a7aa8c/attachment.html>

Reply | Threaded
Open this post in threaded view
|

[Haskell-cafe] Anonymous FFI calls

Adam Vogt
On Wed, Feb 11, 2015 at 12:26 PM, Sylvain Henry <hsyl20 at gmail.com> wrote:

> 2015-02-11 16:18 GMT+01:00 Francesco Mazzoli <f at mazzo.li>:
>>
>>
>> Relatedly, if I have some function pointer known at runtime that
>> addresses a C function that takes some arguments, I have no way to
>> invoke it from Haskell, since all FFI imports must be declared at
>> compile-time, and I don't know the address of the symbol I want to
>> execute at compile time.
>
>
> You can use FunPtr and wrapper imports to convert a FunPtr into a Haskell
> function.
> https://hackage.haskell.org/package/base-4.7.0.2/docs/Foreign-Ptr.html#g:2
>
> You may be interested in my dynamic-linker-template package [1] to avoid
> having to write boilerplate wrappers.

Code using http://hackage.haskell.org/package/libffi looks pretty
similar, though the specification needs terms (such as argCInt)
instead of a mandatory type signature. GHC itself includes libffi so
maybe the foreign import "dynamic" calls end up doing exactly the same
thing as the hackage libffi does.

Reply | Threaded
Open this post in threaded view
|

[Haskell-cafe] Anonymous FFI calls

Francesco Mazzoli
In reply to this post by Sylvain HENRY
On 11 February 2015 at 18:26, Sylvain Henry <hsyl20 at gmail.com> wrote:
> You can use FunPtr and wrapper imports to convert a FunPtr into a Haskell
> function.
> https://hackage.haskell.org/package/base-4.7.0.2/docs/Foreign-Ptr.html#g:2

The problem with that scheme is that you have to define a FFI import
per type.  TH solutions are not viable (and cumbersome) for our use
case -- the biggest problem being related to the TH staging
restriction.

> Code using http://hackage.haskell.org/package/libffi looks pretty
> similar, though the specification needs terms (such as argCInt)
> instead of a mandatory type signature. GHC itself includes libffi so
> maybe the foreign import "dynamic" calls end up doing exactly the same
> thing as the hackage libffi does.

Thanks!  That seems like a good workaround for the time being -- we
had actually considered for a split second re-implementing the C
calling convention, I should have suspected that such a library
existed already.

However, while that library works for pointers, it does not (and
can't) have the facilities to refer to symbols.

In any case, there still is the need for such a facility built-in GHC,
since such solutions are always going to incur in some overhead,
compared to calling C functions directly.  For example in the linked
bindings to `libffi' the arguments are passed as a list, which is
certainly a big difference from storing them into registers.

And again, it seems like 95% of the work is already done, since
`foreign import's are already desugared to Haskell functions with a
primitive call to the C function.

Francesco

Reply | Threaded
Open this post in threaded view
|

[Haskell-cafe] Anonymous FFI calls

Simon Peyton Jones
In reply to this post by Adam Vogt
There's a bunch of useful info on this thread.   Does anyone feel able to distill it to our main FFI wiki page:
        https://wiki.haskell.org/GHC/Using_the_FFI
That way, future generations will benefit.

(These user-oriented wiki pages are all linked from GHC's documentation page https://wiki.haskell.org/GHC.)

Thanks!

Simon

| -----Original Message-----
| From: ghc-devs [mailto:ghc-devs-bounces at haskell.org] On Behalf Of adam
| vogt
| Sent: 11 February 2015 19:32
| To: Sylvain Henry
| Cc: ghc-devs at haskell.org; haskell
| Subject: Re: [Haskell-cafe] Anonymous FFI calls
|
| On Wed, Feb 11, 2015 at 12:26 PM, Sylvain Henry <hsyl20 at gmail.com> wrote:
| > 2015-02-11 16:18 GMT+01:00 Francesco Mazzoli <f at mazzo.li>:
| >>
| >>
| >> Relatedly, if I have some function pointer known at runtime that
| >> addresses a C function that takes some arguments, I have no way to
| >> invoke it from Haskell, since all FFI imports must be declared at
| >> compile-time, and I don't know the address of the symbol I want to
| >> execute at compile time.
| >
| >
| > You can use FunPtr and wrapper imports to convert a FunPtr into a
| Haskell
| > function.
| > https://hackage.haskell.org/package/base-4.7.0.2/docs/Foreign-
| Ptr.html#g:2
| >
| > You may be interested in my dynamic-linker-template package [1] to
| avoid
| > having to write boilerplate wrappers.
|
| Code using http://hackage.haskell.org/package/libffi looks pretty
| similar, though the specification needs terms (such as argCInt)
| instead of a mandatory type signature. GHC itself includes libffi so
| maybe the foreign import "dynamic" calls end up doing exactly the same
| thing as the hackage libffi does.
| _______________________________________________
| ghc-devs mailing list
| ghc-devs at haskell.org
| http://www.haskell.org/mailman/listinfo/ghc-devs

Reply | Threaded
Open this post in threaded view
|

[Haskell-cafe] Anonymous FFI calls

Michael Sloan
In reply to this post by Francesco Mazzoli
It seems like addTopDecls[1] will able to help here.  Unfortunately,
the function is not well documented and not very discoverable because
it's only exported by Language.Haskell.TH.

The documentation doesn't mention that it can only be used to create
new top level functions and FFI imports[2].  I think that adding FFI
imports was the main motivation for implementing it.   In the past
I've wanted to generate instances via this function, but unfortunately
it's not implemented..

Hope that helps!
-Michael

[1] http://hackage.haskell.org/package/template-haskell-2.9.0.0/docs/Language-Haskell-TH-Syntax.html#v:addTopDecls

[2] https://github.com/ghc/ghc/blob/1d982ba10f590828b78eba992e73315dee33f78a/compiler/typecheck/TcSplice.hs#L818

On Wed, Feb 11, 2015 at 2:26 AM, Francesco Mazzoli <f at mazzo.li> wrote:

> Hi,
>
> I am in a situation where it would be very useful to call C functions
> without an explicit FFI import.  For example, I'd like to be able to do
>
>     (foreign import ccall "cadd" :: CInt -> CInt -> CInt) 1 2
>
> instead of declaring the foreign import explicitely at the top level.
>
> Is there a way to do this or to achieve similar results in some other
> way?
>
> If not, I imagine it would be easy to implement such a facility in GHC,
> given that the code implementing calling to C functions must already be
> present to implement "proper" FFI imports.  I think such an addition
> would be useful in many cases.
>
> Thanks,
> Francesco
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply | Threaded
Open this post in threaded view
|

[Haskell-cafe] Anonymous FFI calls

Simon Peyton Jones
I would LOVE someone to improve the documentation for addTopDecls.  Manuel Chakravarty and Geoff Mainland were responsible for the implementation.

Simon

| -----Original Message-----
| From: ghc-devs [mailto:ghc-devs-bounces at haskell.org] On Behalf Of Michael
| Sloan
| Sent: 11 February 2015 23:19
| To: Francesco Mazzoli
| Cc: ghc-devs at haskell.org; haskell
| Subject: Re: [Haskell-cafe] Anonymous FFI calls
|
| It seems like addTopDecls[1] will able to help here.  Unfortunately,
| the function is not well documented and not very discoverable because
| it's only exported by Language.Haskell.TH.
|
| The documentation doesn't mention that it can only be used to create
| new top level functions and FFI imports[2].  I think that adding FFI
| imports was the main motivation for implementing it.   In the past
| I've wanted to generate instances via this function, but unfortunately
| it's not implemented..
|
| Hope that helps!
| -Michael
|
| [1] http://hackage.haskell.org/package/template-haskell-
| 2.9.0.0/docs/Language-Haskell-TH-Syntax.html#v:addTopDecls
|
| [2]
| https://github.com/ghc/ghc/blob/1d982ba10f590828b78eba992e73315dee33f78a/
| compiler/typecheck/TcSplice.hs#L818
|
| On Wed, Feb 11, 2015 at 2:26 AM, Francesco Mazzoli <f at mazzo.li> wrote:
| > Hi,
| >
| > I am in a situation where it would be very useful to call C functions
| > without an explicit FFI import.  For example, I'd like to be able to do
| >
| >     (foreign import ccall "cadd" :: CInt -> CInt -> CInt) 1 2
| >
| > instead of declaring the foreign import explicitely at the top level.
| >
| > Is there a way to do this or to achieve similar results in some other
| > way?
| >
| > If not, I imagine it would be easy to implement such a facility in GHC,
| > given that the code implementing calling to C functions must already be
| > present to implement "proper" FFI imports.  I think such an addition
| > would be useful in many cases.
| >
| > Thanks,
| > Francesco
| > _______________________________________________
| > Haskell-Cafe mailing list
| > Haskell-Cafe at haskell.org
| > http://www.haskell.org/mailman/listinfo/haskell-cafe
| _______________________________________________
| ghc-devs mailing list
| ghc-devs at haskell.org
| http://www.haskell.org/mailman/listinfo/ghc-devs

Reply | Threaded
Open this post in threaded view
|

[Haskell-cafe] Anonymous FFI calls

Michael Sloan
I'd love for the implementation to be as powerful as the documentation
suggests it is :D

However, yes, in the meantime, fixing the documentation would be great!

Also, I meant to say that addTopDecls is only exported by
"Language.Haskell.TH.Syntax".  While this is a digression, there are a
few other handy functions that are oddly left out of
"Language.Haskell.TH": addDependentFile, addModFinalizer, and possibly
more.

-Michael

On Wed, Feb 11, 2015 at 3:25 PM, Simon Peyton Jones
<simonpj at microsoft.com> wrote:

> I would LOVE someone to improve the documentation for addTopDecls.  Manuel Chakravarty and Geoff Mainland were responsible for the implementation.
>
> Simon
>
> | -----Original Message-----
> | From: ghc-devs [mailto:ghc-devs-bounces at haskell.org] On Behalf Of Michael
> | Sloan
> | Sent: 11 February 2015 23:19
> | To: Francesco Mazzoli
> | Cc: ghc-devs at haskell.org; haskell
> | Subject: Re: [Haskell-cafe] Anonymous FFI calls
> |
> | It seems like addTopDecls[1] will able to help here.  Unfortunately,
> | the function is not well documented and not very discoverable because
> | it's only exported by Language.Haskell.TH.
> |
> | The documentation doesn't mention that it can only be used to create
> | new top level functions and FFI imports[2].  I think that adding FFI
> | imports was the main motivation for implementing it.   In the past
> | I've wanted to generate instances via this function, but unfortunately
> | it's not implemented..
> |
> | Hope that helps!
> | -Michael
> |
> | [1] http://hackage.haskell.org/package/template-haskell-
> | 2.9.0.0/docs/Language-Haskell-TH-Syntax.html#v:addTopDecls
> |
> | [2]
> | https://github.com/ghc/ghc/blob/1d982ba10f590828b78eba992e73315dee33f78a/
> | compiler/typecheck/TcSplice.hs#L818
> |
> | On Wed, Feb 11, 2015 at 2:26 AM, Francesco Mazzoli <f at mazzo.li> wrote:
> | > Hi,
> | >
> | > I am in a situation where it would be very useful to call C functions
> | > without an explicit FFI import.  For example, I'd like to be able to do
> | >
> | >     (foreign import ccall "cadd" :: CInt -> CInt -> CInt) 1 2
> | >
> | > instead of declaring the foreign import explicitely at the top level.
> | >
> | > Is there a way to do this or to achieve similar results in some other
> | > way?
> | >
> | > If not, I imagine it would be easy to implement such a facility in GHC,
> | > given that the code implementing calling to C functions must already be
> | > present to implement "proper" FFI imports.  I think such an addition
> | > would be useful in many cases.
> | >
> | > Thanks,
> | > Francesco
> | > _______________________________________________
> | > Haskell-Cafe mailing list
> | > Haskell-Cafe at haskell.org
> | > http://www.haskell.org/mailman/listinfo/haskell-cafe
> | _______________________________________________
> | ghc-devs mailing list
> | ghc-devs at haskell.org
> | http://www.haskell.org/mailman/listinfo/ghc-devs

Reply | Threaded
Open this post in threaded view
|

[Haskell-cafe] Anonymous FFI calls

Michael Sloan
Hi!

I don't mean to be accusatory with the whole documentation thing - I
totally understand that most folks have a lot going on!  In this
particular case, I got very excited when I initially saw addTopDecls,
because it says it allows you to "Add additional top-level
declarations".  After implementing something to use it, I got rather
disappointed as it couldn't add instances.

So, when I say "not implemented", it's just that the docs say it
allows you to add additional top-level decls, when you can really only
add functions, variable bindings, and foreign import decls.

I didn't realize that not re-exporting these functions is due to their
experimental status.  That makes sense!  I'm glad it was a conscious
decision.  I just tried writing a demo of addTopDecls, but
unfortunately using it to add a function and referencing this function
causes a GHC internal error:
https://gist.github.com/mgsloan/53d7fa50338c696e5c80 .  I haven't
tried it with a foreign import yet.

-Michael

On Wed, Feb 11, 2015 at 3:51 PM, Geoffrey Mainland
<mainland at cs.drexel.edu> wrote:

> Typed Template Haskell happened at the end of my tenure at MSR, and
> there was a mad rush to get it in to the compiler in time for 7.8.3 at
> the same time I was starting a new job. I'm afraid the documentation is
> indeed wanting, but the alternative was no typed Template Haskell.
>
> One side-effect was that the functions you note as present only in
> Language.Haskell.TH.Syntax were not thoroughly vetted, so we didn't
> re-export them from Language.Haskell.TH.
>
> I am willing to help with documentation if people want to use this
> functionality. Has anyone attempted to use addTopDecls? Michael, when
> you say "it's not implemented," what do you mean? What, exactly, is not
> implemented?
>
> Cheers,
> Geoff
>
> On 2/11/15 6:32 PM, Michael Sloan wrote:
>> I'd love for the implementation to be as powerful as the documentation
>> suggests it is :D
>>
>> However, yes, in the meantime, fixing the documentation would be great!
>>
>> Also, I meant to say that addTopDecls is only exported by
>> "Language.Haskell.TH.Syntax".  While this is a digression, there are a
>> few other handy functions that are oddly left out of
>> "Language.Haskell.TH": addDependentFile, addModFinalizer, and possibly
>> more.
>>
>> -Michael
>>
>> On Wed, Feb 11, 2015 at 3:25 PM, Simon Peyton Jones
>> <simonpj at microsoft.com> wrote:
>>> I would LOVE someone to improve the documentation for addTopDecls.  Manuel Chakravarty and Geoff Mainland were responsible for the implementation.
>>>
>>> Simon
>>>
>>> | -----Original Message-----
>>> | From: ghc-devs [mailto:ghc-devs-bounces at haskell.org] On Behalf Of Michael
>>> | Sloan
>>> | Sent: 11 February 2015 23:19
>>> | To: Francesco Mazzoli
>>> | Cc: ghc-devs at haskell.org; haskell
>>> | Subject: Re: [Haskell-cafe] Anonymous FFI calls
>>> |
>>> | It seems like addTopDecls[1] will able to help here.  Unfortunately,
>>> | the function is not well documented and not very discoverable because
>>> | it's only exported by Language.Haskell.TH.
>>> |
>>> | The documentation doesn't mention that it can only be used to create
>>> | new top level functions and FFI imports[2].  I think that adding FFI
>>> | imports was the main motivation for implementing it.   In the past
>>> | I've wanted to generate instances via this function, but unfortunately
>>> | it's not implemented..
>>> |
>>> | Hope that helps!
>>> | -Michael
>>> |
>>> | [1] http://hackage.haskell.org/package/template-haskell-
>>> | 2.9.0.0/docs/Language-Haskell-TH-Syntax.html#v:addTopDecls
>>> |
>>> | [2]
>>> | https://github.com/ghc/ghc/blob/1d982ba10f590828b78eba992e73315dee33f78a/
>>> | compiler/typecheck/TcSplice.hs#L818
>>> |
>>> | On Wed, Feb 11, 2015 at 2:26 AM, Francesco Mazzoli <f at mazzo.li> wrote:
>>> | > Hi,
>>> | >
>>> | > I am in a situation where it would be very useful to call C functions
>>> | > without an explicit FFI import.  For example, I'd like to be able to do
>>> | >
>>> | >     (foreign import ccall "cadd" :: CInt -> CInt -> CInt) 1 2
>>> | >
>>> | > instead of declaring the foreign import explicitely at the top level.
>>> | >
>>> | > Is there a way to do this or to achieve similar results in some other
>>> | > way?
>>> | >
>>> | > If not, I imagine it would be easy to implement such a facility in GHC,
>>> | > given that the code implementing calling to C functions must already be
>>> | > present to implement "proper" FFI imports.  I think such an addition
>>> | > would be useful in many cases.
>>> | >
>>> | > Thanks,
>>> | > Francesco
>>> | > _______________________________________________
>>> | > Haskell-Cafe mailing list
>>> | > Haskell-Cafe at haskell.org
>>> | > http://www.haskell.org/mailman/listinfo/haskell-cafe
>>> | _______________________________________________
>>> | ghc-devs mailing list
>>> | ghc-devs at haskell.org
>>> | http://www.haskell.org/mailman/listinfo/ghc-devs
>

Reply | Threaded
Open this post in threaded view
|

[Haskell-cafe] Anonymous FFI calls

Luite Stegeman
In reply to this post by Simon Peyton Jones
Another example, similar to Manuel's work but for GHCJS JavaScriptFFI
is ghcjs-ffiqq: https://github.com/ghcjs/ghcjs-ffiqq . There is some
automatic marshalling of arguments and results using the ToJSRef /
FromJSRef typeclasses. I'm working on integrating the improvements in
these typeclasses back into ghcjs-base. The quasiquoter might be moved
there too, but I'm not sure yet.

The (ugly) implementation is here:
https://github.com/ghcjs/ghcjs-ffiqq/blob/master/src/GHCJS/Foreign/QQ.hs

Adapting to ccall imports should be straightforward, but implementing
the full functionality would require a bit more stub code generation,
since this code depends on GHCJS' native support for inline foreign
import argument placeholders ($1, $2, $3 etc) placeholders.

luite

On Wed, Feb 11, 2015 at 11:40 PM, Simon Peyton Jones
<simonpj at microsoft.com> wrote:

> This may be relevant.  http://www.cse.unsw.edu.au/~chak/papers/CMCK14.html
> Manuel gave a talk about something like this at the Haskell Symposium.
>
> Simon
>
> |  -----Original Message-----
> |  From: Haskell-Cafe [mailto:haskell-cafe-bounces at haskell.org] On Behalf
> |  Of Francesco Mazzoli
> |  Sent: 11 February 2015 10:26
> |  To: haskell; ghc-devs at haskell.org
> |  Subject: [Haskell-cafe] Anonymous FFI calls
> |
> |  Hi,
> |
> |  I am in a situation where it would be very useful to call C functions
> |  without an explicit FFI import.  For example, I'd like to be able to
> |  do
> |
> |      (foreign import ccall "cadd" :: CInt -> CInt -> CInt) 1 2
> |
> |  instead of declaring the foreign import explicitely at the top level.
> |
> |  Is there a way to do this or to achieve similar results in some other
> |  way?
> |
> |  If not, I imagine it would be easy to implement such a facility in
> |  GHC, given that the code implementing calling to C functions must
> |  already be present to implement "proper" FFI imports.  I think such an
> |  addition would be useful in many cases.
> |
> |  Thanks,
> |  Francesco
> |  _______________________________________________
> |  Haskell-Cafe mailing list
> |  Haskell-Cafe at haskell.org
> |  http://www.haskell.org/mailman/listinfo/haskell-cafe
> _______________________________________________
> ghc-devs mailing list
> ghc-devs at haskell.org
> http://www.haskell.org/mailman/listinfo/ghc-devs

Reply | Threaded
Open this post in threaded view
|

[Haskell-cafe] Anonymous FFI calls

Michael Sloan
In reply to this post by Michael Sloan
It works, cool!  So at least this ought to address Francesco's use-case.

Is there anything wrong with my code?  It definitely looks like a GHC bug.

Yes, I've successfully generated instance declarations from the
top-level.  I may as well describe the usecase: pretty printing a
value of any type in GHCI.  While this feature list isn't the main
point here, it'd be very cool to do things like:

* Look up the value of mutable references such as IORefs
* Print out functions as just their type
* Be able to evaluate particular subtrees of the pretty print output
(like present [1])
* Select a subportion of the output and get a variable out of it

Usage would look like "$(pretty 'x)".  This would reify the type of x
and transitively generate `Pretty` instances for things, by using my
th-reify-many package [2].  It then generates an expression which
invokes `pretty` on `x`, using these new instances.  This instance
generation can do clever things like check if a type has a "Data"
instance, and use it if available.

In this way, we can construct a pretty printer for ghci which will
work for any type.  Certainly, this is still possible without instance
generation, but it'd be quite clever to not need to re-generate the
pretty printing code for every invocation.

-Michael

[1] https://github.com/chrisdone/present
[2] https://github.com/mgsloan/th-reify-many

On Wed, Feb 11, 2015 at 6:51 PM, Geoffrey Mainland
<mainland at cs.drexel.edu> wrote:

> Hi Michael,
>
> Try this out:
>
> https://gist.github.com/mainland/f18cf3827c4b0b64b835
>
> Have you had success generating instance declarations from top-level
> splices?
>
> Geoff
>
> On 02/11/2015 07:41 PM, Michael Sloan wrote:
>> Hi!
>>
>> I don't mean to be accusatory with the whole documentation thing - I
>> totally understand that most folks have a lot going on!  In this
>> particular case, I got very excited when I initially saw addTopDecls,
>> because it says it allows you to "Add additional top-level
>> declarations".  After implementing something to use it, I got rather
>> disappointed as it couldn't add instances.
>>
>> So, when I say "not implemented", it's just that the docs say it
>> allows you to add additional top-level decls, when you can really only
>> add functions, variable bindings, and foreign import decls.
>>
>> I didn't realize that not re-exporting these functions is due to their
>> experimental status.  That makes sense!  I'm glad it was a conscious
>> decision.  I just tried writing a demo of addTopDecls, but
>> unfortunately using it to add a function and referencing this function
>> causes a GHC internal error:
>> https://gist.github.com/mgsloan/53d7fa50338c696e5c80 .  I haven't
>> tried it with a foreign import yet.
>>
>> -Michael
>>
>> On Wed, Feb 11, 2015 at 3:51 PM, Geoffrey Mainland
>> <mainland at cs.drexel.edu> wrote:
>>> Typed Template Haskell happened at the end of my tenure at MSR, and
>>> there was a mad rush to get it in to the compiler in time for 7.8.3 at
>>> the same time I was starting a new job. I'm afraid the documentation is
>>> indeed wanting, but the alternative was no typed Template Haskell.
>>>
>>> One side-effect was that the functions you note as present only in
>>> Language.Haskell.TH.Syntax were not thoroughly vetted, so we didn't
>>> re-export them from Language.Haskell.TH.
>>>
>>> I am willing to help with documentation if people want to use this
>>> functionality. Has anyone attempted to use addTopDecls? Michael, when
>>> you say "it's not implemented," what do you mean? What, exactly, is not
>>> implemented?
>>>
>>> Cheers,
>>> Geoff
>>>
>>> On 2/11/15 6:32 PM, Michael Sloan wrote:
>>>> I'd love for the implementation to be as powerful as the documentation
>>>> suggests it is :D
>>>>
>>>> However, yes, in the meantime, fixing the documentation would be great!
>>>>
>>>> Also, I meant to say that addTopDecls is only exported by
>>>> "Language.Haskell.TH.Syntax".  While this is a digression, there are a
>>>> few other handy functions that are oddly left out of
>>>> "Language.Haskell.TH": addDependentFile, addModFinalizer, and possibly
>>>> more.
>>>>
>>>> -Michael
>>>>
>>>> On Wed, Feb 11, 2015 at 3:25 PM, Simon Peyton Jones
>>>> <simonpj at microsoft.com> wrote:
>>>>> I would LOVE someone to improve the documentation for addTopDecls.  Manuel Chakravarty and Geoff Mainland were responsible for the implementation.
>>>>>
>>>>> Simon
>>>>>
>>>>> | -----Original Message-----
>>>>> | From: ghc-devs [mailto:ghc-devs-bounces at haskell.org] On Behalf Of Michael
>>>>> | Sloan
>>>>> | Sent: 11 February 2015 23:19
>>>>> | To: Francesco Mazzoli
>>>>> | Cc: ghc-devs at haskell.org; haskell
>>>>> | Subject: Re: [Haskell-cafe] Anonymous FFI calls
>>>>> |
>>>>> | It seems like addTopDecls[1] will able to help here.  Unfortunately,
>>>>> | the function is not well documented and not very discoverable because
>>>>> | it's only exported by Language.Haskell.TH.
>>>>> |
>>>>> | The documentation doesn't mention that it can only be used to create
>>>>> | new top level functions and FFI imports[2].  I think that adding FFI
>>>>> | imports was the main motivation for implementing it.   In the past
>>>>> | I've wanted to generate instances via this function, but unfortunately
>>>>> | it's not implemented..
>>>>> |
>>>>> | Hope that helps!
>>>>> | -Michael
>>>>> |
>>>>> | [1] http://hackage.haskell.org/package/template-haskell-
>>>>> | 2.9.0.0/docs/Language-Haskell-TH-Syntax.html#v:addTopDecls
>>>>> |
>>>>> | [2]
>>>>> | https://github.com/ghc/ghc/blob/1d982ba10f590828b78eba992e73315dee33f78a/
>>>>> | compiler/typecheck/TcSplice.hs#L818
>>>>> |
>>>>> | On Wed, Feb 11, 2015 at 2:26 AM, Francesco Mazzoli <f at mazzo.li> wrote:
>>>>> | > Hi,
>>>>> | >
>>>>> | > I am in a situation where it would be very useful to call C functions
>>>>> | > without an explicit FFI import.  For example, I'd like to be able to do
>>>>> | >
>>>>> | >     (foreign import ccall "cadd" :: CInt -> CInt -> CInt) 1 2
>>>>> | >
>>>>> | > instead of declaring the foreign import explicitely at the top level.
>>>>> | >
>>>>> | > Is there a way to do this or to achieve similar results in some other
>>>>> | > way?
>>>>> | >
>>>>> | > If not, I imagine it would be easy to implement such a facility in GHC,
>>>>> | > given that the code implementing calling to C functions must already be
>>>>> | > present to implement "proper" FFI imports.  I think such an addition
>>>>> | > would be useful in many cases.
>>>>> | >
>>>>> | > Thanks,
>>>>> | > Francesco
>>>>> | > _______________________________________________
>>>>> | > Haskell-Cafe mailing list
>>>>> | > Haskell-Cafe at haskell.org
>>>>> | > http://www.haskell.org/mailman/listinfo/haskell-cafe
>>>>> | _______________________________________________
>>>>> | ghc-devs mailing list
>>>>> | ghc-devs at haskell.org
>>>>> | http://www.haskell.org/mailman/listinfo/ghc-devs
>

Reply | Threaded
Open this post in threaded view
|

[Haskell-cafe] Anonymous FFI calls

Simon Peyton Jones
In reply to this post by Michael Sloan
|  Also, I meant to say that addTopDecls is only exported by
|  "Language.Haskell.TH.Syntax".  While this is a digression, there are a
|  few other handy functions that are oddly left out of
|  "Language.Haskell.TH": addDependentFile, addModFinalizer, and possibly
|  more.

That does seem wrong.  Do make a patch!

SIMon

|  
|  -Michael
|  
|  On Wed, Feb 11, 2015 at 3:25 PM, Simon Peyton Jones
|  <simonpj at microsoft.com> wrote:
|  > I would LOVE someone to improve the documentation for addTopDecls.
|  Manuel Chakravarty and Geoff Mainland were responsible for the
|  implementation.
|  >
|  > Simon
|  >
|  > | -----Original Message-----
|  > | From: ghc-devs [mailto:ghc-devs-bounces at haskell.org] On Behalf Of
|  > | Michael Sloan
|  > | Sent: 11 February 2015 23:19
|  > | To: Francesco Mazzoli
|  > | Cc: ghc-devs at haskell.org; haskell
|  > | Subject: Re: [Haskell-cafe] Anonymous FFI calls
|  > |
|  > | It seems like addTopDecls[1] will able to help here.
|  Unfortunately,
|  > | the function is not well documented and not very discoverable
|  > | because it's only exported by Language.Haskell.TH.
|  > |
|  > | The documentation doesn't mention that it can only be used to
|  create
|  > | new top level functions and FFI imports[2].  I think that adding
|  FFI
|  > | imports was the main motivation for implementing it.   In the past
|  > | I've wanted to generate instances via this function, but
|  > | unfortunately it's not implemented..
|  > |
|  > | Hope that helps!
|  > | -Michael
|  > |
|  > | [1] http://hackage.haskell.org/package/template-haskell-
|  > | 2.9.0.0/docs/Language-Haskell-TH-Syntax.html#v:addTopDecls
|  > |
|  > | [2]
|  > |
|  https://github.com/ghc/ghc/blob/1d982ba10f590828b78eba992e73315dee33
|  > | f78a/
|  > | compiler/typecheck/TcSplice.hs#L818
|  > |
|  > | On Wed, Feb 11, 2015 at 2:26 AM, Francesco Mazzoli <f at mazzo.li>
|  wrote:
|  > | > Hi,
|  > | >
|  > | > I am in a situation where it would be very useful to call C
|  > | > functions without an explicit FFI import.  For example, I'd like
|  > | > to be able to do
|  > | >
|  > | >     (foreign import ccall "cadd" :: CInt -> CInt -> CInt) 1 2
|  > | >
|  > | > instead of declaring the foreign import explicitely at the top
|  level.
|  > | >
|  > | > Is there a way to do this or to achieve similar results in some
|  > | > other way?
|  > | >
|  > | > If not, I imagine it would be easy to implement such a facility
|  in
|  > | > GHC, given that the code implementing calling to C functions
|  must
|  > | > already be present to implement "proper" FFI imports.  I think
|  > | > such an addition would be useful in many cases.
|  > | >
|  > | > Thanks,
|  > | > Francesco
|  > | > _______________________________________________
|  > | > Haskell-Cafe mailing list
|  > | > Haskell-Cafe at haskell.org
|  > | > http://www.haskell.org/mailman/listinfo/haskell-cafe
|  > | _______________________________________________
|  > | ghc-devs mailing list
|  > | ghc-devs at haskell.org
|  > | http://www.haskell.org/mailman/listinfo/ghc-devs

Reply | Threaded
Open this post in threaded view
|

[Haskell-cafe] Anonymous FFI calls

Francesco Mazzoli
Thanks to everyone who replied!

It seems like that through a combination of facilities like `libffi'
and `addTopDecls' I can do everything that I wanted to do.

I still want to take a shot at implementing anonymous FFI calls, since
IMHO I think they are a very small but useful addition to the
language.

Francesco

On 12 February 2015 at 09:29, Simon Peyton Jones <simonpj at microsoft.com> wrote:

> |  Also, I meant to say that addTopDecls is only exported by
> |  "Language.Haskell.TH.Syntax".  While this is a digression, there are a
> |  few other handy functions that are oddly left out of
> |  "Language.Haskell.TH": addDependentFile, addModFinalizer, and possibly
> |  more.
>
> That does seem wrong.  Do make a patch!
>
> SIMon
>
> |
> |  -Michael
> |
> |  On Wed, Feb 11, 2015 at 3:25 PM, Simon Peyton Jones
> |  <simonpj at microsoft.com> wrote:
> |  > I would LOVE someone to improve the documentation for addTopDecls.
> |  Manuel Chakravarty and Geoff Mainland were responsible for the
> |  implementation.
> |  >
> |  > Simon
> |  >
> |  > | -----Original Message-----
> |  > | From: ghc-devs [mailto:ghc-devs-bounces at haskell.org] On Behalf Of
> |  > | Michael Sloan
> |  > | Sent: 11 February 2015 23:19
> |  > | To: Francesco Mazzoli
> |  > | Cc: ghc-devs at haskell.org; haskell
> |  > | Subject: Re: [Haskell-cafe] Anonymous FFI calls
> |  > |
> |  > | It seems like addTopDecls[1] will able to help here.
> |  Unfortunately,
> |  > | the function is not well documented and not very discoverable
> |  > | because it's only exported by Language.Haskell.TH.
> |  > |
> |  > | The documentation doesn't mention that it can only be used to
> |  create
> |  > | new top level functions and FFI imports[2].  I think that adding
> |  FFI
> |  > | imports was the main motivation for implementing it.   In the past
> |  > | I've wanted to generate instances via this function, but
> |  > | unfortunately it's not implemented..
> |  > |
> |  > | Hope that helps!
> |  > | -Michael
> |  > |
> |  > | [1] http://hackage.haskell.org/package/template-haskell-
> |  > | 2.9.0.0/docs/Language-Haskell-TH-Syntax.html#v:addTopDecls
> |  > |
> |  > | [2]
> |  > |
> |  https://github.com/ghc/ghc/blob/1d982ba10f590828b78eba992e73315dee33
> |  > | f78a/
> |  > | compiler/typecheck/TcSplice.hs#L818
> |  > |
> |  > | On Wed, Feb 11, 2015 at 2:26 AM, Francesco Mazzoli <f at mazzo.li>
> |  wrote:
> |  > | > Hi,
> |  > | >
> |  > | > I am in a situation where it would be very useful to call C
> |  > | > functions without an explicit FFI import.  For example, I'd like
> |  > | > to be able to do
> |  > | >
> |  > | >     (foreign import ccall "cadd" :: CInt -> CInt -> CInt) 1 2
> |  > | >
> |  > | > instead of declaring the foreign import explicitely at the top
> |  level.
> |  > | >
> |  > | > Is there a way to do this or to achieve similar results in some
> |  > | > other way?
> |  > | >
> |  > | > If not, I imagine it would be easy to implement such a facility
> |  in
> |  > | > GHC, given that the code implementing calling to C functions
> |  must
> |  > | > already be present to implement "proper" FFI imports.  I think
> |  > | > such an addition would be useful in many cases.
> |  > | >
> |  > | > Thanks,
> |  > | > Francesco
> |  > | > _______________________________________________
> |  > | > Haskell-Cafe mailing list
> |  > | > Haskell-Cafe at haskell.org
> |  > | > http://www.haskell.org/mailman/listinfo/haskell-cafe
> |  > | _______________________________________________
> |  > | ghc-devs mailing list
> |  > | ghc-devs at haskell.org
> |  > | http://www.haskell.org/mailman/listinfo/ghc-devs

Reply | Threaded
Open this post in threaded view
|

[Haskell-cafe] Anonymous FFI calls

Simon Peyton Jones
| Thanks to everyone who replied!
|
| It seems like that through a combination of facilities like `libffi'
| and `addTopDecls' I can do everything that I wanted to do.

Great.  But please, please, do write up what you learned on the FFI wiki page
        https://wiki.haskell.org/GHC/Using_the_FFI

Simon

| -----Original Message-----
| From: Francesco Mazzoli [mailto:f at mazzo.li]
| Sent: 12 February 2015 09:00
| To: Simon Peyton Jones
| Cc: Michael Sloan; Manuel Chakravarty; Geoffrey Mainland
| (mainland at cs.drexel.edu); ghc-devs at haskell.org; haskell
| Subject: Re: [Haskell-cafe] Anonymous FFI calls
|
| Thanks to everyone who replied!
|
| It seems like that through a combination of facilities like `libffi'
| and `addTopDecls' I can do everything that I wanted to do.
|
| I still want to take a shot at implementing anonymous FFI calls, since
| IMHO I think they are a very small but useful addition to the
| language.
|
| Francesco
|
| On 12 February 2015 at 09:29, Simon Peyton Jones <simonpj at microsoft.com>
| wrote:
| > |  Also, I meant to say that addTopDecls is only exported by
| > |  "Language.Haskell.TH.Syntax".  While this is a digression, there are
| a
| > |  few other handy functions that are oddly left out of
| > |  "Language.Haskell.TH": addDependentFile, addModFinalizer, and
| possibly
| > |  more.
| >
| > That does seem wrong.  Do make a patch!
| >
| > SIMon
| >
| > |
| > |  -Michael
| > |
| > |  On Wed, Feb 11, 2015 at 3:25 PM, Simon Peyton Jones
| > |  <simonpj at microsoft.com> wrote:
| > |  > I would LOVE someone to improve the documentation for addTopDecls.
| > |  Manuel Chakravarty and Geoff Mainland were responsible for the
| > |  implementation.
| > |  >
| > |  > Simon
| > |  >
| > |  > | -----Original Message-----
| > |  > | From: ghc-devs [mailto:ghc-devs-bounces at haskell.org] On Behalf
| Of
| > |  > | Michael Sloan
| > |  > | Sent: 11 February 2015 23:19
| > |  > | To: Francesco Mazzoli
| > |  > | Cc: ghc-devs at haskell.org; haskell
| > |  > | Subject: Re: [Haskell-cafe] Anonymous FFI calls
| > |  > |
| > |  > | It seems like addTopDecls[1] will able to help here.
| > |  Unfortunately,
| > |  > | the function is not well documented and not very discoverable
| > |  > | because it's only exported by Language.Haskell.TH.
| > |  > |
| > |  > | The documentation doesn't mention that it can only be used to
| > |  create
| > |  > | new top level functions and FFI imports[2].  I think that adding
| > |  FFI
| > |  > | imports was the main motivation for implementing it.   In the
| past
| > |  > | I've wanted to generate instances via this function, but
| > |  > | unfortunately it's not implemented..
| > |  > |
| > |  > | Hope that helps!
| > |  > | -Michael
| > |  > |
| > |  > | [1] http://hackage.haskell.org/package/template-haskell-
| > |  > | 2.9.0.0/docs/Language-Haskell-TH-Syntax.html#v:addTopDecls
| > |  > |
| > |  > | [2]
| > |  > |
| > |  https://github.com/ghc/ghc/blob/1d982ba10f590828b78eba992e73315dee33
| > |  > | f78a/
| > |  > | compiler/typecheck/TcSplice.hs#L818
| > |  > |
| > |  > | On Wed, Feb 11, 2015 at 2:26 AM, Francesco Mazzoli <f at mazzo.li>
| > |  wrote:
| > |  > | > Hi,
| > |  > | >
| > |  > | > I am in a situation where it would be very useful to call C
| > |  > | > functions without an explicit FFI import.  For example, I'd
| like
| > |  > | > to be able to do
| > |  > | >
| > |  > | >     (foreign import ccall "cadd" :: CInt -> CInt -> CInt) 1 2
| > |  > | >
| > |  > | > instead of declaring the foreign import explicitely at the top
| > |  level.
| > |  > | >
| > |  > | > Is there a way to do this or to achieve similar results in
| some
| > |  > | > other way?
| > |  > | >
| > |  > | > If not, I imagine it would be easy to implement such a
| facility
| > |  in
| > |  > | > GHC, given that the code implementing calling to C functions
| > |  must
| > |  > | > already be present to implement "proper" FFI imports.  I think
| > |  > | > such an addition would be useful in many cases.
| > |  > | >
| > |  > | > Thanks,
| > |  > | > Francesco
| > |  > | > _______________________________________________
| > |  > | > Haskell-Cafe mailing list
| > |  > | > Haskell-Cafe at haskell.org
| > |  > | > http://www.haskell.org/mailman/listinfo/haskell-cafe
| > |  > | _______________________________________________
| > |  > | ghc-devs mailing list
| > |  > | ghc-devs at haskell.org
| > |  > | http://www.haskell.org/mailman/listinfo/ghc-devs

Reply | Threaded
Open this post in threaded view
|

[Haskell-cafe] Anonymous FFI calls

Francesco Mazzoli
On 12 February 2015 at 10:02, Simon Peyton Jones <simonpj at microsoft.com> wrote:
> | Thanks to everyone who replied!
> |
> | It seems like that through a combination of facilities like `libffi'
> | and `addTopDecls' I can do everything that I wanted to do.
>
> Great.  But please, please, do write up what you learned on the FFI wiki page
>         https://wiki.haskell.org/GHC/Using_the_FFI

I'll try to find the time to do that.  I'll definitely have the time
to simply write "you should check out X and Y".

Francesco

Reply | Threaded
Open this post in threaded view
|

[Haskell-cafe] Anonymous FFI calls

Sylvain HENRY
In reply to this post by Simon Peyton Jones
Hi,

The FFI pages on the wiki are not really in a good shape in my opinion
(especially for newcomers).

I have started a fresh one here:
https://wiki.haskell.org/Foreign_Function_Interface_(FFI)

This is just the first draft. I will improve it, probably split it in
several pages and merge information from other pages, especially pages
linked on https://wiki.haskell.org/FFI

Sylvain

2015-02-12 10:02 GMT+01:00 Simon Peyton Jones <simonpj at microsoft.com>:

> | Thanks to everyone who replied!
> |
> | It seems like that through a combination of facilities like `libffi'
> | and `addTopDecls' I can do everything that I wanted to do.
>
> Great.  But please, please, do write up what you learned on the FFI wiki
> page
>         https://wiki.haskell.org/GHC/Using_the_FFI
>
> Simon
>
> | -----Original Message-----
> | From: Francesco Mazzoli [mailto:f at mazzo.li]
> | Sent: 12 February 2015 09:00
> | To: Simon Peyton Jones
> | Cc: Michael Sloan; Manuel Chakravarty; Geoffrey Mainland
> | (mainland at cs.drexel.edu); ghc-devs at haskell.org; haskell
> | Subject: Re: [Haskell-cafe] Anonymous FFI calls
> |
> | Thanks to everyone who replied!
> |
> | It seems like that through a combination of facilities like `libffi'
> | and `addTopDecls' I can do everything that I wanted to do.
> |
> | I still want to take a shot at implementing anonymous FFI calls, since
> | IMHO I think they are a very small but useful addition to the
> | language.
> |
> | Francesco
> |
> | On 12 February 2015 at 09:29, Simon Peyton Jones <simonpj at microsoft.com>
> | wrote:
> | > |  Also, I meant to say that addTopDecls is only exported by
> | > |  "Language.Haskell.TH.Syntax".  While this is a digression, there are
> | a
> | > |  few other handy functions that are oddly left out of
> | > |  "Language.Haskell.TH": addDependentFile, addModFinalizer, and
> | possibly
> | > |  more.
> | >
> | > That does seem wrong.  Do make a patch!
> | >
> | > SIMon
> | >
> | > |
> | > |  -Michael
> | > |
> | > |  On Wed, Feb 11, 2015 at 3:25 PM, Simon Peyton Jones
> | > |  <simonpj at microsoft.com> wrote:
> | > |  > I would LOVE someone to improve the documentation for addTopDecls.
> | > |  Manuel Chakravarty and Geoff Mainland were responsible for the
> | > |  implementation.
> | > |  >
> | > |  > Simon
> | > |  >
> | > |  > | -----Original Message-----
> | > |  > | From: ghc-devs [mailto:ghc-devs-bounces at haskell.org] On Behalf
> | Of
> | > |  > | Michael Sloan
> | > |  > | Sent: 11 February 2015 23:19
> | > |  > | To: Francesco Mazzoli
> | > |  > | Cc: ghc-devs at haskell.org; haskell
> | > |  > | Subject: Re: [Haskell-cafe] Anonymous FFI calls
> | > |  > |
> | > |  > | It seems like addTopDecls[1] will able to help here.
> | > |  Unfortunately,
> | > |  > | the function is not well documented and not very discoverable
> | > |  > | because it's only exported by Language.Haskell.TH.
> | > |  > |
> | > |  > | The documentation doesn't mention that it can only be used to
> | > |  create
> | > |  > | new top level functions and FFI imports[2].  I think that adding
> | > |  FFI
> | > |  > | imports was the main motivation for implementing it.   In the
> | past
> | > |  > | I've wanted to generate instances via this function, but
> | > |  > | unfortunately it's not implemented..
> | > |  > |
> | > |  > | Hope that helps!
> | > |  > | -Michael
> | > |  > |
> | > |  > | [1] http://hackage.haskell.org/package/template-haskell-
> | > |  > | 2.9.0.0/docs/Language-Haskell-TH-Syntax.html#v:addTopDecls
> | > |  > |
> | > |  > | [2]
> | > |  > |
> | > |
> https://github.com/ghc/ghc/blob/1d982ba10f590828b78eba992e73315dee33
> | > |  > | f78a/
> | > |  > | compiler/typecheck/TcSplice.hs#L818
> | > |  > |
> | > |  > | On Wed, Feb 11, 2015 at 2:26 AM, Francesco Mazzoli <f at mazzo.li>
> | > |  wrote:
> | > |  > | > Hi,
> | > |  > | >
> | > |  > | > I am in a situation where it would be very useful to call C
> | > |  > | > functions without an explicit FFI import.  For example, I'd
> | like
> | > |  > | > to be able to do
> | > |  > | >
> | > |  > | >     (foreign import ccall "cadd" :: CInt -> CInt -> CInt) 1 2
> | > |  > | >
> | > |  > | > instead of declaring the foreign import explicitely at the top
> | > |  level.
> | > |  > | >
> | > |  > | > Is there a way to do this or to achieve similar results in
> | some
> | > |  > | > other way?
> | > |  > | >
> | > |  > | > If not, I imagine it would be easy to implement such a
> | facility
> | > |  in
> | > |  > | > GHC, given that the code implementing calling to C functions
> | > |  must
> | > |  > | > already be present to implement "proper" FFI imports.  I think
> | > |  > | > such an addition would be useful in many cases.
> | > |  > | >
> | > |  > | > Thanks,
> | > |  > | > Francesco
> | > |  > | > _______________________________________________
> | > |  > | > Haskell-Cafe mailing list
> | > |  > | > Haskell-Cafe at haskell.org
> | > |  > | > http://www.haskell.org/mailman/listinfo/haskell-cafe
> | > |  > | _______________________________________________
> | > |  > | ghc-devs mailing list
> | > |  > | ghc-devs at haskell.org
> | > |  > | http://www.haskell.org/mailman/listinfo/ghc-devs
> _______________________________________________
> ghc-devs mailing list
> ghc-devs at haskell.org
> http://www.haskell.org/mailman/listinfo/ghc-devs
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/ghc-devs/attachments/20150213/0d9c99ce/attachment-0001.html>

Reply | Threaded
Open this post in threaded view
|

[Haskell-cafe] Anonymous FFI calls

Simon Peyton Jones
I have started a fresh one here: https://wiki.haskell.org/Foreign_Function_Interface_(FFI)

Very helpful thank you.  We NEED people to invest effort in good user guides.

Do also include info from
https://wiki.haskell.org/GHC/Using_the_FFI
as well as
https://wiki.haskell.org/FFI

Once you?ve absorbed them both, kill them off and replace with forwarding pointers. Or better still overwrite one or the other with your new page (people may have bookmarked the old links).

What we don?t want is lots of alternative pages, that don?t refer to each other, with contradictory info.

Simon

From: Sylvain Henry [mailto:hsyl20 at gmail.com]
Sent: 13 February 2015 18:28
To: Simon Peyton Jones
Cc: Francesco Mazzoli; Manuel Chakravarty; ghc-devs at haskell.org; Geoffrey Mainland (mainland at cs.drexel.edu)
Subject: Re: [Haskell-cafe] Anonymous FFI calls

Hi,

The FFI pages on the wiki are not really in a good shape in my opinion (especially for newcomers).

I have started a fresh one here: https://wiki.haskell.org/Foreign_Function_Interface_(FFI)

This is just the first draft. I will improve it, probably split it in several pages and merge information from other pages, especially pages linked on https://wiki.haskell.org/FFI

Sylvain

2015-02-12 10:02 GMT+01:00 Simon Peyton Jones <simonpj at microsoft.com<mailto:simonpj at microsoft.com>>:
| Thanks to everyone who replied!
|
| It seems like that through a combination of facilities like `libffi'
| and `addTopDecls' I can do everything that I wanted to do.

Great.  But please, please, do write up what you learned on the FFI wiki page
        https://wiki.haskell.org/GHC/Using_the_FFI

Simon

| -----Original Message-----
| From: Francesco Mazzoli [mailto:f at mazzo.li<mailto:f at mazzo.li>]
| Sent: 12 February 2015 09:00
| To: Simon Peyton Jones
| Cc: Michael Sloan; Manuel Chakravarty; Geoffrey Mainland
| (mainland at cs.drexel.edu<mailto:mainland at cs.drexel.edu>); ghc-devs at haskell.org<mailto:ghc-devs at haskell.org>; haskell
| Subject: Re: [Haskell-cafe] Anonymous FFI calls
|
| Thanks to everyone who replied!
|
| It seems like that through a combination of facilities like `libffi'
| and `addTopDecls' I can do everything that I wanted to do.
|
| I still want to take a shot at implementing anonymous FFI calls, since
| IMHO I think they are a very small but useful addition to the
| language.
|
| Francesco
|
| On 12 February 2015 at 09:29, Simon Peyton Jones <simonpj at microsoft.com<mailto:simonpj at microsoft.com>>
| wrote:
| > |  Also, I meant to say that addTopDecls is only exported by
| > |  "Language.Haskell.TH.Syntax".  While this is a digression, there are
| a
| > |  few other handy functions that are oddly left out of
| > |  "Language.Haskell.TH<http://Language.Haskell.TH>": addDependentFile, addModFinalizer, and
| possibly
| > |  more.
| >
| > That does seem wrong.  Do make a patch!
| >
| > SIMon
| >
| > |
| > |  -Michael
| > |
| > |  On Wed, Feb 11, 2015 at 3:25 PM, Simon Peyton Jones
| > |  <simonpj at microsoft.com<mailto:simonpj at microsoft.com>> wrote:
| > |  > I would LOVE someone to improve the documentation for addTopDecls.
| > |  Manuel Chakravarty and Geoff Mainland were responsible for the
| > |  implementation.
| > |  >
| > |  > Simon
| > |  >
| > |  > | -----Original Message-----
| > |  > | From: ghc-devs [mailto:ghc-devs-bounces at haskell.org<mailto:ghc-devs-bounces at haskell.org>] On Behalf
| Of
| > |  > | Michael Sloan
| > |  > | Sent: 11 February 2015 23:19
| > |  > | To: Francesco Mazzoli
| > |  > | Cc: ghc-devs at haskell.org<mailto:ghc-devs at haskell.org>; haskell
| > |  > | Subject: Re: [Haskell-cafe] Anonymous FFI calls
| > |  > |
| > |  > | It seems like addTopDecls[1] will able to help here.
| > |  Unfortunately,
| > |  > | the function is not well documented and not very discoverable
| > |  > | because it's only exported by Language.Haskell.TH<http://Language.Haskell.TH>.
| > |  > |
| > |  > | The documentation doesn't mention that it can only be used to
| > |  create
| > |  > | new top level functions and FFI imports[2].  I think that adding
| > |  FFI
| > |  > | imports was the main motivation for implementing it.   In the
| past
| > |  > | I've wanted to generate instances via this function, but
| > |  > | unfortunately it's not implemented..
| > |  > |
| > |  > | Hope that helps!
| > |  > | -Michael
| > |  > |
| > |  > | [1] http://hackage.haskell.org/package/template-haskell-
| > |  > | 2.9.0.0/docs/Language-Haskell-TH-Syntax.html#v:addTopDecls<http://2.9.0.0/docs/Language-Haskell-TH-Syntax.html#v:addTopDecls>
| > |  > |
| > |  > | [2]
| > |  > |
| > |  https://github.com/ghc/ghc/blob/1d982ba10f590828b78eba992e73315dee33
| > |  > | f78a/
| > |  > | compiler/typecheck/TcSplice.hs#L818
| > |  > |
| > |  > | On Wed, Feb 11, 2015 at 2:26 AM, Francesco Mazzoli <f at mazzo.li<mailto:f at mazzo.li>>
| > |  wrote:
| > |  > | > Hi,
| > |  > | >
| > |  > | > I am in a situation where it would be very useful to call C
| > |  > | > functions without an explicit FFI import.  For example, I'd
| like
| > |  > | > to be able to do
| > |  > | >
| > |  > | >     (foreign import ccall "cadd" :: CInt -> CInt -> CInt) 1 2
| > |  > | >
| > |  > | > instead of declaring the foreign import explicitely at the top
| > |  level.
| > |  > | >
| > |  > | > Is there a way to do this or to achieve similar results in
| some
| > |  > | > other way?
| > |  > | >
| > |  > | > If not, I imagine it would be easy to implement such a
| facility
| > |  in
| > |  > | > GHC, given that the code implementing calling to C functions
| > |  must
| > |  > | > already be present to implement "proper" FFI imports.  I think
| > |  > | > such an addition would be useful in many cases.
| > |  > | >
| > |  > | > Thanks,
| > |  > | > Francesco
| > |  > | > _______________________________________________
| > |  > | > Haskell-Cafe mailing list
| > |  > | > Haskell-Cafe at haskell.org<mailto:Haskell-Cafe at haskell.org>
| > |  > | > http://www.haskell.org/mailman/listinfo/haskell-cafe
| > |  > | _______________________________________________
| > |  > | ghc-devs mailing list
| > |  > | ghc-devs at haskell.org<mailto:ghc-devs at haskell.org>
| > |  > | http://www.haskell.org/mailman/listinfo/ghc-devs
_______________________________________________
ghc-devs mailing list
ghc-devs at haskell.org<mailto:ghc-devs at haskell.org>
http://www.haskell.org/mailman/listinfo/ghc-devs

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/ghc-devs/attachments/20150213/39926952/attachment-0001.html>