Program Analysis and Verification 0368-4479

Slides:



Advertisements
Similar presentations
Continuing Abstract Interpretation We have seen: 1.How to compile abstract syntax trees into control-flow graphs 2.Lattices, as structures that describe.
Advertisements

8. Introduction to Denotational Semantics. © O. Nierstrasz PS — Denotational Semantics 8.2 Roadmap Overview:  Syntax and Semantics  Semantics of Expressions.
Tutorial on Widening (and Narrowing) Hongseok Yang Seoul National University.
Program Analysis and Verification
ISBN Chapter 3 Describing Syntax and Semantics.
1 Undecidability Andreas Klappenecker [based on slides by Prof. Welch]
Comp 205: Comparative Programming Languages Semantics of Imperative Programming Languages denotational semantics operational semantics logical semantics.
Lecture 02 – Structural Operational Semantics (SOS) Eran Yahav 1.
Foundations of Data-Flow Analysis. Basic Questions Under what circumstances is the iterative algorithm used in the data-flow analysis correct? How precise.
CPSC 411, Fall 2008: Set 12 1 CPSC 411 Design and Analysis of Algorithms Set 12: Undecidability Prof. Jennifer Welch Fall 2008.
Programming Language Semantics Denotational Semantics Chapter 5.
Programming Language Semantics Denotational Semantics Chapter 5 Based on a lecture by Martin Abadi.
1 Operational Semantics Mooly Sagiv Tel Aviv University Textbook: Semantics with Applications.
1 Undecidability Andreas Klappenecker [based on slides by Prof. Welch]
Programming Language Semantics Mooly SagivEran Yahav Schrirber 317Open space html://
Discrete Mathematics Lecture 4 Harper Langston New York University.
1 Iterative Program Analysis Part I Mooly Sagiv Tel Aviv University Textbook: Principles of Program.
Programming Language Semantics Denotational Semantics Chapter 5 Part II.
Data Flow Analysis Compiler Design Nov. 3, 2005.
1 Iterative Program Analysis Mooly Sagiv Tel Aviv University Textbook: Principles of Program Analysis.
Programming Language Semantics Mooly SagivEran Yahav Schrirber 317Open space html://
Abstract Interpretation Part I Mooly Sagiv Textbook: Chapter 4.
1 Program Analysis Mooly Sagiv Tel Aviv University Textbook: Principles of Program Analysis.
Data Flow Analysis Compiler Design Nov. 8, 2005.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs, Data-flow Analysis Data-flow Frameworks --- today’s.
Semantics with Applications Mooly Sagiv Schrirber html:// Textbooks:Winskel The.
Describing Syntax and Semantics
Programming Language Semantics Denotational Semantics Chapter 5 Part III Based on a lecture by Martin Abadi.
Program Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
1 Program Analysis Mooly Sagiv Tel Aviv University Textbook: Principles of Program Analysis.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
Solving fixpoint equations
Program Analysis and Verification Spring 2015 Program Analysis and Verification Lecture 2: Operational Semantics I Roman Manevich Ben-Gurion University.
1 Iterative Program Analysis Abstract Interpretation Mooly Sagiv Tel Aviv University Textbook:
Program Analysis and Verification Spring 2014 Program Analysis and Verification Lecture 11: Abstract Interpretation III Roman Manevich Ben-Gurion University.
Program Analysis and Verification Noam Rinetzky Lecture 6: Abstract Interpretation 1 Slides credit: Roman Manevich, Mooly Sagiv, Eran Yahav.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Albert Gatt LIN3021 Formal Semantics Lecture 4. In this lecture Compositionality in Natural Langauge revisited: The role of types The typed lambda calculus.
Universidad Nacional de ColombiaUniversidad Nacional de Colombia Facultad de IngenieríaFacultad de Ingeniería Departamento de Sistemas- 2002Departamento.
Program Analysis and Verification Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics I Roman Manevich Ben-Gurion University.
CS 267: Automated Verification Lecture 3: Fixpoints and Temporal Properties Instructor: Tevfik Bultan.
Programming Languages and Design Lecture 3 Semantic Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
Mathematical Preliminaries
Program Analysis and Verification Noam Rinetzky Lecture 2: Operational Semantics 1 Slides credit: Tom Ball, Dawson Engler, Roman Manevich, Erik.
Program Analysis and Verification Spring 2015 Program Analysis and Verification Lecture 13: Abstract Interpretation V Roman Manevich Ben-Gurion University.
Compiler Principles Fall Compiler Principles Lecture 7: Lowering Correctness Roman Manevich Ben-Gurion University of the Negev.
Sequence Control Syntax and Semantics Jian Xu March 3, 2004 CS706, CAS McMaster.
Operational Semantics Mooly Sagiv Tel Aviv University Textbook: Semantics with Applications Chapter.
Program Analysis and Verification
1 Iterative Program Analysis Abstract Interpretation Mooly Sagiv Tel Aviv University Textbook:
Program Analysis and Verification
Operational Semantics Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
Data Flow Analysis II AModel Checking and Abstract Interpretation Feb. 2, 2011.
Program Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
Program Analysis and Verification Noam Rinetzky Lecture 8: Abstract Interpretation 1 Slides credit: Roman Manevich, Mooly Sagiv, Eran Yahav.
1 Iterative Program Analysis Part II Mathematical Background Mooly Sagiv Tel Aviv University
Chaotic Iterations Mooly Sagiv Tel Aviv University Textbook: Principles of Program Analysis.
Chaotic Iterations Mooly Sagiv Tel Aviv University Textbook: Principles of Program Analysis.
Operational Semantics Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson
Operational Semantics Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson
Chapter 2 1. Chapter Summary Sets The Language of Sets - Sec 2.1 – Lecture 8 Set Operations and Set Identities - Sec 2.2 – Lecture 9 Functions and sequences.
Program Analysis and Verification Noam Rinetzky Lecture 2: Operational Semantics 1 Slides credit: Tom Ball, Dawson Engler, Roman Manevich, Erik.
Iterative Program Analysis Abstract Interpretation
Spring 2017 Program Analysis and Verification Operational Semantics
Program Analysis and Verification
Concurrent Models of Computation
Denotational Semantics (Denotational Semantics)
Spring 2016 Program Analysis and Verification Operational Semantics
Program Analysis and Verification
Presentation transcript:

