Term Rewriting with Traversal Functions

Report 4 Downloads 152 Views
Thionville

1

Term Rewriting with Traversal Functions Mark van den Brand Paul Klint Jurgen Vinju April 18th, 2001

CWI

Amsterdam

Thionville

2

Overview • Term Rewriting Problems • Solutions • Traversal functions • Limitations • Conclusion & Questions

CWI

Amsterdam

Thionville

3

Term Rewriting Problems

CWI

Amsterdam

Thionville

3-a

Term Rewriting Problems • Term rewriting is expressive, but...

CWI

Amsterdam

Thionville

3-b

Term Rewriting Problems • Term rewriting is expressive, but... • Term traversal is implicit (e.g. innermost) only sort preserving transformations no extra arguments (i.e. context) fixed path

CWI

Amsterdam

Thionville

3-c

Term Rewriting Problems • Term rewriting is expressive, but... • Term traversal is implicit (e.g. innermost) only sort preserving transformations no extra arguments (i.e. context) fixed path • Confluence non-trivial trs’s are not confluent

CWI

Amsterdam

Thionville

4

Solutions

CWI

Amsterdam

Thionville

4-a

Solutions • Encode strategies using functional style

CWI

ASF+SDF

Amsterdam

Thionville

4-b

Solutions • Encode strategies using functional style • Strategy primitives

CWI

ASF+SDF ELAN

Amsterdam

Thionville

4-c

Solutions • Encode strategies using functional style • Strategy primitives • Strategy & Traversal primitives

CWI

ASF+SDF ELAN Stratego

Amsterdam

Thionville

4-d

Solutions • Encode strategies using functional style • Strategy primitives • Strategy & Traversal primitives

ASF+SDF ELAN Stratego

• Very nice! But:

CWI

Amsterdam

Thionville

4-e

Solutions • Encode strategies using functional style • Strategy primitives • Strategy & Traversal primitives

ASF+SDF ELAN Stratego

• Very nice! But: Encoding is boring for large systems

CWI

Amsterdam

Thionville

4-f

Solutions • Encode strategies using functional style • Strategy primitives • Strategy & Traversal primitives

ASF+SDF ELAN Stratego

• Very nice! But: Encoding is boring for large systems ELAN Strategies do not solve term traversal

CWI

Amsterdam

Thionville

4-g

Solutions • Encode strategies using functional style • Strategy primitives • Strategy & Traversal primitives

ASF+SDF ELAN Stratego

• Very nice! But: Encoding is boring for large systems ELAN Strategies do not solve term traversal Stratego traversal primitives are not typed

CWI

Amsterdam

Thionville

4-h

Solutions • Encode strategies using functional style • Strategy primitives • Strategy & Traversal primitives

ASF+SDF ELAN Stratego

• Very nice! But: Encoding is boring for large systems ELAN Strategies do not solve term traversal Stratego traversal primitives are not typed Strategic Programming is another paradigm

CWI

Amsterdam

Thionville

4-i

Solutions • Encode strategies using functional style • Strategy primitives • Strategy & Traversal primitives

ASF+SDF ELAN Stratego

• Very nice! But: Encoding is boring for large systems ELAN Strategies do not solve term traversal Stratego traversal primitives are not typed Strategic Programming is another paradigm • Traversal functions! less powerful fully typed simple CWI

Amsterdam

Thionville

5

Traversal functions

CWI

Amsterdam

Thionville

5-a

Traversal functions • Characteristics:

CWI

Amsterdam

Thionville

5-b

Traversal functions • Characteristics: typed prefix functions

CWI

Amsterdam

Thionville

5-c

Traversal functions • Characteristics: typed prefix functions three generic types: trafo, accu or both

CWI

Amsterdam

Thionville

5-d

Traversal functions • Characteristics: typed prefix functions three generic types: trafo, accu or both two generic strategies: bottom-up (all), top-down (stop)

CWI

Amsterdam

Thionville

5-e

Traversal functions • Characteristics: typed prefix functions three generic types: trafo, accu or both two generic strategies: bottom-up (all), top-down (stop) simple semantics, easy implementation

CWI

Amsterdam

Thionville

5-f

Traversal functions • Characteristics: typed prefix functions three generic types: trafo, accu or both two generic strategies: bottom-up (all), top-down (stop) simple semantics, easy implementation • The questions are:

CWI

Amsterdam

Thionville

5-g

Traversal functions • Characteristics: typed prefix functions three generic types: trafo, accu or both two generic strategies: bottom-up (all), top-down (stop) simple semantics, easy implementation • The questions are: What can we do with them?

CWI

Amsterdam

Thionville

5-h

Traversal functions • Characteristics: typed prefix functions three generic types: trafo, accu or both two generic strategies: bottom-up (all), top-down (stop) simple semantics, easy implementation • The questions are: What can we do with them? What can we not do with them?

CWI

Amsterdam

Thionville

6

