Programming Language Semantics Denotational Semantics Chapter 5 Based on a lecture by Martin Abadi.

Slides:



Advertisements
Similar presentations
10 October 2006 Foundations of Logic and Constraint Programming 1 Unification ­An overview Need for Unification Ranked alfabeths and terms. Substitutions.
Advertisements

Discrete Mathematics Lecture 5 Alexander Bukharovich New York University.
The Engineering Design of Systems: Models and Methods
DATAFLOW PROCESS NETWORKS Edward A. Lee Thomas M. Parks.
1 Introduction to Computability Theory Lecture12: Decidable Languages Prof. Amos Israeli.
Lecture 02 – Structural Operational Semantics (SOS) Eran Yahav 1.
1 Introduction to Computability Theory Lecture3: Regular Expressions Prof. Amos Israeli.
Introduction to Computability Theory
Foundations of Data-Flow Analysis. Basic Questions Under what circumstances is the iterative algorithm used in the data-flow analysis correct? How precise.
Programming Language Semantics Denotational Semantics Chapter 5.
Worklist algorithm Initialize all d i to the empty set Store all nodes onto a worklist while worklist is not empty: –remove node n from worklist –apply.
1 Operational Semantics Mooly Sagiv Tel Aviv University Textbook: Semantics with Applications.
Lecture 8 Recursively enumerable (r.e.) languages
Programming Language Semantics Mooly SagivEran Yahav Schrirber 317Open space html://
Discrete Mathematics Lecture 4 Harper Langston New York University.
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://
Kahn’s Principle and the Semantics of Discrete Event Systems Xiaojun Liu EE290N Class Project December 10, 2004.
Abstract Interpretation Part I Mooly Sagiv Textbook: Chapter 4.
A Denotational Semantics For Dataflow with Firing Edward A. Lee Jike Chong Wei Zheng Paper Discussion for.
Administrative stuff Office hours: After class on Tuesday.
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.
Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson
Programming Language Semantics Denotational Semantics Chapter 5 Part III Based on a lecture by Martin Abadi.
Claus Brabrand, ITU, Denmark DATA-FLOW ANALYSISMar 25, 2009 Static Analysis: Data-Flow Analysis II Claus Brabrand IT University of Copenhagen (
Claus Brabrand, UFPE, Brazil Aug 09, 2010DATA-FLOW ANALYSIS Claus Brabrand ((( ))) Associate Professor, Ph.D. ((( Programming, Logic, and.
1 Program Analysis Mooly Sagiv Tel Aviv University Textbook: Principles of Program Analysis.
Induction and recursion
Partially Ordered Sets (POSets)
Relations Chapter 9.
Induction and recursion
Solving fixpoint equations
Chapter 8. Section 8. 1 Section Summary Introduction Modeling with Recurrence Relations Fibonacci Numbers The Tower of Hanoi Counting Problems Algorithms.
Program Analysis and Verification Noam Rinetzky Lecture 6: Abstract Interpretation 1 Slides credit: Roman Manevich, Mooly Sagiv, Eran Yahav.
Week 15 - Wednesday.  What did we talk about last time?  Review first third of course.
Chapter 9. Chapter Summary Relations and Their Properties n-ary Relations and Their Applications (not currently included in overheads) Representing Relations.
Program Analysis and Verification
MA/CSSE 474 Theory of Computation More Reduction Examples Non-SD Reductions.
The Integers. The Division Algorithms A high-school question: Compute 58/17. We can write 58 as 58 = 3 (17) + 7 This forms illustrates the answer: “3.
Relations, Functions, and Matrices Mathematical Structures for Computer Science Chapter 4 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesFunctions.
CS 611: Lecture 6 Rule Induction September 8, 1999 Cornell University Computer Science Department Andrew Myers.
April 14, 2015Applied Discrete Mathematics Week 10: Equivalence Relations 1 Properties of Relations Definition: A relation R on a set A is called transitive.
Universidad Nacional de ColombiaUniversidad Nacional de Colombia Facultad de IngenieríaFacultad de Ingeniería Departamento de Sistemas- 2002Departamento.
CS201: Data Structures and Discrete Mathematics I
Mathematical Preliminaries
Chapter 9. Chapter Summary Relations and Their Properties n-ary Relations and Their Applications (not currently included in overheads) Representing Relations.
Program Analysis and Verification Spring 2015 Program Analysis and Verification Lecture 13: Abstract Interpretation V Roman Manevich Ben-Gurion University.
Mathematical Induction Section 5.1. Climbing an Infinite Ladder Suppose we have an infinite ladder: 1.We can reach the first rung of the ladder. 2.If.
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:
Operational Semantics Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
Program Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
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.
Week 8 - Wednesday.  What did we talk about last time?  Relations  Properties of relations  Reflexive  Symmetric  Transitive.
Chapter 5 Relations and Operations
Fixpoints and Reachability
Lecture 5 Floyd-Hoare Style Verification
Concurrent Models of Computation
Lecture 10: Fixed Points ad Infinitum M.C. Escher, Moebius Ants
Presentation transcript:

Programming Language Semantics Denotational Semantics Chapter 5 Based on a lecture by Martin Abadi

Course Grade Course notes 10% Homework assignments 90%

Introduction Denotational semantics is supposed to be mathematical: –The meaning of an expression is a mathematical object –A fair amount of mathematics is involved Denotational semantics is compositional Denotational semantics is more abstract and canonical than operational semantics –No small step vs. big step Denotational semantics is also called –Fixed point semantics –Mathematical semantics –Scott-Strachey semantics

Plan Definition of the denotational semantics of IMP (first attempt) Complete partial orders and related properties –Montonicity –Continuity Definition of denotational semantics of IMP

Denotational semantics A: Aexp  (  N) B: Bexp  (  T) C: Com  (    ) Defined by structural induction

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

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   )

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

Denotational semantics of commands? Running a command c starting from a state  yields another state  ’ So, we may try to define C  c  as a function that maps  to  ’: –C .  : Com  (    )

Denotational semantics of commands? Problem: running a command might not yield anything if the command does not terminate We introduce the special element  to denote a special outcome that stands for non- termination For any set X, we write X  for X  {  } Convention: –whenever f  X  X  we extend f to X   X  “strictly” so that f(  ) = 

Denotational semantics of commands? We try: –C .  : Com  (      ) C  skip   =  C  c 0 ; c 1   = C  c 1  (C  c 0   ) C  if b then c0 else c1   = if B  b   then C  c 0   else C  c 1   C  while b do c   = ?

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

Denotational semantics of commands? Abbreviation W=C  while b do C  Idea: we rely on the equivalence while b do c  if b then (c; while b do c) else skip We may try using unwinding equation W(  ) = if B  b   then W(C  c   ) 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)

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? inputoutput

Motivation Let “isone” be a function that must return “1$” when the input string has at least a 1 and “0$” otherwise What should the result of “isone” be on the partial input “00.. 0” ? –It must be the empty string  – Monotonicity : Output is never retracted More information about the input is reflected in more information about the output How do we express monotonicity precisely?

Montonicity Define a partial order x  y –A partial order is reflexive, transitive, and antisymmetric –y is a refinement of x For streams of bits x  y when x is a prefix of y For programs, a typical order is: –No output (yet)  some output

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

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 

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)

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

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

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)

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

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 )

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)

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

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

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

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 ) For a flat cpo A, any monotonic function f: A   A  such that f is strict is continuous Chapter 8 of the textbook includes many more continuous functions

