{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses #-}
module Test.Matroid.Helpers where
import Data.List (foldl')
import Data.Set (Set)
import qualified Data.Set as S
isMonotoneUnitIncreasing :: Ord a => (Set a -> Int)
-> [a]
-> Bool
isMonotoneUnitIncreasing :: (Set a -> Int) -> [a] -> Bool
isMonotoneUnitIncreasing Set a -> Int
rk [a]
e = Bool
result
where (Set a
_,Bool
result,Int
_) = ((Set a, Bool, Int) -> a -> (Set a, Bool, Int))
-> (Set a, Bool, Int) -> [a] -> (Set a, Bool, Int)
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (Set a, Bool, Int) -> a -> (Set a, Bool, Int)
checkStep (forall a. Set a
S.empty :: Set a, Bool
True, Int
0 :: Int) [a]
e
checkStep :: (Set a, Bool, Int) -> a -> (Set a, Bool, Int)
checkStep (Set a
x0,Bool
False,Int
r) a
_ = (Set a
x0,Bool
False,Int
r)
checkStep (Set a
x0,Bool
True,Int
r) a
x = let x1 :: Set a
x1 = a -> Set a -> Set a
forall a. Ord a => a -> Set a -> Set a
S.insert a
x Set a
x0
r1 :: Int
r1 = Set a -> Int
rk Set a
x1
in (Set a
x1, (Int
r Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
r1) Bool -> Bool -> Bool
&& (Int
r1 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
r Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1), Int
r1)
isMonotoneDecreasingBool :: Ord a => (Set a -> Bool)
-> [a]
-> Bool
isMonotoneDecreasingBool :: (Set a -> Bool) -> [a] -> Bool
isMonotoneDecreasingBool Set a -> Bool
indep [a]
e = Bool
result
where (Set a
_,Bool
result,Bool
_) = ((Set a, Bool, Bool) -> a -> (Set a, Bool, Bool))
-> (Set a, Bool, Bool) -> [a] -> (Set a, Bool, Bool)
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (Set a, Bool, Bool) -> a -> (Set a, Bool, Bool)
checkStep (forall a. Set a
S.empty :: Set a, Bool
True, Bool
True) [a]
e
checkStep :: (Set a, Bool, Bool) -> a -> (Set a, Bool, Bool)
checkStep (Set a
x0,Bool
False,Bool
v) a
_ = (Set a
x0,Bool
False,Bool
v)
checkStep (Set a
x0,Bool
True,Bool
True) a
x = let x1 :: Set a
x1 = a -> Set a -> Set a
forall a. Ord a => a -> Set a -> Set a
S.insert a
x Set a
x0
v1 :: Bool
v1 = Set a -> Bool
indep Set a
x1
in (Set a
x1,Bool
True,Bool
v1)
checkStep (Set a
x0,Bool
True,Bool
False) a
x = let x1 :: Set a
x1 = a -> Set a -> Set a
forall a. Ord a => a -> Set a -> Set a
S.insert a
x Set a
x0
v1 :: Bool
v1 = Set a -> Bool
indep Set a
x1
in (Set a
x1,Bool
v1 Bool -> Bool -> Bool
forall a. Eq a => a -> a -> Bool
== Bool
False,Bool
False)
hasIndepExchangeProperty :: Ord a => (Set a -> Bool)
-> Set a
-> Set a
-> Bool
hasIndepExchangeProperty :: (Set a -> Bool) -> Set a -> Set a -> Bool
hasIndepExchangeProperty Set a -> Bool
indep Set a
x Set a
y
| Bool -> Bool
not ((Set a -> Bool
indep Set a
x) Bool -> Bool -> Bool
&& (Set a -> Bool
indep Set a
y)) = Bool
True
| Set a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length Set a
x Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Set a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length Set a
y = Bool
True
| Set a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length Set a
x Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Set a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length Set a
y = Set a -> [a] -> Bool
check Set a
y ([a] -> Bool) -> [a] -> Bool
forall a b. (a -> b) -> a -> b
$ Set a -> [a]
forall a. Set a -> [a]
S.toList (Set a -> [a]) -> Set a -> [a]
forall a b. (a -> b) -> a -> b
$ Set a
x Set a -> Set a -> Set a
forall a. Ord a => Set a -> Set a -> Set a
`S.difference` Set a
y
| Bool
otherwise = Set a -> [a] -> Bool
check Set a
x ([a] -> Bool) -> [a] -> Bool
forall a b. (a -> b) -> a -> b
$ Set a -> [a]
forall a. Set a -> [a]
S.toList (Set a -> [a]) -> Set a -> [a]
forall a b. (a -> b) -> a -> b
$ Set a
y Set a -> Set a -> Set a
forall a. Ord a => Set a -> Set a -> Set a
`S.difference` Set a
x
where check :: Set a -> [a] -> Bool
check Set a
x_ (a
x0:[a]
xs)
| Set a -> Bool
indep (Set a -> Bool) -> Set a -> Bool
forall a b. (a -> b) -> a -> b
$ Set a
x_ Set a -> Set a -> Set a
forall a. Ord a => Set a -> Set a -> Set a
`S.union` a -> Set a
forall a. a -> Set a
S.singleton a
x0 = Bool
True
| Bool
otherwise = Set a -> [a] -> Bool
check Set a
x_ [a]
xs
check Set a
_ [a]
_ = Bool
False
isIsotoneSetMap :: Ord a => (Set a -> Set a)
-> [a]
-> Bool
isIsotoneSetMap :: (Set a -> Set a) -> [a] -> Bool
isIsotoneSetMap Set a -> Set a
cl [a]
e = Bool
result
where (Set a
_,Bool
result,Set a
_) = ((Set a, Bool, Set a) -> a -> (Set a, Bool, Set a))
-> (Set a, Bool, Set a) -> [a] -> (Set a, Bool, Set a)
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (Set a, Bool, Set a) -> a -> (Set a, Bool, Set a)
checkStep (forall a. Set a
S.empty :: Set a, Bool
True, forall a. Set a
S.empty :: Set a) [a]
e
checkStep :: (Set a, Bool, Set a) -> a -> (Set a, Bool, Set a)
checkStep (Set a
x0,Bool
False,Set a
c0) a
_ = (Set a
x0,Bool
False,Set a
c0)
checkStep (Set a
x0, Bool
True,Set a
c0) a
x = let x1 :: Set a
x1 = a -> Set a -> Set a
forall a. Ord a => a -> Set a -> Set a
S.insert a
x Set a
x0
c1 :: Set a
c1 = Set a -> Set a
cl Set a
x1
isSuperset :: Bool
isSuperset = Set a
c0 Set a -> Set a -> Bool
forall a. Ord a => Set a -> Set a -> Bool
`S.isSubsetOf` Set a
c1
in (Set a
x1,Bool
isSuperset,Set a
c1)