1 Type system extensions in GHC
GHC comes with a rather large collection of type-system extensions (beyond Haskell 98). They are all documented in the user manual, but this page is a place to record observations, notes, and suggestions on them.
1.1 Overlapping instances
Here an interesting message about the interaction of existential types and overlapping instances.
1.2 Indexed data types and indexed newtypes
Indexed data types (including associated data types) are a very recent addition to GHC's type system extensions that is not yet included in the user manual. To use the extension, you need to obtain a version of GHC from its source repository.
1.3 Stand-alone deriving clauses
Bjorn Bringert has recently implemented "stand-alone deriving" declarations, documented briefly here . There are a few loose ends which I summarise here:
- The current syntax is
deriving Show for T
- There seems to be a consensus that this would be better:
derive instance Show T
- so that it looks more like a regular instance declaration. Here is not a new keyword; it's a "special-id", distinguished by the followingderivekeyword.instance
- Because it looks like a regular instance declaration, it would arguably be reasonable to require the programmer to supply the context. It seems odd to say:
derive instance Show (T a)
- and perhaps cleaner to say
derive instance Show a => Show (T a)
- (At the moment, the compiler figures out the appropriate context, but at some point that automation may run out of steam.)
- GHC's "newtype deriving mechanism" (see ) should obviously work in a standalone deriving setting too. But perhaps it can be generalised a little. Currently you can only say
deriving C a for Foo
- (where Foo is the newtype), and get an instance for . But what if you want and instance for(C a Foo), 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. ConsiderC Foo a
newtype Foo = F Int newtype Bar = B Bool derive instance C Foo Bar
- Which of these thee instances do we want?
instance C Foo Bool => C Foo Bar instance C Int Bar => C Foo Bar instance C Int Bool => C Foo Bar
- 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.
- Incidentally, notice that the third of the alternatives in the previous bullet unwraps two newtypes simultaneously. John Meacham suggested this example:
class SetLike m k where instance SetLike IntSet Int where newtype Id = Id Int newtype IdSet = IdSet IntSet derive instance SetLike IntSet Int => SetLike IdSet Id
- Suppose two modules, M1 and M2 both contain an identical standalone deriving declaration
derive Show T
- 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.)