|
| 1 | +{-# LANGUAGE CPP #-} |
| 2 | +{-# LANGUAGE DeriveDataTypeable #-} |
| 3 | +module Distribution.Compat.NonEmptySet ( |
| 4 | + NonEmptySet, |
| 5 | + -- * Construction |
| 6 | + singleton, |
| 7 | + -- * Conversions |
| 8 | + toNonEmpty, |
| 9 | + fromNonEmpty, |
| 10 | + toList, |
| 11 | + -- * Query |
| 12 | + member, |
| 13 | + -- * Map |
| 14 | + map, |
| 15 | +) where |
| 16 | + |
| 17 | +import Prelude (Bool (..), Eq, Ord (..), Read, Show (..), String, error, return, showParen, showString, ($), (++), (.)) |
| 18 | + |
| 19 | +import Control.DeepSeq (NFData (..)) |
| 20 | +import Data.Data (Data) |
| 21 | +import Data.List.NonEmpty (NonEmpty (..)) |
| 22 | +import Data.Semigroup (Semigroup (..)) |
| 23 | +import Data.Typeable (Typeable) |
| 24 | + |
| 25 | +import qualified Data.Foldable as F |
| 26 | +import qualified Data.Set as Set |
| 27 | + |
| 28 | +import Distribution.Compat.Binary (Binary (..)) |
| 29 | +import Distribution.Utils.Structured |
| 30 | + |
| 31 | +#if MIN_VERSION_binary(0,6,0) |
| 32 | +import Control.Applicative (empty) |
| 33 | +#else |
| 34 | +import Control.Monad (fail) |
| 35 | +#endif |
| 36 | + |
| 37 | +newtype NonEmptySet a = NES (Set.Set a) |
| 38 | + deriving (Eq, Ord, Typeable, Data, Read) |
| 39 | + |
| 40 | +------------------------------------------------------------------------------- |
| 41 | +-- Instances |
| 42 | +------------------------------------------------------------------------------- |
| 43 | + |
| 44 | +instance Show a => Show (NonEmptySet a) where |
| 45 | + showsPrec d s = showParen (d > 10) |
| 46 | + $ showString "fromNonEmpty " |
| 47 | + . showsPrec 11 (toNonEmpty s) |
| 48 | + |
| 49 | +instance Binary a => Binary (NonEmptySet a) where |
| 50 | + put (NES s) = put s |
| 51 | + get = do |
| 52 | + xs <- get |
| 53 | + if Set.null xs |
| 54 | +#if MIN_VERSION_binary(0,6,0) |
| 55 | + then empty |
| 56 | +#else |
| 57 | + then fail "NonEmptySet: empty" |
| 58 | +#endif |
| 59 | + else return (NES xs) |
| 60 | + |
| 61 | +instance Structured a => Structured (NonEmptySet a) where |
| 62 | + structure = containerStructure |
| 63 | + |
| 64 | +instance NFData a => NFData (NonEmptySet a) where |
| 65 | + rnf (NES x) = rnf x |
| 66 | + |
| 67 | +-- | Note: there aren't @Monoid@ instance. |
| 68 | +instance Ord a => Semigroup (NonEmptySet a) where |
| 69 | + NES x <> NES y = NES (Set.union x y) |
| 70 | + |
| 71 | +instance F.Foldable NonEmptySet where |
| 72 | + foldMap f (NES s) = F.foldMap f s |
| 73 | + foldr f z (NES s) = F.foldr f z s |
| 74 | + |
| 75 | +#if MIN_VERSION_base(4,8,0) |
| 76 | + toList = toList |
| 77 | + null _ = False |
| 78 | + length (NES s) = F.length s |
| 79 | +#endif |
| 80 | + |
| 81 | +------------------------------------------------------------------------------- |
| 82 | +-- Constructors |
| 83 | +------------------------------------------------------------------------------- |
| 84 | + |
| 85 | +singleton :: a -> NonEmptySet a |
| 86 | +singleton = NES . Set.singleton |
| 87 | + |
| 88 | +------------------------------------------------------------------------------- |
| 89 | +-- Conversions |
| 90 | +------------------------------------------------------------------------------- |
| 91 | + |
| 92 | +fromNonEmpty :: Ord a => NonEmpty a -> NonEmptySet a |
| 93 | +fromNonEmpty (x :| xs) = NES (Set.fromList (x : xs)) |
| 94 | + |
| 95 | +toNonEmpty :: NonEmptySet a -> NonEmpty a |
| 96 | +toNonEmpty (NES s) = case Set.toList s of |
| 97 | + [] -> panic "toNonEmpty" |
| 98 | + x:xs -> x :| xs |
| 99 | + |
| 100 | +toList :: NonEmptySet a -> [a] |
| 101 | +toList (NES s) = Set.toList s |
| 102 | + |
| 103 | +------------------------------------------------------------------------------- |
| 104 | +-- Query |
| 105 | +------------------------------------------------------------------------------- |
| 106 | + |
| 107 | +member :: Ord a => a -> NonEmptySet a -> Bool |
| 108 | +member x (NES xs) = Set.member x xs |
| 109 | + |
| 110 | +------------------------------------------------------------------------------- |
| 111 | +-- Map |
| 112 | +------------------------------------------------------------------------------- |
| 113 | + |
| 114 | +map |
| 115 | + :: ( Ord b |
| 116 | +#if !MIN_VERSION_containers(0,5,2) |
| 117 | + , Ord a |
| 118 | +#endif |
| 119 | + ) |
| 120 | + => (a -> b) -> NonEmptySet a -> NonEmptySet b |
| 121 | +map f (NES x) = NES (Set.map f x) |
| 122 | + |
| 123 | +------------------------------------------------------------------------------- |
| 124 | +-- Internal |
| 125 | +------------------------------------------------------------------------------- |
| 126 | + |
| 127 | +panic :: String -> a |
| 128 | +panic msg = error $ "NonEmptySet invariant violated: " ++ msg |
0 commit comments