module Hasklet3 where import Data.Semigroup (All(..)) -- | A list of pairs of elements of type a AND b. data ListP a b = NilP | ConsP a b (ListP a b) deriving (Eq,Show) -- | A list of elements of either type a OR b. data ListE a b = NilE | ConsL a (ListE a b) | ConsR b (ListE a b) deriving (Eq,Show) -- | Containers with two different element types that can be mapped over. -- -- Instances of Bifunctor should satisfy the following laws: -- * bimap id id <=> id -- * bimap (f1 . f2) (g1 . g2) <=> bimap f1 g1 . bimap f2 g2 -- class Bifunctor t where bimap :: (a -> c) -> (b -> d) -> t a b -> t c d -- | Test cases for Bifunctor instances. -- -- >>> bimap (+1) (>3) (ConsP 1 2 (ConsP 3 4 NilP)) -- ConsP 2 False (ConsP 4 True NilP) -- -- >>> bimap (+1) even (ConsL 1 (ConsR 2 (ConsR 3 (ConsL 4 NilE)))) -- ConsL 2 (ConsR True (ConsR False (ConsL 5 NilE))) -- -- [Bifunctor instances go here.] -- | Map over the left elements of a bifunctor. -- -- >>> mapL (+5) (ConsP 1 2 (ConsP 3 4 NilP)) -- ConsP 6 2 (ConsP 8 4 NilP) -- -- >>> mapL even (ConsL 1 (ConsR 2 (ConsR 3 (ConsL 4 NilE)))) -- ConsL False (ConsR 2 (ConsR 3 (ConsL True NilE))) -- mapL = undefined -- | Map over the right elements of a bifunctor. -- -- >>> mapR (+5) (ConsP 1 2 (ConsP 3 4 NilP)) -- ConsP 1 7 (ConsP 3 9 NilP) -- -- >>> mapR even (ConsL 1 (ConsR 2 (ConsR 3 (ConsL 4 NilE)))) -- ConsL 1 (ConsR True (ConsR False (ConsL 4 NilE))) -- mapR = undefined -- | Containers with two different element types that can be folded to -- a single summary value. class Bifoldable t where bifoldr :: (a -> c -> c) -> (b -> c -> c) -> c -> t a b -> c -- | Test cases for Bifoldable instances. -- -- >>> let addL x (y,z) = (x+y, z) -- >>> let mulR x (y,z) = (y, x*z) -- -- >>> bifoldr addL mulR (0,1) (ConsP 1 2 (ConsP 3 4 NilP)) -- (4,8) -- -- >>> bifoldr addL mulR (0,1) (ConsL 1 (ConsR 2 (ConsR 3 (ConsL 4 NilE)))) -- (5,6) -- -- [Bifoldable instances go here.] -- | Fold over the left elements of a bifoldable. -- -- >>> foldrL (+) 0 (ConsP 2 3 (ConsP 4 5 NilP)) -- 6 -- -- >>> foldrL (*) 1 (ConsL 2 (ConsR 3 (ConsR 4 (ConsL 5 NilE)))) -- 10 -- foldrL = undefined -- | Fold over the right elements of a bifoldable. -- -- >>> foldrR (+) 0 (ConsP 2 3 (ConsP 4 5 NilP)) -- 8 -- -- >>> foldrR (*) 1 (ConsL 2 (ConsR 3 (ConsR 4 (ConsL 5 NilE)))) -- 12 -- foldrR = undefined -- | Map each element in a bifoldable to a common monoid type and combine -- the results. This function is used by the 'checkAll' and 'toEitherList' -- functions below. -- -- >>> checkAll odd even (ConsP 1 2 (ConsP 3 4 NilP)) -- True -- -- >>> checkAll odd even (ConsL 1 (ConsL 2 (ConsL 3 (ConsR 4 NilE)))) -- False -- -- >>> toEitherList (ConsP 1 True (ConsP 2 False NilP)) -- [Left 1,Right True,Left 2,Right False] -- -- >>> toEitherList (ConsL 1 (ConsL 2 (ConsL 3 (ConsR "hi" NilE)))) -- [Left 1,Left 2,Left 3,Right "hi"] -- bifoldMap = undefined -- | Check whether all of the elements in a bifoldable satisfy the given -- predicates. The 'All' monoid used in the implementation is the boolean -- monoid under conjunction. checkAll :: Bifoldable t => (a -> Bool) -> (b -> Bool) -> t a b -> Bool checkAll f g = getAll . bifoldMap (All . f) (All . g) -- | Create a list of all elements in a bifoldable. toEitherList :: Bifoldable t => t a b -> [Either a b] toEitherList = bifoldMap (\x -> [Left x]) (\y -> [Right y])