CORNELL CS 611 - Lecture 13 Naming and State

Unformatted text preview:

CS611 Lecture 13 Naming and State 9/26/01Scribe: Sabina Petride Lecturer: Andrew Myers1 uF!We extend uF with ML-style reference cells:e ::= ...| ref e |!e | e1:= e2| e1; e2,where ref e creates a new location containing e,!e derefences expression e,ande1:= e2updates location e1with the value of e2.Noticethate1:= e2is special, since it has side-effects (also called mutations). So e1; e2evaluates e1with or without side-effects, and then evaluates e2.For example, consider the following expression:let x = ref 1 in(x := 2; !x)It creates a new location containing 1, then the location stores the value 2, and dereferencing returns 2.Using reference cells, we can model more complicated mutable structures, for example mutable arrays:let x = ref 1, ref 2, ref 0 in ....2 SOS for uF!Because of possible side-effects, expressions alone are not adequate configurations any more: we need a pairexpression-store: e, σ .IfLoc is a countable set of locations, then a store σ is a partial function that mapslocations to values. An evaluation relation is written as e, σ → e,σ, and a final configuration has the formv, σ.Since expressions in uF have no side-effects, the following inference rule shows how to lift uF evaluationrelation to the uF! evaluation relation:euF→ ee, σ → e,σ.Next we extend the notion of value such that locations l ∈ Loc are considered values, too:v ::= ...| l.We say that a program is well-formed if it does not contain any locations.Accordingly, we extend the evaluation contexts:C ::= ...| ref C |!C | C := e | v := C | C; e.The evaluation context definition enforces a strict left-to-right evaluation order on the := expression.This is important in order to retain the Church-Rosser property.We are now able to write down the SOS:l ∈ dom(σ)refv, σ → l,σ[l → v] !l,σ → σ(l ),σl := v, σ → #u, σ [l → v] v; e, σ → e, σ.Notice that the first rule has a side condition l ∈ dom(σ), ensuring that the newly allocated location l isnot previously bound in the store σ.We define the result of the := expression to be the unit value #u to reinforce the idea that this is anexpression evaluated for its side-effect.13 Translation to uFGiven an expression e in uF!, an environment ρ and a state σ, we define D [[ e]] ρσ to be the uF term thatevaluates e in ρ and σ to some value v and returns the pair v, σ ,whereσis the store after executing e.We will assume that the environment ρ and store σ are uF terms; initially, the environment is some ρ0andthe state is σ0, since it doesn’t matter what they are until we want to check errors.Before defining the translation, we introduce three functions we’ll make use of:• mallo c σ = l : returns the location l not allocated in σ• lo okup σl= σ(l): returns the value stored at location l in state σ• update σlv= σ[l → v]: the state is updated such that value v is stored at the location l.There are many possible implementations of these operations; we require only that they satisfy thefollowing specification (the operation allocated is needed to write the specification and to implement anerror-checking version of the semantics):lo okup(update(slv) l)=vlo okup(update(slv) l)=lookup(sl), where l = lallocated(malloc(σ) σ)=falseallocated(l update(σlv)) = trueallocated(lσ0)=falseupdate(update(σlv) lv)=update(update(σlv) lv), where l = lupdate(update(σlv) lv)=update(σlv).We now give the translation:(1) D[[ n]] ρσ = n, σ (2) D[[ x]] ρσ = ρ“x“,σ (3) D[[ if e0then e1else e2]] ρσ == let p0= D [[ e0]] ρσ inlet b = left p0inlet σ= right p0inif b then D[[ e1]] ρσelse D[[ e2]] ρσ(4)D[[ e1; e2]] ρσ = let p1= D[[ e1]] ρσ inlet σ= right p1inD[[ e2]] ρσ(5)D[[ ref e]] ρσ = let p0= D[[ e]] ρσ inlet v = left p0inlet σ= right p0inlet l = malloc σinl, updatestore σlv (6)D[[ ! e]] ρσ = let p0= D[[ e]] ρσ inlet v = left p0inlet σ= right p0inlookup σv, σ (7)D[[ e1:= e2]] ρσ = let p1= D[[ e1]] ρσ inlet l = left p1inσ= right p1in2let p2= D[[ e2]] ρσinlet v = left p2inlet σ= right p2in#u, updatestore σlv Some explanations are need. For example (1) should evaluate n in ρ and σ, which of course is n,andreturn the pair n, σ ; much in the same way, in (2) we should evaluate variable x in ρ and σ,whichisρ“x“,andreturnitinpairwithσ. The rest of the rules are recurrent: each time we take the translation of anexpression in ρ and σ and get a pair from which we extract the actual value and the new state and thenperform translations in the same environment ρ, but in the new state. Since the environment is not changed,these translation rules show the difference between environments and states.We said that environments and states are treated as uF terms; to give an example, rule (1) may berewritten as D[[ n]] = ( λρ(λσn, σ )).Not all the times we are interested in the actual value an expression evaluates to in ρ and σ; for examplein rule (4) we only need to translate e1and then make explicit the new state, required for the translationof e2.We must also pay attention to all the possible side-effects: in rule (5) e may have side effects, such thatwe do not actually create a new location and assign a value to it in the state σ where e is evaluated, but inthe state σresulted from the evaluation.The malloc function should not be mistaken for the similar function in C, since it just returns a locationnot allocated in the current state, and no updates are done; successive calls to malloc return the samelocation.Thinking about these rules, it becomes apparent that at any given point exactly one state is needed. So itis possible to have a single state, and having only one state at each time would avoid the problem of creatinga large number of states. However, there are language features like transactions that require duplication ofthe state, semantically at least.4 Mutable VariablesSuppose now that we want all variables to be mutable. We extend the uF expressions toe ::= ...| x := e | e1; e2.We can desugar this extended uF to uF! and let the translation of such an expression e to be M[[ e]]given by the following rules:(1)M[[ x]] = ! x(2)M[[ x := e]] = x := M[[ e]](3)M[[ let x = e1in e2]] = let x = ref M[[ e1]] in M[[ e2]](4)M[[ λx e]] = λx M[[ e]] = λxlet x = ref xin M[[


View Full Document

CORNELL CS 611 - Lecture 13 Naming and State

Download Lecture 13 Naming and State
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 13 Naming and State and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 13 Naming and State 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?