Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/FSharpPlus/Data/Free.fs
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ module Free =
loop y x

let inline map3 (f: 'T->'U->'V->'W) (x: Free<'``Functor<'T>``,'T>) (y: Free<'``Functor<'U>``,'U>) (z: Free<'``Functor<'V>``,'V>) : Free<'``Functor<'W>``,'W> =
let rec loop (y: Free<_,_>) (x: Free<_,_>) (z: Free<_,_>) =
let rec loop (y: Free<_,_>) (z: Free<_,_>) (x: Free<_,_>) =
match run x with
| Pure x -> map2<'U,'V,'W,'``Functor<'U>``,'``Functor<Free<'Functor<'U>,'U>>``,'``Functor<Free<'Functor<'V>,'V>>``,'``Functor<Free<'Functor<'W>,'W>>``,'``Functor<'V>``,'``Functor<'W>``> (f x) y z : Free<'``Functor<'W>``,'W>
| Roll (x: ^``Functor<Free<'Functor<'T>,'T>>``) -> Roll (Map.Invoke (loop y: Free<'``Functor<'T>``,'T> -> _) x: '``Functor<Free<'Functor<'W>,'W>>``)
loop y x z
| Pure x -> map2<'U,'V,'W,'``Functor<'U>``,'``Functor<Free<'Functor<'U>,'U>>``,'``Functor<Free<'Functor<'W>,'W>>``,'``Functor<Free<'Functor<'V>,'V>>``,'``Functor<'V>``,'``Functor<'W>``> (f x) y z : Free<'``Functor<'W>``,'W>
| Roll (x: ^``Functor<Free<'Functor<'T>,'T>>``) -> Roll (Map.Invoke (loop y z: Free<'``Functor<'T>``,'T> -> _) x: '``Functor<Free<'Functor<'W>,'W>>``)
loop y z x

/// Folds the Free structure into a Monad
let inline fold (f: '``Functor<'T>`` -> '``Monad<'T>``) (x: Free<'``Functor<'U>``,'U>) : '``Monad<'U>`` =
Expand Down Expand Up @@ -105,4 +105,4 @@ type Free<'``functor<'t>``,'t> with

static member Delay (x: unit -> Free<'``Functor<'T>``,'T>) = x ()

#endif
#endif
36 changes: 36 additions & 0 deletions tests/FSharpPlus.Tests/Free.fs
Original file line number Diff line number Diff line change
Expand Up @@ -316,3 +316,39 @@ module Fold =
|> Identity.run

areStEqual (Ok { Id = FooId "1"; Name = "test" }) response

module Lift3 =

type Instruction<'next> =
| Read of int * (string -> 'next)
static member Map(i, f) =
match i with
| Read (x, next) -> Read(x, next >> f)

let read x = Read(x, id) |> Free.liftF

type ApplicativeBuilder<'a>() =
inherit MonadFxStrictBuilder<'a>()

member inline _.BindReturn(x, f) = map f x

let applicative<'a> = ApplicativeBuilder<'a>()

[<Test>]
let ``should be able to use applicative CE which requires Lift3`` () =
let program =
applicative {
let! a = read 1
and! b = read 2
and! c = read 3
return a, b, c
}

let result =
program
|> Free.fold
(function
| Read (i, next) -> i |> string |> next |> result)
|> Identity.run

areStEqual result ("1", "2", "3")