Upload
others
View
3
Download
0
Embed Size (px)
Citation preview
Semantics with Applications
1. Introduction
Hanne Riis Nielson, Flemming Nielson
(thanks to Henrik Pilegaard)
[SwA] Hanne Riis Nielson, Flemming NielsonSemantics with Applications: An AppetizerSpringer, 2007
1 / 31
Syntax and SemanticsReading material: Chapter 1 of SwA
2 / 31
Language While: Syntactic Categories
I Numerals n ∈ Num
n ::= 0 | 1 | n0 | n1
I Variables x ∈ Var – e.g. x, y, z, . . .
I Arithmetic expressions a ∈ Aexp
a ::= n | x | a1 + a2 | a1 ? a2 | a1 − a2
I Boolean expressions b ∈ Bexp
b ::= true | false | a1 = a2 | a1 ≤ a2 | ¬b | b1 ∧ b2
I Statements S ∈ Stm
S ::= x := a | skip | S1;S2 | if b then S1 else S2 | while b do S
3 / 31
Example Programs
I The factorial programI If x = n > 0 initially, then y = n! after program termination
y := 1; while ¬(x = 1) do (y := y ? x ; x := x − 1)
4 / 31
Compositional Definitions
I The syntactic category is specified by an abstract syntax giving theI basis elements
I composite elements – decomposition into their immediateconstituents
a ::= n | x | a1 + a2 | a1 ? a2 | a1 − a2
5 / 31
Abstract vs. Concrete Syntax
I Abstract syntax focuses on the structure of expressions andstatements
I ignores scanning and parsing aspects
I formalises a set of allowed parse trees
I Concrete syntax deals with the scanning and parsing aspectsI provides sufficient information to construct unique parse trees
6 / 31
Abstract vs. Concrete Syntax
I In this part of the course, we are not concerned with concrete syntax
I We use brackets to disambiguate the syntax where necessary
while x < y do (x := x + 1; y := 0)or
(while x < y do x := x + 1); y := 0
I Assume familiar precedence rules for arithmetic and boolean operators
write 1 + x ? 2 for 1 + (x ? 2) and not for (1 + x) ? 2
7 / 31
Semantics of Arithmetic and Boolean Expressions
8 / 31
States
I We have used states before in the vending machine example
I Now we can be more formal: a state is an element of the set
State = Var→ Z
where Z is the set of integers, i.e. . . . ,−1, 0, 1, 2, . . .
I Other representations used: “table” as in the vending machineexample, or “list” [x 7→ 3, y 7→ 0]
I Lookup: Each state s specifies a value s x for every x ∈ VarWe use the variant notation s(x) as well
I Update:
(s[y 7→ n])(x) =
{n if x = ys(x) if x 6= y
9 / 31
Semantic Functions
I Crucial distinction between the syntax (grammar) and the semantics(meaning) of a program
I A semantic function is a total function that maps syntactic
elements to a mathematical object representing their meaning
10 / 31
Semantics of Numerals
Numerals
I Assume that numerals n ∈ Num (in binary form)
I Function N : Num→ Z maps numerals to integers Z
I N [[0]] = 0,
I N [[1]] = 1,
I N [[n0]] =?,
I N [[n1]] =?,
I Notation: “syntactic brackets” [[ and ]] to enclose syntactic elements
11 / 31
Semantics of Numerals
Numerals
I Assume that numerals n ∈ Num (in binary form)
I Function N : Num→ Z maps numerals to integers Z
I N [[0]] = 0,
I N [[1]] = 1,
I N [[n0]] = 2 · N [[n]],
I N [[n1]] = 2 · N [[n]] + 1,
I Notation: “syntactic brackets” [[ and ]] to enclose syntactic elements
12 / 31
Semantics of Arithmetic Expressions
I The value of an expression such as 2 ? x + y depends on the values ofthe variables x and z – these are determined by the current state
I The semantics of an arithmetic expression a in current state s is givenby the semantic function
A : Aexp→ (State→ Z)
I The function A takes its parameters one at a time:
I A[[x + 1]] has functionality State→ Z
I Given a state s with s x = 2 we have A[[x + 1]]s = 3
13 / 31
Semantics of Arithmetic Expressions
The semantics of arithmetic expressions is given by:
14 / 31
Semantics of Arithmetic Expressions
The semantics of arithmetic expressions is given by:
15 / 31
Semantics of Arithmetic Expressions
I Operators +, ?,− on the right hand side are the usual arithmeticoperations, on the left hand side they are just pieces of syntax
I Note that we have one clause for each of the different forms ofarithmetic expressions
Given a state s with s x = 3 and s y = 1 we have:
A[[2 ? x + y]]s =
A[[2 ? x]]s +A[[y]]s
=
A[[2]]s · A[[x]]s + 1
=
N [[2]] · 3 + 1 = 2 · 3 + 1 = 7
16 / 31
Semantics of Arithmetic Expressions
I Operators +, ?,− on the right hand side are the usual arithmeticoperations, on the left hand side they are just pieces of syntax
I Note that we have one clause for each of the different forms ofarithmetic expressions
Given a state s with s x = 3 and s y = 1 we have:
A[[2 ? x + y]]s = A[[2 ? x]]s +A[[y]]s=
A[[2]]s · A[[x]]s + 1
=
N [[2]] · 3 + 1 = 2 · 3 + 1 = 7
17 / 31
Semantics of Arithmetic Expressions
I Operators +, ?,− on the right hand side are the usual arithmeticoperations, on the left hand side they are just pieces of syntax
I Note that we have one clause for each of the different forms ofarithmetic expressions
Given a state s with s x = 3 and s y = 1 we have:
A[[2 ? x + y]]s = A[[2 ? x]]s +A[[y]]s= A[[2]]s · A[[x]]s + 1=
N [[2]] · 3 + 1 = 2 · 3 + 1 = 7
18 / 31
Semantics of Arithmetic Expressions
I Operators +, ?,− on the right hand side are the usual arithmeticoperations, on the left hand side they are just pieces of syntax
I Note that we have one clause for each of the different forms ofarithmetic expressions
Given a state s with s x = 3 and s y = 1 we have:
A[[2 ? x + y]]s = A[[2 ? x]]s +A[[y]]s= A[[2]]s · A[[x]]s + 1= N [[2]] · 3 + 1 = 2 · 3 + 1 = 7
19 / 31
Compositional Definitions
I The semantics is defined by compositional definitions of a function:I A semantic clause for each of the basis elements
I A semantic clause for each of the methods for constructingcomposite elements – defined in terms of the semantics of their
immediate constituents
20 / 31
Semantics of Boolean Expressions
I Analogous to the development for arithmetic expressions
I Semantic function for boolean expressions
B : Bexp→ (State→ T)
I Set of (semantic) truth values T consists of tt (for true) and ff (forfalse)
21 / 31
Semantics of Boolean ExpressionsThe semantics of boolean expressions is given by:
22 / 31
Semantics of Boolean ExpressionsThe semantics of boolean expressions is given by:
23 / 31
Properties of the Semantics
24 / 31
Mathematical Induction
Mathematical induction is used to prove properties of things that can begiven a size as a natural number (being a non-negative integer).
I Prove that 2n ≤ (n + 1)! ≤ (n + 1)n.
I Basis: for n = 0 we must prove 1 ≤ 1 ≤ 1 which is immediate.
I Induction step: for n = m + 1 > 0 we must prove2m+1 ≤ (m + 1 + 1)! ≤ (m + 1 + 1)m+1
and may use2m ≤ (m + 1)! ≤ (m + 1)m
25 / 31
Structural Induction
Structural induction is used to prove properties of the semantics. It worksin a similar way as mathematical induction:
I Prove that the property holds for all the basis elements (formathematical induction: 0)
I Prove that the property holds for all the composite elements :Assume that the property holds for all the immediate constituents ofthe element (this is called the induction hypothesis) and prove that italso holds for the element itself (for mathematical induction: n+1)
Structural induction can alternatively be explained as mathematicalinduction on an upper bound on the size of the element (think number ofASCII characters).
26 / 31
Semantics of Numerals
QuizIs the function N [[]] total? I.e., for each n ∈ Num, there exists exactly onei ∈ Z such that N [[n]] = i?
Yes. Proof by structural induction.
I case n = 0: ?
I case n = n′0: ?
I cases n = 1 or n = n′1 are similar
27 / 31
Semantics of Numerals
QuizIs the function N [[]] total? I.e., for each n ∈ Num, there exists exactly onei ∈ Z such that N [[n]] = i?
Yes. Proof by structural induction.
I case n = 0: ?
I case n = n′0: ?
I cases n = 1 or n = n′1 are similar
28 / 31
Free Variables
I We want to formalise the fact that the value of an arithmeticexpression only depends on the values of variables occurring in it
I The set of free variables FV (a) of expression a:
FV (n) = ∅FV (x) = {x}FV (a1 + a2) = FV (a1) ∪ FV (a2)FV (a1 ? a2) = FV (a1) ∪ FV (a2)FV (a1 − a2) = FV (a1) ∪ FV (a2)
LemmaLet s and s ′ be two states satisfying that s x = s ′ x for all x ∈ FV (a).Then A[[a]]s = A[[a]]s ′.
Proof by structural induction.
29 / 31
Substitutions
I We want to formalise the fact that a substitution within an expressioncan be mimicked by a similar change of the state
I Substitution of a variable y with a0 in the expression a:
n[y 7→ a0] = n
x [y 7→ a0] =
{a0 if x = yx if x 6= y
(a1 + a2)[y 7→ a0] = (a1[y 7→ a0]) + (a2[y 7→ a0])(a1 ? a2)[y 7→ a0] = (a1[y 7→ a0]) ? (a2[y 7→ a0])(a1 − a2)[y 7→ a0] = (a1[y 7→ a0])− (a2[y 7→ a0])
LemmaA[[a[y 7→ a0]]]s = A[[a]](s[y 7→ A[[a0]]s]) for all states s.
Proof: Do Exercise 1.14
30 / 31
Summary
I Language While: syntax and semantics
I Proof technique: structural induction
Exercise Class
I Exercises 1.2, 1.14, 1.15 from SwA.
31 / 31