https://wiki.haskell.org/api.php?action=feedcontributions&user=Cydparser&feedformat=atomHaskellWiki - User contributions [en]2020-03-30T20:46:53ZUser contributionsMediaWiki 1.27.4https://wiki.haskell.org/index.php?title=GHC/Stand-alone_deriving_declarations&diff=60842GHC/Stand-alone deriving declarations2016-06-15T17:57:54Z<p>Cydparser: /* Standalone deriving */</p>
<hr />
<div>[[Category:GHC|Stand-alone deriving declarations]]<br />
<br />
''This page is from an early point in the life of the stand-alone deriving mechanism. Please see the linked documentation for an up-to-date account of the present situation.''<br />
<br />
== Standalone deriving ==<br />
<br />
GHC supports so-called "stand-alone deriving" declarations, which are described in the [https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/glasgow_exts.html#stand-alone-deriving-declarations user manual section]. <br />
<br />
This page mentions points that may not be immediately obvious from the manual.<br />
<br />
== Deriving data types with non-standard contexts ==<br />
<br />
In Haskell 98, and GHC, you can't say this<br />
<haskell><br />
data T m = MkT (m Int) deriving Eq<br />
</haskell><br />
because the instance declaration would have a non-standard context. It would have to look like this:<br />
<haskell><br />
instance Eq (m Int) => Eq (T m) where ...<br />
</haskell><br />
Of course, you can write the instance manually, but then you have to write<br />
all that tiresome code for equality. Standalone deriving lets you supply the context yourself, but have GHC write the code:<br />
<haskell><br />
data T m = MkT (m Int)<br />
deriving instance Eq (m Int) => Eq (T m)<br />
</haskell><br />
Of course, you'll need to add the flags <hask>-XFlexibleContexts</hask> and <hask>-XUndecideableInstances</hask> to allow this instance declaration, but that's fair enough.<br />
<br />
The same applies to data type declarations involving type functions.<br />
<br />
== Variations (not implemented) ==<br />
<br />
This section collects some un-implemented ideas.<br />
<br />
=== Interaction with "newtype-deriving" ===<br />
<br />
GHC's "newtype deriving mechanism" (see [http://www.haskell.org/ghc/dist/current/docs/users_guide/deriving.html#newtype-deriving]) should obviously work in a standalone deriving setting too. But perhaps it can be generalised a little. Currently you can only say<br />
<haskell><br />
deriving instance C a Foo<br />
</haskell><br />
(where Foo is the newtype), and get an instance for <hask>(C a Foo)</hask>. But what if you want and instance for <hask>C Foo a</hask>, where the new type is not the last parameter. You can't do that at the moment. However, even with the new instance-like syntax, it's not clear to me how to signal the type to be derived. Consider<br />
<haskell><br />
newtype Foo = F Int<br />
newtype Bar = B Bool<br />
deriving instance C Foo Bar<br />
</haskell><br />
Which of these thee instances do we want?<br />
<haskell><br />
instance C Foo Bool => C Foo Bar<br />
instance C Int Bar => C Foo Bar<br />
instance C Int Bool => C Foo Bar<br />
</haskell><br />
The obvious way to signal this is to give the instance context (just as above). This is perhaps another reason for having an explicit instance context in a standalone deriving declaration.<br />
<br />
Incidentally, notice that the third of the alternatives in the previous bullet unwraps two newtypes simultaneously. John Meacham suggested this example:<br />
<haskell><br />
class SetLike m k where <br />
instance SetLike IntSet Int where<br />
<br />
newtype Id = Id Int<br />
newtype IdSet = IdSet IntSet<br />
deriving instance SetLike IntSet Int => SetLike IdSet Id<br />
</haskell><br />
<br />
=== Duplicate instances === <br />
<br />
Suppose two modules, M1 and M2 both contain an identical standalone deriving declaration<br />
<haskell><br />
deriving Show T<br />
</haskell><br />
Then, can you import M1 and M2 into another module X and use show on values of type T, or will you get an overlapping instance error? Since both instances are derived in the very same way, their code must be identical, so arguably we can choose either. (There is some duplicated code of course.)<br />
<br />
This situation is expected to be common, as the main use of the standalone feature is to obtain derived instances that were omitted when the data type was defined.<br />
<br />
But, that means whether or not an instance was derived is now part of the module's. Programs would be able to use this (mis)feature to perform a compile-time check and execute code differently depending on whether any given instance is derived or hand-coded:<br />
<haskell><br />
module MA(A) where<br />
data A = A deriving Show<br />
<br />
module MB(B) where<br />
data B = B deriving Show<br />
<br />
module MC where<br />
import MA<br />
import MB<br />
<br />
-- verify that the A and B Show instances were derived<br />
-- (they need to be derived to ensure the output can<br />
-- be parsed in our non-Haskell code).<br />
deriving instance Show A <br />
deriving instance Show B<br />
</haskell><br />
The writer of MC already knows that MA and MB defined instances of Show for A and B. He just wants to ensure that nobody changes either module to use a non-derived instance; if someone does try to use a non-derived instance:<br />
<haskell><br />
module MA(A) where<br />
data A = A<br />
instance Show A where<br />
show _ = "a"<br />
</haskell><br />
then they will get an overlapping instance error in MC. <br />
<br />
The result is that programs would be able to require, for any Class, not just that an instance of the class was defined for a type, but that a /derived/ instance was defined. Is this good?</div>Cydparser