Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Continuation-passing style
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===CPS in Haskell=== A function <code>pyth</code> to calculate a hypotenuse using the [[Pythagorean theorem]] can be written in [[Haskell]]. A traditional implementation of the <code>pyth</code> function looks like this: <syntaxhighlight lang="haskell"> pow2 :: Float -> Float pow2 x = x ** 2 add :: Float -> Float -> Float add x y = x + y pyth :: Float -> Float -> Float pyth x y = sqrt (add (pow2 x) (pow2 y)) </syntaxhighlight> To transform the traditional function to CPS, its signature must be changed. The function will get another argument of function type, and its return type depends on that function: <syntaxhighlight lang="haskell"> pow2' :: Float -> (Float -> a) -> a pow2' x cont = cont (x ** 2) add' :: Float -> Float -> (Float -> a) -> a add' x y cont = cont (x + y) -- Types a -> (b -> c) and a -> b -> c are equivalent, so CPS function -- may be viewed as a higher order function sqrt' :: Float -> ((Float -> a) -> a) sqrt' x = \cont -> cont (sqrt x) pyth' :: Float -> Float -> (Float -> a) -> a pyth' x y cont = pow2' x (\x2 -> pow2' y (\y2 -> add' x2 y2 (\anb -> sqrt' anb cont))) </syntaxhighlight> First we calculate the square of ''a'' in <code>pyth'</code> function and pass a lambda function as a continuation which will accept a square of ''a'' as a first argument. And so on until the result of the calculations are reached. To get the result of this function we can pass <code>id</code> function as a final argument which returns the value that was passed to it unchanged: <code>pyth' 3 4 id == 5.0</code>. The mtl [[Library (computing)|library]], which is shipped with [[Glasgow Haskell Compiler]] (GHC), has the module <code>Control.Monad.Cont</code>. This module provides the Cont type, which implements Monad and some other useful functions. The following snippet shows the <code>pyth'</code> function using Cont: <syntaxhighlight lang="haskell"> pow2_m :: Float -> Cont a Float pow2_m a = return (a ** 2) pyth_m :: Float -> Float -> Cont a Float pyth_m a b = do a2 <- pow2_m a b2 <- pow2_m b anb <- cont (add' a2 b2) r <- cont (sqrt' anb) return r </syntaxhighlight> Not only has the syntax become cleaner, but this type allows us to use a function <code>callCC</code> with type <code>MonadCont m => ((a -> m b) -> m a) -> m a</code>. This function has one argument of a function type; that function argument accepts the function too, which discards all computations going after its call. For example, let's break the execution of the <code>pyth</code> function if at least one of its arguments is negative returning zero: <syntaxhighlight lang="haskell"> pyth_m :: Float -> Float -> Cont a Float pyth_m a b = callCC $ \exitF -> do -- $ sign helps to avoid parentheses: a $ b + c == a (b + c) when (b < 0 || a < 0) (exitF 0.0) -- when :: Applicative f => Bool -> f () -> f () a2 <- pow2_m a b2 <- pow2_m b anb <- cont (add' a2 b2) r <- cont (sqrt' anb) return r </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)