0% found this document useful (0 votes)
37 views14 pages

Nielson - Semantics With Applications An Appetizer

The document provides an overview of the semantics of the programming language 'While', detailing its syntactic categories, meta-variables, and BNF notation. It discusses semantic functions for expressions and statements, including operational, denotational, and axiomatic semantics, along with examples such as 'SWAP' and 'FACTORIAL'. Additionally, it outlines the definitions and properties of various semantic relations and transition systems.

Uploaded by

Shen JinWei
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views14 pages

Nielson - Semantics With Applications An Appetizer

The document provides an overview of the semantics of the programming language 'While', detailing its syntactic categories, meta-variables, and BNF notation. It discusses semantic functions for expressions and statements, including operational, denotational, and axiomatic semantics, along with examples such as 'SWAP' and 'FACTORIAL'. Additionally, it outlines the definitions and properties of various semantic relations and transition systems.

Uploaded by

Shen JinWei
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Nielson - Semantics with

Applications, An Appetizer

The Example Language While


The Semantics Functions for “Expressions”
The Semantics Functions for Statements
Glossary
Running Examples
Swap/Exchange
Factorial

The Example Language While


A. The Syntactic-categories -

1. Numerals

2. Variables

3. Arithmetic expressions

4. Boolean expressions

5. Statements.

B. The Meta-variables - notations that will be used to range over constructs of each
category.

1. n will range over numerals, Num,

2. x will range over variables, Var,

3. a will range over arithmetic expressions, Aexp,

4. bwill range over boolean expressions, Bexp,

5. S will range over statements, Stm.

[Notation] The meta-variables may be primed or subscripted like n, n′ , n1 which all


stand for numerals.

C. The BNF:

1. n ::= to be given somewhere (e.g. binary or decimal numeral systems).

2. x ::= to be given somewhere (e.g. strings of letters and digits starting with a
letter).

Nielson - Semantics with Applications, An Appetizer 1


3. a ::= n ∣ x ∣ a1 + a2 ∣ a1 ⋆ a2 ∣ a1 − a2
​ ​ ​ ​ ​ 

4. b ::= true ∣ false ∣ a1 = a2 ∣ a1 ≤ a2 ∣ ¬b ∣ b1 ∧ b2


​ ​ ​ ​ ​ 

5. S ::= x := a ∣ skip ∣ S1 ; S2 ∣ if b then S1 else S2 ∣ while b do S


​ ​ ​ ​


D. The Abstract Syntax Tree view

Example code: SWAP

graph TD
S_root[S] --> S_1[S]
S_root[S] --> S_root_Semi[;]
S_root[S] --> S_2[S]

S_1[S] --> S_1_V_z_l[z]


S_1[S] --> S_1_Ass[:=]
S_1[S] --> S_1_Arith_r[a]
S_1_Arith_r[a] --> S_1_Arith_r_V_x[x]

S_2[S] --> S_3[S]


S_2[S] --> S_3_Semi[;]
S_2[S] --> S_4[S]

S_3[S] --> S_3_V_z_l[x]


S_3[S] --> S_3_Ass[:=]
S_3[S] --> S_3_Arith_r[a]
S_3_Arith_r[a] --> S_3_Arith_r_V_x[y]

S_4[S] --> S_4_V_z_l[x]


S_4[S] --> S_4_Ass[:=]
S_4[S] --> S_4_Arith_r[a]
S_4_Arith_r[a] --> S_4_Arith_r_V_x[y]

Example code: FACT (exercise 1.1)

graph TD
S_root[S]
S_root --> S_init[S]
S_root --> S_root_semi[;]
S_root --> S_while[S]

S_init --> S_init_V_z_l[y]


S_init --> S_init_Ass[:=]

Nielson - Semantics with Applications, An Appetizer 2


S_init --> S_init_Arith_r[a]
S_init_Arith_r --> S_init_Arith_r_V_x[1]

S_while --> S_while_while[while]


S_while --> S_while_Bexp[b]
S_while --> S_while_do[do]
S_while --> S_while_comp[S]

S_while_Bexp --> S_while_Bexp_neg[¬]


