-- | Representation of @'Set' 'Bool'@ as a 4-element enum type.
--
-- All operations in constant time and space.
--
-- Mimics the interface of 'Data.Set'.
--
-- Import as:
-- @
--    import qualified Agda.Utils.BoolSet as BoolSet
--    import Agda.Utils.BoolSet (BoolSet)
-- @

module Agda.Utils.BoolSet
  ( BoolSet
  , (\\)
  , complement
  , delete
  , difference
  , elems
  , empty
  , fromList, fromAscList, fromDistinctAscList
  , insert
  , intersection
  , isSubsetOf
  , lookupMin
  , member
  , notMember
  , null
  , singleton
  , size
  , toList, toAscList
  , toSingleton
  , total
  , union
  ) where

import Prelude hiding (null)

import Agda.Utils.Impossible

-- | Isomorphic to @'Set' 'Bool'@.
data BoolSet = SetEmpty | SetTrue | SetFalse | SetBoth
  deriving (BoolSet -> BoolSet -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: BoolSet -> BoolSet -> Bool
$c/= :: BoolSet -> BoolSet -> Bool
== :: BoolSet -> BoolSet -> Bool
$c== :: BoolSet -> BoolSet -> Bool
Eq, Eq BoolSet
BoolSet -> BoolSet -> Bool
BoolSet -> BoolSet -> Ordering
BoolSet -> BoolSet -> BoolSet
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: BoolSet -> BoolSet -> BoolSet
$cmin :: BoolSet -> BoolSet -> BoolSet
max :: BoolSet -> BoolSet -> BoolSet
$cmax :: BoolSet -> BoolSet -> BoolSet
>= :: BoolSet -> BoolSet -> Bool
$c>= :: BoolSet -> BoolSet -> Bool
> :: BoolSet -> BoolSet -> Bool
$c> :: BoolSet -> BoolSet -> Bool
<= :: BoolSet -> BoolSet -> Bool
$c<= :: BoolSet -> BoolSet -> Bool
< :: BoolSet -> BoolSet -> Bool
$c< :: BoolSet -> BoolSet -> Bool
compare :: BoolSet -> BoolSet -> Ordering
$ccompare :: BoolSet -> BoolSet -> Ordering
Ord, Int -> BoolSet -> ShowS
[BoolSet] -> ShowS
BoolSet -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BoolSet] -> ShowS
$cshowList :: [BoolSet] -> ShowS
show :: BoolSet -> String
$cshow :: BoolSet -> String
showsPrec :: Int -> BoolSet -> ShowS
$cshowsPrec :: Int -> BoolSet -> ShowS
Show, Int -> BoolSet
BoolSet -> Int
BoolSet -> [BoolSet]
BoolSet -> BoolSet
BoolSet -> BoolSet -> [BoolSet]
BoolSet -> BoolSet -> BoolSet -> [BoolSet]
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: BoolSet -> BoolSet -> BoolSet -> [BoolSet]
$cenumFromThenTo :: BoolSet -> BoolSet -> BoolSet -> [BoolSet]
enumFromTo :: BoolSet -> BoolSet -> [BoolSet]
$cenumFromTo :: BoolSet -> BoolSet -> [BoolSet]
enumFromThen :: BoolSet -> BoolSet -> [BoolSet]
$cenumFromThen :: BoolSet -> BoolSet -> [BoolSet]
enumFrom :: BoolSet -> [BoolSet]
$cenumFrom :: BoolSet -> [BoolSet]
fromEnum :: BoolSet -> Int
$cfromEnum :: BoolSet -> Int
toEnum :: Int -> BoolSet
$ctoEnum :: Int -> BoolSet
pred :: BoolSet -> BoolSet
$cpred :: BoolSet -> BoolSet
succ :: BoolSet -> BoolSet
$csucc :: BoolSet -> BoolSet
Enum, BoolSet
forall a. a -> a -> Bounded a
maxBound :: BoolSet
$cmaxBound :: BoolSet
minBound :: BoolSet
$cminBound :: BoolSet
Bounded)

-- * Query

null :: BoolSet -> Bool
null :: BoolSet -> Bool
null = (BoolSet
SetEmpty forall a. Eq a => a -> a -> Bool
==)

size :: BoolSet -> Int
size :: BoolSet -> Int
size = \case
  BoolSet
SetEmpty -> Int
0
  BoolSet
SetTrue  -> Int
1
  BoolSet
