File tree Expand file tree Collapse file tree 2 files changed +17
-3
lines changed Expand file tree Collapse file tree 2 files changed +17
-3
lines changed Original file line number Diff line number Diff line change @@ -61,8 +61,10 @@ instance
61
61
#endif
62
62
=> DecidablyEmpty (Maybe a) where
63
63
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 )
66
68
deriving instance DecidablyEmpty a => DecidablyEmpty (Dual a )
67
69
instance DecidablyEmpty (First a ) where
68
70
isEmpty (First a) = isNothing a
Original file line number Diff line number Diff line change @@ -12,9 +12,13 @@ module Data.Patch.Class where
12
12
import Data.Functor.Identity
13
13
import Data.Kind (Type )
14
14
import Data.Maybe
15
+ import Data.Semigroup
16
+ ( Sum (.. )
17
+ , Product (.. )
15
18
#if !MIN_VERSION_base(4,11,0)
16
- import Data.Semigroup ( Semigroup (.. ) )
19
+ , Semigroup (.. )
17
20
#endif
21
+ )
18
22
import Data.Proxy
19
23
20
24
-- | A 'Patch' type represents a kind of change made to a datastructure.
@@ -41,6 +45,14 @@ instance forall (a :: Type). Patch (Proxy a) where
41
45
type PatchTarget (Proxy a ) = a
42
46
apply ~ Proxy _ = Nothing
43
47
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
+
44
56
-- | Like '(.)', but composes functions that return patches rather than
45
57
-- functions that return new values. The Semigroup instance for patches must
46
58
-- apply patches right-to-left, like '(.)'.
You can’t perform that action at this time.
0 commit comments