Semantics with Applications - PowerPoint PPT Presentation

About This Presentation
Title:

Semantics with Applications

Description:

html://www.cs.tau.ac.il/~msagiv/courses/sem08.html. Textbooks:Winskel ... Usually defined using context free grammar contextual constraints. Semantics ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 43
Provided by: thoma423
Category:

less

Transcript and Presenter's Notes

Title: Semantics with Applications


1
Semantics with Applications
  • Mooly Sagiv
  • msagiv_at_post
  • Schrirber 317
  • 03-640-7606
  • html//www.cs.tau.ac.il/msagiv/courses/sem08.htm
    l
  • TextbooksWinskel
  • The Formal Semantics of Programming
    Languages
  • Types and Programming Languages Benjamin C.
    Pierce

2
Outline
  • Course requirements
  • What is semantics
  • Who needs semantics
  • Forms of semantics
  • Tentative Plan
  • Trace semantics
  • Introduction to operational semantics

3
Course Requirements
  • Prerequisites
  • Compiler Course
  • Basic set theory and logic
  • A theoretical course
  • Forms of induction
  • Domain theory
  • No algorithms
  • Grade
  • Course Notes 10
  • Assignments 60
  • Mostly theoretical with some programming
  • Home exam 30

4
Modern Programming Languages
  • Imperative
  • PL/1
  • Pascal
  • C
  • Object Oriented
  • C
  • Java
  • C
  • Functional
  • Scheme
  • ML
  • Ocaml
  • F
  • Haskel
  • Logic
  • Prolog

5
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

6
Benefits of Formal Semantics
  • Programming language design
  • hard-to-define hard-to-implementhard-to-use
  • Avoid design mistakes
  • Programming language implementation
  • Compiler Correctness
  • Correctness of program optimizations
  • Design of Static Analysis
  • Programming language understanding
  • Program correctness
  • Type checking
  • Program equivalence
  • Automatic generation of interpreter
  • Techniques used in software engineering

7
Desired Features of PL Semantics
  • Tractable
  • as simple as possible without losing the ability
    to express behavior accurately
  • Abstract
  • uncluttered by 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

8
Alternative Formal Semantics
  • Operational Semantics Plotkin, Kahn
  • The meaning of the program is described
    operationally
  • Trace based Semantics
  • Structural Operational Semantics
  • Natural 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

9
Tentative Plan
  • A simple programming language IMP
  • Natural Semantics of IMP
  • Structural operational Semantics of IMP
  • Denotational Semantics of IMP
  • Axiomatic Semantics
  • IMP
  • Non-Determinism and Parallelism
  • Rely Guarantee Axiomatic Semantics
  • Separation Logic
  • Type inference/checking

10
IMP A Simple Imperative Language
  • numbers N
  • Positive and negative numbers
  • n, m ? N
  • truth values Ttrue, false
  • locations Loc
  • X, Y ? Loc
  • arithmetic Aexp
  • a ? Aexp
  • boolean expressions Bexp
  • b ? Bexp
  • commands Com
  • c ? Com

11
Abstract Syntax for IMP
  • Aexp
  • a n X a0 a1 a0 a1 a0 ? a1
  • Bexp
  • b true false a0 a1 a0 ? a1 ?b b0
    ?b1 b0 ? b1
  • Com
  • c skip X a c0 c1 if b then c0
    else c1 while b do c

23?4-5
(2(3?4))-5
((23)?4))-5
12
Example Program
Y 1 while ?(X1) do Y Y X X X - 1
13
But what about semantics
14
Trace Based Semantics
  • For every program P define a set potential states
    ?(P)
  • Let ? be the set of finite and infinite traces
    over ?
  • ? ?(P) ? ?(P)?
  • The meaning of P is a set of maximal traces ?P??
    ?

