-
Notifications
You must be signed in to change notification settings - Fork 18
/
13-Interpreter-v1.fsx
373 lines (311 loc) · 12.2 KB
/
13-Interpreter-v1.fsx
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
(* ======================================
13-Interpreter-v1.fsx
Part of "Thirteen ways of looking at a turtle"
Related blog post: http://fsharpforfunandprofit.com/posts/13-ways-of-looking-at-a-turtle/
======================================
Way #13: The interpreter pattern
In this design, the client builds a data structure (`TurtleProgram`) that represents the instructions.
This Turtle Program can then interpreted later in various ways
====================================== *)
#load "Common.fsx"
#load "FPTurtleLib2.fsx"
open Common
open FPTurtleLib2
// ============================================================================
// Turtle Program V0 -- doesn't work as command and response are not linked
// ============================================================================
module TurtleProgram_v0 =
open Turtle
// we send this to the turtle...
type TurtleCommand =
| Move of Distance
| Turn of Angle
| PenUp
| PenDown
| SetColor of PenColor
// ... and the turtle replies with one of these
type TurtleResponse =
| Moved of MoveResponse
| Turned
| PenWentUp
| PenWentDown
| ColorSet of SetColorResponse
// ============================================================================
// need pairs
// ============================================================================
// Move command => pair of (Move command parameters), (function MoveResponse -> something)
// Turn command => pair of (Turn command parameters), (function unit -> something)
// ============================================================================
// so, steal design from a recursive list structure like "List"
// ============================================================================
module ListExample =
type List<'a> =
| Empty
| Cons of ('a * List<'a>)
// ============================================================================
// Turtle Program V1
// * needs an explicit Stop case
// * not generic
// ============================================================================
module TurtleProgram_v1 =
open Turtle
/// Create a union type to represent each instruction
type TurtleProgram =
// (input params) (response)
| Stop
| Move of Distance * (MoveResponse -> TurtleProgram)
| Turn of Angle * (unit -> TurtleProgram)
| PenUp of (* none *) (unit -> TurtleProgram)
| PenDown of (* none *) (unit -> TurtleProgram)
| SetColor of PenColor * (SetColorResponse -> TurtleProgram)
// ------------------------
// Example of Turtle Program V1 object
// ------------------------
module TurtleProgram_v1_Example =
open TurtleProgram_v1
let drawTriangle =
Move (100.0, fun response ->
Turn (120.0<Degrees>, fun () ->
Move (100.0, fun response ->
Turn (120.0<Degrees>, fun () ->
Move (100.0, fun response ->
Turn (120.0<Degrees>, fun () ->
Stop))))))
// val drawTriangle : TurtleProgram
// ------------------------
// Interpreters for Turtle Program v1
// ------------------------
module TurtleProgram_v1_Interpreter =
open TurtleProgram_v1
/// Interpret by calling the turtle functions
let rec interpretAsTurtle state program =
let log = printfn "%s"
match program with
| Stop ->
state
| Move (dist,next) ->
let result,newState = Turtle.move log dist state
let nextProgram = next result // compute the next step
interpretAsTurtle newState nextProgram
| Turn (angle,next) ->
let newState = Turtle.turn log angle state
let nextProgram = next() // compute the next step
interpretAsTurtle newState nextProgram
| PenUp next ->
let newState = Turtle.penUp log state
let nextProgram = next()
interpretAsTurtle newState nextProgram
| PenDown next ->
let newState = Turtle.penDown log state
let nextProgram = next()
interpretAsTurtle newState nextProgram
| SetColor (color,next) ->
let result,newState = Turtle.setColor log color state
let nextProgram = next result
interpretAsTurtle newState nextProgram
/// Interpret by accumulating distance
let rec interpretAsDistance distanceSoFar program =
let recurse = interpretAsDistance
let log = printfn "%s"
match program with
| Stop ->
distanceSoFar
| Move (dist,next) ->
let newDistanceSoFar = distanceSoFar + dist
let result = Turtle.MoveOk // hard-code result
let nextProgram = next result
recurse newDistanceSoFar nextProgram
| Turn (angle,next) ->
// no change in distanceSoFar
let nextProgram = next()
recurse distanceSoFar nextProgram
| PenUp next ->
// no change in distanceSoFar
let nextProgram = next()
recurse distanceSoFar nextProgram
| PenDown next ->
// no change in distanceSoFar
let nextProgram = next()
recurse distanceSoFar nextProgram
| SetColor (color,next) ->
// no change in distanceSoFar
let result = Turtle.ColorOk // hard-code result
let nextProgram = next result
recurse distanceSoFar nextProgram
// ------------------------
// TurtleProgram_v1_Interpreter Tests
// ------------------------
do
let program = TurtleProgram_v1_Example.drawTriangle
let interpret = TurtleProgram_v1_Interpreter.interpretAsTurtle // choose an interpreter
let initialState = Turtle.initialTurtleState
interpret initialState program |> ignore
// output
(*
Move 100.0
...Draw line from (0.0,0.0) to (100.0,0.0) using Black
Turn 120.0
Move 100.0
...Draw line from (100.0,0.0) to (50.0,86.6) using Black
Turn 120.0
Move 100.0
...Draw line from (50.0,86.6) to (0.0,0.0) using Black
Turn 120.0
*)
do
let program = TurtleProgram_v1_Example.drawTriangle // same program
let interpret = TurtleProgram_v1_Interpreter.interpretAsDistance // choose an interpreter
let initialState = 0.0
interpret initialState program |> printfn "Total distance moved is %0.1f"
// output
(*
Total distance moved is 300.0
*)
// ============================================================================
// Turtle Program v1 computation expression
// * changed type to be generic so that bind works properly
// ============================================================================
module TurtleProgram_v1_Workflow =
open Turtle
type TurtleProgram<'a> =
| Stop of 'a
| Move of Distance * (MoveResponse -> TurtleProgram<'a>)
| Turn of Angle * (unit -> TurtleProgram<'a>)
| PenUp of (unit -> TurtleProgram<'a>)
| PenDown of (unit -> TurtleProgram<'a>)
| SetColor of PenColor * (SetColorResponse -> TurtleProgram<'a>)
let returnT x =
Stop x
let rec bindT f inst =
match inst with
| Stop x ->
f x
| Move(dist,next) ->
(*
Move(dist,fun response -> (bindT f)(next response))
*)
// "next >> bindT f" is a shorter version of function response
Move(dist,next >> bindT f)
| Turn(angle,next) ->
Turn(angle,next >> bindT f)
| PenUp(next) ->
PenUp(next >> bindT f)
| PenDown(next) ->
PenDown(next >> bindT f)
| SetColor(color,next) ->
SetColor(color,next >> bindT f)
// define a computation expression builder
type TurtleProgramBuilder() =
member this.Return(x) = returnT x
member this.Bind(x,f) = bindT f x
member this.Zero(x) = returnT ()
// create an instance of the computation expression builder
let turtleProgram = TurtleProgramBuilder()
// ------------------------
// Interpreters for Turtle Program v1 workflow
// ------------------------
module TurtleProgram_v1_WorkflowInterpreter =
open TurtleProgram_v1_Workflow
/// Interpret by calling the turtle functions
let rec interpretAsTurtle state program =
let log = printfn "%s"
match program with
| Stop x ->
state
| Move (dist,next) ->
let result,newState = Turtle.move log dist state
let nextProgram = next result
interpretAsTurtle newState nextProgram
| Turn (angle,next) ->
let newState = Turtle.turn log angle state
let nextProgram = next()
interpretAsTurtle newState nextProgram
| PenUp next ->
let newState = Turtle.penUp log state
let nextProgram = next()
interpretAsTurtle newState nextProgram
| PenDown next ->
let newState = Turtle.penDown log state
let nextProgram = next()
interpretAsTurtle newState nextProgram
| SetColor (color,next) ->
let result,newState = Turtle.setColor log color state
let nextProgram = next result
interpretAsTurtle newState nextProgram
/// Interpret by accumulating distance
let rec interpretAsDistance distanceSoFar program =
let recurse = interpretAsDistance
let log = printfn "%s"
match program with
| Stop x ->
distanceSoFar
| Move (dist,next) ->
let newDistanceSoFar = distanceSoFar + dist
let result = Turtle.MoveOk // hard-code result
let nextProgram = next result
recurse newDistanceSoFar nextProgram
| Turn (angle,next) ->
// no change in distanceSoFar
let nextProgram = next()
recurse distanceSoFar nextProgram
| PenUp next ->
// no change in distanceSoFar
let nextProgram = next()
recurse distanceSoFar nextProgram
| PenDown next ->
// no change in distanceSoFar
let nextProgram = next()
recurse distanceSoFar nextProgram
| SetColor (color,next) ->
// no change in distanceSoFar
let result = Turtle.ColorOk // hard-code result
let nextProgram = next result
recurse distanceSoFar nextProgram
// ------------------------
// Example using workflow
// * because bind is not generic, code fails to compile
// ------------------------
module TurtleProgram_v1_WorkflowExample =
open TurtleProgram_v1_Workflow
// helper functions
let stop = fun x -> Stop x
let move dist = Move (dist, stop)
let turn angle = Turn (angle, stop)
let penUp = PenUp stop
let penDown = PenDown stop
let setColor color = SetColor (color,stop)
let handleMoveResponse log moveResponse = turtleProgram {
match moveResponse with
| Turtle.MoveOk ->
()
| Turtle.HitABarrier ->
// turn 90 before trying again
log "Oops -- hit a barrier -- turning"
let! x = turn 90.0<Degrees>
()
}
// example
let drawTwoLines log = turtleProgram {
let! response = move 60.0
do! handleMoveResponse log response
let! response = move 60.0
do! handleMoveResponse log response
}
// val drawTwoLines: TurtleProgram<unit>
// ------------------------
// Interpret a workflow
// ------------------------
// Interpret as turtle
do
let log = printfn "%s"
let program = TurtleProgram_v1_WorkflowExample.drawTwoLines log
let interpret = TurtleProgram_v1_WorkflowInterpreter.interpretAsTurtle
let initialState = Turtle.initialTurtleState
interpret initialState program |> ignore
// Interpret as distance
do
let log = printfn "%s"
let program = TurtleProgram_v1_WorkflowExample.drawTwoLines log
let interpret = TurtleProgram_v1_WorkflowInterpreter.interpretAsDistance
let initialState = 0.0
interpret initialState program |> printfn "Total distance moved is %0.1f"