Data.Equivalence.Monad

equivalence-0.3.1: Maintaining an equivalence relation implemented as union-find using STT.

Copyright | Patrick Bahr, 2010
License | All Rights Reserved
Maintainer | Patrick Bahr
Stability | unknown
Portability | unknown
Safe Haskell | None
Language | Haskell98

Data.Equivalence.Monad

Description

This is an alternative interface to the union-find implementation in '`STT'`. It is wrapped into the monad transformer `EquivT`.

Synopsis

- class (Monad m, Ord v) => MonadEquiv c v d m | m -> v, m -> c, m -> d where
- newtype EquivT s c v m a = EquivT {
  - unEquivT :: ReaderT (Equiv s c v) (STT s m) a
  }
- type EquivT' s = EquivT s ()
- type EquivM s c v = EquivT s c v Identity
- type EquivM' s v = EquivM s () v
- runEquivT :: Monad m => (v -> c) -> (c -> c -> c) -> (forall s. EquivT s c v m a) -> m a
- runEquivT' :: Monad m => (forall s. EquivT' s v m a) -> m a
- runEquivM :: (v -> c) -> (c -> c -> c) -> (forall s. EquivM s c v a) -> a
- runEquivM' :: (forall s. EquivM' s v a) -> a

# Documentation

class (Monad m, Ord v) => MonadEquiv c v d m | m -> v, m -> c, m -> d where #

This class specifies the interface for a monadic computation that maintains an equivalence relation.

Minimal complete definition

equivalent, classDesc, equateAll, removeClass, getClass, combineAll, (===), desc, remove

Methods

equivalent :: v -> v -> m Bool #

This function decides whether the two given elements are equivalent in the current equivalence relation

classDesc :: v -> m d #

This function obtains the descriptor of the given element's equivalence class.

equateAll :: [v] -> m () #

This function equates the element in the given list. That is, it unions the equivalence classes of the elements and combines their descriptor.

equate :: v -> v -> m () #

This function equates the given two elements. That is it unions the equivalence classes of the two elements.

removeClass :: v -> m Bool #

This function removes the equivalence class of the given element. If there is no corresponding equivalence class, `False` is returned; otherwise `True`.

getClass :: v -> m c #

This function provides the equivalence class the given element is contained in.

combineAll :: [c] -> m () #

This function combines all equivalence classes in the given list. Afterwards all elements in the argument list represent the same equivalence class!

combine :: c -> c -> m c #

This function combines the two given equivalence classes. Afterwards both arguments represent the same equivalence class! One of it is returned in order to represent the new combined equivalence class.

(===) :: c -> c -> m Bool #

This function decides whether the two given equivalence classes are the same.

desc :: c -> m d #

This function returns the descriptor of the given equivalence class.

remove :: c -> m Bool #

This function removes the given equivalence class. If the equivalence class does not exists anymore `False` is returned; otherwise `True`.

Instances

MonadEquiv c v d m => MonadEquiv c v d (StateT s m) # |
Methods equivalent :: v -> v -> StateT s m Bool # classDesc :: v -> StateT s m d # equateAll :: [v] -> StateT s m () # equate :: v -> v -> StateT s m () # removeClass :: v -> StateT s m Bool # getClass :: v -> StateT s m c # combineAll :: [c] -> StateT s m () # combine :: c -> c -> StateT s m c # (===) :: c -> c -> StateT s m Bool # desc :: c -> StateT s m d # remove :: c -> StateT s m Bool #
MonadEquiv c v d m => MonadEquiv c v d (ExceptT e m) # |
Methods equivalent :: v -> v -> ExceptT e m Bool # classDesc :: v -> ExceptT e m d # equateAll :: [v] -> ExceptT e m () # equate :: v -> v -> ExceptT e m () # removeClass :: v -> ExceptT e m Bool # getClass :: v -> ExceptT e m c # combineAll :: [c] -> ExceptT e m () # combine :: c -> c -> ExceptT e m c # (===) :: c -> c -> ExceptT e m Bool # desc :: c -> ExceptT e m d # remove :: c -> ExceptT e m Bool #
(MonadEquiv c v d m, Error e) => MonadEquiv c v d (ErrorT e m) # |
Methods equivalent :: v -> v -> ErrorT e m Bool # classDesc :: v -> ErrorT e m d # equateAll :: [v] -> ErrorT e m () # equate :: v -> v -> ErrorT e m () # removeClass :: v -> ErrorT e m Bool # getClass :: v -> ErrorT e m c # combineAll :: [c] -> ErrorT e m () # combine :: c -> c -> ErrorT e m c # (===) :: c -> c -> ErrorT e m Bool # desc :: c -> ErrorT e m d # remove :: c -> ErrorT e m Bool #
(MonadEquiv c v d m, Monoid w) => MonadEquiv c v d (WriterT w m) # |
Methods equivalent :: v -> v -> WriterT w m Bool # classDesc :: v -> WriterT w m d # equateAll :: [v] -> WriterT w m () # equate :: v -> v -> WriterT w m () # removeClass :: v -> WriterT w m Bool # getClass :: v -> WriterT w m c # combineAll :: [c] -> WriterT w m () # combine :: c -> c -> WriterT w m c # (===) :: c -> c -> WriterT w m Bool # desc :: c -> WriterT w m d # remove :: c -> WriterT w m Bool #
MonadEquiv c v d m => MonadEquiv c v d (ReaderT * r m) # |
Methods equivalent :: v -> v -> ReaderT * r m Bool # classDesc :: v -> ReaderT * r m d # equateAll :: [v] -> ReaderT * r m () # equate :: v -> v -> ReaderT * r m () # removeClass :: v -> ReaderT * r m Bool # getClass :: v -> ReaderT * r m c # combineAll :: [c] -> ReaderT * r m () # combine :: c -> c -> ReaderT * r m c # (===) :: c -> c -> ReaderT * r m Bool # desc :: c -> ReaderT * r m d # remove :: c -> ReaderT * r m Bool #
(Monad m, Ord v) => MonadEquiv (Class s d v) v d (EquivT s d v m) # |
Methods equivalent :: v -> v -> EquivT s d v m Bool # classDesc :: v -> EquivT s d v m d # equateAll :: [v] -> EquivT s d v m () # equate :: v -> v -> EquivT s d v m () # removeClass :: v -> EquivT s d v m Bool # getClass :: v -> EquivT s d v m (Class s d v) # combineAll :: [Class s d v] -> EquivT s d v m () # combine :: Class s d v -> Class s d v -> EquivT s d v m (Class s d v) # (===) :: Class s d v -> Class s d v -> EquivT s d v m Bool # desc :: Class s d v -> EquivT s d v m d # remove :: Class s d v -> EquivT s d v m Bool #

newtype EquivT s c v m a #

This monad transformer encapsulates computations maintaining an equivalence relation. A monadic computation of type `EquivT` `s c v m a` maintains a state space indexed by type `s`, maintains an equivalence relation over elements of type `v` with equivalence class descriptors of type `c` and contains an internal monadic computation of type `m a`.

Constructors

EquivT |
Fields unEquivT :: ReaderT (Equiv s c v) (STT s m) a

Instances

MonadError e m => MonadError e (EquivT s c v m) # |
Methods throwError :: e -> EquivT s c v m a # catchError :: EquivT s c v m a -> (e -> EquivT s c v m a) -> EquivT s c v m a #
MonadReader r m => MonadReader r (EquivT s c v m) # |
Methods ask :: EquivT s c v m r # local :: (r -> r) -> EquivT s c v m a -> EquivT s c v m a # reader :: (r -> a) -> EquivT s c v m a #
MonadState st m => MonadState st (EquivT s c v m) # |
Methods get :: EquivT s c v m st # put :: st -> EquivT s c v m () # state :: (st -> (a, st)) -> EquivT s c v m a #
(Monoid w, MonadWriter w m) => MonadWriter w (EquivT s c v m) # |
Methods writer :: (a, w) -> EquivT s c v m a # tell :: w -> EquivT s c v m () # listen :: EquivT s c v m a -> EquivT s c v m (a, w) # pass :: EquivT s c v m (a, w -> w) -> EquivT s c v m a #
MonadTrans (EquivT s c v) # |
Methods lift :: Monad m => m a -> EquivT s c v m a #
(Monad m, Ord v) => MonadEquiv (Class s d v) v d (EquivT s d v m) # |
Methods equivalent :: v -> v -> EquivT s d v m Bool # classDesc :: v -> EquivT s d v m d # equateAll :: [v] -> EquivT s d v m () # equate :: v -> v -> EquivT s d v m () # removeClass :: v -> EquivT s d v m Bool # getClass :: v -> EquivT s d v m (Class s d v) # combineAll :: [Class s d v] -> EquivT s d v m () # combine :: Class s d v -> Class s d v -> EquivT s d v m (Class s d v) # (===) :: Class s d v -> Class s d v -> EquivT s d v m Bool # desc :: Class s d v -> EquivT s d v m d # remove :: Class s d v -> EquivT s d v m Bool #
Monad m => Monad (EquivT s c v m) # |
Methods (>>=) :: EquivT s c v m a -> (a -> EquivT s c v m b) -> EquivT s c v m b # (>>) :: EquivT s c v m a -> EquivT s c v m b -> EquivT s c v m b # return :: a -> EquivT s c v m a # fail :: String -> EquivT s c v m a #
Functor m => Functor (EquivT s c v m) # |
Methods fmap :: (a -> b) -> EquivT s c v m a -> EquivT s c v m b # (<$) :: a -> EquivT s c v m b -> EquivT s c v m a #
(Functor m, Monad m) => Applicative (EquivT s c v m) # |
Methods pure :: a -> EquivT s c v m a # (<*>) :: EquivT s c v m (a -> b) -> EquivT s c v m a -> EquivT s c v m b # (*>) :: EquivT s c v m a -> EquivT s c v m b -> EquivT s c v m b # (<*) :: EquivT s c v m a -> EquivT s c v m b -> EquivT s c v m a #

type EquivT' s = EquivT s () #

This monad transformer is a special case of `EquivT` that only maintains trivial equivalence class descriptors of type `()`.

type EquivM s c v = EquivT s c v Identity #

This monad encapsulates computations maintaining an equivalence relation. A monadic computation of type `EquivM` `s c v a` maintains a state space indexed by type `s`, maintains an equivalence relation over elements of type `v` with equivalence class descriptors of type `c` and returns a value of type `a`.

type EquivM' s v = EquivM s () v #

This monad is a special case of `EquivM` that only maintains trivial equivalence class descriptors of type `()`.

runEquivT #

Arguments

:: Monad m |
=> (v -> c) | used to construct an equivalence class descriptor for a singleton class
-> (c -> c -> c) | used to combine the equivalence class descriptor of two classes which are meant to be combined.
-> (forall s. EquivT s c v m a) |
-> m a |

This function runs a monadic computation that maintains an equivalence relation. The first tow arguments specify how to construct an equivalence class descriptor for a singleton class and how to combine two equivalence class descriptors.

runEquivT' :: Monad m => (forall s. EquivT' s v m a) -> m a #

This function is a special case of `runEquivT` that only maintains trivial equivalence class descriptors of type `()`.

runEquivM #

Arguments

:: (v -> c) | used to construct an equivalence class descriptor for a singleton class
-> (c -> c -> c) | used to combine the equivalence class descriptor of two classes which are meant to be combined.
-> (forall s. EquivM s c v a) |
-> a |

This function runs a monadic computation that maintains an equivalence relation. The first tow arguments specify how to construct an equivalence class descriptor for a singleton class and how to combine two equivalence class descriptors.

runEquivM' :: (forall s. EquivM' s v a) -> a #

This function is a special case of `runEquivM` that only maintains trivial equivalence class descriptors of type `()`.