PICO module Pico-syntax imports Pico-Identifiers Pico-Integers Pico-Strings Types exports sorts PROGRAM context-free syntax "begin" DECLS {STATEMENT ";"}* "end" -> PROGRAM "declare" {ID-TYPE ","}* ";" -> DECLS PICO-ID ":" TYPE -> ID-TYPE PICO-ID ":=" EXP -> STATEMENT "if" EXP "then" {STATEMENT ";"}* "else" {STATEMENT ";"}* "fi" -> STATEMENT "while" EXP "do" {STATEMENT ";"}* "od" -> STATEMENT PICO-ID -> EXP PICO-NAT-CON -> EXP PICO-STR-CON -> EXP EXP "+" EXP -> EXP {left} EXP "-" EXP -> EXP {left} EXP "*" EXP -> EXP {left}

Figure 1:

CWI

Amsterdam

Thionville

7

Transformer module Pico-Replace imports Pico-syntax exports context-free syntax replace(PROGRAM,ID,ID) -> PROGRAM {traversal(trafo)} equations [] replace(Id1,Id1,Id2) = Id2 Figure 2:

CWI

Amsterdam

Thionville

8

Transformer (cont’d) replace( begin declare input : natural, output : natural; input := 12; output := 1; while input - 1 do output := output * 2; input := input - 1 od end, input, INPUT) Figure 3: CWI

Amsterdam

Thionville

9

Transformer (cont’d) begin declare INPUT : natural, output : natural; INPUT := 12; output := 1; while INPUT - 1 do output := output * 2; INPUT := INPUT - 1 od end Figure 4:

CWI

Amsterdam

Thionville

10

Accumulator module Pico-Count imports Pico-syntax exports context-free syntax count(PROGRAM,NAT) -> NAT {traversal(accu)} equations [] count(Id,Nat) = suc(Nat) Figure 5:

CWI

Amsterdam

Thionville

11

Accumulating Transformer module Pico-ReplaceCount imports Pico-syntax exports context-free syntax replacecount(PROGRAM,NAT,ID,ID) -> PROGRAM # NAT {traversal(accu,trafo)} equations [] replacecount(Id1,Nat,Id1,Id2) = (Id2,suc(Nat)) Figure 6:

CWI

Amsterdam

Thionville

12

Experience • COBOL transformations • CASL transformations • SDF tools: typechecker, statistics • New DSL: Framework for SDF Transformation (FST)

CWI

Amsterdam

Thionville

12-a

Experience • COBOL transformations • CASL transformations • SDF tools: typechecker, statistics • New DSL: Framework for SDF Transformation (FST) • Significant reduction in number of rules • Interpreter efficient enough

CWI

Amsterdam

Thionville

13

Limitations

CWI

Amsterdam

Thionville

13-a

Limitations 1. Types: no arbitrary non-sort-preserving traversals

CWI

Amsterdam

Thionville

13-b

Limitations 1. Types: no arbitrary non-sort-preserving traversals 2. Traversals: no arbitrary traversal paths through trees

CWI

Amsterdam

Thionville

13-c

Limitations 1. Types: no arbitrary non-sort-preserving traversals 2. Traversals: no arbitrary traversal paths through trees 3. Strategies: still no control over application of rewrite rules

CWI

Amsterdam

Thionville

13-d

Limitations 1. Types: no arbitrary non-sort-preserving traversals 2. Traversals: no arbitrary traversal paths through trees 3. Strategies: still no control over application of rewrite rules 1. Use traversal functions for the unaffected nodes

CWI

Amsterdam

Thionville

13-e

Limitations 1. Types: no arbitrary non-sort-preserving traversals 2. Traversals: no arbitrary traversal paths through trees 3. Strategies: still no control over application of rewrite rules 1. Use traversal functions for the unaffected nodes 2. Use conditions and default rules to guide the path

CWI

Amsterdam

Thionville

13-f

Limitations 1. Types: no arbitrary non-sort-preserving traversals 2. Traversals: no arbitrary traversal paths through trees 3. Strategies: still no control over application of rewrite rules 1. Use traversal functions for the unaffected nodes 2. Use conditions and default rules to guide the path 3. Still encode strategies using functions

CWI

Amsterdam

Thionville

14

Conclusions

CWI

Amsterdam

Thionville

14-a

Conclusions • Automated tree traversal

CWI

Amsterdam

Thionville

14-b

Conclusions • Automated tree traversal • Statically typed

CWI

Amsterdam

Thionville

14-c

Conclusions • Automated tree traversal • Statically typed • Simple semantics, first-order term rewriting

CWI

Amsterdam

Thionville

14-d

Conclusions • Automated tree traversal • Statically typed • Simple semantics, first-order term rewriting • It works (various applications)

CWI

Amsterdam

Thionville

14-e

Conclusions • Automated tree traversal • Statically typed • Simple semantics, first-order term rewriting • It works (various applications) • Future work: Efficient compilation

CWI

Amsterdam

Thionville

15

Questions

CWI

Amsterdam