Program Analysis and Verification Noam Rinetzky Lecture 4: Denotational Semantics Slides credit: Roman Manevich, Mooly Sagiv, Eran Yahav

Good manners Mobiles 2

Admin Grades – First home assignment will be published on Tuesday – (contents according to progress today) Due lesson 6 Scribes (this week) ?Scribes (next week) From now on – in singles 3

What do we mean? P syntax semantics P: x  7 4

Why formal semantics? Implementation-independent definition of a programming language Automatically generating interpreters (and some day maybe full fledged compilers) Verification and debugging – if you don’t know what it does, how do you know its incorrect? 5

Programming Languages Syntax “how do I write a program?” – BNF – “Parsing” Semantics “What does my program mean?” – … 6

Program semantics Operational: State-transformer Denotational: Mathematical objects Axiomatic: Predicate-transformer 7

Denotational semantics 8

Syntax: While Abstract syntax: a ::= n | x | a 1 + a 2 | a 1  a 2 | a 1 – a 2 b ::= true | false | a 1 = a 2 | a 1  a 2 |  b | b 1  b 2 S ::= x := a | skip | S 1 ; S 2 | if b then S 1 else S 2 | while b do S 9

Syntactic categories n  Numnumerals x  Varprogram variables a  Aexparithmetic expressions b  Bexpboolean expressions S  Stmstatements 10

Denotational semantics A: Aexp  (  N) B: Bexp  (  T) S: Stm  (    ) Defined by structural induction A  a , B  b , S ns  S , S sos  S  11

Semantic categories Z Integers {0, 1, -1, 2, -2, …} T Truth values { ff, tt } State Var  Z Example state:s=[ x  5, y  7, z  0] Lookup: s x = 5 Update: s[ x  6] = [ x  6, y  7, z  0] 12

