Redundant arguments in in nitary rewriting (extended abstract)? Salvador Lucas Departamento de Sistemas Informaticos y Computacion Universidad Politecnica de Valencia Camino de Vera s/n, E-46022 Valencia, Spain e.mail:
[email protected]
1 Introduction
In [AEL00], we have investigated the problem of nding redundant arguments of function symbols de ned by means of the rules of a Term Rewriting System (TRS). We have provided a semantic de nition of redundancy which is parametric w.r.t. a given semantics S mapping nite terms into sets of terms which can be obtained by reducing the initial term. Given a semantics S for a TRS R, we say that the i-th argument of a function symbol f is redundant w.r.t. S and R if for all ground context C [ ], f -rooted ground term t and ground term s, S(C [t]) = S(C [t[s] ]). Example 1. Consider the TRS R [AEL00]: i
zero(0) zero(1)
! !
0 0
where 0 and 1 are the only possible values. The argument of zero is redundant for evaluating (i.e., obtaining the value of) any call C [zero(t)] for arbitrary contexts C [ ] and terms t (see [AEL00]). Thus, the i-th argument of a function f is redundant if we do not need to consider t in order to compute the semantics of any call containing a subterm f (t1 ; : : :; t ). Functions with redundant arguments may arise as a consequence of the uncontrolled application of automatic transformation processes during the formal development and optimization of programs (e.g., partial evaluation techniques [AFV98,CD93,JGS93]). Such kind of manipulations can introduce encumbrances in the generated code that programmers usually (or presumably) do not write. Redundant arguments are useless for computing the semantics of expressions. Moreover, they introduce spatial and temporal overheads due to the need of allocating memory space for representing those arguments and the possibility of traversing their structure when looking for a redex or reducing redexes within redundant arguments during the computation. i
k
?
This work has been partially supported by CICYT TIC 98-0445-C03-01 and by Accion Integrada hispano-alemana HA1997-0073.
Example 2. (Continuing Example 1) For an arbitrary term t, we know that the
value of zero(t) is 0. However, in order to evaluate the term zero(zero(zero(1))), we need to perform three reduction steps: zero(zero(zero(1)))
!
zero(zero(0))
!
zero(0)
!
0
We have formalized program analysis techniques for detecting these kinds of redundancies as well as correct and complete transformations which are able to eliminate dead code which appears in the form of redundant function arguments. Example 3. (Continuing Example 1) Concerning computation of values, TRS zero
!
0
zero
!
0
is semantically equivalent to R in Example 1; in order to use this transformed TRS, terms including calls to the unary function zero should also be transformed by eliminatingthe argument. For instance, the derivation of zero(zero(zero(1))) to value 0 of Example 2 contracts into thus avoiding wasteful memory allocations and reduction steps. Lazy functional languages admit the possibility of giving in nite values as the meaning of some expressions [PE93,Rea93]. In nite values are de ned as limits of converging in nite sequences of partially de ned values which are more and more de ned. Example 4. Consider the TRS R: from(x)
!
x:from(s(x))
from(0)
!
0:from(1)
The term from(0) has no normal form, since each application of the from rule always introduces a new function call for the function from. However, the following reduction sequence (we use 0,1,2,: : : to abbreviate 0,s(0),s(s(0)),: : :):
!
suggests that the `in nite value',
0:1:2:3:
0:1:from(2)
!
which corresponds to the in nite list of all natural numbers, could be considered as the (in nite) value of from(0). According to this situation, some research has been done concerning in nitary rewriting, i.e., rewriting that also considers in nite reduction sequences, probably involving in nite terms, and even term rewriting systems built from in nite terms [DKP91,KKSV95,Mid97]. The redundancy of an argument of a function f in a TRS R depends on the semantic properties of R that we are interested in observing. In [AEL00], we consider dierent (reduction) semantics including the standard normalization semantics nf (typical of pure rewriting [Jou94]) and the evaluation semantics eval (closer to functional programming [PE93,Rea93]). We have also formalized
some methods for analyzing redundancies w.r.t. eval. Since the results in [AEL00] show that eval provides the best oportunities for nding out redundancies, the emphasis in eval deployed in [AEL00] is quite justi ed. However, there are two main motivations for investigating redundancy of arguments with respect to in nitary semantics such as !?eval and !?nf (the respective extensions of eval and nf when considering in nitary rewritings) rather than just eval as done in [AEL00]. The rst one is that eval is not the adequate semantic framework for lazy languages; the use of information about redundancies based on eval can lead to unsound erasures of arguments. Example 5. The rst argument of from as de ned by the rule of Example 4 is redundant w.r.t. eval: since no expression C [from(t)] has a normal form for any term t, the set of nite values to which C [from(t)] rewrites is empty; hence eval(C [from(t)]) = eval(C [from(s)]) = ?. However, the elimination of the argument would yield the following transformed TRS (see [AEL00]): from
!
0:from
Note that this is not correct since the in nite list 0:0: :0: which is the (only) in nite value of from in the transformed TRS (according to !?eval) is not reachable from any call from(t). Hence, information about redundancies w.r.t. eval can (and should) not be used for eliminating redundant arguments in lazy languages. The second motivation is that, even if we are actually interested in eval (e.g., when considering eager languages such as OBJ, Maude, ELAN, or Hope), the framework in [AEL00] is not very useful for detecting redundancies in non-terminating TRSs because termination is required for eectively deciding about redundancies w.r.t. eval. The reason is that, according to the framework in [AEL00], ensuring eval-completeness (i.e., that eval(t) 6= ? for every ground term t) is quite important for characterizing redundancies w.r.t. eval. However, for ensuring eval-completeness we usually require termination of the TRS (see [HH80]). Since (even if we are not directly interested in in nite values) non-termination is frequent in real programs, it is interesting to isolate the detection of redundancies from the requirement of termination of the TRS. In this way, and according to [AEL00], if we are able to develop methods for discovering redundancies w.r.t. !?eval (or even !?nf ) then we can safely use this information even with eval (note that in Example 5 we wrongly tried the opposite way). In this paper, we discuss how to deal with non-terminating TRSs and in nitary semantics in the analysis of redundant arguments in term rewriting systems. References [AEL00] M. Alpuente, S. Escobar, and S. Lucas. Redundancy Arguments in Term Rewriting. In M. Alpuente, editor, Proc of 9th International Workshop on Functional and Logic Programming, WFLP'2000, Universidad Politecnica de Valencia, 2000.
[AFV98] M. Alpuente, M. Falaschi, and G. Vidal. Partial Evaluation of Functional Logic Programs. ACM TOPLAS, 20(4):768{844, 1998. [CD93] C. Consel and O. Danvy. Tutorial notes on Partial Evaluation. In Conference Record of the 20th Annual ACM Symposium on Principles of Programming Languages, POPL'93, pages 493-501. ACM, New York, 1993. [Der87] N. Dershowitz. Termination of rewriting. Journal of Symbolic Computation, 3:69-115, 1987. [DKP91] N. Dershowitz, S. Kaplan, and D. Plaisted. Rewrite, rewrite, rewrite, rewrite, rewrite. Theoretical Computer Science 83:71-96, 1991. [FH88] A.J. Field and P.G. Harrison. Functional Programming. Addison-Wesley Publishing Company, 1988. [HH80] G. Huet and J.M. Hullot. Proofs by Induction in Equational Theories with Constructors. In Proc. of FOCS'80, pages 96-107. IEEE Computer Society, Los Alamitos, California, 1980. [JGS93] N.D. Jones, C.K. Gomard, and P. Sestoft. Partial Evaluation and Automatic Program Generation. Prentice-Hall, Englewood Clis, NJ, 1993. [Jou94] J.-P. Jouannaud. Rewrite Proofs and Computations. In H. Schwichtenberg, editor, Proof and Computation, NATO Series F: Computer and Sciences, 139:173-218, Springer-Verlag, Berlin, 1994. [KKSV95] R. Kennaway, J.W. Klop, M.R. Sleep, and F.-J. de Vries. Trans nite reductions in Orthogonal Term Rewriting Systems. Information and Computation 119(1):18-38, 1995. [Mid97] A. Middeldorp. Call by Need Computations to Root-Stable Form. In Conference Record of the 24th Annual ACM Symposium on Principles of Programming Languages, POPL'97, pages 94-105, ACM Press, 1997. [PE93] R. Plasmeijer and M. van Eekelen. Functional Programming and Parallel Graph Rewriting. Addison-Wesley, Reading, MA, 1993. [Rea93] C. Reade. Elements of Functional Programming. Addison-Wesley Publishing Company, 1993.