HaskellWiki

Haskell | Wiki community | Recent changes
Random page | Special pages

 

Not logged in
Log in | Help

Note: new account creation has been disabled as an anti-spam measure.

Continuation

Categories: Idioms | Glossary

Contents


1 General or introductory materials

1.1 Powerful metaphors, images

Here is a collection of short descriptions, analogies or metaphors, that illustrate this difficult concept, or an aspect of it.

1.1.1 Imperative metaphors

1.1.2 Functional metaphors

1.2 External links

2 Examples

2.1 Citing haskellized Scheme examples from Wikipedia

Quoting the Scheme examples (with their explanatory texts) from Wikipedia's Continuation-passing style article, but Scheme examples are translated to Haskell, and some straightforward modifications are made to the explanations (e.g. replacing word Scheme with Haskell, or using abbreviated name fac instead of factorial).

In the Haskell programming language, the simplest of direct-style functions is the identity function:

 id :: a -> a
 id a = a

which in CPS becomes:

 idCPS :: a -> (a -> r) -> r
 idCPS a ret = ret a

where ret is the continuation argument (often also called k). A further comparison of direct and CPS style is below.

Direct style
Continuation passing style
 mysqrt :: Floating a => a -> a
 mysqrt a = sqrt a
 print (mysqrt 4) :: IO ()
 mysqrtCPS :: a -> (a -> r) -> r
 mysqrtCPS a k = k (sqrt a)
 mysqrtCPS 4 print :: IO ()
 mysqrt 4 + 2 :: Floating a => a
 mysqrtCPS 4 (+ 2) :: Floating a => a
 fac :: Integral a => a -> a
 fac 0 = 1
 fac n'@(n + 1) = n' * fac n
 fac 4 + 2 :: Integral a => a
 facCPS :: a -> (a -> r) -> r
 facCPS 0 k = k 1
 facCPS n'@(n + 1) k = facCPS n $ \ret -> k (n' * ret)
 facCPS 4 (+ 2) :: Integral a => a

The translations shown above show that CPS is a global transformation; the direct-style factorial, fac takes, as might be expected, a single argument. The CPS factorial, facCPS takes two: the argument and a continuation. Any function calling a CPS-ed function must either provide a new continuation or pass its own; any calls from a CPS-ed function to a non-CPS function will use implicit continuations. Thus, to ensure the total absence of a function stack, the entire program must be in CPS.

As an exception, mysqrt calls sqrt without a continuation — here sqrt is considered a primitive operator; that is, it is assumed that sqrt will compute its result in finite time and without abusing the stack. Operations considered primitive for CPS tend to be arithmetic, constructors, accessors, or mutators; any O(1) operation will be considered primitive.

The quotation ends here.

2.2 Intermediate structures

The function Foreign.C.String.withCString converts a Haskell string to a C string. But it does not provide it for eternal use but restricts the use of the C string to a sub-procedure, because it will cleanup the C string after its use. It has signature withCString :: String -> (CString -> IO a) -> IO a. This looks like continuation and the functions from continuation monad can be used, e.g. for allocation of a whole array of pointers:

multiCont :: [(r -> a) -> a] -> ([r] -> a) -> a
multiCont xs = runCont (sequence (map Cont xs))
 
withCStringArray0 :: [String] -> (Ptr CString -> IO a) -> IO a
withCStringArray0 strings act =
   multiCont
      (map withCString strings)
      (\rs -> withArray0 nullPtr rs act)

However, the right associativity of sequence leads to inefficiencies here.

See:

2.3 More general examples

Maybe it is confusing, that

coincide in the above examples. It is not a necessity (it does not belong to the essence of the continuation concept), so I try to figure out an example which avoids this confusing coincidence:

 newSentence :: Char -> Bool
 newSentence = flip elem ".?!"
 
 newSentenceCPS :: Char -> (Bool -> r) -> r
 newSentenceCPS c k = k (elem c ".?!")

but this is a rather uninteresting example. Let us see another one that uses at least recursion:

 mylength :: [a] -> Integer
 mylength [] = 0
 mylength (_ : as) = succ (mylength as)
 
 mylengthCPS :: [a] -> (Integer -> r) -> r
 mylengthCPS [] k = k 0
 mylengthCPS (_ : as) k = mylengthCPS as (k . succ)
 
 test8 :: Integer
 test8 = mylengthCPS [1..2006] id
 
 test9 :: IO ()
 test9 = mylengthCPS [1..2006] print

You can download the Haskell source code (the original examples plus the new ones): Continuation.hs.

3 Continuation monad

4 Delimited continuation

5 Linguistics

Chris Barker: Continuations in Natural Language

6 Applications

ZipperFS
Oleg Kiselyov's zipper-based file server/OS where threading and exceptions are all realized via delimited continuations.

Retrieved from "http://haskell.org/haskellwiki/Continuation"

This page has been accessed 10,734 times. This page was last modified 21:08, 5 December 2008. Recent content is available under a simple permissive license.