0% found this document useful (0 votes)
8 views

Semantics of Programming Languages Preliminary Exercises

Uploaded by

M
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Semantics of Programming Languages Preliminary Exercises

Uploaded by

M
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Technische Universität Wien SS 2024

Fakultät für Informatik Repetition Sheet


Assoc. Prof. Florian Zuleger Wednesday, 13 March 2024
Mark Chimes, M.Sc.

Exercises on Semantics of Programming Languages

This exercise sheet is optional. It does not count towards exam admission.

Exercise 1 Proof by induction

Consider the While language introduced in the lectures. We define the big-step se-
mantics of arithmetic expressions as follows.
The semantics has two kinds of configurations:

⟨a, s⟩ denoting that a has to be evaluated in state s ∶ Var → Z, and


z denoting the final value (an element of Z)

The transition relation →Aexp has the form ⟨a, s⟩ →Aexp z. The semantics is given in
Table 1.

⟨n, s⟩ →Aexp N ⟦n⟧


⟨x, s⟩ →Aexp s x
⟨a1 , s⟩ →Aexp z1 , ⟨a2 , s⟩ →Aexp z2
where z = z1 + z2
⟨a1 + a2 , s⟩ →Aexp z
⟨a1 , s⟩ →Aexp z1 , ⟨a2 , s⟩ →Aexp z2
where z = z1 ∗ z2
⟨a1 ∗ a2 , s⟩ →Aexp z
⟨a1 , s⟩ →Aexp z1 , ⟨a2 , s⟩ →Aexp z2
where z = z1 − z2
⟨a1 − a2 , s⟩ →Aexp z

Table 1: Natural semantics of arithmetic expressions

Let us define a function A ∶ Aexp → (State → Z) as follows:

∀a ∈ Aexp, ∀s ∈ State, A⟦a⟧s = z ∶⇔ ⟨a, s⟩ →Aexp z

Show that this function is well defined by proving that for each a ∈ Aexp and each
s ∈ State there is exactly one value z ∈ Z such that ⟨a, s⟩ →Aexp z.

Hint: Use induction on the structure of arithmetic expressions.


• First, show that the property holds for every atomic expression (i.e., for numerals
and for variables).

• Then, prove that the property holds for every composite expression a1 ∼ a2 , where
∼ ∈ {+, ∗, −}, using the assumption that it holds for a1 and a2 .
Exercise 2 Extension of Boolean expressions. Semantic equivalence.

Again consider the While language introduced in the lectures. The semantics of Boolean
expressions is given by the function

B ∶ Bexp → (State → {tt, ff}).

The definition of B is given in Table 2.

