<div dir="ltr"><div>Richard, I just watched your talk about simplifying the constraint solver: <a href="https://www.youtube.com/watch?v=flwz6V5Ps8w">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><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><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><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><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">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>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><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>If that's the case, how do you envision plugin writers interacting with the solver?</div><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><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">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 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>