From 8cd78d7c7a889a9b2facd8218cd2c87649ffcf34 Mon Sep 17 00:00:00 2001 From: Kei Hibino Date: Tue, 17 Apr 2018 20:29:10 +0900 Subject: [PATCH] sql-words: add Semigroup instance for GHC 8.4.x. --- sql-words/sql-words.cabal | 2 ++ .../src/Language/SQL/Keyword/Internal/Type.hs | 31 +++++++++++++------ 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/sql-words/sql-words.cabal b/sql-words/sql-words.cabal index 2a2457fb..3d1396dc 100644 --- a/sql-words/sql-words.cabal +++ b/sql-words/sql-words.cabal @@ -29,6 +29,8 @@ library Language.SQL.Keyword.Internal.Type build-depends: base >=4.5 && <5 + if impl(ghc < 8) + build-depends: semigroups hs-source-dirs: src ghc-options: -Wall diff --git a/sql-words/src/Language/SQL/Keyword/Internal/Type.hs b/sql-words/src/Language/SQL/Keyword/Internal/Type.hs index e99ff8c4..dfe2f291 100644 --- a/sql-words/src/Language/SQL/Keyword/Internal/Type.hs +++ b/sql-words/src/Language/SQL/Keyword/Internal/Type.hs @@ -19,7 +19,8 @@ module Language.SQL.Keyword.Internal.Type ( import Data.String (IsString(..)) import Data.List (find) -import Data.Monoid (Monoid (..), (<>)) +import Data.Semigroup (Semigroup (..)) +import Data.Monoid (Monoid (..)) -- | Diff String type for low-cost concatination. @@ -46,12 +47,15 @@ instance Show DString where instance Read DString where readsPrec _ s = [(dString s, [])] +dappend :: DString -> DString -> DString +DString f `dappend` DString g = DString $ f . g + +instance Semigroup DString where + (<>) = dappend + instance Monoid DString where mempty = DString id - DString f `mappend` DString g = DString $ f . g - -dspace :: DString -dspace = dString " " + mappend = dappend -- | Type represent SQL keywords. data Keyword = SELECT | ALL | DISTINCT | ON @@ -130,14 +134,23 @@ instance IsString Keyword where found Nothing s = word s found (Just (w, _)) _ = w --- | 'Keyword' default concatination separate by space. -instance Monoid Keyword where - mempty = fromDString mempty - a `mappend` b = fromDString $ toDString a `append'` toDString b where +kappend :: Keyword -> Keyword -> Keyword +a `kappend` b = fromDString $ toDString a `append'` toDString b + where append' p q | isEmptyDString p = q | isEmptyDString q = p | otherwise = p <> dspace <> q + dspace :: DString + dspace = dString " " + +instance Semigroup Keyword where + (<>) = kappend + +-- | 'Keyword' default concatination separate by space. +instance Monoid Keyword where + mempty = fromDString mempty + mappend = kappend -- | Show 'Keyword'