Understanding Strategies
Pt04
The purpose of this assignment is to understand how strategies work. In directory strat/ of the progtrans-a package you should add or
extend the modules strat-test.str and traversal-test.str such that they are compiled and run when
make check is invoked.
The modules should define a number of tests using the Stratego:StrategoUnit test framework. An example is provided in module traversal-test.str which is already in the package. For each item in the lists below you should write a strategy definition and a few tests that demonstrate the working of the strategy.
The exercises should produce transformations on terms over the following signature:
signature
sorts Term
constructors
A : Term
B : Term
F : Term * Term -> Term
G : Term * Term -> Term
H : Term * Term -> Term
I : Int -> Term
However, your solutions should be as general as possible.
Composing Strategies
Create module
strat-test.str and write strategies for the following exercises.
Note that all traversals should be implemented using congruence operators.
- Increment all integers in a term with 1
- Increment all integers in a term with a constant
- Replace all occurrences of G by H
- Replace all outermost occurrences of G by H
- Replace all innermost occurrences of G by H
Generic Traversal
Extend module
traversal-test.str with strategies defining traversals according to the following descriptions.
Note however, that traversals should be as generic as allowed by the description. For instance, it should be possible to add or delete constructors if these are not directly mentioned in the description.
- Increment all integers in a term with 1
- Increment all integers in a term with a constant
- Replace all occurrences of G by H
- Replace all outermost occurrences of G by H
- Replace all innermost occurrences of G by H
Type Unifying Strategies
Further extend module
traversal-test.str with strategies for the following type-unifying transformations.
- Count the number of occurrences of G and A nodes
- Compute the sum of all numbers in a tree
- Collect all I subterms.
- Collect all I subterms, but not those that are direct or indirect subterms of an H term.
- Collect all F subterms.
- Collect all outermost F subterms.
- Collect all F subterms, but not those that are not direct or indirect subterms of an H term.
--
EelcoVisser - 20 Jan 2003