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

Lecture 1

The document discusses formal semantics of programming languages, specifically arithmetic expressions. It introduces big-step and small-step operational semantics as well as denotational semantics, and discusses properties like determinism, termination, and equivalence of the different semantics.

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)
11 views

Lecture 1

The document discusses formal semantics of programming languages, specifically arithmetic expressions. It introduces big-step and small-step operational semantics as well as denotational semantics, and discusses properties like determinism, termination, and equivalence of the different semantics.

Uploaded by

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

Formal Semantics of

Programming Languages
Florian Zuleger
SS 2023

1
Arithmetic Expressions (Exp)
Syntax given by grammar in BNF:
E ::= n | E1 + E2 | E1 * E2 | ...
where
• n ranges over the numerals 0, 1, …
• +, *, … are symbols for operations

We will always work with abstract syntax.


Useful notions: abstract syntax tree (AST), well-
bracketed expressions, arithmetic conventions, …
2
Alternative: Inference Rules

NUM n is a numeral
n

E1 E2 E1 E2 …
ADD MUL
E1 + E2 E1 * E2

Exp is the set of expressions that can be derived


using these rules.
3
Anatomy of an Inference Rule

Finitely many
hypotheses!

hypothesis1 ... hypothesisn


NAME side condition
conclusion

4
Big-Step Semantics of Exp

Judgements:
En

Meaning:
Evaluating expression E results in number n.

5
Big-Step Semantics of Exp

B-NUM n = number(n)
nn

E1  n1 E2  n2
B-ADD n3 = n1 + n2
E1 + E2  n3
+ is the arithmetic function that takes two
numbers (not numerals!) and returns their sum

Similar rules for *, …


6
How to Read Axioms
The axiom
B-NUM n = number(n)
nn
says:
for every numeral n,
the numeral n evaluates to the number n.

In (B-NUM) n is a kind of variable: you can put


any numeral you like in its place. These are called
meta-variables.
7
Numbers and Numerals
B-NUM n = number(n)
nn

Why do we make a difference between numbers and


numerals?

E.g. 100 can be a number in base two or base eight, but


is looks as number in base ten, too. Thus we can
interpret 100 in different ways!

Because of this difficulty we assume a function that


converts numerals into numbers.
8
How to Read Rules
The inference rule
E1  n1 E2  n2
B-ADD n3 = n1 + n2
E1 + E2  n3
says:
for any expressions E1 and E2,
if it is the case that E1  n1
and if it is the case that E2  n2
then it is the case that E1 + E2  n3
where n3 is the number such that n3 = n1 + n2

In (B-ADD) E1, E2, n1 , n2, n3 are meta-variables.


9
Rules are Schemas
Because the E’s and n’s in these rules are
metavariables each rule is really a schema
(pattern) for an infinite collection of rules. Some
of these instances are a bit silly, for example:
3 4 4 5
B-ADD
(3 + 4)  9

This rule is valid, but is useless, because it is not


the case that 3  4. That is to say, the
hypotheses of the rule are never satisfied.
10
Example
A proof that (3 + (2 + 1))  6

B-NUM B-NUM

B-NUM
2 2 1 1
B-ADD
3 3 (2+1)  3
B-ADD
(3 + (2+1))  6

Notation: `big (3 + (2 + 1))  6

11
Small-step semantics of Exp

Judgements:
E1 ! E2

Meaning:
After performing one step of evaluation of E1
the expression E2 remains to be evaluated.

12
Small-step semantics of Exp

E1 ! E1’
S-LEFT
E1 + E2 ! E1’ + E2
E2 ! E2’
S-RIGHT
n + E2 ! n + E2’

S-ADD number(n3) =
n1 + n2 ! n3 number(n1) + number(n2)

Similar rules for *, …


13
Small-step semantics of Exp

E1 ! E1’
S-LEFT
E1 + E2 ! E1’ + E2
E2 ! E2’
S-RIGHT
n + E2 ! n + E2’

S-ADD number(n3) =
n1 + n2 ! n3 number(n1) + number(n2)

Similar rules for *, …


14
Examples
A derivation: S-ADD
3 + 7 ! 10
S-LEFT
(3 + 7) + (8 + 1) ! 10 + (8 + 1)
Another derivation: S-ADD
8+1!9
S-RIGHT
10 + (8 + 1) ! 10 + 9

Notation: `sm (3 + 7) + (8 + 1) ! 10 + (8 + 1)
`sm 10 + (8 + 1) ! 10 + 9
15
Choice semantics of Exp

E1 !ch E1’
S-LEFT
E1 + E2 !ch E1’ + E2
E2 !ch E2’
S-RIGHT
E1 + E2 !ch E1 + E2’

S-ADD number(n3) =
n1 + n2 !ch n3 number(n1) + number(n2)

16
Examples
A derivation: S-ADD
3 + 7 !ch 10
S-LEFT
(3 + 7) + (8 + 1) !ch (3 + 7) + 9

Notation: `ch (3 + 7) + (8 + 1) !ch 10 + (8 + 1)


