As noted in Trac #12837, these classes are special and the user should not be able to define their own instances.
Thanks @sjorn3 . I added Adam as a reviewer, could you please edit the differential title and description to describe the change you have made? The commit message is based on this information. so it is best to be a bit more descriptive.
Perhaps something like "Disallow users to write instances for KnownNat and KnownSymbol" and make sure to reference the ticket in the description.
This patch solves the problem well and fits with existing code in the area. Thanks, @sjorn3 !
However, the existing code in the area is not as well organized as it could be. Perhaps we can improve. The problem is that TcInstDcls.tcClsInstDecl calls the doClsInstErrorChecks function edited here. That function starts out by calling TcHsType.tcHsClsInstType, which calls TcValidity.checkValidInstance which calls TcValidity.checkValidInstHead. That last function prevents hand-written instances of the so-called abstract classes, which are (~), (~~), and Coercible. However, we really should add Typeable, KnownNat, and KnownSymbol to this list and then move the check that's in doClsInstErrorChecks to checkValidInstHead. It's just painful to have two separate checks for the same sort of problem.
I propose this logic: doClsInstErrorChecks does the checks that depend on the body of the instance (currently, this would just be the no-bindings-in-boot-files check) and checkVAlidInstHead does the checks that depend on the type (or other bits in the instance head). According to this logic, the abstract-class check moves.
Of course, I'm asking here just to clean up some existing mess and am happy to mark the current patch for acceptance as is, if you'd prefer to get the patch in. Thanks again!
Regarding the refactoring, for now I'd rather just get the patch in as I have exams coming up (last on the 10th) and need to focus on them, but would be happy to give it a go once they're done with.
I'll take a look at writing a regression test tomorrow evening, thanks again.