title: Scheme: Compiling with continuations
body: <p>im currently writing a compiler in OCaml for a subset of scheme and am having trouble understanding how to compile with continuations. I found some great resources, namely:</p>
  
  <ul>
  <li>The cps slides of the cmsu compiler course:<br>
  <a href="https://www.cs.umd.edu/class/fall2017/cmsc430/" rel="nofollow noreferrer">https://www.cs.umd.edu/class/fall2017/cmsc430/</a></li>
  <li>This explanation of    another cs course:<br>
  <a href="https://www.cs.utah.edu/~mflatt/past-courses/cs6520/public_html/s02/cps.pdf" rel="nofollow noreferrer">https://www.cs.utah.edu/~mflatt/past-courses/cs6520/public_html/s02/cps.pdf</a></li>
  <li>Matt Mights posts on a-normal form and cps:<br>
  <a href="http://matt.might.net/articles/a-normalization/" rel="nofollow noreferrer">http://matt.might.net/articles/a-normalization/</a> and<br>
  <a href="http://matt.might.net/articles/cps-conversion/" rel="nofollow noreferrer">http://matt.might.net/articles/cps-conversion/</a></li>
  </ul>
  
  <p>Using the anormal transformation introduced in the anormal-paper, I now have code where function calls are either bound to a variable or returned.</p>
  
  <p>Example:</p>
  
  <pre><code>(define (fib n)
    (if (&lt;= n 1)
        n
        (+ (fib (- n 1)) 
           (fib (- n 2)))))
  </code></pre>
  
  <p>becomes:</p>
  
  <pre><code>(define (fib n)
    (let ([c (&lt;= n 1)])
      (if c
          n
          (let ([n-1 (- n 1)])
            (let ([v0 (fib n-1)])
               (let ([n-2 (- n 2)])
                 (let ([v1 (fib n-2)])
                   (+ v0 v1)))))))
  </code></pre>
  
  <p>In order to cps-transform, I now have to:</p>
  
  <ol>
  <li>add cont-parameters to all non-primitive functions</li>
  <li>call the cont-parameter on tail-positions </li>
  <li>transform all non-primitive function calls, so that they escape the let-binding and become an extra lambda with the previous let-bound variable as sole argument and the previous let-body
  as the body</li>
  </ol>
  
  <p>The result would look like:</p>
  
  <pre><code>(define (fib n k)
    (let ([c (&lt;= n 1)])
      (if c
          (k n)
          (let ([n-1 (- n 1)])
            (fib n-1 
              (lambda (v0) 
                (let ([n-2 (- n 2)]) 
                  (fib n-2
                    (lambda (v1) 
                      (k (+ v0 v1))))))))))
  </code></pre>
  
  <p>Is this correct?</p>
  
  <p>The csmu-course also talks about how Programs in CPS require no stack and never return. Is that because we don't need to to save the adresses to return to and closures as well as other datatypes are stored on the heap and references are kept alive by using the closures? </p>
  
  <p>The csmu also talks about desugaring of call/cc:</p>
  
  <pre><code>(call/cc) =&gt; ((lambda (k f) (f k k)))
  </code></pre>
  
  <p>when using such desugaring, how does:</p>
  
  <pre><code>(+ 2 (call/cc (lambda (k) (k 2))))
  </code></pre>
  
  <p>in MIT-Scheme return 4, since the current continuation would probably be something like display?</p>