---
tags: PLT
---
# Reading "Introduction to Cont"
Try to force myself to focus on monochrom's [article][cont].
[cont]: http://www.vex.net/~trebla/haskell/cont.xhtml
> # Continuation-Passing Style
>
> The continuation-passing style means your program does not give its answer directly ("the direct style", what you've always done); instead, your program takes one more parameter, a function parameter k, and your program's last job is to call k with the answer. We say that k is the continuation, and this is why we say this style continuation-passing -- you user passes a continuation to your program. (And your program may be using a subprogram, and so you may be paasing some continuation to that, too.)
>
> If you ask what's the difference from callbacks? Right, same difference!
>
> What happened here was that this same idea was cool amnog researchers, who coined "continuation", before it was cool among programmers, who coined "callback".
same difference XDDD
> Here is a very boring sample (but least distraction) of defining programs that answer 1, showing both the direct style and the continuation-passing style.
>
> ```
> -- Direct style.
> one_ds :: Integer
> one_ds = 1
>
> -- Continuation-passing style.
> one_cps :: (Integer -> r) -> r
> one_cps = \k -> k 1
> -- Or, one k = k 1
> ```
寫成 `one k = k 1` 對我來說比較好懂,就像寫成 `function one (callback) { callback(1) }` 一樣。但我沒有想過連 constant 也能寫成 CPS 的樣子。
而且我沒有想過他傳回來的值應該是什麼,自然也不知道 type 是 `(Integer -> r) -> r` 。
> How about an actual function such that the answer varies with the input? Here is a squaring function in the continuation-passing style (though not completely):
>
> ```
> square_cps1 :: Integer -> (Integer -> r) -> r
> square_cps1 x = \k -> k (x * x)
> ```
變得更清楚了。
> Why I said not completely: See how I was using (*), a direct-style multiplier? Now, I am not saying that you must never mix the two styles; you mix them as you see fit. But I want to show you what things look like when you use a subprogram that has a continuation-passing interface, and one of the least-setup ways is to imagine that even some built-in arithmetic you need is in the continuation-passing style, like this:
>
> ```
> mul_cps :: Integer -> Integer -> (Integer -> r) -> r
> -- Specification: @mul_cps m n k@ calls k with the product of m and n.
> -- Imagine it's the built-in multiplier.
> -- But for the sake of experimenting, I make:
> mul_cps x y = \k -> k $! (x * y)
> -- The $! kills laziness and is probably a good idea for arithmetic.
> ```
>
> So what I need to do for squaring: I receive from my user a number x and a continuation k, and my job is to ensure that somehow k is called with the square of x. Ah! I will have `mul_cps` do both the multiplying and calling for me.
>
> ```
> square_cps2 :: Integer -> (Integer -> r) -> r
> square_cps2 x = \k -> mul_cps x x k
> ```
>
> That was the first example of you calling a subprogram (`mul_cps`) and passing a continuation to it. It was also a boring one in the sense that you just pass your user's k verbatim to the subprogram. Below is an example where you have to improvise a new continuation.
>
> Write a function to square the input and add one. Assume that the built-in adder is in only the continuation-passing style (but fortunately 1 is still provided directly rather than the zealotry of one_cps):
>
> ```
> add_cps :: Integer -> Integer -> (Integer -> r) -> r
> -- Specification: @add_cps m n k@ calls k with the sum of m and n.
> -- Imagine it's the built-in adder.
> -- But for the sake of experimenting, I make:
> add_cps x y = \k -> k $! (x + y)
> ```
>
> You can pass the user's number x twice to `mul_cps`, but what continuation should you pass? The continuation should be written by you, because you are the one saying you are not done yet, you still have to add one. (And then you are done and you are ready to call your user's continuation.) So, you write this continuation yourself to say: Take the received answer s (you know it's x^2), give it to `add_cps` along with 1 for adding, and the sum is to be sent to your user's k.
>
> ```
> squareplus1_cps :: Integer -> (Integer -> r) -> r
> squareplus1_cps x = \k -> mul_cps x x (\s -> add_cps s 1 k)
> ```
###### tags: note