CS 1 Fall 2007

Big Idea

The Substitution Model

Wednesday, October 3, 2007


In order to evaluate a mathematical expression, we need to have a precise model for establishing what the meaning of the expression is. In algebra, we know that a symbol (e.g. x) can be used to refer to a value (e.g. the number 3). Once this relationship between a symbol and a value has been established, we can evaluate any expression containing the symbol by substituting the value for the symbol in the expression everywhere it occurs. Once all possible such substitutions have been done, what remains is an expression containing only primitive operations which can be evaluated according to the rules of arithmetic. Similarly, we can evaluate a function at particular values of its arguments by substituting the values passed in as arguments to the function for the corresponding symbols that are used to refer to those arguments, and then evaluating the resulting expression.

The simplest model for evaluating computer programs works exactly the same way. If an expression contains only numbers and primitive operators (like +, -, * etc.), it can be evaluated directly using the rules of arithmetic. If an expression contains symbols that have been associated with numbers, then the expression can be evaluated by substituting the value (number) corresponding to each symbol for that symbol in the expression, and repeating until the expression contains only numbers and primitive operators. This can then be evaluated by using the rules of arithmetic. Functions (also known as procedures) can be evaluated at particular values of their arguments by substituting the values passed in as arguments to the function for the corresponding symbols that are used to refer to those arguments, and then evaluating the resulting expression, just as we do in mathematics.

This model is powerful enough to permit us to perform many significant computations. In order for the model to be useful as a way of understanding programs, we have to be able to mechanically evaluate expressions by hand all the way until we get an answer (a job that normally the computer will do for us). Although this process is tedious to do by hand, it illustrates that there is no magic to how computers work. The evaluation of a computer program is a completely mechanical process, and evaluators of computer programs are themselves computer programs, a profound concept that you will encounter again and again as you learn more about computer science.