S_while_Bexp --> S_while_Bexp_Bexp[b]
S_while_Bexp_Bexp --> S_while_Bexp_Bexp_l_Aexp[a]
S_while_Bexp_Bexp --> S_while_Bexp_Bexp_eq[=]
S_while_Bexp_Bexp --> S_while_Bexp_Bexp_r_Aexp[a]
S_while_Bexp_Bexp_l_Aexp --> S_while_Bexp_Bexp_l_Aexp_Var[x]
S_while_Bexp_Bexp_r_Aexp --> S_while_Bexp_Bexp_r_Aexp_Num[1]

S_while_comp --> S_while_comp_1[S]


S_while_comp --> S_while_comp_semi[;]
S_while_comp --> S_while_comp_2[S]

S_while_comp_1 --> S_while_comp_1_l_Var[y]


S_while_comp_1 --> S_while_comp_1_ass[:=]
S_while_comp_1 --> S_while_comp_1_r_Aexp[a]

S_while_comp_1_r_Aexp --> S_while_comp_1_r_Aexp_operand1[a]


S_while_comp_1_r_Aexp --> S_while_comp_1_r_Aexp_operator[☆]
S_while_comp_1_r_Aexp --> S_while_comp_1_r_Aexp_operand2[a]

S_while_comp_1_r_Aexp_operand1 --> S_while_comp_1_r_Aexp_operand1_Var[y]


S_while_comp_1_r_Aexp_operand2 --> S_while_comp_1_r_Aexp_operand2_Var[x]

S_while_comp_2 --> S_while_comp_2_l_Var[x]


S_while_comp_2 --> S_while_comp_2_ass[:=]
S_while_comp_2 --> S_while_comp_2_r_Aexp[a]

S_while_comp_2_r_Aexp --> S_while_comp_2_r_Aexp_operand1[a]


S_while_comp_2_r_Aexp --> S_while_comp_2_r_Aexp_operator[-]
S_while_comp_2_r_Aexp --> S_while_comp_2_r_Aexp_operand2[a]

S_while_comp_2_r_Aexp_operand1 --> S_while_comp_2_r_Aexp_operand1_Var[x]


S_while_comp_2_r_Aexp_operand2 --> S_while_comp_2_r_Aexp_operand2_Num[1]

Nielson - Semantics with Applications, An Appetizer 3


When we talk about syntactic entities such as XYZ expressions (or statements), we
should always be talking about the (unambiguous) abstract syntax (represented by their
ASTs).

[Notation] We use parentheses when emphasis is put on the linear notations, e.g. SWAP:
(z := x; x := y); y := z 

[Notation] To cut down the use of parentheses, the customary binding powers (a.k.a
precedences) of +, ⋆, −etc., is placed.

The Semantics Functions for “Expressions”


For numerals, arithmetic expressions and boolean expressions, the semantics functions are
specified once and for all.

For statements, different semantic approach (Operational, Denotational, Axiomatic) will be


given separately.

N : Num → Z , The semantic function for numerals

For demonstration purpose, we only give semantic function for numerals in binary
numerals. It is easy to define the similar results for decimal numerals.

1. N [[0]] = 0 

2. N [[1]] = 1 

3. N [[n0]] = 2 ⋅ N [[n]]  compositional definition

4. N [[n1]] = 2 ⋅ N [[n]] + 1  compositional definition

State = Var → Z , The states. To each variable a given state will associate its current
value.

[Notation] Table representation

(WIP)

[Notation] List representation

(WIP)

[Notation] Abbreviation
(WIP)

A : Aexp → State → Z , The semantic function for arithmetic expressions.

1. A[[n]]s = N [[n]] 

2. A[[x]]s = s x 

Nielson - Semantics with Applications, An Appetizer 4


A[[a1 + a2 ]]s = A[[a1 ]]s + compositional definition; notice the
3.
​ ​ ​

A[[a2 ]]s ​  RHS +

4.

5.

B : Bexp → State → T , The semantic function for boolean expressions.

T = {tt, ff} 

1. B[[true]]s = tt 

2. B[[false]]s = ff 

