COSE312: Compilers Lecture 14 Semantic Analysis (4) Hakjoo Oh 2017 Spring Hakjoo Oh COSE312 2017 Spring, Lecture 14 May 8, 2017 1 / 30
Denotational Semantics In denotational semantics, we are interested in the mathematical meaning of a program. Compositional semantics: The meaning of an expression is defined with the meanings of its immediate subexpressions. Hakjoo Oh COSE312 2017 Spring, Lecture 14 May 8, 2017 2 / 30
Denotational Semantics of Expressions 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 c x := a skip c 1; c 2 if b c 1 c 2 while b c A[[ a ]] : State Z A[[ n ]](s) = n A[[ x ]](s) = s(x) A[[ a 1 + a 2 ]](s) = A[[ a 1 ]](s) + A[[ a 2 ]](s) A[[ a 1 a 2 ]](s) = A[[ a 1 ]](s) A[[ a 2 ]](s) A[[ a 1 a 2 ]](s) = A[[ a 1 ]](s) A[[ a 2 ]](s) B[[ b ]] : State T B[[ true ]](s) = true B[[ false ]](s) = false B[[ a 1 = a 2 ]](s) = A[[ a 1 ]](s) = A[[ a 2 ]](s) B[[ a 1 a 2 ]](s) = A[[ a 1 ]](s) A[[ a 2 ]](s) B[[ b ]](s) = B[[ b ]](s) = false B[[ b 1 b 2 ]](s) = B[[ b 1 ]](s) B[[ b 2 ]](s) Hakjoo Oh COSE312 2017 Spring, Lecture 14 May 8, 2017 3 / 30
Denotational Semantics of Commands where C [ x := a ](s) = s[x A [ a ](s)] C [ skip ] = id C [ c 1 ; c 2 ] = C [ c 2 ] C [ c 1 ] C [ if b c 1 c 2 ] = cond(b [ b ], C [ c 1 ], C [ c 2 ]) C [ while b c ] = fix F { g(s) f(s) = true cond(f, g, h) = λs. h(h) f(s) = false F (g) = cond(b [ b ], g C [ c ], id) Hakjoo Oh COSE312 2017 Spring, Lecture 14 May 8, 2017 4 / 30
Denotational Semantics of Loops Note that the meaning of the while loop is the mathematical object (i.e. partial function in State State) that satisfies the equation: C [ while b c ] = cond(b [ b ], C [ while b c ] C [ c ], id). We can rewrite the equation: where C [ while b c ] = F (C [ while b c ]) F (g) = cond(b [ b ], g C [ c ], id). The meaning of the while loop is defined to be the fixed point of F : C [ while b c ] = fix F where fix F denotes the least fixed point of F. Hakjoo Oh COSE312 2017 Spring, Lecture 14 May 8, 2017 5 / 30
Example while (x = 0) skip F fix F Hakjoo Oh COSE312 2017 Spring, Lecture 14 May 8, 2017 6 / 30
Some Questions Does the least fixed point fix F exist? Is fix F unique? How to compute fix F? Hakjoo Oh COSE312 2017 Spring, Lecture 14 May 8, 2017 7 / 30
Fixed Point Theory Theorem Let f : D D be a continuous function on a CPO D. Then f has a (unique) least fixed point, fix (f), and fix (f) = n 0 The denotational semantics is well-defined if State State is a CPO, and f n ( ). F : (State State) (State State) is a continuous function. Hakjoo Oh COSE312 2017 Spring, Lecture 14 May 8, 2017 8 / 30
Plan Complete Partial Order Continuous Functions Least Fixed Point Hakjoo Oh COSE312 2017 Spring, Lecture 14 May 8, 2017 9 / 30
Partially Ordered Set Definition (Partial Order) We say a binary relation is a partial order on a set D iff is reflexive: p D. p p transitive: p, q, r D. p q q r = p r anti-symmetric: p, q D. p q q p = p = q We call such a pair (D, ) partially ordered set, or poset. Lemma If a partially ordered set (D, ) has a least element d, then d is unique. Hakjoo Oh COSE312 2017 Spring, Lecture 14 May 8, 2017 10 / 30
Exercise Let S be a non-empty set. Prove that (P(S), ) is a partially ordered set. Hakjoo Oh COSE312 2017 Spring, Lecture 14 May 8, 2017 11 / 30
Exercise Let X Y be the set of all partial functions from a set X to a set Y, and define f g iff Dom(f) Dom(g) x Dom(f). f(x) = g(x). Prove that (X Y, ) is a partially ordered set. Hakjoo Oh COSE312 2017 Spring, Lecture 14 May 8, 2017 12 / 30
Least Upper Bound Definition (Least Upper Bound) Let (D, ) be a partially ordered set and let Y be a subset of D. An upper bound of Y is an element d of D such that d Y. d d. An upper bound d of Y is a least upper bound if and only if d d for every upper bound d of Y. The least upper bound of Y is denoted by Y. Lemma If Y has a least upper bound d, then d is unique. Hakjoo Oh COSE312 2017 Spring, Lecture 14 May 8, 2017 13 / 30
Chain Definition (Chain) Let (D, ) be a poset and Y a subset of D. Y is called a chain if Y is totally ordered: Example y 1, y 2 Y.y 1 y 2 or y 2 y 1. Consider the poset (P({a, b, c}), ). Y 1 = {, {a}, {a, c}} Y 2 = {, {a}, {c}, {a, c}} Hakjoo Oh COSE312 2017 Spring, Lecture 14 May 8, 2017 14 / 30
Complete Partial Order (CPO) Definition (CPO) A poset (D, ) is a CPO, if every chain Y D has Y D. Definition (Complete Lattice) A poset (D, ) is a complete lattice, if every subset Y D has Y D. Lemma If (D, ) is a CPO, then it has a least element given by =. Hakjoo Oh COSE312 2017 Spring, Lecture 14 May 8, 2017 15 / 30
Examples Example Let S be a non-empty set. Then, (P(S), ) is a CPO. The lub Y for Y is Y. The least element is. Hakjoo Oh COSE312 2017 Spring, Lecture 14 May 8, 2017 16 / 30
Examples Example The poset (X Y, ) of all partial functions from a set X to a set Y, equipped with the partial order Dom(f) Dom(g) x Dom(f). f(x) = g(x) is a CPO (but not a complete lattice). The lub of a chain Y is the partial function f with Dom(f) = f i Y Dom(f i) and { fn (x) x Dom(f f(x) = i ) for some f i Y undef otherwise The least element = λx.undef. Hakjoo Oh COSE312 2017 Spring, Lecture 14 May 8, 2017 17 / 30
Monotone Functions Definition (Monotone Functions) A function f : D E between posets is monotone iff d, d D. d d = f(d) f(d ). Hakjoo Oh COSE312 2017 Spring, Lecture 14 May 8, 2017 18 / 30
Example Consider (P({a, b, c}), ) and (P({d, e}), ) and two functions f 1, f 2 : P({a, b, c}) P({d, e}) X {a, b, c} {a, b} {a, c} {b, c} {a} {b} {c} f 1 (X) {d, e} {d} {d, e} {d, e} {d} {d} {e} X {a, b, c} {a, b} {a, c} {b, c} {a} {b} {c} f 2 (X) {d} {d} {d} {e} {d} {e} {e} {e} Hakjoo Oh COSE312 2017 Spring, Lecture 14 May 8, 2017 19 / 30
Exercise Determine which of the following functionals of are monotone: 1 F 0 (g) = g. (State State) (State State) { g1 g 2 F 1 (g) = 1 = g 2 where g g 2 otherwise 1 g 2. { 3 g(s) s(x) 0 F 2 (g) = λs. s s(x) = 0 Hakjoo Oh COSE312 2017 Spring, Lecture 14 May 8, 2017 20 / 30
Properties of Monotone Functions Lemma Let (D 1, 1 ), (D 2, 2 ), and (D 3, 3 ) be CPOs. Let f : D 1 D 2 and g : D 2 D 3 be monotone functions. Then, g f : D 1 D 3 is a monotone function. Hakjoo Oh COSE312 2017 Spring, Lecture 14 May 8, 2017 21 / 30
Properties of Monotone Functions Lemma Let (D 1, 1 ) and (D 2, 2 ) be CPOs. Let f : D 1 D 2 be a monotone function. If Y is a chain in D 1, then f(y ) = {f(d) d Y } is a chain in D 2. Furthermore, f(y ) f( Y ). Hakjoo Oh COSE312 2017 Spring, Lecture 14 May 8, 2017 22 / 30
Continuous Functions Definition (Continuous Functions) A function f : D 1 D 2 defined on posets (D 1, 1 ) and (D 2, 2 ) is continuous if it is monotone and it preserves least upper bounds of chains: f(y ) = f( Y ) for all non-empty chains Y in D 1. If f( Y ) = f(y ) holds for the empty chain (that is, = f( )), then we say that f is strict. Hakjoo Oh COSE312 2017 Spring, Lecture 14 May 8, 2017 23 / 30
Properties of Continuous Functions Lemma Let f : D 1 D 2 be a monotone function defined on posets (D 1, 1 ) and (D 2, 2 ) and D 1 is a finite set. Then, f is continuous. Hakjoo Oh COSE312 2017 Spring, Lecture 14 May 8, 2017 24 / 30
Properties of Continuous Functions Lemma Let (D 1, 1 ), (D 2, 2 ), and (D 3, 3 ) be CPOs. Let f : D 1 D 2 and g : D 2 D 3 be continuous functions. Then, g f : D 1 D 3 is a continuous function. Hakjoo Oh COSE312 2017 Spring, Lecture 14 May 8, 2017 25 / 30
Least Fixed Points Definition (Fixed Point) Let (D, ) be a poset. A fixed point of a function f : D D is an element d D such that f(d) = d. We write fix (f) for the least fixed point of f, if it exists, such that f(fix (f)) = fix (f) d D. f(d) = d = fix (f) d Hakjoo Oh COSE312 2017 Spring, Lecture 14 May 8, 2017 26 / 30
Fixed Point Theorem Theorem (Kleene Fixed Point) Let f : D D be a continuous function on a CPO D. Then f has a least fixed point, fix (f), and where f n ( ) = fix (f) = n 0 f n ( ) { n = 0 f(f n 1 ( )) n > 0 Hakjoo Oh COSE312 2017 Spring, Lecture 14 May 8, 2017 27 / 30
Proof We show the claims of the theorem by showing that n 0 f n ( ) exists and it is indeed equivalent to fix (f). First note that n 0 f n ( ) exists because f 0 ( ) f 1 ( ) f 2 ( )... is a chain. We show by induction that n N.f n ( ) f n+1 ( ): f( ) ( is the least element) f n ( ) f n+1 ( ) = f n+1 ( ) f n+2 ( ) (monotonicity of f) Now, we show that fix (f) = n 0 f n ( ) in two steps: We show that n 0 f n ( ) is a fixed point of f: f( f n ( )) = f(f n ( )) n 0 n 0 = n 0 = n 0 f n+1 ( ) f n ( ) continuity of f Hakjoo Oh COSE312 2017 Spring, Lecture 14 May 8, 2017 28 / 30
Proofs We show that n 0 f n ( ) is smaller than all the other fixed points. Suppose d is a fixed point, i.e., f(d) = d. Then, f n ( ) d since n N.f n ( ) d: n 0 f 0 ( ) = d, f n ( ) d = f n+1 ( ) f(d) = d. Therefore, we conclude fix (f) = n 0 f n ( ). Hakjoo Oh COSE312 2017 Spring, Lecture 14 May 8, 2017 29 / 30
Well-definedness of the Semantics The function F F (g) = cond(b [ b ], g C [ c ], id) is continuous. Lemma Let g 0 : State State, p : State T, and define F (g) = cond(p, g, g 0 ). Then, F is continuous. Lemma Let g 0 : State State, and define F (g) = g g 0. Then F is continuous. Hakjoo Oh COSE312 2017 Spring, Lecture 14 May 8, 2017 30 / 30