W(C  c   ) if B  b  (  )=true F(W)= .  if B  b  (  )=false  if B  b  (  )=  Fixed Points Solve the equation: where W:∑   ∑  W= C  while be do c  This equation can be written as W = F( W) with: W(C  c   ) if B  b  (  )=true W(  ) =  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

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 (  )

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

Denotational Semantics of IMP ∑  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

Denotational Semantics of IMP Recall that W is a fixed point of F:[[∑   ∑  ]  [∑   ∑  ]] F is continuous Thus, we set C  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(C  c  (  )) if B  b  (  )=true F(w) = .  if B  b  (  )=false  if B  b  (  )= 

Example(1) while true do skip F:[[∑   ∑  ]  [∑   ∑  ]] w(C  c  (  )) if B  b  (  )=true F = w. .  if B  b  (  )=false  if B  b  (  )=  B  true  =.true C  skip  =.  F = w. .w(  )

Example(1) while true do skip F:[[∑   ∑  ]  [∑   ∑  ]] F = w. .w(  ) Var ={x} C  while true do skip  =  F k (  )= .  .  . [x  1] . [x  2] .  .  (  (x)+1/x) . z if  (x)=2 3 if  (x)  2 . [x  3]

Example(2) while false do c F:[[∑   ∑  ]  [∑   ∑  ]] w(C  c  (  )) if B  b  (  )=true F = w. .  if B  b  (  )=false  if B  b  (  )=  B  false  =.false F = w. . 

Example(2) while true do skip F:[[∑   ∑  ]  [∑   ∑  ]] F = w. .  Var ={x} C  while false do C  =  F k (  )= .  .  . [x  1] . [x  2] .  .  (  (x)+1/x) . z if  (x)=2 3 if  (x)  2

Example(3) while x  3 do x = x -1 F:[[∑   ∑  ]  [∑   ∑  ]] B  x!=3  =.  (x)  3 F = w. . c  x=x-1  =.  (  (x)-1/x)) w(C  c  (  )) if B  b  (  )=true F = w. .  if B  b  (  )=false  if B  b  (  )=  w(  (  (x)-1/x))) if  (x)  3  if  (x)=3

Example(3) while x  3 do x = x -1 F = w. . B(  (  (x)-1/x))) if  (x)  3  if  (x)=3 F0()F0() .  F1()F1() . if  (x)=3 then  (3/x) else  F2()F2() . if 3  (x)  4 then  (3/x) else  Fk()Fk() . if 3  (x)  3+k-1 then  (3/x) else  kFk()kFk() . if 3  (x) then  (3/x) else 

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

Equivalence of Semantics ,  ’  :  ’=C  c     ’   *  ’

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

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}

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)

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