Skip to content

Commit 3b2e4a7

Browse files
committed
Fix instances for DecidablyEmpty for Sum and Product; add instances for Patch
1 parent 7896b83 commit 3b2e4a7

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/Data/Monoid/DecidablyEmpty.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@ instance
6161
#endif
6262
=> DecidablyEmpty (Maybe a) where
6363
isEmpty = isNothing
64-
deriving instance (Num a, DecidablyEmpty a) => DecidablyEmpty (Product a)
65-
deriving instance (DecidablyEmpty a, Num a) => DecidablyEmpty (Sum a)
64+
instance (Num a, Eq a) => DecidablyEmpty (Product a) where
65+
isEmpty = (== 1)
66+
instance (Num a, Eq a) => DecidablyEmpty (Sum a) where
67+
isEmpty = (== 0)
6668
deriving instance DecidablyEmpty a => DecidablyEmpty (Dual a)
6769
instance DecidablyEmpty (First a) where
6870
isEmpty (First a) = isNothing a

src/Data/Patch/Class.hs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ module Data.Patch.Class where
1212
import Data.Functor.Identity
1313
import Data.Kind (Type)
1414
import Data.Maybe
15+
import Data.Semigroup
16+
( Sum (..)
17+
, Product (..)
1518
#if !MIN_VERSION_base(4,11,0)
16-
import Data.Semigroup (Semigroup(..))
19+
, Semigroup(..)
1720
#endif
21+
)
1822
import Data.Proxy
1923

2024
-- | A 'Patch' type represents a kind of change made to a datastructure.
@@ -41,6 +45,14 @@ instance forall (a :: Type). Patch (Proxy a) where
4145
type PatchTarget (Proxy a) = a
4246
apply ~Proxy _ = Nothing
4347

48+
instance (Num a, Eq a) => Patch (Sum a) where
49+
type PatchTarget (Sum a) = a
50+
apply (Sum a) b = if a == 0 then Nothing else Just $ a + b
51+
52+
instance (Num a, Eq a) => Patch (Product a) where
53+
type PatchTarget (Product a) = a
54+
apply (Product a) b = if a == 1 then Nothing else Just $ a * b
55+
4456
-- | Like '(.)', but composes functions that return patches rather than
4557
-- functions that return new values. The Semigroup instance for patches must
4658
-- apply patches right-to-left, like '(.)'.

0 commit comments

Comments
 (0)