tt, if A[[a1 ]]s = A[[a2 ]]s


3.  compositional
​ ​

B[[a1 = a2 ]]s
​ ​ = { ​

ff, if A[[a1 ]]s 


​ = A[[a2 ]]s

4.

5.

6.

FV : Num∣Var∣Aexp∣Bexp → 2
Var
, The free variables in an expression.

[Definition]
(WIP)

[Theorem] (Lemma 1.12) (Exercise 1.13)

(WIP)

[y ↦ a0 ] : Num∣Var∣Aexp∣Bexp → Num∣Var∣Aexp∣Bexp
​ , The substitution
for a variable y in an expression by another arithmetic expression a0 . ​

Use suffix notation.

Be distributive over all other constructs.

[y ↦ v] : State → State , The updating for states.

Use suffix notation

[Definition]
[Theorem] (Exercise 1.14) (Exercise 1.15) The relationship between substitutions and
updating:

Nielson - Semantics with Applications, An Appetizer 5


The Semantics Functions for Statements
Overview

1. Operational semantics - HOW to execute the programs. Different approaches of the


method are

a. Natural (or Big-step) semantics - ns -

b. Structural operational (or Small-step) semantics - sos -

c. Reduction (contextual) semantics

2. Denotational semantics - WHAT EFFECT of executing a program; Every syntactic


construct is mapped to a mathematical object compositionally. (e.g. the semantics
functions Aand B )

3. Axiomatic semantics -

I - Transition Systems and the semantics functions Sns , Ssos 


​ ​

[Definition] Configurations: A configuration is either a statement-state tuple ⟨S , s⟩or


a singleton state s. Conf = (Stm × State) ⨆ State 

A configuration of the form ⟨S , s⟩is called an intermediate configuration.

A configuration of the form sis called an terminal configuration.

[Definition] The transition relation →for Natural semantics.

→ is a relation on Conf defined by Table 2.1.

The first two rules are called axioms (schema).

The rest of the rules has, within each of them, a set of premises, a conclusion
and a set of conditions.

When we apply the rules and axioms to derive a transition ⟨S , s⟩ → s



, we obtain
a derivation tree, …

the root of which is ⟨S , s⟩ → s



,

the leaves instances of axioms,

the internal nodes conclusions of instatianted rules.

Nielson - Semantics with Applications, An Appetizer 6


[Definition] Properties for Natural semantics.

We say the execution of a statement S on a state s…

terminates iff there is a state s′ related by the configuration ⟨S , s⟩.

loops, otherwise.

Two statements S1 and S2 are semantically equivalent iff for all states s, s′ we

have ⟨S1 , s⟩​ → s



≡ ⟨S2 , s⟩ → s


.

[Definition] We say that ns - the relation →on Conf - is deterministic if for all
S , s, s1 , s2
​ we have ⟨S , s⟩
​ → s1 and ⟨S , s⟩
​ → s2 implies s1
​ ​ = s2 .

[Theorem] (Theorem 2.9) The ns →on Conf is deterministic.

[Definition] The semantic function Sns ​ : Stm → (State ↪ State) 

For each statement S the partial function Sns [[S ]]is given by ​

′ ′
s , if ⟨S , s⟩ → s
Sns [[S ]]s = {
​ ​ 
undef, otherwise

By Theorem 2.9, Sns is well-defined.


Example: If we denote Omega = while true do skip , then

Sns [[Omega]]s = undef



for any given state s.

[Definition] The transition relation ⇒for Structural operational semantics.

Nielson - Semantics with Applications, An Appetizer 7


⇒ is a relation on Conf defined by Table 2.2.

A configuration ⟨S , s⟩is stuck if it is not in the domain of ⇒. (Sometimes it is


convenient to write ⟨S , s⟩ ⇒ stuck )

The relation ⇒can be composed with itself to produce new relation ⇒i and ⇒∗ 
where:

γ0 ⇒

i
γi means there are isteps in the execution from γ0 to γi .
​ ​ ​

γ0 ⇒


γi means there are a finite number of steps from γ0 to γi .
​ ​ ​

[Definition] Properties for Structural operational semantics.

[Definition] A derivation sequence of a statement S starting in a state sis either

A finite path from γ0 to γi where γ0


​ ​ ​ = ⟨S , s⟩ and γi is a terminal

configuration (state) or stuck. In such case, we say the execution of ⟨S , s⟩


terminates.

A infinite path. In such case, we say the execution of ⟨S , s⟩loops.

[Definition] We say that sos - the relation ⇒on Conf - is deterministic if for all
S , s, γ1 , γ2​ we have ⟨S , s⟩
​ ⇒ γ1 and ⟨S , s⟩
​ ⇒ γ2 implies γ1
​ ​ = γ2 .

[Theorem] (Exercise 2.22) The sos ⇒on Conf is deterministic.

Two statements S1 and S2 are semantically equivalent iff for all states swe have

⟨S1 , s⟩ ⇒


γ ≡ ⟨S2 , s⟩ ⇒ ​

γ . (both terminates with the same

Nielson - Semantics with Applications, An Appetizer 8


configuration/stuck or both loops)

[Definition] The semantic function Ssos ​ : Stm → (State ↪ State) 

For each statement S the partial function Ssos [[S ]]is given by ​

′ ∗ ′
s , if ⟨S , s⟩ ⇒ s
Ssos [[S ]]s = {
​ ​ 
undef, otherwise

By Exercise 2.22, Ssos is well-defined.


[Theorem] (Theorem 2.26) For every S of While, Sns [[S ]] = Ssos [[S ]]
​ ​ .

II - Chain complete partial order and the semantics function Sds  ​

Direct style Denotational semantics (Table 5.1)

The function cond and FIX:

[Definition] The predicates: Pred = State → T 

[Definition] The domain: Dom = State ↪ State 

[Definition] The condition function: cond : Pred × Dom × Dom → Dom 


g1 s, if p s = tt
defined by cond(p, g1 , g2 ) 

​ ​ = { ​

g2 s, ​ if p s = ff

[Definition] The (least) fixed point of F : Dom → Dom 

where F g = cond(B[[b]], g ∘ Sds [[S ]], id) ​




[Definition] The fix operator: FIX : (Dom → Dom) → Dom 

The Domain Domand fixed point theory

[Definition] The ordering ⊑on the partial functions Dom = State ↪ State 

Nielson - Semantics with Applications, An Appetizer 9


g1 ⊑ g2
​ iff graph(g1 )
​ ​ ⊆ graph(g2 ) ​ .

[Definition] The bottom ⊥ ∈ Dom 

⊥ s = undef for all s.

[Theorem] (Lemma 5.25) (Dom, ⊑) is a ccpo (chain complete partially ordered
set)

The least upper bound ⊔Y of a chain Y is given by:

graph(⨆ Y ) = ⋃{ graph(g) ∣ g ∈ Y } 

[Definition] A function is monotone iff it preserves ⊑

[Definition] A monotone function is continuous iff it is monotone and it further


preserves the least upper bounds of nonempty chains:

⨆ {f d ∣ d ∈ Y } = f (⨆ Y ) 

[Theorem] (Theorem 5.37) Given a continuous function f : D → D on the ccpo


(D, ⊑, ⊥) . Then FIX f = ⨆{f
n
⊥ ∣ n ≥ 0} defines an element of D such that
the element is the least fixed point of f .

[Theorem] (Proposition 5.47) The semantics function Sds defined by Table 5.1 ​

defines a total function in Stm → Dom.

Properties for Denotational semantics.

Two statements S1 and S2 are semantically equivalent iff Sds [[S1 ]]


​ ​ ​ = Sds [[S2 ]]
​ ​ .

III - Assertions and the semantics and the axiomatic inference system ⊢p  ​

[Definition] Logical variables and program variables

[Definition] Predicates

Pred = State → T 

We use P , Q, I N V ,… to denote predicates.

Each boolean expression b ∈ Bexp defines a predicate B[[b]].

Predicates calculus:

1. P1 ∧ P2
​ 

2. P1 ∨ P2



3. ¬P 

4. P1 ⇒ P2
​ 

5. P [x ↦ A[[a]]] 

Nielson - Semantics with Applications, An Appetizer 10


It is convenient to depense with B[[⋯]]and A[[⋯]]inside square brackets and
preconditions, postconditions

[Lemma] (Exercise 9.7)

1. B[[b[x ↦ a]]] = B[[b]][x ↦ A[[a]]] for all band a

2. B[[b1 ∧ b2 ]] = B[[b1 ]] ∧ B[[b2 ]]


​ ​ ​ ​ 

3. B[[¬b]] = ¬B[[b]] 

[Definition] Assertions (Hoare Triples): {P } S {Q} 

P , predicate, the precondition

Q , predicate, the postcondition

S the statement

⊢p ​ {P } S {Q} means that the assertion is provable.

The inference system (Table 9.1)

Glossary
descriptive constructs or
name or abstraction properties
explanation definitions

While syntax: explanation: constructs:


1. 1. numerals 1. strings of

Nielson - Semantics with Applications, An Appetizer 11


n : Num  2. variables digits
2. 3. arithmetic 2. strings of
x : Var  expressions letters and
3. 4. boolean digits starting
a : Aexp  expressions with a letter
4. 5. 3. 4. 5. (given
b : Bexp  statements below)
5.
S : Stm 

a ::= 

b ::= 

S ::= 

the semantic
N [[_]] : Num → Z  function for
numerals

to each
variable the State =

s : State  state will Var → Z 


associate its
current value.

determination
A[[_]] : Aexp → State → Z 
of the value of
an arithmetic
A[[a]] : State → Z  Table 1.1
expressions
under a
A[[a]]s : Z 
specific state

determination
B[[_]] : Bexp → State → T 
of the value of
a boolean
B[[b]] : State → T  Table 1.2
expressions
under a
B[[b]]s : T 
specific state

Lemma 1.12
FV : Aexp∣Bexp → Var  free variables $1.4, page 16
Exercise 1.13

substitutions
[y ↦ a0 ] : Aexp → Aexp 
for expressions

updates for Exercise 1.14


[y ↦ v] : State → State  page 18
states (v : Z ) Exercise 1.15

Natural semantics for While - derivation Table 2.1 Theorem 2.9


trees

⟨S , s⟩ → s



Nielson - Semantics with Applications, An Appetizer 12


1.
[assns ] ​ 
2.
[skipns ] ​ 
3.
tt
[ifns ] ​

4.
ff
[ifns ] ​ 
5.

tt
[whilens ] ​

6.
ff
[whilens ] ​


Sns : Stm → State ↪ State 


The semantic

function for
Sns [[S ]] : State ↪ State 
Natural

Semantics
Sns [[S ]]s : State ∪ {⊥}



Structural semantics for While

⟨S0 , s0 ⟩ ⇒ ⟨S1 , s1 ⟩ ⇒ ⋯ ⇒ sn
​ ​ ​ 

1.
[asssos ] ​ 
2. Lemma 2.19
- derivation
[skip ]  Exercise 2.20
sos
trees

3. Exercise 2.21
- derivation
[compsos ]
1
 Exercise 2.22
sequences

4.
[compsos ]
2
​ 
5.

tt
[if ]
sos

6.
ff
[ifsos ] ​ 
7.
[whilesos ] ​ 

Ssos : Stm → State ↪ State 


Theorem 2.26

Lemma 2.27
Ssos [[S ]] : State ↪ State 
Lemma 2.28

Ssoc [[S ]]s : State ∪ {⊥}


​ 

Sds : Stm → State ↪ State


​  Denotational Table 5.1
semantics
Sds [[S ]] : State ↪ State



Nielson - Semantics with Applications, An Appetizer 13


Sds [[S ]]s : State ∪ {⊥}
​ 

cond : (State → T) × (State ↪ State) ×

(State ↪ State) → (State ↪ State) 

FIX :

((State ↪ State) → (State ↪ State)) →

(State ↪ State) 

Running Examples
Swap/Exchange

Factorial

Nielson - Semantics with Applications, An Appetizer 14

You might also like