Skip to content

Instantly share code, notes, and snippets.

@kputnam
Last active August 29, 2015 13:57
Show Gist options
  • Save kputnam/9849404 to your computer and use it in GitHub Desktop.
Save kputnam/9849404 to your computer and use it in GitHub Desktop.
{-# LANGUAGE TemplateHaskell #-}
import Data.Monoid
import Data.DeriveTH
data Venn a =
Venn
{ both :: a -- number of elements in both t and p
, pNotT :: a -- number of elements in p but not t
, tNotP :: a -- number of elements in t but not p
, neither :: a -- number of elements in neither t nor p
} deriving (Show)
$( derive makeMonoid ''Venn )
main :: IO ()
main = print $ a <> b
where
a = Venn (Sum 1) (Sum 2) (Sum 3) (Sum 4)
b = Venn (Sum 0) (Sum 1) (Sum 0) (Sum 1)
-- Success!
-- Venn {both = Sum {getSum = 1}, pNotT = Sum {getSum = 3}, tNotP = Sum {getSum = 3}, neither = Sum {getSum = 5}}
{-# LANGUAGE DeriveGeneric #-}
import GHC.Generics (Generic)
import Data.Monoid
data Venn a =
Venn
{ both :: a -- number of elements in both t and p
, pNotT :: a -- number of elements in p but not t
, tNotP :: a -- number of elements in t but not p
, neither :: a -- number of elements in neither t nor p
} deriving (Show, Generic)
instance Monoid a => Monoid (Venn a)
main :: IO ()
main = print $ a <> b
where
a = Venn (Sum 1) (Sum 2) (Sum 3) (Sum 4)
b = Venn (Sum 0) (Sum 1) (Sum 0) (Sum 1)
--
-- *** Exception: test.hs:14:10-36: No instance nor default method for class operation Data.Monoid.mappend
--
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment