by Francisco Durán, Santiago Escobar and Salvador Lucas.
Handling infinite objects is a typical feature of lazy (functional) languages. Although reductions in Maude are basically innermost (or eager), Maude is able to exhibit a similar behavior by using strategy annotations. Maude strategy annotations are lists of non-negative integers associated to function symbols which specify the ordering in which the arguments are (eventually) evaluated in function calls: when considering a function call f(t1,...,tk), only the arguments whose indices are present as positive integers in the local strategy (i1··· in) for f are evaluated (following the specified ordering). If 0 is found, a reduction step on the whole term f(t1,...,tk) is attempted. In fact, Maude gives a strategy annotation (1 2 ··· k 0) to each symbol f without an explicit strategy annotation.
Consider the following modules LAZY-NAT and LIST-NAT defining sorts Nat and LNat, and symbols 0 and s for defining natural numbers, and symbols nil (the empty list) and _._ for the construction of lists.
fmod LAZY-NAT is sort Nat . op 0 : -> Nat . op s : Nat -> Nat [strat (0)] . op _+_ : Nat Nat -> Nat . vars M N : Nat . eq 0 + N = N . eq s(M) + N = s(M + N) . endfm fmod LIST-NAT is pr LAZY-NAT . sorts LNat . subsort Nat < LNat . op _._ : Nat LNat -> LNat [strat (1 0)] . op nil : -> LNat . op nats : -> LNat . op incr : LNat -> LNat . op length : LNat -> Nat . vars X Y : Nat . vars XS YS : LNat . eq incr(X . XS) = s(X) . incr(XS) . eq nats = 0 . incr(nats) . eq length(nil) = 0 . eq length(X . XS) = s(length(XS)) . endfmStrategy annotations can often improve the termination behavior of programs (by pruning all infinite rewrite sequences starting from any expression). In the example above, the strategies (0) and (1 0) for symbols s and _._, respectively, guarantee that the resulting program is terminating. This termination proof of the specification can be formally proved by using the tool MU-TERM (note that both strategies are necessary for such a proof of termination). Strategy annotations can also improve the efficiency of computations (e.g., by reducing the number of attempted matchings or avoiding useless or duplicated reductions) [Eker98].
Nevertheless, the absence of some indices in the local strategies can also jeopardize the ability of such strategies to compute normal forms. For instance, the evaluation of the expression s(0) + s(0) using the Maude 2.0 [CDELMMT03] yields the following:
Maude> (red s(0) + s(0) .) result Nat: s(0 + s(0))
Due to the annotation (0) for the symbol s, the contraction of the redex 0 + s(0) is not possible and the evaluation stops here.
The handicaps, regarding correctness and completeness of computations, of using (only) positive annotations are discussed in, e.g., [AEGL02,WRLA02,Lucas01, NO01,OF00], and a number of solutions have been proposed:
In [DEL04], we have introduced two new commands (norm and eval) to make techniques 1 and 2 available for the execution of Maude programs, follow this link for the implementation of both commands. In this piece of work we show how to bring on-demand strategies into Maude. Before entering into details, we show how negative indices can improve Maude strategy annotations.
The following NATS-TO-BIN module uses previous modules LAZY-NAT and LIST-NAT and implements the binary encoding of natural numbers as lists of 0 and 1 (starting from the least significative bit).
fmod NATS-TO-BIN is ex LAZY-NAT . pr LIST-NAT . op 1 : -> Nat . op natToBin : Nat -> LNat . op natToBin2 : Nat Nat -> LNat . vars M N X : Nat . vars XS YS : LNat . eq natToBin2(0, 0) = 0 . eq natToBin2(0, M) = 0 . natToBin(M) . eq natToBin2(s(0), 0) = 1 . eq natToBin2(s(0), M) = 1 . natToBin(M) . eq natToBin2(s(s(N)), M) = natToBin2(N, s(M)) . eq natToBin(N) = natToBin2(N, 0) . endfm
The evaluation of the expression natToBin(s(0) + s(0)) should yield the binary representation of 2. However, we get:
Maude> (red natToBin(s(0) + s(0)) .) result LNat: natToBin2(s(0 + s(0)), 0)
The problem is that the current strategy annotations disallow the evaluation of subexpression 0 + s(0) in natToBin2(s(0 + s(0)), 0), thus disabling the application of the last equation for natToBin2. The use of the command norm introduced in [DEL04] does not solve this problem, since it just normalizes non-reduced subexpressions:
Maude> (norm natToBin(s(0) + s(0)) .) result LNat: natToBin2(s(s(0)), 0)
On-demand strategy annotations can solve this problem. In fact, the use of the strategy (-1 0) for symbol s, declaring its first argument as evaluable only on-demand, permits to recover the desired behavior while keeps termination of the program. The on-demand evaluation obtains a head-normal form:
Maude> (red natToBin(s(0) + s(0)) .) result LNat : 0 . natToBin(s(0))However, the redefinition of Maude command red is not able to provide the intended binary representation of 2, since the annotation 2 is missing in the strategy list for symbol _._ and we must redefine the command norm of DEL04 to perform a layered normalization of the output given by the on-demand evaluation previously presented.
Maude> (norm natToBin(s(0) + s(0)) .) result LNat : 0 . 1
The reflective capabilities of Maude are the key for building such language extensions, which turn out to be very simple to use thanks to the infrastructure provided by Full Maude. Full Maude is an extension of Maude written in Maude itself, that endows Maude with notation for object-oriented modules and with a powerful and extensible module algebra [CDELMMQ02]. Its design, and the level of abstraction at which it is given, make of it an excellent metalevel tool in which testing and experimenting with features and capabilities not present in (Core) Maude [DM98,Duran99,CDELMMQ02]. We make use of the extensibility and flexibility of Full Maude to permit the use of both red (the usual evaluation command of Maude) and norm (introduced in [DEL04]) with Maude programs using on-demand strategy annotations.
First, the original Full Maude file full-maude.maude contains a small bug that avoids proper execution of local strategies with negative annotations. Thus, the fixed Full Maude file full-maude-fixOct102004.maude should be downloaded and included in the distribution directory or in the local directory with the Maude file redOnDemand.maude (shown below).
Second, download the following Maude file with auxiliary functions utils.maude.
Third, download the appropriate Maude files:
The example used in the description section above is available here for testing the implementation.
[AEGL02]
M. Alpuente, S. Escobar, B. Gramlich, and S. Lucas.
Improving on-demand strategy annotations.
In Matthias Baaz and Andrei Voronkov, editors, Proc. 9th Int.
Conf. on Logic for Programming, Artificial Intelligence, and Reasoning
(LPAR'02), Lecture Notes in Computer Science, Tbilisi, Georgia, October
2002. Springer-Verlag.
[WRLA02]
M. Alpuente, S. Escobar, S. Lucas.
Correct and Complete (Positive) Strategy Annotations for OBJ.
4th Int'l Workshop on Rewriting Logic and its Applications (WRLA 2002), Pisa (Italy).
Electronic Notes on Theoretical Computer Science, Vol. 71. To appear 2003.
[CDEMS99]
M. Clavel, F. Durán, S. Eker, J. Meseguer, and M.-O. Stehr.
Maude as a formal meta-tool.
In J. Wing, J. Woodcock, and J. Davies, editors,
FM'99 - Formal Methods (Vol. II),
volume 1709 of Lecture Notes in Computer Science,
pages 1684--1704. Springer-Verlag, 1999.
[CDELMMQ02]
M. Clavel, F. Durán, S. Eker, P. Lincoln, N. Martí-Oliet, J. Meseguer,
and J. Quesada.
Maude: specification and programming in rewriting logic.
Theoretical Computer Science 285(2):187-243, 2002.
[CDELMMT03]
M. Clavel, F. Durán, S. Eker, P. Lincoln, N. Martí-Oliet, J. Meseguer, and C. Talcott.
The Maude 2.0 System.
In R. Nieuwenhuis, editor,
Proc. of 14th International Conference on Rewriting Techniques and
Applications, RTA'03, Lecture Notes in Computer Science 2706:76-87,
Springer, 2003.
[Duran99]
F. Durán.
A Reflective Module Algebra with Applications to the Maude Language.
PhD thesis, Universidad de Málaga, June 1999.
[DEL04]
F. Durán, S. Escobar, S. Lucas.
New evaluation commands for Maude within Full Maude.
In Proc. of 5th International Workshop on Rewriting Logic and its Applications,
WRLA'04, ENTCS to appear, 2004.
[DM98]
F. Durán and J. Meseguer.
An extensible module algebra for Maude.
In C. Kirchner and H. Kirchner, editors, Proceedings
of 2nd International Workshop on Rewriting Logic and its Applications
(WRLA'98), volume 15 of Electronic Notes in Theoretical Computer Science. Elsevier, 1998.
[Eker98]
S. Eker.
Term Rewriting with Operator Evaluation Strategies.
In C. Kirchner and H. Kirchner, editors, Proceedings
of 2nd International Workshop on Rewriting Logic and its Applications
(WRLA'98), volume 15 of Electronic Notes in Theoretical Computer Science. Elsevier, 1998.
[Lucas01]
S. Lucas.
Termination of on-demand rewriting and termination of OBJ programs.
In Proc. of 3rd International Conference on Principles and
Practice of Declarative Programming, PPDP'01, pages 82-93, ACM Press, 2001.
[Lucas02a]
S. Lucas.
Context-sensitive rewriting strategies.
Information and Computation, 178(1):293-343, 2002.
[Lucas02b]
S. Lucas.
Termination of (Canonical) Context-Sensitive Rewriting.
In S. Tison, editor, Proc. 13th International
Conference on Rewriting Techniques and Applications, RTA'02, Lecture Notes in
Computer Science 2378:296-310, Springer, 2002.
[Lucas03a]
S. Lucas.
Semantics of programs with strategy annotations.
Technical Report DSIC-II/08/03, DSIC, Universidad Politécnica de
Valencia, 2003.
[Lucas03b]
S. Lucas.
Termination of programs with strategy annotations.
Technical Report DSIC II/20/03, Universidad Politécnica de
Valencia, Sep. 2003.
[Meseguer99]
J. Meseguer.
Research directions in rewriting logic.
In U. Berger and H. Schwichtenberg, editors, Computational
Logic. Springer-Verlag, 1999.
[MM02]
N. Martí-Oliet and J. Meseguer.
Rewriting logic as a logical and semantic framework.
volume 9, pages 1--87. Kluwer Academic Publishers, second edition,
2002.
[NO01]
M. Nakamura and K. Ogata.
The evaluation strategy for head normal form with and without on-demand flags.
In K. Futatsugi, editor, Proc. of 3rd International
Workshop on Rewriting Logic and its Applications, WRLA'00,
Electronic Notes in Theoretical Computer Science, volume 36, 17 pages, 2001.
[OF00]
K.~Ogata and K.~Futatsugi.
Operational semantics of rewriting with the on-demand evaluation strategy.
In Proc. of 2000 International Symposium on Applied Computing, SAC'00,
pages 756--763. ACM Press, New York, 2000.
ELP | GPLIS | DSIC | UPV |
Last update October 13 2004 # sescobar@dsic.upv.es