patternMinor
How would I write an if-statement in continuation passing style without an if-statement?
Viewed 0 times
withoutcontinuationstatementpassingwritewouldstylehow
Problem
Using continuation passing style, many common language constructs can be written in a simple language with recursion, including loops (using trampolining), exceptions, generators and even function returns. I have found numerous examples of other language constructs, but all examples I have seen use if-statements directly. Noting the power of CPS, I got curious: Can one write an if-statement in CPS without using an if-statement directly?
The naive attempt would be something along these lines:
but obviously this is a failure, since I used an if-statement directly.
The naive attempt would be something along these lines:
if_cps(condition, true_body, false_body, current_continuation)
{
if(condition) true_body(current_continuation);
else false_body(current_continuation);
}but obviously this is a failure, since I used an if-statement directly.
Solution
I think you are over-attributing things to CPS. First, CPS lets you model exceptions, generators, and functional calls, but it doesn't create them. For example, modeling exceptions with CPS only gives me exception-like behavior in code that is itself in CPS. I can, of course, use CPS as an implementation mechanism, an intermediate language, for exceptions or generators. Compare this to a language with first-class continuations, e.g. Scheme's
Second, recursion gives you loops, not CPS. If you had a language which couldn't express recursion nor had built-in loops, CPS transforming would clearly not change this. You also don't need CPS to express loops as recursion, though it may be useful for things like
To address your specific question, in some sense
call-with-current-continuation. With that, I could implement exceptions as a library.Second, recursion gives you loops, not CPS. If you had a language which couldn't express recursion nor had built-in loops, CPS transforming would clearly not change this. You also don't need CPS to express loops as recursion, though it may be useful for things like
break and continue.To address your specific question, in some sense
if is part of the definition of booleans. If you want to do anything non-trivial with a boolean, you must at some point use if. You could choose a different, non-abstract representation of booleans via their Church encoding, but converting the "native" booleans to this representation would require if, and at any rate this would just be word play. While our if-cps would not require using if in this representation, this would only be because if is represented by the identity function in this representation. Or to put it another way, we are using this representation's if.Context
StackExchange Computer Science Q#54803, answer score: 4
Revisions (0)
No revisions yet.