<<

Notes on and Automata

Uday S. Reddy

November 9, 1994

In this article, I define a semantics for Algol programs with Reynolds’s syntactic control of interference (?; ?) in terms of comonoids in coherent spaces (also called correletation spaces).

1 Background: Monoids

1.1 Definition A (in ) is a triple M = (M, 1, ·) where • M is a set, • 1 ∈ M is an element called the unit, and • · : M × M → M is a binary called multiplication such that the following identities hold: • x · 1= x = 1 · x, for all x ∈ M, and • (x · y) · z = x · (y · z), for all x,y,z ∈ M. Note that the unit is necessarily unique. We often omit the · operator and write x · y as simply xy. The structure is called a if we don’t insist on the unit being present. It is called a commutative monoid if, in addition, • xy = yx, for all x, y ∈ M. Suppose only some instances of the commutativity equation hold so that the structure is a “partially commutative” monoid. Then, we can define a ∼⊆ M × M such that x ∼ y ⇐⇒ xy = yx. Evidently, ∼ is reflexive and symmetric. A zero in a monoid is an element 0 ∈ M such that 0x =0= x0 for all x ∈ M. Note that 0 is necessarily unique. A submonoid of M is a M ′ ⊆ M containing 1 and closed under multiplication. If x ∈ M, we use the notation xn for the n-fold product x ··· x (with x0 = 1). x∗ denotes the n ∗ set { x : n ≥ 0 }. Similarly, if S ⊆ M, S denotes the set {x1 ··· xn : x1,...,xn ∈ S }. Note that the S∗ is the least submonoid of M including S. If S∗ = M, we say that S is a set of generators for M.

1.2 Examples The following examples will be of much interest to our discussion: (i) Let Q be a set. The set of partial functions from Q to Q (called transformations of Q) forms a monoid. The identity transformation 1Q is the unit and the composition f; g of transforma- tions is the multiplication. We find it convenient to use postfix notation for transformations. Then, the above operations are defined by

q1Q = q (qf)g, if qf is defined q(f; g) = (undefined, otherwise

1 We denote this monoid by [Q → Q]. It has a zero element, viz., the undefined transformation. Any submonoid of [Q → Q] is called a transformation monoid. (ii) More generally, in any C, the on an object A form a monoid, denoted EndC(A). So, the set of binary relations over Q and the set of total functions from Q to Q form monoids too. (iii) Let Σ be a set (of “symbols”). The set of strings over Σ, denoted Σ∗ is a monoid with as the multiplication and the empty string ǫ as the unit. It is called the generated by Σ. Similarly, the set of nonempty strings over Σ (Σ+) is the free semigroup generated by Σ, and the set of finite over Σ is the free commutative monoid generated by Σ. Some more examples of passing interest are as follows: (iv) The set of natural numbers ω forms a monoid with multiplication as the and the 1 as the unit. This is, in fact, a commutative monoid. (v) The set of natural numbers, again, forms a commutative monoid with addition as the binary operation and 0 as the unit. This is isomorphic, in the sense to be made precise below, to the monoid {1}∗ (the free monoid generated by {1}). (vi) The set of n × n matrices forms a monoid under matrix multiplication as the multiplication and the identity matrix as the unit. This is, of course, a special case of the endomorphisms in a category example mentioned above. The following examples give important constructions on monoids: (vii) If M = (M, 1, ·) is a monoid, its dual monoid is (M, 1, ∗) where ∗ is defined by x ∗ y = y · x. Thus, the notion of monoid is a symmetric concept. (viii) Let M be a monoid. For each x ∈ M, there corresponds a right multiplication operator Rx : M → M defined by zRx = zx. The set of right multiplication operators forms a monoid, in fact, a transformation monoid over M. The unit is R1 and the multiplication is Rx · Ry = Rxy. In fact, the right multiplication monoid is isomorphic to the monoid itself. Similarly, the left multiplication operators Lx : M → M are defined by Lx(z) = xz. They have a unit L1 and multiplication Lx · Ly = Lyx. Thus, the left multiplication operators form a monoid that is isomorphic to the dual of M. (ix) Let M be a monoid. An equivalence relation ≡ ⊆ M × M is called a (monoid) if x ≡ x′ ∧ y ≡ y′ =⇒ xy ≡ x′y′. A congruence class is a maximal set X ⊆ M such that all the elements of X are equivalent to each other. The congruence class containing an element x is [x] = { y : y ≡ x }. The set of such congruence classes forms a monoid. The unit element is [1] and the multiplication of equivalence classes is defined by: [x] · [y]=[xy]. The fact that ≡ is a congruence relation ensures that multiplication is well-defined (as may be verified). This monoid is called the quotient monoid of M under ≡, denoted M/≡. (x) A pair (Σ, ∼), where Σ is a set and ∼⊆ Σ×Σ a symmetric relation, is called an independence alphabet (and ∼ its independence relation). Consider strings over Σ and the least congruence ≡ such that a ∼ b =⇒ ab ≡ ba. A congruence class [a1 ··· an] consists of all strings which only differ in the relative order of consecutive independent symbols. Such congruence classes are called traces and the quotient monoid Σ∗/≡ is called the free partially commutative monoid generated by Σ. (xi) Let Σ be an alphabet and S ⊆ Σ∗ be a set of strings. S induces an equivalence on strings by x ≈ y ⇐⇒ ∀z,z′. (zxz′ ∈ S ⇐⇒ zyz′ ∈ S). This is, in fact, a congruence relation, called the syntactic congruence or the Myhill congruence of S. Suppose x ≈ x′ and y ≈ y′. Then, zxx′z′ ∈ S ⇐⇒ zyx′z′ ∈ S from the first assumption and zyx′z′ ∈ S ⇐⇒ zyy′z′ ∈ S from the second assumption. Hence, xx′ ≈ yy′. Intuitively, x and y are equivalent if they behave

2 the same way as segments of strings in S. For example, let Σ = {a, b} and S be the regular set ab∗. Under the Myhill congruence of S, b ≈ bn+1 and a2 ≈ an+2 ≈ ban+1. Thus, the congruence classes are [ǫ], [a], [b] and [a2]. [ǫ] is the unit. [a2] is a zero. For the others, multiplication is defined by [a] · [b]=[a] and [b] · [a]=[a2]. The monoid Σ∗/≈ is called the of S. More generally, a subset of any monoid S ⊆ M induces a syntactic congruence and a syntactic monoid. (xii) If S ⊆ Σ∗ is a set of strings, one can also define a syntactic right congruence on Σ∗ by x ≈r y iff ∀z. (xz ∈ S ⇐⇒ yz ∈ S). This is a right congruence relation in the sense that x ≈r y =⇒ xv ≡ yv for all v ∈ Σ∗. For example, for S = ab∗, the right congruence classes are [ǫ], [a] (containing all abn) and [a2] (containing all bn). Let the set of right congruence classes be Q. Then, each string x ∈ Σ∗ has a corresponding right multiplication operator on Q, defined by [z]Rx = [zx]. The set of right multiplication operators Rx forms a monoid with unit R1 and multiplication Rx · Ry = Rxy. This is in fact a transformation monoid over Q. As will be seen below, such a transformation monoid is nothing but an automaton.

1.3 Definition A monoid h : (M, 1, ·) → (M ′, 1′, ·′) is a h : M → M ′ such that (i) h(1) = 1′, and (ii) h(x · y)= h(x) ·′ h(y) for all x, y ∈ M. A one-one homomorphism is called a monomorphism and an onto homomorphism is called an epimorphism.1 If there is an inverse homomorphism h−1 such that h ◦ h−1 = h−1 ◦ h = id then h is called an isomorphism, and M and M ′ are said to be isomorphic. Whenever h : M → M ′ is a homomorphism, the image of h, defined by Im(h) = { y : ∃x ∈ M. h(x)= u }, is a submonoid of M ′. Dually, the equivalence relation ≡ on M defined by x ≡ y ⇐⇒ h(x)= h(y) is a congruence relation (called the kernel of h). The homomorphism h′ : M/≡ → M ′ given by h′([x]) = h(x), is then a mono. Therefore, M/≡ and Im(h) are isomorphic. If f : Σ → M is a function, it extends to a unique homomorphism h : Σ∗ → M such that h restricted to Σ is f. We can define it explicitly by h(a1 ··· an)= f(a1) ··· f(an). If h is a surjection, M is isomorphic to the quotient monoid Σ∗/≡ where ≡ is the kernel of h. More generally, if M has a set of generators S, to specify a homomorphism h : M → M ′, we only need to specify it on S.

2 Background: Automata

2.4 Definition Let Σ be a set (the alphabet or the instruction set of the automaton). A Σ- automaton is a pair A = (Q, F ) where Q is a set (the state set) and F : Σ → [Q → Q] is a function called the interpretation (mapping instructions to transformations of Q). We also call A an automaton class of type Σ, and denote this fact by writing A = (Q, F )Σ. This kind of a machine is what is traditionally called a “” (because we have no distinguished start and final states), except that the function F is often treated as a partial function of type F : Q × Σ → Q and called a transition function. This is just an uncurried version of our interpretation function. More significantly, note that we have no finiteness restrictions on Q or Σ. So, our automata are not necessarily finite state.

1The latter is not an epi in the categorical sense.

3 We often write F (σ) as Fσ. The application of transformations is written in postfix: Fσ(q) as qFσ. These conventions achieve much economy and will also be seen to be very natural with respect to monoids. F extends to a unique monoid homomorphism Σ∗ → [Q → Q]. We denote this by F as well:

Fa1···an = Fa1 ··· Fan Note that the image of F is a transformation monoid over Q. ∗ ′ We also extend F to sets S ⊆ Σ . FS is a function FS : Q → PQ defined by qFS = { q : ∃x ∈ ′ S, qFx = q }. So, qFS is the set of states reachable from q via instruction from S. An automaton is said to be monogenic if there is a state q0 ∈ Q such that all states are reachable from q0, i.e., q0FΣ∗ = Q.

2.5 Definition Let A = (Q, F ) be a Σ-automaton. An A-machine is a pair M = (A, q) where q ∈ Q. A is called the class of the machine and q its current state. The function of a machine is to define a set: ∗ L(M)= qFΣ∗ = { x ∈ Σ : qFx defined } L(M) is traditionally called the language of the machine, but one can also think of it as the set of instruction sequences which the machine successfully executes. Note that L(M) is prefix-closed. Another kind of machine is a difference machine M = (A,i,t) where i and t are states, called the initial and terminal states respectively. The language of a difference machine is ∗ L(M)= { x ∈ Σ : qFx = t } A recognizer M = (A, i, T ) is like a difference machine except that there is a set of terminal states T ⊆ Q. The language of a recognizer is defined similarly: ∗ L(M)= { x ∈ Σ : qFx ∈ T }

2.6 Let L ⊆ Σ∗ be a set. Consider the syntactic right congruence of L: x ≈r y ⇐⇒ ∀z, (xz ∈ L ⇐⇒ yz ∈ L). Let Q be the set of right congruence classes. As noted in 1.2, the right multiplication operators of Σ∗ form a transformation monoid over Q. If R : Σ∗ → [Q → Q] is defined by [z]Rx =[zx], then A = (Q, R) is a Σ-automaton. It is monogenic with q0 =[ǫ]. By considering a suitable set of terminal states T = { [z] : z ∈ L }, we have a recognizer M = (A, q0, T ) with L(M)= L. This is, in fact, the minimal recognizer with language L. Thus, we obtain a recognizer for any language L ⊆ Σ∗. This recognizer is, in fact, unique upto renaming of states.

2.7 Next, we show that L uniquely determines the automaton class itself. The effect of an instruction x ∈ Σ∗ is to take the automaton from one state to another. So, if two sequences have the same interpretation in the automaton, they are equivalent for the automaton. This equivalence coincides with the syntactic congruence of L. If A = (Q, F ) is an automaton, we have that Fx = Fy =⇒ x ≈L(M) y for all machines of class ′ ′ A. Suppose Fx = Fy. To say that zxz ∈ L(M) is to say that there are states q, q ∈ Q such that ′ ′ ′ ′ iFz = q, qFx = q and q Fz′ ∈ T . Since Fx = Fy, clearly, zxz ∈ L(M) ⇐⇒ zyz ∈ L(M). Thus, x ≈L(M) y. If L ⊆ Σ∗, consider the right-congruence automaton A = (Q, R) constructed in 2.6. Suppose ′ ′ r x ≈L y, i.e., zxz ∈ L ⇐⇒ zyz ∈ L. Considering a particular z, we have zx ≈ zy, and, so, [zx]=[zy] ∈ Q. Next, by varying z, we find that [z]Rx =[z]Ry for all z, which means Rx = Ry.

4 2.8 Definition The above considerations suggest that, in general, the alphabet of an automaton should be regarded as a monoid. Let X be a monoid. An X-automaton is a pair (Q, F ) where Q is a set and F : X → [Q → Q] is a monoid homomorphism. Thus, Σ-automata are nothing but Σ∗-automata with Σ∗ regarded as a monoid. We also call an X-automaton an abstract automaton of type X. Machines for X-automata can be defined similar to the above. If F is mono, i.e., Fx = Fy =⇒ x = y then the image of X under F is a submonoid of [Q → Q]. In other words, X is isomorphic to a transformation monoid over Q. By extension, the automaton (Q, F ) is itself called a transformation monoid. We also call it a fully abstract automaton. Our notion of automata allows us to make a smooth transition between syntax and semantics (or, as will be seen later, between intensional and extensional semantics of programs). Given a concrete automaton, (Q, F )Σ, we can make it abstract by using the monoid X = Σ∗/≡ where ≡ is the kernel of the homomorphism F : Σ∗ → [Q → Q]. The factor F ′ : X → [Q → Q] gives a fully abstract automaton (Q, F ′)X . Conversely, given an abstract automaton (Q, F )X , we can generate a concrete automaton by taking some set of generators S ⊆ M to be Σ.

2.9 Examples (i) Let EL = {up, dn} be an alphabet. Consider the automaton El 3 with states Q = {0, 1, 2} and the interpretation 0 1 2 0 1 2 Fup = Fdn = 1 2 − ! − 0 1 ! El 3 can be thought of as the behavior of primitive elevators running between three floors. up (dn) is interpreted as instruction to move up (down) to the next floor. The up instruction at the top floor and the dn instruction at the bottom floor are undefined. The automaton is monogenic with any state as q0. A particular elevator is then an El 3-machine. (ii) A more friendly elevator has the alphabet FEL = {up.clink, up.beep, dn.clink, dn.beep} where clink and beep are understood as the sounds emitted by the elevator in response to an instruc- tion. The friendly version of the above elevator FEl 3 has the same state set but the following interpretation: 0 1 2 0 1 2 Fup.clink = Fup.beep = 1 2 − ! − − 2 !

0 1 2 0 1 2 Fdn.clink = Fdn.beep = − 0 1 ! 0 − − ! Note that up.clink is a single symbol of the alphabet. We discuss below how to decompose such symbols into their input and output parts. (iii) A counter object has the alphabet CNT = {val.i : i ∈N}∪{inc}. The automaton Counter has states Q = N and interpretation: val.i : i 7→ i inc : i 7→ (i + 1) for all i ∈N

It is monogenic with q0 = 0. (iv) A “stepper” object that successively steps through a sequence of numbers has the alphabet STEP = {next.i : i ∈ N }. A stepper Pos that steps through the positive has states Q = N and interpretation next.i : (i − 1) 7→ i for all i ≥ 0.

5 3 The structure of automata

3.10 The first question to be asked is what are the for automata? This is to be considered carefully. If one naively interprets automata as “structures”, one would be led to morphisms that preserve such structure. On the other hand, automata are not structures; they are cocrete implementations. The state sets of automata are part of theri internal structure. Morphisms can only preserve externally visible structure. Suppose a “function” from automata of type Σ to automata of type Γ maps an automaton (Q, F )Σ to an automaton (Q′, F ′)Γ. It is essentially constructing a new automaton (Q′, F ′)Γ from an original automaton (Q, F )Σ. The following considerations arise: (i) The function must construct the states of the new automaton from the states of the original automaton. Thus, we need a partial function φ : Q → Q′. (ii) It should interpret each instruction of the new automaton by issuing instructions to the original automaton. This leads to a function f : Γ → Σ∗. Note that this function runs backwards. (iii) The behavior of the original automaton under these instructions must be compatible with what is expected for the new automaton, i.e., the following diagram must commute:

Ff(b) Q - Q

φ φ ? ′ ? Fb Q′ - Q′

(iv) Finally, since this must hold for every instruction b of the new automaton, we require that, for every state in Q′, there corresponds some state in Q. In other words, φ must be surjective These considerations lead to the following definition.

3.11 definition An implementation of automata is a pair φf : (Q, F )Σ → (Q′, F ′)Γ where f : Γ → Σ∗ is a function and φ : Q → Q′ is a surjective partial function such that the above diagram commutes. We say that (Q, F )Σ implements (Q′, F ′)Γ via φf . Notice that an implementation uniquely determines the behavior of the new automaton F ′. To ′ ′ −1 ′ ′ ′ determine q Fb, find a q ∈ φ (q ) which is nonempty by definition. Then, q Fb = qFf(b).

3.12 Examples (i) Consider function f : EL → FEL given by

up.clink up.beep dn.clink dn.beet f = up ǫ dn ǫ !

f An implementation φ : El 3 → FEl 3 of friendly elevators in terms of ordinary elevators is given by the mapping φ(q)= q. (ii) There is an implementation of two-storey elevators in terms of three-storey elevators: φf : El 3 → El2 : f is idEL and φ is the partial assignment 0 7→ 0, 1 7→ 1. Clearly, there can be no implementation of El 3 using El 2. f (iii) An implementation of the stepper Pos in terms of Counter is φ where φ is idN and f(next.i)= (val.(i − 1)) · inc.

6 3.13 Next, we must add outputs to automata themselves. The traditional mechanism for this is in terms of Mealy machines. These machines have additional components for an output alphabet Θ and an output function G : Q × Σ → Θ. While this is is essentially what we need, the explicit framework turns out to be a little cumbersome. For instance, to define a Counter, we need val and inc in the input alphabets and N in the output alphabet. But, the machine should not produce an integer in response to inc. To circumvent such issues, we define the following variant of Mealy machines.

3.14 Definition A modular automaton is an automaton (Q, F )Σ equipped with two “projec- tion” functions πi : Σ → Σi and πo : Σ → Σo to appropriate alphabets Σi and Σo, such that • for any q ∈ Q, if qFa and qFb are defined πi(a)= πi(b) then a = b. ′ An object is a modular automaton such that, for all q ∈ Q and a ∈ Σi, there exists a ∈ Σ such ′ that πi(a )= a and qFa′ is defined. For example, for counters, we can define Σi = {val, inc} and Σ0 = N + {∗}. Counter is then a modular automaton. In fact, it is an object because every state has transitions for val and inc operations. An implementation of modular automata is a triple φ(f, g) : (Q, F )Σ → (Q′, F ′)Γ where f : Γi → Σi∗ and g : Σ → Γ are functions

3.15 Examples (i) An implementation of Counter in terms of storage cells is as follows: φf : Cell → Counter where φ is idN and val.i inc f = get.i (get.i) · (put.(i + 1)) !

7