<div dir="ltr"><div>Sorry for my mistakes. It is as you said, I changed the example, but didn't properly follow through in the rest of the email.</div><div><br></div><div>I'm glad I've understood the intended interaction with the constraint solver correctly then.</div><div>(With regard to the coercion... I usually just end up using UnivCo...)<br></div><div><br></div><div>It seems like a function like `rewritePlugin` is something we could/should just build on top of the existing plugin mechanism, and have something along the line of:<br></div><div><br></div><div>> -- | Create a 'TcPluginSolver' from the provided rewrite function<br></div><div>> rewritePluginSolver ::</div><div>> (s -> TyCon -> [TcType] -> TcPluginM (Maybe TcType)) -> <br></div><div>> (s ->
<a id="gmail-t:TcPluginSolver" class="gmail-def">TcPluginSolver)</a>
</div><div><br></div><div>where `rewritePlugin` just traverses constraints with the given rewrite and creates the evidence, emits new wanteds, etc.</div><div>(We could develop such a function outside of the `ghc` package, and only once we have a satisfactory solution, perhaps consider it for inclusion in the `ghc` package.)</div><div>This way, we won't have to make the common case of simplifying type families slow, and still provide a straightforward API.<br></div><div><br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, 30 Nov 2020 at 20:03, Richard Eisenberg <<a href="mailto:rae@richarde.dev">rae@richarde.dev</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="overflow-wrap: break-word;"><br><div><br><blockquote type="cite"><div>On Nov 30, 2020, at 11:41 AM, Christiaan Baaij <<a href="mailto:christiaan.baaij@gmail.com" target="_blank">christiaan.baaij@gmail.com</a>> wrote:</div><br><div><div dir="ltr"><div>Richard, I just watched your talk about simplifying the constraint solver: <a href="https://www.youtube.com/watch?v=flwz6V5Ps8w" target="_blank">https://www.youtube.com/watch?v=flwz6V5Ps8w</a></div><div>and then realised that no flattening at all means also no flattening of [W]anteds.</div></div></div></blockquote><div><br></div><div>Yes, that's true.</div><br><blockquote type="cite"><div><div dir="ltr"><div><br></div><div>Now, let's say I have a type family called "Magic" for which I want to write a constraint solver plugin; and some type family "Foo" and "Bar"<br></div><div>And then lets say we start out with an original [W]anted: Foo (Magic 8) ~ Bar 42</div><div><br></div><div>Then if I understand flattening correctly, that would get flattened to:</div><div>[w] Magic 8 ~ fmv1</div><div>[w] fmv1 ~ 42</div><div>[w] Foo fmv1 ~ fmv2</div><div>[w] fmv2 ~ Bar fmv1</div><div><br></div><div>Correct?</div></div></div></blockquote><div><br></div><div>I get</div><div><br></div><div>[W] Magic 8 ~ fmv1</div><div>[W] Foo fmv1 ~ fmv2</div><div>[W] Bar 42 ~ fmv3</div><div>[W] fmv2 ~ fmv3</div><div><br></div><blockquote type="cite"><div><div dir="ltr"><div><br></div><div>So currently, with flattened wanted, all a constraint solver plugin has to do is look for [w]anted where the head of the type family application is "Magic", [w] Magic 8 ~ fmv1.</div><div>Then perhaps do some substitution for fmv1 to get back [w] Magic 8 ~ 42, use its internal solving rules to reduce Magic 8 to 42, and conclude what "[w] 42 ~ 42" can be solved by reflexivity.</div></div></div></blockquote><div><br></div><div>It looks like maybe you edited your example afterwards? This doesn't quite line up, because we don't have [W] Magic 8 ~ 42 anywhere. But I see your point, in that flattening brings the type family out to the top level.</div><br><blockquote type="cite"><div><div dir="ltr"><div><br></div><div>But in a version without flattening, all we would see is:</div><div>[W]
Foo (Magic 8) ~ Foo 42 <br></div></div></div></blockquote><div><br></div><div>Did you mean [W] Foo (Magic 8) ~ Bar 42? Then, yes.</div><br><blockquote type="cite"><div><div dir="ltr"><div><br></div><div>And all I can do is to traverse the entire [W]anted, look for type family applications of "Magic" an then improve to:</div><div>[W] Foo 42 ~ Bar 42</div><div><br></div><div>Now... I can't declare that [W]anted solved, because i have no idea what Foo and Bar do (they might also be type families that can only be solved through constraint solver plugins).</div><div>Currently, the API for constraint solving plugins allows me to return _either_: <a href="http://hackage.haskell.org/package/ghc-8.10.1/docs/TcRnTypes.html#t:TcPluginResult" target="_blank">http://hackage.haskell.org/package/ghc-8.10.1/docs/TcRnTypes.html#t:TcPluginResult</a></div><div>A) A set of contradictions</div><div>B) A set of solved [W] constraints + A set of new [W] constraints</div><div><br></div><div>With the above API, the only way to move forward is to: 1) declare "[w] Foo (Magic 8) ~ Bar 42" solved, while at the same time emitting a new wanted "[w] Foo 42 ~ Bar 42"</div></div></div></blockquote><div><br></div><div>Yes, that's what you would do. Indeed, it's what GHC does whenever simplifying part of a Wanted.</div><br><blockquote type="cite"><div><div dir="ltr"><div>But it's basically the same wanted... so I would like a function of type "updateWantedType :: Ct -> TcPredType -> Ct" (which I don't think exists in the current GHC API)</div></div></div></blockquote><div><br></div><div>This would not be hard to write, though you'd also need evidence relating the old type and the new one. Here's an attempt (that I have not tested, beyond the ability to compile it):</div><div><br></div><div><div></div><blockquote type="cite"><div>import GHC.Plugins</div><div>import GHC.Tc.Utils.TcType</div><div>import GHC.Tc.Types.Evidence</div><div>import GHC.Tc.Types.Constraint</div><div>import GHC.Tc.Plugin</div><div><br></div><div>updateWantedType :: Ct -- initial constraint; always unsolved equality wanted</div><div> -- t1 ~N t2</div><div> -> TcPredType -- desired new constraint: s1 ~N s2</div><div> -> CoercionN -- lhs_co :: t1 ~N s1</div><div> -> CoercionN -- rhs_co :: t2 ~N s2</div><div> -> TcPluginM (Ct, EvTerm) -- new constraint, evidence for *old* constraint</div><div>-- NB: the *old* constraint is solved by the returned EvTerm;</div><div>-- return the EvTerm with the *old* constraint in TcPluginOk</div><div>updateWantedType old_ct new_pred lhs_co rhs_co = do</div><div> new_w <- newWanted (ctLoc old_ct) new_pred</div><div> let new_co = ctEvCoercion new_w -- :: s1 ~N s2</div><div> old_co = lhs_co `mkTransCo` new_co `mkTransCo` mkSymCo rhs_co</div><div> old_ev = evCoercion old_co</div><div> return (mkNonCanonical new_w, old_ev)</div></blockquote><div><br></div></div><div>That would seem to do the trick. This function is essentially extracted from GHC.Tc.Solver.Canonical.rewriteEqEvidence, and GHC does it many many times while working with equalities.</div><br><blockquote type="cite"><div><div dir="ltr"><div><br></div><div>But you might think the above way of working with the constraint solver as a plugin write, where we "solve" a wanted, only to emit an "improved" wanted, is _not_ the way things should work.</div></div></div></blockquote><div><br></div><div>I don't quite know how to interpret this statement. This way of working is commonplace. For example, it's what we do when trying to prove a goal by hand when working backwards: we simplify the goal Wanted by rewriting it to another (presumably simpler) Wanted -- as long as the new Wanted can prove the old one.</div><br><blockquote type="cite"><div><div dir="ltr"><div>If that's the case, how do you envision plugin writers interacting with the solver?</div></div></div></blockquote><div><br></div><div>As you've outlined above.</div><br><blockquote type="cite"><div><div dir="ltr"><div>Note that the above way of interacting with the constraint solver, "solving" + emitting "improved", is something I already sorta do, but in a different context:</div><div>In "ghc-typelits-natnormalise", when asked to solve [w] "a + Foo b ~ Foo b + c", I solve it, but then immediately emitting "[w] a ~ c".<br></div></div></div></blockquote><div><br></div><div>And that's really the way to do it.</div><div><br></div><div>A slightly different problem than the one you've outlined here is that generating the coercions to pass to updatedWantedType might be challenging. So I wonder about a new kind of plugin, called a "rewriter plugin", that looks like</div><div><br></div><div>> rewriterPlugin :: TyCon -> [TcType] -> TcPluginM (Maybe TcType)</div><div><br></div><div>possibly with some other state passed in (including other constraints, if there is demand). The tycon would always be a type family, and the types would always exactly saturate the type family. Then, the plugin could reduce the type family if it knows how to do so. This seems to be a fairly simple interface and would be very, very easy to insert into the new infrastructure. (I'm a little worried about performance, because simplifying type families is code that's hammered on. But one step at a time.) Would that satisfy your need?</div><div><br></div><div>Richard</div><br><blockquote type="cite"><div><div dir="ltr"><div><br></div><div>-- Christiaan<br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, 20 Nov 2020 at 20:43, Richard Eisenberg <<a href="mailto:rae@richarde.dev" target="_blank">rae@richarde.dev</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><br><div><br><blockquote type="cite"><div>On Nov 19, 2020, at 12:40 PM, Iavor Diatchki <<a href="mailto:iavor.diatchki@gmail.com" target="_blank">iavor.diatchki@gmail.com</a>> wrote:</div><br><div><div dir="ltr">Hello,<div><br></div><div>I haven't worked on that stuff in Haskell for a long time, but here are some thoughts:</div><div> - I think plugins should generally be agnostic to the form of the constraints they are given, thus flattening or not should not affect them---after all, the user might have written the constraints in the "flattened" form in the first place. So I think a plugin needs to convert the constraints to whatever form it assumes anyways.</div><div> - I always thought that derived constraints were a pretty clever way for disseminating information in the constraint solver, is there a note somewhere on what's going to be the new mechanism?</div></div></div></blockquote><div><br></div><div>This Note (<a href="https://gitlab.haskell.org/ghc/ghc/-/blob/wip/derived-refactor/compiler/GHC/Tc/Types/Constraint.hs#L1678" target="_blank">https://gitlab.haskell.org/ghc/ghc/-/blob/wip/derived-refactor/compiler/GHC/Tc/Types/Constraint.hs#L1678</a>) is the best I can offer, though it may be out of date. In the final version of the patch, that Note will naturally be updated. Essentially, the new plan is just to use Wanteds instead of Deriveds, which will achieve the same goal (but do not need to be distinct from Deriveds).</div><div><br></div><div>Richard</div><br><blockquote type="cite"><div><div dir="ltr"><div><br></div><div>-Iavor</div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Nov 19, 2020 at 2:21 AM Christiaan Baaij <<a href="mailto:christiaan.baaij@gmail.com" target="_blank">christiaan.baaij@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>I always forget what flattening did/does. Is it the thing where it turns a "complex" type family application:<br><br></div><div>F (G y) (H z) ~ a<br></div><div><br></div><div>into:<br><br></div><div>G y ~ p</div><div>H z ~ q</div><div>F p q ~ a</div><div><br></div><div>?</div><div><br></div><div>If so, then I'm all for removing that. Since I actually wrote/hacked a function that "reverts" that process (for [G]ivens only): <a href="https://hackage.haskell.org/package/ghc-tcplugins-extra-0.4/docs/src/GHC.TcPluginM.Extra.html#flattenGivens" target="_blank">https://hackage.haskell.org/package/ghc-tcplugins-extra-0.4/docs/src/GHC.TcPluginM.Extra.html#flattenGivens</a><br></div><div>Which I use in all of my plugins. (PS it should perhaps be called "unflattenGiven"? like I said, I always get confused about flatten vs unflatten).</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, 19 Nov 2020 at 05:20, Richard Eisenberg <<a href="mailto:rae@richarde.dev" target="_blank">rae@richarde.dev</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi all,<br>
<br>
I'm hard at work on two significant refactorings within GHC's constraint solver. The first is at <a href="https://gitlab.haskell.org/ghc/ghc/-/merge_requests/4149" rel="noreferrer" target="_blank">https://gitlab.haskell.org/ghc/ghc/-/merge_requests/4149</a>. It removes flattening meta-variables and flattening skolems. This is a very nice simplification. Instead, it just reduces type families directly. My other patch (held up by the first) is at <a href="https://gitlab.haskell.org/ghc/ghc/-/tree/wip/derived-refactor" rel="noreferrer" target="_blank">https://gitlab.haskell.org/ghc/ghc/-/tree/wip/derived-refactor</a> and will remove Derived constraints, to be replaced by a little bit of cleverness in suppressing certain confusing error messages. My guess is that either or both of these will invalidate the current behavior of type-checker plugins. Sadly, this is not just a case of finding a new function that replicates the old behavior -- enough is changing under the hood that you might actually have to rewrite chunks of your code.<br>
<br>
I have never written a type-checker plugin, and so I don't currently have advice for you. But if you are a plugin author affected by this change and want help, please reach out -- I would be happy to walk you through the changes, and then hopefully make a little video explaining the process to other plugin authors.<br>
<br>
Neither patch will make it for 9.0, but I expect both to be in 9.2. There may be more where this came from (<a href="https://gitlab.haskell.org/ghc/ghc/-/issues/18965" rel="noreferrer" target="_blank">https://gitlab.haskell.org/ghc/ghc/-/issues/18965</a>) in the future, but it's all for a good cause.<br>
<br>
(I have bcc'd plugin authors that I'm aware of. Just adding this in case you're surprised at receiving this email.)<br>
<br>
Thanks,<br>
Richard</blockquote></div>
_______________________________________________<br>
ghc-devs mailing list<br>
<a href="mailto:ghc-devs@haskell.org" target="_blank">ghc-devs@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs</a><br>
</blockquote></div>
</div></blockquote></div><br></div></blockquote></div>
</div></blockquote></div><br></div></blockquote></div>