Applications and extensions of context-sensitive rewriting

https://doi.org/10.1016/j.jlamp.2021.100680Get rights and content

Abstract

Context-sensitive rewriting is a restriction of term rewriting which is obtained by imposing replacement restrictions on the arguments of function symbols. It has proven useful to analyze computational properties of programs written in sophisticated rewriting-based programming languages such as CafeOBJ, Haskell, Maude, OBJ*, etc. Also, a number of extensions (e.g., to conditional rewriting or constrained equational systems) and generalizations (e.g., controlled rewriting or forbidden patterns) of context-sensitive rewriting have been proposed. In this paper, we provide an overview of these applications and related issues.

Introduction

When computing with reduction-based systems, rules cannot be applied just anywhere [97, page 34]. For instance, in Generalized Rewrite Theories [16], [17], the use of replacement restrictions, aimed at avoiding this, brings “a substantial increase in expressive power of the Rewriting Logic formalism” (see [93]) that

“has to do with the fact that rewrites should not happen everywhere, because in many applications suitable evaluation strategies or context-dependent rewrites could considerably improve performance and even avoid non-termination. Correspondingly, rewrite theories can be generalized by forbidding rewriting under certain operators or operator positions (frozen operators and arguments). Although this could be regarded as a purely operational aspect, the frequent need for it in many applications suggests that it should be supported directly at the semantic level of rewrite theories.” [17, Section 1]

Perhaps this observation explains why replacement restrictions imposed by context-sensitive rewriting (CSR [78], [85]) have been used to analyze semantic aspects and properties of several programming languages and systems. Reporting on these applications of CSR is the main purpose of this paper.

In CSR a replacement map μ specifies, for each k-ary symbol f, the active argument positions μ(f){1,,k} where rewriting is allowed in a function call f(t1,,tk) (while any other argument ti with iμ(f) remains frozen). In unrestricted rewriting, if a term s rewrites into t, then, for all k-ary function symbols f and arguments i, 1ik, we have that f(si) rewrites into f(ti), i.e., s still rewrites into t when surrounded by any syntactic context f(i). In CSR, this happens (and it is top-down propagated) for indices iμ(f) only.

A motivating example  In connection with the use of CSR to reinforce termination of programs, consider the Maude [19] program in Fig. 1 which is a Maude presentation of the TRS in [81, Example 2] with the replacement map used in [85, Examples 6.7 and 9.8] and that can be used to compute approximations to π2/6=1,64493406684823 as the sum of the n components of an initial sublist s=first(sn(0),terms(s(0)))1 of the infinite list terms(s(0)), consisting of 112,122,132,,1n2, , where each reciprocal fraction 1m above is represented as recip(sm(0)) see [44, page 265]. Note that, if we drop the frozenness annotation for the list constructor _:_, i.e.,

  •  op _:_ : S S -> S [frozen (2)] .

which corresponds to a replacement map μ given by
and μ(f)={1,,k} for any other k-ary symbol f, the program is not terminating due to the last program rule which permits an infinite recursion on successive recursive calls to terms(sn(0)) for n0. The attempt to use the Maude command rewrite to obtain the first 4 components of the sequence approximating π2/6 yields:
  • Maude> rew first(s(s(s(s(0)))),terms(s(0))) .
    rewrite in ExSec11_1_Luc02 : first(s(s(s(s(0)))), terms(s(0))) .
    rewrites: 6 in 0ms cpu (0ms real) (750000 rewrites/second)
    result S: recip(s(0)) : first(s(s(s(0))), terms(s(s(0))))

