A survey of rewriting strategies in program transformation systems l.jpg
This presentation is the property of its rightful owner.
Sponsored Links
1 / 39

A Survey of Rewriting Strategies in Program Transformation Systems PowerPoint PPT Presentation


  • 145 Views
  • Uploaded on
  • Presentation posted in: General

A Survey of Rewriting Strategies in Program Transformation Systems. Part II Author: Eelco Visser Speaker: Shih-hsi Liu. Section 5. Program Transformation Paradigms. Outline. Interactive Program Transformation Syntactic Abstractions in Intentional Programming Simple Tree Parsing

Download Presentation

A Survey of Rewriting Strategies in Program Transformation Systems

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


A survey of rewriting strategies in program transformation systems l.jpg

A Survey of Rewriting Strategies in Program Transformation Systems

Part II

Author: Eelco Visser

Speaker: Shih-hsi Liu


Section 5 l.jpg

Section 5

  • Program Transformation Paradigms


Outline l.jpg

Outline

  • Interactive Program Transformation

  • Syntactic Abstractions in Intentional Programming

  • Simple Tree Parsing

  • Tree Parsing with Dynamic Programming

  • Term Rewriting

  • Term Rewriting with Strategy Annotations

  • Functional Rewriting

  • Rewriting with Traversal Functions

  • Controlling Rewriting by Reflection

  • Sequences of Canonical Forms

  • Non-deterministic Sequential Strategies

  • Generic Traversal Strategies


Interactive program transformation draco l.jpg

Interactive Program Transformation - Draco

  • First support the transformation of high-level domain specific programs to executable code

  • Transformation rules for optimizations and refinement

  • Application controlled by user via an interactive process.

  • User select domain, instance (region in AST) and locale (node in AST)

  • APPLY, SUGGEST, TRANSFORM


Syntactic abstractions in intentional programming l.jpg

Syntactic Abstractions in Intentional Programming

  • Program represented by source tree (source graph)

  • User enter, modify, and compiler source code directly (no need to work on program code stored as text format)

  • Reduction: transforming source tree into lower-level tree

  • R-code intention: basic construct translated to some form of machine code by code generator

  • The order of reductions is implicit -> not a general program transformation system

  • Treated as a tool for defining and eliminating new syntactic abstractions

  • E.g. while (x<5) TEST : if (x<5)

    ++x { ++x

    goto TEST

    }


Simple tree parsing sorcerer l.jpg

Simple Tree Parsing – SORCERER

  • Tree parser generator for the ANTLR language processing system

  • Generates top-down tree parser that may execute action at any point during a tree walk directing with syntactic and semantic context information (predicate)

  • Work on intermediate representation (IR) expression tree

  • Goal: find structure in a tree by covering the tree with pattern (top-down manner via LL(1)-based parsing strategy)

  • Transformation rule embedded in grammar productions


Simple tree parsing sorcerer 2 l.jpg

Simple Tree Parsing – SORCERER (2)

  • E.g. exp: # (PLUS exp exp) | INT //tree pattern

  • Tree translation to text output:

    exp: # (PLUS exp <<printf(“+”);>> exp)

    | i: INT <<printf(“”%d”,i);>>

  • Tree transformation: by reconstructing trees and returning them as result

    exp: !# (PLUS l:exp r:exp) //! is semantic predicate

    << #exp = #(PLUS r l);>>

    | INT


Simple tree parsing sorcerer 3 l.jpg

