[GHC] #16362: Deriving a class via an instance that has a TypeError constraint using standalone deriving fails during compilation.

GHC ghc-devs at haskell.org
Mon Feb 25 18:51:07 UTC 2019


#16362: Deriving a class via an instance that has a TypeError constraint using
standalone deriving fails during compilation.
-------------------------------------+-------------------------------------
        Reporter:  j9794             |                Owner:  (none)
            Type:  bug               |               Status:  new
        Priority:  normal            |            Milestone:
       Component:  Compiler          |              Version:  8.6.3
      Resolution:                    |             Keywords:
                                     |  CustomTypeErrors
Operating System:  Unknown/Multiple  |         Architecture:
 Type of failure:  GHC rejects       |  Unknown/Multiple
  valid program                      |            Test Case:
      Blocked By:                    |             Blocking:
 Related Tickets:                    |  Differential Rev(s):
       Wiki Page:                    |
-------------------------------------+-------------------------------------

Comment (by diatchki):

 If I understand what's going on here, the custom type errors work as
 intended.

 I think of using `TypeError` in the context of an instance declaration as
 saying "this instance does not exist".  I think it would be nicer to have
 an actual language construct to say that, but for the time being
 `TypeError` is convenient "hack" that achieves a similar result.

 So, the standalone instance in the original example has to derive and
 instance for `Num Foo` by using the non-existing instance for `NotNum
 Foo`.  As a result GHC reports that it can't do that, using the custom
 type error provided.  I think that this makes sense.

 OTOH, if you write the alternative that Ryan wrote:

 {{{
 deriving via (NotNum Foo) instance TypeError (Text "Not a num") => Num Foo
 }}}

 you are really saying that there is no instance for `Num Foo` and for the
 exact same reason that there is no instance for `NotNum Foo`.  Deriving a
 non-existing instance seems like a bit of an odd thing to do though, as
 there is nothing to derive...

 I guess it makes sense if you are trying to report a class of errors in
 the same way,
 but you could get the same by just writing the instance without deriving:
 {{{
 instance Num (NotNum Foo) => Num Foo
 }}}

-- 
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/16362#comment:6>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler


More information about the ghc-tickets mailing list