The replacement restrictions make the program terminating by avoiding reductions on the second argument of _:_ (thus cutting the aforementioned infinite recursion). However, the program fails to obtain the expected outcome [11,14,19,116], represented by the program expressionrecip(s(0)):recip(s4(0)):recip(s9(0)):(recip(s16(0)):nil. In Section 10.2, though, we show how to overcome this problem.

Contributions of the paper  In [85] the basic aspects and facts about CSR were described, including the practical use of CSR by means of the interpreters of existing rewriting-based languages with capabilities to express replacement maps (in particular, Maude, as exemplified above), the ability of CSR to simulate unrestricted rewriting, the analysis of confluence and termination of CSR, and how CSR can be used to compute canonical forms (head-normal forms, values, normal forms, and (approximations of) infinite normal forms) which are of interest in rewriting-based computations and (algebraic, equational, and functional) programming languages.

In this paper we focus on applications and extensions of CSR developed in the last 20 years by several authors to model, investigate, and prove properties of rewriting and rewriting-based programming languages. After some preliminary definitions in Sections 2 and 3 (which try to make this paper sufficiently self-contained), we explore the use of CSR to analyze termination properties of variants of rewriting like first-order lazy functional programs, and innermost and outermost rewriting (Section 4), conditional rewriting (Section 5), productivity (Section 6), and runtime complexity (Section 7). Section 8 investigates the interaction of CSR with related notions of rewriting, like conditional rewriting, constrained rewriting, equational rewriting, and narrowing. Section 9 shows how the notion of CSR has been modified to be used with rewriting to achieve more flexibility, leading to on-demand strategy annotations, lazy rewriting, rewriting with forbidden patterns, and controlled term rewriting. Section 10 shows how the theory of CSR has been used to analyze properties of OBJ programs2 and improve their computations by including (in Maude) techniques developed for CSR like normalization via μ-normalization which can now be used through Maude's strategy language. In the development of these sections, we made an effort to provide a uniform presentation and draw connections among them and provide illustrative examples of use. Section 11 concludes. Some technical results in the paper are new (thus labeled with (⋆)).

Section snippets

Preliminaries

This section collects some definitions and notations from term rewriting. More details and missing notions can be found in [12], [115]. In the following, P(A) denotes the powerset of a set A. Given a binary relation RA×A on a set A, we denote its transitive closure by R+, and its reflexive and transitive closure by R. An element aA is irreducible (or an R-normal form), if there exists no b such that aRb. We say that b is an R-normal form of a (written aR!b), if aRb and b is an R-normal

Context-sensitive rewriting

The concepts and notations in this section are extracted from [85]. A replacement map is a mapping μ:FP(N) satisfying that, for all symbols f in F, μ(f){1,,ar(f)}. The set of replacement maps for a signature F is MF (for TRSs R=(F,R), we use MR instead). Replacement maps are compared as follows: μμ if for all fF,μ(f)μ(f); we often say that μ is more restrictive than μ. Extreme cases are μ, which disallows replacements in all arguments: μ(f)= for all fF; and μ, which restricts no

Termination of CSR and other termination properties

In the following, we show how termination of CSR has been used to prove other termination properties like termination of variants of rewriting with TRSs like in lazy (first-order) functional programs (Section 4.1), innermost rewriting (Section 4.2), and outermost rewriting (Section 4.3).

CSR in the analysis of conditional rewriting

Recall from [115, Section 7] the usual notions and notations regarding Conditional Term Rewriting Systems (CTRSs). Conditional rules are written rc, where and r are respectively called the left- and right-hand side of the rule, and the conditional part c is a sequence s1t1,,sntn, where si,ti are terms. Terms s and t of a condition st are called the left- and right-hand side of the condition, respectively (lhs and rhs for short). A CTRS R whose rules satisfy Var(r)Var()Var(c) is

Productivity

Productivity in lazy functional languages “captures the idea of computability, of progress of infinite-list programs. If an infinite-list program is productive, then every element of the list can be computed in finite ‘time’” [124].

Remark 3 Infinite terms

Productivity often involves the possibility of computing (i.e., approaching) infinite terms. Formally, infinite terms are partial functions t:N>0FX from sequences of positive numbers to symbols where the domain Dom(t) is a tree-domain, i.e., an infinite set of

Obtaining bounds on runtime complexity

The derivational height dh(s,R) of a term s with respect to a finitely branching relation R is defined (for R-terminating terms s) as the maximal length of R-sequences starting from s [67]. Then, given nN, the derivational complexity for R is dcR(n)=max{dh(s,R)||s|n}, where |s| is the size of s, i.e., the number of symbols occurring in s. Hence, for all terms s, dh(s,R)dcR(|s|). In derivational complexity analysis, we aim at finding (upper and lower) asymptotic bounds on dcR(n). Since dh(s,R

CSR for variants of term rewriting

In order to make the advantages of CSR available in other computational settings and programming languages, several extensions to more general rewriting-based frameworks like conditional rewriting, constrained rewriting (where the rules include a conditional part to be tested before being able to rewrite any call), equational rewriting (where terms are rewritten modulo an equational theory), and narrowing (where pattern matching is replaced by unification when function calls are bound to rules)

Variants of CSR for term rewriting

Several authors have devised weaker restrictions of rewriting trying to improve the computational power of CSR. As discussed in [85, Section 11], the canonical replacement map μRcan is the usual starting point to use CSR in computations with a (left-linear) TRS R. This guarantees that a number of results and techniques can be used to perform semantically meaningful computations, see [85, Section 9]. However, termination of CSR is often an important reason to use CSR instead of (a strategy for)

Analysis of OBJ programs

In OBJ programs an operator evaluation strategy for a k-ary symbol f is a sequence ξ(f)=(i1i2in), where ij{0,,k} for all 1jn. The evaluation of a term t=f(t1,,tk) proceeds by considering the i1,,in-th immediate subterms of t from left to right. If ij=0, then an attempt to apply a rule to t is made (where t is t with ti1,,tij1 replaced by their evaluated versions ti1,,tij1); otherwise, the evaluation of tij (into tij) is recursively accomplished. The order of indices i1,i2,,in

Conclusions

We have given an overview on applications and extensions of CSR reported by several authors during the last 20 years. Such applications and extensions come from quite different subfields of term rewriting and rewriting-based programming languages: termination analysis and strategies, conditional rewriting, productivity, computational complexity, etc. We made an effort to use a uniform notation for material coming from different authors, and also to introduce unifying approaches hopefully

Declaration of Competing Interest

The authors declare that they have no known competing financial interests or personal relationships that could have appeared to influence the work reported in this paper.

Acknowledgements

I thank Santiago Escobar for his help in the use of narrowing in Maude. I also thank Narciso Martí-Oliet and Rubén Rubio for their help in the details of the Maude strategy language. Thanks are also due to Nao Hirokawa and Georg Moser for their comments on Remark 5. Finally, I'm grateful to Jürgen Giesl, José Meseguer, Masahiko Sakai, and Hans Zantema for reading and commenting on an earlier version of this manuscript.

References (131)

  • J. Endrullis et al.

    Productivity of stream definitions

    Theor. Comput. Sci.

    (2010)
  • J. Endrullis et al.

    Lazy productivity via termination

    Theor. Comput. Sci.

    (2011)
  • M. Fernández

    Relaxing monotonicity for innermost termination

    Inf. Process. Lett.

    (2005)
  • O. Fissore et al.

    Induction for termination with local strategies

    Electron. Notes Theor. Comput. Sci.

    (2001)
  • J.A. Goguen et al.

    Order-sorted algebra I: equational deduction for multiple inheritance, overloading, exceptions and partial operations

    Theor. Comput. Sci.

    (1992)
  • M. Hanus

    The integration of functions into logic programming: from theory to practice

    J. Log. Program.

    (1994)
  • D. Hofbauer

    Termination proofs by multiset path orderings imply primitive recursive derivation lengths

    Theor. Comput. Sci.

    (1992)
  • S. Kaplan

    Simplifying conditional term rewriting systems: unification, termination and confluence

    J. Symb. Comput.

    (1987)
  • S. Lucas

    Context-sensitive rewriting strategies

    Inf. Comput.

    (2002)
  • S. Lucas

    Lazy rewriting and context-sensitive rewriting

    Electron. Notes Theor. Comput. Sci.

    (2002)
  • S. Lucas et al.

    Operational termination of conditional term rewriting systems

    Inf. Process. Lett.

    (2005)
  • S. Lucas et al.

    Operational termination of membership equational programs: the order-sorted way

    Electron. Notes Theor. Comput. Sci.

    (2009)
  • S. Lucas et al.

    Normal forms and normal theories in conditional rewriting

    J. Log. Algebraic Methods Program.

    (2016)
  • S. Lucas et al.

    Dependency pairs for proving termination properties of conditional term rewriting systems

    J. Log. Algebraic Methods Program.

    (2017)
  • S. Lucas et al.

    The 2D dependency pair framework for conditional rewrite systems. Part I: definition and basic processors

    J. Comput. Syst. Sci.

    (2018)
  • J. Meseguer

    Conditional rewriting logic as a unified model of concurrency

    Theor. Comput. Sci.

    (1992)
  • J. Meseguer

    Twenty years of rewriting logic

    J. Log. Algebraic Program.

    (2012)
  • B. Alarcón et al.

    Improving context-sensitive dependency pairs

  • B. Alarcón et al.

    A dependency pair framework for a OR C-termination

  • M. Alpuente et al.

    Improving on-demand strategy annotations

  • N. Andrianarivelo et al.

    Transforming prefix-constrained or controlled rewrite systems

  • N. Andrianarivelo et al.

    Confluence of prefix-constrained rewrite systems

  • S. Antoy

    Definitional trees

  • S. Antoy et al.

    Functional logic programming

    Commun. ACM

    (2010)
  • F. Baader et al.

    Term Rewriting and All That

    (1998)
  • G. Bonfante et al.

    Complexity classes and rewrite systems with polynomial interpretation

  • G. Bonfante et al.

    Algorithms with polynomial interpretation termination proof

    J. Funct. Program.

    (2001)
  • R. Bruni et al.

    Generalized rewrite theories

  • A. Cichon et al.

    Polynomial interpretations and the complexity of algorithms

  • M. Clavel et al.

    All About Maude - A High-Performance Logical Framework, How to Specify, Program and Verify Systems in Rewriting Logic

    (2007)
  • N. Dershowitz et al.

    Canonical conditional rewrite systems

  • F. Durán et al.

    Programming and symbolic computation in Maude

    J. Log. Algebraic Methods Program.

    (2020)
  • F. Durán et al.

    Proving operational termination of membership equational programs

    High.-Order Symb. Comput.

    (2008)
  • F. Durán et al.

    MTT: the Maude termination tool (system description)

  • F. Durán et al.

    Proving termination of membership equational programs

  • J. Endrullis et al.

    Data-oblivious stream productivity

  • J. Endrullis et al.

    Productivity of stream definitions

  • J. Endrullis et al.

    From outermost to context-sensitive rewriting

  • J. Endrullis et al.

    Transforming outermost into context-sensitive rewriting

    Log. Methods Comput. Sci.

    (2010)
  • J. Endrullis et al.

    Matrix interpretations for proving termination of term rewriting

    J. Autom. Reason.

    (2008)
  • Cited by (8)

    • Local confluence of conditional and generalized term rewriting systems

      2024, Journal of Logical and Algebraic Methods in Programming
    • Proving and disproving confluence of context-sensitive rewriting

      2022, Journal of Logical and Algebraic Methods in Programming
      Citation Excerpt :

      Confluence and termination of CSR were first investigated in [17,18]. The ability of CSR to reinforce termination in nonterminating rewrite systems has attracted the attention of a number of researchers, see [20, Section 7], [21, Sections 4, 5, and 6], and the references therein. Regarding (local) confluence of CSR, besides guaranteeing the unicity of normal forms computed by CSR, other uses have been explored: (i) it can be used as a sufficient condition for proving confluence of unrestricted rewriting [11] and also to guarantee uniqueness of normal forms obtained by unrestricted rewriting [20, Section 8.5]; (ii) it can be used (with termination of CSR) to guarantee uniqueness of infinitary normal forms in infinitary rewriting [20, Section 9.4]; (iii) it can be used to (dis)prove confluence of conditional TRSs and conditional CSR [21, Sections 5.3 & 8.1.2]; also, (iv) it has been used in the analysis of derivational complexity of normalization using CSR [22, Section 7.2.3].

    • Metalevel transformation of strategies

      2022, Journal of Logical and Algebraic Methods in Programming
    • Confluence Framework: Proving Confluence with CONFident

      2022, Lecture Notes in Computer Science (including subseries Lecture Notes in Artificial Intelligence and Lecture Notes in Bioinformatics)
    View all citing articles on Scopus

    Partially supported by the EU (FEDER), and projects RTI2018-094403-B-C32 and PROMETEO/2019/098.

    View full text