Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factor out Additive #39

Merged
merged 6 commits into from
Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Revision history for patch

## Unreleased

* Use `commutative-semigroups` for `Commutative`, making `Additive` a
deprecated alias.

## 0.0.6.0 - 2022-06-10

* Add `PatchOrReplacement`, patch which either is some other patch type or a
Expand Down
1 change: 1 addition & 0 deletions patch.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ library
default-language: Haskell2010
build-depends: base >= 4.9 && < 4.17
, constraints-extras >= 0.3 && < 0.4
, commutative-semigroups >= 0.0 && < 0.2
, containers >= 0.6 && < 0.7
, dependent-map >= 0.3 && < 0.5
, dependent-sum >= 0.6 && < 0.8
Expand Down
7 changes: 4 additions & 3 deletions src/Data/Patch.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module Data.Patch
, module X
) where

import Data.Semigroup.Commutative
import Control.Applicative (liftA2)
import Data.Functor.Const (Const (..))
import Data.Functor.Identity
Expand All @@ -23,7 +24,7 @@ import Data.Semigroup (Semigroup (..))
#endif
import GHC.Generics

import Data.Semigroup.Additive as X
import qualified Data.Semigroup.Additive as X
import Data.Patch.Class as X
import Data.Patch.DMap as X hiding (getDeletions)
import Data.Patch.DMapWithMove as X
Expand All @@ -46,10 +47,10 @@ class (Semigroup q, Monoid q) => Group q where
(~~) :: q -> q -> q
r ~~ s = r <> negateG s

-- | The elements of an 'Additive' 'Semigroup' can be considered as patches of their own type.
-- | The elements of an 'Commutative' 'Semigroup' can be considered as patches of their own type.
newtype AdditivePatch p = AdditivePatch { unAdditivePatch :: p }

instance Additive p => Patch (AdditivePatch p) where
instance Commutative p => Patch (AdditivePatch p) where
type PatchTarget (AdditivePatch p) = p
apply (AdditivePatch p) q = Just $ p <> q

Expand Down
48 changes: 5 additions & 43 deletions src/Data/Semigroup/Additive.hs
Original file line number Diff line number Diff line change
@@ -1,52 +1,14 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE ConstraintKinds #-}

{-|
Description : A class for commutative semigroups
-}
module Data.Semigroup.Additive
{-# DEPRECATED "Use 'Data.Semigroup.Commutative'" #-}
( Additive
) where

import Data.Functor.Const (Const (..))
import Data.Functor.Identity
import Data.Proxy
#if !MIN_VERSION_base(4,12,0)
-- for :*: and :.: semigroup instances
import Data.Orphans ()
#endif
#if !MIN_VERSION_base(4,11,0)
import Data.Semigroup (Semigroup (..))
#endif
import GHC.Generics
import Data.Semigroup.Commutative

-- | An 'Additive' 'Semigroup' is one where (<>) is commutative
class Semigroup q => Additive q where

-- | Trivial additive semigroup.
instance Additive ()

-- | Product additive semigroup.
-- A Pair of additive semigroups gives rise to a additive semigroup
instance (Additive a, Additive b) => Additive (a, b)

-- See https://gitlab.haskell.org/ghc/ghc/issues/11135#note_111802 for the reason Compose is not also provided.
-- Base does not define Monoid (Compose f g a) so this is the best we can
-- really do for functor composition.
instance Additive (f (g a)) => Additive ((f :.: g) a)

-- | Product of additive semigroups, Functor style.
instance (Additive (f a), Additive (g a)) => Additive ((f :*: g) a)

-- | Trivial additive semigroup, Functor style
instance Additive (Proxy x)

-- | Const lifts additive semigroups into a functor.
instance Additive a => Additive (Const a x)

-- | Identity lifts additive semigroups pointwise (at only one point)
instance Additive a => Additive (Identity a)

-- | Functions lift additive semigroups pointwise.
instance Additive b => Additive (a -> b)
{-# DEPRECATED Additive "Use 'Data.Semigroup.Commutative.Commutative'" #-}
type Additive = Commutative