summaryrefslogtreecommitdiff
path: root/Haskell-book/15/semigroup/src/Validation.hs
blob: a5d1c9ca05ce98c89735183552f27a707d9ee91e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
module Validation where

import Data.Semigroup
import Test.QuickCheck (arbitrary, Arbitrary(..), frequency)

data Validation a b = Failure a | Success b deriving (Eq, Show)

instance Semigroup a => Semigroup (Validation a b) where
    (Success x) <> _ = Success x
    _ <> (Success x) = Success x
    (Failure x) <> (Failure y) = Failure $ x <> y

instance (Arbitrary a, Arbitrary b) => Arbitrary (Validation a b) where
    arbitrary = do
        x <- arbitrary
        y <- arbitrary
        frequency [ (1, return $ Success x), (1, return $ Failure y) ]