Center

Page

Web

Wiki

# Understanding Strategies

Pt03
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.

1. Increment all integers in a term with 1
2. Increment all integers in a term with a constant
3. Replace all occurrences of G by H
4. Replace all outermost occurrences of G by H
5. 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.

1. Increment all integers in a term with 1
2. Increment all integers in a term with a constant
3. Replace all occurrences of G by H
4. Replace all outermost occurrences of G by H
5. 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.

1. Count the number of occurrences of G and A nodes
2. Compute the sum of all numbers in a tree
3. Collect all I subterms.
4. Collect all I subterms, but not those that are direct or indirect subterms of an H term.
5. Collect all F subterms.
6. Collect all outermost F subterms.
7. Collect all F subterms, but not those that are not direct or indirect subterms of an H term.

-- EelcoVisser - 20 Jan 2003