|
| 1 | +{-# LANGUAGE CPP #-} |
| 2 | +{-# LANGUAGE DefaultSignatures #-} |
| 3 | +{-# LANGUAGE GeneralizedNewtypeDeriving #-} |
| 4 | +{-# LANGUAGE StandaloneDeriving #-} |
| 5 | +{-# LANGUAGE TypeOperators #-} |
| 6 | + |
1 | 7 | -- TODO upstream somwhere else?
|
2 | 8 | module Data.Monoid.DecidablyEmpty where
|
3 | 9 |
|
| 10 | +import Data.Functor.Identity |
| 11 | +import Data.Functor.Const |
| 12 | +import Data.Monoid |
| 13 | +import Data.Maybe (isNothing) |
| 14 | +#if MIN_VERSION_base(4,11,0) |
| 15 | +import Data.Ord |
| 16 | +#endif |
| 17 | +import Data.Proxy |
| 18 | +import Data.Semigroup hiding (First, Last) |
| 19 | +#if MIN_VERSION_base(4,12,0) |
| 20 | +import GHC.Generics |
| 21 | +#endif |
| 22 | + |
| 23 | +import qualified Data.IntSet as IntSet |
| 24 | +import qualified Data.IntMap as IntMap |
| 25 | +import qualified Data.Map as Map |
| 26 | +import qualified Data.Sequence as Seq |
| 27 | +import qualified Data.Set as Set |
| 28 | + |
| 29 | +import Data.GADT.Compare |
| 30 | +import qualified Data.Dependent.Map as DMap |
| 31 | + |
| 32 | +-- | A 'DecidablyEmpty' is one where it can be computed whether or not an |
| 33 | +-- arbitrary value is 'mempty'. |
| 34 | +-- |
| 35 | +-- By using this class rather than 'Eq', we avoid unnecessary constraining the |
| 36 | +-- contents of 'Functor's. This makes it possible to efficiently combine and/or |
| 37 | +-- nest patch maps with 'Eq'-lacking values (e.g. functions) at the leaves. |
4 | 38 | class Monoid a => DecidablyEmpty a where
|
5 |
| - isNull :: a -> Bool |
| 39 | + isEmpty :: a -> Bool |
| 40 | + default isEmpty :: Eq a => a -> Bool |
| 41 | + isEmpty = (==) mempty |
| 42 | + |
| 43 | +-- base |
| 44 | + |
| 45 | +instance DecidablyEmpty Ordering |
| 46 | +instance DecidablyEmpty () |
| 47 | +instance DecidablyEmpty Any |
| 48 | +instance DecidablyEmpty All |
| 49 | +-- instance DecidablyEmpty Lifetime |
| 50 | +-- instance DecidablyEmpty Event |
| 51 | +instance DecidablyEmpty [a] where |
| 52 | + isEmpty = null |
| 53 | +instance |
| 54 | +#if MIN_VERSION_base(4,11,0) |
| 55 | + Semigroup a |
| 56 | +#else |
| 57 | + Monoid a |
| 58 | +#endif |
| 59 | + => DecidablyEmpty (Maybe a) where |
| 60 | + isEmpty = isNothing |
| 61 | +deriving instance (Num a, DecidablyEmpty a) => DecidablyEmpty (Product a) |
| 62 | +deriving instance (DecidablyEmpty a, Num a) => DecidablyEmpty (Sum a) |
| 63 | +deriving instance DecidablyEmpty a => DecidablyEmpty (Dual a) |
| 64 | +instance DecidablyEmpty (First a) where |
| 65 | + isEmpty (First a) = isNothing a |
| 66 | +instance DecidablyEmpty (Last a) where |
| 67 | + isEmpty (Last a) = isNothing a |
| 68 | +deriving instance DecidablyEmpty a => DecidablyEmpty (Identity a) |
| 69 | +instance Semigroup a => DecidablyEmpty (Option a) where |
| 70 | + isEmpty (Option a) = isNothing a |
| 71 | +deriving instance DecidablyEmpty m => DecidablyEmpty (WrappedMonoid m) |
| 72 | +instance (Ord a, Bounded a) => DecidablyEmpty (Max a) |
| 73 | +instance (Ord a, Bounded a) => DecidablyEmpty (Min a) |
| 74 | +instance DecidablyEmpty (Proxy s) |
| 75 | +deriving instance DecidablyEmpty a => DecidablyEmpty (Const a b) |
| 76 | +#if MIN_VERSION_base(4,11,0) |
| 77 | +deriving instance DecidablyEmpty a => DecidablyEmpty (Down a) |
| 78 | +#endif |
| 79 | +#if MIN_VERSION_base(4,12,0) |
| 80 | +deriving instance DecidablyEmpty p => DecidablyEmpty (Par1 p) |
| 81 | +instance DecidablyEmpty (U1 p) |
| 82 | +deriving instance DecidablyEmpty (f p) => DecidablyEmpty (Rec1 f p) |
| 83 | +deriving instance DecidablyEmpty (f p) => DecidablyEmpty (M1 i c f p) |
| 84 | +deriving instance DecidablyEmpty c => DecidablyEmpty (K1 i c p) |
| 85 | +instance (DecidablyEmpty (f p), DecidablyEmpty (g p)) => DecidablyEmpty ((f :*: g) p) where |
| 86 | + isEmpty (x :*: y) = isEmpty x && isEmpty y |
| 87 | +deriving instance DecidablyEmpty (f (g p)) => DecidablyEmpty ((f :.: g) p) |
| 88 | +#endif |
| 89 | + |
| 90 | +instance (DecidablyEmpty a, DecidablyEmpty b) => DecidablyEmpty (a, b) where |
| 91 | + isEmpty (a, b) = isEmpty a && isEmpty b |
| 92 | +instance (DecidablyEmpty a, DecidablyEmpty b, DecidablyEmpty c) => DecidablyEmpty (a, b, c) where |
| 93 | + isEmpty (a, b, c) = isEmpty a && isEmpty b && isEmpty c |
| 94 | +instance (DecidablyEmpty a, DecidablyEmpty b, DecidablyEmpty c, DecidablyEmpty d) => DecidablyEmpty (a, b, c, d) where |
| 95 | + isEmpty (a, b, c, d) = isEmpty a && isEmpty b && isEmpty c && isEmpty d |
| 96 | +instance (DecidablyEmpty a, DecidablyEmpty b, DecidablyEmpty c, DecidablyEmpty d, DecidablyEmpty e) => DecidablyEmpty (a, b, c, d, e) where |
| 97 | + isEmpty (a, b, c, d, e) = isEmpty a && isEmpty b && isEmpty c && isEmpty d && isEmpty e |
| 98 | + |
| 99 | +-- containers |
| 100 | + |
| 101 | +instance DecidablyEmpty IntSet.IntSet where |
| 102 | + isEmpty = IntSet.null |
| 103 | +instance DecidablyEmpty (IntMap.IntMap v) where |
| 104 | + isEmpty = IntMap.null |
| 105 | +instance Ord k => DecidablyEmpty (Map.Map k v) where |
| 106 | + isEmpty = Map.null |
| 107 | +instance DecidablyEmpty (Seq.Seq v) where |
| 108 | + isEmpty = Seq.null |
| 109 | +instance Ord k => DecidablyEmpty (Set.Set k) where |
| 110 | + isEmpty = Set.null |
| 111 | + |
| 112 | +-- dependent-map |
| 113 | + |
| 114 | +instance GCompare k => DecidablyEmpty (DMap.DMap k v) where |
| 115 | + isEmpty = DMap.null |
0 commit comments