on dpatch-map-inner-patch help sign in
patch/src/Data/Monoid/DecidablyEmpty.hs
115 lines 4.2 kB history raw
1{-# LANGUAGE CPP #-}
2{-# LANGUAGE DefaultSignatures #-}
3{-# LANGUAGE GeneralizedNewtypeDeriving #-}
4{-# LANGUAGE StandaloneDeriving #-}
5{-# LANGUAGE TypeOperators #-}
7-- TODO upstream somwhere else?
8module Data.Monoid.DecidablyEmpty where
10import Data.Functor.Identity
11import Data.Functor.Const
12import Data.Monoid
13import Data.Maybe (isNothing)
14#if MIN_VERSION_base(4,11,0)
15import Data.Ord
16#endif
17import Data.Proxy
18import Data.Semigroup hiding (First, Last)
19#if MIN_VERSION_base(4,12,0)
20import GHC.Generics
21#endif
23import qualified Data.IntSet as IntSet
24import qualified Data.IntMap as IntMap
25import qualified Data.Map as Map
26import qualified Data.Sequence as Seq
27import qualified Data.Set as Set
29import Data.GADT.Compare
30import qualified Data.Dependent.Map as DMap
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.
38class Monoid a => DecidablyEmpty a where
39 isEmpty :: a -> Bool
40 default isEmpty :: Eq a => a -> Bool
41 isEmpty = (==) mempty
43-- base
45instance DecidablyEmpty Ordering
46instance DecidablyEmpty ()
47instance DecidablyEmpty Any
48instance DecidablyEmpty All
49-- instance DecidablyEmpty Lifetime
50-- instance DecidablyEmpty Event
51instance DecidablyEmpty [a] where
52 isEmpty = null
53instance
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
61deriving instance (Num a, DecidablyEmpty a) => DecidablyEmpty (Product a)
62deriving instance (DecidablyEmpty a, Num a) => DecidablyEmpty (Sum a)
63deriving instance DecidablyEmpty a => DecidablyEmpty (Dual a)
64instance DecidablyEmpty (First a) where
65 isEmpty (First a) = isNothing a
66instance DecidablyEmpty (Last a) where
67 isEmpty (Last a) = isNothing a
68deriving instance DecidablyEmpty a => DecidablyEmpty (Identity a)
69instance Semigroup a => DecidablyEmpty (Option a) where
70 isEmpty (Option a) = isNothing a
71deriving instance DecidablyEmpty m => DecidablyEmpty (WrappedMonoid m)
72instance (Ord a, Bounded a) => DecidablyEmpty (Max a)
73instance (Ord a, Bounded a) => DecidablyEmpty (Min a)
74instance DecidablyEmpty (Proxy s)
75deriving instance DecidablyEmpty a => DecidablyEmpty (Const a b)
76#if MIN_VERSION_base(4,11,0)
77deriving instance DecidablyEmpty a => DecidablyEmpty (Down a)
78#endif
79#if MIN_VERSION_base(4,12,0)
80deriving instance DecidablyEmpty p => DecidablyEmpty (Par1 p)
81instance DecidablyEmpty (U1 p)
82deriving instance DecidablyEmpty (f p) => DecidablyEmpty (Rec1 f p)
83deriving instance DecidablyEmpty (f p) => DecidablyEmpty (M1 i c f p)
84deriving instance DecidablyEmpty c => DecidablyEmpty (K1 i c p)
85instance (DecidablyEmpty (f p), DecidablyEmpty (g p)) => DecidablyEmpty ((f :*: g) p) where
86 isEmpty (x :*: y) = isEmpty x && isEmpty y
87deriving instance DecidablyEmpty (f (g p)) => DecidablyEmpty ((f :.: g) p)
88#endif
90instance (DecidablyEmpty a, DecidablyEmpty b) => DecidablyEmpty (a, b) where
91 isEmpty (a, b) = isEmpty a && isEmpty b
92instance (DecidablyEmpty a, DecidablyEmpty b, DecidablyEmpty c) => DecidablyEmpty (a, b, c) where
93 isEmpty (a, b, c) = isEmpty a && isEmpty b && isEmpty c
94instance (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
96instance (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
99-- containers
101instance DecidablyEmpty IntSet.IntSet where
102 isEmpty = IntSet.null
103instance DecidablyEmpty (IntMap.IntMap v) where
104 isEmpty = IntMap.null
105instance Ord k => DecidablyEmpty (Map.Map k v) where
106 isEmpty = Map.null
107instance DecidablyEmpty (Seq.Seq v) where
108 isEmpty = Seq.null
109instance Ord k => DecidablyEmpty (Set.Set k) where
110 isEmpty = Set.null
112-- dependent-map
114instance GCompare k => DecidablyEmpty (DMap.DMap k v) where
115 isEmpty = DMap.null