{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
module Data.POSet.Internal where
import Algebra.PartialOrd
import Control.DeepSeq (NFData (rnf))
import qualified Data.List as List
import Data.POMap.Lazy (POMap)
import qualified Data.POMap.Lazy as POMap
import GHC.Exts (coerce)
import qualified GHC.Exts
import Text.Read (Lexeme (Ident), Read (..), lexP, parens,
prec, readListPrecDefault)
newtype POSet k
= POSet (POMap k ())
instance PartialOrd k => Eq (POSet k) where
POSet POMap k ()
a == :: POSet k -> POSet k -> Bool
== POSet POMap k ()
b = POMap k ()
a POMap k () -> POMap k () -> Bool
forall a. Eq a => a -> a -> Bool
== POMap k ()
b
instance PartialOrd k => PartialOrd (POSet k) where
POSet POMap k ()
a leq :: POSet k -> POSet k -> Bool
`leq` POSet POMap k ()
b = POMap k ()
a POMap k () -> POMap k () -> Bool
forall a. PartialOrd a => a -> a -> Bool
`leq` POMap k ()
b
instance Show a => Show (POSet a) where
showsPrec :: Int -> POSet a -> ShowS
showsPrec Int
p POSet a
xs = Bool -> ShowS -> ShowS
showParen (Int
p Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
10) (ShowS -> ShowS) -> ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$
String -> ShowS
showString String
"fromList " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> ShowS
forall a. Show a => a -> ShowS
shows (POSet a -> [a]
forall k. POSet k -> [k]
toList POSet a
xs)
instance (Read a, PartialOrd a) => Read (POSet a) where
readPrec :: ReadPrec (POSet a)
readPrec = ReadPrec (POSet a) -> ReadPrec (POSet a)
forall a. ReadPrec a -> ReadPrec a
parens (ReadPrec (POSet a) -> ReadPrec (POSet a))
-> ReadPrec (POSet a) -> ReadPrec (POSet a)
forall a b. (a -> b) -> a -> b
$ Int -> ReadPrec (POSet a) -> ReadPrec (POSet a)
forall a. Int -> ReadPrec a -> ReadPrec a
prec Int
10 (ReadPrec (POSet a) -> ReadPrec (POSet a))
-> ReadPrec (POSet a) -> ReadPrec (POSet a)
forall a b. (a -> b) -> a -> b
$ do
Ident String
"fromList" <- ReadPrec Lexeme
lexP
[a] -> POSet a
forall k. PartialOrd k => [k] -> POSet k
fromList ([a] -> POSet a) -> ReadPrec [a] -> ReadPrec (POSet a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ReadPrec [a]
forall a. Read a => ReadPrec a
readPrec
readListPrec :: ReadPrec [POSet a]
readListPrec = ReadPrec [POSet a]
forall a. Read a => ReadPrec [a]
readListPrecDefault
instance NFData a => NFData (POSet a) where
rnf :: POSet a -> ()
rnf (POSet POMap a ()
m) = POMap a () -> ()
forall a. NFData a => a -> ()
rnf POMap a ()
m
instance Foldable POSet where
foldr :: (a -> b -> b) -> b -> POSet a -> b
foldr a -> b -> b
f = (b -> POMap a () -> b) -> b -> POSet a -> b
coerce ((a -> () -> b -> b) -> b -> POMap a () -> b
forall k a b. (k -> a -> b -> b) -> b -> POMap k a -> b
POMap.foldrWithKey @_ @() (\a
k ()
_ b
acc -> a -> b -> b
f a
k b
acc))
{-# INLINE foldr #-}
foldl :: (b -> a -> b) -> b -> POSet a -> b
foldl b -> a -> b
f = (b -> POMap a () -> b) -> b -> POSet a -> b
coerce ((b -> a -> () -> b) -> b -> POMap a () -> b
forall b k a. (b -> k -> a -> b) -> b -> POMap k a -> b
POMap.foldlWithKey @_ @_ @() (\b
k a
acc ()
_ -> b -> a -> b
f b
k a
acc))
{-# INLINE foldl #-}
null :: POSet a -> Bool
null POSet a
m = POSet a -> Int
forall a. POSet a -> Int
size POSet a
m Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
{-# INLINE null #-}
length :: POSet a -> Int
length = POSet a -> Int
forall a. POSet a -> Int
size
{-# INLINE length #-}
instance PartialOrd k => GHC.Exts.IsList (POSet k) where
type Item (POSet k) = k
fromList :: [Item (POSet k)] -> POSet k
fromList = [Item (POSet k)] -> POSet k
forall k. PartialOrd k => [k] -> POSet k
fromList
toList :: POSet k -> [Item (POSet k)]
toList = POSet k -> [Item (POSet k)]
forall k. POSet k -> [k]
toList
size :: POSet k -> Int
size :: POSet k -> Int
size = (POMap k () -> Int) -> POSet k -> Int
coerce (POMap k () -> Int
forall k v. POMap k v -> Int
POMap.size @_ @())
{-# INLINE size #-}
width :: POSet k -> Int
width :: POSet k -> Int
width = (POMap k () -> Int) -> POSet k -> Int
coerce (POMap k () -> Int
forall k v. POMap k v -> Int
POMap.width @_ @())
{-# INLINE width #-}
member :: PartialOrd k => k -> POSet k -> Bool
member :: k -> POSet k -> Bool
member = (k -> POMap k () -> Bool) -> k -> POSet k -> Bool
coerce (PartialOrd k => k -> POMap k () -> Bool
forall k v. PartialOrd k => k -> POMap k v -> Bool
POMap.member @_ @())
{-# INLINE member #-}
notMember :: PartialOrd k => k -> POSet k -> Bool
notMember :: k -> POSet k -> Bool
notMember = (k -> POMap k () -> Bool) -> k -> POSet k -> Bool
coerce (PartialOrd k => k -> POMap k () -> Bool
forall k v. PartialOrd k => k -> POMap k v -> Bool
POMap.notMember @_ @())
{-# INLINE notMember #-}
lookupLT :: PartialOrd k => k -> POSet k -> [k]
lookupLT :: k -> POSet k -> [k]
lookupLT k
k = ((k, ()) -> k) -> [(k, ())] -> [k]
forall a b. (a -> b) -> [a] -> [b]
List.map @(_,()) (k, ()) -> k
forall a b. (a, b) -> a
fst ([(k, ())] -> [k]) -> (POSet k -> [(k, ())]) -> POSet k -> [k]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (POMap k () -> [(k, ())]) -> POSet k -> [(k, ())]
coerce (k -> POMap k () -> [(k, ())]
forall k v. PartialOrd k => k -> POMap k v -> [(k, v)]
POMap.lookupLT @_ @() k
k)
{-# INLINE lookupLT #-}
lookupLE :: PartialOrd k => k -> POSet k -> [k]
lookupLE :: k -> POSet k -> [k]
lookupLE k
k = ((k, ()) -> k) -> [(k, ())] -> [k]
forall a b. (a -> b) -> [a] -> [b]
List.map @(_,()) (k, ()) -> k
forall a b. (a, b) -> a
fst ([(k, ())] -> [k]) -> (POSet k -> [(k, ())]) -> POSet k -> [k]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (POMap k () -> [(k, ())]) -> POSet k -> [(k, ())]
coerce (k -> POMap k () -> [(k, ())]
forall k v. PartialOrd k => k -> POMap k v -> [(k, v)]
POMap.lookupLE @_ @() k
k)
{-# INLINE lookupLE #-}
lookupGE :: PartialOrd k => k -> POSet k -> [k]
lookupGE :: k -> POSet k -> [k]
lookupGE k
k = ((k, ()) -> k) -> [(k, ())] -> [k]
forall a b. (a -> b) -> [a] -> [b]
List.map @(_,()) (k, ()) -> k
forall a b. (a, b) -> a
fst ([(k, ())] -> [k]) -> (POSet k -> [(k, ())]) -> POSet k -> [k]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (POMap k () -> [(k, ())]) -> POSet k -> [(k, ())]
coerce (k -> POMap k () -> [(k, ())]
forall k v. PartialOrd k => k -> POMap k v -> [(k, v)]
POMap.lookupGE @_ @() k
k)
{-# INLINE lookupGE #-}
lookupGT :: PartialOrd k => k -> POSet k -> [k]
lookupGT :: k -> POSet k -> [k]
lookupGT k
k = ((k, ()) -> k) -> [(k, ())] -> [k]
forall a b. (a -> b) -> [a] -> [b]
List.map @(_,()) (k, ()) -> k
forall a b. (a, b) -> a
fst ([(k, ())] -> [k]) -> (POSet k -> [(k, ())]) -> POSet k -> [k]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (POMap k () -> [(k, ())]) -> POSet k -> [(k, ())]
coerce (k -> POMap k () -> [(k, ())]
forall k v. PartialOrd k => k -> POMap k v -> [(k, v)]
POMap.lookupGT @_ @() k
k)
{-# INLINE lookupGT #-}
isSubsetOf :: PartialOrd k => POSet k -> POSet k -> Bool
isSubsetOf :: POSet k -> POSet k -> Bool
isSubsetOf = (POMap k () -> POMap k () -> Bool) -> POSet k -> POSet k -> Bool
coerce ((PartialOrd k, Eq ()) => POMap k () -> POMap k () -> Bool
forall k v. (PartialOrd k, Eq v) => POMap k v -> POMap k v -> Bool
POMap.isSubmapOf @_ @())
{-# INLINE isSubsetOf #-}
isProperSubsetOf :: PartialOrd k => POSet k -> POSet k -> Bool
isProperSubsetOf :: POSet k -> POSet k -> Bool
isProperSubsetOf = (POMap k () -> POMap k () -> Bool) -> POSet k -> POSet k -> Bool
coerce ((PartialOrd k, Eq ()) => POMap k () -> POMap k () -> Bool
forall k v. (PartialOrd k, Eq v) => POMap k v -> POMap k v -> Bool
POMap.isProperSubmapOf @_ @())
{-# INLINE isProperSubsetOf #-}
empty :: POSet k
empty :: POSet k
empty = POMap k () -> POSet k
forall k. POMap k () -> POSet k
POSet POMap k ()
forall k v. POMap k v
POMap.empty
{-# INLINE empty #-}
singleton :: k -> POSet k
singleton :: k -> POSet k
singleton k
k = POMap k () -> POSet k
forall k. POMap k () -> POSet k
POSet (k -> () -> POMap k ()
forall k v. k -> v -> POMap k v
POMap.singleton k
k ())
{-# INLINE singleton #-}
insert :: (PartialOrd k) => k -> POSet k -> POSet k
insert :: k -> POSet k -> POSet k
insert k
k = (POMap k () -> POMap k ()) -> POSet k -> POSet k
coerce (k -> () -> POMap k () -> POMap k ()
forall k v. PartialOrd k => k -> v -> POMap k v -> POMap k v
POMap.insert k
k ())
{-# INLINE insert #-}
delete :: (PartialOrd k) => k -> POSet k -> POSet k
delete :: k -> POSet k -> POSet k
delete = (k -> POMap k () -> POMap k ()) -> k -> POSet k -> POSet k
coerce (PartialOrd k => k -> POMap k () -> POMap k ()
forall k v. PartialOrd k => k -> POMap k v -> POMap k v
POMap.delete @_ @())
{-# INLINE delete #-}
union :: PartialOrd k => POSet k -> POSet k -> POSet k
union :: POSet k -> POSet k -> POSet k
union = (POMap k () -> POMap k () -> POMap k ())
-> POSet k -> POSet k -> POSet k
coerce (PartialOrd k => POMap k () -> POMap k () -> POMap k ()
forall k v. PartialOrd k => POMap k v -> POMap k v -> POMap k v
POMap.union @_ @())
{-# INLINE union #-}
unions :: PartialOrd k => [POSet k] -> POSet k
unions :: [POSet k] -> POSet k
unions = ([POMap k ()] -> POMap k ()) -> [POSet k] -> POSet k
coerce (PartialOrd k => [POMap k ()] -> POMap k ()
forall k v. PartialOrd k => [POMap k v] -> POMap k v
POMap.unions @_ @())
{-# INLINE unions #-}
difference :: PartialOrd k => POSet k -> POSet k -> POSet k
difference :: POSet k -> POSet k -> POSet k
difference = (POMap k () -> POMap k () -> POMap k ())
-> POSet k -> POSet k -> POSet k
coerce (PartialOrd k => POMap k () -> POMap k () -> POMap k ()
forall k a b. PartialOrd k => POMap k a -> POMap k b -> POMap k a
POMap.difference @_ @() @())
{-# INLINE difference #-}
intersection :: PartialOrd k => POSet k -> POSet k -> POSet k
intersection :: POSet k -> POSet k -> POSet k
intersection = (POMap k () -> POMap k () -> POMap k ())
-> POSet k -> POSet k -> POSet k
coerce (PartialOrd k => POMap k () -> POMap k () -> POMap k ()
forall k a b. PartialOrd k => POMap k a -> POMap k b -> POMap k a
POMap.intersection @_ @() @())
{-# INLINE intersection #-}
filter :: (k -> Bool) -> POSet k -> POSet k
filter :: (k -> Bool) -> POSet k -> POSet k
filter k -> Bool
f = (POMap k () -> POMap k ()) -> POSet k -> POSet k
coerce ((k -> () -> Bool) -> POMap k () -> POMap k ()
forall k v. (k -> v -> Bool) -> POMap k v -> POMap k v
POMap.filterWithKey @_ @() (\k
k ()
_ -> k -> Bool
f k
k))
{-# INLINE filter #-}
partition :: (k -> Bool) -> POSet k -> (POSet k, POSet k)
partition :: (k -> Bool) -> POSet k -> (POSet k, POSet k)
partition k -> Bool
f = (POMap k () -> (POMap k (), POMap k ()))
-> POSet k -> (POSet k, POSet k)
coerce ((k -> () -> Bool) -> POMap k () -> (POMap k (), POMap k ())
forall k v. (k -> v -> Bool) -> POMap k v -> (POMap k v, POMap k v)
POMap.partitionWithKey @_ @() (\k
k ()
_ -> k -> Bool
f k
k))
{-# INLINE partition #-}
takeWhileAntitone :: (k -> Bool) -> POSet k -> POSet k
takeWhileAntitone :: (k -> Bool) -> POSet k -> POSet k
takeWhileAntitone = ((k -> Bool) -> POMap k () -> POMap k ())
-> (k -> Bool) -> POSet k -> POSet k
coerce ((k -> Bool) -> POMap k () -> POMap k ()
forall k v. (k -> Bool) -> POMap k v -> POMap k v
POMap.takeWhileAntitone @_ @())
dropWhileAntitone :: (k -> Bool) -> POSet k -> POSet k
dropWhileAntitone :: (k -> Bool) -> POSet k -> POSet k
dropWhileAntitone = ((k -> Bool) -> POMap k () -> POMap k ())
-> (k -> Bool) -> POSet k -> POSet k
coerce ((k -> Bool) -> POMap k () -> POMap k ()
forall k v. (k -> Bool) -> POMap k v -> POMap k v
POMap.dropWhileAntitone @_ @())
spanAntitone :: (k -> Bool) -> POSet k -> (POSet k, POSet k)
spanAntitone :: (k -> Bool) -> POSet k -> (POSet k, POSet k)
spanAntitone = ((k -> Bool) -> POMap k () -> (POMap k (), POMap k ()))
-> (k -> Bool) -> POSet k -> (POSet k, POSet k)
coerce ((k -> Bool) -> POMap k () -> (POMap k (), POMap k ())
forall k v. (k -> Bool) -> POMap k v -> (POMap k v, POMap k v)
POMap.spanAntitone @_ @())
map :: PartialOrd k2 => (k1 -> k2) -> POSet k1 -> POSet k2
map :: (k1 -> k2) -> POSet k1 -> POSet k2
map = ((k1 -> k2) -> POMap k1 () -> POMap k2 ())
-> (k1 -> k2) -> POSet k1 -> POSet k2
coerce (PartialOrd k2 => (k1 -> k2) -> POMap k1 () -> POMap k2 ()
forall k2 k1 v.
PartialOrd k2 =>
(k1 -> k2) -> POMap k1 v -> POMap k2 v
POMap.mapKeys @_ @_ @())
{-# INLINE map #-}
mapMonotonic :: (k1 -> k2) -> POSet k1 -> POSet k2
mapMonotonic :: (k1 -> k2) -> POSet k1 -> POSet k2
mapMonotonic = ((k1 -> k2) -> POMap k1 () -> POMap k2 ())
-> (k1 -> k2) -> POSet k1 -> POSet k2
coerce ((k1 -> k2) -> POMap k1 () -> POMap k2 ()
forall k1 k2 v. (k1 -> k2) -> POMap k1 v -> POMap k2 v
POMap.mapKeysMonotonic @_ @_ @())
{-# INLINE mapMonotonic #-}
foldr' :: (a -> b -> b) -> b -> POSet a -> b
foldr' :: (a -> b -> b) -> b -> POSet a -> b
foldr' a -> b -> b
f = (b -> POMap a () -> b) -> b -> POSet a -> b
coerce ((a -> () -> b -> b) -> b -> POMap a () -> b
forall k a b. (k -> a -> b -> b) -> b -> POMap k a -> b
POMap.foldrWithKey' @_ @() (\a
k ()
_ b
acc -> a -> b -> b
f a
k b
acc))
{-# INLINE foldr' #-}
foldl' :: (b -> a -> b) -> b -> POSet a -> b
foldl' :: (b -> a -> b) -> b -> POSet a -> b
foldl' b -> a -> b
f = (b -> POMap a () -> b) -> b -> POSet a -> b
coerce ((b -> a -> () -> b) -> b -> POMap a () -> b
forall b k a. (b -> k -> a -> b) -> b -> POMap k a -> b
POMap.foldlWithKey' @_ @_ @() (\b
k a
acc ()
_ -> b -> a -> b
f b
k a
acc))
{-# INLINE foldl' #-}
lookupMin :: PartialOrd k => POSet k -> [k]
lookupMin :: POSet k -> [k]
lookupMin = ((k, ()) -> k) -> [(k, ())] -> [k]
forall a b. (a -> b) -> [a] -> [b]
List.map @(_,()) (k, ()) -> k
forall a b. (a, b) -> a
fst ([(k, ())] -> [k]) -> (POSet k -> [(k, ())]) -> POSet k -> [k]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (POMap k () -> [(k, ())]) -> POSet k -> [(k, ())]
coerce (PartialOrd k => POMap k () -> [(k, ())]
forall k v. PartialOrd k => POMap k v -> [(k, v)]
POMap.lookupMin @_ @())
{-# INLINE lookupMin #-}
lookupMax :: PartialOrd k => POSet k -> [k]
lookupMax :: POSet k -> [k]
lookupMax = ((k, ()) -> k) -> [(k, ())] -> [k]
forall a b. (a -> b) -> [a] -> [b]
List.map @(_,()) (k, ()) -> k
forall a b. (a, b) -> a
fst ([(k, ())] -> [k]) -> (POSet k -> [(k, ())]) -> POSet k -> [k]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (POMap k () -> [(k, ())]) -> POSet k -> [(k, ())]
coerce (PartialOrd k => POMap k () -> [(k, ())]
forall k v. PartialOrd k => POMap k v -> [(k, v)]
POMap.lookupMax @_ @())
{-# INLINE lookupMax #-}
elems :: POSet k -> [k]
elems :: POSet k -> [k]
elems = (POMap k () -> [k]) -> POSet k -> [k]
coerce (POMap k () -> [k]
forall k v. POMap k v -> [k]
POMap.keys @_ @())
{-# INLINE elems #-}
toList :: POSet k -> [k]
toList :: POSet k -> [k]
toList = (POMap k () -> [k]) -> POSet k -> [k]
coerce (POMap k () -> [k]
forall k v. POMap k v -> [k]
POMap.keys @_ @())
{-# INLINE toList #-}
fromList :: (PartialOrd k) => [k] -> POSet k
fromList :: [k] -> POSet k
fromList = ([(k, ())] -> POMap k ()) -> [(k, ())] -> POSet k
coerce (PartialOrd k => [(k, ())] -> POMap k ()
forall k v. PartialOrd k => [(k, v)] -> POMap k v
POMap.fromList @_ @()) ([(k, ())] -> POSet k) -> ([k] -> [(k, ())]) -> [k] -> POSet k
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (k -> (k, ())) -> [k] -> [(k, ())]
forall a b. (a -> b) -> [a] -> [b]
List.map (, ())
{-# INLINE fromList #-}