Ticket #1169: Class.hs

File Class.hs, 3.1 KB (added by guest, 10 years ago)

Version 4 of the module source. Accomodated latest Cont changes.

1{-# OPTIONS -fallow-undecidable-instances #-}
2-- Search for -fallow-undecidable-instances to see why this is needed
4{- |
5Module      :  Control.Monad.Cont.Class
6Copyright   :  (c) The University of Glasgow 2001,
7               (c) Jeff Newbern 2003-2007,
8               (c) Andriy Palamarchuk 2007
9License     :  BSD-style (see the file libraries/base/LICENSE)
11Maintainer  :  libraries@haskell.org
12Stability   :  experimental
13Portability :  non-portable (multi-parameter type classes)
15[Computation type:] Computations which can be interrupted and resumed.
17[Binding strategy:] Binding a function to a monadic value creates
18a new continuation which uses the function as the continuation of the monadic
21[Useful for:] Complex control structures, error handling,
22and creating co-routines.
24[Zero and plus:] None.
26[Example type:] @'Cont' r a@
28The Continuation monad represents computations in continuation-passing style
30In continuation-passing style function result is not returned,
31but instead is passed to another function,
32received as a parameter (continuation).
33Computations are built up from sequences
34of nested continuations, terminated by a final continuation (often @id@)
35which produces the final result.
36Since continuations are functions which represent the future of a computation,
37manipulation of the continuation functions can achieve complex manipulations
38of the future of the computation,
39such as interrupting a computation in the middle, aborting a portion
40of a computation, restarting a computation, and interleaving execution of
42The Continuation monad adapts CPS to the structure of a monad.
44Before using the Continuation monad, be sure that you have
45a firm understanding of continuation-passing style
46and that continuations represent the best solution to your particular
47design problem.
48Many algorithms which require continuations in other languages do not require
49them in Haskell, due to Haskell's lazy semantics.
50Abuse of the Continuation monad can produce code that is impossible
51to understand and maintain.
54module Control.Monad.Cont.Class (
55    MonadCont(..),
56  ) where
58class (Monad m) => MonadCont m where
59    {- | @callCC@ (call-with-current-continuation)
60    calls a function with the current continuation as its argument.
61    Provides an escape continuation mechanism for use with Continuation monads.
62    Escape continuations allow to abort the current computation and return
63    a value immediately.
64    They achieve a similar effect to 'Control.Monad.Error.throwError'
65    and 'Control.Monad.Error.catchError'
66    within an 'Control.Monad.Error.Error' monad.
67    Advantage of this function over calling @return@ is that it makes
68    the continuation explicit,
69    allowing more flexibility and better control
70    (see examples in "Control.Monad.Cont").
72    The standard idiom used with @callCC@ is to provide a lambda-expression
73    to name the continuation. Then calling the named continuation anywhere
74    within its scope will escape from the computation,
75    even if it is many layers deep within nested computations.
76    -}
77    callCC :: ((a -> m b) -> m a) -> m a