Simple Tree Parsing – SORCERER (3)

  • Semantic predicate (test structure)

    exp: <<root->down!=NULL && root->down->right!=NULL>>?

    #(MINUS exp exp) // Binary exp (subtract)

    | #(MINUS exp); //Unary exp (negate)

  • Syntactic predicate

    exp: (#MINUS ..))? #(MINUS exp exp)

    | #(MINUS exp);


Tree parsing with dynamic programming burg l.jpg

Tree Parsing with Dynamic Programming - BURG

  • BURG: program that generates a fast tree parser using BURS (Bottom-Up Rewrite System) technology

  • Use dynamic programming to compute all possible parses in one traversal when a tree grammar is ambiguous

  • Goal: Find the lowest cost covering of IR expression tree

  • Two traversals of the tree :

    - one bottom-up traversal to label each node with a state that encode all optimal match (matching RHS to tree pattern)

    - second top-down traversal that uses the states to select and emit code (select machine instructions driven by the goal nonterminal for the root of the tree


Tree parsing with dynamic programming burg 2 l.jpg

Tree Parsing with Dynamic Programming – BURG (2)

  • Grammar : set of rules

  • Rule: n->t (c)

  • LHS: n=nonterminal

  • RHS: t=tree pattern

  • C: Cost

  • Find least-cost parse by DP


Tree parsing with dynamic programming burg 3 l.jpg

Tree Parsing with Dynamic Programming – BURG (3)

  • BURG specification

    #1 goal -> reg (0) #5 reg ->Plus(reg,reg) (2)

    #2 reg -> Reg (0) #6 addr -> reg (0)

    #3 reg -> Int (1) #7 addr -> Int (0)

    #4 reg -> Fetch(addr) (2) #8 addr -> Plus(reg,Int) (0)

  • E.g. Fetch(Fetch(Plus(Reg,Int)))

  • 4(4(6(5(2,3)))) => cost =7

  • 4(4(8(2))) =>cost=4


Term rewriting l.jpg

Term Rewriting

  • Supported by OBJ, ASF+SDF, ELAN etc.

  • Rewrite rule: t1->t2 (transformation of a term matching pattern t1 to the instantiation of t2)

  • Use De Morgan rule and propositional logic rule

  • Rewrite engine employ different strategies:

    - innermost: all subterms of a term are normalized before rules are applied to term itself

    - outermost: subterms closest to the root of term are rewritten first


Term rewriting 2 l.jpg

Term Rewriting (2)

  • Complete normalization is not adequate for program transformation

  • Rewrite system for programming languages will often be non-terminating and non-confluent

  • (Section 4.2 transformation strategies:

    Confluent and terminating => a unique normal form for every program)

  • e.g: DAOL+DAOR  DOAL+DOAR


Term rewriting 3 l.jpg

Term Rewriting (3)

Signature

sorts Prop

constructors

False : Prop

True : Prop

Atom : String->Prop

Not : Prop -> Prop

And : Prop * Prop -> Prop

Or : Prop * Prop -> Prop

(continued)


Term rewriting 4 l.jpg

Term Rewriting (4)

rules

DAOL : And (Or(x,y),z) -> Or(And(x,z),And(y,z))

DAOR : And(z,Or(x,y)) -> Or(And(z,x),And(z,y))

DOAL : Or(And(x,y),z)) -> And(Or(x,z),Or(y,z))

DOAR : Or(z,And(x,y)) -> And(Or(z,x),Or(z,y))

DN : Not(Not(x)) ->x

DMA : Not(And(x,y)) -> Or(Not(x),Not(y))

DMO : Not(Or(x,y)) -> And(Not(x),Not(y))


Term rewriting with strategy annotations 1 l.jpg

Term Rewriting with Strategy Annotations (1)

  • Problem of Term Rewriting: term with infinite reduction paths cannot be resolved by removing unnecessary rules

  • E.g. pure innermost rewriting strategy a term Fac(3)

    rules

    Fac : Fac(x) -> If (Eq(x,0),1,Mul(x,Fac(Sub(x,1)))

    IfT : If (True,x,y) -> x

    IfF : If (False,x,y) ->y

    IfE : If (p,x,x) ->x

  • Fac(3) never terminates, since IfT, IfF always evaluate after arguments of If (never know the results of Eq(x,0)


Term rewriting with strategy annotations 2 just in time l.jpg

Term Rewriting with Strategy Annotations (2) – Just-in-time

  • delay the evaluation of arguments, but guarantee the term reached after evaluation is a normal form with respect to the rewrite system

  • E.g. 1 innermost strategy:

    strat(c) = [1,2,3,…,R1,R2,R3]

    All its arguments should be evaluated first anf then the rules Ri

  • E.g. 2 Solution for Term Rewriting

    stract(If) = [1,IfT,IfF,2,3,IfE]

    Only the first argument should be evaluated before IfT and IfF

  • a permutation of argument positions and rules in which rules are applied as early as possible


Term rewriting with strategy annotations 3 e strategy l.jpg

Term Rewriting with Strategy Annotations (3) – E-Strategy

  • Problem of Just-in-time: cannot handle non-normal forms for some terms (e.g. Inf(n) for some n)

  • Solution: not all arguments need to be evaluated

  • Strategy annotation: list of argument positions and root position (0)

    - annotation declares the order of evaluations of term at root

    - Root position 0 indicates evaluation of term at root

  • E.g. strat(1 0): evaluate first argument of Cons and then Cons itself (root)


Term rewriting with strategy annotations 4 laziness l.jpg

Term Rewriting with Strategy Annotations (4) – Laziness

  • No reduction should be performed for subterms of that argument, unless needed for matching.

  • E.g. Lazy(Cons,2) delays the second argument evaluation


Functional rewriting l.jpg

Functional Rewriting

  • Solution to problem of control over application of rewrite rules: introduce additional constructors (functions) that achieve normalization under a restricted set of rules

  • Use Disjunctive normal form(DNF)

  • Innermost normalization strategy: recursively traverse terms


Functional rewriting 2 l.jpg

Functional Rewriting (2)

  • Cons:

    - functional programming style of rewriting

    - overhead in the form of traversal rules for each constructor in the signature, intertwining of rules and function definitions

    => make reuse of rules impossible

    => leads to specifications harder to understand


Functional rewriting 3 l.jpg

Functional Rewriting (3)

rules

DNF1 : dnf(True) -> True

DNF2 : dnf(False) -> False

DNF3 : dnf(Atom(x) -> Atom(x)

DNF4 : dnf(Not(x)) -> not(dnf(x))

DNF5 : dnf(And(x,y)) -> and(dnf(x),dnf(y))

DNF6 : dnf(Or(x,y)) -> Or(dnf(x),dnf(y))

AND1 : and(Or(x,y),z) -> Or(and(x,z),and(y,z))

AND2 : and(z,Or(x,y)) -> Or(and(z,x),and(z,y))

AND3 : and(x,y) -> And(x,y) (default)

NOT1 : not(Not(x)) -> x

NOT2 : not(And(x,y)) -> Or(not(x),not(y))

NOT3 : not(Or(x,y)) -> and(not(x),not(y))

NOT4 : not(x) -> Not(x) (default)


Continue l.jpg

Continue …

Speaker: Fei Cao


5 8 rewriting with traversal functions l.jpg

5.8 Rewriting with Traversal Functions

  • Traversal Rules vs. Normal Rules

    • Overhead

    • Requires new generator

  • Traversal Function

    • Run-time support by rewriting engine

    • Still overhead with smart constructor is used


Rewriting with traversal functions in asf sdf l.jpg

Rewriting with Traversal Functions in ASF+SDF

  • Strategy

    • Top-down

    • Bottom-up

  • Transformation vs. Accumulation

  • Parameterization


Slide26 l.jpg

signature

constructors

dnf : Prop -> Prop {traversal(trafo,bottom-up)}

and : Prop * Prop -> Prop

not : Prop -> Prop

rules

DNF4 : dnf(Not(x)) -> not(x)

DNF5 : dnf(And(x,y)) -> and(x,y)

AND1 : and(Or(x,y),z) -> Or(and(x,z),and(y,z))

AND2 : and(z,Or(x,y)) -> Or(and(z,x),and(z,y))

AND3 : and(x,y) -> And(x,y) (default)

NOT1 : not(Not(x)) -> x

NOT2 : not(And(x,y)) -> Or(not(x),not(y))

NOT3 : not(Or(x,y)) -> and(not(x),not(y))

NOT4 : not(x) -> Not(x) (default)

Figure 11: Disjunctive Normal Form with traversal function (Version 1)


Slide27 l.jpg

signature

constructors

dnf : Prop -> Prop {traversal(trafo,bottom-up)}

rules

AND1 : dnf(And(Or(x,y),z) -> dnf(Or(And(x,z)),And(y,z))

AND2 : dnf(And(z,Or(x,y)) -> dnf(Or(And(z,x)),And(z,y))

NOT1 : dnf(Not(Not(x)) -> x

NOT2 : dnf(Not(And(x,y)) -> dnf(Or(Not(x),Not(y)))

NOT3 : dnf(Not(Or(x,y)) -> dnf(And(Not(x),Not(y)))

Figure 12: Disjunctive Normal Form with traversal function (Version 2)


Rewriting with traversal functions l.jpg

Rewriting with Traversal Functions

  • Limitations

    • No separation of rules from strategies

      • Rules un-reusable

      • Even hard to distinguish the two things

    • Limited Range of Traversal

      • Top-down traversal stops when rule applies

    • Abstraction restricted


5 9 controlling rewriting by reflection l.jpg

5.9 Controlling Rewriting by Reflection

  • Using Reflection to support specification generation

  • Maude


5 10 sequences of canonical forms l.jpg

5.10 Sequences of Canonical Forms

  • TAMPR

    • Numerical domain

    • Stepwise

      • Pre-processing

      • Exhaustively apply rewrite rules

      • Post-processing


Slide31 l.jpg

(x2 + 2x + 1)y2 + (x2 - 9)y - (20x2 + 18x - 18)

sum-of-monomonials;

x-commuted-to-right;

like-powers-collected;

x-factored-out

(y2 + y - 20)x2 + (2y2 - 18)x + (y2 - 9y + 18)


5 11 non deterministic sequential strategies l.jpg

5.11 Non-Deterministic Sequential Strategies

  • ELAN

    • User-definable Strategy

      • Unlabeled rewrite rules—fixed innermost strategy

      • Labeled rules-- user defined strategy

    • Strategy Operator

      • id

      • fail

      • dk, dc

      • first

      • iterate, repeat

      • normalize


Slide33 l.jpg

[Delete] (E U {s=s} ; R) => (E ; R) end

[Compose] (E ; R U {s->t}) => (E ; R U {s->u}) if reduce(t->u) end

[Simplify] (E U {s=t} ; R) => (E U {s=u} ; R) if reduce(t->u) end

[Orient] (E U {s=t) ; R) => (E ; R U {s->t}) if s > t end

[Collapse] (E ; R U {s->t}) => (E U {u=t} ; R) if reduce(s->u) end

[Deduce] (E ; R) => (E U {s=t} ; R) if s=t in CP(R) end

completion =>

repeat*(repeat*(repeat*(Collapse);

repeat*(Compose) ;

repeat*(Simplify) ;

repeat*(Delete) ;

repeat*(Orient)) ;

Deduce)


5 12 generic traversal strategies l.jpg

5.12 Generic Traversal Strategies

  • Stratego

    • Generic traversal strategies based on one-level traversal operators

  • Sequential Programming

    • identity

    • failure

    • non-determinitic

    • deterministic

    • recursive closure

    • test

    • negation


Slide35 l.jpg

strategies

disj-nf =

innermost(DAOL + DAOR + DN + DMA + DMO)

conj-nf =

innermost(DOAL + DOAR + DN + DMA + DMO)

T =

T1 + T2 + T3 + T4 + T5 + T6 + T7 + T8 + T9 + T10 +

T11 + T12 + T13 + T14 + T15 + T16 + T17 + T18 + T19

eval =

bottomup(repeat(T))

desugar =

topdown(try(DefI + DefE))

impl-nf =

topdown(repeat(DefN + DefA2 + DefO1 + DefE))

Figure 14: Various transformations on propositional formulae.


Slide36 l.jpg

strategies

try(s) = s <+ id

repeat(s) = rec x(try(s; x))

while(c, s) = rec x(try(c; s; x))

do-while(s, c) = rec x(s; try(c; x))

while-not(c, s) = rec x(c <+ s; x)

for(i, c, s) = i; while-not(c, s)

Figure 15: Generic iteration strategies.


Generic traversal strategies l.jpg

Generic Traversal Strategies

  • Term Traversal

    • Congruent operator

      • C(t1,…,tn) C(s1,,,sn)

  • Match, Building and Variable Binding

    • Match: ?t binding variable to the subject term

    • Build: !t replace the bounded subject term with a new bunded term in t.

      L = {x1,...,xn: ?l; where(s); !r}


Generic traversal strategies38 l.jpg

Generic Traversal Strategies

  • Generic Strategies

    • Strategy library

    • Language independent , parameterizable operations

    • overhead


Scoped dynamic rewrite rules l.jpg

Scoped Dynamic Rewrite Rules

  • The issue:

    • Rule is context-free but transformation problems are often context sensitive

  • Solution

    • Extend traversal so as to provide context data to transformation rule

      • Accumulation in ASF+SDF traversal function

      • Cons: control flow tangled with data

    • Use contextual rules

    • Scoped dynamic rewrite rules

      • Under control of a normal strategy

      • Range of rule controled by scope


  • Login