module Control.Lens.Empty
(
AsEmpty(..)
) where
import Control.Lens.Iso
import Control.Lens.Prism
import Control.Lens.Review
import Data.ByteString as StrictB
import Data.ByteString.Lazy as LazyB
import Data.HashMap.Lazy as HashMap
import Data.HashSet as HashSet
import Data.IntMap as IntMap
import Data.IntSet as IntSet
import Data.Map as Map
import Data.Maybe
import Data.Monoid
import Data.Profunctor.Unsafe
import Data.Sequence as Seq
import Data.Set as Set
import Data.Text as StrictT
import Data.Text.Lazy as LazyT
import Data.Vector as Vector
import Data.Vector.Unboxed as Unboxed
import Data.Vector.Storable as Storable
#ifndef mingw32_HOST_OS
import GHC.Event
#endif
class AsEmpty a where
_Empty :: Prism' a ()
#ifndef HLINT
default _Empty :: (Monoid a, Eq a) => Prism' a ()
_Empty = only mempty
#endif
instance AsEmpty Ordering
instance AsEmpty ()
instance AsEmpty Any
instance AsEmpty All
#ifndef mingw32_HOST_OS
instance AsEmpty Event
#endif
instance (Eq a, Num a) => AsEmpty (Product a)
instance (Eq a, Num a) => AsEmpty (Sum a)
instance AsEmpty (Maybe a) where
_Empty = _Nothing
instance AsEmpty (Last a) where
_Empty = nearly (Last Nothing) (isNothing .# getLast)
instance AsEmpty (First a) where
_Empty = nearly (First Nothing) (isNothing .# getFirst)
instance AsEmpty a => AsEmpty (Dual a) where
_Empty = iso getDual Dual . _Empty
instance (AsEmpty a, AsEmpty b) => AsEmpty (a,b) where
_Empty = prism' (\() -> (_Empty # (), _Empty # ())) $ \(s,s') -> case _Empty Left s of
Left () -> case _Empty Left s' of
Left () -> Just ()
_ -> Nothing
_ -> Nothing
instance (AsEmpty a, AsEmpty b, AsEmpty c) => AsEmpty (a,b,c) where
_Empty = prism' (\() -> (_Empty # (), _Empty # (), _Empty # ())) $ \(s,s',s'') -> case _Empty Left s of
Left () -> case _Empty Left s' of
Left () -> case _Empty Left s'' of
Left () -> Just ()
Right _ -> Nothing
Right _ -> Nothing
Right _ -> Nothing
instance AsEmpty [a] where
_Empty = nearly [] Prelude.null
instance AsEmpty (Map k a) where
_Empty = nearly Map.empty Map.null
instance AsEmpty (HashMap k a) where
_Empty = nearly HashMap.empty HashMap.null
instance AsEmpty (IntMap a) where
_Empty = nearly IntMap.empty IntMap.null
instance AsEmpty (Set a) where
_Empty = nearly Set.empty Set.null
instance AsEmpty (HashSet a) where
_Empty = nearly HashSet.empty HashSet.null
instance AsEmpty IntSet where
_Empty = nearly IntSet.empty IntSet.null
instance AsEmpty (Vector.Vector a) where
_Empty = nearly Vector.empty Vector.null
instance Unbox a => AsEmpty (Unboxed.Vector a) where
_Empty = nearly Unboxed.empty Unboxed.null
instance Storable a => AsEmpty (Storable.Vector a) where
_Empty = nearly Storable.empty Storable.null
instance AsEmpty (Seq a) where
_Empty = nearly Seq.empty Seq.null
instance AsEmpty StrictB.ByteString where
_Empty = nearly StrictB.empty StrictB.null
instance AsEmpty LazyB.ByteString where
_Empty = nearly LazyB.empty LazyB.null
instance AsEmpty StrictT.Text where
_Empty = nearly StrictT.empty StrictT.null
instance AsEmpty LazyT.Text where
_Empty = nearly LazyT.empty LazyT.null