SetFalse -> Int
1
  BoolSet
SetBoth  -> Int
2

member :: Bool -> BoolSet -> Bool
member :: Bool -> BoolSet -> Bool
member Bool
b = \case
  BoolSet
SetEmpty -> Bool
False
  BoolSet
SetBoth  -> Bool
True
  BoolSet
SetTrue  -> Bool
b
  BoolSet
SetFalse -> Bool -> Bool
not Bool
b

-- | @not . member b@.
notMember :: Bool -> BoolSet -> Bool
notMember :: Bool -> BoolSet -> Bool
notMember Bool
b = Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> BoolSet -> Bool
member Bool
b

isSubsetOf ::  BoolSet -> BoolSet -> Bool
isSubsetOf :: BoolSet -> BoolSet -> Bool
isSubsetOf = forall a b c. ((a, b) -> c) -> a -> b -> c
curry forall a b. (a -> b) -> a -> b
$ \case
  (BoolSet
SetEmpty , BoolSet
_        ) -> Bool
True
  (BoolSet
_        , BoolSet
SetBoth  ) -> Bool
True
  (BoolSet
SetTrue  , BoolSet
SetTrue  ) -> Bool
True
  (BoolSet
SetFalse , BoolSet
SetFalse ) -> Bool
True
  (BoolSet, BoolSet)
_                      -> Bool
False

lookupMin :: BoolSet -> Maybe Bool
lookupMin :: BoolSet -> Maybe Bool
lookupMin = \case
  BoolSet
SetEmpty -> forall a. Maybe a
Nothing
  BoolSet
SetTrue  -> forall a. a -> Maybe a
Just Bool
True
  BoolSet
_        -> forall a. a -> Maybe a
Just Bool
False

-- | @toSingleton s == Just b@ iff @s == singleton b@.
toSingleton :: BoolSet -> Maybe Bool
toSingleton :: BoolSet -> Maybe Bool
toSingleton  = \case
  BoolSet
SetTrue  -> forall a. a -> Maybe a
Just Bool
True
  BoolSet
SetFalse -> forall a. a -> Maybe a
Just Bool
False
  BoolSet
_        -> forall a. Maybe a
Nothing

-- * Construction

-- | The empty set.
empty :: BoolSet
empty :: BoolSet
empty = BoolSet
SetEmpty

-- | The full set.
total :: BoolSet
total :: BoolSet
total = BoolSet
SetBoth

-- | A singleton set.
singleton :: Bool -> BoolSet
singleton :: Bool -> BoolSet
singleton = \case
  Bool
True  -> BoolSet
SetTrue
  Bool
False -> BoolSet
SetFalse

insert :: Bool -> BoolSet -> BoolSet
insert :: Bool -> BoolSet -> BoolSet
insert Bool
b = \case
  BoolSet
SetBoth  -> BoolSet
SetBoth
  BoolSet
SetEmpty -> Bool -> BoolSet
singleton Bool
b
  BoolSet
SetTrue  -> if Bool
b then BoolSet
SetTrue else BoolSet
SetBoth
  BoolSet
SetFalse -> if Bool
b then BoolSet
SetBoth else BoolSet
SetFalse

delete :: Bool -> BoolSet -> BoolSet
delete :: Bool -> BoolSet -> BoolSet
delete Bool
b = \case
  BoolSet
SetEmpty -> BoolSet
SetEmpty
  BoolSet
SetTrue  -> if Bool
b then BoolSet
SetEmpty else BoolSet
SetTrue
  BoolSet
SetFalse -> if Bool
b then BoolSet
SetFalse else BoolSet
SetEmpty
  BoolSet
SetBoth  -> if Bool
b then BoolSet
SetFalse else BoolSet
SetTrue

-- * Combine

complement :: BoolSet -> BoolSet
complement :: BoolSet -> BoolSet
complement = \case
  BoolSet
SetEmpty -> BoolSet
SetBoth
  BoolSet
SetBoth  -> BoolSet
SetEmpty
  BoolSet
SetTrue  -> BoolSet
SetFalse
  BoolSet
SetFalse -> BoolSet
SetTrue

