-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy path12-BranchingOnResponse.fsx
184 lines (138 loc) · 5.03 KB
/
12-BranchingOnResponse.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
(* ======================================
12-BranchingOnResponse.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 #12: Monadic control flow -- Making decisions in the turtle computation expression
In this design, the turtle can reply to certain commands with errors.
The code demonstrates how the client can make decisions inside the turtle computation expression
while the state is being passed around behind the scenes.
====================================== *)
#load "Common.fsx"
#load "FPTurtleLib2.fsx"
open Common
open FPTurtleLib2
// ======================================
// TurtleStateComputation
// ======================================
/// Create a type to wrap a function like:
/// oldState -> (a,newState)
type TurtleStateComputation<'a> =
TurtleStateComputation of (Turtle.TurtleState -> 'a * Turtle.TurtleState)
/// Functions that work with TurtleStateComputation
module TurtleStateComputation =
let runT turtle state =
let (TurtleStateComputation innerFn) = turtle
innerFn state
let returnT x =
let innerFn state =
(x,state)
TurtleStateComputation innerFn
let bindT f xT =
let innerFn state =
let x,state2 = runT xT state
runT (f x) state2
TurtleStateComputation innerFn
let mapT f =
bindT (f >> returnT)
let toComputation f =
let innerFn state =
let (result,newState) = f state
(result,newState)
TurtleStateComputation innerFn
let toUnitComputation f =
let f2 state =
(),f state
toComputation f2
// define a computation expression builder
type TurtleBuilder() =
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 turtle = TurtleBuilder()
// ======================================
// TurtleComputationClient
// ======================================
module TurtleComputationClient =
open TurtleStateComputation
/// Function to log a message
let log message =
printfn "%s" message
let initialTurtleState =
Turtle.initialTurtleState
// ----------------------------------------
// monadic versions of the Turtle functions
// ----------------------------------------
let move dist =
toComputation (Turtle.move log dist)
// val move : Distance -> TurtleStateComputation<MoveResponse>
let turn angle =
toUnitComputation (Turtle.turn log angle)
// val turn : Angle -> TurtleStateComputation<unit>
let penDown =
toUnitComputation (Turtle.penDown log)
// val penDown : TurtleStateComputation<unit>
let penUp =
toUnitComputation (Turtle.penUp log)
// val penUp : TurtleStateComputation<unit>
let setColor color =
toComputation (Turtle.setColor log color)
// val setColor : PenColor -> TurtleStateComputation<SetColorResponse>
// ----------------------------------------
// draw various things
// ----------------------------------------
let handleMoveResponse moveResponse = turtle {
match moveResponse with
| Turtle.MoveOk ->
() // do nothing
| Turtle.HitABarrier ->
// turn 90 before trying again
printfn "Oops -- hit a barrier -- turning"
do! turn 90.0<Degrees>
}
(*
// it is an error to NOT response to `move` now!
let drawShape() =
// define a set of instructions
let t = turtle {
do! move 60.0
// error FS0001:
// This expression was expected to have type
// Turtle.MoveResponse
// but here has type
// unit
do! move 60.0
}
// finally, run the monad using the initial state
runT t initialTurtleState
*)
let drawShapeWithoutResponding() =
// define a set of instructions
let t = turtle {
let! response = move 60.0
let! response = move 60.0
let! response = move 60.0
return ()
}
// finally, run the monad using the initial state
runT t initialTurtleState
let drawShape() =
// define a set of instructions
let t = turtle {
let! response = move 60.0
do! handleMoveResponse response
let! response = move 60.0
do! handleMoveResponse response
let! response = move 60.0
do! handleMoveResponse response
}
// finally, run the monad using the initial state
runT t initialTurtleState
// ======================================
// Turtle Monad Tests
// ======================================
(*
TurtleComputationClient.drawShapeWithoutResponding()
TurtleComputationClient.drawShape()
*)