Stand-alone deriving declarations added

Björn Bringert bringert at cs.chalmers.se
Thu Oct 5 10:50:40 EDT 2006


Bulat Ziganshin wrote:
> Hello Bjorn,
> Thursday, October 5, 2006, 12:05:00 PM, you wrote:
>> I have just pushed some patches which add support for stand-alone
>> 'deriving' declarations.
> thank you! i like this a lot
>> deriving Class for Type
> is 'for' now reserved exclusively for deriving? can i define 'for'
> function, for example?

Yes, just like, for example, "as" does in import declarations, "for"  
only has a special meaning in a stand-alone deriving clause. You can  
still define a "for" function.

> what is behavior of your extension when there is no access to
> internals of a type? say,
> import Data.Map (Map)
> deriving Typeable for Map

It should be the same as if you tried writing the instance by hand,  
but I have to admit that I haven't tested that.

>> and for multi-parameter type classes:
>> deriving (Class t1 ... tn) for Type
> which means that stand-alone deriving has more features than old  
> one? :)

You can actually do this with normal newtype deriving too. The given  
type must be the last argument to the class. See "Generalising the  
deriving clause" in the type GHC manual.

Stand-alone deriving declarations are actually a little bit weaker  
than normal deriving clauses, since the current implementation does  
not let you reference the type arguments of a newtype in the  
arguments to the MPTC. This example from the GHC manual cannot  
currently be done with stand-alone deriving:

   class StateMonad s m | m -> s where ...
   instance Monad m => StateMonad s (State s m) where ...

   newtype Parser tok m a = Parser (State [tok] (Failure m) a)
                          deriving (Monad, StateMonad [tok])


The below does not work, since tok is not in scope:

deriving StateMonad [tok] for Parser

Maybe the syntax should be changed to be:

deriving (Class t1 ... tn) for Type a1 ... an

where a1 ... an are type variables. This would allow:

deriving StateMonad [tok] for Parser tok m a


/Björn



More information about the Cvs-ghc mailing list