[CSUSB]
>> [CNS]
>> [Comp Sci Dept]
>> [R J Botting]
>> [CSci620]
>>
denotational
[Source]
These notes discuss and develop the denotational semantics of While. While was defined in [ The Language While in while ]
The heart of this is the theory of the mathematical systems used in denotational systems: Scott's theory of complete posets or domains [ fixed.html ]
Previously we were able to define a semantic function in terms of the natural
semantics of the language, however we did not have such a compositional
definition for the function. But we have seen two examples of denotational
semantics already.
Examples
Integer Expressions
[ Semantics of Expressions in while ]
Boolean Expressions
[ Semantics of Boolean Expressions in while ]
Idea
The idea is to simplify the definition of a language by using more powerful
mathematical models. For example the definition of the semantic map A of an
Aexp is less complex than the semantics B of Bexp because each Aexp is
associated with an algebraic expression - an object in a known mathematical
system. The special cases in B used to define the meaning of Bexp would
not have been needed if tt and ff had been the elements in a classical
Boolean algebra with two elements. For example the four rules in Bexp that
define the boolean operators ~ and /\ are:
Similarly the definition of Bexpp can be greatly simplified by (1) assuming {tt,ff} is a Boolean algebra with negation, conjunction, disjunction etc. Hint: why not use the same symbols in the Boolean algebra are is Bexpp whenever possible?
The effect on the programmer is profound... the language means what it says! Indeed expressions in the language can be treated as if they where really elements in the same algebra. A set of denotational semantics almost guarantees that the language is unlikely to surprise its users, A mathematician would say, with good reason, that denotational semantics defines a homomorphism between the syntax and the semantics. However compiler writers would have a difficult problem - for example such a correspondence would raise questions about whether short-circuit evaluation was permitted.
Another problem occurs when we define the ints as mathematical integers. The finite representation that is available and efficient can not be mapped into the normal algebra of the integers. This means that a compromise would have to be created where the semantics of ints is defined in terms of a finite approximation to the integers. This is usually done informally. The idea of using denotations that are elements in algebras could formalize this idea precisely.
Basis of Denotational Semantics
For a simple language like While it is enough for the
mathematical model to be a set of partial functions mapping the state of the
variables (itself a function) into another state. To make the model more
rigorous a pseudo-element undefined is added to the states:
The semantics will use three ways of combining functions:
Operations on Functions
The choice of the best of the available fixed points leads to the theory in the heart of these notes. [ Theory of Fixed Points ]
. . . . . . . . . ( end of section Introduction) <<Contents | Index>>
Semantics of While
Explanations
Each definition defines the meaning of a statement as an expression in the
mathematical calculus of relations and functions: Id, fun, o, cond, FIX.
The definition of D <<x:=a >> implies that if x=y then D <<x:=a >> s y = A <<a >> s but if not x=y then D <<x:=a >> s y = s y. Similarly for all variables x, D <<skip >> s x = s x.
The composition rule defines a unique successor state if and only if both component are defined. Also note that the order of S1 and S2 are reversed on opposite sides of the definition. Recall "f o g" = "g; f".
The semantics of if b then S1 else S2 uses a special function cond( B <<b >> , S1, s2). Notice that it takes three functions and returns a function:
The defining properties of cond are
Note: How I do Cond
I prefer to define f | g as behaving like f when f is defined and like g
when g is defined. However it is an error if f and g are both defined. An
alternative is f |-> g which behaves like f when f is defined and otherwise like g.
Definition of the loop semantics
The definition of D <<while b do S >> is the cleverest part of these
semantics. The fixed point operator FIX and the functional F on which it
operates on a carefully chosen to give precisely the effect we want, without
using any form of explicit recursion.... the recursion is hidden inside FIX!
To show that this definition is going to work like previous ones, and to gain
understanding of how FIX and F work we can show that if we want this
Let
But we want this to equal w so, we want F(w) = w.
In other words to get the structural (operational) semantics of a while loop we need to choose to define the denotation of a while loop to be a fixed point of the specific functional chosen.
Note: We use the term functional for F to remind us that it is a function that operates on other functions. The terminology is inherited from functional analysis.
[ Partial answer to Exercise 1 ]
Can we be sure that the definition of While actually defines a unique meaning for D <<while b do S >> ? And why can we be sure? And what is the general rules that make this sure.
Exercise 2
Net
We will show that all the functionals of the following form
Now suppose that s'=D <<S >> s and B <<b >> s = tt and that g is such that F(g)=g,
then
In other words, a fixed point function has a constant value when applied to
success iteration round a while loop. Because
Similarly you can show that if D <<S >> s is undefined and B <<b >> s=tt and F(g) = g, then g(s) is undefined.
Also we have (please work out why) that whatever D <<S >> is, if
B <<b >> =ff implies that (F g)s = s.
Exercise 3
Prove the last two results - the proofs are like the first one above.
Consider three case:
When the statement the loop (S) fails to terminate and loops after being repeated (and terminating) a number of times.
When the W loop itself loops from a given initial state of s0, even tho' S terminates each time.
. . . . . . . . . ( end of section Semantics of While) <<Contents | Index>>
Theory of Fixed Points
[ fixed.html ]
This shows that if a function F has a fixed point FIX F then it can be found by iterating F in a Kleene sequence: {}, F{}, F F{}, F F F{}, which should fit with our intuitive feel for the meaning of loops.
Existence of Direct Semantics for While
The theory of fixed points allows us to prove that our denotational
semantics defines a unique meaning (denotation) as a function in the space
of continuous partial functions.
[ Existence of Direct Semantics for While in fixed ]
Exercise 4
Write down a loop that is undefined for all possible
states. Prove that it is undefined.
Exercise 5
Give denotational semantics for a repeat-until statement.
Exercise 6
Give denotational semantics for a for statement.
Summary
The denotational semantics is in terms of the fixed points of continuous functionals. These have unique least fixed points -- precisely those that their Kleene sequences converge toward.
Note. 7a and 7b prove that the semantics defines a monoid of equivalent statements.
What are the functions g0, g1, g2, g3, defined by:
. . . . . . . . . ( end of section Partial answer to Exercise 1) <<Contents | Index>>
Partial Functions
To make the presentation simpler if f:S<>->S we write
Normally we say that
I will write
More details in
[ fixed.html ]
Theory of Partially Ordered Sets
[ Theory of Partially Ordered Sets in fixed ]
A Paradox
It is worth noting that if we want a set of functions to be
rich enough to allow use to define functions in out programming
languages that we can not use as our set of denotations the complete set
of all partial functions on that domain. Suppose our domain was
Let
Dana Scott and Gorden Plotkin found a subset of continuous functions in S<>->S with these attractive properties:
. . . . . . . . . ( end of section Footnotes) <<Contents | Index>>
Glossary
. . . . . . . . . ( end of section Notes on the Denotational Semantics of Programming Languages) <<Contents | Index>>