Open jonathanlking opened 1 year ago
A solution to this is to make sure
deriveArgDict ''B
comes beforederiveArgDict ''A
, which does generate the expected code, but this can be difficult to work out just from the error message.
Check out https://github.com/gelisam/klister This is the proper solution to keep order independence and I hope we get it someday in Haskell. @david-christiansen being HF director is auspicious for this :)
I am curious what the
hasArgDictInstance
check actually prevents? Can we remove it, always "optimistically" generate constraints and just let GHC's constraint solver handle this for us (failing if the constraint can't be satisfied).
I don't understand this. Are you asking why we can't also do Data.Constraint.Extras.argDict x
--- why we sometimes need to do Data.Constraint.Dict
instead?
Haskell languages changes are up to the GHC Steering Committee, not me :-) I'm glad you think Klister is promising, though, and I hope to someday have some time to do work on it again.
Well that's why I used a vague work like "auspicious" :) For now, I just want to collect a Real World™ instance of the problem that solves to refer to sometime later.
Thanks for sharing klister with me, I hadn't heard about it before and it looks interesting! :sparkles:
I don't understand this. Are you asking why we can't also do Data.Constraint.Extras.argDict x --- why we sometimes need to do Data.Constraint.Dict instead?
Sorry, I don't think I asked the question very well, let me try again:
I've found that removing the hasArgDictInstance
check makes the problem I described go away.
If I removed it (e.g. https://github.com/jonathanlking/constraints-extras/commit/e395ee1e04b0b1326a0ae958af3775e9fa62548a), what could go wrong? (i.e. what is the function of it?)
Consider the following toy example:
Compiling this errors with:
Dumping the splices (with
-ddump-splices
) we can see the generated code is:When instead I would expect it to generate:
As the
ArgDict
instance forB
has not been generated yet,hasArgDictInstance
isFalse
inderiveArgDict
, so noConstraintsFor B c
/recursive call toargDict
is generated.A solution to this is to make sure
deriveArgDict ''B
comes beforederiveArgDict ''A
, which does generate the expected code, but this can be difficult to work out just from the error message.I am curious what the
hasArgDictInstance
check actually prevents? Can we remove it, always "optimistically" generate constraints and just let GHC's constraint solver handle this for us (failing if the constraint can't be satisfied).I have patched our copy of
constraints-extras
in our codebase to do this and haven't noticed any errors, but am very possibly missing something!