next up previous contents index
Next: Confluence and termination Up: Tree Rewriting Previous: Short circuiting rewrite

 

Conditional failure with failrewrite

From version 2.3.0 onward, the failrewrite statement may be used wherever a rewrite statement is expected. The failrewrite statement is an alternate way of writing conditional rewriting rules, and it is often useful when the condition to be checked is too complex to be written as a guard.

When a failrewrite statement is executed within the rhs action of a rewrite rule, the current pattern is rejected and the rewriter will proceed to try other matches.

For instance, suppose the current redex is DIV(INT 0,INT 0) and the following rules are specified within a rewriting system:

   DIV(INT X,INT Y): { if (Y == 0) failrewrite; else rewrite(NUM(X/Y); }
|  DIV(INT 0,Y):     NUM(0)

The execution of these rules will proceed as follows: both patterns match the redex, but the first rule will be tried first. Since Y is zero, the failrewrite statement is executed. The rewriter will proceed to try the second rule and the redex will be replaced with NUM(0). Note that if we omit the failrewrite statement in the first rule, as in:

   DIV(INT X,INT Y): { if (Y != 0) rewrite(NUM(X/Y); }
|  DIV(INT 0,Y):     NUM(0)
the rewriter will commit on the first rule and no action will be performed on the redex.

Like rewrite and cutrwerite, failrewrite acts as a branching statement and all statements immediatedly following failrewrite are dead code.



Allen Leung
Mon Apr 7 14:33:55 EDT 1997