Download Formal Semantics of Programming Languages

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Formal Semantics of Programming Languages
Topic 1: Introduction
虞慧群
[email protected]
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: AB, A&B
Disjunction: AB, 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: aA
Subset: AB
Important set:


(empty set)
(natural numbers)
Constructions on sets

Comprehension: { x X | P(x)} or { x | x X & P(x)}


Powerset: Pow(X) = {Y | YX}
Indexed sets: {xi | i I}
Union: XY = {a | aX or a Y }
Intersection: XY = {a | aX & a Y }
Product: X  Y = {(a,b)| aX) & b Y) }

Set difference: X\Y = {a | aX & a  Y }



Relations and Functions

Binary relation



An element of Pow(XY ), or
A subset of XY
Partial Function f: XY
 A relation f  XY 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  XY, and S  YZ.
 SR = {(x,z)  XZ | y  Y. (x,y) R & (y,z)  S}
Properties
Let R  XY, S  YZ, and S  ZW.
 T(SR) = (TS)R


R  IdX = IdY R = R
Let R be a relation on X.
 R0 = IdX

Rn+1 = RRn
Equivalence relation


An equivalence relation is a relation R  XX on
set X which is

Reflexive: xX. xRx

Symmetric: x,y. X. xRy  yRx, and

Transitive: x,y,z. X. xRy & yRz  xRz.
Equivalence class

{x}R = {yX | yRx}
Conclusion



Notion of semantics
Forms of semantics
Basic Mathematics
Exercise 1
1.1 Let R  XY, S  YZ, and S  ZW.
Show T(SR) = (TS)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
Related documents