Multi-parameter type class

From HaskellWiki
Revision as of 05:19, 10 December 2020 by Ysangkok (talk | contribs) (→‎Also see: fix Haskell prime wiki link)
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.


The GHC Users Guide has a Multi Parameter Type Classes section.

About

Basically, type classes which can take multiple arguments, such as:

class Monad m => VarMonad m v where
  new :: a -> m (v a)
  get :: v a -> m a
  put :: v a -> a -> m ()

instance VarMonad IO IORef where ...
instance VarMonad (ST s) (STRef s) where ...

To enable them, use the {-# LANGUAGE MultiParamTypeClasses #-} pragma.

If you think of a single-parameter type class as a set of types, then a multi-parameter type class is a relation between types.

Naive use of MPTCs may result in ambiguity, so functional dependencies were developed as a method of resolving that ambiguity, declaring that some subset of the parameters is sufficient to determine the values of the others.

Some uses of MPTCs with functional dependencies can be replaced with type families.

The above example can even be implemented in plain Haskell 98.

Also see

The archived Haskell' page