Feedback Call by name: macros with renaming 

Forum: U Chicago, Com Sci 221, autumn 2000
Re: News Assignment #6, due Monday 13 November (Mike O'Donnell)
Re: Question Confused about Call by name (Gary Yeung)
Date: 2000, Nov 13
From: Mike O'Donnell <odonnell@cs.uchicago.edu>

Probably the easiest way to understand the effect of call by name is to think of it as a sort of sanitized macro processing.

  1. Rename any local variables in the procedure body so that they are different from all variables in the actual parameters.
  2. Substitute the text of each actual parameter for every occurrence of the corresponding formal parameter in the procedure body.
  3. Execute the procedure body with substitutions, interpreting each copy of an actual parameter according to the meanings of the names at the point of call.
Example:

procedure P(x, y);

  var i, j;

  i := i + 1;

  y := y + 1;

  x := y;

end P;

...

call P(A[i+k], k+2);

...
A, i, k are all global variables. Suppose that before the call to P, i=17 and k=30. First, rename the local copy of i in the body of P:

begin

  var m, j;

  m := m + 1;

  y := y + 1;

  x := y;

end;
Then substitute in the actual parameters:

begin

  var m, j;

  m := m + 1;

  k := k + 1;

  A[i+k] := k+2;

end;
Finally, execute the code above, as if it occurred at the location of the call. Afterwards, i=17, k=31, and A[48]=33. The change to the local value of m has no impact outside of the procedure body.

The few languages (particularly ALGOL) that offer call by name usually implement it by passing thunks---procedures that return the l values of the actual parameters.



Mike O'D.


to: "Call by name: macros with renaming"