... | @@ -3,7 +3,7 @@ This page documents yet another proposal to make class hierarchies easier to dea |
... | @@ -3,7 +3,7 @@ This page documents yet another proposal to make class hierarchies easier to dea |
|
|
|
|
|
## Example
|
|
## Example
|
|
|
|
|
|
```wiki
|
|
```
|
|
classMonad m where
|
|
classMonad m where
|
|
return :: a -> m a
|
|
return :: a -> m a
|
|
(>>=):: m a ->(a -> m b)-> m b
|
|
(>>=):: m a ->(a -> m b)-> m b
|
... | @@ -16,13 +16,9 @@ class Monad m where |
... | @@ -16,13 +16,9 @@ class Monad m where |
|
(<*>)= ap
|
|
(<*>)= ap
|
|
|
|
|
|
instanceMonadMaybewhere
|
|
instanceMonadMaybewhere
|
|
return = Just
|
|
return =JustNothing>>=_=Nothing(Just x)>>= f = f x
|
|
Nothing >>= _ = Nothing
|
|
|
|
(Just x) >>= f = f x
|
|
|
|
|
|
|
|
deriving (Functor, Applicative) -- these inherit the constraints and params above
|
|
deriving(Functor,Applicative)-- these inherit the constraints and params above-- ORderivinginstanceFunctor m
|
|
-- OR
|
|
|
|
deriving instance Functor m
|
|
|
|
derivinginstanceApplicative m -- these specify constraints
|
|
derivinginstanceApplicative m -- these specify constraints
|
|
```
|
|
```
|
|
|
|
|
... | @@ -100,7 +96,7 @@ In a deep class hierarchy (i.e. with long chains of superclasses), it may be con |
... | @@ -100,7 +96,7 @@ In a deep class hierarchy (i.e. with long chains of superclasses), it may be con |
|
|
|
|
|
For example:
|
|
For example:
|
|
|
|
|
|
```wiki
|
|
```
|
|
classFunctor f where
|
|
classFunctor f where
|
|
fmap ::(a -> b)-> f a -> f b
|
|
fmap ::(a -> b)-> f a -> f b
|
|
|
|
|
... | @@ -109,9 +105,7 @@ class Functor f => Applicative f where |
... | @@ -109,9 +105,7 @@ class Functor f => Applicative f where |
|
(<*>):: f (a -> b)-> f a -> f b
|
|
(<*>):: f (a -> b)-> f a -> f b
|
|
|
|
|
|
derivingdefaultinstanceFunctor f where
|
|
derivingdefaultinstanceFunctor f where
|
|
fmap f = (pure f <*>)
|
|
fmap f =(pure f <*>)classApplicative m =>Monad m where
|
|
|
|
|
|
class Applicative m => Monad m where
|
|
|
|
return :: a -> m a
|
|
return :: a -> m a
|
|
(>>=):: m a ->(a -> m b)-> m b
|
|
(>>=):: m a ->(a -> m b)-> m b
|
|
fail ::String-> m a
|
|
fail ::String-> m a
|
... | @@ -133,34 +127,29 @@ This proposal does not address directly how to split a class into pieces, but th |
... | @@ -133,34 +127,29 @@ This proposal does not address directly how to split a class into pieces, but th |
|
|
|
|
|
Here is a class we want to split:
|
|
Here is a class we want to split:
|
|
|
|
|
|
```wiki
|
|
```
|
|
class Num a where
|
|
classNum a where(+):: a -> a -> a
|
|
(+) :: a -> a -> a
|
|
|
|
(*):: a -> a -> a
|
|
(*):: a -> a -> a
|
|
```
|
|
```
|
|
|
|
|
|
|
|
|
|
And here is how we might split it:
|
|
And here is how we might split it:
|
|
|
|
|
|
```wiki
|
|
```
|
|
classAdditive a where
|
|
classAdditive a where
|
|
add :: a -> a -> a
|
|
add :: a -> a -> a
|
|
classMultiplicative a where
|
|
classMultiplicative a where
|
|
mult :: a -> a -> a
|
|
mult :: a -> a -> a
|
|
|
|
|
|
class (Additive a, Multiplicative a) => Num a where
|
|
class(Additive a,Multiplicative a)=>Num a where(+):: a -> a -> a
|
|
(+) :: a -> a -> a
|
|
|
|
(+)= add
|
|
(+)= add
|
|
|
|
|
|
(*):: a -> a -> a
|
|
(*):: a -> a -> a
|
|
(*)= mult
|
|
(*)= mult
|
|
|
|
|
|
derivingdefaultinstanceAdditive a where
|
|
derivingdefaultinstanceAdditive a where
|
|
add = (+)
|
|
add =(+)derivingdefaultinstanceMultiplicative a where
|
|
deriving default instance Multiplicative a where
|
|
mult =(*){-# MINIMAL (add | (+)), (mult | (*)) #-}
|
|
mult = (*)
|
|
|
|
|
|
|
|
{-# MINIMAL (add | (+)), (mult | (*)) #-}
|
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
|
... | | ... | |