# User:Zzo38/Proposal about classes

### From HaskellWiki

(New page: This document is proposal relating to various things dealing with classes in Haskell. ==Monads== Move <tt>join</tt> into the <tt>Monad</tt> class, to allow monads to defined in terms of u...) |
|||

(3 intermediate revisions by one user not shown) | |||

Line 12: | Line 12: | ||

join :: m (m a) -> m a; | join :: m (m a) -> m a; | ||

fail :: String -> m a; | fail :: String -> m a; | ||

− | + | ||

a >>= f = join $ fmap f a; | a >>= f = join $ fmap f a; | ||

m >> k = m >>= \_ -> k; | m >> k = m >>= \_ -> k; | ||

join = (>>= id); | join = (>>= id); | ||

fail = error; | fail = error; | ||

− | + | ||

fmap f m = m >>= return . f; | fmap f m = m >>= return . f; | ||

}; | }; | ||

Line 28: | Line 28: | ||

==Classes of classes== | ==Classes of classes== | ||

See proposal of kinds. | See proposal of kinds. | ||

+ | |||

+ | ==Universal class== | ||

+ | There can be one universal class that all types automatically belong to. This is a polymorphic class (see proposal of kinds) of kind <tt>forall k. [k]</tt>; that is, it is for any kind (including other classes). | ||

+ | |||

+ | ==Generalize an existing class== | ||

+ | <!-- class RestrictMonad (UniversalClass :: [*]) <- Monad; --> | ||

+ | |||

+ | ==Automatic instances== | ||

+ | You can have a Template Haskell command, that takes a name of class, and a function that uses the definition of a type to make up a instance declaration for that class. If there is no such instance, it tries to make up the instance using that function. If it results in <tt>fail</tt>, then it is error as if there is no instance normally, but the fail message is also displayed. | ||

+ | |||

+ | ==See also== | ||

+ | * [[Superclass defaults]] | ||

+ | |||

+ | [[Category:Proposals]] |

## Latest revision as of 05:09, 4 September 2011

This document is proposal relating to various things dealing with classes in Haskell.

## Contents |

## [edit] 1 Monads

Move `join` into the `Monad` class, to allow monads to defined in terms of unit/join/fmap. Together with things described below, it can automatically be `Functor` as well, and define `fmap` if you are using return/bind definitions.

## [edit] 2 Default superclass definitions

For example, for monads you can have:

class Functor m => Monad (m :: * -> *) where { (>>=) :: forall a b. m a -> (a -> m b) -> m b; (>>) :: forall a b. m a -> m b -> m b; return :: a -> m a; join :: m (m a) -> m a; fail :: String -> m a; a >>= f = join $ fmap f a; m >> k = m >>= \_ -> k; join = (>>= id); fail = error; fmap f m = m >>= return . f; };

In this case, you can define the defaults for `Functor` if they are not already defined.

## [edit] 3 Restrictive classes

For example, make a restrictive monad that only contains instances of `Ord`. I am not sure about syntax or other details.

## [edit] 4 Classes of classes

See proposal of kinds.

## [edit] 5 Universal class

There can be one universal class that all types automatically belong to. This is a polymorphic class (see proposal of kinds) of kind `forall k. [k]`; that is, it is for any kind (including other classes).

## [edit] 6 Generalize an existing class

## [edit] 7 Automatic instances

You can have a Template Haskell command, that takes a name of class, and a function that uses the definition of a type to make up a instance declaration for that class. If there is no such instance, it tries to make up the instance using that function. If it results in `fail`, then it is error as if there is no instance normally, but the fail message is also displayed.