Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation as a ZipFunctor? #280

Closed
NickDarvey opened this issue Feb 25, 2020 · 2 comments
Closed

Validation as a ZipFunctor? #280

NickDarvey opened this issue Feb 25, 2020 · 2 comments

Comments

@NickDarvey
Copy link
Contributor

I'm not even sure if I'm phrasing this right, but, is Validation a ZipFunctor? And would this be a valid implementation of it?

module Validation =
  let zip = liftA2 tuple2

I can open a PR if it actually is a zippable functor.

@gusty
Copy link
Member

gusty commented Feb 25, 2020

That's a very interesting question.

Unfortunately I think the answer is No and I'll tell you why:

Let's suppose Validation is a ZipFunctor which means that it has a zip operation which is the inverse of Functor's unzip (which is defined as unzip s = map fst s, map snd s).

Then we can do:

> zip (Validation<string,int>.Success 42) (Validation<string,float>.Failure "Error") ;;
val it : Validation<string,(int * float)> = Failure "Error"

and now

> unzip it ;;
val it : Validation<string,int> * Validation<string,float> =
  (Failure "Error", Failure "Error")

but now

> it ||> zip;;
val it : Validation<string,(int * float)> = Failure "ErrorError"

so we came back to a different value !

Note however that Result seems to be well behaved in this regard:

> zip (Result<int,string>.Ok 42) (Result<float,string>.Error "No") ;;
[<Struct>]
val it : Result<(int * float),string> = Error "No"

> unzip it ;;
val it : Result<int,string> * Result<float,string> = (Error "No", Error "No")

> it ||> zip;;
[<Struct>]
val it : Result<(int * float),string> = Error "No"

Does it makes sense?

@NickDarvey
Copy link
Contributor Author

That does make sense. Thank you for the accessible explanation @gusty 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants