Com S 541 Lecture -*- Outline -*- * stateful model (6.3) state is a pair of an identity and a value ** cells (6.3.1) *** syntax ------------------------------------------ CELLS ::= ... | {NewCell X C} | {Exchange C X Y} Statement Sugars: X = C:=Y ==> {Exchange C X Y} C := Y ==> _ := C:= Y X = @C ==> Expression Sugars {NewCell X} ==> local C in {NewCell X C} C end {Exchange C X} ==> local Y in {Exchange C X Y} Y end @C ==> local X in X = @C X end ------------------------------------------ Desugaring for @C ... X = C := X Adds the above to the declarative model with exceptions and security (NewName, !!). This model doesn't include threads. Q: What does {NewCell X C} do? creates a new cell with initial content X Q: What does {Exchange C X Y} do? atomically binds X to @C, and makes Y the new content of C Q: What does @C do? gets the value of cell C, synonym for Cell.access Q: How does the desugaring for X = @C work? If X is unbound, it's value is set to the contents of C, and C is set to it. *** semantics ------------------------------------------ RECALL MUTABLE STORE DOMAIN m in MutableStore = Variable -> Variable Operations: {}: MutableStore update: MutableStore -> Variable x Variable -> MutableStore lookup: MutableStore x Variable -> Variable lookup(update(m)(y,x), z) = if z == y then x else lookup(m,z) Notation: write x:y for a binding in MutableStore typical m is {p1:s1, p2:s2} update({p1:s1})(p2, s2) = {p1:s1, p2:s2} lookup({p1:s1, p2:s2}, p1) = s1 ------------------------------------------ Now we can think of the pairs x:y in the mutable store as cells. ------------------------------------------ SEMANTICS Sequential (-d->) configurations: (ST,s,m) in State = Stack x Store x MutableStore + Message Message = String Stack = ( x Environment)* Store = Variable -> Value T = Message + { (nil,s,m) | s in Store, m in MutableStore} [NewCell call] (({NewCell X Y},E)|Rest, s, m) -d-> (Rest, s', m') where n is a cell name and n not in range(s) and unbound(s, E(Y)) and s' = bind(s)(s(E(Y)), n) and m' = update(m)(E(Y), E(X)) [Exchange call] (({Exchange X Y Z},E)|Rest, s, m) -d-> (Rest, s', m') where determined(s, E(X)) and s(E(X)) is a cell name and w = lookup(m)(E(X)) and m' = update(m)(E(X), E(Z)) and s' = bind(s)(s(E(Y)), w) ------------------------------------------ Q: Why is the update in [NewCell call] update(m)(E(Y), E(X)), and not update(m)(E(X), E(Y))? Because Y is the cell Q: What should happen if determined(s, E(Y))? raise an error condition Q: What's different between [NewCell call] and [NewPort call]? the type of the name Q: Why make a difference? So that you can only use Send with ports and Exchange with cells. Q: In what sense is Exchange atomic? If you think of the concurrent semantics, there isn't a step that can get in between Q: In the [Exchange call] rule, is it okay if Y or Z is unbound? Yes Q: What should happen if E(X) is unbound in the Exchange call rule? Suspends Q: What should happen if E(X) is bound to a port name in the Exchange call rule? raise an error condition. Q: How should the memory management (gc) rules be changed? same as we did for ports (so in essence no change from that) need to say that a variable y is reachable if x is reachable and store has cell x:y So if x is unreachable and mutable store has x:y, then remove x:y. ------------------------------------------ EXAMPLE % File ModelTest.oz local C One Z OldC in {NewCell One C} One = 1 {Exchange C OldC Z} end ------------------------------------------ *** relation to declarative programming (6.3.3) Q: Is a stateful program declarative? No, doesn't have referential transparency Q: Can we write programs in the stateful model that act declaratively? Yes, e.g., SumList They say it's a good goal. Q: Can you write an iterative version of Reverse of a List using a cell? Is your answer declarative? (See Reverse.oz) *** sharing and equality (6.3.4) **** sharing or aliasing ------------------------------------------ SHARING OR ALIASING def: Two identifiers are *aliases* if they refer to the same cell Example: declare X = {NewCell 0} Y = X Y := 99 {Browse @X} ------------------------------------------ File is AliasingExample.oz Q: Is aliasing a good thing? Yes, needed in many situations. No, can be hard to reason about if not controlled by encapsulation How? Using ADTs ------------------------------------------ STRUCTURAL VS TOKEN EQUALITY declare X = lang(name: "oz") Y = lang(name: "oz") CX = {NewCell X} CY = {NewCell Y} {Browse @CX == @CY} {Browse CX == CY} ------------------------------------------ File: Equality.oz Q: What does this display? Why? true and then false Because, cells are only the same if they are at the same location, coincidence of contents is temporary.