title: Order of evaluation compound procedures metacircular 'apply'
body: <p>I'm studying <a href="https://mitpress.mit.edu/sites/default/files/sicp/full-text/book/book-Z-H-26.html#%25_sec_4.1.1" rel="nofollow noreferrer">SICP chapter 4</a> and its implementation of a metacircular Scheme evaluator. I'm having difficulties understanding how user defined procedures are handled by its <code>apply</code> procedure.</p>
  <p>The metacircular evaluator is  composed of two main procedures: <code>eval</code> and <code>apply</code>. The basic idea is to recursively apply <code>eval</code> until there's only self-evaluating expressions (like numbers and strings) or expressions with primitive procedures that can be handled directly by <code>apply</code>.</p>
  <p>The evaluator works following the <a href="https://mitpress.mit.edu/sites/default/files/sicp/full-text/book/book-Z-H-21.html#%25_sec_3.2" rel="nofollow noreferrer">environment model of evaluation</a>, where we bind variables to their associated values and create new frames every time <code>lambda</code> is invoked. Procedure definitions are handled this way. The procedure name is binded in the environment and when it's called, its body is evaluated in a new frame where the parameters have been binded to the arguments used to call it.</p>
  <p>This specific part is reflected in the following lines of the <code>apply</code> procedure:</p>
  <pre><code>(define (apply procedure arguments)
    (cond (...)
          ((compound-procedure? procedure)
           (eval-sequence
             <b>(procedure-body procedure)</b>
             (extend-environment
               (procedure-parameters procedure)
               arguments
               (procedure-environment procedure))))
          (...)</code></pre>
  <p>An user defined procedure is recognized by the <code>compound-procedure?</code> predicate. The <code>eval-sequence</code> procedure just evaluates <code>(procedure-body procedure)</code> and <code>(extend-environment ...)</code> sequentially and returns the value of the last expression.</p>
  <p><strong>My problem is</strong> that, as I understand it, <em>we should extend the environment first</em> and <em>only then</em> evaluate the body of the procedure:</p>
  <pre><code>         (eval-sequence
             (extend-environment
               (procedure-parameters procedure)
               arguments
               (procedure-environment procedure))
             <b>(procedure-body procedure)</b>)</code></pre>
  <p>For example in:</p>
  <pre><code>(define (square x) (* x x))
  (square 5)
  </code></pre>
  <p>The first line will bind <code>square</code> to a lambda (with its associated parameters and body). This binding will be recognized in the second line. Then I understand we create a new frame where <code>x = 5</code> and only then is the body of square executed. But this order seems to be inverted by the <code>apply</code> procedure, where the body of the procedure is evaluated before binding the parameters to the arguments.</p>
  <p>I'd really appreciate if someone could help me understand this issue.</p>