You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello there. I was a little bit surprised by the behavior of the Alt instance in this library, which goes like this:
--| For two errors, this instance reports only the last of them.instanceAlt (Validationerr) whereFailure _ <!> x =
x
Success a <!> _ =Success a
This is lawful in the sense that it is associative, but then the constant semigroup is a lawful semigroup implementation for any type whatsoever. For the same reason the constant semigroup cannot be a monoid, this instance also prevents us from having a lawful Alternative instance.
I originally got my wires crossed and started looking at the Alternative instance in the wrong library:
instanceMonoiderr=>Alternative (Validationerr) whereFailure _ <|> x =
x
Success a <|> _ =Success a
empty =Failuremempty
Which violates the requirement that x <|> empty = x. It seems the full Alternative is no longer part of this library.
Would it make sense to have the following Alternative instance for Validation instead?
instanceSemigroupe=>Alt (Validatione)
whereFailure e1 <|>Failure e2 =Failure$ e1 <> e2
Success x <|> _ =Success x
_ <|>Success x =Success x
instanceMonoide=>Alternative (Validatione)
empty=Failuremempty
This does not (necessarily*) discard any failures, and is a lawful implementation of Alternative. To verify this is a lawful monoid, we can observe that:
Either a :: * -> * is a lawful applicative
For any applicative (f :: * -> *, liftA2, pure) and any monoid (e :: *, <>, mempty) there is a monoid (f e :: *, liftA2 (<>), pure mempty)
All of this is still true if we relabel the constructors of Either a e in the following way:
dataEither'ae=Successa | FailureeinstanceApplicative (Either'a)
where
liftA2 f (Failure e1) (Failure e2) =Failure$ e1 `f` e2
liftA2 _ (Success x) _ =Success x
liftA2 _ _ (Success x) =Success x
pure e =Failure e
instanceMonoide=>Monoid (Either'ae)
where(<>)= liftA2 (<>)mempty=puremempty
* For folks who are interested in preserving the old behavior where half the errors get thrown away, you can always just plug in the Const semigroup and it will work the way the instance does right now.
The text was updated successfully, but these errors were encountered:
Hello there. I was a little bit surprised by the behavior of the
Alt
instance in this library, which goes like this:This is lawful in the sense that it is associative, but then the constant semigroup is a lawful semigroup implementation for any type whatsoever. For the same reason the constant semigroup cannot be a monoid, this instance also prevents us from having a lawful
Alternative
instance.I originally got my wires crossed and started looking at the
Alternative
instance in the wrong library:Which violates the requirement that
x <|> empty = x
. It seems the fullAlternative
is no longer part of this library.Would it make sense to have the following
Alternative
instance forValidation
instead?This does not (necessarily*) discard any failures, and is a lawful implementation of
Alternative
. To verify this is a lawful monoid, we can observe that:Either a :: * -> *
is a lawful applicativeFor any applicative
(f :: * -> *, liftA2, pure)
and any monoid(e :: *, <>, mempty)
there is a monoid(f e :: *, liftA2 (<>), pure mempty)
All of this is still true if we relabel the constructors of
Either a e
in the following way:Compare the suggested
Alternative
instance with:* For folks who are interested in preserving the old behavior where half the errors get thrown away, you can always just plug in the
Const
semigroup and it will work the way the instance does right now.The text was updated successfully, but these errors were encountered: