Definition 3.3 A function between biorders f : D â E is said to be monotone stable if it ..... f(y): suppose (c, v) â f(x) = f(x ⪠y) and so by Lemma 5.2, (c, v) â f(y) or.
Available online at www.sciencedirect.com
Electronic Notes in Theoretical Computer Science 319 (2015) 271–287 www.elsevier.com/locate/entcs
Sequential Algorithms for Unbounded Nondeterminism J. Laird1 Department of Computer Science, University of Bath, UK
Abstract We give extensional and intensional characterizations of higher-order functional programs with unbounded nondeterminism: as stable and monotone functions between the biorders of states of ordered concrete data structures, and as sequential algorithms (states of an exponential ocds) which compute them. Our fundamental result establishes that these representations are equivalent, by showing how to construct a unique sequential algorithm which computes a given stable and monotone function. We illustrate by defining a denotational semantics for a functional language with countable nondeterminism (“fair PCF”), with an interpretation of fixpoints which allows this to be proved to be computationally adequate. We observe that our model contains functions which cannot be computed in fair PCF, by identifying a further property of the definable elements, and so show that it is not fully abstract. Keywords: Sequential Algorithms, Nondeterminism, Fairness, Biorders
1
Introduction
This paper develops a model of higher-order computation with unbounded nondeterminism. In this setting we may write programs which will always return a value but may take an unbounded number of steps to do so, corresponding to the notion of fairness [6]. A major challenge for capturing such programs is that they do not correspond to continuous functions, in general. In domain theory, this may be resolved by weakening the continuity properties required (e.g. to ω1 -continuity [1]), although this admits many undefinable functions and leaves fewer principles with which to reason about program behaviour. A more intensional representation of programs (for example, as strategies in a games model) offers the possibility of studying unbounded nondeterminism in computation more directly, although traditional representations of strategies as collections of finite sequences of moves are insufficient to capture the distinction between infinite interactions and finite, but unbounded ones [7]. 1
Research supported by UK EPSRC grant EP/K037633/1
http://dx.doi.org/10.1016/j.entcs.2015.12.017 1571-0661/© 2015 The Author. Published by Elsevier B.V. This is an open access article under the CC BY-NC-ND license (http://creativecommons.org/licenses/by-nc-nd/4.0/).
272
J. Laird / Electronic Notes in Theoretical Computer Science 319 (2015) 271–287
We take an approach which relates extensional and intensional representations of programs with unbounded nondeterminism: our main result is an equivalence between stable and monotone functions and sequential algorithms on ordered concrete data structures. We show that these equivalent representations may be used to interpret a simple functional programming language with unbounded nondeterminism (fair PCF). We show that this model contains elements which are not definable as terms, leading to a failure of full abstraction and suggesting how it could be further refined. 1.1
Related Work
Our model is based on an intensional description of stable and monotone functions on biorders generated from ordered concrete data structures. Biorders, which combine some intensional information, in the form of the stable order, with the extensional (Scott) order, were introduced by Berry [2]. In previous work, the author has shown that stable and continuous functions on biorders with a (extensionally) greatest element are (Milner-Vuillemin) sequential, and used them to construct models of sequential languages such as the lazy λ-calculus [12], as well as λ-calculi with nondeterminism [11]. However, although these models technically carry information about program behaviour, they do not do so in a transparent way. Concrete data structures were introduced by Kahn and Plotkin [9], as part of a definition of sequentiality for higher-order functionals, but the more intensional notion of sequential algorithm (a state of a “function-space” CDS) introduced by Berry and Curien [3] offers an appealing model of computation in its own right. On the one hand, concrete data structures correspond to a positional form of games, and sequential algorithms to positional strategies (see e.g. [8]). On the other, sequential algorithms may be related to purely extensional models: in the deterministic case, Cartwright, Curien and Felleisen [4] have established that they compute, and are equivalent to “observably sequential” functions; the author has given a more abstract representation of the latter as bistable functions on bistable biorders [12,10]. To interpret sequential, but nondeterministic programs (corresponding to stable and monotone functions on Berry-style biorders, which are sequential, but not strongly sequential ) as sequential algorithms, we abandon the consistency condition on states (that any cell may be filled with at most one value). However, this also requires an ordering on cells and values (corresponding to game positions), to reflect the fact that (for example) any program which may diverge in response to a given argument may still diverge in response to an argument with a wider range of behaviours. This notion of an ordered concrete data structure was introduced in [13] in which stable and continuous functions were shown to correspond to finite-branching sequential algorithms. Interestingly, the stable order on non-deterministic sequential algorithms had been described by Roscoe [15] on processes in CSP — the “strong order” — in work approximately contemporaneous with the discoveries of bidomains and of sequential algorithms. Here, we extend the correspondence between stable functions and sequential algorithms to unbounded nondeterminism. This requires a new notion of ordinal-indexed interaction, to distinguish computations which are
J. Laird / Electronic Notes in Theoretical Computer Science 319 (2015) 271–287
?−→n n ∈ N
(λx.M ) N −→M [N/x]
If0 0−→λx.λy.x
273
YM −→M (YM )
If0 suc(n)−→λxλy.(y n)
M −→M M N −→M N
M −→M suc M −→suc M
M −→M If0 M −→If0 M
Table 1 Operational Semantics for Fair PCF
infinite from those which are finite but unbounded.
2
PCF With Unbounded Nondeterminism
In order to illustrate the interaction between higher-order functions, recursion and unbounded nondeterminism, we introduce a programming language which combines them — an extension of PCF with natural number choice — for which we will give a denotational semantics. In other words, we add to the simply-typed λ-calculus over the single ground type nat, the following constants 2 : •
Numerals 0 : nat and suc : nat → nat.
•
Conditional If0 : nat → nat → (nat → nat) → nat. 3
•
Fixpoints: Y : (T → T ) → T for each type T .
•
Error and Choice: : nat and ? : nat.
2.1
Operational Semantics
The (small-step) reduction relation for closed terms is defined in Table 1. We study a must-testing semantics (unbounded choice is definable from bounded choice up to may-testing equivalence: a sequential algorithms semantics for the latter was presented in [13]). Define ⇓ (must-convergence) to be the least predicate on programs (closed terms of type nat) such that for any program M , if M ⇓ for all programs M such that M −→ M then M ⇓. (So, in particular, every numeral is must-convergent, and if M1 −→ M2 −→ . . . −→ Mn −→ . . . then none of the Mi are must-convergent.) Thus we may define must-approximation () and mustequivalence () as the least precongruence and congruence on terms (respectively) such that if M N or M N then M ⇓ implies N ⇓. Clearly, is a partial order on the -equivalence classes of closed terms at each type. 2 Our denotational semantics can also interpret the catch operator of observably sequential PCF, but we omit this since our focus is not on full abstraction. 3 If its first argument evaluates to suc(n), this passes n to its third argument, so we may define pred : nat → nat = λx.((If0 x) 0) λy.y.
274
J. Laird / Electronic Notes in Theoretical Computer Science 319 (2015) 271–287
2.2
Examples
Evidently, we may express bounded choice (up to must-equivalence) using countable choice — e.g. defining M or N = If0 ? then M else λx.N . But attempting to define countable choice using bounded choice — e.g. as (Yλf.λx.(x or (f (suc x)))) 0 — will fail (evaluation may always take the right hand branch and so diverge). To express (for example) the fair merge of infinite streams of objects of type T , the latter may be represented as objects of type nat → T (i.e. head(M ) = M 0, tail(M ) = λx.M (suc x) and M :: N = ((If0 y) M ) N . The fair merge function, merge : (nat → T ) → (nat → T ) → nat → T returns any interleaving of its arguments which includes all entries from both lists by alternately taking a nonempty initial segment of unbounded length from each stream. (Yλf λxλuλv.If0 x then((f suc(?) v u)) else λy.head(u) :: ((f y tail(u) v))) ? Conversely, we may express countable choice in terms of fair merge — e.g. by returning the position of the first zero in a fair merge of the stream of 1s with the stream of 0s: (Yλf.λx.λy.If0 head(x) then y else (f tail(x) (suc(y)))) (merge (λx.0 )(λx.1)) 0 For each i ∈ N, define ?i : nat by ?0 =? and ?i+1 = suc?i . Then (we claim: proof via the denotational semantics is straightforward) ?i ?i+1 for each i ∈ N, and is a -least upper bound for the chain ?i | i ∈ N. This may be used to show that many first-order functions definable in fair PCF are not -continuous. For example, λx.If0 x then 0 else Ω : nat → nat: If0 ?i then 0 else Ω diverges for all i, but If0 then 0 else Ω converges. Note that this example also shows that application is not -continuous with respect to functions as well as arguments — i.e. λf.(f ) is a -least upper bound for the chain of terms Mi = λf.(f ?i ) | i ∈ ω but Mi λx.If0 x then 0 else Ω diverges for all i, whereas (λf.f ) λx.If0 x then 0 else Ω converges. This creates difficulties for defining well-behaved least fixed points for functions, which we will resolve semantically by working with the stable order, for which application is continuous with respect to functions (although not arguments).
3
Complete Biorders
We generalize the notion of biorder [2,5] to infinite meets (corresponding to infinite branching under a must-testing interpretation). Definition 3.1 A complete biorder is a complete (meet) lattice (|D|, ) with a second partial order ≤s on |D| such that: •
If x ≤s y then x y.
•
⊥ ≤s x for all x ∈ D (where ⊥ =
•
For any X, Y ⊆ D, if X ≤s Y (meaning: ∀x ∈ X∃y ∈ Y.x ≤s y ∧ ∀y ∈ Y.∃x ∈ X.x ≤s y) then X ≤s Y ).
D).
J. Laird / Electronic Notes in Theoretical Computer Science 319 (2015) 271–287
275
We write ↑ X if X ⊆ |D| is bounded above in the stable order. Lemma 3.2 If ↑ X then X is the greatest lower bound for X in the stable order. Proof. Supposing x ≤s y for all x ∈ X: • For any x ∈ X, X ≤s {x, y} and so X ≤s x = {x, y}. • If z ≤s x for all x ∈ X, then {z} ≤s X and so {z} ≤s X.
2
Definition 3.3 A function between biorders f : D → E is said to be monotone stable if it preserves both orders, and is conditionally multiplicative — i.e. if ↑ X, then f ( X) = f (X). Let CB be the category in which objects are complete biorders and morphisms from D to E are monotone stable functions from D to E. Proposition 3.4 CB is Cartesian closed. Proof. Products are defined pointwise. The internal hom [D, E] is the lattice of monotone stable functions (i.e. ( F )(x) = f ∈F f (x)), with the stable ordering defined: f ≤s g if for all x, y ∈ D, x ≤s y =⇒ f (x) ≤s g(y) and g(y) = f (y) g(x).
4
2
Ordered Concrete Data Structures
A concrete data structure [9,3] consists of sets of cells, values and events (which are pairs of cells and values), and an enabling relation between events and cells. The idea is that each step of a sequential computation is represented as an event (the filling of a cell with a value), which may be dependent on some combination of previous events having occurred (as specified by the enabling relation). This may be considered as a two-player game between the environment, which may propose an enabled cell to be filled, and the program, which can then fill it with a value. Deterministic programs correspond to deterministic strategies for this game which specify a unique value for filling enabled cells. They are represented as states: sets of events which satisfy two conditions: consistency — every cell must be filled with a unique value — and safety — for every filled cell there is a finite chain of enablings of filled cells within the state, back to an “initial cell” which does not depend on any prior events. In order to model unbounded nondeterministic computation with must-testing we adapt this setting in the following ways: •
Removing the consistency condition on states, so that a cell may be filled with multiple different values.
•
Placing an ordering on cells and values, (and thus events) and requiring states to be upwards closed under this ordering. This reflects the fact that (for example) the response of a function to an argument which is a nondeterministic choice of x and y must include all of its responses to both x and y.
276
J. Laird / Electronic Notes in Theoretical Computer Science 319 (2015) 271–287
•
Including a distinct element ⊥ — representing divergence — with which any cell may be filled (cf. the representation of divergence in the game semantics of must-testing in [7]).
•
Extending the safety condition to allow infinite chains of enabling events. (This captures the distinction between infinite interaction, and that which is finite but unbounded.)
Definition 4.1 A (filiform) ordered concrete data structure (ocds) A is a tuple (C(A), V (A), A , E(A)) where C(A), V (A) (the cells and values of A) are partial orders not containing the distinguished element ⊥, E(A) ⊆ C(A) × V (A) is a set of events and A ⊆ (E(A) ∪ {∗}) × C(A) is a relation (enabling) such that (c, v) c implies c < c . We write E(A)⊥ for the partial order E(A) ∪ (C(A) × {⊥}), with (c, ⊥) ≤ (c , v) if c ≤ c . A simple example of an ocds, which we shall use to interpret the type of natural numbers, is N = ({c}, N, {(∗, c)}, {c} × N), which has a single initial cell which may be filled with any natural number value. Definition 4.2 A proof of an event e is an ordinal sequence of events (cα , aα ) | α ≤ κ such that eκ ≤ e and for α ≤ κ: •
If α = 0 then ∗ cα (c0 is initial),
•
If α = β + 1 then eβ cα , If α = β