Personal tools

Multi-parameter type class

From HaskellWiki

(Difference between revisions)
Jump to: navigation, search
(Glossary)
Line 2: Line 2:
 
[[Category:Glossary]]
 
[[Category:Glossary]]
 
[[Category:Stub articles]]
 
[[Category:Stub articles]]
 
 
 
== About ==
 
== About ==
   
 
Basically, [[type class|type classes]] which can take multiple arguments, such as:
 
Basically, [[type class|type classes]] which can take multiple arguments, such as:
   
<hask>
+
<haskell>
class Foo a b
+
class Monad m => VarMonad m v where
</hask>
+
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 ...
  +
</haskell>
  +
  +
The correct LANGUAGE pragma for them is <hask>{-# LANGUAGE MultiParamTypeClasses #-}</hask>
  +
  +
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.
   
Without [[functional dependencies]] or [[associated types]], these multi-parameter type classes may cause too much ambiguity to pass the type-checker.
+
Some uses of MPTCs with functional dependencies can be replaced with [[type families]].
   
 
== Also see ==
 
== Also see ==

Revision as of 17:18, 21 December 2012

1 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 ...
The correct LANGUAGE pragma for them is
{-# LANGUAGE MultiParamTypeClasses #-}

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.

2 Also see

The Haskell' page