Denotational Semantics A “mathematical” semantics –  S  is a mathematical object – A fair amount of mathematics is involved Compositional –  while b do S  = F(  b ,  S  ) Recall: More abstract and canonical than Op. Sem. – No notion of “execution” Merely definitions – No small step vs. big step Concurrency is an issue 13 if B  b  s = tt

Denotational Semantics 14

Important features Syntax independence: The denotations of programs should not involve the syntax of the source language. Soundness: All observably distinct programs have distinct denotations; Full abstraction: Two programs have the same denotations precisely when they are observationally equivalent. Compositionality 15

Plan Denotational semantics of While (1st attempt) Math – Complete partial orders – Monotonicity – Continuity Denotational semantics of while 16

Denotational semantics A: Aexp  (  N) B: Bexp  (  T) S: Stm  (    ) Defined by structural induction – Compositional definition 17

Denotational semantics A: Aexp  (  N) B: Bexp  (  T) S: Stm  (    ) Defined by structural induction – Compositional definition 18 A  a , B  b , S ns  S , S sos  S 

Denotational semantics of Aexp A: Aexp  (  N) A  n  = {( , n) |   } A  X  = {( ,  X) |   } A  a 0 +a 1  = {( , n 0 +n 1 ) | ( , n 0 )  A  a 0 , ( ,n 1 )  A  a 1  } A  a 0 -a 1  = {( , n 0 -n 1 ) | ( , n 0 )  A  a 0 , ( ,n 1 )  A  a 1  } A  a 0  a 1  = {( , n 0  n 1 ) | ( , n 0 )  A  a 0 , ( ,n 1 )  A  a 1  } Lemma: A  a  is a function 19 Functions represented as sets of pairs

Denotational semantics of Aexp with A: Aexp  (  N) A  n  = .n A  X  = .  (X) A  a 0 +a 1  = .(A  a 0   +A  a 1   ) A  a 0 -a 1  = .(A  a 0   -A  a 1   ) A  a 0  a 1  = .(A  a 0    A  a 1   ) 20 Functions represented as lambda expressions

Denotational semantics of Bexp B: Bexp  (  T) B  true  = {( , true) |   } B  false  = {( , false) |   } B  a 0 =a 1  = {( , true) |   & A  a 0   =A  a 1   }  {( , false) |   & A  a 0   A  a 1   } B  a 0  a 1  = {( , true) |   & A  a 0    A  a 1   }  {( , false) |   & A  a 0    A  a 1   } B  b  = {( ,  T t) |  , ( , t)  B  b  } B  b 0  b 1  = {( , t 0  T t 1 ) |  , ( , t 0 )  B  b 0 , ( , t 1 )  B  b 1  } B  b 0  b 1  = {( , t 0  T t 1 ) |  , ( , t 0 )  B  b 0 , ( , t 1 )  B  b 1  } Lemma: B  b  is a function 21

Denotational semantics of statements? Intuition: – Running a statement s starting from a state  yields another state  ’ Can we define S  s  as a function that maps  to  ‘ ? – S .  : Stm  (    ) 22

Denotational semantics of commands? 23

Denotational semantics of statements? We try: – S .  : Stm  (      ) S  skip   =  S  s 0 ; s 1   = S  s 1  (S  s 0   ) S  if b then s 0 else s 1   = if B  b   then S  s 0   else S  s 1   24

Examples S  X:= 2; X:=1   =  [X  1] S  if true then X:=2; X:=1 else …   =  [X  1] – The semantics does not care about intermediate states – So far, we did not explicitly need  25

Denotational semantics of loops? S  while b do s   = ? 26

Denotational semantics of loops? Goal: Find a function from states to states such which defines the meaning of W Intuition: – while b do s  – if b then (s; while b do s) else skip 27

Denotational semantics of loops? Goal: Find a function from states to states such which defines the meaning of W Intuition: – S  while b do s  = – S  if b then (s; while b do s) else skip  28

Denotational semantics of loops? Goal: Find a function from states to states such which defines the meaning of W Intuition: – S  while b do s  = – S  if b then (s; while b do s) else skip  29

