FSharpPlus : the fsi blocks when I try to combine two transformers

170 views Asked by At

The fsi blocks when I try to combine these two transformers, without any warning or error message.

open FSharpPlus
open FSharpPlus.Data

type MyError = | Err of string

let f : WriterT<ResultT<Async<Result<_, MyError>>>> = monad {
    do! liftAsync <| Async.Sleep 30
    do! tell ["abc"]
    let! r0 = lift<| ResultT.hoist (Ok 25)
    return r0 + 5
    }

let g = monad {
    let! r1 = f
    do! tell ["def"]
    do! liftAsync <| Async.Sleep 50
    let! r2 = lift<| ResultT.hoist (Ok 2)
    return r1 + r2
    }

let runStackT stack = stack |> WriterT.run |> ResultT.run |> Async.RunSynchronously

#time "on"
let finalResult = runStackT g
#time "off"

What is the problem and how to make this code work?

1

There are 1 answers

2
Gus On

As stated in the comments, this is an F# bug.

Your code is correct, and even if it wasn't there's no reason why the F# compiler should hang.

I've found a workaround, if you change these lines:

...
let g: WriterT<ResultT<Async<Result<_, MyError>>>> = monad {
    let! r1 = f
    let! _ = tell ["def"]
    let! _ = liftAsync <| Async.Sleep 50
    let! r2 = WriterT.Lift <| ResultT.hoist (Ok 2)
    ...

it will work.

Note that changing a do! for a let! _ = is a trick I use frequently to improve type inference.