95
Composing Transformations with Strategy Combinators CS4200 | Compiler Construction | November 12, 2020 Eelco Visser

Composing Transformations with Strategy Combinators

  • Upload
    others

  • View
    9

  • Download
    0

Embed Size (px)

Citation preview

Composing Transformations with Strategy Combinators

CS4200 | Compiler Construction | November 12, 2020

Eelco Visser

Rewriting = Matching & Building

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

Parameterized Rewrite Rules

20

f(x,y|a,b): lhs -> rhs- strategy or rule parameters x,y- term parameters a,b- no matching

f(|a,b): lhs -> rhs- optional strategy parameters

f(x,y): lhs -> rhs- optional term parameters

f: lhs -> rhs

Parameterized Rewrite Rules

Parameterized Rewrite Rules: Map

22

[ 1, 2, 3]

[ 2, 3, 4]

map(inc)

inc

1

2

3

2

3

4inc

inc

map(s): [] -> []map(s): [x|xs] -> [<s> x | <map(s)> xs]

Parameterized Rewrite Rules: Zip

23

[ 1, 2, 3]

[ (1,4), (2,5), (3,6)]

[ 4, 5, 6]

zip

[ 1, 2, 3]

[ 5, 7, 9]

[ 4, 5, 6]

zip(add)

map(add)

zip(s): ([],[]) -> []zip(s): ([x|xs],[y|ys]) -> [<s> (x,y) | <zip(s)> (xs,ys)]

zip = zip(id)

Parameterized Rewrite Rules: Fold

24

[1,2,3] foldr(!0,add) 6

[]

0

[3]

3

[2,3]

56

[1,2,3]

foldr(s1,s2): [] -> <s1>foldr(s1,s2): [x|xs] -> <s2> (x,<foldr(s1,s2)> xs)

Parameterized Rewrite Rules: Inverse

25

[1,2,3] inverse(|[]) [3,2,1]

[2,3]

[1][]

[1,2,3] [3]

[2,1] [3,2,1]

[]

inverse(|is): [] -> isinverse(|is): [x|xs] -> <inverse(|[x|is])> xs

Traversal Combinators

26

27

28

29

30

31

32

33

Traversal: Topdown

34

Const

Mul

Const

3 4 5

Const

Add1

switch: Add(e1, e2) -> Add(e2, e1)switch: Mul(e1, e2) -> Mul(e2, e1)

topdown(s) = s ; all(topdown(s))

topdown(switch)

Traversal: Topdown

35

Mul

Const

3

Const

4 5

Const

Add1

2

switch: Add(e1, e2) -> Add(e2, e1)switch: Mul(e1, e2) -> Mul(e2, e1)

topdown(s) = s ; all(topdown(s))

topdown(switch)

Traversal: Topdown

36

Mul

Const

3

Const

5 4

Const

Add1

2

3

switch: Add(e1, e2) -> Add(e2, e1)switch: Mul(e1, e2) -> Mul(e2, e1)

topdown(s) = s ; all(topdown(s))

topdown(switch)

Traversal: Topdown/Try

37

Const

Mul

Const

3 4 5

Const

Add1

switch: Add(e1, e2) -> Add(e2, e1)switch: Mul(e1, e2) -> Mul(e2, e1)

topdown(s) = s ; all(topdown(s))

topdown(try(switch))

Traversal: Topdown/Try

38

Mul

Const

3

Const

4 5

Const

Add1

2

switch: Add(e1, e2) -> Add(e2, e1)switch: Mul(e1, e2) -> Mul(e2, e1)

topdown(s) = s ; all(topdown(s))

topdown(try(switch))

Traversal: Topdown/Try

39

Mul

Const

3

Const

5 4

Const

Add1

2

3

4

5

6

7

8

switch: Add(e1, e2) -> Add(e2, e1)switch: Mul(e1, e2) -> Mul(e2, e1)

topdown(s) = s ; all(topdown(s))

topdown(try(switch))

Traversal: Alltd

40

Const

Mul

Const

3 4 5

Const

Add1

switch: Add(e1, e2) -> Add(e2, e1)switch: Mul(e1, e2) -> Mul(e2, e1)

alltd(s) = s <+ all(alltd(s))