`ch (3 + 7) + (8 + 1) !ch (3 + 7) + 9

True or False?: `ch E1 !ch E2 implies `sm E1 ! E2


`sm E1 ! E2 implies `ch E1 !ch E2

17
Recap: Reflexive Transitive Closure
Given a relation ! we define its reflexive
transitive closure !* as follows:

E !* E’ iff either
• E = E’ (no steps of the relation are needed to
get from E to E’) or
• there is a finite sequence of relation steps
E ! E1 ! E2 ! … ! Ek ! E’.

18
Executing Small-step Semantics
We say E evaluates to n, if E !* n.

Questions:
- E  n and E  m implies m=n (determinism)?
- Is there always an n s.t. E !* n (termination)?
- E !* n iff E  n (equivalence of semantics)?
- E !* n iff E !ch* n (determinism of choice)?

19
Denotational Semantics

We will define the denotational semantics of


expressions via a function:

«-¬: Exp ! N.

20
Denotation of Expressions
Here is the definition of our semantic function. This
is an example of definition by structural induction.

«n¬ = n, using n = number(n)


«E1 + E2¬ = «E1¬ + «E2¬

Remember the distinction between numbers and


numerals. We only make this distinction on this
slide.
21
Calculating Semantics
«(1 + (2 + 3))¬ = «1¬ + «(2 + 3)¬
= 1 + «(2 + 3)¬
= 1 + («2¬ + «3¬)
= 1 + (2 + 3)
=1+5
=6

22
Associativity of Addition
Theorem «E1 + (E2 + E3)¬ = «(E1 + E2) + E3¬

Proof «E1 + (E2 + E3)¬ = «E1¬ + «(E2 + E3)¬


= «E1¬ + («E2¬ + «E3¬)
= («E1¬ + «E2¬) + «E3¬
= («E1 + E2¬) + «E3¬
= «(E1 + E2) + E3¬

Exercise: Show a similar fact using the operational


semantics (more cumbersome!).
23
Contextual Equivalence (Informal)
We shall now introduce a very important idea in
semantics, that of contextual equivalence.

Intuition: Equivalent programs can be used


interchangeably; if P1  P2 and P1 is used in some
context, C[P1], then we should get the same effect if
we replace P1 with P2 : we expect C[P1]  C[P2].

To make this more precise, we say that a context


C[−] is a program with a hole where you would
ordinarily expect to see a sub-program.
24
Some Exp contexts
C1[-] = -.
C2[-] = (- + 2).
C3[-] = ((- + 1) + -).

Grammar for expression contexts


C ::= - | n | C1 + C2 | C1 * C2 | ...

25
Filling the holes
C1[-] = -.
C2[-] = (- + 2).
C3[-] = ((- + 1) + -).

C1[(3+4)] = (3+4).
C2[(3+4)] = ((3+4) + 2).
C3[(3+4)] = (((3+4) + 1) + (3+4)).

26
Contextual Equivalence
Definition
Expressions E1 and E2 are contextually
equivalent with respect to the big-step
semantics, if for all contexts C[−] and all
numbers n,

C[E1]  n iff C[E2]  n.

27
Compositionality and Contextual
Equivalence
Recall: the denotational semantics is
compositional, i.e., the meaning of an
expression phrase is built out of the meanings of
its sub-expressions.
Thus, each context determines a “function
between meanings”, i.e., for each C[−] there is a
function fC: N ! N such that
«C[E]¬ = fC(«E¬)
for any expression E.
28
Compositionality and Contextual
Equivalence
A consequence of such a function fC: N ! N for a
context C[−] is that

if «E1¬ = «E2¬ then


«C[E1]¬ = fC(«E1¬) = fC(«E2¬) = «C[E2]¬!

It remains to relate the structural and the


denotational semantics, i.e., we want to establish
that «E¬ = n iff E  n for all expressions E!

29
Equivalence Theorem
Theorem For all expressions E, «E¬ = n , E  n.

Proof
We split the proof into the two lemmas for the
cases
«E¬ = n ( E  n
and
«E¬ = n ) E  n.
30
Equivalence Theorem
Lemma For all expressions E, «E¬ = n ( E  n.

Proof By structural induction on the expression E.

Base Case: E is a numeral n. We have E  n.


This can only be derived from the rule (B-NUM).
B-NUM n = number(n)
nn
By the definition of the denotational semantics we
have «E¬ = n, so the base case is solved.
31
Equivalence Theorem
Induction Step: Suppose E is of the form (E1 + E2).
We have (E1 + E2)  n.
This can only be derived from the rule (B-ADD)
using hypotheses E1  n1 and E2  n2 for some
numbers n1, n2 with n3 = n1+n2.
E1  n1 E2  n2
B-ADD n = n1 + n2
E1 + E 2  n

By the inductive hypothesis, «Ei¬ = ni for i = 1, 2, and


therefore «E¬ = «E1¬ + «E2¬ = n1 + n2 = n.
32
Equivalence Theorem
Lemma For all expressions E, «E¬ = n )E  n.

Proof By structural induction on the expression E.

Base Case: E is a numeral n. We have «E¬ = n.


By the rule (B-NUM) we can derive n  n.
B-NUM n = number(n)
nn
This solves the base case.
33
Equivalence Theorem
Induction Step: Suppose E is of the form (E1 + E2). We
have «E1 + E2¬ = n.
By the definition of the denotational semantics we
have n = «E1 + E2¬ = «E1¬ + «E2¬ = n1 + n2, where ni is the
number such that «Ei¬ = ni.
From the induction hypothesis we get Ei  ni for i=1,2.
Thus we derive (E1 + E2)  n from rule (B-ADD).

E1  n1 E2  n2
B-ADD n = n1 + n2
E1 + E 2  n
34

You might also like