Difference between revisions of "Multi-parameter type class"

From HaskellWiki
Jump to navigation Jump to search
(note inclusion in ghc2021, link functional dependencies vs type families)
 
(3 intermediate revisions by the same user not shown)
Line 3: Line 3:
 
[[Category:Stub articles]]
 
[[Category:Stub articles]]
   
{{GHCUsersGuide|glasgow_exts|multi-parameter-type-classes|a Multi Parameter Type Classes section}}
+
{{GHCUsersGuide|exts/multi_param_type_classes||a Multi Parameter Type Classes section}}
   
 
== About ==
 
== About ==
Line 21: Line 21:
 
To enable them, use the <hask>{-# LANGUAGE MultiParamTypeClasses #-}</hask> pragma.
 
To enable them, use the <hask>{-# LANGUAGE MultiParamTypeClasses #-}</hask> 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.
+
If you think of a single-parameter type class as a set of types, then a multi-parameter type class (MPTC) 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.
 
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]].
+
Some uses of MPTCs with functional dependencies can be replaced with [[type families]]. See [[functional dependencies vs. type families]].
  +
  +
MPTCs are part of [[GHC2021]].
   
 
The above example can even be implemented in
 
The above example can even be implemented in
Line 32: Line 34:
 
== Also see ==
 
== Also see ==
   
[http://hackage.haskell.org/trac/haskell-prime/wiki/MultiParamTypeClasses The Haskell' page]
+
[http://web.archive.org/web/20190404224552/https://prime.haskell.org/wiki/MultiParamTypeClasses The archived Haskell' page]

Latest revision as of 22:52, 21 July 2021


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 (MPTC) 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. See functional dependencies vs. type families.

MPTCs are part of GHC2021.

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

Also see

The archived Haskell' page