




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





1. Introduction.
Here show, by way of example, the use of denotational semantics for the specification of semantics of programming languages.
In general, a denotational semantic description consists of three parts: a set of syntactic domains, a set of semantic domains, and a set of semantic functions. The semantic functions map syntactic domains into semantic domains. Typically the syntactic domains are AST’s; a semantic function might map an AST into a value (in a semantic domain), which "denotes" the meaning of the construct.
Example:
Suppose we were to give the denotational semantic description of a two-operand machine language, for a 16-bit machine with 8 registers. The syntactic domain might be the set of AST’s of the form < Opcode Operand Operand >. The semantic domains might as follows:
Register: {0,1,2,3,4,5,6,7} Value: (16-bit binary number) Address: (16-bit binary number) Memory: Address → Value RegValues: Register → Value
A semantic function might be EE (for EEvaluate):
EE: AST → (RegValues × Memory) → (RegValues × Memory)
EE takes an AST, then the current configuration of the machine, i.e. a (registers,memory) pair, and pro- duces a new (registers,memory) pair that shows the effect of the operation. For example, consider a "reg- ister-to-memory" add operation:
EE[+ r m] = λ (R,M). let Result = R r + M m in (R,( λ a.a eq m → Result | M a))
The meaning of an operation "+ r m", where r is a register and m is a memory address, is a function that:
1.- Takes a RegValues function R, and a Memory function M; 2.- Applies R to r (obtaining the contents of register r), applies M to m (obtaining the con- tents of address m), adds the two values, and calls the result "Result"; 3.- Returns a pair consisting of: a) The same RegValues fucntion R, and b) A new memory function that, when applied to m, returns Result, and behaves like M otherwise.
The "meaning" of the operation is evident in this description: register-to-memory operation "+" adds the contents of the given register and the given memory location. It also updates the given memory location with the result of the addition, and leaves the registers unchanged.
2. The "
" operator.
The "
" operator is defined as follows:
= λ f. λ g. λ x. f x eq error → error | g(f x)
i.e., "
" takes two functions, f and g, and yields a new function on x that returns g(f x) if (f x) does NOT
evaluate to error, and error if (f x) does evaluate to error. "
" simply serves as a convenient way to put
error checking in our denotational definition everywhere without laboriously specifying it ourselves every- where. In the denotational semantics description of Tiny we will be using "
" as an INFIX operator, writ-
ing f
g instead of
f g.
Thus
f
g =^ λ^ x. f x eq error^ →^ error | g(f x)
This is purely a syntactic convention of ours that turns out to make denotational semantics descriptions read easier. It allows a "left-to-right flow" reading of such descriptions, exemplified below. Consider an exam- ple where
f = λ y. 2/y g = λ z. z+
Then f
g =
λ x. ( λ y.2/y) x eq error → error | ( λ z.z+3) (( λ y.2/y)x) = λ x. 2/x eq error → error | 2/x+
Consider applying f
g to an actual argument, say, 4:
(f
g) 4 = 2/4 eq error → error | 2/4+3 = 2/4+3 = 3 1/2.
Now consider
(f
g) 0 = 2/0 eq error → error | 2/0+3 = error, since 2/0 eq error (division by zero).
It is to catch things like division by zero or undeclared identifiers that we use "
Now here’s the left-to-right bent for "
". Re-consider (f
g) 4. The computation can be depicted by
the diagram
Output: Val* Input: Val* Val: Num + Bool
4.3. Functionalities of semantic functions.
EE: E → State → (Val × State) CC: C → State → State PP: P → Input → Output
4.4. Definitions of semantic functions.
Auxiliary functions:
Return: Val → State → (Val × State) λ v. λ s. (v,s)
Check: Domain → (Val × State) → (Val × State) λ D. λ (v,s). v ∈ D → (v,s) | error
Dummy: State → State λ s. s
Cond: (State→State) → (State→State) → (ValxState) → State λF 1. λF 2. λ (v,s). s = > (v → F 1 | F 2 )
Replace: Mem → Id → Val → Mem λ m. λ i. λ v. ( λ i’. i’ eq i → v | m i’)
"Return" takes a value and a state and returns them as a tuple, which is the output of EE. "Check" checks that the Val parameter is in the Domain. If so, it merely returns the Val and State. "Dummy" doesn’t do very much: it returns the given state. "Cond" takes two State-to-State functions, and a (Value,State) pair. It returns the result of applying one of the functions to the State, depending on the Value. Finally, "Replace" takes an old Mem, Id, and Val, and produces a new Mem in which the Id is associated with the Val.
Now for EE, CC, and PP:
EE[0] = Return 0; EE[1] = Return 1; EE[2] = Return 2; ... etc. ...
EE[true] = Return true; EE[false] = Return false
EE[read] = λ (m,i,o). Null i → error | (Head i, (m, Tail i, o))
"read" pulls the first input symbol from the input, and replaces the input with the rest of the input. Example: EE[read](m, (5,3,1), o) for any memory m and output o has value (5, (m, (3,1), o)). The latter is a Val × State pair. The new State reflects the fact that a value has been pulled off the input. Null checks for the nil tuple. Head gets the first element of the tuple, and Tail gets the tuple contain- ing all but the first element.
EE[I] = λ (m,i,o). m I eq → error | (m I, (m,i,o))
Look up an identifier; return its value along with the same state that came in.
EE[] = EE[E]
(Check Bool)
( λ (v,s).((not v),s) )
Evaluate the expression, make sure it is boolean, and negate it.
EE[<≤ E 1 E 2 >] = EE[E 1 ]
(Check Num)
( λ (v 1 ,s 1 ).s 1 = > EE[E 2 ] = > (Check Num) = > ( λ (v 2 ,s 2 ).(v 1 ≤v 2 ,s 2 )) )
Evaluate the first argument, then the second. Return their comparison, and the new state.
EE[<+ E 1 E 2 >] = EE[E 1 ]
(Check Num)
( λ (v 1 ,s 1 ).s 1 = > EE[E 2 ] = > (Check Num) = > ( λ (v 2 ,s 2 ).(v 1 +v 2 ,s 2 )) )
Evaluate the arguments and return their sum, with the new state.
CC[<:= I E>] = EE[E]
( λ (v,(m,i,o)). (Replace m I v, i, o))
CC[] = EE[E]
( λ (v,(m,i,o)). (m,i,o aug v))
CC[] = EE[E]
(Check Bool)
(Cond CC[C 1 ] CC[C 2 ])
CC[] = EE[E]
(Check Bool)
(Cond (CC[<; C >]) Dummy)
CC[<; C 1 C 2 >] = CC[C 1 ]
PP[] = ( λ i. CC[C](( λ i. ), i, nil) )
( λ (m,i,o).o) The meaning of a program is a function that takes an input i, computes the value of CC[C] applied to an initial configuration with an everywhere-undefined memory, the input i, and a null out- put, takes the resulting value from CCC and sends it into a function that discards everything but the output.
5. A Tiny example.
Let us work out a TINY example (which won’t be so tiny):
What is the meaning of
PP[>>>] 5?
Simplify the expression s 1 = > ...:
s 1 = > λ s.(2,s) = > (Check Num) = > ( λ (v 2 ,s 2 ).(v 1 +v 2 ,s 2 )) = (2,s 1 ) = > (Check Num) = > ( λ (v 2 ,s 2 ).(v 1 +v 2 ,s 2 )) = (2,s 1 ) = > ( λ (v 2 ,s 2 ).(v 1 +v 2 ,s 2 )) = (v 1 +2,s 1 )
So:
EE[<+ 1 2>] = ( λ s.(1,s))
(Check Num)
( λ (v 1 ,s 1 ).(v 1 +2,s 1 ))
Now compute ( λ s.(1,s))
(Check Num):
= λ x.( λ s.(1,s))x eq error → error | (Check Num) ( ( λ s.(1,s))x ) = λ x.(1,x) eq error → error | (Check Num) (1,x) = λ x.(Check Num) (1,x) = λ x.(1,x) = λ s.(1,s)
So:
EE[<+ 1 2>] = ( λ s.(1,s))
( λ (v 1 ,s 1 ).(v 1 +2,s 1 )) = λ x. ( λ s.(1,s)) x eq error → error | ( λ (v 1 ,s 1 ). (v 1 +2,s 1 )) (( λ s.(1,s))x) = λ x. (1,x) eq error → error | ( λ (v 1 ,s 1 ). (v 1 +2,s 1 )) (1,x) = λ x. ( λ (v 1 ,s 1 ).(v 1 +2,s 1 )) (1,x) = λ x. (1+2,x) = λ x. (3,x) = λ s. (3,s)
This is as we expect: <+ 1 2> produces the value 3 and the same input state as it receives.
Now back to the earlier subproblem:
EE[<+ read <+ 1 2>>] = EE[read]
(Check Num)
( λ (v 1 ,s 1 ).s 1 = > ( λ s.(3,s)) = > (Check Num) = > ( λ (v 2 ,s 2 ). (v 1 +v 2 ,s 2 )) )
Simplify the s 1 = > ... expression:
s 1 = > ( λ s.(3,s)) = > (Check Num) = > ( λ (v 2 ,s 2 ).(v 1 +v 2 ,s 2 )) = (3,s 1 ) = > (Check Num) = > ( λ (v 2 ,s 2 ).(v 1 +v 2 ,s 2 )) = (3,s 1 ) = > ( λ (v 2 ,s 2 ). (v 1 +v 2 ,s 2 )) = (v 1 +3,s 1 )
So
EE[<+ read <+ 1 2>>] = EE[read]
(Check Num)
(^ λ^ (v 1 ,s 1 ).(v 1 +3,s 1 )).
Now let us substitute this value back into our computation for PP[]. If you recall, our last line in this computation was
( λ i. , 5, nil) = > EE[<+ read ...>] = > ( λ (v,(m,i,o)).(m,i,o aug v)) = > ( λ (m,i,o).o).
This now becomes
( λ i. ,5,nil) = > EE[read]
(Check Num)
( λ (v 1 ,s 1 ).(v 1 +3,s 1 )) = > ( λ (v,(m,i,o)).(m,i,o aug v)) = > ( λ (m,i,o).o)
= ( λ i. ,5,nil) = > EE[read] = > (Check Num) = > ( λ (v 1 ,s 1 ).(v 1 +3,s 1 )) = > ( λ (v,(m,i,o)).(m,i,o aug v)) = > ( λ (m,i,o).o) = Null 5→error| (Head 5,( λ i. ,Tail 5,nil)) = > (Check Num) = > ( λ (v 1 ,s 1 ).(v 1 +3,s 1 )) = > ( λ (v,(m,i,o)).(m,i,o aug v)) = > ( λ (m,i,o).o)
= (5,( λ i. ,nil,nil)) = > (Check Num) = > ( λ (v 1 ,s 1 ).(v 1 +3,s 1 )) = > ( λ (v,(m,i,o)).(m,i,o aug v)) = > ( λ (m,i,o).o)
= (5,( λ i. ,nil,nil)) = > ( λ (v 1 ,s 1 ).(v 1 +3,s 1 )) = > ( λ (v,(m,i,o)).(m,i,o aug v)) = > ( λ (m,i,o).o)
= (8,( λ i. ,nil,nil)) = > ( λ (v,(m,i,o)).(m,i,o aug v)) = > ( λ (m,i,o).o)
= ( λ i. ,nil,nil aug 8) = > ( λ (m,i,o).o)
= nil aug 8 (Whew!)
An exercise. Denotational semantics have a property that no other method seen so far has had: The mean- ing of any construct can be obtained, usually as a function of the state in which that meaning is to be evalu- ated. Note that we refer to the meaning of ANY construct, i.e. any portion of a program. To illustrate fur- ther this property, and as an exercise, you may want to derive the value of
PP[>>>] (notice we have NOT applied it to 5),
and then apply the result to 5 to see if you get the answer 8.