Skip to content

Commit 661a2c8

Browse files
authored
Improve sequential traversals type inference (#596)
1 parent 547874e commit 661a2c8

File tree

4 files changed

+24
-20
lines changed

4 files changed

+24
-20
lines changed

src/FSharpPlus/Control/Traversable.fs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,19 @@ type Sequence =
1515
static member inline InvokeOnInstance (t: '``Traversable<'Functor<'T>>``) = (^``Traversable<'Functor<'T>>`` : (static member Sequence : _ -> _) t) : '``Functor<'Traversable<'T>>``
1616

1717
[<EditorBrowsable(EditorBrowsableState.Never)>]
18-
static member inline ForInfiniteSequences (t: seq<_>, isFailure, conversion) =
18+
static member inline ForInfiniteSequences (t: seq<_>, [<InlineIfLambda>]isFailure, [<InlineIfLambda>]conversion, [<InlineIfLambda>]result) =
1919
let add x y = y :: x
2020
let mutable go = true
21-
let mutable r = result []
21+
let mutable r = Unchecked.defaultof<_>
22+
let mutable isEmpty = true
2223
use e = t.GetEnumerator ()
2324
while go && e.MoveNext () do
2425
if isFailure e.Current then go <- false
25-
r <- Map.Invoke add r <*> e.Current
26-
Map.Invoke (List.rev >> conversion) r
26+
if isEmpty then r <- Map.Invoke List.singleton e.Current
27+
else r <- Map.Invoke add r <*> e.Current
28+
isEmpty <- false
29+
if isEmpty then result (conversion [])
30+
else Map.Invoke (List.rev >> conversion) r
2731

2832
type Traverse =
2933
inherit Default1
@@ -63,14 +67,14 @@ type Traverse =
6367
Traces.add "Traverse seq, 'T -> Functor<'U>"
6468
#endif
6569
let mapped = Seq.map f t
66-
Sequence.ForInfiniteSequences (mapped, IsLeftZero.Invoke, List.toSeq) : '``Functor<seq<'U>>``
70+
Sequence.ForInfiniteSequences (mapped, IsLeftZero.Invoke, List.toSeq, Return.Invoke) : '``Functor<seq<'U>>``
6771

6872
static member inline Traverse (t: NonEmptySeq<'T>, f: 'T -> '``Functor<'U>``, [<Optional>]_output: '``Functor<NonEmptySeq<'U>>``, [<Optional>]_impl: Default2) =
6973
#if TEST_TRACE
7074
Traces.add "Traverse NonEmptySeq, 'T -> Functor<'U>"
7175
#endif
7276
let mapped = NonEmptySeq.map f t
73-
Sequence.ForInfiniteSequences (mapped, IsLeftZero.Invoke, NonEmptySeq.ofList) : '``Functor<NonEmptySeq<'U>>``
77+
Sequence.ForInfiniteSequences (mapped, IsLeftZero.Invoke, NonEmptySeq.ofList, Return.Invoke) : '``Functor<NonEmptySeq<'U>>``
7478

7579
static member inline Traverse (t: ^a, f, [<Optional>]_output: 'R, [<Optional>]_impl: Default1) : 'R =
7680
#if TEST_TRACE
@@ -186,26 +190,26 @@ type Sequence with
186190
Seq.foldBack cons_f t (result Seq.empty)
187191

188192
static member inline Sequence (t: seq<'``Applicative<'T>``>, [<Optional>]_output: '``Applicative<seq<'T>>`` , [<Optional>]_impl: Default4) : '``Applicative<seq<'T>>`` =
189-
Sequence.ForInfiniteSequences (t, IsLeftZero.Invoke, List.toSeq)
193+
Sequence.ForInfiniteSequences (t, IsLeftZero.Invoke, List.toSeq, Return.Invoke)
190194

191195
static member Sequence (t: seq<option<'t>> , [<Optional>]_output: option<seq<'t>> , [<Optional>]_impl: Default3) : option<seq<'t>> = Option.Sequential t
192196
#if !FABLE_COMPILER
193197
static member Sequence (t: seq<voption<'t>> , [<Optional>]_output: voption<seq<'t>> , [<Optional>]_impl: Default3) : voption<seq<'t>> = ValueOption.Sequential t
194198
#endif
195199
static member Sequence (t: seq<Result<'t,'e>>, [<Optional>]_output: Result<seq<'t>, 'e>, [<Optional>]_impl: Default3) : Result<seq<'t>, 'e> = Result.Sequential t
196200
static member Sequence (t: seq<Choice<'t,'e>>, [<Optional>]_output: Choice<seq<'t>, 'e>, [<Optional>]_impl: Default3) : Choice<seq<'t>, 'e> = Choice.Sequential t
197-
static member Sequence (t: seq<list<'t>> , [<Optional>]_output: list<seq<'t>> , [<Optional>]_impl: Default3) : list<seq<'t>> = Sequence.ForInfiniteSequences (t, List.isEmpty, List.toSeq)
198-
static member Sequence (t: seq<'t []> , [<Optional>]_output: seq<'t> [] , [<Optional>]_impl: Default3) : seq<'t> [] = Sequence.ForInfiniteSequences (t, Array.isEmpty, List.toSeq)
201+
static member Sequence (t: seq<list<'t>> , [<Optional>]_output: list<seq<'t>> , [<Optional>]_impl: Default3) : list<seq<'t>> = Sequence.ForInfiniteSequences (t, List.isEmpty, List.toSeq, List.singleton)
202+
static member Sequence (t: seq<'t []> , [<Optional>]_output: seq<'t> [] , [<Optional>]_impl: Default3) : seq<'t> [] = Sequence.ForInfiniteSequences (t, Array.isEmpty, List.toSeq, Array.singleton)
199203

200204
#if !FABLE_COMPILER
201205
static member Sequence (t: seq<Async<'t>> , [<Optional>]_output: Async<seq<'t>> , [<Optional>]_impl: Default3) : Async<seq<'t>> = Async.SequentialLazy t
202206
#endif
203-
static member inline Sequence (t: NonEmptySeq<'``Applicative<'T>``>, [<Optional>]_output: '``Applicative<NonEmptySeq<'T>>`` , [<Optional>]_impl: Default4) : '``Applicative<NonEmptySeq<'T>>`` = Sequence.ForInfiniteSequences (t, IsLeftZero.Invoke, NonEmptySeq.ofList)
207+
static member inline Sequence (t: NonEmptySeq<'``Applicative<'T>``>, [<Optional>]_output: '``Applicative<NonEmptySeq<'T>>``, [<Optional>]_impl: Default4) : '``Applicative<NonEmptySeq<'T>>`` = Sequence.ForInfiniteSequences (t, IsLeftZero.Invoke, NonEmptySeq.ofList, fun _ -> Unchecked.defaultof<_>)
204208
static member Sequence (t: NonEmptySeq<option<'t>> , [<Optional>]_output: option<NonEmptySeq<'t>> , [<Optional>]_impl: Default3) : option<NonEmptySeq<'t>> = Option.Sequential t |> Option.map NonEmptySeq.unsafeOfSeq
205209
static member Sequence (t: NonEmptySeq<Result<'t,'e>>, [<Optional>]_output: Result<NonEmptySeq<'t>, 'e>, [<Optional>]_impl: Default3) : Result<NonEmptySeq<'t>, 'e> = Result.Sequential t |> Result.map NonEmptySeq.unsafeOfSeq
206210
static member Sequence (t: NonEmptySeq<Choice<'t,'e>>, [<Optional>]_output: Choice<NonEmptySeq<'t>, 'e>, [<Optional>]_impl: Default3) : Choice<NonEmptySeq<'t>, 'e> = Choice.Sequential t |> Choice.map NonEmptySeq.unsafeOfSeq
207-
static member Sequence (t: NonEmptySeq<list<'t>> , [<Optional>]_output: list<NonEmptySeq<'t>> , [<Optional>]_impl: Default3) : list<NonEmptySeq<'t>> = Sequence.ForInfiniteSequences(t, List.isEmpty , NonEmptySeq.ofList)
208-
static member Sequence (t: NonEmptySeq<'t []> , [<Optional>]_output: NonEmptySeq<'t> [] , [<Optional>]_impl: Default3) : NonEmptySeq<'t> [] = Sequence.ForInfiniteSequences(t, Array.isEmpty, NonEmptySeq.ofList)
211+
static member Sequence (t: NonEmptySeq<list<'t>> , [<Optional>]_output: list<NonEmptySeq<'t>> , [<Optional>]_impl: Default3) : list<NonEmptySeq<'t>> = Sequence.ForInfiniteSequences(t, List.isEmpty , NonEmptySeq.ofList, fun _ -> Unchecked.defaultof<_>)
212+
static member Sequence (t: NonEmptySeq<'t []> , [<Optional>]_output: NonEmptySeq<'t> [] , [<Optional>]_impl: Default3) : NonEmptySeq<'t> [] = Sequence.ForInfiniteSequences(t, Array.isEmpty, NonEmptySeq.ofList, fun _ -> Unchecked.defaultof<_>)
209213
#if !FABLE_COMPILER
210214
static member Sequence (t: NonEmptySeq<Async<'t>> , [<Optional>]_output: Async<NonEmptySeq<'t>> , [<Optional>]_impl: Default3) = Async.SequentialLazy t |> Async.map NonEmptySeq.unsafeOfSeq : Async<NonEmptySeq<'t>>
211215
#endif
@@ -217,7 +221,7 @@ type Sequence with
217221
#if !FABLE_COMPILER
218222
static member inline Sequence (t: voption<_>, [<Optional>]_output: 'R, [<Optional>]_impl: Sequence) : 'R = match t with ValueSome x -> Map.Invoke ValueSome x | _ -> result ValueNone
219223
#endif
220-
static member inline Sequence (t: list<_> , [<Optional>]_output: 'R, [<Optional>]_impl: Sequence) : 'R = Sequence.ForInfiniteSequences(t, IsLeftZero.Invoke, id)
224+
static member inline Sequence (t: list<_> , [<Optional>]_output: 'R, [<Optional>]_impl: Sequence) : 'R = Sequence.ForInfiniteSequences(t, IsLeftZero.Invoke, id, Return.Invoke)
221225

222226
static member inline Sequence (t: Map<_,_> , [<Optional>]_output: 'R, [<Optional>]_impl: Sequence) : 'R =
223227
let insert_f k x ys = Map.Invoke (Map.add k) x <*> ys
@@ -233,7 +237,7 @@ type Sequence with
233237
| Choice1Of2 a -> Map.Invoke Choice<'T,'Error>.Choice1Of2 a
234238
| Choice2Of2 e -> Return.Invoke (Choice<'T,'Error>.Choice2Of2 e)
235239

236-
static member inline Sequence (t: _ [] , [<Optional>]_output: 'R , [<Optional>]_impl: Sequence) : 'R = Sequence.ForInfiniteSequences(t, IsLeftZero.Invoke, Array.ofList)
240+
static member inline Sequence (t: _ [] , [<Optional>]_output: 'R , [<Optional>]_impl: Sequence) : 'R = Sequence.ForInfiniteSequences(t, IsLeftZero.Invoke, Array.ofList, Return.Invoke)
237241

238242
static member inline Sequence (t: Id<'``Functor<'T>``> , [<Optional>]_output: '``Functor<Id<'T>>`` , [<Optional>]_impl: Sequence) : '``Functor<Id<'T>>`` = Traverse.Invoke id t
239243

tests/FSharpPlus.Tests/Asyncs.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ module Async =
103103

104104
let t123 = Async.map3 (fun x y z -> [x; y; z]) t1 t2 t3
105105
let t123' = transpose [t1; t2; t3]
106-
let t123'' = sequence [t1; t2; t3] : Async<int list>
106+
let t123'' = sequence [t1; t2; t3]
107107
CollectionAssert.AreEquivalent ((Async.AsTaskAndWait t123).Exception.InnerExceptions, (Async.AsTaskAndWait t123').Exception.InnerExceptions, "Async.map3 (fun x y z -> [x; y; z]) t1 t2 t3 is the same as transpose [t1; t2; t3]")
108108
CollectionAssert.AreNotEquivalent ((Async.AsTaskAndWait t123).Exception.InnerExceptions, (Async.AsTaskAndWait t123'').Exception.InnerExceptions, "Async.map3 (fun x y z -> [x; y; z]) t1 t2 t3 is not the same as sequence [t1; t2; t3]")
109109

tests/FSharpPlus.Tests/Traversals.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ module Traversable =
115115
// It hangs if we try to share this value between tests
116116
let expectedEffects =
117117
[
118-
"""f(x) <*> Right 0"""
118+
// map does this -> """f(x) <*> Right 0"""
119119
"""f(x) <*> Right 1"""
120120
"""f(x) <*> Right 2"""
121121
"""f(x) <*> Right 3"""
@@ -180,7 +180,7 @@ module Traversable =
180180
// It hangs if we try to share this value between tests
181181
let expectedEffects =
182182
[
183-
"""f(x) <*> Right 0"""
183+
// map does this -> """f(x) <*> Right 0"""
184184
"""f(x) <*> Right 1"""
185185
"""f(x) <*> Right 2"""
186186
"""f(x) <*> Right 3"""

tests/FSharpPlusFable.Tests/FSharpTests/General/Traversable.fs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ let traversable = testList "Traversable" [
135135

136136
let expectedEffects =
137137
[
138-
"""f(x) <*> Right 0"""
138+
// map does this -> """f(x) <*> Right 0"""
139139
"""f(x) <*> Right 1"""
140140
"""f(x) <*> Right 2"""
141141
"""f(x) <*> Right 3"""
@@ -243,7 +243,7 @@ let traversable = testList "Traversable" [
243243
testCase "e" (fun () ->
244244
let expectedEffects =
245245
[
246-
"""f(x) <*> Right 0"""
246+
// map does this -> """f(x) <*> Right 0"""
247247
"""f(x) <*> Right 1"""
248248
"""f(x) <*> Right 2"""
249249
"""f(x) <*> Right 3"""
@@ -262,7 +262,7 @@ let traversable = testList "Traversable" [
262262
testCase "f" (fun () ->
263263
let expectedEffects =
264264
[
265-
"""f(x) <*> Right 0"""
265+
// map does this -> """f(x) <*> Right 0"""
266266
"""f(x) <*> Right 1"""
267267
"""f(x) <*> Right 2"""
268268
"""f(x) <*> Right 3"""

0 commit comments

Comments
 (0)