### Typed generic traversal with term rewriting strategies

Ralf Lämmel
2003 The Journal of Logic and Algebraic Programming
A typed model of strategic term rewriting is developed. The key innovation is that generic traversal is covered. To this end, we define a typed rewriting calculus S γ . The calculus employs a many-sorted type system extended by designated generic strategy types γ . We consider two generic strategy types, namely the types of type-preserving and type-unifying strategies. S γ offers traversal combinators to construct traversals or schemes thereof from many-sorted and generic strategies. The
more » ... al combinators model different forms of one-step traversal, that is, they process the immediate subterms of a given term without anticipating any scheme of recursion into terms. To inhabit generic types, we need to add a fundamental combinator to lift a many-sorted strategy s to a generic type γ . This step is called strategy extension. The semantics of the corresponding combinator states that s is only applied if the type of the term at hand fits, otherwise the extended strategy fails. This approach dictates that the semantics of strategy application must be type-dependent to a certain extent. Typed strategic term rewriting with coverage of generic term traversal is a simple but expressive model of generic programming. It has applications in program transformation and program analysis. Structure of the paper. In Section 2, we provide a gentle introduction to the subject of strategic programming, and to the rewriting calculus S γ . Examples of traversal strategies are given. The design of the type system is motivated. As an aside, we use the term "type" for types of variables, constant symbols, function symbols, terms, strategies, and combinators. We also use the term "sort" in the many-sorted sense if it is more suggestive. In Section 3, we start the formal definition of S γ with its many-sorted core. In this phase, we cannot yet cover the traversal primitives. A minor contribution is here that we show in detail how to cope with type-changing rewrite rules. In Section 4, we provide a type system for generic strategies. The two aforementioned schemes of type preservation and type unification are covered. A few supplementary issues to complement S γ are addressed in Section 5. Implementation issues and related work are discussed in Sections 6 and 7. The paper is concluded in Section 8. Objective. An important meta-goal of the present paper is to develop a simple and selfcontained model of typeful generic programming in the sense of generic traversal of manysorted terms. To this end, we basically resort to a first-order setting of term rewriting. We want to clearly identify the necessary machinery to accomplish generic traversal in such a simple setting. We also want to enable a simple implementation of the intriguing concept of typed generic traversal. The S γ expressiveness is developed in a stepwise manner. In the course of the paper, we show that our type system is sensible from a strategic programmer's point of view. We contend that the type system of S γ disciplines strategic programs in a useful and not too restrictive manner.