alltd(switch)

Traversal: Alltd

41

Mul

Const

3

Const

4 5

Const

Add1

switch: Add(e1, e2) -> Add(e2, e1)switch: Mul(e1, e2) -> Mul(e2, e1)

alltd(s) = s <+ all(alltd(s))

alltd(switch)

Traversal: bottomup

42

Const

Mul

Const

3 4 5

Const

Add1

2

3

4

switch: Add(e1, e2) -> Add(e2, e1)switch: Mul(e1, e2) -> Mul(e2, e1)

bottomup(s) = all(bottomup(s)) ; s

bottomup(switch)

Traversal: Bottomup

43

Const

Mul

Const

3 4 5

Const

Add1

2

3

switch: Add(e1, e2) -> Add(e2, e1)switch: Mul(e1, e2) -> Mul(e2, e1)

bottomup(s) = all(bottomup(s)) ; s

bottomup(try(switch))

Traversal: Bottomup

44

Const

Mul

Const

3 4 5

Const

Add1

2

3

4

5

6

7

8

switch: Add(e1, e2) -> Add(e2, e1)switch: Mul(e1, e2) -> Mul(e2, e1)

bottomup(s) = all(bottomup(s)) ; s

bottomup(try(switch))

Traversal: Bottomup

45

Const

Mul

Const

3 4 5

Const

Add1

2

3

4

switch: Add(e1, e2) -> Add(e2, e1)switch: Mul(e1, e2) -> Mul(e2, e1)

bottomup(s) = all(bottomup(s)) ; s

bottomup(try(switch))

Traversal: Bottomup

46

Const

Mul

Const

3 5 4

Const

Add1

2

3

4

switch: Add(e1, e2) -> Add(e2, e1)switch: Mul(e1, e2) -> Mul(e2, e1)

bottomup(s) = all(bottomup(s)) ; s

bottomup(try(switch))

Traversal: Bottomup

47

Const

Mul

Const

3 5 4

Const

Add1

switch: Add(e1, e2) -> Add(e2, e1)switch: Mul(e1, e2) -> Mul(e2, e1)

bottomup(s) = all(bottomup(s)) ; s

bottomup(try(switch))

Traversal: Bottomup

48

Mul

Const

3

Const

5 4

Const

Add1

switch: Add(e1, e2) -> Add(e2, e1)switch: Mul(e1, e2) -> Mul(e2, e1)

bottomup(s) = all(bottomup(s)) ; s

bottomup(try(switch))

49

50

Traversal: Innermost

51

Const

Mul

Const

3 4 5

Const

Add1

2

3

4

5

6

7

8

switch: Add(e1, e2) -> Add(e2, e1)switch: Mul(e1, e2) -> Mul(e2, e1)

innermost(s) = bottomup(try(s ; innermost(s)))

innermost(switch)

Traversal: Innermost

52

Const

Mul

Const

3 4 5

Const

Add1

2

3

4

switch: Add(e1, e2) -> Add(e2, e1)switch: Mul(e1, e2) -> Mul(e2, e1)

innermost(s) = bottomup(try(s ; innermost(s)))

innermost(switch)

Traversal: Innermost

53

Const

Mul

Const

3 5 4

Const

Add1

2

3

4

9

10

11

12

switch: Add(e1, e2) -> Add(e2, e1)switch: Mul(e1, e2) -> Mul(e2, e1)

innermost(s) = bottomup(try(s ; innermost(s)))

innermost(switch)

Traversal: Innermost

54

Const

Mul

Const

3 5 4

Const

Add1

2

3

4

switch: Add(e1, e2) -> Add(e2, e1)switch: Mul(e1, e2) -> Mul(e2, e1)

innermost(s) = bottomup(try(s ; innermost(s)))

innermost(switch)

Traversal: Innermost

55

Const

Mul

Const

3 4 5

Const

Add1

2

3

4

switch: Add(e1, e2) -> Add(e2, e1)switch: Mul(e1, e2) -> Mul(e2, e1)

innermost(s) = bottomup(try(s ; innermost(s)))

innermost(switch)

56

57

58

59

60

61

Type-Unifying Transformations

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

Except where otherwise noted, this work is licensed under