Denotational semantics of loops? Abbreviation W=S  while b do s  Solution 1: – W(  ) = if B  b   then W(S  s   ) else  Unacceptable solution – Defines W in terms of itself – It not evident that a suitable W exists – It may not describe W uniquely (e.g., for while true do skip) 30

Denotational semantics of loops? Goal: Find a function from states to states such which defines the meaning of W Approach: Solve domain equation – S  while b do s  = – S  if b then (s; while b do s) else skip  31

Introduction to Domain Theory We will solve the unwinding equation through a general theory of recursive equations Think of programs as processors of streams of bits (streams of 0’s and 1’s, possibly terminated by $) What properties can we expect? input output 32

Motivation Let “isone” be a function that must return “1$” when the input string has at least a 1 and “0$” otherwise – isone(00…0$) = 0$ – isone(xx…1…$) =1$ – isone(0…0) =? Monotonicity: in terms of information – Output is never retracted More information about the input is reflected in more information about the output – How do we express monotonicity precisely? 33

Montonicity Define a partial order x  y – A partial order is reflexive, transitive, and anti-symmetric – y is a refinement of x “more precise” For streams of bits x  y when x is a prefix of y For programs, a typical order is: – No output (yet)  some output 34

Montonicity A set equipped with a partial order is a poset Definition: – D and E are postes – A function f: D  E is monotonic if  x, y  D: x  D y  f(x)  E f(y) – The semantics of the program ought to be a monotonic function More information about the input leads to more information about the output 35

Montonicity Example Consider our “isone” function with the prefix ordering Notation: – 0 k is the stream with k consecutive 0’s – 0  is the infinite stream with only 0’s Question (revisited): what is isone(0 k )? – By definition, isone(0 k $) = 0$ and isone(0 k 1$) = 1$ – But 0 k  0 k $ and 0 k  0 k 1$ – “isone” must be monotone, so: isone( 0 k )  isone( 0 k $) = 0$ isone( 0 k )  isone( 0 k 1$) = 1$ – Therefore, monotonicity requires that isone(0 k ) is a common prefix of 0$ and 1$, namely  36

Motivation Are there other constraints on “isone”? Define “isone” to satisfy the equations – isone(  )=  – isone(1s)=1$ – isone(0s)=isone(s) – isone($)=0$ What about 0  ? Continuity: finite output depends only on finite input (no infinite lookahead) – Intuition: A program that can produce observable results can do it in a finite time 37

Chains A chain is a countable increasing sequence = {x i  X | x 0  x 1  … } An upper bound of a set if an element “bigger” than all elements in the set The least upper bound is the “smallest” among upper bounds: – x i   for all i  N –   y for all upper bounds y of and it is unique if it exists 38

Complete Partial Orders Not every poset has an upper bound – with   n and n  n for all n  N – {1, 2} does not have an upper bound Sometimes chains have no upper bound …  210210 The chain 0  1  2  … does not have an upper bound 39

Complete Partial Orders It is convenient to work with posets where every chain (not necessarily every set) has a least upper bound A partial order P is complete if every chain in P has a least upper bound also in P We say that P is a complete partial order (cpo) A cpo with a least (“bottom”) element  is a pointed cpo (pcpo) 40

Examples of cpo’s Any set P with the order x  y if and only if x = y is a cpo It is discrete or flat If we add  so that  x for all x  P, we get a flat pointed cpo The set N with  is a poset with a bottom, but not a complete one The set N  {  } with n  is a pointed cpo The set N with  is a cpo without bottom Let S be a set and P(S) denotes the set of all subsets of S ordered by set inclusion – P(S) is a pointed cpo 41

Constructing cpos If D and E are pointed cpos, then so is D × E (x, y)  D×E (x’, y’) iff x  D x’ and y  E y’  D×E = (  D,  E )  (x i, y i ) = (  D x i,  E y i ) 42

Constructing cpos (2) If S is a set of E is a pcpos, then so is S  E m  m’ iff  s  S: m(s)  E m’(s)  S  E = s.  E  (m, m’ ) = s.m(s)  E m’(s) 43

Continuity A monotonic function maps a chain of inputs into a chain of outputs: x 0  x 1  …  f(x 0 )  f(x 1 )  … It is always true that:  i  f(  i ) But f(  i )   i is not always true 44

A Discontinuity Example 32103210 11 f(  i )   i 45

Continuity Each f(x i ) uses a “finite” view of the input f(  ) uses an “infinite” view of the input A function is continuous when f(  ) =  i The output generated using an infinite view of the input does not contain more information than all of the outputs based on finite inputs 46

Continuity Each f(x i ) uses a “finite” view of the input f(  ) uses an “infinite” view of the input A function is continuous when f(  ) =  i The output generated using an infinite view of the input does not contain more information than all of the outputs based on finite inputs Scott’s thesis: The semantics of programs can be described by a continuous functions 47

Examples of Continuous Functions For the partial order ( N  {  },  ) – The identity function is continuous id(  n i ) =  id(n i ) – The constant function “five(n)=5” is continuous five(  n i ) =  five(n i ) – If isone(0  ) =  then isone is continuos For a flat cpo A, any monotonic function f: A   A  such that f is strict is continuous Chapter 8 of the Wynskel textbook includes many more continuous functions 48

Fixed Points Solve equation: where W:∑   ∑  ; W= S  while be do s  Alternatively, W = F(W) where: F(W) = . W(S  s   ) if B  b  (  )=true W(  ) =  if B  b  (  )=false  if B  b  (  )=  49 { W(S  s   ) if B  b  (  )=true  if B  b  (  )=false  if B  b  (  )=  {

Fixed Point (cont) Thus we are looking for a solution for W = F( W) – a fixed point of F Typically there are many fixed points We may argue that W ought to be continuous W  [∑   ∑  ] Cut the number of solutions We will see how to find the least fixed point for such an equation provided that F itself is continuous 50

Fixed Point Theorem Define F k = x. F( F(… F( x)…)) (F composed k times) If D is a pointed cpo and F : D  D is continuous, then – for any fixed-point x of F and k  N F k (  )  x – The least of all fixed points is  k F k (  ) Proof: i.By induction on k. Base: F 0 (  ) =   x Induction step: F k+1 (  ) = F( F k (  ))  F( x) = x ii.It suffices to show that  k F k (  ) is a fixed-point F(  k F k (  )) =  k F k+1 (  ) =  k F k (  ) 51

Fixed-Points (notes) If F is continuous on a pointed cpo, we know how to find the least fixed point All other fixed points can be regarded as refinements of the least one – They contain more information, they are more precise – In general, they are also more arbitrary 52

Fixed-Points (notes) If F is continuous on a pointed cpo, we know how to find the least fixed point All other fixed points can be regarded as refinements of the least one – They contain more information, they are more precise – In general, they are also more arbitrary – They also make less sense for our purposes 53

Denotational Semantics of While ∑  is a flat pointed cpo – A state has more information on non-termination – Otherwise, the states must be equal to be comparable (information- wise) We want strict functions ∑   ∑  – therefore, continuous functions The partial order on ∑   ∑  f  g iff f(x) =  or f(x) = g(x) for all x  ∑  – g terminates with the same state whenever f terminates – g might terminate for more inputs 54

Denotational Semantics of While Recall that W is a fixed point of F:[[∑   ∑  ]  [∑   ∑  ]] F is continuous Thus, we set S  while b do c  =  F k (  ) – Least fixed point Terminates least often of all fixed points Agrees on terminating states with all fixed point w(S  s  (  )) if B  b  (  )=true F(w) = .  if B  b  (  )=false  if B  b  (  )=  55 {

Denotational Semantics of While S  skip  = .  S  X := exp  = .  [X  A  exp   ] S  s 0 ; s 1  = . S  s 1  (S  s 0   ) S  if b then s 0 else s 1  = . if B  b   then S  s 0   else S  s 1   S  while b do s  =  F k (  ) – k=0, 1, … – F = w. . if B  b  (  )=true w(S  s  (  )) else  56

Example(1) while true do skip F:[[∑   ∑  ]  [∑   ∑  ]] w(S  s  (  )) if B  b  (  )=true F = w. .  if B  b  (  )=false  if B  b  (  )=  B  true  =.true S  skip  =.  F = w. .w(  ) F 0 (  )=   F 1 (  ) =   F 2 (  ) =  = =  57 {

Example(2) while false do s F:[[∑   ∑  ]  [∑   ∑  ]] B  false  =.false F = w. .  F 0 (  )=   F 1 (  ) = .   F 2 (  ) = .  = .  58 w(S  s  (  )) if B  b  (  )=true F = w. .  if B  b  (  )=false  if B  b  (  )=  {

Example(3)  while x  3 do x = x -1  =  F k (  ) k=0, 1, … where F = w. . if  (x)  3 w(  [x   (x) -1]) else  F0()F0()  F1()F1() if  (x)  3  (  [x   (x) -1]) else  if  (x)  3 then  else  F2()F2() if  (x)  3 then F 1 (  [x   (x) -1] ) else  if  (x)  3 then (if  [x   (x) -1] x  3 then  else  [x   (x) -1] ) else  if  (x)  3 (if  (x)  4 then  else  [x   (x) -1] ) else  if  (x)  {3, 4} then  [x  3] else  F k (  ) lfp(F) if  (x)  {3, 4, …k} then  [x  3] else  if  (x)  3 then  [x  3] else  59

Example 4 Nested Loops P == Z := 0 ; while X > 0 do ( Y := X; while (Y>0) do Z := Z + Y ; Y: = Y- 1; ) X = X – 1 ) 60

Example 4 Nested Loops P == Z := 0 ; while X > 0 do ( Y := X; while (Y>0) do Z := Z + Y ; Y: = Y- 1; ) X = X – 1 ) s  inner-loop  = [Y  0][Z   (Z)+  (Y) * (  (Y)+1)/2] if  (Y)  0  if  (Y)<0 s  outer-loop  = [Y  0] [X  0] [Z   (Z)+  (X)  (  (X) + 1)  (1 + (2  (X) + 1)/3)/4 ] if  (X)  0  if  (X)<0 sS=sS= [Y  0] [X  0] [Z   (X)  (  (X) + 1)  (1 + (2  (X) + 1)/3)/4 ] if  (X)  0  if  (X)<0 61 { { {

Equivalence of Semantics ,  ’  :  ’=S  s     ’   *  ’ 62

Complete Partial Orders Let (D,  ) be a partial order – D is a complete lattice if every subset has both greatest lower bounds and least upper bounds 63

Knaster-Tarski Theorem Let f: L  L be a monotonic function on a complete lattice L The least fixed point lfp(f) exists – lfp(f) =  {x  L: f(x)  x} 64

Fixed Points A monotone function f: L  L where (L, , , , ,  ) is a complete lattice Fix(f) = { l: l  L, f(l) = l} Red(f) = {l: l  L, f(l)  l} Ext(f) = {l: l  L, l  f(l)} – l 1  l 2  f(l 1 )  f(l 2 ) Tarski’s Theorem 1955: if f is monotone then: – lfp(f) =  Fix(f) =  Red(f)  Fix(f) – gfp(f) =  Fix(f) =  Ext(f)  Fix(f)   f(  ) f(  ) f2()f2() f2()f2() Fix(f) Ext(f) Red(f) gfp(f) lfp(f) 65

Summary Denotational definitions are not necessarily better than operational semantics, and they usually require more mathematical work The mathematics may be done once and for all The mathematics may pay off: Some of its techniques are being transferred to operational semantics. It is trivial to prove that – If B  b 1  = B  b 2  and C  c 1  = C  c 2  – Then C  while b 1 do c 1  = C  while b 2 do c 2  compare with the operational semantics 66

Summary Denotational semantics provides a way to declare the meaning of programs in an abstract way side-effects loops Recursion Gotos non-determinism – But not low level concurrency Fixed point theory provides a declarative way to specify computations – Many usages 67

The End 68