b. The abstract machine (2.4.2) (skip) i. little step semantics in general ------------------------------------------ COMPUTATION (LITTLE STEP) SEMANTICS Meaning Programs <-------> Answers | ^ input | | output | | v -->* | State ------------> T def: a is in Meaning[[P]] iff there is a g in T such that input[[P]] -->* g and output(g) = a ------------------------------------------ ii. terminal transition systems ------------------------------------------ TERMINAL TRANSITION SYSTEM (TTS) (State, -->, T) State: a set of configurations -->: transition relation, a binary relation on State (also written ==>) T: subset of State, with terminal configrations, must be such that if g in T then there is no g' such that g --> g' -->* reflexive, transitive closure of --> ------------------------------------------ What are the possible outcomes for a program? a set of terminal states an infinite computation getting stuck (a config g, such that g not in T but there is no g' such that g --> g') iii. TTS for Oz ------------------------------------------ TTS FOR OZ (ST,s) in State = Stack x Store + Message Stack = ( x Environment)* T = {(nil,s) | s in Store} + Message Message = String input[[S]] = ((S,{})|nil, {}) output((nil,s)) = s output(Msg) = Msg ------------------------------------------ What states are terminal? How should we define the transitions (-->)? ------------------------------------------ TRANSITIONS (-->) [skip] ((skip, E) | Rest, s) --> (Rest, s) [sequence] ((S1 S2, E) | Rest, s) --> ((S1, E) | (S2, E) | Rest, s) [local] ((local X in S end,E)|Rest, s) --> ((S,E')|Rest, s') where E' = E+{X-->x} and x#s' = alloc(s) [var-var binding] ((X=Y, E) | Rest, s) --> (Rest, s') where s' = unify(s)(E(X), E(Y)) and isStore(s') [var-var bindingerror] ((X=Y, E) | Rest, s) --> ((raise failure(Msg) end, E) | Rest, s') where (Msg,s') = unify(s)(E(X), E(Y)) [value creation] ((X=V, E) | Rest, s) --> (Rest, s3) where y#s' = alloc(s) and v = MV[[V]](E) and s'' = bind(s')({y},v) and s3 = unify(s'')(E(X),y) and isStore(s3) [value creation error] ((X=V, E) | Rest, s) --> ((raise failure(Msg) end, E) | Rest, s3) where y#s' = alloc(s) and v = MV[[V]](E) and s'' = bind(s')({y},v) and (Msg, s3) = unify(s'')(E(X),y) [if-true] ((if X then S1 else S2 end,E)|Rest, s) --> ((S1, E)|Rest, s) where determined(s, E(X)) and s(E(X)) == true [if-false] ((if X then S1 else S2 end, E)|Rest, s) --> ((S2, E)|Rest, s) where determined(s, E(X)) and s(E(X)) == false [if-error] ((if X then S1 else S2 end, E)|Rest, s) --> ((raise error(...) end, E)|Rest, s) where determined(s, E(X)) and not(s(E(X)) == true) and not(s(E(X)) == false) [application] (({X Y1 ... Yn}, E)|Rest, s) --> ((Body, E')|Rest, s) where determined(s,E(X)) and s(E(X)) in Closure and [Z1 ... Zn] = args(s(E(X))) and Body = body(s(E(X))) and E' = env(s(E(X))) + {Z1 -->E(Y1)} + ... + {Zn -->E(Yn)} [application-error] (({X Y1 ... Yn}, E)|Rest, s) --> ((raise error(...),E) | Rest, s) where determined(s,E(X)) and not(s(E(X)) in Closure) or s(E(X)) does not have n arguments [case-match] ((case X of L(F1: X1 ... Fn:Xn) then S1 else S2 end, E)|Rest, s) --> ((S1, E') | Rest, s) where determined(s,E(X)) and isRecord(s(E(X))) and Label(s(E(X))) == L and Arity(s(E(X))) == [F1 ... Fn] and E' = E + {X1 -->s(E(X)).F1, ..., Xn -->s(E(X)).Fn} [case-else] ((case X of L(F1: X1 ... Fn:Xn) then S1 else S2 end, E)|Rest, s) --> ((S2, E) | Rest, s) where determined(s,E(X)) and not(isRecord(s(E(X)))) or not(Label(s(E(X))) == L) or not(Arity(s(E(X))) == [F1 ... Fn]) ------------------------------------------ What happens if one of the identifiers in var-var binding is not in the domain of the environment, E? What free identifiers are allowed in a program? What's the parameter passing mechanism? What happens to an if-statement when the condition's identifier denotes a location that is not determined? Can the matching case change the store? ------------------------------------------ MEANING OF VALUE EXPRESSIONS MV: ValueExpression -> Environment -> Value MV[[X]](E) = E(X), where X in MV[[N]](E) = N, where N in MV[[L]](E) = L, where L in MV[[L(F1:X1, ..., Fn:Xn)]](E) = L(F1: MV[[X1]](E), ..., Fn: MV[[X1]](E)), where L(F1:X1, ..., Fn:Xn) in MV[[proc {$ F1 ... Fn} Body end]](E) = (proc {$ F1 ... Fn} Body end, E|FVP), where FVP = FV(Body) \ {F1 ... Fn} is the set of free identifiers in the procedure ------------------------------------------ iv. Examples (2.4.5) ------------------------------------------ EXAMPLES let S1 = local X in S2 end let S2 = local R in X=2 R=X end Then ((S1,{})|nil), {}) --> ((S2, {X-->x1})|nil, {x1}) --> ((X=2 R=X, {X-->x1,R-->x2})|nil, {x1,x2}) --> ((X=2,{X-->x1,R-->x2})| (R=X,{X-->x1,R-->x2})|nil, {x1,x2}) --> ((R=X,{X-->x1,R-->x2})|nil, {x1=2,x2,x3=2}) --> (nil, {x1=2,x2=2,x3=2}) ------------------------------------------ Is this final state terminal? ------------------------------------------ ANOTHER EXAMPLE local X in local Y in Y = proc {$ ?R} R=X end X=true local X in X=false local Z in {Y Z} if Z then skip else Z=X end end end end end let S0 = local X in S1 end % statements S1 = local Y in S2 S3 S4 end S2 = Y = proc {$ ?R} R=X end S3 = X=true S4 = local X in S5 S6 end S5 = X=false S6 = local Z in {Y Z} S7 end S7 = if Z then skip else Z=X end E2 = {X-->x1, Y-->x2} % environments E3 = {X-->x5, Y-->x2} E4 = {X-->x5, Y-->x2, Z-->x6} C1 = (proc {$ ?R} R=X end, {X-->x1}) % a closure s5 = {x1=true,x2=C1,x3=C1,x4=true,x5} % some stores s6 = {x1=true,x2=C1,x3=C1,x4=true,x5,x6} s7 = {x1=true,x2=C1,x3=C1,x4=true,x5,x6=true} We calculate as follows... ((local X in S1 end,{})|nil, {}) --> ((local Y in S2 S3 S4 end,{X-->x1})|nil, {x1}) --> ((S2 S3 S4,{X-->x1,Y-->x2})|nil, {x1,x2}) --> ((Y = proc {$ ?R} R=X end,E2)|(S3 S4,E2)|nil, {x1,x2}) --> ((X=true,E2)|(local X in S5 S6 end,E2)|nil, {x1,x2=C1,x3=C1}) --> ((local X in S5 S6 end,E2)|nil, {x1=true,x2=C1,x3=C1,x4=true}) --> ((S5 S6,{X-->x5,Y-->x2})|nil, s5) --> ((local Z in {Y Z} S7 end,E3)|nil, s5) --> x6}> (({Y Z} S7,E4)|nil, s6) --> (({Y Z},E4)|(S7,E4)|nil, s6) --> ((R=X,{X-->x1,R-->x6})|(S7,E4)|nil, s6) --> ((if Z then skip else Z=X end,E4)|nil, s7) --> ((skip,E4)|nil, s7) --> (nil, s7) ------------------------------------------