CPS 343/543 Lecture notes: Variable assignment



Coverage: [EOPL] §3.7 (pp. 98-107)


Basics

  • now:
    • denoted value = ref(expressed value)
    • expressed value = number + procval
    • similar to Scheme
  • references are called L-values
  • expressed values are known as R-values, based on the side of the assignment statement in which they appear
  • new production rule and constructor on p. 98
  • difference between binding and variable assignment
    • binding associates a name with a value
    • assignment changes the value of an existing binding
    • binding is all about the association of names to values
    • `assignment is all about the sharing of values between different procedures' [EOPL] p. 98


Illustrative example programs

  • program on p. 99
    • notice novel use of let is to simulate sequential execution
    • notice that even and odd share variable x
    • the procedures communicate by changing the state of x
    • x is a so-called global variable
  • alternative is to inter-procedure communication through parameter passing
    • consider the idea of redirecting standard I/O
    • print ("hello") vs. print (port, "hello")
    • latter may pass the port through a long sequence of additional procedures to eventually print the string to the port
    • use of a a global variable avoids the need to pass data through intermediate procedures
    • achieve less overhead by compromising protection
  • `another use of assignment is to create hidden state directly through the use of private variables,' [EOPL] p. 100 (e.g., see sample program on p. 100)
    • count is a private variable which keeps track of how many times g is called
    • this expression returns 3
  • in our language, `we will choose to create a new reference for each formal parameter at every procedure call' [EOPL] p. 100
    • this policy is known as call-by-value (or pass-by-value)' [EOPL] p.100
    • `when we assign to a formal parameter, the assignment is local to the procedure' [EOPL] p. 100
    • second example program on p. 100 illustrates the effect of passing parameters by value (i.e., here, as opposed to the previous example, x is not shared)


Reference data type

  • deref and setref!
  • deref is the analog of the * (star) in C when preceding a variable reference
  • however, dereferencing is implicit in our language akin to Scheme or Java, but in contrast to C
  • `we assume the familiar environment representation with a value vector in each rib' [EOPL] p. 100
  • `references will be elements of rib vectors, which are assignable using vector-set!' (a function built into Scheme) [EOPL] p. 100
  • see abstract-syntax implementation on p. 101
  • `we define deref and setref! in terms of primitive-deref and primitive-setref! because we will reuse the latter two procedures in our later implementations of references' [EOPL] p. 101



  • (regenerated from [EOPL] Fig. 3.13 on p. 101)


Environment revisited

  • to make use of references
  • `we assume that the denoted values in an environment are of the form Ref(X) for some X' [EOPL] p. 102
  • `the procedure apply-env-ref is similar to apply-env, except that when it finds the matching identifier, it returns the reference instead of its value' [EOPL] p. 102
  • then `the procedure apply-env and be defined in terms of apply-env-ref and deref' [EOPL] p. 102
  • see code in Fig. 3.14 on p. 102
  • augmented eval-expression procedure to accommodate varassign-exp variant of expression (see p. 102)
  • notice the use of (begin ...) syntactic form; built into Scheme to achieve sequential execution
  • notice that a value must be returned; typically that return value will be ignored (as in C), especially if assignment is used with a let expression to simulate sequential execution


References

    [EOPL] D.P. Friedman, M. Wand, and C.T. Haynes. Essentials of Programming Languages. MIT Press, Cambridge, MA, Second edition, 2001.

Return Home