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

Invalid instance HaskVerdict (a :&& b) r #14

Open
fizruk opened this issue Jan 20, 2016 · 1 comment
Open

Invalid instance HaskVerdict (a :&& b) r #14

fizruk opened this issue Jan 20, 2016 · 1 comment

Comments

@fizruk
Copy link

fizruk commented Jan 20, 2016

@jkarni earlier you've wondered why this test fails.

Here's localized problem (reproducible with stack ghci):

>>> :m + Data.Aeson Verdict.JSON
>>> :set -XOverloadedStrings
>>> decode "\"\"" :: Maybe (Validated (MinLength 1) String)
Nothing
>>> decode "\"\"" :: Maybe (Validated (MaxLength 1 :&& MinLength 10) String)
Just ""

The reason is this instance for :&&:

instance (HaskVerdict a r, HaskVerdict b r) => HaskVerdict (a :&& b) r where
    haskVerdict _ x = And <$> haskVerdict pa x <*> haskVerdict pb x
      where pa = Proxy :: Proxy a
            pb = Proxy :: Proxy b

The Applicative in question is Maybe and for this problem Nothing indicates success.
So this implementation shortcuts with success whenever any of the conditions is met.

To fix this you can use this implementation instead:

instance (HaskVerdict a r, HaskVerdict b r) => HaskVerdict (a :&& b) r where
    haskVerdict _ x =
      case (haskVerdict pa x, haskVerdict pb x) of
        (Just ex, Just ey) -> Just (And ex ey)
        (Just ex, _) -> Just ex
        (_, Just ey) -> Just ey
        _ -> Nothing
      where pa = Proxy :: Proxy a
            pb = Proxy :: Proxy b
@jkarni
Copy link
Owner

jkarni commented Jan 20, 2016

Oh, of course! Thanks!

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