-
Notifications
You must be signed in to change notification settings - Fork 1
/
Monad.fs
1078 lines (852 loc) · 38.5 KB
/
Monad.fs
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
namespace Yaaf.FSharp.Functional
#nowarn "40"
open System
open System.Collections
open System.Collections.Generic
open Yaaf.FSharp.Collections
/// Generic monadic operators
module Operators =
/// Inject a value into the monadic type
let inline returnM builder x = (^M: (member Return: 'b -> 'c) (builder, x))
let inline bindM builder m f = (^M: (member Bind: 'd -> ('e -> 'c) -> 'c) (builder, m, f))
let inline liftM builder f m =
let inline ret x = returnM builder (f x)
bindM builder m ret
/// Sequential application
let inline applyM (builder1:^M1) (builder2:^M2) f m =
bindM builder1 f <| fun f' ->
bindM builder2 m <| fun m' ->
returnM builder2 (f' m')
module Async =
open Operators
/// Sequentially compose two actions, passing any value produced by the second as an argument to the first.
let inline bind f m = async.Bind(m,f)
/// Inject a value into the async type
let inline returnM x = returnM async x
/// Sequentially compose two actions, passing any value produced by the first as an argument to the second.
let inline (>>=) m f = bindM async m f
/// Flipped >>=
let inline (=<<) f m = bindM async m f
/// Sequential application
let inline (<*>) f m = applyM async async f m
/// Sequential application
let inline ap m f = f <*> m
/// Flipped map
let inline pipe m f = liftM async f m
let inline pipe2 x y f = returnM f <*> x <*> y
let inline pipe3 x y z f = returnM f <*> x <*> y <*> z
/// Transforms an async value by using a specified mapping function.
let inline map f m = pipe m f
/// Promote a function to a monad/applicative, scanning the monadic/applicative arguments from left to right.
let inline lift2 f x y = returnM f <*> x <*> y
/// Infix map
let inline (<!>) f m = pipe m f
/// Sequence actions, discarding the value of the first argument.
let inline ( *>) x y = pipe2 x y (fun _ z -> z)
/// Sequence actions, discarding the value of the second argument.
let inline ( <*) x y = pipe2 x y (fun z _ -> z)
/// Sequentially compose two async actions, discarding any value produced by the first
let inline (>>.) m f = bindM async m (fun _ -> f)
/// Left-to-right Kleisli composition
let inline (>=>) f g = fun x -> f x >>= g
/// Right-to-left Kleisli composition
let inline (<=<) x = flip (>=>) x
let foldM f s =
Seq.fold (fun acc t -> acc >>= (flip f) t) (returnM s)
let inline sequence s =
let inline cons a b = lift2 List.cons a b
List.foldBack cons s (returnM [])
let inline mapM f x = sequence (List.map f x)
module ZipList =
let returnM v = Seq.initInfinite (fun _ -> v)
/// Sequential application
let (<*>) f a = Seq.zip f a |> Seq.map (fun (k,v) -> k v)
/// Sequential application
let inline ap m f = f <*> m
/// Promote a function to a monad/applicative, scanning the monadic/applicative arguments from left to right.
let inline lift2 f a b = returnM f <*> a <*> b
/// Sequence actions, discarding the value of the first argument.
let inline ( *>) x y = lift2 (fun _ z -> z) x y
/// Sequence actions, discarding the value of the second argument.
let inline ( <*) x y = lift2 (fun z _ -> z) x y
module Option =
/// The maybe monad.
/// This monad is my own and uses an 'T option. Others generally make their own Maybe<'T> type from Option<'T>.
/// The builder approach is from Matthew Podwysocki's excellent Creating Extended Builders series http://codebetter.com/blogs/matthew.podwysocki/archive/2010/01/18/much-ado-about-monads-creating-extended-builders.aspx.
type MaybeBuilder() =
member this.Return(x) = Some x
member this.ReturnFrom(m: 'T option) = m
member this.Bind(m, f) = Option.bind f m
member this.Zero() = None
member this.Combine(m, f) = Option.bind f m
member this.Delay(f: unit -> _) = f
member this.Run(f) = f()
member this.TryWith(m, h) =
try this.ReturnFrom(m)
with e -> h e
member this.TryFinally(m, compensation) =
try this.ReturnFrom(m)
finally compensation()
member this.Using(res:#IDisposable, body) =
this.TryFinally(body res, fun () -> match res with null -> () | disp -> disp.Dispose())
member this.While(guard, f) =
if not (guard()) then this.Zero() else
this.Bind(f(), fun _ -> this.While(guard, f))
member this.For(sequence:seq<_>, body) =
this.Using(sequence.GetEnumerator(),
fun enum -> this.While(enum.MoveNext, this.Delay(fun () -> body enum.Current)))
let maybe = MaybeBuilder()
/// Option wrapper monoid
let monoid (m: _ ISemigroup) =
{ new Monoid<_>() with
override this.Zero() = None
override this.Combine(a, b) =
match a,b with
| Some a, Some b -> Some (m.Combine(a,b))
| Some a, None -> Some a
| None, Some a -> Some a
| None, None -> None }
open Operators
/// Inject a value into the option type
let inline returnM x = returnM maybe x
/// Sequentially compose two actions, passing any value produced by the first as an argument to the second.
let inline (>>=) m f = bindM maybe m f
/// Flipped >>=
let inline (=<<) f m = bindM maybe m f
/// Sequential application
let inline (<*>) f m = applyM maybe maybe f m
/// Sequential application
let inline ap m f = f <*> m
/// Infix map
let inline (<!>) f m = Option.map f m
/// Promote a function to a monad/applicative, scanning the monadic/applicative arguments from left to right.
let inline lift2 f a b = returnM f <*> a <*> b
/// Sequence actions, discarding the value of the first argument.
let inline ( *>) x y = lift2 (fun _ z -> z) x y
/// Sequence actions, discarding the value of the second argument.
let inline ( <*) x y = lift2 (fun z _ -> z) x y
/// Sequentially compose two maybe actions, discarding any value produced by the first
let inline (>>.) m f = bindM maybe m (fun _ -> f)
/// Left-to-right Kleisli composition
let inline (>=>) f g = fun x -> f x >>= g
/// Right-to-left Kleisli composition
let inline (<=<) x = flip (>=>) x
/// Maps a Nullable to Option
let ofNullable (n: _ Nullable) =
if n.HasValue
then Some n.Value
else None
/// Maps an Option to Nullable
let toNullable =
function
| None -> Nullable()
| Some x -> Nullable(x)
/// True -> Some(), False -> None
let inline ofBool b = if b then Some() else None
/// Converts a function returning bool,value to a function returning value option.
/// Useful to process TryXX style functions.
let inline tryParseWith func = func >> function
| true, value -> Some value
| false, _ -> None
/// If true,value then returns Some value. Otherwise returns None.
/// Useful to process TryXX style functions.
let inline ofBoolAndValue b =
match b with
| true,v -> Some v
| _ -> None
/// Maps Choice 1Of2 to Some value, otherwise None.
let ofChoice =
function
| Choice1Of2 a -> Some a
| _ -> None
/// Gets the value associated with the option or the supplied default value.
let inline getOrElse v =
function
| Some x -> x
| None -> v
/// Gets the value associated with the option or the supplied default value.
let inline getOrElseLazy (v: _ Lazy) =
function
| Some x -> x
| None -> v.Value
/// Gets the value associated with the option or the supplied default value from a function.
let inline getOrElseF v =
function
| Some x -> x
| None -> v()
/// Gets the value associated with the option or the default value for the type.
let getOrDefault =
function
| Some x -> x
| None -> Unchecked.defaultof<_>
/// Gets the option if Some x, otherwise the supplied default value.
let inline orElse v =
function
| Some x -> Some x
| None -> v
/// Applies a predicate to the option. If the predicate returns true, returns Some x, otherwise None.
let inline filter pred =
function
| Some x when pred x -> Some x
| _ -> None
/// Attempts to cast an object. Returns None if unsuccessful.
[<CompiledName("Cast")>]
let inline cast (o: obj) =
try
Some (unbox o)
with _ -> None
let foldM f s =
Seq.fold (fun acc t -> acc >>= (flip f) t) (returnM s)
let inline sequence s =
let inline cons a b = lift2 List.cons a b
List.foldBack cons s (returnM [])
let inline mapM f x = sequence (List.map f x)
let inline getOrElseWith v f =
function
| Some x -> f x
| None -> v
// Additional Option-Module extensions
/// Haskell-style maybe operator
let option (defaultValue : 'U) (map : 'T -> 'U) = function
| None -> defaultValue
| Some a -> map a
/// transforms a function in the Try...(input, out output) style
/// into a function of type: input -> output Option
/// Example: fromTryPattern(System.Double.TryParse)
/// See Examples.Option
let fromTryPattern (tryFun : ('input -> (bool * 'output))) =
fun input ->
match tryFun input with
| (true, output) -> Some output
| (false, _) -> None
/// Concatenates an option of option.
let inline concat x =
x >>= id
module Nullable =
let (|Null|Value|) (x: _ Nullable) =
if x.HasValue then Value x.Value else Null
let create x = Nullable x
/// Gets the value associated with the nullable or the supplied default value.
let getOrDefault n v = match n with Value x -> x | _ -> v
/// Gets the value associated with the nullable or the supplied default value.
let getOrElse (n: Nullable<'T>) (v: Lazy<'T>) = match n with Value x -> x | _ -> v.Force()
/// Gets the value associated with the Nullable.
/// If no value, throws.
let get (x: Nullable<_>) = x.Value
/// Converts option to nullable
let ofOption = Option.toNullable
/// Converts nullable to option
let toOption = Option.ofNullable
/// Monadic bind
let bind f x =
match x with
| Null -> Nullable()
| Value v -> f v
/// True if Nullable has value
let hasValue (x: _ Nullable) = x.HasValue
/// True if Nullable does not have value
let isNull (x: _ Nullable) = not x.HasValue
/// Returns 1 if Nullable has value, otherwise 0
let count (x: _ Nullable) = if x.HasValue then 1 else 0
/// Evaluates the equivalent of List.fold for a nullable.
let fold f state x =
match x with
| Null -> state
| Value v -> f state v
/// Performs the equivalent of the List.foldBack operation on a nullable.
let foldBack f x state =
match x with
| Null -> state
| Value v -> f x state
/// Evaluates the equivalent of List.exists for a nullable.
let exists p x =
match x with
| Null -> false
| Value v -> p x
/// Evaluates the equivalent of List.forall for a nullable.
let forall p x =
match x with
| Null -> true
| Value v -> p x
/// Executes a function for a nullable value.
let iter f x =
match x with
| Null -> ()
| Value v -> f v
/// Transforms a Nullable value by using a specified mapping function.
let map f x =
match x with
| Null -> Nullable()
| Value v -> Nullable(f v)
/// Convert the nullable to an array of length 0 or 1.
let toArray x =
match x with
| Null -> [||]
| Value v -> [| v |]
/// Convert the nullable to a list of length 0 or 1.
let toList x =
match x with
| Null -> []
| Value v -> [v]
/// Promote a function to a monad/applicative, scanning the monadic/applicative arguments from left to right.
let lift2 f (a: _ Nullable) (b: _ Nullable) =
if a.HasValue && b.HasValue
then Nullable(f a.Value b.Value)
else Nullable()
let mapBool op a b =
match a,b with
| Value x, Value y -> op x y
| _ -> false
let inline (+?) a b = (lift2 (+)) a b
let inline (-?) a b = (lift2 (-)) a b
let inline ( *?) a b = (lift2 ( *)) a b
let inline (/?) a b = (lift2 (/)) a b
let inline (>?) a b = (mapBool (>)) a b
let inline (>=?) a b = a >? b || a = b
let inline (<?) a b = (mapBool (<)) a b
let inline (<=?) a b = a <? b || a = b
let inline notn (a: bool Nullable) =
if a.HasValue
then Nullable(not a.Value)
else Nullable()
let inline (&?) a b =
let rec and' a b =
match a,b with
| Null, Value y when not y -> Nullable(false)
| Null, Value y when y -> Nullable()
| Null, Null -> Nullable()
| Value x, Value y -> Nullable(x && y)
| _ -> and' b a
and' a b
let inline (|?) a b = notn ((notn a) &? (notn b))
type Int32 with
member x.n = Nullable x
type Double with
member x.n = Nullable x
type Single with
member x.n = Nullable x
type Byte with
member x.n = Nullable x
type Int64 with
member x.n = Nullable x
type Decimal with
member x.n = Nullable x
module State =
type State<'T, 'State> = 'State -> 'T * 'State
let getState = fun s -> (s,s)
let putState s = fun _ -> ((),s)
let eval m s = m s |> fst
let exec m s = m s |> snd
let empty = fun s -> ((), s)
let bind k m = fun s -> let (a, s') = m s in (k a) s'
/// The state monad.
/// The algorithm is adjusted from my original work off of Brian Beckman's http://channel9.msdn.com/shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad/.
/// The approach was adjusted from Matthew Podwysocki's http://codebetter.com/blogs/matthew.podwysocki/archive/2009/12/30/much-ado-about-monads-state-edition.aspx and mirrors his final result.
type StateBuilder() =
member this.Return(a) : State<'T,'State> = fun s -> (a,s)
member this.ReturnFrom(m:State<'T,'State>) = m
member this.Bind(m:State<'T,'State>, k:'T -> State<'U,'State>) : State<'U,'State> = bind k m
member this.Zero() = this.Return ()
member this.Combine(r1, r2) = this.Bind(r1, fun () -> r2)
member this.TryWith(m:State<'T,'State>, h:exn -> State<'T,'State>) : State<'T,'State> =
fun env -> try m env
with e -> (h e) env
member this.TryFinally(m:State<'T,'State>, compensation) : State<'T,'State> =
fun env -> try m env
finally compensation()
member this.Using(res:#IDisposable, body) =
this.TryFinally(body res, (fun () -> match res with null -> () | disp -> disp.Dispose()))
member this.Delay(f) = this.Bind(this.Return (), f)
member this.While(guard, m) =
if not(guard()) then this.Zero() else
this.Bind(m, (fun () -> this.While(guard, m)))
member this.For(sequence:seq<_>, body) =
this.Using(sequence.GetEnumerator(),
(fun enum -> this.While(enum.MoveNext, this.Delay(fun () -> body enum.Current))))
let state = new StateBuilder()
open Operators
/// Inject a value into the State type
let inline returnM x = returnM state x
/// Sequentially compose two actions, passing any value produced by the first as an argument to the second.
let inline (>>=) m f = bindM state m f
/// Flipped >>=
let inline (=<<) f m = bindM state m f
/// Sequential application
let inline (<*>) f m = applyM state state f m
/// Sequential application
let inline ap m f = f <*> m
/// Transforms a State value by using a specified mapping function.
let inline map f m = liftM state f m
/// Infix map
let inline (<!>) f m = map f m
/// Promote a function to a monad/applicative, scanning the monadic/applicative arguments from left to right.
let inline lift2 f a b = returnM f <*> a <*> b
/// Sequence actions, discarding the value of the first argument.
let inline ( *>) x y = lift2 (fun _ z -> z) x y
/// Sequence actions, discarding the value of the second argument.
let inline ( <*) x y = lift2 (fun z _ -> z) x y
/// Sequentially compose two state actions, discarding any value produced by the first
let inline (>>.) m f = bindM state m (fun _ -> f)
/// Left-to-right Kleisli composition
let inline (>=>) f g = fun x -> f x >>= g
/// Right-to-left Kleisli composition
let inline (<=<) x = flip (>=>) x
let foldM f s =
Seq.fold (fun acc t -> acc >>= (flip f) t) (returnM s)
let inline sequence s =
let inline cons a b = lift2 List.cons a b
List.foldBack cons s (returnM [])
let inline mapM f x = sequence (List.map f x)
module Reader =
type Reader<'R,'T> = 'R -> 'T
let bind k m = fun r -> (k (m r)) r
/// The reader monad.
/// This monad comes from Matthew Podwysocki's http://codebetter.com/blogs/matthew.podwysocki/archive/2010/01/07/much-ado-about-monads-reader-edition.aspx.
type ReaderBuilder() =
member this.Return(a) : Reader<'R,'T> = fun _ -> a
member this.ReturnFrom(a:Reader<'R,'T>) = a
member this.Bind(m:Reader<'R,'T>, k:'T -> Reader<'R,'U>) : Reader<'R,'U> = bind k m
member this.Zero() = this.Return ()
member this.Combine(r1, r2) = this.Bind(r1, fun () -> r2)
member this.TryWith(m:Reader<'R,'T>, h:exn -> Reader<'R,'T>) : Reader<'R,'T> =
fun env -> try m env
with e -> (h e) env
member this.TryFinally(m:Reader<'R,'T>, compensation) : Reader<'R,'T> =
fun env -> try m env
finally compensation()
member this.Using(res:#IDisposable, body) =
this.TryFinally(body res, (fun () -> match res with null -> () | disp -> disp.Dispose()))
member this.Delay(f) = this.Bind(this.Return (), f)
member this.While(guard, m) =
if not(guard()) then this.Zero() else
this.Bind(m, (fun () -> this.While(guard, m)))
member this.For(sequence:seq<_>, body) =
this.Using(sequence.GetEnumerator(),
(fun enum -> this.While(enum.MoveNext, this.Delay(fun () -> body enum.Current))))
let reader = new ReaderBuilder()
let ask : Reader<'R,'R> = id
let asks f = reader {
let! r = ask
return (f r) }
let local (f:'r1 -> 'r2) (m:Reader<'r2,'T>) : Reader<'r1, 'T> = f >> m
open Operators
/// Inject a value into the Reader type
let inline returnM x = returnM reader x
/// Sequentially compose two actions, passing any value produced by the first as an argument to the second.
let inline (>>=) m f = bindM reader m f
/// Flipped >>=
let inline (=<<) f m = bindM reader m f
/// Sequential application
let inline (<*>) f m = applyM reader reader f m
/// Sequential application
let inline ap m f = f <*> m
/// Transforms a Reader value by using a specified mapping function.
let inline map f m = liftM reader f m
/// Infix map
let inline (<!>) f m = map f m
/// Promote a function to a monad/applicative, scanning the monadic/applicative arguments from left to right.
let inline lift2 f a b = returnM f <*> a <*> b
/// Sequence actions, discarding the value of the first argument.
let inline ( *>) x y = lift2 (fun _ z -> z) x y
/// Sequence actions, discarding the value of the second argument.
let inline ( <*) x y = lift2 (fun z _ -> z) x y
/// Sequentially compose two reader actions, discarding any value produced by the first
let inline (>>.) m f = bindM reader m (fun _ -> f)
/// Left-to-right Kleisli composition
let inline (>=>) f g = fun x -> f x >>= g
/// Right-to-left Kleisli composition
let inline (<=<) x = flip (>=>) x
let foldM f s =
Seq.fold (fun acc t -> acc >>= (flip f) t) (returnM s)
let inline sequence s =
let inline cons a b = lift2 List.cons a b
List.foldBack cons s (returnM [])
let inline mapM f x = sequence (List.map f x)
module Undo =
// UndoMonad on top of StateMonad
open State
let undoable = state
type History<'T> = {
Current: 'T
Undos : 'T list
Redos : 'T list }
let newHistory x = { Current = x; Undos = [x]; Redos = [] }
let current history = history.Current
let getHistory = getState
let putToHistory x = undoable {
let! history = getState
do! putState { Current = x;
Undos = history.Current :: history.Undos
Redos = [] } }
let exec m s = m s |> snd |> current
let getCurrent<'T> = undoable {
let! (history:'T History) = getState
return current history}
let combineWithCurrent f x = undoable {
let! currentVal = getCurrent
do! putToHistory (f currentVal x) }
let undo<'T> = undoable {
let! (history:'T History) = getState
match history.Undos with
| [] -> return false
| (x::rest) ->
do! putState { Current = x;
Undos = rest;
Redos = history.Current :: history.Redos }
return true}
let redo<'T> = undoable {
let! (history:'T History) = getState
match history.Redos with
| [] -> return false
| (x::rest) ->
do! putState { Current = x;
Undos = history.Current :: history.Undos;
Redos = rest }
return true }
module Writer =
open Yaaf.FSharp.Functional.Monoid
type Writer<'W, 'T> = unit -> 'T * 'W
let bind (m: Monoid<_>) (k:'T -> Writer<'W,'U>) (writer:Writer<'W,'T>) : Writer<'W,'U> =
fun () ->
let (a, w) = writer()
let (a', w') = (k a)()
(a', m.Combine(w, w'))
/// Inject a value into the Writer type
let returnM (monoid: Monoid<_>) a =
fun () -> (a, monoid.Zero())
/// The writer monad.
/// This monad comes from Matthew Podwysocki's http://codebetter.com/blogs/matthew.podwysocki/archive/2010/02/01/a-kick-in-the-monads-writer-edition.aspx.
type WriterBuilder<'W>(monoid: 'W Monoid) =
member this.Return(a) : Writer<'W,'T> = returnM monoid a
member this.ReturnFrom(w:Writer<'W,'T>) = w
member this.Bind(writer, k) = bind monoid k writer
member this.Zero() = this.Return ()
member this.TryWith(writer:Writer<'W,'T>, handler:exn -> Writer<'W,'T>) : Writer<'W,'T> =
fun () -> try writer()
with e -> (handler e)()
member this.TryFinally(writer, compensation) =
fun () -> try writer()
finally compensation()
member this.Using<'d,'W,'T when 'd :> IDisposable and 'd : null>(resource : 'd, body : 'd -> Writer<'W,'T>) : Writer<'W,'T> =
this.TryFinally(body resource, fun () -> match resource with null -> () | disp -> disp.Dispose())
member this.Combine(comp1, comp2) = this.Bind(comp1, fun () -> comp2)
member this.Delay(f) = this.Bind(this.Return (), f)
member this.While(guard, m) =
match guard() with
| true -> this.Bind(m, (fun () -> this.While(guard, m)))
| _ -> this.Zero()
member this.For(sequence:seq<'T>, body:'T -> Writer<'W,unit>) =
this.Using(sequence.GetEnumerator(),
fun enum -> this.While(enum.MoveNext, this.Delay(fun () -> body enum.Current)))
let writer = WriterBuilder(List.monoid<string>)
let tell w = fun () -> ((), w)
let listen m = fun () -> let (a, w) = m() in ((a, w), w)
let pass m = fun () -> let ((a, f), w) = m() in (a, f w)
let listens monoid f m =
let writer = WriterBuilder(monoid)
writer {
let! (a, b) = m
return (a, f b) }
let censor monoid (f:'w1 -> 'w2) (m:Writer<'w1,'T>) : Writer<'w2,'T> =
let writer = WriterBuilder(monoid)
writer { let! a = m
return (a, f)
} |> pass
open Operators
let inline private ret x = returnM writer x
/// Sequentially compose two actions, passing any value produced by the first as an argument to the second.
let inline (>>=) m f = bindM writer m f
/// Flipped >>=
let inline (=<<) f m = bindM writer m f
/// Sequential application
let inline (<*>) f m = applyM writer writer f m
/// Sequential application
let inline ap m f = f <*> m
/// Transforms a Writer value by using a specified mapping function.
let inline map f m = liftM writer f m
/// Infix map
let inline (<!>) f m = map f m
/// Promote a function to a monad/applicative, scanning the monadic/applicative arguments from left to right.
let inline lift2 f a b = ret f <*> a <*> b
/// Sequence actions, discarding the value of the first argument.
let inline ( *>) x y = lift2 (fun _ z -> z) x y
/// Sequence actions, discarding the value of the second argument.
let inline ( <*) x y = lift2 (fun z _ -> z) x y
/// Sequentially compose two state actions, discarding any value produced by the first
let inline (>>.) m f = bindM writer m (fun _ -> f)
/// Left-to-right Kleisli composition
let inline (>=>) f g = fun x -> f x >>= g
/// Right-to-left Kleisli composition
let inline (<=<) x = flip (>=>) x
let foldM f s =
Seq.fold (fun acc t -> acc >>= (flip f) t) (ret s)
let inline sequence s =
let inline cons a b = lift2 List.cons a b
List.foldBack cons s (ret [])
let inline mapM f x = sequence (List.map f x)
module Choice =
/// Inject a value into the Choice type
let returnM = Choice1Of2
/// If Choice is 1Of2, return its value.
/// Otherwise throw ArgumentException.
let get =
function
| Choice1Of2 a -> a
| Choice2Of2 e -> invalidArg "choice" (sprintf "The choice value was Choice2Of2 '%A'" e)
/// Wraps a function, encapsulates any exception thrown within to a Choice
let inline protect f x =
try
Choice1Of2 (f x)
with e -> Choice2Of2 e
/// Attempts to cast an object.
/// Stores the cast value in 1Of2 if successful, otherwise stores the exception in 2Of2
let inline cast (o: obj) = protect unbox o
/// Sequential application
let ap x f =
match f,x with
| Choice1Of2 f, Choice1Of2 x -> Choice1Of2 (f x)
| Choice2Of2 e, _ -> Choice2Of2 e
| _ , Choice2Of2 e -> Choice2Of2 e
/// Sequential application
let inline (<*>) f x = ap x f
/// Transforms a Choice's first value by using a specified mapping function.
let map f =
function
| Choice1Of2 x -> f x |> Choice1Of2
| Choice2Of2 x -> Choice2Of2 x
/// Infix map
let inline (<!>) f x = map f x
/// Promote a function to a monad/applicative, scanning the monadic/applicative arguments from left to right.
let inline lift2 f a b = f <!> a <*> b
/// Sequence actions, discarding the value of the first argument.
let inline ( *>) a b = lift2 (fun _ z -> z) a b
/// Sequence actions, discarding the value of the second argument.
let inline ( <*) a b = lift2 (fun z _ -> z) a b
/// Monadic bind
let bind f =
function
| Choice1Of2 x -> f x
| Choice2Of2 x -> Choice2Of2 x
/// Sequentially compose two actions, passing any value produced by the first as an argument to the second.
let inline (>>=) m f = bind f m
/// Flipped >>=
let inline (=<<) f m = bind f m
/// Sequentially compose two either actions, discarding any value produced by the first
let inline (>>.) m1 m2 = m1 >>= (fun _ -> m2)
/// Left-to-right Kleisli composition
let inline (>=>) f g = fun x -> f x >>= g
/// Right-to-left Kleisli composition
let inline (<=<) x = flip (>=>) x
/// Maps both parts of a Choice.
/// Applies the first function if Choice is 1Of2.
/// Otherwise applies the second function
let inline bimap f1 f2 =
function
| Choice1Of2 x -> Choice1Of2 (f1 x)
| Choice2Of2 x -> Choice2Of2 (f2 x)
/// Maps both parts of a Choice.
/// Applies the first function if Choice is 1Of2.
/// Otherwise applies the second function
let inline choice f1 f2 =
function
| Choice1Of2 x -> f1 x
| Choice2Of2 x -> f2 x
/// Transforms a Choice's second value by using a specified mapping function.
let inline mapSecond f = bimap id f
type EitherBuilder() =
member this.Return a = returnM a
member this.Bind (m, f) = bind f m
member this.ReturnFrom m = m
let choose = EitherBuilder()
/// If Choice is 1Of2, returns Some value. Otherwise, returns None.
let toOption = Option.ofChoice
/// If Some value, returns Choice1Of2 value. Otherwise, returns the supplied default value.
let ofOption o =
function
| Some a -> Choice1Of2 a
| None -> Choice2Of2 o
let foldM f s =
Seq.fold (fun acc t -> acc >>= flip f t) (returnM s)
let inline sequence s =
let inline cons a b = lift2 List.cons a b
List.foldBack cons s (returnM [])
let inline mapM f x = sequence (List.map f x)
#if !PORTABLE
module Validation =
open Choice
open Monoid
let (|Success|Failure|) =
function
| Choice1Of2 a -> Success a
| Choice2Of2 e -> Failure e
/// Sequential application, parameterized by append
let apa append x f =
match f,x with
| Choice1Of2 f, Choice1Of2 x -> Choice1Of2 (f x)
| Choice2Of2 e, Choice1Of2 x -> Choice2Of2 e
| Choice1Of2 f, Choice2Of2 e -> Choice2Of2 e
| Choice2Of2 e1, Choice2Of2 e2 -> Choice2Of2 (append e1 e2)
/// Sequential application, parameterized by semigroup
let inline apm (m: _ ISemigroup) = apa (curry m.Combine)
type CustomValidation<'T>(semigroup: 'T ISemigroup) =
/// Sequential application
member this.ap x = apm semigroup x
/// Promote a function to a monad/applicative, scanning the monadic/applicative arguments from left to right.
member this.lift2 f a b = returnM f |> this.ap a |> this.ap b
/// Sequence actions, discarding the value of the first argument.
member this.apr b a = this.lift2 (fun _ z -> z) a b
/// Sequence actions, discarding the value of the second argument.
member this.apl b a = this.lift2 (fun z _ -> z) a b
member this.seqValidator f =
let inline cons a b = this.lift2 (flip List.cons) a b
Seq.map f >> Seq.fold cons (returnM [])
member this.sequence s =
let inline cons a b = this.lift2 List.cons a b
List.foldBack cons s (returnM [])
member this.mapM f x = this.sequence (List.map f x)
type NonEmptyListSemigroup<'T>() =
interface ISemigroup<'T NonEmptyList> with
member x.Combine(a,b) = NonEmptyList.append a b
type NonEmptyListValidation<'T>() =
inherit CustomValidation<'T NonEmptyList>(NonEmptyListSemigroup<'T>())
/// Sequential application
let inline ap x = apa NonEmptyList.append x
/// Sequential application
let inline (<*>) f x = ap x f
/// Promote a function to a monad/applicative, scanning the monadic/applicative arguments from left to right.
let inline lift2 f a b = returnM f <*> a <*> b
/// Sequence actions, discarding the value of the first argument.
let inline ( *>) x y = lift2 (fun _ z -> z) x y
/// Sequence actions, discarding the value of the second argument.
let inline ( <*) x y = lift2 (fun z _ -> z) x y
let seqValidator f =
let inline cons a b = lift2 (flip List.cons) a b
Seq.map f >> Seq.fold cons (returnM [])
let inline sequence s =
let inline cons a b = lift2 List.cons a b
List.foldBack cons s (returnM [])
let inline mapM f x = sequence (List.map f x)
#endif
#if NET40
module Task =
open System.Threading
open System.Threading.Tasks
/// Task result
type Result<'T> =
/// Task was canceled
| Canceled
/// Unhandled exception in task
| Error of exn
/// Task completed successfully
| Successful of 'T
let run (t: unit -> Task<_>) =
try
let task = t()
task.Result |> Result.Successful
with
| :? OperationCanceledException -> Result.Canceled
| :? AggregateException as e ->
match e.InnerException with
| :? TaskCanceledException -> Result.Canceled
| _ -> Result.Error e
| e -> Result.Error e
let toAsync (t: Task<'T>): Async<'T> =
let abegin (cb: AsyncCallback, state: obj) : IAsyncResult =
match cb with
| null -> upcast t
| cb ->
t.ContinueWith(fun (_ : Task<_>) -> cb.Invoke t) |> ignore
upcast t
let aend (r: IAsyncResult) =
(r :?> Task<'T>).Result
Async.FromBeginEnd(abegin, aend)
/// Transforms a Task's first value by using a specified mapping function.
let inline mapWithOptions (token: CancellationToken) (continuationOptions: TaskContinuationOptions) (scheduler: TaskScheduler) f (m: Task<_>) =
m.ContinueWith((fun (t: Task<_>) -> f t.Result), token, continuationOptions, scheduler)
/// Transforms a Task's first value by using a specified mapping function.
let inline map f (m: Task<_>) =
m.ContinueWith(fun (t: Task<_>) -> f t.Result)
let inline bindWithOptions (token: CancellationToken) (continuationOptions: TaskContinuationOptions) (scheduler: TaskScheduler) (f: 'T -> Task<'U>) (m: Task<'T>) =
m.ContinueWith((fun (x: Task<_>) -> f x.Result), token, continuationOptions, scheduler).Unwrap()
let inline bind (f: 'T -> Task<'U>) (m: Task<'T>) =
m.ContinueWith(fun (x: Task<_>) -> f x.Result).Unwrap()
let inline returnM a =
let s = TaskCompletionSource()
s.SetResult a
s.Task
/// Sequentially compose two actions, passing any value produced by the first as an argument to the second.
let inline (>>=) m f = bind f m
/// Flipped >>=
let inline (=<<) f m = bind f m
/// Sequentially compose two either actions, discarding any value produced by the first
let inline (>>.) m1 m2 = m1 >>= (fun _ -> m2)
/// Left-to-right Kleisli composition
let inline (>=>) f g = fun x -> f x >>= g
/// Right-to-left Kleisli composition
let inline (<=<) x = flip (>=>) x
/// Promote a function to a monad/applicative, scanning the monadic/applicative arguments from left to right.
let inline lift2 f a b =
a >>= fun aa -> b >>= fun bb -> f aa bb |> returnM
/// Sequential application
let inline ap x f = lift2 id f x
/// Sequential application
let inline (<*>) f x = ap x f
/// Infix map
let inline (<!>) f x = map f x
/// Sequence actions, discarding the value of the first argument.
let inline ( *>) a b = lift2 (fun _ z -> z) a b
/// Sequence actions, discarding the value of the second argument.
let inline ( <*) a b = lift2 (fun z _ -> z) a b