B⟦true⟧s = tt
B⟦f alse⟧s = ff
tt if A⟦a1 ⟧s = A⟦a2 ⟧s
B⟦a1 = a2 ⟧s = {
ff if A⟦a1 ⟧s ≠ A⟦a2 ⟧s
tt if A⟦a1 ⟧s ≤ A⟦a2 ⟧s
B⟦a1 ≤ a2 ⟧s = {
ff if A⟦a1 ⟧s > A⟦a2 ⟧s
tt if B⟦b⟧s = ff
B⟦¬b⟧s = {
ff if B⟦b⟧s = tt
tt if B⟦b1 ⟧s = tt and B⟦b2 ⟧s = tt
B⟦b1 ∧ b2 ⟧s = {
ff if B⟦b1 ⟧s = ff or B⟦b2 ⟧s = ff

Table 2: The semantics of Boolean expressions

We define the syntactic category Bexp’ as the following extension of Bexp:

b ∶∶= true ∣ f alse ∣ a1 = a2 ∣ a1 ≠ a2 ∣ a1 ≤ a2 ∣ a1 ≥ a2


∣ a1 <a2 ∣ a1 >a2 ∣ ¬b ∣ b1 ∨ b2 ∣ b1 ∧ b2 ∣ b1 ⇒ b2 ∣ b1 ⇔ b2

a) Provide a formal semantics of Bexp′ by defining a function B ′ ∶ Bexp’ → (State →


{tt, ff}) that extends the semantic function B from Table 2.
Your semantics should reflect the “usual” semantics familiar from mathematics. For
example, a1 >a2 should hold iff the value of a1 is greater than the value of a2 and
b1 ⇒ b2 should hold iff b1 implies b2 .

b) Two Boolean expressions b1 and b2 are equivalent if for all states s,

B ′ ⟦b1 ⟧s = B ′ ⟦b2 ⟧s.

Show that for each b′ of Bexp’ there exists a Boolean expression b of Bexp such
that b′ and b are equivalent.
Exercise 3 Substitution

We define a semantic function A for arithmetic expressions in Table 3.

A⟦n⟧s = N ⟦n⟧
A⟦x⟧s = sx
A⟦a1 + a2 ⟧s = A⟦a1 ⟧s + A⟦a2 ⟧s
A⟦a1 − a2 ⟧s = A⟦a1 ⟧s − A⟦a2 ⟧s
A⟦a1 ∗ a2 ⟧s = A⟦a1 ⟧s ∗ A⟦a2 ⟧s

Table 3: Semantic function for arithmetic expressions

We define the substitution of variable y by arithmetic expression a0 , written a[y ↦ a0 ],


in Table 4.

n[y ↦ a0 ] = n
a0 if x = y
x[y ↦ a0 ] = {
x if x ≠ y
(a1 + a2 )[y ↦ a0 ] = a1 [y ↦ a0 ] + a2 [y ↦ a0 ]
(a1 ∗ a2 )[y ↦ a0 ] = a1 [y ↦ a0 ] ∗ a2 [y ↦ a0 ]
(a1 − a2 )[y ↦ a0 ] = a1 [y ↦ a0 ] − a2 [y ↦ a0 ]

Table 4: Substitution for arithmetic expressions

Finally, we introduce a notion of substitution for states.

v if x = y
(s[y ↦ v])x = { where y ∈ Var, v ∈ Z.
s x if x ≠ y

Prove that the following property holds:

A⟦a[y ↦ a0 ]⟧s = A⟦a⟧(s[y ↦ A⟦a0 ⟧s])


Exercise 4 Determinism of Small-Step Semantics

Consider the small-step semantics of the While language below.

[asssos ] ⟨x ∶= a, s⟩ ⇒ s[x ↦ A⟦a⟧s]


[skipsos ] ⟨skip, s⟩ ⇒ s
⟨S1 , s⟩ ⇒ ⟨S1′ , s′ ⟩
[comp1sos ]
⟨S1 ; S2 , s⟩ ⇒ ⟨S1′ ; S2 , s′ ⟩
⟨S1 , s⟩ ⇒ s′
[comp2sos ]
⟨S1 ; S2 , s⟩ ⇒ ⟨S2 , s′ ⟩
[iftt
sos ] ⟨if b then S1 else S2 , s⟩ ⇒ ⟨S1 , s⟩ if B⟦b⟧s = tt

[iffsos
f
] ⟨if b then S1 else S2 , s⟩ ⇒ ⟨S2 , s⟩ if B⟦b⟧s = ff

[whilesos ] ⟨while b do S, s⟩ ⇒ ⟨if b then (S; while b do S) else skip, s⟩

Prove that the semantics is deterministic, i.e., for all choices of S, s, γ and γ ′ we have
that if ⟨S, s⟩ ⇒ γ and ⟨S, s⟩ ⇒ γ ′ then γ = γ ′ .

Hint: Use rule induction:

• Prove that the property holds for all axioms of the inference system.

• Prove that the property holds for all rules of the inference system: For each rule
assume that the property holds for its premises. From this assumption, show that
it also holds for the conclusion of the rule provided that the conditions of the rule
are satisfied.

You might also like