Programmable Rewriting Strategies
The manual then begins to discuss “programmable rewriting strategies” in Section 5.2 and beyond to show how to use Stratego’s various features to overcome the difficulties with the last several examples.
The next example is in Section 5.3.1, which shows a unified treatment of
disjunctive and conjunctive normal forms that avoids the potential combinatorial
explosion of rules seen in the functionalized example. As usual, the example
Stratego module prop-laws
is in trans/prop-laws.str
and included in
trans/spoofax_propositional_language.str
. Also as typical, some of the identifiers
in the module had to be changed to avoid namespace conflicts when they are all
imported into the main language module.
But after going through the previous examples, it seems that trying them is most convenient with the Spoofax Testing Language:
- No glue transformation is needed and no ESV files need to be modified.
- It’s not necessary to place the SPL expression to be tested in a separate file.
- The SPL expression, name of strategy to run, and results of that run are all
kept together (in the
test/manual-suite.spt
file).
Hence, the test cases for Section 5.3.1 and beyond, through the end of the Tutorial/Reference Manual, are captured in this repository solely in terms of SPT Test Cases. You can of course clone the repository and create menu items for any of them if you want to try them that way. Just remember to add a “glue” strategy for them as shown in the first example.
Hopefully these examples are helpful to your exploration and understanding of Stratego – certainly creating them was to mine.
Additional notes on particular sections or tests follow.
Chapter 6
Many of the examples in
Chapter 6
are in the form of interactive “Stratego shell” sessions. For this repository, we
have to convert them to rule definitions in a Stratego file (chap6.str
) and
test cases for each of the executed examples.
Also, there are some examples for a Plus-Times signature; we transpose these to Or-And for the sake of keeping everything within the SPL language project.
To illustrate extending a rule, we need to give the rule we’re going to extend
a different identifier and add both definitions to that (otherwise our original
rule SwapArgs
would already have been extended).
The chap6.str
file also includes a solution to the exercise
in Section 6.2
. On the other hand, it is not clear whether
the Spoofax Eclipse IDE has a “debug” operator for Stratego, or if so how
it would work within the IDE, so the repository does not
attempt to implement the “debug” example.
The remainder of the chapter (from section 6.3 on) consists of general information about Stratego syntax and information about the Stratego compiler. As it’s not clear whether the latter applies to the Spoofax Eclipse IDE, there are no further test cases in this repository beyond Section 6.2.
Chapter 7
The content and style of chapter 7 are similar to chapter 6. Again, there is a
trans/chap7.str
Stratego file defining several of the rules used in the examples
from the chapter. As before, the examples with a different signature are recast
into the SPL signature. Specifically, Z
becomes False
, S(x)
becomes
Impl(True(), x)
, and P
becomes Or
. (At least with these choices, all of the
transformations in the chapter remain valid in the usual semantics for the SPL
signature, even if a bit unorthodox.)
Chapter 8
This chapter is even more just a collection of interactive examples. In this repository we just give some examples of swapping and variable scope using the SPL signature, and some free-form examples of term wrapping and projecting. There’s no attempt to do any of the arithmetic examples because SPL does not have an integer type.
Chapter 9
This chapter is much more in the style of the early chapters: there are several
explicit Stratego modules, and we incorporate each one into this repository. As
before, we need to differentiate the names in some modules since they all end
up being loaded at once; but we also take advantage of the common sections of
some of the modules and re-use rather than repeat the code. In addition, the
disjunctive and conjunctive normal forms of the same test expression as used
in Chapter Five are used for test cases in trans/manual-suite.spt
.
Note that this repository does not include prop-dnf12
because at this
point the strategies there have become identical to the ones
in Section 6.2
used to introduce the idea of splitting
transformations into local rewrite rules applied via a traversal strategy. So
they have already been implemented and tested above.
On the other hand, in trans/sec9_1.str
, the repository does contain a
solution for the second exercise in
Section 9.1.2.
On the other hand there is no solution provided for the
exercise just before
Section 9.2.1.
The rest of Chapter 9 and all of the remaining chapters deal either with signatures other than SPL or just with abstract principles, so that concludes the Stratego examples provided in this repository.