Changes between Version 17 and Version 18 of DefaultSuperclassInstances


Ignore:
Timestamp:
Aug 22, 2011 7:34:18 AM (3 years ago)
Author:
simonpj
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • DefaultSuperclassInstances

    v17 v18  
    5858'''SLPJ''': are there any other design goals? 
    5959 
     60------------------------------ 
    6061== The proposal == 
    6162 
     
    184185So the above instance would generate an intrinsic instance for `Applicative` but not for `Functor`. 
    185186 
     187=== Details === 
     188 
     189Each default superclass instance declaration in a `class` declaration must be for 
     190a distinct class.  So one of these is OK and the other is not: 
     191{{{ 
     192    -- This is ILLEGAL 
     193    class (Tweedle dum, Tweedle dee) => Rum dum dee where 
     194      instance Tweedle dum where ... 
     195      instance Tweedle dee where ... 
     196 
     197    -- But this is OK  
     198    class (Tweedle dum, Tweedle dee) => Rum dum dee where 
     199      instance Tweedle dee where ... 
     200}}} 
     201By requiring that intrinsic superclasses be class-distinct, we ensure that the distribution of methods to spawned instances is unambiguous.  
     202 
     203=== Flags === 
     204 
     205The declaration of a class with a default superclass instance would require a language extension flag; but the ''instances'' of such a class would not.  Again Design Goal 1 means that we want to impact client code as little as possible. 
     206 
     207--------------------------- 
     208== Possible variations == 
     209 
     210=== The design of the opt-out mechanism === 
     211 
     212The [ http://www.haskell.org/haskellwiki/Superclass_defaults superclass default proposal] deals with the question of opt-outs by intead requiring you to opt ''in''.  A `Monad` instance would look like 
     213{{{ 
     214  instance (Functor T, Applicative T, Monad T) where 
     215    (>>=) = ...blah... 
     216    return = ...bleh... 
     217}}} 
     218where we explicitly ask the compiler to generate an instance of `Applicative T` and `Functor T`.   The disadvantage is that you have to know to do so, which contradicts Design Goal 1. 
     219 
    186220Jón's proposal had a more subtle opt-out policy, namely that an 
    187221intrinsic superclass can be quietly pre-empted by an instance for the 
    188 superclass from a prior or the present module. Note that to declare an 
     222superclass from a prior or the present module.  
     223For example, instead of  
     224{{{ 
     225  instance Functor T where ... 
     226 
     227  instance Monad T where 
     228    return x = ... 
     229    ba >>= bf = ... 
     230    hiding instance Functor 
     231}}} 
     232you would simply say 
     233{{{ 
     234  instance Functor T where ... 
     235 
     236  instance Monad T where 
     237    return x = ... 
     238    ba >>= bf = ... 
     239}}} 
     240Of course, the instance of `Functor T` might be in a different module entirely. 
     241Note that to declare an 
    189242instance of the subclass, one must produce an instance of the 
    190243superclass by the same module at the latest.  
     
    219272dangerous tendency to be permanent. 
    220273 
    221 == Other designs == 
    222  
    223 The [ http://www.haskell.org/haskellwiki/Superclass_defaults superclass default proposal] deals with the question of opt-outs by intead requiring you to opt ''in''.  A `Monad` instance would look like 
    224 {{{ 
    225   instance (Applicative m, Monad m) where 
    226     (>>=) = ...blah... 
    227     (<*)  = ...bleh... 
    228 }}} 
    229 where we explicitly ask the compiler to generate an instance of `Applicative`.   The disadvantage is that you have to know to do so, which contracts Design Goal 1. 
    230  
    231274== Multi-headed instance declarations == 
    232275 
     
    257300them. 
    258301 
    259 == Details == 
    260  
    261 Each default superclass instance declaration in a `class` declaration must be for 
    262 a distinct class.  So one of these is OK and the other is not: 
    263 {{{ 
    264     -- This is ILLEGAL 
    265     class (Tweedle dum, Tweedle dee) => Rum dum dee where 
    266       instance Tweedle dum where ... 
    267       instance Tweedle dee where ... 
    268  
    269     -- But this is OK  
    270     class (Tweedle dum, Tweedle dee) => Rum dum dee where 
    271       instance Tweedle dee where ... 
    272 }}} 
    273 By requiring that intrinsic superclasses be class-distinct, we ensure that the distribution of methods to spawned instances is unambiguous.  
    274  
     302