blob: 9cdce8933bedcdbd524a657ec441dc42c1d21652 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
{-# LANGUAGE DeriveTraversable #-}
module Data.Bag where
data Bag a
= BTwo (Bag a) (Bag a)
| BOne a
| BZero
deriving (Functor, Foldable, Traversable)
instance Semigroup (Bag a) where (<>) = BTwo
instance Monoid (Bag a) where mempty = BZero
instance Applicative Bag where
pure = BOne
BZero <*> _ = BZero
_ <*> BZero = BZero
BOne f <*> b = f <$> b
BTwo b1 b2 <*> b = BTwo (b1 <*> b) (b2 <*> b)
|