15
Example Program
pc?1, x ?2 pc?2, x ?2 pc?3, x ?2 pc?2, x
?1 pc?3, x ?1 pc?2, x ?0 pc?4, x ?0
..
pc?1, x ?-7 pc?2, x ?-7 pc?4, x ?-7
1 while 2(Xgt0) do 3X X 1 4
16
Example Program
pc?1, x ?2 pc?2, x ?2 pc?3, x ?2 pc?2, x
?2 pc?3, x ?2 pc?2, x ?2 pc?3, x ?2 ?
..
1 while 2(true) do 3 skip 4
17
Limitations of trace based semantics
  • The program counter is an implementation detail
  • Equivalent programs do not necessarily have the
    same set of traces
  • Hard to define semantics by induction on the
    syntax
  • Hard to prove properties of the programming
    language

18
Chapter 2
  • Introduction to
  • Operational Semantics

19
Expression Evaluation
  • States
  • Mapping locations to values
  • ? - The set of states
  • ? Loc ? N
  • ?(X) ?Xvalue of X in ?
  • ? X ? 5, Y ? 7
  • The value of X is 5
  • The value of Y is 7
  • The value of Z is undefined
  • For a ? Exp, ? ??, n ? N,
  • lta, ?gt ? n
  • a is evaluated in ? to n

20
Evaluating (a0 a1) at ?
  • Evaluate a0 to get a number n0 at ?
  • Evaluate a1 to get a number n1 at ?
  • Add n0 and n1

21
Expression Evaluation Rules
  • Numbers
  • ltn, ?gt ? n
  • Locations
  • ltX, ?gt? ?(X)
  • Sums
  • Subtractions
  • Products

Axioms
22
Derivations
  • A rule instance
  • Instantiating meta variables with corresponding
    values

23
Derivation (Tree)
  • Axioms in the leafs
  • Rule instances at internal nodes

24
Computing a derivation
  • We write lta, ?gt ? n when there exists a
    derivation tree whose root is lta, ?gt ? n
  • Can be computed in a top-down manner
  • At every node try all derivations in parallel

5
16
21
25
Recap
  • Operational Semantics
  • The rules can be implemented easily
  • Define interpreter
  • Natural semantics

26
Equivalence of IMP expressions
iff
a0 ? a1
27
Boolean Expression Evaluation Rules
  • lttrue, ?gt ? true
  • ltfalse, ?gt ? false

28
Boolean Expression Evaluation Rules(cont)

29
Equivalence of Boolean expressions
iff
b0 ?b1
30
Extensions
  • Shortcut evaluation of Boolean expressions
  • Parallel evaluation of Boolean expressions
  • Other data types

31
The execution of commands
  • ltc, ?gt ? ?
  • c terminates on ? in a final state ?
  • Initial state ?0
  • ?0(X)0 for all X
  • Handling assignments ltX5, ?gt ? ?
  • ltX5, ?gt ? ?5/X

32
Rules for commands
  • ltskip, ?gt ? ?
  • Sequencing
  • Conditionals

Atomic
33
Rules for commands (while)
34
Example Program
Y 1 while ?(X1) do Y Y X X X - 1
35
Equivalence of commands
iff
c0 ?c1
36
Proposition 2.8
while b do c ? if b then (c while b do c) else
skip
37
Small Step Operational Semantics
  • The natural semantics defines evaluation in large
    steps
  • Abstracts computation time
  • It is possible to define a small step operational
    semantics
  • lta, ?gt ?1 lta, ?gt
  • one step of executing a in a state ? yields a
    in a state ?

38
SOS for Additions
39
SOS Rules for commands
  • ltskip, ?gt ? 1 ?
  • Sequencing

Atomic
40
SOS Rules for commands
  • Conditionals

41
SOS rules for while
ltwhile b do c, ?gt ?1 lt if b then (c while b do
c) else skip, ? gt
42
Summary
  • Operational semantics enables to naturally
    express program behavior
  • Can handle
  • Non determinism
  • Concurrency
  • Procedures
  • Object oriented
  • Pointers and dynamically allocated structures
  • But remains very closed to the implementation
  • Two programs which compute the same functions are
    not necessarily equivalent
Write a Comment
User Comments (0)
About PowerShow.com