question about GHC API on GHC plugin

Mike Izbicki mike at izbicki.me
Fri Aug 21 23:57:28 UTC 2015


Ahh... I get it now!  Thanks for your patience :)

I have a new question: I'm working on supporting literals now.  I'm
having trouble creating something that looks like `(App (Var F#) (Lit
1.0))` because I don't know how to create a variable that corresponds
to the `F#` constructor.  The mkWiredInName function looks promising,
but overly complicated.  Is this the correct function?  If so, what do
I pass in for the Module, Unique, TyThing, and BuiltInSyntax
parameters?

On Thu, Aug 20, 2015 at 5:55 PM, Andrew Farmer <afarmer at ittc.ku.edu> wrote:
> The `buildDictionary` function takes a Var with a dictionary type, and
> builds the expression which implements that dictionary.
>
> For instance, you might create a new Var:
>
> x :: Num Float
>
> and pass that to buildDictionary. It will return:
>
> (x, [NonRec x $fNumFloat])
>
> which you could blindly turn into:
>
> let x = $fNumFloat
> in x
>
> or you could do what buildDictionaryT (a bit further down in the same
> module), and spot that case and just return $fNumFloat directly. (The
> list can have more than one element in the case that dictionaries are
> built in terms of other dictionaries.)
>
> Thus, you've built a dictionary expression of type Num Float.
>
> As I understand it, you want to pass something 'log' and get back the
> dictionary argument. You'll need to choose a type (like Float), but
> once that is done, it should be easy to use buildDictionary to build
> the dictionary arguments... just take apart the type of 'log @ Float',
> make a new Var with the argument type, build a dictionary expression,
> and apply it.
>
> On Thu, Aug 20, 2015 at 5:05 PM, Mike Izbicki <mike at izbicki.me> wrote:
>> I'm pretty sure the `buildDictionary` function doesn't do what I need.
>> AFAICT, you pass it a `Var` which contains a dictionary, and it tells
>> you what is in that dictionary.  What I need is a function with type
>> `Var -> Var` where the first `Var` contains a function, and the output
>> `Var` is the dictionary.
>>
>> For example, given the expression:
>>
>>     log (a1+a2)
>>
>> In core, this might look like:
>>
>>     log @ Float $fFloatingFloat (+ @ Float $fNumFloat a1 a2)
>>
>> I want to mechanically construct the core code above.  When doing so,
>> each function within a type class has an extra argument, which is the
>> dictionary for that type class.  `log` no longer takes one parameter;
>> in core, it takes two.  I'm having trouble figuring out how to get the
>> appropriate dictionary to pass as the "dictionary parameter" to these
>> functions.
>>
>> On Mon, Aug 17, 2015 at 4:21 PM, Andrew Farmer <afarmer at ittc.ku.edu> wrote:
>>> HERMIT has some code for building dictionaries for a given predicate
>>> type (by invoking the typechecker functions that do this):
>>>
>>> https://github.com/ku-fpg/hermit/blob/master/src/HERMIT/Dictionary/GHC.hs#L223
>>>
>>> The functions to run TcM computations inside CoreM are here:
>>>
>>> https://github.com/ku-fpg/hermit/blob/master/src/HERMIT/Monad.hs#L242
>>> and
>>> https://github.com/ku-fpg/hermit/blob/master/src/HERMIT/GHC/Typechecker.hs#L47
>>>
>>> Perhaps that will help get you started?
>>>
>>> I would like to push these interfaces back into the GHC API at some
>>> point, but just haven't done it yet.
>>>
>>> HTH
>>> Andrew
>>>
>>> On Mon, Aug 17, 2015 at 4:12 PM, Mike Izbicki <mike at izbicki.me> wrote:
>>>> I'm not sure how either of those two functions can help me.  The
>>>> problem is that given an operator (e.g. `+`), I don't know the name of
>>>> the dictionary that needs to be passed in as the first argument to the
>>>> operator.  I could probably hard code these names, but then the plugin
>>>> wouldn't be able to work with alternative preludes.
>>>>
>>>> On Fri, Aug 7, 2015 at 11:20 PM, Edward Z. Yang <ezyang at mit.edu> wrote:
>>>>> Hello Mike,
>>>>>
>>>>> Give importDecl from LoadIface a try, or maybe tcLookupGlobal if
>>>>> you're in TcM.
>>>>>
>>>>> Edward
>>>>>
>>>>> Excerpts from Mike Izbicki's message of 2015-08-07 15:40:30 -0700:
>>>>>> I'm trying to write a GHC plugin.  The purpose of the plugin is to
>>>>>> provide Haskell bindings to Herbie. Herbie
>>>>>> (https://github.com/uwplse/herbie) is a program that takes a
>>>>>> mathematical statement as input, and gives you a numerically stable
>>>>>> formula to compute it as output.  The plugin is supposed to automate
>>>>>> this process for Haskell programs.
>>>>>>
>>>>>> I can convert the core expressions into a format for Herbie just fine.
>>>>>> Where I'm having trouble is converting the output from Herbie back
>>>>>> into core.  Given a string that represents a numeric operator (e.g.
>>>>>> "log" or "+"), I can get that converted into a Name that matches the
>>>>>> Name of the version of that operator in scope at the location.  But in
>>>>>> order to create an Expr, I need to convert the Name into a Var.  All
>>>>>> the functions that I can find for this (e.g. mkGlobalVar) also require
>>>>>> the type of the variable.  But I can't find a way to figure out the
>>>>>> Type given a Name.  How can I do this?
>>>> _______________________________________________
>>>> ghc-devs mailing list
>>>> ghc-devs at haskell.org
>>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs
>>>>
>>


More information about the ghc-devs mailing list