Assignment Generic Traversal Strategies
Pt04
This assignment consists of two parts. The first part consists of exercises for
understanding traversal strategies. The second is a transformation on Tiger programs
using strategies.
Understanding Traversal Strategies
The purpose of this assignment is to understand how traversal strategies work.
You should write modules strat-test.str and traversal-test.str
The modules should define a number of tests using the Stratego:StrategoUnit test framework.
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
Create 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.