[Haskell-cafe] Anonymous FFI calls
Michael Sloan
mgsloan at gmail.com
Thu Feb 12 03:13:21 UTC 2015
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
>
More information about the ghc-devs
mailing list