Properly writing typechecker plugins

Simon Peyton Jones simonpj at microsoft.com
Fri Aug 2 07:57:55 UTC 2019


. I went looking in the GHC source, only to discover that you, Simon, are apparently deeply suspicious<https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgitlab.haskell.org%2Fghc%2Fghc%2Fblob%2F986643cb3506f2eedce96bf2d2c03873f105fad5%2Fcompiler%2Ftypecheck%2FTcInteract.hs%23L276-277&data=02%7C01%7Csimonpj%40microsoft.com%7C497b4eadac5048bba19f08d717023589%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637003170572848664&sdata=3FxV6%2FaJ8zhkcyRMbZwFoqBOzFmvHwLYY%2FsYUX7hh08%3D&reserved=0> of typechecker plugins that solve derived constraints!

What a good thing I left _some_ breadcrumbs to follow!

I think my suspicion were about two separate matters

  *   We should not need to delete solved _givens_ from the inert set.  We can augment givens with extra facts, but deleting them seems wrong.
  *   There should be no Derived constraints in the inert set anyway.  They should all be in the WantedConstraints passed to runTcPluginsWanted.    They were extracted from the inert set, along with the Deriveds, by getUnsolvedInerts in solve_simple_wanteds
I can’t account for what’s happening to your Deriveds, but if you do some more detective work, I’m happy to play consultant.

Though, for what it’s worth, I think it would be even more helpful if some of the relevant information made its way into the GHC user’s guide, since I found that more discoverable than the wiki page (which took a comparatively large amount of digging to locate).

By definition you are better placed that any of us to know where to put this info -- after all, you know where you looked!   It would be a great service to everyone if you wrote a new chapter for the user guide, drawing on material that already exists -- and then signposted that new chapter from the existing wiki page.

Adding signposts to the wiki page too would be helpful, since it was hard to find.

Thanks!

Simion

From: Alexis King <lexi.lambda at gmail.com>
Sent: 02 August 2019 05:31
To: Simon Peyton Jones <simonpj at microsoft.com>
Cc: ghc-devs at haskell.org
Subject: Re: Properly writing typechecker plugins

My thanks to you, Simon, for your prompt response. After reading your message, I realized I perhaps read too far into this sentence on the wiki page:

Defining type families in plugins is more work than it needs to be, because the current interface forces the plugin to search the unsolved constraints for the type family in question (which might be anywhere within the types), then emit a new given constraint to reduce the type family.

Emphasis mine. Although that sentence seems to imply that adding given constraints from a typechecker plugin is both possible and sanctioned, your message, my experimentation, and the GHC source code all seem to agree that is not true, after all. After realizing that was a dead end, I puzzled for some time on how one is intended to solve nested type families after all, only to realize that a proper solution is simpler than I had realized.

To make things more concrete, I was confused about how my plugin might solve a constraint like

[W] {co_0} :: F (ToLower "Foo") ~# Length "bar"

if it knows about ToLower and Length but nothing about F. The solution is actually extremely straightforward to implement, but the idea was not at all obvious to me at first. Namely, it’s possible to just recursively walk the whole type and solve any known families bottom-up, producing a new constraint:

[W] {co_1} :: F "foo" ~# 3

At the same time, to construct evidence for the first constraint, the recursive function can also build a coercion as it goes, producing

co_2 = (F (Univ :: "foo" ~ ToLower "Foo")) ~# (Univ :: 3 ~ Length "bar")

which can be used to cast the evidence for the new constraint to evidence for the old one:

co_0 := co_2 ; {co_1}

That all seems to be working well, and it’s much nicer than whatever it was I was trying before. There’s one small wrinkle I’ve bumped into, however, which was preventing the solver from terminating. Specifically, typechecker plugins do not seem to be able to solve derived constraints.

To elaborate, my plugin was getting called with the constraint [D] (Length "f" ~ 1), which it was happily turning into [D] (1 ~ 1), but the derived constraint was never removed from the inert set, and it would produce new [D] (1 ~ 1) constraints forever. I went looking in the GHC source, only to discover that you, Simon, are apparently deeply suspicious<https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgitlab.haskell.org%2Fghc%2Fghc%2Fblob%2F986643cb3506f2eedce96bf2d2c03873f105fad5%2Fcompiler%2Ftypecheck%2FTcInteract.hs%23L276-277&data=02%7C01%7Csimonpj%40microsoft.com%7C497b4eadac5048bba19f08d717023589%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637003170572848664&sdata=3FxV6%2FaJ8zhkcyRMbZwFoqBOzFmvHwLYY%2FsYUX7hh08%3D&reserved=0> of typechecker plugins that solve derived constraints! I don’t know if there’s a reason behind that—maybe I’m going about things the wrong way, and I shouldn’t need to solve those derived constraints—but in the meantime, I added some kludgey mutable state to keep track of the derived constraints my plugin has already attempted to solve so it won’t try to solve them again.

Anyway, that aside, if anyone finds what I’ve written in this email helpful, I can certainly flesh it out a bit more and stick it in a wiki page somewhere. Though, for what it’s worth, I think it would be even more helpful if some of the relevant information made its way into the GHC user’s guide, since I found that more discoverable than the wiki page (which took a comparatively large amount of digging to locate).

Thanks again,
Alexis


On Aug 1, 2019, at 03:01, Simon Peyton Jones <simonpj at microsoft.com<mailto:simonpj at microsoft.com>> wrote:

Alexis

Thanks for writing this up so carefully.  I hope that others will join in.   And please then put the distilled thought onto the wiki page(s) so they are not lost.

