Concepts+Techniques+and+Models+of+Computer+Programming_Part4

# Concepts+Techniques+and+Models+of+Computer+Programming_Part4...

This preview shows pages 1–3. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 48 Declarative Computation Model x 1 x 2 person 25 "George" name age "X" "Y" Inside the store Figure 2.13: A partial value with no unbound variables, i.e., a complete value x 1 "X" "Y" x 2 Inside the store Figure 2.14: Two variables bound together be bound to several partial values, as long as they are compatible with each other. We say a set of partial values is compatible if the unbound variables in them can be bound in such a way as to make them all equal. For example, person(age:25) and person(age: x ) are compatible (because x can be bound to 25), but person(age:25) and person(age:26) are not. 2.2.7 Variable-variable binding Variables can be bound to variables. For example, consider two unbound variables x 1 and x 2 referred to by the identifiers X and Y . After doing the bind X=Y , we get the situation in Figure 2.14. The two variables x 1 and x 2 are equal to each other. The figure shows this by letting each variable refer to the other. We say that { x 1 , x 2 } form an equivalence set . 4 We also write this as x 1 = x 2 . Three variables that are bound together are written as x 1 = x 2 = x 3 or { x 1 , x 2 , x 3 } . Drawn in a figure, these variables would form a circular chain. Whenever one variable in an equivalence set is bound, then all variables see the binding. Figure 2.15 shows the result of doing X=[1 2 3] . 4 From a formal viewpoint, the two variables form an equivalence class with respect to equal- ity. Copyright c 2001-3 by P. Van Roy and S. Haridi. All rights reserved. 2.2 The single-assignment store 49 x 1 1 2 3 nil Inside the store "X" "Y" x 2 Figure 2.15: The store after binding one of the variables 2.2.8 Dataflow variables In the declarative model, creating a variable and binding it are done separately. What happens if we try to use the variable before it is bound? We call this a variable use error . Some languages create and bind variables in one step, so that use errors cannot occur. This is the case for functional programming languages. Other languages allow creating and binding to be separate. Then we have the following possibilities when there is a use error: 1. Execution continues and no error message is given. The variable’s content is undefined, i.e. it is “garbage”: whatever is found in memory. This is what C++ does. 2. Execution continues and no error message is given. The variable is initial- ized to a default value when it is declared, e.g., to 0 for an integer. This is what Java does. 3. Execution stops with an error message (or an exception is raised). This is what Prolog does for arithmetic operations. 4. Execution waits until the variable is bound and then continues. These cases are listed in increasing order of niceness. The first case is very bad, since different executions of the same program can give different results. What’s more, since the existence of the error is not signaled, the programmer is not even aware when this happens. The second is somewhat better. If the program has a use error, then at least it will always give the same result, even if it is a wrong...
View Full Document

{[ snackBarMessage ]}

### Page1 / 30

Concepts+Techniques+and+Models+of+Computer+Programming_Part4...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online