Skip to content

Commit

Permalink
Address warning noncanonical-monoid-instances as per documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianhjr committed Nov 19, 2021
1 parent 760fce6 commit 9b51f8d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 30 deletions.
32 changes: 18 additions & 14 deletions core/Test/Tasty/Ingredients/ConsoleReporter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,12 @@ import Data.Char.WCWidth (wcwidth)
import Data.List (isInfixOf)
import Data.Maybe
import Data.Monoid (Any(..))
import qualified Data.Semigroup as Sem
import Data.Typeable
import Options.Applicative hiding (action, str, Success, Failure)
import System.IO
import System.Console.ANSI
#if !MIN_VERSION_base(4,11,0)
import Data.Semigroup (Semigroup)
import qualified Data.Semigroup (Semigroup((<>)))
import Data.Monoid
import Data.Foldable (foldMap)
#endif
Expand Down Expand Up @@ -87,11 +86,13 @@ data TestOutput

-- The monoid laws should hold observationally w.r.t. the semantics defined
-- in this module
instance Sem.Semigroup TestOutput where
(<>) = Seq
instance Monoid TestOutput where
mempty = Skip
mappend = Seq
instance Semigroup TestOutput where
(<>) = mappend
#if !MIN_VERSION_base(4,11,0)
mappend = (Sem.<>)
#endif

applyHook :: ([TestName] -> Result -> IO Result) -> TestOutput -> TestOutput
applyHook hook = go []
Expand Down Expand Up @@ -294,11 +295,13 @@ data Statistics = Statistics
, statFailures :: !Int -- ^ Number of active tests that failed.
}

instance Sem.Semigroup Statistics where
Statistics t1 f1 <> Statistics t2 f2 = Statistics (t1 + t2) (f1 + f2)
instance Monoid Statistics where
Statistics t1 f1 `mappend` Statistics t2 f2 = Statistics (t1 + t2) (f1 + f2)
mempty = Statistics 0 0
instance Semigroup Statistics where
(<>) = mappend
#if !MIN_VERSION_base(4,11,0)
mappend = (Sem.<>)
#endif

-- | @computeStatistics@ computes a summary 'Statistics' for
-- a given state of the 'StatusMap'.
Expand Down Expand Up @@ -638,14 +641,15 @@ data Maximum a
= Maximum a
| MinusInfinity

instance Ord a => Sem.Semigroup (Maximum a) where
Maximum a <> Maximum b = Maximum (a `max` b)
MinusInfinity <> a = a
a <> MinusInfinity = a
instance Ord a => Monoid (Maximum a) where
mempty = MinusInfinity

Maximum a `mappend` Maximum b = Maximum (a `max` b)
MinusInfinity `mappend` a = a
a `mappend` MinusInfinity = a
instance Ord a => Semigroup (Maximum a) where
(<>) = mappend
#if !MIN_VERSION_base(4,11,0)
mappend = (Sem.<>)
#endif

-- | Compute the amount of space needed to align \"OK\"s and \"FAIL\"s
computeAlignment :: OptionSet -> TestTree -> Int
Expand Down
15 changes: 7 additions & 8 deletions core/Test/Tasty/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,10 @@ import Data.Proxy
import Data.Typeable
import Data.Monoid
import Data.Foldable
import qualified Data.Semigroup as Sem
import qualified Data.Set as S
import Prelude hiding (mod) -- Silence FTP import warnings
import Options.Applicative
#if !MIN_VERSION_base(4,11,0)
import Data.Semigroup (Semigroup)
import qualified Data.Semigroup (Semigroup((<>)))
#endif

-- | An option is a data type that inhabits the `IsOption` type class.
class Typeable v => IsOption v where
Expand Down Expand Up @@ -98,12 +95,14 @@ data OptionValue = forall v . IsOption v => OptionValue v
newtype OptionSet = OptionSet (Map TypeRep OptionValue)

-- | Later options override earlier ones
instance Sem.Semigroup OptionSet where
OptionSet a <> OptionSet b =
OptionSet $ Map.unionWith (flip const) a b
instance Monoid OptionSet where
mempty = OptionSet mempty
OptionSet a `mappend` OptionSet b =
OptionSet $ Map.unionWith (flip const) a b
instance Semigroup OptionSet where
(<>) = mappend
#if !MIN_VERSION_base(4,11,0)
mappend = (Sem.<>)
#endif

-- | Set the option value
setOption :: IsOption v => v -> OptionSet -> OptionSet
Expand Down
19 changes: 11 additions & 8 deletions core/Test/Tasty/Runners/Reducers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,31 @@ module Test.Tasty.Runners.Reducers where

import Control.Applicative
import Prelude -- Silence AMP import warnings
import qualified Data.Semigroup as Sem
#if !MIN_VERSION_base(4,11,0)
import Data.Semigroup (Semigroup)
import qualified Data.Semigroup (Semigroup((<>)))
import Data.Monoid
#endif

-- | Monoid generated by '*>'
newtype Traversal f = Traversal { getTraversal :: f () }
instance Applicative f => Sem.Semigroup (Traversal f) where
Traversal f1 <> Traversal f2 = Traversal $ f1 *> f2
instance Applicative f => Monoid (Traversal f) where
mempty = Traversal $ pure ()
Traversal f1 `mappend` Traversal f2 = Traversal $ f1 *> f2
instance Applicative f => Semigroup (Traversal f) where
(<>) = mappend
#if !MIN_VERSION_base(4,11,0)
mappend = (Sem.<>)
#endif

-- | Monoid generated by @'liftA2' ('<>')@
--
-- Starting from GHC 8.6, a similar type is available from "Data.Monoid".
-- This type is nevertheless kept for compatibility.
newtype Ap f a = Ap { getApp :: f a }
deriving (Functor, Applicative, Monad)
instance (Applicative f, Monoid a) => Sem.Semigroup (Ap f a) where
(<>) = liftA2 mappend
instance (Applicative f, Monoid a) => Monoid (Ap f a) where
mempty = pure mempty
mappend = liftA2 mappend
instance (Applicative f, Monoid a) => Semigroup (Ap f a) where
(<>) = mappend
#if !MIN_VERSION_base(4,11,0)
mappend = (Sem.<>)
#endif

0 comments on commit 9b51f8d

Please sign in to comment.