# Ticket #1169: Class.hs

File Class.hs, 3.1 KB (added by guest, 9 years ago) |
---|

Line | |
---|---|

1 | {-# OPTIONS -fallow-undecidable-instances #-} |

2 | -- Search for -fallow-undecidable-instances to see why this is needed |

3 | |

4 | {- | |

5 | Module : Control.Monad.Cont.Class |

6 | Copyright : (c) The University of Glasgow 2001, |

7 | (c) Jeff Newbern 2003-2007, |

8 | (c) Andriy Palamarchuk 2007 |

9 | License : BSD-style (see the file libraries/base/LICENSE) |

10 | |

11 | Maintainer : libraries@haskell.org |

12 | Stability : experimental |

13 | Portability : non-portable (multi-parameter type classes) |

14 | |

15 | [Computation type:] Computations which can be interrupted and resumed. |

16 | |

17 | [Binding strategy:] Binding a function to a monadic value creates |

18 | a new continuation which uses the function as the continuation of the monadic |

19 | computation. |

20 | |

21 | [Useful for:] Complex control structures, error handling, |

22 | and creating co-routines. |

23 | |

24 | [Zero and plus:] None. |

25 | |

26 | [Example type:] @'Cont' r a@ |

27 | |

28 | The Continuation monad represents computations in continuation-passing style |

29 | (CPS). |

30 | In continuation-passing style function result is not returned, |

31 | but instead is passed to another function, |

32 | received as a parameter (continuation). |

33 | Computations are built up from sequences |

34 | of nested continuations, terminated by a final continuation (often @id@) |

35 | which produces the final result. |

36 | Since continuations are functions which represent the future of a computation, |

37 | manipulation of the continuation functions can achieve complex manipulations |

38 | of the future of the computation, |

39 | such as interrupting a computation in the middle, aborting a portion |

40 | of a computation, restarting a computation, and interleaving execution of |

41 | computations. |

42 | The Continuation monad adapts CPS to the structure of a monad. |

43 | |

44 | Before using the Continuation monad, be sure that you have |

45 | a firm understanding of continuation-passing style |

46 | and that continuations represent the best solution to your particular |

47 | design problem. |

48 | Many algorithms which require continuations in other languages do not require |

49 | them in Haskell, due to Haskell's lazy semantics. |

50 | Abuse of the Continuation monad can produce code that is impossible |

51 | to understand and maintain. |

52 | -} |

53 | |

54 | module Control.Monad.Cont.Class ( |

55 | MonadCont(..), |

56 | ) where |

57 | |

58 | class (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"). |

71 | |

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 |

78 |