Download presentation
Presentation is loading. Please wait.
Published byReginald Elliott Modified over 9 years ago
1
Formal Semantics of Programming Languages 虞慧群 yhq@ecust.edu.cn Topic 1: Introduction
2
Outline What is semantics Who needs semantics Forms of semantics Basic set theory
3
Modern Programming Languages Imperative PL/1 Pascal C Object Oriented C++ Java C# Functional Scheme ML Logic Prolog
4
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
5
Benefits of Formal Semantics Programming language design hard- to-define= hard-to-implement=hard-to-use Avoid design mistakes Programming language implementation 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
6
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
7
Alternative Formal Semantics Operational Semantics [Plotkin] The meaning of the program is described “operationally” Structural Operational Semantics Denotational Semantics [Strachey, Scott] The meaning of the program is an input/output relation Axiomatic Semantics [Floyd, Hoare] The meaning of the program is observed properties Proof rules to show that the program is correct Complement each other
8
Basic Set Theory Logical notation Sets Relations and functions
9
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)
10
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)
11
Constructions on sets Comprehension: { x X | P(x)} or { x | x X & P(x)} Powerset: Pow(X) = {Y | Y X } Indexed sets: {x i | 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 }
12
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: Id X = {(x,x) | x X}
13
Lambda notation -notation x X.e Function f = {(x,e) | x X } For example x .(x+1) is the successor
14
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 Id X = Id Y R = R Let R be a relation on X. R 0 = Id X R n+1 = R R n
15
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 }
16
Conclusion Notion of semantics Forms of semantics Basic Mathematics
17
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.