CS 541 Lecture -*- Outline -*- * State and Mutation (Schmidt Ch. 5) ** Mutation changing the value of an object idea: reference object (cell, container, location) refers to some other object, reference may be altered *** the language FL! (ref E) makes a new reference (refers to result of E) (^ E) returns the value of the reference E (dereference) (:= E1 E2) alters the reference E1 to refer to E2's value (begin E1 E2) evaluates E1 and then E2, returns E2 (sequential) *** semantics (go over definition and look at) semantic domains: Proc, Reference, Value, Outcome, Store, Storable notes: stores map locations to storable-values * Tr the boolean tells whether that location is used valuation function signatures: valP, valE semantic clauses: P, new constructs what is the calling mechanism? *** Analysis **** Problems makes programs more sequential makes programs harder to reason about note that can't find value of expression without store no referential transparency (f(E) <> f(E)) **** What good is this? can communicate with arbitrary other parts of program can change small part of large data structure without making a copy, and without explicitly passing as argument e.g., databases, simulations, ... corresponds to von Neuman machines so efficient on them... **** What changes could be made? break store into pieces (arrays) (ref I E) (^ I E) makes it easier to tell that two things are different preventing side effects during expression evaluation use while loops instead of recursion (see SL) OR separate functions from procedures (as in Euclid) ** State *** Imperative language (SL) idea: program state is given by pc + memory use "if" and "while" for delayed evaluation *** Semantics look at semantics... the pc is maintained in the semantics... ** Combining procedures with imperatives (BS) like Pascal idea: program state is given by environment + pc + memory