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

Composing Transformations with Strategy Combinators

Feb 08, 2022

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Composing Transformations with Strategy Combinators

Composing Transformations with Strategy Combinators

CS4200 | Compiler Construction | November 12, 2020

Eelco Visser

Page 2: Composing Transformations with Strategy Combinators

Rewriting = Matching & Building

2

Page 3: Composing Transformations with Strategy Combinators

3

Page 4: Composing Transformations with Strategy Combinators

4

Page 5: Composing Transformations with Strategy Combinators

5

Page 6: Composing Transformations with Strategy Combinators

6

Page 7: Composing Transformations with Strategy Combinators

7

Page 8: Composing Transformations with Strategy Combinators

8

Page 9: Composing Transformations with Strategy Combinators

9

Page 10: Composing Transformations with Strategy Combinators

10

Page 11: Composing Transformations with Strategy Combinators

11

Page 12: Composing Transformations with Strategy Combinators

12

Page 13: Composing Transformations with Strategy Combinators

13

Page 14: Composing Transformations with Strategy Combinators

14

Page 15: Composing Transformations with Strategy Combinators

15

Page 16: Composing Transformations with Strategy Combinators

16

Page 17: Composing Transformations with Strategy Combinators

17

Page 18: Composing Transformations with Strategy Combinators

18

Page 19: Composing Transformations with Strategy Combinators

19

Page 20: Composing Transformations with Strategy Combinators

Parameterized Rewrite Rules

20

Page 21: Composing Transformations with Strategy Combinators

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

Page 22: Composing Transformations with Strategy Combinators

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]

Page 23: Composing Transformations with Strategy Combinators

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)

Page 24: Composing Transformations with Strategy Combinators

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)

Page 25: Composing Transformations with Strategy Combinators

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

Page 26: Composing Transformations with Strategy Combinators

Traversal Combinators

26

Page 27: Composing Transformations with Strategy Combinators

27

Page 28: Composing Transformations with Strategy Combinators

28

Page 29: Composing Transformations with Strategy Combinators

29

Page 30: Composing Transformations with Strategy Combinators

30

Page 31: Composing Transformations with Strategy Combinators

31

Page 32: Composing Transformations with Strategy Combinators

32

Page 33: Composing Transformations with Strategy Combinators

33

Page 34: Composing Transformations with Strategy Combinators

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)

Page 35: Composing Transformations with Strategy Combinators

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)

Page 36: Composing Transformations with Strategy Combinators

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)

Page 37: Composing Transformations with Strategy Combinators

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))

Page 38: Composing Transformations with Strategy Combinators

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))

Page 39: Composing Transformations with Strategy Combinators

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))

Page 40: Composing Transformations with Strategy Combinators

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)

Page 41: Composing Transformations with Strategy Combinators

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)

Page 42: Composing Transformations with Strategy Combinators

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)

Page 43: Composing Transformations with Strategy Combinators

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))

Page 44: Composing Transformations with Strategy Combinators

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))

Page 45: Composing Transformations with Strategy Combinators

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))

Page 46: Composing Transformations with Strategy Combinators

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))

Page 47: Composing Transformations with Strategy Combinators

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))

Page 48: Composing Transformations with Strategy Combinators

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))

Page 49: Composing Transformations with Strategy Combinators

49

Page 50: Composing Transformations with Strategy Combinators

50

Page 51: Composing Transformations with Strategy Combinators

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)

Page 52: Composing Transformations with Strategy Combinators

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)

Page 53: Composing Transformations with Strategy Combinators

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)

Page 54: Composing Transformations with Strategy Combinators

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)

Page 55: Composing Transformations with Strategy Combinators

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)

Page 56: Composing Transformations with Strategy Combinators

56

Page 57: Composing Transformations with Strategy Combinators

57

Page 58: Composing Transformations with Strategy Combinators

58

Page 59: Composing Transformations with Strategy Combinators

59

Page 60: Composing Transformations with Strategy Combinators

60

Page 61: Composing Transformations with Strategy Combinators

61

Page 62: Composing Transformations with Strategy Combinators

Type-Unifying Transformations

62

Page 63: Composing Transformations with Strategy Combinators

63

Page 64: Composing Transformations with Strategy Combinators

64

Page 65: Composing Transformations with Strategy Combinators

65

Page 66: Composing Transformations with Strategy Combinators

66

Page 67: Composing Transformations with Strategy Combinators

67

Page 68: Composing Transformations with Strategy Combinators

68

Page 69: Composing Transformations with Strategy Combinators

69

Page 70: Composing Transformations with Strategy Combinators

70

Page 71: Composing Transformations with Strategy Combinators

71

Page 72: Composing Transformations with Strategy Combinators

72

Page 73: Composing Transformations with Strategy Combinators

73

Page 74: Composing Transformations with Strategy Combinators

74

Page 75: Composing Transformations with Strategy Combinators

75

Page 76: Composing Transformations with Strategy Combinators

76

Page 77: Composing Transformations with Strategy Combinators

77

Page 78: Composing Transformations with Strategy Combinators

78

Page 79: Composing Transformations with Strategy Combinators

79

Page 80: Composing Transformations with Strategy Combinators

80

Page 81: Composing Transformations with Strategy Combinators

81

Page 82: Composing Transformations with Strategy Combinators

82

Page 83: Composing Transformations with Strategy Combinators

83

Page 84: Composing Transformations with Strategy Combinators

84

Page 85: Composing Transformations with Strategy Combinators

85

Page 86: Composing Transformations with Strategy Combinators

86

Page 87: Composing Transformations with Strategy Combinators

87

Page 88: Composing Transformations with Strategy Combinators

88

Page 89: Composing Transformations with Strategy Combinators

89

Page 90: Composing Transformations with Strategy Combinators

90

Page 91: Composing Transformations with Strategy Combinators

91

Page 92: Composing Transformations with Strategy Combinators

92

Page 93: Composing Transformations with Strategy Combinators

93

Page 94: Composing Transformations with Strategy Combinators

94

Page 95: Composing Transformations with Strategy Combinators

95

Except where otherwise noted, this work is licensed under