From 2195a82f70bec8b0fc750930645694ba7e4790ce Mon Sep 17 00:00:00 2001 From: Oleg Grenrus Date: Sun, 7 Jan 2018 22:36:20 +0200 Subject: [PATCH] Add Semigroup (Predicate a) instance --- .gitignore | 2 ++ src/Data/Functor/Contravariant.hs | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/.gitignore b/.gitignore index 7001208..276e37b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ _darcs dist +dist-newstyle +.ghc.environment.* diff --git a/src/Data/Functor/Contravariant.hs b/src/Data/Functor/Contravariant.hs index 59f9986..8096afd 100644 --- a/src/Data/Functor/Contravariant.hs +++ b/src/Data/Functor/Contravariant.hs @@ -310,9 +310,18 @@ newtype Predicate a = Predicate { getPredicate :: a -> Bool } instance Contravariant Predicate where contramap f g = Predicate $ getPredicate g . f +#if defined(MIN_VERSION_semigroups) || __GLASGOW_HASKELL__ >= 711 +instance Semigroup (Predicate a) where + Predicate p <> Predicate q = Predicate $ \a -> p a && q a +#endif + instance Monoid (Predicate a) where mempty = Predicate $ const True +#if defined(MIN_VERSION_semigroups) || __GLASGOW_HASKELL__ >= 711 + mappend = (<>) +#else mappend (Predicate p) (Predicate q) = Predicate $ \a -> p a && q a +#endif -- | Defines a total ordering on a type as per 'compare' --