Given the following type and member function
type Result<'TSuccess, 'TError> = | Success of 'TSuccess | Error of 'TError list with member this.apply fn = match (fn, this) with | Success(f), Success(x) -> Success(fx) | Error(e), Success(_) -> Error(e) | Success(_), Error(e) -> Error(e) | Error(e1), Error(e2) -> Error(List.concat [e1;e2])
and the following built-in function
let inline (<*>) (f: ^A) (t:^A) = let apply' = (^A : (member apply : ^A -> ^A) (t, f)) apply'
And this call site
let y () = Success (fun x -> x + 1) <*> (Success 3)
I get the following error
let y () = Success (fun x -> x + 1) <*> (Success 3);; -----------^^^^^^^^^^^^^^^^^^^^^^^^ /Users/robkuz/stdin(473,12): error FS0001: Type constraint mismatch. The type Result<'a,'c> is not compatible with type Result<('a -> 'b),'c> The resulting type would be infinite when unifying ''a' and ''a -> 'b'
All this is an attempt to imitate the Haskells Applicative, and the signature should be
(<*>) :: forall fa b. Apply f => f (a -> b) -> fa -> fb
But I do not know that there is no way to express this in F #
Any ideas on how to do this?
source share