Some quick thoughts from me:


  *   Flattening.  I’m pretty sure we pass constraints unflattened because that’s what someone wanted at  the time.  It could easily be changed, but it might complicate the API.  E.g. you might reasonably want to know the mapping from type variable to function application. There is no fundameental obstacle.


  *   Letting the plugin add given constraints.  This looks a bit like: let the plugin prove lemmas and hand them back to GHC (along with their proof) to exploit. Yes, that seems reasonable too.  Again, something new in the API.

I don’t understand enough of your type-class instance question to comment meaningfully, but perhaps others will.

Nothing about the plugin interface is cast in stone.  There are quite a few “customers” but few enough that they’ll probably be happy to adapt to changes.   Go for it, in consultation with them!

Simon



From: ghc-devs <ghc-devs-bounces at haskell.org<mailto:ghc-devs-bounces at haskell.org>> On Behalf Of Alexis King
Sent: 01 August 2019 07:55
To: ghc-devs at haskell.org<mailto:ghc-devs at haskell.org>
Subject: Properly writing typechecker plugins

Hi all,

I have recently decided to try writing a GHC typechecker plugin so I can get my hands on some extra operations on type-level strings. My plugin works, but only sort of—I know some things about it are plain wrong, and I have a sneaking suspicion that plenty of other things are not handled properly.

First, some context about what I do and don’t already know: I have a high-level understanding of the basic concepts behind GHC’s solver. I understand what evidence is and what purpose it serves, I mostly understand the different flavors of constraints, and I think I have a decent grasp on some of the operational details of how individual passes work. I’ve spent a little time reading through comments in the GHC source code, along with pieces of the source code itself, but I’m sure my understanding is pretty patchy.

With that out of the way, here are my questions:


  1.  First, I’m trying to understand: why are wanted constraints passed to typechecker plugins unflattened? This is my single biggest point of confusion. It certainly seems like the opposite of what I want. Consider that I have a type family

    type family ToUpper (s :: Symbol) :: Symbol where {}

that I wish to solve in my plugin. At first, I just naïvely looked through the bag of wanted constraints and looked for constraints of the shape

    t ~ ToUpper s

but this isn’t good enough, since my plugin regularly receives constraints that look more like

    t ~ SomeOtherTypeFamily (ToUpper s)

so I have to recursively grovel through every type equality constraint looking for an application of a family I care about. Furthermore, once I’ve found one, I’m not sure how to actually let GHC know that I’ve solved it—do I really have to just generate a new given constraint and let GHC’s solver connect the dots?

I have seen the note on the typechecker plugins wiki page<https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgitlab.haskell.org%2Fghc%2Fghc%2Fwikis%2Fplugins%2Ftype-checker%23under-discussion-defining-type-families&data=02%7C01%7Csimonpj%40microsoft.com%7C497b4eadac5048bba19f08d717023589%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637003170572858662&sdata=Ap5D%2BqsSiCU4v3YBFy%2BXiwYXqIQEj9oB7GQSypgZRXk%3D&reserved=0> about possibly baking support for type families into the plugin interface, which would indeed be nicer than the status quo, but it seems odd to me that they aren’t just passed to plugins flattened, which seems like it would spare a lot of effort. Isn’t the flattened representation really what typechecker plugins would like to see, anyway?
  2.  But let’s put families aside for a moment. I’m not just solving type families in my plugin, I’m also solving classes. These classes have no methods, but they do have functional dependencies. For example, I have a class

    class Append (a :: Symbol) (b :: Symbol) (c :: Symbol) | a b -> c, a c -> b, b c -> a

which is like GHC.TypeLits.AppendSymbol, but the fundeps make GHC a bit happier when running it “backwards” (since GHC doesn’t really know about AppendSymbol’s magical injectivity, so it sometimes complains).

In any case, I was hoping that GHC’s solver would handle the improvement afforded by the fundeps for me once I provided evidence for Append constraints, but that doesn’t seem to be the case. Currently, I am therefore manually generating derived constraints based on the functional dependency information, plumbing FunDepOrigin2 through and all. Is there some way to cooperate better with GHC’s solver so I don’t have to duplicate all that logic in my plugin?

I guess one thing I didn’t try is returning given constraints from my solver instead of just solving them and providing evidence. That is, if my plugin received a

    [W] d1 :: Append "foo" "bar" c

constraint, then instead of solving the constraint directly, I could leave it alone and instead return a new constraint

    [G] d2 :: Append "foo" "bar" "baz"

and presumably GHC’s solver would use that constraint to improve and solve d1. But similar to my confusion about type families above, I’m uncertain if that’s the intended method or not, since it seems like it’s sort of circumventing the plugin API.
  3.  Finally, on a related note, building evidence for these solver-generated typeclass instances is a bit of a pain. They have no methods, but they do sometimes have superclasses. Currently, I’ve been generating CoreExprs as carefully as I’m able to after reading through the desugaring code: I call dataConWrapId on the result of classDataCon, then use mkTyConApp and mkCoreApps on that directly. It seems to work okay now, but it didn’t always: -dcore-lint thankfully caught my mistakes, but I’ve been wondering if there’s a safer way to build the dictionary that I’ve been missing.

That’s it for now—I’ve just been muddling through until things work. Once I get something that feels closer to right, maybe I’ll put the code somewhere and ask for more low-level comments if anyone would like to take the time to offer them, but for now, I’m still working on the high-level ideas. The wiki pages I’ve found have been very helpful; my appreciation to all who have contributed to them!

Many thanks,
Alexis

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-devs/attachments/20190802/180f51a8/attachment.html>


More information about the ghc-devs mailing list