Lecture 1
Lecture 1
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
NUM n is a numeral
n
E1 E2 E1 E2 …
ADD MUL
E1 + E2 E1 * E2
Finitely many
hypotheses!
4
Big-Step Semantics of Exp
Judgements:
En
Meaning:
Evaluating expression E results in number n.
5
Big-Step Semantics of Exp
B-NUM n = number(n)
nn
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
B-NUM B-NUM
B-NUM
2 2 1 1
B-ADD
3 3 (2+1) 3
B-ADD
(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)
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)
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
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
«-¬: Exp ! N.
20
Denotation of Expressions
Here is the definition of our semantic function. This
is an example of definition by structural induction.
22
Associativity of Addition
Theorem «E1 + (E2 + E3)¬ = «(E1 + E2) + E3¬
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,
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
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.
E1 n1 E2 n2
B-ADD n = n1 + n2
E1 + E 2 n
34