Skip to content

Commit 60bb890

Browse files
committed
Made chain take world by capture now.
1 parent 26b1cd8 commit 60bb890

1 file changed

Lines changed: 26 additions & 46 deletions

File tree

Nu/Nu/World/WorldChain.fs

Lines changed: 26 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,28 @@ open Prime
99
/// The Chain monad. Allows the user to define a chain of operations over the world that
1010
/// optionally spans across a bounded number of events.
1111
type [<ReferenceEquality>] Chain<'e, 'a> =
12-
Chain of (World -> Either<'e -> Chain<'e, 'a>, 'a>)
12+
Chain of (unit -> Either<'e -> Chain<'e, 'a>, 'a>)
1313

1414
/// Implements the chain monad.
1515
type ChainBuilder () =
1616

1717
/// Functor map for the chain monad.
1818
[<DebuggerHidden; DebuggerStepThrough>]
1919
member this.Map (f : 'a -> 'b) (a : Chain<'e, 'a>) : Chain<'e, 'b> =
20-
Chain (fun world ->
20+
Chain (fun () ->
2121
let chainMapper eir =
2222
match eir with
23-
| Left c -> Left (fun world -> this.Map f (c world))
23+
| Left c -> Left (fun a -> this.Map f (c a))
2424
| Right a -> Right (f a)
25-
let eir = match a with Chain b -> b world
25+
let eir = match a with Chain b -> b ()
2626
chainMapper eir)
2727

2828
/// Applicative apply for the chain monad.
2929
/// TODO: Implement!
3030
[<DebuggerHidden; DebuggerStepThrough>]
3131
member this.Apply (c : Chain<'e, 'a -> 'b>) (_ : Chain<'e, 'a>) : Chain<'e, 'b> =
32-
Chain (fun world ->
33-
match (match c with Chain f -> f world) with
32+
Chain (fun () ->
33+
match (match c with Chain f -> f ()) with
3434
// ^--- NOTE: unbounded recursion here
3535
| _ -> failwithnie ())
3636

@@ -42,11 +42,11 @@ type ChainBuilder () =
4242
/// Monadic bind for the chain monad.
4343
[<DebuggerHidden; DebuggerStepThrough>]
4444
member this.Bind (c : Chain<'e, 'a>, cont : 'a -> Chain<'e, 'b>) : Chain<'e, 'b> =
45-
Chain (fun world ->
46-
match (match c with Chain f -> f world) with
45+
Chain (fun () ->
46+
match (match c with Chain f -> f ()) with
4747
// ^--- NOTE: unbounded recursion here
4848
| Left c -> Left (fun e -> this.Bind (c e, cont))
49-
| Right v -> match cont v with Chain f -> f world)
49+
| Right v -> match cont v with Chain f -> f ())
5050

5151
/// ChainBuilder operators.
5252
[<AutoOpen>]
@@ -70,22 +70,6 @@ module Chain =
7070
/// Monadic bind for the chain monad.
7171
let [<DebuggerHidden; DebuggerStepThrough>] inline bind c a = chain.Bind (c, a)
7272

73-
/// Get the world.
74-
let [<DebuggerHidden>] get : Chain<'e, World> =
75-
Chain (fun world -> Right world)
76-
77-
/// Get the world as transformed via 'by'.
78-
let [<DebuggerHidden; DebuggerStepThrough>] getBy by : Chain<'e, 'a> =
79-
Chain (fun world -> Right (by world))
80-
81-
/// Update the world with an additional transformed world parameter.
82-
let [<DebuggerHidden; DebuggerStepThrough>] updateBy by expr : Chain<'e, unit> =
83-
Chain (fun world -> expr (by world) world; Right ())
84-
85-
/// Update the world.
86-
let [<DebuggerHidden; DebuggerStepThrough>] update expr : Chain<'e, unit> =
87-
Chain (fun world -> expr world; Right ())
88-
8973
/// Get the next event.
9074
let [<DebuggerHidden>] next : Chain<'e, 'e> =
9175
Chain (fun _ -> Left returnM)
@@ -98,48 +82,44 @@ module Chain =
9882
let [<DebuggerHidden; DebuggerStepThrough>] reactData<'a, 's when 's :> Simulant> expr : Chain<Event<'a, 's>, unit> =
9983
chain {
10084
let! e = next
101-
let! world = get
102-
expr (e.Data) world
85+
expr (e.Data) ()
10386
return () }
10487

10588
/// React to the next event, using the event's value in the reaction.
10689
let [<DebuggerHidden; DebuggerStepThrough>] reactEvent expr : Chain<'e, unit> =
10790
chain {
10891
let! e = next
109-
let! world = get
110-
expr e world
92+
expr e ()
11193
return () }
11294

11395
/// React to the next event, discarding the event's value.
11496
let [<DebuggerHidden; DebuggerStepThrough>] react expr : Chain<'e, unit> =
11597
chain {
11698
do! pass
117-
let! world = get
118-
expr world
99+
expr ()
119100
return () }
120101

121102
/// Loop in a chain context while 'pred' evaluate to true considering only the loop data.
122-
let rec [<DebuggerHidden; DebuggerStepThrough>] loop (i : 'i) (step : 'i -> 'i) (pred : 'i -> World -> bool) (c : 'i -> Chain<'e, unit>) =
103+
let rec [<DebuggerHidden; DebuggerStepThrough>] loop (i : 'i) (step : 'i -> 'i) (pred : 'i -> bool) (c : 'i -> Chain<'e, unit>) =
123104
chain {
124-
let! world = get
125-
do! if pred i world then
105+
do! if pred i then
126106
chain {
127107
do! c i
128108
let i = step i
129109
do! loop i step pred c }
130110
else returnM () }
131111

132112
/// Loop in a chain context while 'pred' evaluates to true considering only the world state.
133-
let [<DebuggerHidden; DebuggerStepThrough>] during (pred : World -> bool) (c : Chain<'e, unit>) =
134-
loop () id (fun _ -> pred) (fun _ -> c)
113+
let [<DebuggerHidden; DebuggerStepThrough>] during (pred : unit -> bool) (c : Chain<'e, unit>) =
114+
loop () id pred (fun _ -> c)
135115

136116
/// Step once into a chain.
137-
let [<DebuggerHidden; DebuggerStepThrough>] step (c : Chain<'e, 'a>) (world : World) : Either<'e -> Chain<'e, 'a>, 'a> =
138-
match c with Chain f -> f world
117+
let [<DebuggerHidden; DebuggerStepThrough>] step (c : Chain<'e, 'a>) : Either<'e -> Chain<'e, 'a>, 'a> =
118+
match c with Chain f -> f ()
139119

140120
/// Advance a chain value by one step, providing 'e'.
141-
let [<DebuggerHidden; DebuggerStepThrough>] advance (c : 'e -> Chain<'e, 'a>) (e : 'e) (world : World) : Either<'e -> Chain<'e, 'a>, 'a> =
142-
step (c e) world
121+
let [<DebuggerHidden; DebuggerStepThrough>] advance (c : 'e -> Chain<'e, 'a>) (e : 'e) : Either<'e -> Chain<'e, 'a>, 'a> =
122+
step (c e)
143123

144124
/// Chain functions for the world.
145125
[<AutoOpen>]
@@ -149,15 +129,15 @@ module WorldChain =
149129

150130
/// Run a chain to its end, providing 'e' for all its steps.
151131
[<DebuggerHidden; DebuggerStepThrough>]
152-
static member chainConstant (c : Chain<'e, 'a>) (e : 'e) (world : World) : 'a =
153-
match Chain.step c world with
154-
| Left chain -> World.chainConstant (chain e) e world
132+
static member chainConstant (c : Chain<'e, 'a>) (e : 'e) : 'a =
133+
match Chain.step c with
134+
| Left chain -> World.chainConstant (chain e) e
155135
| Right v -> v
156136

157137
/// Run a chain to its end, providing unit for all its steps.
158138
[<DebuggerHidden; DebuggerStepThrough>]
159-
static member chainUnit (c : Chain<unit, 'a>) (world : World) : 'a =
160-
World.chainConstant c () world
139+
static member chainUnit (c : Chain<unit, 'a>) : 'a =
140+
World.chainConstant c ()
161141

162142
/// Execute a chain over the given stream.
163143
[<DebuggerHidden; DebuggerStepThrough>]
@@ -173,7 +153,7 @@ module WorldChain =
173153
World.unsubscribe subscriptionId world
174154
let advance = fun evt world ->
175155
let chain = World.getEventState stateId world : Event<'a, Simulant> -> Chain<Event<'a, Simulant>, unit>
176-
let advanceResult = Chain.advance chain evt world
156+
let advanceResult = Chain.advance chain evt
177157
match advanceResult with
178158
| Right () -> unsubscribe world
179159
| Left chainNext -> World.addEventState stateId chainNext world

0 commit comments

Comments
 (0)