forked from klauso/PL1-2014
-
Notifications
You must be signed in to change notification settings - Fork 0
/
02-pattern-matching.scala
252 lines (226 loc) · 7.06 KB
/
02-pattern-matching.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Notes from the 2nd exercise session of the course
// "Programming Languages I" at Tübingen University.
// Winter term 2014/2015.
//
// Alternatives to Pattern Matching
// ================================
//
// Last week, we have seen this code with pattern matching:
object UsingPatternMatching {
// Abstract Syntax Tree
trait Exp
case class Num(n: Int) extends Exp
case class Add(lhs: Exp, rhs: Exp) extends Exp
// Example
val onePlusEight = Add(Num(1), Add(Num(5), Num(3)))
// Interpreter
def eval(e: Exp): Int =
e match {
case Num(n) => n
case Add(l, r) => eval(l) + eval(r)
}
// Test
def test {
print(eval(onePlusEight))
}
}
// Here are some of the ways to write this code without pattern
// matching.
//
// Using isInstanceOf and asInstanceOf
// -----------------------------------
//
// Instead of pattern matching, we could also use isInstanceOf
// and asInstanceOf to distinguish Num and Add nodes in the
// abstract syntax tree. In fact, the Scala compiler will desugar
// the code that uses pattern matching into something similar to
// the code below:
object UsingInstanceOf {
// Abstract Syntax Tree
trait Exp
case class Num(n: Int) extends Exp
case class Add(lhs: Exp, rhs: Exp) extends Exp
// Example
val onePlusEight = Add(Num(1), Add(Num(5), Num(3)))
// Interpreter
def eval(e: Exp): Int =
if (e.isInstanceOf[Num])
e.asInstanceOf[Num].n
else if (e.isInstanceOf[Add])
eval(e.asInstanceOf[Add].lhs) + eval(e.asInstanceOf[Add].rhs)
else
sys.error("unknown expression")
def test {
print(eval(onePlusEight))
}
}
// In Scala, you should never write such code by hand. Instead, use
// pattern matching which gets desugared to something similar
// anyway.
//
// Using Dynamic Dispatch
// ----------------------
//
// Here is a solution that uses dynamic dispatch:
object UsingDynamicDispatch {
// Abstract Syntax Tree
trait Exp {
def eval: Int
}
case class Num(n: Int) extends Exp {
def eval: Int = n
}
case class Add(lhs: Exp, rhs: Exp) extends Exp {
def eval: Int = lhs.eval + rhs.eval
}
// Example
val onePlusEight = Add(Num(1), Add(Num(5), Num(3)))
// Test
def test {
print(onePlusEight.eval)
}
}
// When we talked about this code, we have seen a diagram for the
// Composite pattern on the blackboard:
//
// +-------+
// | Exp | 2
// +-------+ <------+
// | eval | |
// | ... |
// +-------+ |
// ^ |
// /_\ |
// | |
// +----+----+ |
// | | |
// +-------+ +-------+ |
// | Num | | Add | |
// +-------+ +-------+ --+
// | eval | | eval |
// | ... | | ... |
// +-------+ +-------+
//
// Note: If we want to add more operations beside eval, we have
// to add them to all of the classes.
//
// In this lecture, we will work a lot with abstract syntax
// trees, so Composite-like structures will be important.
// Using the Visitor Pattern
// -------------------------
//
// And finally a solution that uses visitors:
object UsingVisitors {
// Visitor interface
// (one method per case class!)
trait Visitor[Value] {
def num(n: Int): Value
def add(lhs: Value, rhs: Value): Value
}
// Abstract Syntax Tree & Folding the Visitor
trait Exp {
def foldExp[Value](v: Visitor[Value]): Value
}
case class Num(n: Int) extends Exp {
def foldExp[Value](v: Visitor[Value]): Value =
v.num(n)
}
case class Add(lhs: Exp, rhs: Exp) extends Exp {
def foldExp[Value](v: Visitor[Value]): Value =
v.add(lhs.foldExp(v), rhs.foldExp(v))
}
// Evaluation
object Eval extends Visitor[Int] {
def num(n: Int): Int = n
def add(lhs: Int, rhs: Int): Int = lhs + rhs
}
// Example
val onePlusEight = Add(Num(1), Add(Num(5), Num(3)))
// Test
def test {
print(onePlusEight.foldExp(Eval))
}
}
// Compare this with the visitor code at the end of
// 03-ae.scala. There:
//
// - a visitor is a case class that stores functions
// - concrete visitors are values of that case class
// - foldExp is implemented with pattern matching
//
// Here:
//
// - a visitor is an interface with abstract methods
// - concrete visitors are instances of that interface
// - foldExp is implemented with dynamic dispatch
//
// These details differ, but both versions are implementations of
// the visitor pattern.
//
// When we talked about this code, we have seen a diagram for the
// Visitor pattern on the blackboard:
//
// +---------------+ +-----------+
// | Exp | 2 | Visitor |
// +---------------+ <---------+ +-----------+
// | fold(Visitor) | | | num |
// +---------------+ | | add |
// ^ | +-----------+
// /_\ | ^
// | | /_\
// +--------+-------+ | |
// | | | +----+----+
// +---------------+ +---------------+ | | |
// | Num | | Add | | +-------+ +-------+
// +---------------+ +---------------+ --+ | Eval | | ... |
// | fold(Visitor) | | fold(Visitor) | +-------+ +-------+
// +---------------+ +---------------+ | num | | num |
// | add | | add |
// +-------+ +-------+
//
// Note that if we want to add more operations beside Eval, we
// can just add more classes. We don't have to change the
// existing code. This is one of the benefits of the Visitor
// pattern.
//
// For the examples in this lecture, we'll mostly just use
// pattern matching. When you implement a language in the context
// of a bigger sotware project, you might want to look into the
// Visitor pattern and other approaches again.
//
// HOMEWORK ASSIGNMENT
// ===================
// HOMEWORK
//
// Email homework as Scala source file to:
//
//
// Work in groups of 1 or 2 students. Send the email CC to the
// other student in your team.
//
// Put "pl1-hw02" in subject, please
//
// 0. write in the email:
// - your names
// - your student ids ("Matrikelnummer")
// 1. implement a pretty printer for the language with Add and Num.
//
// The pretty printer should turn an Exp into a String. It should
// only add parentheses where necessary.
//
// Examples:
//
// Num(1) ==> "1"
// Add(Num(1), Num(2)) ==> "1 + 2"
// Add(Num(1), Add(Num(2), Num(3))) ==> "1 + (2 + 3)"
// Add(Add(Num(1), Num(2)), Num(3)) ==> "1 + 2 + 3"
//
// No need for parentheses to the left of Add, because by
// default, Add is executed left-to-right.
//
// If you want, you can experiment with implementing the pretty
// printer with pattern matching, with dynamic dispatch, and as a
// visitor.
//
// Send question by email to [email protected]