Personal tools

Multi-parameter type class

From HaskellWiki

(Difference between revisions)
Jump to: navigation, search
(a uniform interface to mutable variables can be implemented in Haskell 98)
 
(One intermediate revision by one user not shown)
Line 16: Line 16:
 
</haskell>
 
</haskell>
   
The correct LANGUAGE pragma for them is <hask>{-# LANGUAGE MultiParamTypeClasses #-}</hask>
+
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 is a relation between types.
Line 23: Line 23:
   
 
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]].
  +
  +
The above example can even be implemented in
  +
[[Mutable variable|plain Haskell 98]].
   
 
== Also see ==
 
== Also see ==

Latest revision as of 19:01, 9 June 2013

[edit] 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 ...
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.

[edit] 2 Also see

The Haskell' page