Changes between Version 24 and Version 25 of DefaultSuperclassInstances


Ignore:
Timestamp:
May 28, 2013 9:41:11 AM (2 years ago)
Author:
simonpj
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • DefaultSuperclassInstances

    v24 v25  
    3232The major design goal is this:
    3333
    34  * '''Design goal 1: a class C can be re-factored into a class C with a superclass S, without disturbing any clients.'''
     34===  Design goal 1: a class C can be re-factored into a class C with a superclass S, without disturbing any clients ===
    3535
    3636The difficulty is that if you start with
     
    5858Design goal 1 is that this change should not force clients to change their code.  Haskell 98 does not satisfy this goal.  In Haskell 98 the client function `foo` is fine unmodified, but the instance declaration would have to be split into two.
    5959
    60 '''SLPJ''': are there any other design goals?
     60=== Design goal 2: write implementations of sub-classes that imply their superclass implementations ===
     61
     62Example 1: once you say
     63{{{
     64instance Ord (T a) where
     65  compare = ...
     66}}}
     67then the implementation of `(==)` in `Eq` is obvious.  So, in the class decl for `Ord` we'd like to say (modulo syntax, see below)
     68{{{
     69class Eq a => Ord a where
     70  ...
     71  instance Eq a where
     72    (==) a b = case compare a b of { EQ -> True; _ -> False }
     73}}}
     74
     75Example 2: once you say `instance Monad M`, the instances for `Functor M` and `Applicative M` can be derivived from the definitions of `(>>=)` and `return`.  And similarly if you say `instance Applicative M`, the `Functor M` instance is derivable.
     76
     77Example 3: once you say `instance Traversable T` you can derive `Foldable` and `Functor`.
     78
     79Obvious question: if something is both `Foldable` and `Applicative`, which `Functor` instance do you get?
     80
     81
    6182
    6283------------------------------