difference, (\\) :: BoolSet -> BoolSet -> BoolSet
difference :: BoolSet -> BoolSet -> BoolSet
difference = forall a b c. ((a, b) -> c) -> a -> b -> c
curry forall a b. (a -> b) -> a -> b
$ \case
  (BoolSet
SetEmpty , BoolSet
_        ) -> BoolSet
SetEmpty
  (BoolSet
_        , BoolSet
SetBoth  ) -> BoolSet
SetEmpty
  (BoolSet
s        , BoolSet
SetEmpty ) -> BoolSet
s
  (BoolSet
SetBoth  , BoolSet
SetTrue  ) -> BoolSet
SetFalse
  (BoolSet
SetBoth  , BoolSet
SetFalse ) -> BoolSet
SetTrue
  (BoolSet
SetTrue  , BoolSet
SetTrue  ) -> BoolSet
SetEmpty
  (BoolSet
SetTrue  , BoolSet
SetFalse ) -> BoolSet
SetTrue
  (BoolSet
SetFalse , BoolSet
SetTrue  ) -> BoolSet
SetFalse
  (BoolSet
SetFalse , BoolSet
SetFalse ) -> BoolSet
SetEmpty
\\ :: BoolSet -> BoolSet -> BoolSet
(\\)       = BoolSet -> BoolSet -> BoolSet
difference

intersection ::  BoolSet -> BoolSet -> BoolSet
intersection :: BoolSet -> BoolSet -> BoolSet
intersection = forall a b c. ((a, b) -> c) -> a -> b -> c
curry forall a b. (a -> b) -> a -> b
$ \case
  (BoolSet
SetEmpty , BoolSet
_        ) -> BoolSet
SetEmpty
  (BoolSet
_        , BoolSet
SetEmpty ) -> BoolSet
SetEmpty
  (BoolSet
SetBoth  , BoolSet
s        ) -> BoolSet
s
  (BoolSet
s        , BoolSet
SetBoth  ) -> BoolSet
s
  (BoolSet
SetTrue  , BoolSet
SetTrue  ) -> BoolSet
SetTrue
  (BoolSet
SetFalse , BoolSet
SetTrue  ) -> BoolSet
SetEmpty
  (BoolSet
SetTrue  , BoolSet
SetFalse ) -> BoolSet
SetEmpty
  (BoolSet
SetFalse , BoolSet
SetFalse ) -> BoolSet
SetFalse

union ::  BoolSet -> BoolSet -> BoolSet
union :: BoolSet -> BoolSet -> BoolSet
union = forall a b c. ((a, b) -> c) -> a -> b -> c
curry forall a b. (a -> b) -> a -> b
$ \case
  (BoolSet
SetBoth  , BoolSet
_        ) -> BoolSet
SetBoth
  (BoolSet
_        , BoolSet
SetBoth  ) -> BoolSet
SetBoth
  (BoolSet
SetEmpty , BoolSet
s        ) -> BoolSet
s
  (BoolSet
s        , BoolSet
SetEmpty ) -> BoolSet
s
  (BoolSet
SetTrue  , BoolSet
SetTrue  ) -> BoolSet
SetTrue
  (BoolSet
SetFalse , BoolSet
SetTrue  ) -> BoolSet
SetBoth
  (BoolSet
SetTrue  , BoolSet
SetFalse ) -> BoolSet
SetBoth
  (BoolSet
SetFalse , BoolSet
SetFalse ) -> BoolSet
SetFalse

-- * Conversion

elems, toList, toAscList :: BoolSet -> [Bool]
elems :: BoolSet -> [Bool]
elems     = \case
  BoolSet
SetEmpty -> []
  BoolSet
SetTrue  -> [Bool
True]
  BoolSet
SetFalse -> [Bool
False]
  BoolSet
SetBoth  -> [Bool
False, Bool
True]
toList :: BoolSet -> [Bool]
toList    = BoolSet -> [Bool]
elems
toAscList :: BoolSet -> [Bool]
toAscList = BoolSet -> [Bool]
elems

fromList, fromAscList, fromDistinctAscList :: [Bool] -> BoolSet
fromList :: [Bool] -> BoolSet
fromList            = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Bool -> BoolSet -> BoolSet
insert BoolSet
SetEmpty
fromAscList :: [Bool] -> BoolSet
fromAscList         = [Bool] -> BoolSet
fromList
fromDistinctAscList :: [Bool] -> BoolSet
fromDistinctAscList = \case
  []            -> BoolSet
SetEmpty
  [Bool
False]       -> BoolSet
SetFalse
  [Bool
True]        -> BoolSet
SetTrue
  [Bool
False, Bool
True] -> BoolSet
SetBoth
  [Bool]
_             -> forall a. HasCallStack => a
__IMPOSSIBLE__