First-Order and Temporal Logics for Nested Words
Rajeev Alur Marcelo Arenas Pablo Barcel´o University of Pennsylvania PUC Chile Universidad de Chile
KoushaEtessami NeilImmerman LeonidLibkin University of Edinburgh University of Massachusetts University of Edinburgh
Abstract Pushdown systems (PDSs), Boolean Programs, and Re- cursive State Machines (RSMs), are equivalent abstract Nested words are a structured model of execution paths models of procedural programs, with finite data abstrac- in procedural programs, reflecting their call and return tion but unbounded call stack. Software model checking nesting structure. Finite nested words also capture the technology is by now thoroughly developed for checking structure of parse trees and other tree-structured data, such ω-regular properties of runs for these models, when the as XML. runs are viewed as ordinary words (see [5, 8, 1]). Unfor- We provide new temporal logics for finite and infi- tunately, temporal logic and ω-regular properties over ordi- nite nested words, which are natural extensions of LTL, nary words are inadequate for expressing a variety of prop- and prove that these logics are first-order expressively- erties of program executions that are useful in interproce- complete. One of them is based on adding a ”within” dural program analysis and software verification. These in- modality, evaluating a formula on a subword, to a logic clude Hoare-like pre/post conditions on procedures, stack CaRet previously studied in the context of verifying prop- inspection properties, and other useful program analysis erties of recursive state machines. The other logic is based properties that go well beyond ω-regular (see [2] for some on the notion of a summary path that combines the linear examples). On the other hand, many such program analy- and nesting structures. For that logic, both model-checking sis properties can easily be expressed when runs are viewed and satisfiability are shown to be EXPTIME-complete. as nested words. Runs of Boolean Programs and RSMs can Finally, we prove that first-order logic over nested words naturally be viewed as nested words once we add “summary has the three-variable property, and we present a tempo- edges” between matching calls and returns, and we can thus ral logic for nested words which is complete for the two- hope to extend model checking technology for procedural variable fragment of first-order. programs using richer temporal logics over nested words which remain tractable for analysis. These considerations motivated the definition of Visibly 1 Introduction Pushdown Languages (VPLs) [3] and the call-return tempo- ral logic CaRet [2]. CaRet is a temporal logic over nested words which extends LTL with new temporal operators that An execution of a procedural program can reveal not just allow for navigation through a nested word both via its ordi- a linear sequence of program states encountered during the nary sequential structure, as well as its matching call-return execution, but also the correspondence between each point summary structure. The standard LTL model checking al- during the execution at which a procedure is called and the gorithms for RSMs and PDSs can be extended to allow point when we return from that procedure call. This leads model checking of CaRet, with essentially the same com- naturally to the notion of a finite or infinite nested word ([4, plexity [2]. VPLs [3] are a richer class of languages that 3, 2]). A nested word is simply a finite or ω-word supplied capture MSO-definable properties of nested words. Re- with an additional binary matching relation which relates cently, results about VPLs have been recast in light of corresponding call and return points (and of course satisfies nested words, and in particular in terms of Nested Word Au- “well-bracketing” properties). Finite nested words offer an tomata [4] which offer a machine acceptor for (ω-)regular alternative way to view any data which has both a sequential nested words, with all the expected closure properties. string structure as well as a tree-like hierarchical structure. Examples of such data are XML documentsand parse trees. Over ordinary words, LTL has long been considered the
1 temporal logic of choice for program verification, not only a tableaux construction which translates an NWTL formula because its temporal operators offer the right abstraction into a Nested Word Automaton, enabling the standard au- for reasoning about events over time, but because it pro- tomata theoretic approach to model checking of Boolean vides a good balance between expressiveness (first-order Programs and RSMs with complexity that is polynomial in complete), conciseness (can be exponentially more suc- the size the model and EXPTIME in the size of the formula. cinct compared to automata), and the complexity of model- We then explore some alternative temporal logics, which checking (linear time in the size of the finite transition sys- extend variants of CaRet with variants of unary “Within” tem, and PSPACE in the size of the temporal formula). operators proposed in [2], and we show that these exten- This raises the question: What is the right temporal logic sions are also FO-complete. However, we observe that the for nested words? model checking and satisfiability problems for these logics The question obviously need not have a unique answer, are 2EXPTIME-complete. These logics are – provably – particularly since nested words can arise in various appli- more concise than NWTL, but we pay for conciseness with cation domains: for example, program verification, as we added complexity. already discussed, or navigation and querying XML doc- It follows from our proof of FO-completenessfor NWTL uments under “sequential” representation (see, e.g., [27]). that over nested words, every first-order formula with one However, it is reasonable to hope that any good temporal free variable can be expressed using only 3 variables. More logic for nested words should possess the same basic quali- generally, we show, using EF games, that 3 variables suffice ties that make LTL a good logic for ordinarywords, namely: for expressing any first order formula with two or fewer free (1) first-order expressive completeness: LTL has the same variables, similarly to the case of words [13] or finite trees expressive power as first-order logic over words, and we [19]. Finally, we show that a natural unary temporal logic would want the same over nested words; (2) reasonable over nested words is expressively complete for first-order complexity for model checking and satisfiability; and (3) logic with 2 variables, echoing a similar result known for nice closure properties: LTL is closed under boolean com- unary temporal logic over ordinary words [9]. binations including negation without any blow-up, and we Related Work. VPLs and nested words were introduced would want the same for a logic over nested words. Finally in [3, 4]. The logic CaRet was defined in [2] with the goal (and perhaps least easy to quantify), we want (4) natural of expressing and checking some natural non-regular pro- temporal operators with simple and intuitive semantics. gram specifications. The theory of VPLs and CaRet has Unfortunately, the logic CaRet appears to be deficient been recast in light of nested words in [4]. Other aspects of with respect to some of these criteria: although it is easily nested words (automata characterizations, games, model- first-order expressible, proving incompleteness – a widely checking) were further studied in [1, 4, 2, 16]. It was also believed conjecture – appears to be quite difficult. Also, observed that nested words are closely related to a sequen- some temporal operators in CaRet (such as the past-time tial, or “event-based” API for XML known as SAX [24] (as call modalities), motivated by program analysis, may not be opposed to a tree-based DOM API [7]). SAX representation viewed as particularly natural in other applications. There is very important in streaming applications, and questions is much related work in the XML community on logics for related to recognizing classes of nested words by the usual trees (see, e.g., surveys [14, 15, 28]), but they tend to have word automata have been addressed in [27, 6]. different kinds of deficiency for our purposes: they concen- While finite nested words can indeed be seen as XML trate on the hierarchical structure of the data and largely documents under the SAX representation, and while much ignore its linear structure; also, they are designed for finite effort has been spent over the past decade on languages trees. for tree-structured data (see, e.g. [14, 15, 28] for surveys), We introduce and study new temporal logics over nested adapting the logics developed for tree-structured data is not words. The main logic we consider, Nested Word Tem- as straightforward as it might seem, even though from the poral Logic (NWTL) extends LTL with both a future and complexity point of view, translations between the DOM past variant of the standard Until operator, which is inter- and the SAX representations are easy [26]. The main prob- preted over summary paths rather than the ordinary linear lem is that most such logics rely on the tree-based repre- sequence of positions. A summary path is the unique short- sentation and ignore the linear structure, making the nat- est directed path one can take between a position in a run ural navigation through nested words rather unnatural un- and some future position, if one is allowed to use both der the tree representation. Translations between DOM and successor edges and matching call-return summary edges. SAX are easy for first-order properties, but verifying nav- We show that NWTL possesses all the desirable proper- igational properties expressed in first-order is necessarily ties we want from a temporal logic on nested words. In non-elementary even for words if one wants to keep the particular, it is both first-order expressively complete and data complexity linear [10]. On the other hand, logics for has good model checking complexity. Indeed we provide XML tend to have good model-checking properties (at least
2 in the finite case), typically matching the complexity of word) induced by elements ℓ such that i ≤ ℓ ≤ j. If j 1 iff ϕ is true in position i − 1, ∗ a1 ...an ∈ Σ , and (µ, call, ret) is a matching on [1,n]. and ⊖µϕ is true in position j if j is a return position with A nested ω-word is a tuple w¯ = (w, µ, call, ret), where matching call i and ϕ is true at i. ω w = a1 . . . ∈ Σ , and (µ, call, ret) is a matching on N. The until/since operators depend on what a path is. In We say that a position i in a nested word w¯ is a call po- general, there are various notions of paths through a nested sition if call(i) holds; a return position if ret(i) holds; word. We shall consider until/since operators for paths that and an internal position if it is neither a call nor a return. are unambiguous: that is, for every pair of positions i and j If µ(i, j) holds, we say that i is the matching call of j, with i < j, there could be at most one path between them. and j is the matching return of i, and write c(j) = i and Then, with respect to any such given notion of a path, we r(i)= j. Calls without matching returns are pending calls, have the until and since operators with the usual semantics: and returns without matching calls are pending returns. A nested word is said to be well-matched if no calls or returns • (¯w,i) |= ϕUψ iff there is a position j ≥ i and a path are pending. Note that for well-matched nested words, the i = i0 < i1 <... 3 the current action i is executed, and its corresponding re- For example, in the figure below, h2, 4, 5i is a call path, turn. Formally, C(i) is the greatest matched call position h3, 4, 6, 7, 8, 10i is both an abstract and a summary path; ji whose matching call is before i. abstract path from 3 to 9). Definition 3.1 (Linear, call and abstract paths) Given positions i < j, a sequence i = i0 • a linear path if ip+1 = ip +1 for all p < k; 1 2 3 4 5 6 7 8 9 10 11 • a call path if ip = C(ip+1) for all p < k; • an abstract path if 4 Expressive Completeness r(ip) if ip is a matched call ip+1 = In this section we study logics that are expressively com- otherwise. (ip +1 plete for FO, i.e. temporal logics that have exactly the same power as FO formulas in one free variable over finite and We shall denote until/since operators corresponding to infinite nested words. In other words, for every formula ϕ U S Uc Sc these paths by / for linear paths, / for call paths, of an expressively complete temporal logic there is an FO and Ua Sa for abstract paths. 1 / formula ϕ′(x) such that (¯w,i) |= ϕ iff w¯ |= ϕ′(i) for every Our logics will have some of the next/previous and un- nested word w¯ and position i in it, and conversely, for every ′ til/since operators. Some examples are: FO formula ψ(x) there is a temporal formula ψ such that w¯ |= ψ(i) iff (¯w,i) |= ψ′. • When we restrict ourselves to the purely linear frag- Our starting point is a logic NWTL (nested-word tempo- ment, our operators are and ⊖, and U and S, i.e. ral logic) based on summary paths introduced in the previ- precisely LTL (with past operators). ous section. We show that this logic is expressively com- • The logic CaRet [2] has the following operators: the plete (and of course remains expressively complete under next operators and µ; the linear and abstract untils the addition of operators present in logics inspired by veri- (i.e., U and Ua), the call since (i.e., Sc) and a previous fication of properties of execution paths in programs). This operator ⊖c that will be defined in Section 4.2. latter remark will be of importance later, when we study the complexity of model checking. Another notion of a path combines both the linear and We then look at logics close to those in verification lit- the nesting structure. It is the shortest path between two erature, i.e. with operators such as call and abstract until positions i and j. Unlike an abstract path, it decides when and since, and ask what needs to be added to them to get to skip a call based on position j. Basically, a summary path expressive completeness. We confirm a conjecture of [2] from i to j moves along successor edges until it finds a call that a within operator is what’s needed: such an operator position k. If k has a matching return ℓ such that j appears evaluates a formula on a nested subword. after ℓ, then the summary path skips the entire call from k to ℓ and continues from ℓ; otherwise the path continues as a 4.1 Expressive completeness and NWTL successor path. Note that every abstract path is a summary path, but there are summarypaths that are not abstract paths. The logic NWTL (nested words temporal logic) has next Definition 3.2 A summary path between i < j in a nested and previous operators, as well as until and since with re- word w¯ is a sequence i = i0 4 Proof sketch. Translation of NWTL into FO is quite property says that FO sentences over the usual, unnested, straightforward, but we show how to do it carefully, to get words can be evaluated without using the previous ⊖ and the 3-variable property. For the converse, we define yet an- since S operators. Let NWTLfuture be the fragment of σ other notion of path, called a strict summary path, that is NWTL that does not use S and the operators ⊖ and ⊖µ. different from summary paths in two ways. First, if it skips a call, it jumps from i not to r(i) but r(i)+1. Second, if it Proposition 4.4 There are FO sentences over nested words reaches a matched return position, it stops. We then look at that cannot be expressed in NWTLfuture. the logic NWTLs in which the semantics of until and since is modified so that they refer to strict summary paths. We then show that NWTLs ⊆ NWTL and FO ⊆ NWTLs. Proof sketch. Let w¯1 and w¯2 be two well-matched nested words, of length n1 and n2 respectively. We first show The former is by a direct translation. The proof of future FO ⊆ NWTLs is in two parts. First we deal with the fi- that, for every NWTL formula, there is an integer k nite case. We look at the standard translation from nested such that w¯1[i1,n1] ≡k w¯2[i2,n2] implies (¯w1,i1) |= ϕ iff words into binary trees. If a matched call position i is trans- (¯w2,i2) |= ϕ. Here ≡k means that Player II has a win in lated into a node s of a tree, then the first position inside the k-round Ehrenfeucht-Fra¨ıss´egame. This follows from expressive-completeness and properties of future formulas. the call is translated into the right successor of s, and the future linear successor of r(i) is translated into the left successor Using this, we show that there is no NWTL formula ⊖ of s. If i is an internal position, or an unmatched call or equivalent to µ⊤ ∧ µ a (checking whether the first return position, its linear successor is translated into the left position is a call, and the position preceding its matching 2 successor of s. With this translation, strict summary paths return is labeled a). become paths in a tree. Note also that adding all other until/since pairs to NWTL We next use until/since-based logics for trees from [18, does not change its expressiveness. That is, if we let + U S Uc Sc Ua Sa 25]. By a slight adaptation of techniques from these papers NWTL be NWTL + { , , , , , }, then: (in particular using the separation property from [18]), we Corollary 4.5 NWTL+ = FO. prove expressive completeness of a translation of NWTLs into a tree logic, and then derive expressive completeness of s Later, when we deal with model-checking, we shall NWTL for finite nested words. prove upper bound results for NWTL+ that, while expres- In the infinite case, we combine the finite case and the sively complete for FO, allows more operators. separation property of [18] with Kamp’s theorem and the separation property of LTL. Note that a nested ω-word 4.2 The within operator is translated into an infinite tree with exactly one infinite branch. A composition argument that labels positions of that branch with types of subtrees reduces each FO formula We now go back to the three until/since operators origi- to an LTL formula over that branch in which propositions nally proposed for temporal logics on nested words, based are types of subtrees, expressible in NWTLs by the proof on the the linear, call, and abstract paths. In other words, µ in the finite case. Using the separation properties, we then our basic logic, denoted by LTL , is show how to translate such a description into NWTLs. 2 ′ ′ Recall that FOk stands for a fragment of FO that consists ϕ, ϕ := ⊤ | a | call | ret | ¬ϕ | ϕ ∨ ϕ | ⊖ ⊖ of formulas which use at most k variables in total. First, ϕ | µϕ | ϕ | µϕ | U ′ S ′ Uc ′ Sc ′ Ua ′ Sa ′ from our translation from NWTL to FO we get: ϕ ϕ | ϕ ϕ | ϕ ϕ | ϕ ϕ | ϕ ϕ | ϕ ϕ Corollary 4.2 Over nested words, every FO formula with We now extend this logic with the following within op- at most one free variable is equivalent to an FO3 formula. erator proposed in [2]. If ϕ is a formula, then Wϕ is a for- mula, and (¯w,i) |= Wϕ iff i is a call, and (¯w[i, j],i) |= ϕ, Furthermore, for FO sentences, we can eliminate the where j = r(i) if i is a matched call and j = |w¯| if i is since operator. an unmatched call. In other words, Wϕ evaluates ϕ on a subword restricted to a single procedure. We denote such Corollary 4.3 For every FO sentence Φ over finite or in- µ finite nested words, there is a formula ϕ of NWTL that an extended logic by LTL + W. does not use the since operator Sσ such that iff w¯ |= Φ µ (¯w, 1) |= ϕ. Theorem 4.6 LTL +W = FO over both finite and infinite nested words. The previous operators ⊖ and ⊖µ, however, are needed even for FO sentences over nested words. This situation The inclusion of LTLµ + W into FO is routine. The con- is quite different thus from LTL, for which the separation verse is done by encoding NWTL into LTLµ + W. 5 CaRet and other within operators The logic CaRet, as states Q, a set of initial states Q0 ⊆ Q, a set of (linear) ac- defined in [2], did not have all the operators of LTLµ. In cepting states F ⊆ Q, a set of pending call accepting states fact it did not have the previous operators ⊖ and ⊖µ, and Fc ⊆ Q, a call-transition relation δc ⊆ Q × Σ × Q × Q, it only had linear and abstract until operators, and the call an internal-transition relation δi ⊆ Q × Σ × Q, a since operator. That is, CaRet was defined as return-transition relation δr ⊆ Q × Q × Σ × Q, and a pending-return-transition relation δpr ⊆ Q × Σ × Q. The ′ ′ ϕ, ϕ := ⊤ | a | call | ret | ¬ϕ | ϕ ∨ ϕ | automaton A starts in the initial state and reads the nested ϕ | µϕ | ⊖cϕ | word from left to right. The state is propagated along U ′ Ua ′ Sc ′ ϕ ϕ | ϕ ϕ | ϕ ϕ , the linear edges as in case of a standard word automaton. However, at a call, the nested word automaton propagates and we assume that a ranges over Σ ∪{pret}, where pret states along the linear edges and also along the nesting edge is true in pendingreturns(which is not definable with the re- (if there is no matching return, then the latter is required maining operators). Here ⊖ is the previous operator cor- c to be in F for acceptance). At a matched return, the new responding to call paths. Formally, (¯w,i) |= ⊖ ϕ if C(i) is c c state is determined based on the states propagated along the defined and (¯w, C(i)) |= ϕ. linear as well as the nesting incoming edges. A natural question is whether there is an expressively- Formally, a run r of the automaton A over a nested complete extension of this logic. It turns out that two within word w¯ = (a a . . . , µ, call, ret) is a sequence q , q ,... operators based on C and R (the innermost call and its re- 1 2 0 1 of states along the linear edges, and a sequence q′, for turn) functions provide such an extension. We define two i every call position i, of states along nesting edges, such new formulas Cϕ and Rϕ with the semantics as follows: that q0 ∈ Q0 and for each position i, if i is a call then ′ • (¯w,i) |= Cϕ iff w¯[j, i] |= ϕ, where j = C(i) if C(i) is (qi−1,ai, qi, qi) ∈ δc; if i is an internal, then (qi−1,ai, qi) ∈ ′ defined, and j =1 otherwise. δi; if i is a return such that µ(j, i), then (qi−1, qj ,ai, qi) ∈ δ ; and if i is an unmatched return then (q ,a , q ) ∈ δ . • (¯w,i) |= Rϕ if w¯[i, j] |= ϕ, where j = R(i) if R(i) r i−1 i i pr The run r is accepting if (1) for all pending calls i, q′ ∈ F , is defined, and j = |w¯| (if w¯ is finite) or ∞ (if w¯ is i c and (2) the final state for finite word of length , infinite) otherwise. qℓ ∈ F ℓ and for infinitely many positions i, qi ∈ F , for nested ω- Theorem 4.7 CaRet + {C, R} = FO over both finite and words. The automaton A accepts the nested word w¯ if it infinite nested words. has an accepting run over w¯. Nested word automata have same expressiveness as the The proof of this result is somewhat involved, and relies monadic second order logic over nested words, and the lan- on different techniques. The operators used in CaRet do not guage emptiness can be checked in polynomial-time [4]. correspond naturally to tree translations of nested words, and the lack of all until/since pairs makes a translation from 5.2 Tableau construction NWTL hard. We thus use a composition argument directly on nested words. We now show how to build an NWA accepting the satis- fying models of a formula of NWTL+. This leads to deci- 5 Model-Checking and Satisfiability sion procedures for satisfiability and model checking. Let us first consider special kinds of summary paths: In this section we show that both model-checking and summary-down paths are allowed to use only call edges satisfiability are single-exponential-time for NWTL. In fact (from a call to the first position inside the call), nesting we prove this bound for NWTL+, an FO-complete exten- edges (from a call to its matching return), and internal edges sion of NWTL with all of U, S, Uc, Sc, Ua, Sa. We use (from an internal or return position to a call or internal po- automata-theoretic techniques, by translating formula into sition). Summary-up paths are allowed to use only return equivalent automata on nested words. We then show that edges (from a position preceding a return to the return), a different expressively complete logic based on adding the nesting edges and internal edges. We will use Uσ↓ and within operator to CaRet requires doubly-exponential time Uσ↑ to denote the corresponding until operators. Observe for model-checking, but is exponentially more succinct. that ϕUσ ψ is equivalent to ϕ Uσ↑(ϕ Uσ↓ψ). Given a formula ϕ, we wish to construct a nested word 5.1 Nested word automata automaton Aϕ whose states correspond to sets of subformu- las of ϕ. Intuitively, given a nested word w¯, a run r, which ′ A nondeterministic nested word automaton is a linear sequence q0q1 . . . of states and states qi labeling (NWA) A over an alphabet Σ is a structure nesting edges from call positions, should be such that each (Q,Q0,F,Fc,δc,δi,δr,δpr) consisting of a finite set of state qi is precisely the set of formulas that hold at position 6 ′ cl i+1. The label qi is used to remember abstract-next formu- ψ ∈ (ϕ), ψ ∈ Ψ iff ψ ∈ Φ; (d)foreach µψ ∈ las that hold at position i and the abstract-previous formulas cl(ϕ), µψ 6∈ Φ. that hold at matching return. For clarity of presentation, we The transition relation ensures that the current symbol is focus on formulas with next operators and µ, and until over summary-down paths. It is easy to modify the con- consistent with the atomic propositions in the current state, struction to allow other types of untils and past operators. and next operators requirements are correctly propagated. Given a formula ϕ, the closure of ϕ, denoted by cl(ϕ), An atom Φ belongs to the set Fc iff Φ does not con- is the smallest set that satisfies the following properties: tain any abstract-next formula, and this ensures that, in an cl(ϕ) contains ϕ, call, ret, int, and ret; if either accepting run, at a pending call, no requirements are prop- agated along the nesting edge. For each until-formula ψ in ¬ψ, or ψ or µψ is in cl(ϕ) then ψ ∈ cl(ϕ); if ψ ∨ ψ′ ∈ cl(ϕ), then ψ, ψ′ ∈ cl(ϕ); if ψ Uσ↓ψ′ ∈ cl(ϕ), the closure, let Fψ be the set of atoms that either do not con- ′ Uσ↓ ′ Uσ↓ ′ cl tain ψ or contain the second argument of ψ. Then a nested then ψ, ψ , (ψ ψ ), and µ(ψ ψ ) are in (ϕ); AP and if cl and is not of the form (for any ), word w¯ over the alphabet 2 satisfies ϕ iff there is a run ψ ∈ (ϕ) ψ ¬θ θ cl then ¬ψ ∈ cl(ϕ). It is straightforward to see that the size of r of Aϕ over w¯ such that for each until-formula ψ ∈ (ϕ), cl(ϕ) is only linear in the size of ϕ. Henceforth, we identify for infinitely many positions i, qi ∈ Fψ. Thus, ¬¬ψ with the formula ψ. Theorem 5.1 For a formula of NWTL+, one can effec- cl ϕ An atom of ϕ is a set Φ ⊆ (ϕ) that satisfies: tively construct a nondeterministic Buchi¨ nested word au- O(|ϕ|) • For every ψ ∈ cl(ϕ), ψ ∈ Φ iff ¬ψ 6∈ Φ . tomaton Aϕ of size 2 accepting the models of ϕ. ′ cl ′ • For every formula ψ ∨ ψ ∈ (ϕ), ψ ∨ ψ ∈ Φ iff Since the automaton Aϕ is exponential in the size of ϕ, ′ (ψ ∈ Φ or ψ ∈ Φ). we can check satisfiability of ϕ in exponential-time by test- • For every formula ψ Uσ↓ψ′ ∈ cl(ϕ), ψ Uσ↓ψ′ ∈ Φ ing emptiness of Aϕ. EXPTIME-hardness follows from the iff either ψ′ ∈ Φ or (ψ ∈ Φ and ret 6∈ Φ and either corresponding hardness result for CaRet. σ↓ ′ σ↓ ′ (ψ U ψ ) ∈ Φ or µ(ψ U ψ ) ∈ Φ). Corollary 5.2 The satisfiability problem for NWTL+ is • Φ contains exactly one of the elements in the set EXPTIME-complete. {call, ret, int}. When programs are modeled by nested word automata These clauses capture local consistency requirements. A (or equivalently, pushdown automata, or recursive state Given a formula ϕ, we build a nested word automaton AP machines), and specifications are given by formulas ϕ of Aϕ as follows. The alphabet Σ is 2 , where AP is the set NWTL+, we can use the classical automata-theoretic ap- of atomic propositions. proach: negate the specification, build the NWA A¬ϕ ac- cepting models that violate , take product with the pro- 1. Atoms of ϕ are states of Aϕ; ϕ gram A, and test for emptiness of L(A) ∩ L(A ). Note 2. An atom is an initial state iff ; ¬ϕ Φ ϕ ∈ Φ that the program typically will be given more compactly, 3. For atoms Φ, Ψ and a symbol a ⊆ AP, (Φ, a, Ψ) is an say, as a Boolean program [5], and thus, the NWA A may internal transition of Aϕ iff (a) int ∈ Φ; and (b) for itself be exponential in the size of the input. p ∈ AP, p ∈ a iff p ∈ Φ; and (c) for each ψ ∈ + cl(ϕ), ψ ∈ Ψ iff ψ ∈ Φ; (d) for each µψ ∈ cl(ϕ), Corollary 5.3 Model checking NWTL specifications with µψ 6∈ Φ. respect to Boolean programs is EXPTIME-complete. 4. For atoms Φ, Ψ , Ψ and a symbol a ⊆ AP, l h 5.3 Checking within operator (Φ, a, Ψl, Ψh) is a call transition of Aϕ iff (a) call ∈ Φ; and (b) for p ∈ AP, p ∈ a iff p ∈ Φ; and (c) for We now show that adding within operators makes model- each ψ ∈ cl(ϕ), ψ ∈ Ψl iff ψ ∈ Φ; and (d) for cl checking doubly exponential. Given a formula ϕ of NWTL each µψ ∈ (ϕ), µψ ∈ Ψh iff µψ ∈ Φ. + or NWTL , let pϕ be a special proposition that does not 5. For atoms Φl, Φh, Ψ and a symbol a ⊆ AP, appear in ϕ. Let Wϕ be the language of nested words w¯ (Φl, Φh, a, Ψ) is a return transition of Aϕ iff (a) ret ∈ such that for each position i, (¯w,i) |= p iff (¯w,i) |= Wϕ. AP ϕ Φl; and (b) for p ∈ , p ∈ a iff p ∈ Φl; and (c) for We construct a doubly-exponential automaton B that cap- cl each ψ ∈ (ϕ), ψ ∈ Ψ iff ψ ∈ Φl; and (d) for tures W . First, using the tableau construction for NWTL+, cl ϕ each µψ ∈ (ϕ), µψ ∈ Φh iff ψ ∈ Φl. we construct an exponential-size automaton A that captures 6. For atoms Φ, Ψ and a symbol a ⊆ AP, (Φ, a, Ψ) is a nested words that satisfy ϕ. Intuitively, every time a propo- pending-return transition of Aϕ iff (a) ret ∈ Φ; and sition pϕ is encountered, we want to start a new copy of A, (b) for p ∈ AP, p ∈ a iff p ∈ Φ; and (c) for each and a state of B keeps track of states of multiple copies of 7 A. At a call, B guesses whether the call has a matching re- Remark: checking w¯ |= ϕ for finite nested words For turn or not. In the latter case, we need to maintain pairs of finite nested words, one evaluates the complexity of check- states of A so that the join at return positions can be done ing whether the given word satisfies a formula, in terms correctly. A state of B, then, is eitheraset ofstates of A ora of the length |w¯| of the word and the size of the formula. set of pairs of states of A. We explain the latter case. A pair A straightforward recursion on subformulas shows that for (q, q′) belongsto the state of B, while reading position i ofa NWTL formulas the complexity of this check is O(|w¯|·|ϕ|), nested word w¯, if the subword from i to the first unmatched and for both logics with within operators, CaRet + {C, R} return can take A from q to q′. When reading an internal and LTLµ + W, it is O(|w¯|2 · |ϕ|). symbol a, a summary (q, q′) in current state can be updated to (u, q′), provided A has an internal transition from q to 5.4 On within and succinctness u on a. Let B read a call symbol a. Consider a summary ′ (q, q ) in the current state, and a call-transition (q,a,ql, qh) We saw that adding within operators to NWTL+ in- of A. Then B guesses the return transition (ul, qh,b,u) that creases the complexity of model-checkingby one exponent. will be used by A at the matching return, and sends the In particular, there could be no polynomial-time translation ′ summary (ql,ul) along the call edge and the triple (b,u,q ) from NWTL+ + W to NWTL+. We now prove a stronger along the nesting edge. While processing a return symbol result that gives a space bound as well: while NWTL+ +W b, the current state of B must contain summaries only of has the same power as NWTL+, its formulae can be ex- the form (q, q) where the two states match, and for each ponentially more succinct than formulas NWTL+. That is, ′ + summary (b,u,q ) retrieved from the state along the nesting there is a sequence ϕn, n ∈ N, of NWTL + W formulas ′ edge, the new state contains (u, q ). Finally, B must enforce such that ϕn is of size O(n), and the smallest formula of + Ω(n) that Wϕ holds when pϕ is read. Only a call symbol a can NWTL equivalent to ϕn is of size 2 . For this result, contain pϕ, and when reading such a symbol, B guesses a we require nested ω-words to be over the alphabet 2AP . call transition (q0,a,ql, qh), where q0 is the initial state of + A, and a return transition (ul, qh,b,qf ), where qf is an ac- Theorem 5.6 NWTL +W is exponentially more succinct + cepting state of A, and sends the summary (ql,ul) along the than NWTL . call edge and the symbol b along the nesting edge. + The proof is based upon succinctness results in [9, 22], Lemma 5.4 For every formula ϕ of NWTL , there is a by adapting their examples to nested words. nested word automaton that accepts the language Wϕ and has size doubly-exponential in |ϕ|. 6 Finite-Variable Fragments Consider a formula ϕ of NWTL++W. For every within- ′ subformula Wϕ of ϕ, let ϕ be obtained from ϕ by substi- We have already seen that FO formulas in one free vari- tuting each top-level subformula Wψ in ϕ by the propo- able over nested words can be written using just three dis- sition pψ. Each of these primed formulas is a formula of + tinct variables, as in the case of the usual, unnested, words. NWTL . Then, if we take the product of the nested word For finite nested words this is a consequence of a tree rep- automata accepting Wϕ′ corresponding to all the within- resentation of nested words and the three-variable property subformulas ϕ, together with the nested word automaton for FO over finite trees [19], and for infinite nested words Aϕ′ , the resulting language captures the set of models of this is a consequence Theorem 4.1. ϕ. Intuitively, the automaton for Wϕ′ is ensuring that the In this section we prove two results. First, we give a truth of the proposition pϕ reflects the truth of the subfor- model-theoretic proof that FO formulas with zero, one, or mula Wϕ. If ϕ itself has a within-subformula Wψ, then the two free variables over nested words (finite or infinite) are automaton for treats it as an atomic proposition , and 3 3 ϕ pψ equivalent to FO formulas. Given the FO = FO col- the automaton checking pψ, running in parallel, makes sure lapse, we ask whether there is a temporal logic expressively that the truth of correctly reflects the truth of . 2 pψ Wψ complete for FO , the two-variable fragment. We adapt For the lower bound, the decision problem for LTL techniques from [9] to find a temporal logic that has the games can be reduced to satisfiability problem for formulas 2 same expressiveness as FO over nested words (in a vo- with linear untils and within operators [17], and this shows cabulary that has successor relations corresponding to the that for CaRet extended with the within operator, the satis- “next” temporal operators). fiability problem is 2EXPTIME-hard. We thus obtain: Proposition 5.5 For the logic NWTL+ extended with the 6.1 The three-variable property within operator W the satisfiability problem and the model checking problem with respect to Boolean programs, are We give a model-theoretic, rather than a syntactic, argu- both 2EXPTIME-complete. ment, that uses Ehrenfeucht-Fra¨ıss´egames and shows that 8 over nested words, formulas with at most two free vari- 6.2 The two-variable fragment ables are equivalent to FO3 formulas. Note that for finite nested words, the translation into trees, already used in the In this section, we construct a temporal logic that cap- proof of Theorem 4.1, can be done using at most three vari- tures the two-variable fragment of FO. Note that for fi- ables. This means that the result of [19] establishing the nite unranked trees, a navigational logic capturing FO2 is 3-variable property for finite ordered unranked trees gives known [20, 19]: it corresponds to a fragment of XPath. us the 3-variable property for finite nested words. We prove However, translating the basic predicates over trees into the that FO = FO3 over arbitrary nested words. vocabulary of nested words requires 3 variables, and thus we cannot apply existing results even in the finite case. 2 Theorem 6.1 Over finite or infinite nested words, every FO Since FO over a linear ordering cannot define the suc- cessor relation but temporal logics have next operators, we formula with at most 2 free variables is equivalent to an explicitly introduce successors into the vocabulary of . FO3 formula. FO These successor relations in effect partition the linear edges into three disjoint types; interior edges, call edges, and re- Proof: We look at infinite nested words since the finite turn edges, and the nesting edges (except those from a po- case was settled in [19]. It is more convenient to prove the sition to its linear successor) into two disjoint types; call- result for ordered unranked forests in which every subtree is return summaries, and call-interior-return summaries. finite. We translate a nested ω-word into such a forest is as • Si(i, j) holds iff j = i +1 and either µ(i, j) or i is not follows: when a call i with µ(i, j) is encountered, it defines a call and j is not a return. a subtree with i as its root, and j +1 as the next sibling (note c that this is different from the translation into binary trees we • S (i, j) holds iff i is a calland j = i+1 is not a return; used before). If i is an internal position, or a pending call • Sr(i, j) holds iff i is nota calland j = i+1 is a return. or return position, then it has no descendants and its next • Scr(i, j) holds iff µ(i, j) and there is a path from i to sibling is i +1. Matched returns do not have next sibling. j using only call and return edges. It is routine to define, in FO, relations and for desc sib cir holds iff and neither nor descendant and younger sibling in such a forest. Further- • S (i, j) µ(i, j) j = i +1 Scr(i, j). more, from these relations, we can define the usual ≤ and µ in nested words using at most 3 variables as follows. The Let T denote the set {c,i,r,cr,cir} of all edge types. In formulas for x ≤ y and µ(x, y) are given by addition to the built-in predicates St for t ∈ T , we add the transitive closure of all unions of subsets of these relations. (y desc x) ∨ ∃z x desc z ∧ ∃x (z ≺sib x ∧ y desc x) That is, for each non-empty set Γ ⊆ T of edge types, let t (y desc x) ∧ ∀z (z desc x) → z ≤ y) . SΓ stand for the union ∪ S , and let ≤Γ be the reflexive- t∈Γ transitive closure of SΓ. Now when we refer to FO2 over Thus, it suffices to prove the three-variable property for nested words, we mean FO in the vocabulary of the unary such ordered forests, which will be referred to as A, B, etc. predicates plus all the ≤Γ’s, the five successor relations, and Gv We shall use pebble games. Let m(A,a1,b1, B,b1,b2) be the built-in unary call and ret predicates. the m-move, v-pebble game on structures A and B where We define a temporal logic unary-NWTL that has future initially pebbles xi are placed on ai in A and bi in B. and past versions of next operators parameterized by edge Gv Player II has a winning strategy for m(A,a1,b1, B,b1,b2) types, and eventually operators parameterized by a set of iff A,a1,a2 and B,b1,b2 agree on all formulas with at edge types. Its formulas are given by: most v variables and quantifier-depth m. We know from ′ [13] that to prove Theorem 6.1, it suffices to show that, ϕ := ⊤ | a | call | ret | ¬ϕ | ϕ ∨ ϕ | t t Γ for all k, if Player II has a winning strategy for the game ϕ | ⊖ ϕ | 3Γϕ | ϕ G3 (A,a ,a ; B,b ,b ), then she also has a winning 3k+2 1 2 1 2 where a ranges over Σ, t ranges over T , and Γ ranges over strategy for the game Gk(A,a ,a ; B,b ,b ). k 1 2 1 2 non-empty subsets of T . The semantics is defined in the We show that Player II can win the k-pebble game by obvious way; for example, (¯w,i) |= 3Γϕ iff for some po- maintaining a set of 3-pebblesubgameson which she copies sition i ≤Γ j, (¯w, j) |= ϕ. Player I’s moves and decides on responses using her win- For an FO2 formula ϕ(x) with one free variable x, let ning strategy for these smaller 3-pebble games. The choice qdp(ϕ) be its quantifier depth, and for a unary-NWTL for- of these sub-games will partition the universe |A| ∪ |B| so mula ϕ′, let odp(ϕ′) be its operator depth. that each play by Player I in the k-pebble game will be an- swered in one 3-pebble game. This is similar to the proof Theorem 6.2 1. unary-NWTL is expressively complete that linear orderings have the 3-variable property [13]. 2 for FO2. 9 2. If formulas are viewed as DAGs (i.e identical sub- [3] R. Alur and P. Madhusudan. Visibly pushdown languages. formulas are shared), then every FO2 formula ϕ(x) In STOC’04, pages 202–211. can be converted to an equivalent unary-NWTL for- [4] R. Alur and P. Madhusudan. Adding nesting structure to mula ϕ′ of size 2O(|ϕ|(qdp(ϕ)+1)) and odp(ϕ′) ≤ words. In DTL’06, pages 1–13. 10 qdp(ϕ). The translation is computable in time [5] T. Ball and S. Rajamani. Bebop: A symbolic model checker polynomial in the size of ϕ′. for boolean programs. In SPIN’00, pages 113–130. 3. Model checking of unary-NWTL can be carried out [6] V. B´ar´any, C. L´oding, O. Serre. Regularity problems for vis- ibly pushdown languages. STACS 2006, pages 420–431. with the same worst case complexity as for NWTL. [7] Document Object Model DOM. http://www.w3.org/DOM. [8] J. Esparza and S. Schwoon. A BDD-based model checker 2 Proof sketch. The translation from unary-NWTL into FO for recursive programs. In CAV’01, pages 324–336. is standard. For the other direction we adapt techniques of [9] K. Etessami, M. Vardi, and T. Wilke. First-order logic with 2 [9]. Given an FO formula ϕ(x), the translation works a two variables and unary temporal logic. Information and follows. When ϕ(x) is of the form a(x), for a proposi- Computation 179(2): 279–295, 2002. tion a, it outputs a. The cases of Boolean connectives are [10] M. Frick, M. Grohe. The complexity of first-order and straightforward. The two cases that remain are when ϕ(x) monadic second-order logic revisited. LICS 2002, 215–224. ∗ ∗ is of the form ∃x ϕ (x) or ∃y ϕ (x, y). In both cases, we [11] G. Gottlob, C. Koch. Monadic datalog and the expressive say that ϕ(x) is existential. In the first case, ϕ(x) is equiv- power of languages for web information extraction. Journal alent to ∃y ϕ∗(y) and, viewing x as a dummy free variable of the ACM 51 (2004), 74–113. in ϕ∗(y), this reduces to the second case. [12] H.W. Kamp. Tense Logic and the Theory of Linear Order. In the second case, we can rewrite ϕ∗(x, y) as Ph.D. Thesis, UCLA, 1968. β(χ0(x, y), ..., χr−1(x, y), ξ0(x), ..., ξs−1(x), ζ0(y), [13] N. Immerman and D. Kozen. Definability with bounded ...,ζt−1(y)), where β is a propositional formula, each χi number of bound variables. Information and Computation, is an atomic order formula, and ξi’s and ζi’s are atomic or 83 (1989), 121-139. existential FO2 formulas with quantifier depth < qdp(ϕ). [14] N. Klarlund, T. Schwentick and D. Suciu. XML: model, In order to be able to recurse on subformulas of ϕ we have schemas, types, logics, and queries. In Logics for Emerg- ing Applications of Databases, Springer, 2003, pages 1–41. to separate the ξi’s from the ζi’s. For that, we consider mu- tually exclusive and complete order types that enumerate [15] L. Libkin. Logics for unranked trees: an overview. In ICALP possible order relations between x and y with respect to 2005, pages 35-50. different St’s. Under each order type, each atomic order [16] C. L¨oding, P. Madhusudan, O. Serre. Visibly pushdown formula evaluates to either ⊤ or ⊥. Furthermore, if τ is games. In FSTTCS 2004, pages 408–420. an order type, ψ(x) an FO2 formula, and ψ′ an equivalent [17] P. Madhusudan, personal communication. unary-NWTL formula, one can obtain a unary-NWTL for- [18] M. Marx. Conditional XPath, the first order complete XPath mula τhψi equivalent to ∃y(τ ∧ ψ(y)). Using this and the dialect. In PODS’04, pages 13–22. ′ ′ ′ hypothesis for ξi for i 10 A Proofs and Intermediate Results Some terminology The quantifier rank (or quantifier depth)ofan FO formula ϕ is the depth of quantifier nesting in ϕ. The rank-k type of a structure M over a relational vocabulary is the set {ϕ | M |= ϕ and the quantifier rank of ϕ is k}, where ϕ ranges over FO sentences over the vocabulary. It is well-known that there are finitely many rank-k types for all k, and for each rank-k type τ there is an FO sentence ϕτ such that M |= ϕτ iff the rank-k type of M is τ. Sometimes we associate types with formulas that define them. Many proofs in this paper make use of Ehrenfeucht-Fra¨ısse` (EF) games. This game is played in two structures, M and M′, over the same vocabulary, by two players, Player I and the Player II. In round i Player I selects a structure, say M, and ′ an element ci in the domain of M; Player II responds by selecting an element ei in the domain of M . Player II wins in k ′ rounds, for k ≥ 0, if {(ci,ei) | i ≤ k} defines a partial isomorphism between M and M . Also, if a¯ is an m-tuple in the ′ ′ domain of M and ¯b is an m-tuple in the domain of M , where m ≥ 0, we write (M, a¯) ≡k (M , ¯b) whenever Player II wins in k rounds no matter how Player I plays, but starting from position (¯a, ¯b). ′ ′ We write M ≡k M iff M and M have the same rank-k type, that is for every FO sentence ϕ of quantifier rank-k, ′ ′ M |= ϕ ⇔ M |= ϕ. It is well-known that M ≡k M iff Player II has a winning strategy in the k-round Ehrenfeucht-Fra¨ıss`e game on M and M′. In the proof of Theorem 6.1, we shall also use k-pebble games. In such a game, Player I and Player II have access k matching pebbles each, and each round consists of Player I either removing, or placing, or replacing a pebble in one structure, and Player II replicating the move in the other structure. The correspondencegiven by the matching pebbles should be a partial isomorphism. If Player II can play while maintaining partial isomorphism for m rounds, then the structures agree on all FOk sentences of quantifier rank up to m; if Player II can play while maintaining partial isomorphism forever, then the structures agree on all FOk sentences. Proof of Theorem 4.1 We start with the easy direction NWTL ⊆ FO. Lemma A.1 For every NWTL formula ϕ, there exists an FO formula αϕ(x) that uses at most three variables x,y,z such that for every nested word w¯ (finite or infinite), we have (¯w,i) |= ϕ iff w¯ |= αϕ(i). Proof of Lemma A.1: The proof is by induction on the formulas and very simple for all the cases except Uσ and Sσ: for example, α µϕ(x) = ∃y µ(x, y) ∧ ∃x (x = y ∧ αϕ(x)) . σ For translating U , we need a few auxiliary formulas. Our first goal is to define a formula γr(x, z) saying that x is R(z), i.e. the return of the innermost call within which z is executed. For that, we start with δ(y,z) = z ∃y (y = x ∧ δ(y,z)) ∧ ∀y (δ(y,z) → y ≥ x). Likewise, we define γc(y,z) stating that that y equals C(z), that is, the innermost call within which z is executed. Now define χ1(y,z) = ∃x γr(x, z) ∧ x ≤ y , χ2(x, z) = ∃y γc(y,z) ∧ y ≥ x and χ(x,y,z) as χ1(y,z) ∧ χ2(x, z). Then this formula says that the summary path from x to ydoes not pass through z, assuming x αψ(x) ∨ ∃y y > x ∧ αϕ(x) ∧ ∃x (x = y ∧ αψ(x)) ∧ ∀z (x