Created
November 13, 2015 05:32
-
-
Save thsutton/aa5ca030ad54dba762fb to your computer and use it in GitHub Desktop.
Compare sets by inclusion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Set where | |
import Data.Set (Set) | |
import qualified Data.Set as S | |
-- | Compare two 'Set's by inclusion or, failing that, by size. | |
compareInclusion :: (Ord e) => Set e -> Set e -> Ordering | |
compareInclusion s1 s2 = | |
let lt = S.isSubsetOf s1 s2 | |
gt = S.isSubsetOf s2 s1 | |
in case (lt, gt) of | |
(True, True) -> EQ | |
(True, False) -> LT | |
(False, True) -> GT | |
(False, False) -> compare (S.size s1) (S.size s2) | |
-- | A set should be 'LT' a larger set. | |
prop_inclusion_order_lt :: [Int] -> Bool | |
prop_inclusion_order_lt l = | |
let s1 = S.fromList l | |
s2 = S.insert 19191 s1 | |
in LT == compareInclusion s1 s2 | |
-- | A larger set should be 'GT' a smaller set. | |
prop_inclusion_order_gt :: [Int] -> Bool | |
prop_inclusion_order_gt l = | |
let s1 = S.fromList l | |
s2 = S.insert 19191 s1 | |
in GT == compareInclusion s2 s1 | |
-- | A set should be 'EQ' to itself. | |
prop_inclusion_order_eq :: [Int] -> Bool | |
prop_inclusion_order_eq l = | |
let s1 = S.fromList l | |
in EQ == compareInclusion s1 s1 | |
-- | A set should be 'EQ' another with the same size. | |
prop_inclusion_order_nc :: [Int] -> Bool | |
prop_inclusion_order_nc l = | |
let s1 = S.fromList l | |
s2 = S.fromList $ map negate l | |
in EQ == compareInclusion s1 s2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment