<html><head><style type="text/css"><!--#xc0554ee3f63b46f blockquote.cite2
{margin-left: 5px; margin-right: 0px; padding-left: 10px; padding-right: 0px; border-left: 1px solid rgb(204, 204, 204); margin-top: 3px; padding-top: 0px;}
#xc0554ee3f63b46f
{font-family: "Segoe UI"; font-size: 12pt;}
--></style><style id="css_styles" type="text/css"><!--blockquote.cite { margin-left: 5px; margin-right: 0px; padding-left: 10px; padding-right:0px; border-left: 1px solid #cccccc }
blockquote.cite2 {margin-left: 5px; margin-right: 0px; padding-left: 10px; padding-right:0px; border-left: 1px solid #cccccc; margin-top: 3px; padding-top: 0px; }
a img { border: 0px; }
table { border-collapse: collapse; }
li[style='text-align: center;'], li[style='text-align: center; '], li[style='text-align: right;'], li[style='text-align: right; '] { list-style-position: inside;}
body { font-family: 'Segoe UI'; font-size: 12pt; }
.quote { margin-left: 1em; margin-right: 1em; border-left: 5px #ebebeb solid; padding-left: 0.3em; }
--></style></head><body><div>Hi Ryan and devs,</div><div><br /></div><div>I opened <a href="https://gitlab.haskell.org/ghc/ghc/-/merge_requests/12941" style="font-size: 12pt;">https://gitlab.haskell.org/ghc/ghc/-/merge_requests/12941</a> with my progress, which seems to work for reasonable examples.</div><div>No documentation yet and almost no test coverage, though.</div><div><br /></div><div>Cheers,</div><div>Sebastian</div>
<div style="clear:both"><br /></div>
<div><br /></div>
<div>
<div>------ Originalnachricht ------</div>
<div>Von "Sebastian Graf" <<a href="mailto:sgraf1337@gmail.com">sgraf1337@gmail.com</a>></div>
<div>An "Ryan Scott" <<a href="mailto:ryan.gl.scott@gmail.com">ryan.gl.scott@gmail.com</a>></div>
<div>Cc "GHC developers" <<a href="mailto:ghc-devs@haskell.org">ghc-devs@haskell.org</a>></div>
<div>Datum 19.06.2024 17:21:33</div>
<div>Betreff Re[2]: Deriving via TH (#12457)</div></div><div><br /></div>
<div id="xc0554ee3f63b46f"><blockquote cite="eme03bc1c5-722a-4be6-9a4f-9d2a8c6672e5@b7de5836.com" type="cite" class="cite2">
<div>Hi Ryan,</div><div><br /></div><div>Thank you for such a detailed reply!</div><div>What you wrote gave me confidence in just duplicating code from `tcClsInstDecl`.<br />The code is a bit of mess now because the tyvars chosen for the `deriving` instance head might not match with the tyvars used returned in the splice, but I think we can sort this out during review once we have a running prototype.</div><div><br /></div>
<div style="clear:both">Sebastian</div>
<div><br /></div>
<div>
<div>------ Originalnachricht ------</div>
<div>Von "Ryan Scott" <<a href="mailto:ryan.gl.scott@gmail.com">ryan.gl.scott@gmail.com</a>></div>
<div>An "Sebastian Graf" <<a href="mailto:sgraf1337@gmail.com">sgraf1337@gmail.com</a>></div>
<div>Cc "GHC developers" <<a href="mailto:ghc-devs@haskell.org">ghc-devs@haskell.org</a>></div>
<div>Datum 19.06.2024 15:07:47</div>
<div>Betreff Re: Deriving via TH (#12457)</div></div><div><br /></div>
<div id="xf9635e0fa2214c6"><blockquote cite="CADdQpRAiHAqMX8JB9RAK_8TcWDm3S+M6xXNz8vK3k8uFg4m6ZQ@mail.gmail.com" type="cite" class="cite2">
<div dir="ltr"><div>Hi Sebastian,<br /></div><div><br /></div><div>This is exciting! I haven't taken a close look at the implementation, but here are my initial reactions:</div><div><ul><li>The implementation of `deriving` proceeds in a somewhat unusual way: you first typecheck the `deriving` clause and use the resulting Core types to figure out what the instance head should be. Then, you generate the method bindings for the instance as parsed code, then proceed to rename and typecheck the bindings. As such, `deriving` goes from typechecking -> renaming -> typechecking, which is somewhat odd...</li><li>...and derived type family instances work in an even more odd way. Rather than generating parsed type family instance declarations (and then renaming/typechecking them), `deriving` directly generates Core axioms for the instances. As such, derived associated type family instances go through a rather different code path than derived method bindings. (There are technical reasons why this is the case, but I won't get into them here.) This asymmetry means that the existing `deriving` machinery has to do some odd things to make everything fit together.<br /></li><li>I think part of the difficulty you're encountering is that the `th` deriving strategy is attempting to produce associated type family instances from TH quotes (i.e., parsed code), rather than from typechecked code. Every other deriving strategy produces its associated type family instances from Core types, however, so they are able to generate instances without renaming or typechecking. You don't have that luxury. As such, I agree that you'll need to reuse other parts of the renamer and typechecker (e.g., the `tcClsInstDecl` function) in order to turn your TH-quoted type family instances into Core axioms.</li><li>
Is it a good idea to integrate this new deriving strategy so tightly with the existing deriving framework? I think it's at least worth trying. The `deriving` code path is <i>just</i> different enough from the code path for ordinary class instances where I think you'll encounter some oddities if you try to implement the `th` deriving strategy out of band. For example, you'll want to be able to dump `th`-derived code that you generate using -ddump-deriv, which currently only happens in the `deriving` code path. I suppose you could change things so that -ddump-deriv does things in multiple places in the code, but I worry that that may lead to an uncomfortable amount of code duplication. (Not to mention that you'll have to be careful to actually emulate everything that the `deriving` code path does, because if you forget something, then that can lead to confusing bugs down the road.)</li><li>Of course, I won't claim that the current design of the `deriving` code path is perfect by any means. If there are ways we could clean things up that would make it easier to implement the `th` deriving strategy, then we should consider doing that.</li></ul><div>I'm not sure if I fully answered the spirit of your question, so feel free to ask follow-up questions with specifics if I missed the mark.<br /></div><div><br /></div><div>Best,</div><div><br /></div><div>Ryan<br /></div></div></div><br /><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Jun 19, 2024 at 4:52 AM Sebastian Graf <<a href="mailto:sgraf1337@gmail.com">sgraf1337@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 class="msg-1438398085486069382">
<div>Hi Ryan and GHC devs,<div><br /></div><div>I'm working on-and-off on a prototype that enables use of TemplateHaskell as a deriving strategy: <a href="https://gitlab.haskell.org/ghc/ghc/-/commit/82aea77ed908fe36bed829c9c4a01ea9b30a0181" style="font-size:12pt">https://gitlab.haskell.org/ghc/ghc/-/commit/82aea77ed908fe36bed829c9c4a01ea9b30a0181</a>.</div><div><span>My current working example is:</span></div><div><br /></div><div>```</div><div>{-# LANGUAGE TemplateHaskell #-}
</div><div>{-# LANGUAGE TypeFamilies #-}
</div><div><br /></div><div>module T12457A where
</div><div><br /></div><div>import GHC.Internal.TH.Lib
</div><div>import GHC.Internal.TH.Syntax
</div><div><br /></div><div>class C a where
</div><div> type Assoc a
</div><div> m :: a -> Int
</div><div> n :: a -> Int
</div><div><br /></div><div>instance DeriveTH C where
</div><div> deriveTH _p head = do
</div><div> let AppT (ConT t) (VarT a) = head
</div><div> x <- newName "x"
</div><div> x2 <- newName "x"
</div><div> addTopDecls =<< [d|
</div><div> $(varP x) = 12
</div><div> $(varP x2) = 23 |]
</div><div> [d|
</div><div> instance C $(varT a) => C ($(conT t) $(varT a)) where
</div><div> type Assoc <span>($(conT t) $(varT a)) </span><span>= Char </span></div><div> m :: Eq a => a -> Int
</div><div> m _ = $(varE x) + 42
</div><div> {-# INLINE m #-}
</div><div> n :: Show a => a -> Int
</div><div> n _ = $(varE x2) + 13 |]
</div><div><br /></div><div>---</div><div><br /></div><div>{-# LANGUAGE TemplateHaskell #-}
</div><div>{-# LANGUAGE ConstraintKinds #-}
</div><div>{-# LANGUAGE DerivingStrategies #-}
</div><div><br /></div><div>module T12457 where
</div><div><br /></div><div>import <a href="http://Language.Haskell.TH">Language.Haskell.TH</a>
</div><div>import GHC.Internal.TH.Lib
</div><div>import T12457A
</div><div><br /></div><div>newtype T a = T [a] deriving th C
</div><div>```</div><div><br /></div><div>I just managed to implement `GHC.Tc.Deriv.Infer.inferConstraints` for this mechanism (still hacky and broken in many ways) and am now stuck in `Deriv.genFamInst`.</div><div>I realised I would need to replicate the first half of `tcClsInstDecl` to implement it. Before long, I will probably also need to replicate the other half to check method bindings.</div><div>That leaves me wondering: Is it a good idea to integrate this new deriving strategy so tightly with the existing deriving framework?</div><div>I would rather just call `tcClsInstDecl`, do a bit of sanity checking for specified constraints in standalone deriving declarations and call it a day.</div><div><br /></div><div>Given that you are the architect of our current deriving code, I hope you are the right person to ask for input.</div><div><br /></div><div>Thanks,</div><div>Sebastian</div></div></div></blockquote></div>
</blockquote></div>
</blockquote></div>
</body></html>