From f3fd569a6beba6971f50ce8a24e60038bea090f2 Mon Sep 17 00:00:00 2001 From: Timothy Clem Date: Fri, 16 Mar 2018 10:55:19 -0700 Subject: [PATCH] Implement Semigroup instances for future compatability --- src/Analysis/CallGraph.hs | 4 +++- src/Assigning/Assignment/Table.hs | 5 ++++- src/Rendering/Imports.hs | 10 ++++++++-- src/Rendering/TOC.hs | 5 ++++- 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/Analysis/CallGraph.hs b/src/Analysis/CallGraph.hs index f62438294..d66c7c51d 100644 --- a/src/Analysis/CallGraph.hs +++ b/src/Analysis/CallGraph.hs @@ -91,10 +91,12 @@ type family CallGraphAlgebraStrategy syntax where CallGraphAlgebraStrategy (TermF f a) = 'Custom CallGraphAlgebraStrategy a = 'Default +instance Semigroup CallGraph where + (<>) = overlay instance Monoid CallGraph where mempty = empty - mappend = overlay + mappend = (<>) instance Ord CallGraph where compare (CallGraph G.Empty) (CallGraph G.Empty) = EQ diff --git a/src/Assigning/Assignment/Table.hs b/src/Assigning/Assignment/Table.hs index ad05a05b0..fce637270 100644 --- a/src/Assigning/Assignment/Table.hs +++ b/src/Assigning/Assignment/Table.hs @@ -29,9 +29,12 @@ lookup :: Enum i => i -> Table i a -> Maybe a lookup i = IntMap.lookup (fromEnum i) . tableBranches +instance (Enum i, Monoid a) => Semigroup (Table i a) where + (Table i1 b1) <> (Table i2 b2) = Table (i1 `mappend` i2) (IntMap.unionWith mappend b1 b2) + instance (Enum i, Monoid a) => Monoid (Table i a) where mempty = Table mempty mempty - mappend (Table i1 b1) (Table i2 b2) = Table (i1 `mappend` i2) (IntMap.unionWith mappend b1 b2) + mappend = (<>) instance (Enum i, Show i) => Show1 (Table i) where liftShowsPrec spA slA d t = showsBinaryWith showsPrec (const (liftShowList spA slA)) "Table" d (tableAddresses t) (toPairs t) diff --git a/src/Rendering/Imports.hs b/src/Rendering/Imports.hs index ea6f08e04..4e1e28e4b 100644 --- a/src/Rendering/Imports.hs +++ b/src/Rendering/Imports.hs @@ -22,9 +22,12 @@ import Rendering.TOC (termTableOfContentsBy, declaration, getDeclaration, toCate newtype ImportSummary = ImportSummary (Map.Map T.Text Module) deriving (Eq, Show) +instance Semigroup ImportSummary where + (<>) (ImportSummary m1) (ImportSummary m2) = ImportSummary (Map.unionWith mappend m1 m2) + instance Monoid ImportSummary where mempty = ImportSummary mempty - mappend (ImportSummary m1) (ImportSummary m2) = ImportSummary (Map.unionWith mappend m1 m2) + mappend = (<>) instance Output ImportSummary where toOutput = toStrict . (<> "\n") . encode @@ -87,9 +90,12 @@ data Module = Module , moduleCalls :: [CallExpression] } deriving (Generic, Eq, Show) +instance Semigroup Module where + (<>) (Module n1 p1 l1 i1 d1 r1) (Module _ p2 _ i2 d2 r2) = Module n1 (p1 <> p2) l1 (i1 <> i2) (d1 <> d2) (r1 <> r2) + instance Monoid Module where mempty = mempty - mappend (Module n1 p1 l1 i1 d1 r1) (Module _ p2 _ i2 d2 r2) = Module n1 (p1 <> p2) l1 (i1 <> i2) (d1 <> d2) (r1 <> r2) + mappend = (<>) instance ToJSON Module where toJSON Module{..} = object diff --git a/src/Rendering/TOC.hs b/src/Rendering/TOC.hs index 6b4efc394..3fd0f6899 100644 --- a/src/Rendering/TOC.hs +++ b/src/Rendering/TOC.hs @@ -37,9 +37,12 @@ import qualified Data.Text as T data Summaries = Summaries { changes, errors :: !(Map.Map T.Text [Value]) } deriving (Eq, Show) +instance Semigroup Summaries where + (<>) (Summaries c1 e1) (Summaries c2 e2) = Summaries (Map.unionWith (<>) c1 c2) (Map.unionWith (<>) e1 e2) + instance Monoid Summaries where mempty = Summaries mempty mempty - mappend (Summaries c1 e1) (Summaries c2 e2) = Summaries (Map.unionWith (<>) c1 c2) (Map.unionWith (<>) e1 e2) + mappend = (<>) instance Output Summaries where toOutput = toStrict . (<> "\n") . encode