Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Formal Semantics of Programming Languages
Topic 1: Introduction
虞慧群
yhq@ecust.edu.cn
Outline
What is semantics
Who needs semantics
Forms of semantics
Basic set theory
Modern Programming Languages
Imperative
PL/1
Pascal
C
Object Oriented
C++
Java
C#
Functional
Scheme
ML
Logic
Prolog
Programming Languages
Syntax
Which string is a legal program?
Usually defined using context free grammar+
contextual constraints
Semantics
What does a program mean?
What is the output of the program on a given run?
When does a runtime error occur?
A formal definition
Benefits of Formal Semantics
Programming language design
Programming language implementation
hard- to-define= hard-to-implement=hard-to-use
Avoid design mistakes
Compiler Correctness
Correctness of program optimizations
Design of Static Analysis
Programming language understanding
Program correctness
Program equivalence
Automatic generation of interpreter
Techniques used in software engineering
Desired Features of PL Semantics
Tractable
as simple as possible without losing the ability to express
behavior accurately
Abstract
uncluttered by removing irrelevant detail
Computational
an accurate abstraction from runtime behavior
Compositional
The meaning of compound language construct is defined
using the meaning of subconstructs
Supports modular reasoning
Alternative Formal Semantics
Operational Semantics [Plotkin]
Denotational Semantics [Strachey, Scott]
The meaning of the program is an input/output relation
Axiomatic Semantics [Floyd, Hoare]
The meaning of the program is described “operationally”
Structural Operational Semantics
The meaning of the program is observed properties
Proof rules to show that the program is correct
Complement each other
Basic Set Theory
Logical notation
Sets
Relations and functions
Logical notation
Conjunction: AB, A&B
Disjunction: AB, A or B
Negation: A
Implication: A B
Logical Equivalence: A B
‘There exists’: x.P(x), !x.P(x) - the uniqueness
‘For all’: x.P(x)
Sets
Sets and properties
X = { x | P(x)}
Russell’s paradox: R = { x | x x}
Set and element: aA
Subset: AB
Important set:
(empty set)
(natural numbers)
Constructions on sets
Comprehension: { x X | P(x)} or { x | x X & P(x)}
Powerset: Pow(X) = {Y | YX}
Indexed sets: {xi | i I}
Union: XY = {a | aX or a Y }
Intersection: XY = {a | aX & a Y }
Product: X Y = {(a,b)| aX) & b Y) }
Set difference: X\Y = {a | aX & a Y }
Relations and Functions
Binary relation
An element of Pow(XY ), or
A subset of XY
Partial Function f: XY
A relation f XY for which
x,y,y’.(x,y) f & (x,y’) f y =y’
Total function: for all x X, there is some y Y
s.t. f(x)=y.
Identity function: IdX = {(x,x) | x X}
Lambda notation
-notation
x X .e
Function f = {(x,e) | x X }
For example
x .(x+1) is the successor
Composing relations
Let R XY, and S YZ.
SR = {(x,z) XZ | y Y. (x,y) R & (y,z) S}
Properties
Let R XY, S YZ, and S ZW.
T(SR) = (TS)R
R IdX = IdY R = R
Let R be a relation on X.
R0 = IdX
Rn+1 = RRn
Equivalence relation
An equivalence relation is a relation R XX on
set X which is
Reflexive: xX. xRx
Symmetric: x,y. X. xRy yRx, and
Transitive: x,y,z. X. xRy & yRz xRz.
Equivalence class
{x}R = {yX | yRx}
Conclusion
Notion of semantics
Forms of semantics
Basic Mathematics
Exercise 1
1.1 Let R XY, S YZ, and S ZW.
Show T(SR) = (TS)R .
1.2 Let R be an equivalence relation on X.
Show if {x}R {y}R , then {x}R = {y}R for any
elements x, y X