Skip to content

Commit 05eeee1

Browse files
committed
Attach operator >=> directly to monad types
1 parent 737b97c commit 05eeee1

File tree

8 files changed

+86
-9
lines changed

8 files changed

+86
-9
lines changed

src/FSharpPlus/Data/Cont.fs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ type Cont<'r,'t> with
7575
static member (<* ) (x: Cont<'R, 'U>, y: Cont<'R, 'T>) : Cont<'R, 'U> = ((fun (k: 'U) (_: 'T) -> k ) </Cont.map/> x : Cont<'R, 'T->'U>) </Cont.apply/> y
7676

7777
static member (>>=) (x, f: 'T->_) = Cont.bind f x : Cont<'R,'U>
78+
79+
/// <summary>
80+
/// Composes left-to-right two Cont functions (Kleisli composition).
81+
/// </summary>
82+
/// <category index="2">Monad</category>
83+
static member (>=>) (f, (g: 'U -> _)) : 'T -> Cont<'R, 'V> = fun x -> Cont.bind g (f x)
7884

7985
static member Delay f = Cont (fun k -> Cont.run (f ()) k) : Cont<'R,'T>
8086
static member TryWith (Cont c, h) = Cont (fun k -> try (c k) with e -> Cont.run (h e) k) : Cont<'R,'T>

src/FSharpPlus/Data/Error.fs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ type ResultT<'``monad<Result<'t,'e>>``> with
7171

7272
static member inline (<*>) (f: ResultT<'``Monad<Result<('T -> 'U),'E>>``>, x: ResultT<'``Monad<Result<'T,'E>>``>) = ResultT.apply f x : ResultT<'``Monad<Result<'U,'E>>``>
7373
static member inline (>>=) (x: ResultT<'``Monad<Result<'T,'E>>``>, f: 'T->ResultT<'``Monad<Result<'U,'E>>``>) = ResultT.bind f x
74+
75+
/// <summary>
76+
/// Composes left-to-right two Result functions (Kleisli composition).
77+
/// </summary>
78+
/// <category index="2">Monad</category>
79+
static member inline (>=>) (f: 'T -> ResultT<'``Monad<Result<'U, 'E>``>, g: 'U -> ResultT<'``Monad<Result<'V, 'E>``>) : 'T -> ResultT<'``Monad<Result<'V, 'E>``> = fun x -> ResultT.bind g (f x)
7480

7581
static member inline get_Zero () : ResultT<'``MonadPlus<Result<'U,'E>>``> = ResultT <| result (Error zero)
7682
static member inline (+) (ResultT x, ResultT y) : ResultT<'``MonadPlus<Result<'U,'E>>``> =
@@ -154,6 +160,12 @@ type ChoiceT<'``monad<Choice<'t,'e>>``> with
154160

155161
static member inline (<*>) (f: ChoiceT<'``Monad<Choice<('T -> 'U),'E>>``>, x: ChoiceT<'``Monad<Choice<'T,'E>>``>) = ChoiceT.apply f x : ChoiceT<'``Monad<Choice<'U,'E>>``>
156162
static member inline (>>=) (x: ChoiceT<'``Monad<Choice<'T,'E>>``>, f: 'T->ChoiceT<'``Monad<Choice<'U,'E>>``>) = ChoiceT.bind f x
163+
164+
/// <summary>
165+
/// Composes left-to-right two Choice functions (Kleisli composition).
166+
/// </summary>
167+
/// <category index="2">Monad</category>
168+
static member inline (>=>) (f: 'T -> ChoiceT<'``Monad<Choice<'U, 'E>``>, g: 'U -> ChoiceT<'``Monad<Choice<'V, 'E>``>) : 'T -> ChoiceT<'``Monad<Choice<'V, 'E>``> = fun x -> ChoiceT.bind g (f x)
157169

158170
static member inline get_Zero () : ChoiceT<'``MonadPlus<Choice<'U,'E>>``> = ChoiceT <| result (Choice2Of2 zero)
159171
static member inline (+) (ChoiceT x, ChoiceT y) : ChoiceT<'``MonadPlus<Choice<'U,'E>>``> =

src/FSharpPlus/Data/Identity.fs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,30 @@
1010
/// Any monad transformer applied to the Identity monad yields a non-transformer version of that monad.
1111
/// Its applicative instance plays a fundamental role in Lens. </summary>
1212
[<Struct>]
13-
type Identity<'t> = Identity of 't with
13+
type Identity<'t> = Identity of 't
14+
15+
/// Basic operations on Identity
16+
[<RequireQualifiedAccess>]
17+
module Identity =
18+
let run (Identity x) = x : 'T
19+
let apply (Identity (f : 'T -> 'U)) (Identity (x: 'T)) : Identity<'U> = Identity (f x)
20+
let bind (f: 'T -> Identity<'U>) (Identity x) : Identity<'U> = f x
21+
22+
type Identity<'t> with
23+
1424
static member Return x = Identity x : Identity<'T>
15-
static member (>>=) (Identity x, f :'T -> Identity<'U>) = f x : Identity<'U>
16-
static member (<*>) (Identity (f : 'T->'U), Identity (x : 'T)) = Identity (f x) : Identity<'U>
25+
1726
static member Lift2 (f, Identity (x: 'T), Identity (y: 'U)) = Identity (f x y) : Identity<'V>
18-
static member Lift3 (f, Identity (x: 'T), Identity (y: 'U), Identity (z: 'V)) = Identity (f x y z) : Identity<'W>
27+
static member Lift3 (f, Identity (x: 'T), Identity (y: 'U), Identity (z: 'V)) : Identity<'W> = Identity (f x y z)
1928
static member Map (Identity x, f : 'T->'U) = Identity (f x) : Identity<'U>
2029
static member Zip (Identity x, Identity y) = Identity (x, y) : Identity<'T * 'U>
2130

22-
/// Basic operations on Identity
23-
[<RequireQualifiedAccess>]
24-
module Identity =
25-
let run (Identity x) = x : 'T
31+
static member (<*>) (Identity (f: 'T -> 'U), Identity (x: 'T)) : Identity<'U> = Identity (f x)
32+
33+
static member (>>=) (Identity x, f: 'T -> Identity<'U>) : Identity<'U> = f x
34+
35+
/// <summary>
36+
/// Composes left-to-right two Id functions (Kleisli composition).
37+
/// </summary>
38+
/// <category index="2">Monad</category>
39+
static member (>=>) (f, (g: 'U -> _)) : 'T -> Identity<'V> = fun x -> Identity.bind g (f x)

src/FSharpPlus/Data/Option.fs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ type OptionT<'``monad<option<'t>>``> with
5252
static member inline (<*>) (f: OptionT<'``Monad<option<('T -> 'U)>``>, x: OptionT<'``Monad<option<'T>``>) = OptionT.apply f x : OptionT<'``Monad<option<'U>``>
5353
static member inline (>>=) (x: OptionT<'``Monad<option<'T>``>, f: 'T -> OptionT<'``Monad<option<'U>``>) = OptionT.bind f x
5454

55+
/// <summary>
56+
/// Composes left-to-right two Option functions (Kleisli composition).
57+
/// </summary>
58+
/// <category index="2">Monad</category>
59+
static member inline (>=>) (f: 'T -> OptionT<'``Monad<option<'U>``>, g: 'U -> OptionT<'``Monad<option<'V>``>) : 'T -> OptionT<'``Monad<option<'V>``> = fun x -> OptionT.bind g (f x)
60+
5561
static member inline get_Zero () : OptionT<'``MonadPlus<option<'T>``> = OptionT <| result None
5662
static member inline (+) (OptionT x, OptionT y) : OptionT<'``MonadPlus<option<'T>``> =
5763
OptionT <| (x >>= function

src/FSharpPlus/Data/Reader.fs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,12 @@ type ReaderT<'r,'``monad<'t>``> with
161161
/// <category index="2">Monad</category>
162162
static member inline (>>=) (x: ReaderT<_,'``Monad<'T>``>, f: 'T->ReaderT<'R,'``Monad<'U>``>) = ReaderT.bind f x : ReaderT<'R, '``Monad<'U>``>
163163

164+
/// <summary>
165+
/// Composes left-to-right two Reader functions (Kleisli composition).
166+
/// </summary>
167+
/// <category index="2">Monad</category>
168+
static member (>=>) (f, (g: 'U -> _)) : 'T -> Reader<'R, 'V> = fun x -> Reader.bind g (f x)
169+
164170
/// <summary>
165171
/// Composes left-to-right two Reader functions (Kleisli composition).
166172
/// </summary>

src/FSharpPlus/Data/State.fs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,14 @@ type State<'s,'t> with
6868

6969
static member Return a = State (fun s -> (a, s)) : State<'S,'T>
7070
static member (>>=) (x, f: 'T->_) = State.bind f x : State<'S,'U>
71-
static member (<*>) (f, x: State<'S,'T>) = State.apply f x : State<'S,'U>
71+
72+
/// <summary>
73+
/// Composes left-to-right two State functions (Kleisli composition).
74+
/// </summary>
75+
/// <category index="2">Monad</category>
76+
static member (>=>) (f, (g: 'U -> _)) : 'T -> State<'S, 'V> = fun x -> State.bind g (f x)
77+
78+
static member (<*>) (f, x: State<'S,'T>) = State.apply f x : State<'S,'U>
7279

7380
/// <summary>
7481
/// Sequences two States left-to-right, discarding the value of the first argument.
@@ -172,6 +179,12 @@ type StateT<'s,'``monad<'t * 's>``> with
172179

173180
static member inline (>>=) (x: StateT<'S,'``Monad<'T * 'S>``>, f: 'T->StateT<'S,'``Monad<'U * 'S>``>) = StateT.bind f x
174181

182+
/// <summary>
183+
/// Composes left-to-right two State functions (Kleisli composition).
184+
/// </summary>
185+
/// <category index="2">Monad</category>
186+
static member inline (>=>) (f: 'T -> StateT<'S, '``Monad<'U * 'S>``>, g: 'U -> StateT<'S, '``Monad<'V * 'S>``>) : 'T -> StateT<'S, '``Monad<'V * 'S>``> = fun x -> StateT.bind g (f x)
187+
175188
static member inline get_Empty () = StateT (fun _ -> getEmpty ()) : StateT<'S,'``MonadPlus<'T * 'S>``>
176189
static member inline (<|>) (StateT m, StateT n) = StateT (fun s -> m s <|> n s) : StateT<'S,'``MonadPlus<'T * 'S>``>
177190

src/FSharpPlus/Data/ValueOption.fs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ type ValueOptionT<'``monad<voption<'t>>``> with
5252
static member inline (<*>) (f: ValueOptionT<'``Monad<voption<('T -> 'U)>``>, x: ValueOptionT<'``Monad<voption<'T>``>) = ValueOptionT.apply f x : ValueOptionT<'``Monad<voption<'U>``>
5353
static member inline (>>=) (x: ValueOptionT<'``Monad<voption<'T>``>, f: 'T -> ValueOptionT<'``Monad<voption<'U>``>) = ValueOptionT.bind f x
5454

55+
/// <summary>
56+
/// Composes left-to-right two option functions (Kleisli composition).
57+
/// </summary>
58+
/// <category index="2">Monad</category>
59+
static member inline (>=>) (f: 'T -> ValueOptionT<'``Monad<voption<'U>``>, g: 'U -> ValueOptionT<'``Monad<voption<'V>``>) : 'T -> ValueOptionT<'``Monad<voption<'V>``> = fun x -> ValueOptionT.bind g (f x)
60+
61+
5562
static member inline get_Zero () : ValueOptionT<'``MonadPlus<voption<'T>``> = ValueOptionT <| result ValueNone
5663
static member inline (+) (ValueOptionT x, ValueOptionT y) : ValueOptionT<'``MonadPlus<voption<'T>``> =
5764
ValueOptionT <| (x >>= function

src/FSharpPlus/Data/Writer.fs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ type Writer<'monoid,'t> with
7878
#endif
7979

8080
static member inline (>>=) (x, f: 'T->_) = Writer.bind f x : Writer<'Monoid,'U>
81+
82+
/// <summary>
83+
/// Composes left-to-right two Writer functions (Kleisli composition).
84+
/// </summary>
85+
/// <category index="2">Monad</category>
86+
static member inline (>=>) (f, (g: 'U -> _)) : 'T -> Writer<'Monoid, 'V> = fun x -> Writer.bind g (f x)
87+
8188
static member inline (<*>) (f, x: Writer<_,'T>) = Writer.apply f x : Writer<'Monoid,'U>
8289

8390
/// <summary>
@@ -169,6 +176,12 @@ type WriterT<'``monad<'t * 'monoid>``> with
169176

170177
static member inline (>>=) (x: WriterT<'``Monad<'T * 'Monoid>``>, f: 'T -> _) = WriterT.bind f x : WriterT<'``Monad<'U * 'Monoid>``>
171178

179+
/// <summary>
180+
/// Composes left-to-right two Writer functions (Kleisli composition).
181+
/// </summary>
182+
/// <category index="2">Monad</category>
183+
static member inline (>=>) (f: 'T -> WriterT<'``Monad<'U * 'Monoid>``>, g: 'U -> WriterT<'``Monad<'V * 'Monoid>``>) : 'T -> WriterT<'``Monad<'V * 'Monoid>``> = fun x -> WriterT.bind g (f x)
184+
172185
static member inline get_Empty () = WriterT (getEmpty ()) : WriterT<'``MonadPlus<'T * 'Monoid>``>
173186
static member inline (<|>) (WriterT m, WriterT n) = WriterT (m <|> n) : WriterT<'``MonadPlus<'T * 'Monoid>``>
174187

0 commit comments

Comments
 (0)