on master help sign in
Add Semigroup instances for existing Monoids

Ensures forwards compatibility with future Haskell/GHC releases as the Semigroup/Monoid Proposal is gradually implemented. Closes #525 and #536.

commit000627c94a4d4aedb0a4216e781f6af65936ef9c authorChristian Barcenas <christian@cbarcenas.com> dateSun 11 Feb 2018, 03:22 (9 years ago) parent157fef5
6 files changed+83
lib/Hakyll/Check.hs+13 lib/Hakyll/Core/Compiler/Internal.hs+14 lib/Hakyll/Core/Identifier/Pattern.hs+13 lib/Hakyll/Core/Routes.hs+17 lib/Hakyll/Core/Rules/Internal.hs+14 lib/Hakyll/Web/Template/Context.hs+12
lib/Hakyll/Check.hs+13
@@ -19,6 +19,9 @@ import Control.Monad.Trans (liftIO)
1919import Control.Monad.Trans.Resource (runResourceT)
2020import Data.List (isPrefixOf)
2121import qualified Data.Map.Lazy as Map
22+#if MIN_VERSION_base(4,9,0)
23+import Data.Semigroup (Semigroup (..))
24+#endif
2225import Network.URI (unEscapeString)
2326import System.Directory (doesDirectoryExist,
2427 doesFileExist)
@@ -85,10 +88,20 @@ data CheckerWrite = CheckerWrite
8588
8689
8790--------------------------------------------------------------------------------
91+#if MIN_VERSION_base(4,9,0)
92+instance Semigroup CheckerWrite where
93+ (<>) (CheckerWrite f1 o1) (CheckerWrite f2 o2) =
94+ CheckerWrite (f1 + f2) (o1 + o2)
95+
96+instance Monoid CheckerWrite where
97+ mempty = CheckerWrite 0 0
98+ mappend = (<>)
99+#else
88100instance Monoid CheckerWrite where
89101 mempty = CheckerWrite 0 0
90102 mappend (CheckerWrite f1 o1) (CheckerWrite f2 o2) =
91103 CheckerWrite (f1 + f2) (o1 + o2)
104+#endif
92105
93106
94107--------------------------------------------------------------------------------
lib/Hakyll/Core/Compiler/Internal.hs+14
@@ -1,5 +1,6 @@
11--------------------------------------------------------------------------------
22-- | Internally used compiler module
3+{-# LANGUAGE CPP #-}
34{-# LANGUAGE FlexibleInstances #-}
45{-# LANGUAGE GADTs #-}
56{-# LANGUAGE GeneralizedNewtypeDeriving #-}
@@ -32,6 +33,9 @@ import Control.Applicative (Alternative (..))
3233import Control.Exception (SomeException, handle)
3334import Control.Monad (forM_)
3435import Control.Monad.Except (MonadError (..))
36+#if MIN_VERSION_base(4,9,0)
37+import Data.Semigroup (Semigroup (..))
38+#endif
3539import Data.Set (Set)
3640import qualified Data.Set as S
3741
@@ -83,10 +87,20 @@ data CompilerWrite = CompilerWrite
8387
8488
8589--------------------------------------------------------------------------------
90+#if MIN_VERSION_base(4,9,0)
91+instance Semigroup CompilerWrite where
92+ (<>) (CompilerWrite d1 h1) (CompilerWrite d2 h2) =
93+ CompilerWrite (d1 ++ d2) (h1 + h2)
94+
95+instance Monoid CompilerWrite where
96+ mempty = CompilerWrite [] 0
97+ mappend = (<>)
98+#else
8699instance Monoid CompilerWrite where
87100 mempty = CompilerWrite [] 0
88101 mappend (CompilerWrite d1 h1) (CompilerWrite d2 h2) =
89102 CompilerWrite (d1 ++ d2) (h1 + h2)
103+#endif
90104
91105
92106--------------------------------------------------------------------------------
lib/Hakyll/Core/Identifier/Pattern.hs+13
@@ -28,6 +28,7 @@
2828--
2929-- The 'capture' function allows the user to get access to the elements captured
3030-- by the capture elements in a glob or regex pattern.
31+{-# LANGUAGE CPP #-}
3132module Hakyll.Core.Identifier.Pattern
3233 ( -- * The pattern type
3334 Pattern
@@ -62,6 +63,9 @@ import Control.Monad (msum)
6263import Data.Binary (Binary (..), getWord8, putWord8)
6364import Data.List (inits, isPrefixOf, tails)
6465import Data.Maybe (isJust)
66+#if MIN_VERSION_base(4,9,0)
67+import Data.Semigroup (Semigroup (..))
68+#endif
6569import Data.Set (Set)
6670import qualified Data.Set as S
6771
@@ -136,9 +140,18 @@ instance IsString Pattern where
136140
137141
138142--------------------------------------------------------------------------------
143+#if MIN_VERSION_base(4,9,0)
144+instance Semigroup Pattern where
145+ (<>) = (.&&.)
146+
147+instance Monoid Pattern where
148+ mempty = Everything
149+ mappend = (<>)
150+#else
139151instance Monoid Pattern where
140152 mempty = Everything
141153 mappend = (.&&.)
154+#endif
142155
143156
144157--------------------------------------------------------------------------------
lib/Hakyll/Core/Routes.hs+17
@@ -25,6 +25,7 @@
2525-- not appear in your site directory.
2626--
2727-- * If an item matches multiple routes, the first rule will be chosen.
28+{-# LANGUAGE CPP #-}
2829{-# LANGUAGE Rank2Types #-}
2930module Hakyll.Core.Routes
3031 ( UsedMetadata
@@ -42,6 +43,9 @@ module Hakyll.Core.Routes
4243
4344
4445--------------------------------------------------------------------------------
46+#if MIN_VERSION_base(4,9,0)
47+import Data.Semigroup (Semigroup (..))
48+#endif
4549import System.FilePath (replaceExtension)
4650
4751
@@ -74,6 +78,18 @@ newtype Routes = Routes
7478
7579
7680--------------------------------------------------------------------------------
81+#if MIN_VERSION_base(4,9,0)
82+instance Semigroup Routes where
83+ (<>) (Routes f) (Routes g) = Routes $ \p id' -> do
84+ (mfp, um) <- f p id'
85+ case mfp of
86+ Nothing -> g p id'
87+ Just _ -> return (mfp, um)
88+
89+instance Monoid Routes where
90+ mempty = Routes $ \_ _ -> return (Nothing, False)
91+ mappend = (<>)
92+#else
7793instance Monoid Routes where
7894 mempty = Routes $ \_ _ -> return (Nothing, False)
7995 mappend (Routes f) (Routes g) = Routes $ \p id' -> do
@@ -81,6 +97,7 @@ instance Monoid Routes where
8197 case mfp of
8298 Nothing -> g p id'
8399 Just _ -> return (mfp, um)
100+#endif
84101
85102
86103--------------------------------------------------------------------------------
lib/Hakyll/Core/Rules/Internal.hs+14
@@ -1,4 +1,5 @@
11--------------------------------------------------------------------------------
2+{-# LANGUAGE CPP #-}
23{-# LANGUAGE GeneralizedNewtypeDeriving #-}
34{-# LANGUAGE Rank2Types #-}
45module Hakyll.Core.Rules.Internal
@@ -16,6 +17,9 @@ import Control.Monad.Reader (ask)
1617import Control.Monad.RWS (RWST, runRWST)
1718import Control.Monad.Trans (liftIO)
1819import qualified Data.Map as M
20+#if MIN_VERSION_base(4,9,0)
21+import Data.Semigroup (Semigroup (..))
22+#endif
1923import Data.Set (Set)
2024
2125
@@ -52,10 +56,20 @@ data RuleSet = RuleSet
5256
5357
5458--------------------------------------------------------------------------------
59+#if MIN_VERSION_base(4,9,0)
60+instance Semigroup RuleSet where
61+ (<>) (RuleSet r1 c1 s1 p1) (RuleSet r2 c2 s2 p2) =
62+ RuleSet (mappend r1 r2) (mappend c1 c2) (mappend s1 s2) (p1 .||. p2)
63+
64+instance Monoid RuleSet where
65+ mempty = RuleSet mempty mempty mempty mempty
66+ mappend = (<>)
67+#else
5568instance Monoid RuleSet where
5669 mempty = RuleSet mempty mempty mempty mempty
5770 mappend (RuleSet r1 c1 s1 p1) (RuleSet r2 c2 s2 p2) =
5871 RuleSet (mappend r1 r2) (mappend c1 c2) (mappend s1 s2) (p1 .||. p2)
72+#endif
5973
6074
6175--------------------------------------------------------------------------------
lib/Hakyll/Web/Template/Context.hs+12
@@ -35,6 +35,9 @@ module Hakyll.Web.Template.Context
3535import Control.Applicative (Alternative (..))
3636import Control.Monad (msum)
3737import Data.List (intercalate)
38+#if MIN_VERSION_base(4,9,0)
39+import Data.Semigroup (Semigroup (..))
40+#endif
3841import Data.Time.Clock (UTCTime (..))
3942import Data.Time.Format (formatTime)
4043import qualified Data.Time.Format as TF
@@ -78,9 +81,18 @@ newtype Context a = Context
7881
7982
8083--------------------------------------------------------------------------------
84+#if MIN_VERSION_base(4,9,0)
85+instance Semigroup (Context a) where
86+ (<>) (Context f) (Context g) = Context $ \k a i -> f k a i <|> g k a i
87+
88+instance Monoid (Context a) where
89+ mempty = missingField
90+ mappend = (<>)
91+#else
8192instance Monoid (Context a) where
8293 mempty = missingField
8394 mappend (Context f) (Context g) = Context $ \k a i -> f k a i <|> g k a i
95+#endif
8496
8597
8698--------------------------------------------------------------------------------