Skip to content

Commit 737b97c

Browse files
authored
Make ErrorT's more foldable (#534)
1 parent 197b697 commit 737b97c

File tree

4 files changed

+66
-10
lines changed

4 files changed

+66
-10
lines changed

docsrc/content/abstraction-alternative.fsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ From F#+
6666
- [``ReaderT<'R, 'MonadPlus<'T>>``](type-readert.html)
6767
- [``WriterT<'MonadPlus<'T * 'Monoid>>``](type-writert.html)
6868
- [``StateT<'S,'MonadPlus<'T * 'S>>``](type-statet.html)
69+
- [``OptionT<'MonadPlus<option<'T>>>``](type-optiont.html)
70+
- [``ValueOptionT<'MonadPlus<voption<'T>>>``](type-valueoptiont.html)
71+
- [``ResultT<'MonadPlus<Result<'T,'Monoid>>>``](type-resultt.html)
72+
- [``ChoiceT<'MonadPlus<Choice<'T,'Monoid>>>``](type-choicet.html)
6973
- [``Compose<'AlternativeF<'AlternativeG<'T>>>``](type-compose.html)
7074
- [``DList<'T>``](type-dlist.html)
7175
- [``ZipList<'S>``](type-ziplist.html)

src/FSharpPlus/Data/Error.fs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,24 @@ type ResultT<'``monad<Result<'t,'e>>``> with
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
7474

75+
static member inline get_Zero () : ResultT<'``MonadPlus<Result<'U,'E>>``> = ResultT <| result (Error zero)
76+
static member inline (+) (ResultT x, ResultT y) : ResultT<'``MonadPlus<Result<'U,'E>>``> =
77+
ResultT <| (x >>= function
78+
| Ok x -> y >>= function
79+
| Ok y -> result (Ok (x ++ y))
80+
| Error _ -> result (Ok x)
81+
| Error x -> y >>= function
82+
| Ok y -> result (Ok y)
83+
| Error y -> result (Error (x ++ y)))
84+
85+
static member inline get_Empty () : ResultT<'``MonadPlus<Result<'U,'E>>``> = ResultT <| result (Error zero)
86+
static member inline (<|>) (ResultT x, ResultT y) : ResultT<'``MonadPlus<Result<'U,'E>>``> =
87+
ResultT <| (x >>= function
88+
| Ok value -> result (Ok value)
89+
| Error x -> y >>= function
90+
| Ok value -> result (Ok value)
91+
| Error y -> result (Error (x ++ y)))
92+
7593
static member inline TryWith (source: ResultT<'``Monad<Result<'T,'E>>``>, f: exn -> ResultT<'``Monad<Result<'T,'E>>``>) = ResultT (TryWith.Invoke (ResultT.run source) (ResultT.run << f))
7694
static member inline TryFinally (computation: ResultT<'``Monad<Result<'T,'E>>``>, f) = ResultT (TryFinally.Invoke (ResultT.run computation) f)
7795
static member inline Using (resource, f: _ -> ResultT<'``Monad<Result<'T,'E>>``>) = ResultT (Using.Invoke resource (ResultT.run << f))
@@ -137,6 +155,24 @@ type ChoiceT<'``monad<Choice<'t,'e>>``> with
137155
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>>``>
138156
static member inline (>>=) (x: ChoiceT<'``Monad<Choice<'T,'E>>``>, f: 'T->ChoiceT<'``Monad<Choice<'U,'E>>``>) = ChoiceT.bind f x
139157

158+
static member inline get_Zero () : ChoiceT<'``MonadPlus<Choice<'U,'E>>``> = ChoiceT <| result (Choice2Of2 zero)
159+
static member inline (+) (ChoiceT x, ChoiceT y) : ChoiceT<'``MonadPlus<Choice<'U,'E>>``> =
160+
ChoiceT <| (x >>= function
161+
| Choice1Of2 x -> y >>= function
162+
| Choice1Of2 y -> result (Choice1Of2 (x ++ y))
163+
| Choice2Of2 _ -> result (Choice1Of2 x)
164+
| Choice2Of2 x -> y >>= function
165+
| Choice1Of2 y -> result (Choice1Of2 y)
166+
| Choice2Of2 y -> result (Choice2Of2 (x ++ y)))
167+
168+
static member inline get_Empty () : ChoiceT<'``MonadPlus<Choice<'U,'E>>``> = ChoiceT <| result (Choice2Of2 zero)
169+
static member inline (<|>) (ChoiceT x, ChoiceT y) : ChoiceT<'``MonadPlus<Choice<'U,'E>>``> =
170+
ChoiceT <| (x >>= function
171+
| Choice1Of2 value -> result (Choice1Of2 value)
172+
| Choice2Of2 x -> y >>= function
173+
| Choice1Of2 value -> result (Choice1Of2 value)
174+
| Choice2Of2 y -> result (Choice2Of2 (x ++ y)))
175+
140176
static member inline TryWith (source: ChoiceT<'``Monad<Choice<'T,'E>>``>, f: exn -> ChoiceT<'``Monad<Choice<'T,'E>>``>) = ChoiceT (TryWith.Invoke (ChoiceT.run source) (ChoiceT.run << f))
141177
static member inline TryFinally (computation: ChoiceT<'``Monad<Choice<'T,'E>>``>, f) = ChoiceT (TryFinally.Invoke (ChoiceT.run computation) f)
142178
static member inline Using (resource, f: _ -> ChoiceT<'``Monad<Choice<'T,'E>>``>) = ChoiceT (Using.Invoke resource (ChoiceT.run << f))

src/FSharpPlus/Data/Option.fs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,19 @@ type OptionT<'``monad<option<'t>>``> with
4949
[<EditorBrowsable(EditorBrowsableState.Never)>]
5050
static member inline Lift3 (f: 'T->'U->'V->'W, x: OptionT<'``Monad<option<'T>``>, y: OptionT<'``Monad<option<'U>``>, z: OptionT<'``Monad<option<'W>``>) = OptionT.map3 f x y z : OptionT<'``Monad<option<'W>``>
5151

52-
static member inline (<*>) (f: OptionT<'``Monad<option<('T -> 'U)>``>, x: OptionT<'``Monad<option<'T>``>) = OptionT.apply f x : OptionT<'``Monad<option<'U>``>
53-
static member inline (>>=) (x: OptionT<'``Monad<option<'T>``>, f: 'T -> OptionT<'``Monad<option<'U>``>) = OptionT.bind f x
54-
55-
static member inline get_Empty () = OptionT <| result None : OptionT<'``MonadPlus<option<'T>``>
56-
static member inline (<|>) (OptionT x, OptionT y) = OptionT <| (x >>= (fun maybe_value -> match maybe_value with Some value -> result (Some value) | _ -> y)) : OptionT<'``MonadPlus<option<'T>``>
52+
static member inline (<*>) (f: OptionT<'``Monad<option<('T -> 'U)>``>, x: OptionT<'``Monad<option<'T>``>) = OptionT.apply f x : OptionT<'``Monad<option<'U>``>
53+
static member inline (>>=) (x: OptionT<'``Monad<option<'T>``>, f: 'T -> OptionT<'``Monad<option<'U>``>) = OptionT.bind f x
54+
55+
static member inline get_Zero () : OptionT<'``MonadPlus<option<'T>``> = OptionT <| result None
56+
static member inline (+) (OptionT x, OptionT y) : OptionT<'``MonadPlus<option<'T>``> =
57+
OptionT <| (x >>= function
58+
| None -> y
59+
| Some x -> y >>= function
60+
| None -> result (Some x)
61+
| Some y -> result (Some (x ++ y)))
62+
63+
static member inline get_Empty () : OptionT<'``MonadPlus<option<'T>``> = OptionT <| result None
64+
static member inline (<|>) (OptionT x, OptionT y) : OptionT<'``MonadPlus<option<'T>``> = OptionT <| (x >>= function Some value -> result (Some value) | _ -> y)
5765

5866
static member inline TryWith (source: OptionT<'``Monad<option<'T>>``>, f: exn -> OptionT<'``Monad<option<'T>>``>) = OptionT (TryWith.Invoke (OptionT.run source) (OptionT.run << f))
5967
static member inline TryFinally (computation: OptionT<'``Monad<option<'T>>``>, f) = OptionT (TryFinally.Invoke (OptionT.run computation) f)

src/FSharpPlus/Data/ValueOption.fs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,19 @@ type ValueOptionT<'``monad<voption<'t>>``> with
4949
[<EditorBrowsable(EditorBrowsableState.Never)>]
5050
static member inline Lift3 (f: 'T->'U->'V->'W, x: ValueOptionT<'``Monad<voption<'T>``>, y: ValueOptionT<'``Monad<voption<'U>``>, z: ValueOptionT<'``Monad<voption<'W>``>) = ValueOptionT.map3 f x y z : ValueOptionT<'``Monad<voption<'W>``>
5151

52-
static member inline (<*>) (f: ValueOptionT<'``Monad<voption<('T -> 'U)>``>, x: ValueOptionT<'``Monad<voption<'T>``>) = ValueOptionT.apply f x : ValueOptionT<'``Monad<voption<'U>``>
53-
static member inline (>>=) (x: ValueOptionT<'``Monad<voption<'T>``>, f: 'T -> ValueOptionT<'``Monad<voption<'U>``>) = ValueOptionT.bind f x
54-
55-
static member inline get_Empty () = ValueOptionT <| result ValueNone : ValueOptionT<'``MonadPlus<voption<'T>``>
56-
static member inline (<|>) (ValueOptionT x, ValueOptionT y) = ValueOptionT <| (x >>= (fun maybe_value -> match maybe_value with ValueSome value -> result (ValueSome value) | _ -> y)) : ValueOptionT<'``MonadPlus<voption<'T>``>
52+
static member inline (<*>) (f: ValueOptionT<'``Monad<voption<('T -> 'U)>``>, x: ValueOptionT<'``Monad<voption<'T>``>) = ValueOptionT.apply f x : ValueOptionT<'``Monad<voption<'U>``>
53+
static member inline (>>=) (x: ValueOptionT<'``Monad<voption<'T>``>, f: 'T -> ValueOptionT<'``Monad<voption<'U>``>) = ValueOptionT.bind f x
54+
55+
static member inline get_Zero () : ValueOptionT<'``MonadPlus<voption<'T>``> = ValueOptionT <| result ValueNone
56+
static member inline (+) (ValueOptionT x, ValueOptionT y) : ValueOptionT<'``MonadPlus<voption<'T>``> =
57+
ValueOptionT <| (x >>= function
58+
| ValueNone -> y
59+
| ValueSome x -> y >>= function
60+
| ValueNone -> result (ValueSome x)
61+
| ValueSome y -> result (ValueSome (x ++ y)))
62+
63+
static member inline get_Empty () : ValueOptionT<'``MonadPlus<voption<'T>``> = ValueOptionT <| result ValueNone
64+
static member inline (<|>) (ValueOptionT x, ValueOptionT y) : ValueOptionT<'``MonadPlus<voption<'T>``> = ValueOptionT <| (x >>= function ValueSome value -> result (ValueSome value) | _ -> y)
5765

5866
static member inline TryWith (source: ValueOptionT<'``Monad<voption<'T>>``>, f: exn -> ValueOptionT<'``Monad<voption<'T>>``>) = ValueOptionT (TryWith.Invoke (ValueOptionT.run source) (ValueOptionT.run << f))
5967
static member inline TryFinally (computation: ValueOptionT<'``Monad<voption<'T>>``>, f) = ValueOptionT (TryFinally.Invoke (ValueOptionT.run computation) f)

0 commit comments

Comments
 (0)