APPENDIX A Denotational semantics: proofs We show that every closed term M has a computable functional [[M ] as its denotation. A.1. Unification We show that for any two constructor terms one can decide whether there exists a unifier, and if so, compute a most general one. A solution of this problem has been given by Robinson (1965). In the formulation of the algorithm below we follow Martelli and Montanari (1982). By a constructor term P, Q (term for short) we mean a term built from variables x, y, z and constructors C by application. A substitution is a finite set ϑ = {P 1 /x 1,..., P n /x n } of pairs of variables and terms, such that x i x j for i j, and P i x i for all i. An element P i /x i of ϑ is called a binding (of x i to P i ). By P ϑ we denote the result of simultaneously replacing each variable x i in P by P i, and call P ϑ the instance of P induced by ϑ. We shall use ϑ, η, ζ for substitutions. Let ε be the empty substitution. For given substitutions ϑ = {P 1 /x 1,..., P n /x n } η = {Q 1 /y 1,..., Q m /y m }, the composition ϑη of ϑ and η is the substitution obtained by deleting in the set {P 1 η/x 1,..., P n η/x n, Q 1 /y 1,..., Q m /y m } all bindings P i η/x i such that P i η = x i, and also all bindings Q j /y j such that y j {x 1,..., x n }. A substitution ϑ is idempotent if ϑϑ = ϑ. A substitution ϑ is called more general than η (written η ϑ), if there is a substitution ζ such that η = ϑζ. ϑ and η are equivalent, if ϑ η ϑ. It is easy to see that (P ϑ)η = P (ϑη), and that composition is associative. We now come to the unification problem. By this we mean the question whether for two given terms P, Q there is a substitution ϑ unifying the two terms, i.e., with the property P ϑ = Qϑ. Let E denote finite equation systems, i.e., multisets {P 1 = Q 1,..., P n = Q n } 59
60 A. DENOTATIONAL SEMANTICS: PROOFS of equations between terms (more precisely pairs of terms). Consider { } as a (contradictory) equation system. A substitution ϑ unifies E, if for every equation P = Q in E we have P ϑ = Qϑ; no ϑ unifies { }. ϑ is a most general unifier (mgu) of E, if ϑ is a unifier of E and η ϑ for every unifier η of E. The following characterization of idempotent mgus will be useful in the proof of the Unification Theorem below. Lemma (Characterization of idempotent mgu s). Let ϑ be a unifier of E. Then ϑ is an idempotent mgu of E iff η = ϑη for all unifiers η of E. Proof. Assume that ϑ is a unifier of E.. Let ϑ be an idempotent mgu of E, and assume that η is a unifier of E. Since ϑ is a mgu of E, we have η = ϑζ for some substitution ζ. Hence η = ϑζ = ϑϑζ = ϑη.. Assume that η = ϑη for all unifiers η of E. Now let η be a unifier of E. Then η ϑ; therefore ϑ is a mgu. Since ϑ is a unifier, by assumption we have ϑ = ϑϑ. Definition (Unification algorithm). E ϑ E is defined by (a) {P = x} E ε {x = P } E, if P is not a variable. (b) {x = x} E ε E. (c) {CP 1... P n = CQ 1... Q n } E ε {P 1 = Q 1,... P n = Q n } E. (d) {CP 1... P n = C Q 1... Q n } E ε { } if C C. (e) {x = P, P 1 (x) = Q 1 (x),..., P n (x) = Q n (x)} {P/x} {P 1 (P ) = Q 1 (P ),... P n (P ) = Q n (P )} if x / FV(P ). (f) {x = P } E ε { }, if x FV(P ) and P x. Proposition. Assume E ϑ E. (a) If η is a unifier of E, then ϑη is a unifier of E. (b) If η is a unifier of E, then η = ϑη and η is a unifier of E. Proof. By cases according to the definition of E ϑ E. Clearly it suffices to treat case (e). Let η be a unifier of E. Then {P/x}η is a unifier of E. Let η be a unifier of E. Then xη = P η, hence η = {P/x}η (since both substitutions coincide on all variables), and moreover Hence η is a unifier of E. Corollary. Assume P i {P/x}η = P i η = Q i η = Q i {P/x}η. E 1 ϑ1 E 2 ϑ2... E n ϑn E n+1. (a) If ϑ is a unifier of E n+1, then ϑ 1... ϑ n ϑ is a unifier of E 1.
A.2. IDEALS AS DENOTATIONS OF TERMS 61 (b) If η is a unifier of E 1, then η = ϑ 1... ϑ n η and η is a unifier of E n+1. Proof. The first part clearly follows from the first part of the Proposition. The second part is proved by induction on n. For n = 0 there is nothing to show. In the step we split the assumption into E 1 ϑ1 E 2 and E 2 ϑ2... E n ϑn E n+1. By the second part of the Proposition we have that η = ϑ 1 η is a unifier of E 2. Hence by IH η = ϑ 2 ϑ n η is a unifier of E n+1. Moreover we have η = ϑ 1 η = ϑ 1 ϑ 2... ϑ n η. Unification Theorem. Let E be a finite equation system. Then every sequence E = E 1 ϑ1 E 2 ϑ2... terminates with E n+1 = or E n+1 = { }. In the first case E is unifiable, and ϑ 1... ϑ n is an idempotent mgu of E. In the second case E is not unifiable. Proof. We first show termination using the lexicographic ordering of N 3. To every E = {P 1 = Q 1,..., P n = Q n } assign a triple (n 1, n 2, n 3 ) N 3 by n 1 := number of variables in E, n 2 := number of occurrences of variables and constructors in E, n 3 := number of equations P = x in E such that P is not a variable. In every step E ϑ E the assigned triple decreases w.r.t. the lexicographic ordering of N 3. This can be verified easily by considering the different cases: For (a), n 1, n 2 remain unchanged, and n 3 decreases. For (b), (c), (d) and (f), n 2 decreases, and n 1 does not increase. For (e), n 1 decreases. Hence our given sequence E 1 ϑ1 E 2 ϑ2... terminates with E n ϑn E n+1. Then it is easy to see that either E n+1 = or E n+1 = { }. Case E n+1 =. By the Corollary ϑ 1... ϑ n is a unifier of E, and by the Proposition we have η = ϑ 1... ϑ n η for every unifier η of E. Hence by the characterization of idempotent mgu s ϑ 1... ϑ n is an idempotent mgu of E. Case E n+1 = { }. Then by the proposition E is not unifiable. A.2. Ideals as denotations of terms Recall the definition of the relation ( U, a) [λ M ] in Section 2.3 The height of a derivation of ( U, a) [λ M ] is defined as usual, by adding 1 at each rule. We define its D-height similarly, where only rules (D) count.
62 A. DENOTATIONAL SEMANTICS: PROOFS We begin with some simple consequences of this definition. The following transformations preserve D-height: (12) (13) (14) (15) V U ( U, a) [λ M ] ( V, a) [λ M ], ( U, V, a) [λ,y M ] ( U, a) [λ M ] if y / FV(M), ( U, V, a) [λ,y (My)] ( U, V, a) [λ M ] if y / FV(M), ( U, V, a) [λ, y (M( P ( y )))] ( U, P ( V ), a) [λ, z (M( z ))]. Proof. (12) and (13) are both proved by easy inductions on the respective derivations. (14). Assume ( U, V, a) [λ,y (My)]. By (A) we then have W such that ( U, V, W ) [λ,y y ] (i.e., V W ) and ( U, V, W, a) [λ,y M ]. By (12) from the latter we obtain ( U, V, V, a) [λ,y M ]. Now since y / FV(M), (13) yields ( U, V, a) [λ M ], as required. Conversely, assume ( U, V, a) [λ M ]. Since y / FV(M), (13) yields ( U, V, V, a) [λ M ]. Clearly we have ( U, V, V ) [λ,y y ]. Hence by (A) ( U, V, a) [λ,y (My)], as required. Notice that the D-height did not change in these transformations. (15). By induction on P, with a side induction on M. We distinguish cases on M. The cases x i, C and D are follow immediately from (13). In case M N the following are equivalent by induction hypothesis: ( U, V, a) [λ, y ((MN)( P ( y )))] W (( U, V, W ) [λ, y (N( P ( y )))] ( U, V, W, a) [λ, y (M( P ( y )))]) W (( U, P ( V ), W ) [λ, y (N( z ))] ( U, P ( V ), W, a) [λ, y (M( z ))]) ( U, P ( V ), a) [λ, y ((MN)( z ))]. The final case is where M is z i. Then we have to show ( U, V, a) [λ, y (P ( y ))] P ( V ) a. We distinguish cases on P ( y ). If P ( y ) is y j, then both sides are equivalent to V j a. In case P ( y ) is (C Q)( y ) the following are equivalent, using the induction hypothesis for Q( y ) ( U, V, a) [λ, y ((C Q)( y ))] ( U, V, a) [λ, y (C Q( y ))] ( U, Q( V ), a) [λ, u (C u )] ( U, Q( V ), a) [λ C] by (14) a (a = C a Q( V ) a )
A.2. IDEALS AS DENOTATIONS OF TERMS 63 C Q( V ) a. Let denote the equivalence relation on formal neighborhoods generated by entailment, i.e., U V means (U V ) (V U). (16) If U P ( V ), then there are W such that U P ( W ) and W V. Proof. By induction on P. The cases x and are clear, and in case P, Q we can apply the induction hypothesis. It remains to treat the case CP ( ). Since U CP ( V ) there is a b 0 such that C b 0 U. Let U i := { a a (C a U a = a i ) }. For the constructor pattern C consider C U. By definition C U = { C a a i U i if U i, and a i = otherwise }. We first show U CU. Assume C a CU. For each i, if U i, then there is an a i such that C a i U and a ii = a i, and if U i = then a i =. Hence U { C a i U i } {C b 0 } C a. Conversely assume C a U. We define Cb CU by b i = a i if a i, b i = if U i =, and otherwise (i.e., if a i = and U i ) take an arbitrary b i U i. Clearly {Cb } C a. By definition U P ( V ). Hence by induction hypothesis there are W such that U P ( W ) and W V. Therefore U CU CP ( W ). Lemma (Unification). If P 1 ( V 1 ) P n ( V n ), then P 1,..., P n are unifiable with a most general unifier ϑ and there exists W such that ( P 1 ϑ)( W ) = = ( P n ϑ)( W ) P 1 ( V 1 ) P n ( V n ). Proof. Assume P 1 ( V 1 ) P n ( V n ). Then P 1 ( V 1 ),..., P n ( V n ) are componentwise consistent and hence P 1,..., P n are unifiable with a most general unifier ϑ. We now proceed by induction on P 1,..., P n. If they are either all empty or all variables the claim is trivial. In the case ( P 1, P 1 ),..., ( P n, P n ) it follows from the linearity condition on variables that a most general unifier of ( P 1, P 1 ),..., ( P n, P n ) is the union of most general unifiers of P 1,..., P n and of P 1,..., P n. Hence the induction hypothesis applies. In the case CP 1,..., CP n the assumption CP 1 ( V 1 ) CP n ( V n ) implies P 1 ( V 1 ) P n ( V n ) and hence again the induction hypothesis applies. The remaining case is when some are variables and the other ones of the form CP i, say x, CP 2,..., CP n. By assumption V 1 C P 2 ( V 2 ) C P n ( V n ).
64 A. DENOTATIONAL SEMANTICS: PROOFS By induction hypothesis we obtain the required W such that ( P 2 ϑ)( W ) = = ( P n ϑ)( W ) P 2 ( V 2 ) P n ( V n ). We need a final preparation before we can tackle consistency of [λ M ]. The information systems C ρ enjoy the pleasant property of coherence, which amounts to the possibility to locate inconsistencies in two-element sets of data objects. Generally, an information system A = (A, Con, ) is coherent if it satisfies: U A is consistent if and only if all of its two-element subsets are. Lemma. Let A and B be information systems. If B is coherent, then so is A B. Proof. Let A = (A, Con A, A ) and B = (B, Con B, B ) be information systems, and consider {(U 1, b 1 ),..., (U n, b n )} Con A B. Assume 1 i<j n ({(U i, b i ), (U j, b j )} Con). We have to show {(U 1, b 1 ),..., (U n, b n )} Con. Let I {1,..., n} and i I U i Con A. We must show { b i i I } Con B. Now since B is coherent by assumption, it suffices to show that {b i, b j } Con B for all i, j I. So let i, j I. By assumption we have U i U j Con A, and hence {b i, b j } Con B. By a similar argument we can prove Lemma (Coherence). The information systems C ρ are all coherent. Proof. By induction of the height U of consistent finite sets of tokens in C ρ, as defined in parts (c) and (d) of the definition in 2.1.5. Lemma (Consistency). [λ M ] is consistent. Proof. Let ( U i, a i ) [λ M ] for i = 1, 2. By coherence it suffices to prove that ( U 1, a 1 ) and ( U 2, a 2 ) are consistent. We shall prove this by induction on the maximum of the D-heights and a side induction on the maximum of the heights. Case (V). Let ( U 1, a 1 ), ( U 2, a 2 ) [λ x i ], and assume that U 1 and U 2 are componentwise consistent. Then U 1i a 1 and U 2i a 2. Since U 1i U 2i is consistent, a 1 and a 2 must be consistent as well. Case (C). For i = 1, 2 we have V i a i ( U i, V i, C a i ) [λ C]. Assume U 1, V 1 and U 2, V 2 are componentwise consistent. The consistency of C a 1 and C a 2 follows from V i a i and the consistency of V 1 and V 2.
A.2. IDEALS AS DENOTATIONS OF TERMS 65 Case (A). For i = 1, 2 we have ( U i, V i, a i ) [λ M ] ( U i, V i ) [λ N ] ( U. i, a i ) [λ (MN)] Assume U 1 and U 2 are componentwise consistent. By the side induction hypothesis for the right premises V 1 V 2 is consistent. Hence by the side induction hypothesis for the left hand sides a 1 and a 2 are consistent. Case (D). For i = 1, 2 we have ( U i, V i, a i ) [λ, yi M i ( y i )] Wi P i ( V i ) ( U i, W (D) i, a i ) [λ D ] for computation rules DP i ( y i ) = M i ( y i ). Assume U 1, W 1 and U 2, W 2 are componentwise consistent; we must show that a 1 and a 2 are consistent. Since W 1 W 2 P i ( V i ) for i = 1, 2, by (16) there are V 1, V 2 such that V i V i and W 1 W 2 P i ( V i ). Then by the unification lemma there are W such that ( P 1 ϑ)( W ) = ( P 2 ϑ)( W ) P i ( V i ) P i ( V i ) for i = 1, 2, where ϑ is the most general unifier of P 1 and P 2. But then also and hence by (12) we have with lesser D-height. Now (15) gives ( y i ϑ)( W ) V i, ( U i, ( y i ϑ)( W ), a i ) [λ, yi M i ( y i )] ( U i, W, a i ) [λ, z M i ( y i )ϑ] without increasing the D-height. Notice that M 1 ( y i )ϑ = M 2 ( y i )ϑ by our condition on computation rules. Hence the induction hypothesis applied to ( U 1, W, a 1 ), ( U 2, W, a 2 ) [λ, z M 1 ( y 1 )ϑ] implies the consistency of a 1 and a 2, as required. Lemma (Deductive closure). [λ M ] is deductively closed, i.e., if W [λ M ] and W ( V, b), then ( V, b) [λ M ]. Proof. By induction on the maximum of the D-heights and a side induction on the maximum of the heights of W [λ M ]. We distinguish cases on the last rule of these derivations (which is determined by M). Case (V). For all ( U, a) W we have U i a ( U, a) [λ x i ].
66 A. DENOTATIONAL SEMANTICS: PROOFS We must show V i b. By assumption W ( V, b), hence W V b. It suffices to prove V i W V. Let c W V ; we show V i c. There are U such that V U and ( U, c) W. But then by the above U i c, hence V i U i c. Case (A). Let W = {( U 1, a 1 ),..., ( U n, a n )}. For each ( U i, a i ) W there is U i such that ( U i, U i, a i ) [λ M ] ( U i, U i ) [λ N ] ( U. i, a i ) [λ (MN)] Define U := { U i V U i }. We first show that U is consistent. Let a, b U. There are i, j such that a U i, b U j and V U i, U j. Then U i and U j are consistent; hence by the consistency of [[λ N ] proved above a and b are consistent as well. Next we show ( V, U) [λ N ]. Let a U; we show ( V, a) [λ N ]. Fix i such that a U i and V U i, and let W i := { ( U i, b) b U i } [λ N ]. Since by the side induction hypothesis [[λ N ] is deductively closed it suffices to prove W i ( V, a), i.e., { b b U i V U i } a. But the latter set equals U i, and a U i. Finally we show ( V, U, b) [λ M ]. Let W := {( U 1, U 1, a 1 ),..., ( U n, U n, a n )} [λ M ]. By side induction hypothesis it suffices to prove that W ( V, U, b), i.e., { a i V U i U U i } b. But by definition of U the latter set equals { a i V U i }, which in turn entails b because by assumption W ( V, b). Now we can use (A) to infer ( V, b) [λ M ], as required. Case (C). Assume W [λ C ]. Then W consists of ( U, U, C a ) such that U a. Assume further W ( V, V, b). Then { C a U, U (( U, U, C a ) W V U V U ) } b. By definition of entailment b has the form C b such that W i := { a U, U, a (a = a i ( U, U, C a ) W V U V U ) } b i. We must show ( V, V, Cb ) [λ C], i.e., V b. It suffices to show V i W i, for every i. Let a W i. Then there are U, U, a such that a = a i, ( U, U, C a ) W and V U. Hence V i U i a i = a. Case (D). Let W = {( U 1, U 1, a 1),..., ( U n, U n, a n )}. For every i there is an U i such that ( Ui, U i, a i) [λ, yi M i ( y i )] U i P i ( U i ) ( U i, U i, a i) [λ D ]
A.3. PRESERVATION OF VALUES 67 for D P i ( y i ) = M i ( y i ) a computation rule. Assume W ( V, V, b). We must prove ( V, V, b) [λ D ]. Let I := { i 1 i n V U i V U i }. Then { a i i I } b, hence I. For i I we have V U i P i ( U i ), hence by (16) there are V i such that V P i ( V i ) and V i U i. In particular for i, j I V P i ( V i ) P j ( V j ). To simplify notation assume I = {1,..., m}. Hence by the unification lemma P 1,..., P m are unifiable with a most general unifier ϑ and there exists W such that ( P 1 ϑ)( W ) = = ( P m ϑ)( W ) P 1 ( V 1) P m ( V m). Let i, j I. Then by the conditions on computation rules M i ϑ = M j ϑ. Also ( y i ϑ)( W ) V i U i. Therefore by (12) and hence by (15) ( V, ( y i ϑ)( W ), a i ) [λ, yi M i ( y i )] ( V, W, a i ) [λ, yi M i ( y i ϑ)]. But M i ( y i ϑ) = M i ϑ = M 1 ϑ = M 1 ( y 1 ϑ) and hence for all i I ( V, W, a i ) [λ, yi M 1 ( y 1 ϑ)]. Therefore X := { ( V, W, a i ) i I } [λ, yi M 1 ( y 1 ϑ)]. Since { a i i I } b, we have X ( V, W, b) and hence the induction hypothesis implies ( V, W, b) [λ, yi M 1 ( y 1 ϑ)]. Using (15) again we obtain ( V, ( y i ϑ)( W ), b) [λ, yi M 1 ( y 1 )]. Since V P 1 ( V 1 ) P 1 (( y 1 ϑ)( W )) we obtain ( V, V, b) [λ D ], by (D). Corollary. [λ M ] is an ideal. A.3. Preservation of values We now prove that our definition above of the denotation of a term is reasonable in the sense that it is not changed by an application of the standard (β- and η-) conversions or a computation rule. For the β-conversion part of this proof it is helpful to first introduce a more standard notation, which involves variable environments. Definition. Assume that all free variables in M are among. [M ] := { b ( U, b) [λ M ] } and [[M ] u, V, y := U, U u [M ] V, y. Let
68 A. DENOTATIONAL SEMANTICS: PROOFS From (13) we obtain [[M ] U,V,y = [M ] U if y / FV(M), and similarly for ideals u, v instead of U, V. We have a useful monotonicity property, which follows from the deductive closure of [[λ M ]. Lemma. (a) If V U, a b and a [M ] U, then b [M ] V. (b) If v u, a b and a [M ] u, then b [M ] v. Proof. (a) V U, a b and ( U, a) [λ M ] together imply ( V, b) [λ M ], by the deductive closure of [λ M ]. (b) follows from (a). Lemma. (a) [x i ] = U i and [x i ] u = u i. (b) [λ y M ] U U,V = { (V, b) b [M ],y } and [λ y M ] u = { (V, b) b [M ] u,v,y }. (c) [MN ] = [M ] [N ] and [MN ] u = [M ] u [N ] u. Proof. (b) It suffices to prove the first part. But (V, b) [λ y M ] and U,V b [M ],y are both equivalent to ( U, V, b) [λ,y M ]. (c) For the first part we argue as follows. c [M ] U [N ] U V [[N]] U ((V, c) [M ] U ) V (( U, V ) [λ N ] ( U, V, c) [λ M ]) ( U, c) [λ (MN)] c [MN ] U. The second part is an easy consequence: by (A) c [M ] u [N ] u V [[N]] u ((V, c) [M ] u ) V [[N]] u U u ((V, c) [M ] U ) U1 u U U V [[N]] 1 U u ((V, c) [M ] ) ( ) U u V [[N]] U ((V, c) [M ] U ) U u (c [M ] U [N ] U ) U u (c [MN ] U ) c [MN ] u. by the first part Here is the proof of the equivalence marked ( ). The upward direction is obvious. For the downward direction we use monotonicity. Assume U 1 u, U V [N ] 1, U u and (V, c) [M ]. Let U 2 := U 1 U u. Then by U monotonicity V [N ] 2 and (V, c) [M ] U 2.
A.3. PRESERVATION OF VALUES 69 Corollary. [λ y M ] u v = [M ] u,v,y. Proof. b [λ y M ] u v V v((v, b) [λ y M ] u ) V v (b [M ] u,v,y ) b [M ] u,v,y. by the lemma, part (b) Lemma (Substitution). [M(z)] u,[[n]] u,z = [M(N)] u. Proof. By induction on M, and cases on the form of M. Case λ y M. For readability we leave out and u. [λ y M(z)] [[N]] z The other cases are easy. = { (V, b) b [M(z)] [[N]],V z,y } = { (V, b) b [M(N)] V y } by induction hypothesis = [λ y M(N)] by the last lemma, part (b) = [(λ y M)(N)]. Lemma (Preservation of values, β). [(λ y M(y))N ] u = [M(N)] u. Proof. Again we leave out, u. By the last two lemmata and the corollary, [(λ y M(y))N ] = [λ y M(y)][[N ] = [M(y)] [[N]] y = [M(N)]. Lemma (Preservation of values, η). [λ y (My)] u = [M ] u Proof. (V, b) [λ y (My)] u U u (( U, V, b) [λ,y (My)]) U u (( U, V, b) [λ M ]) by (14) (V, b) [M ] u. if y / FV(M).