Skip to content

Instantly share code, notes, and snippets.

@thsutton
Created November 13, 2015 05:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thsutton/aa5ca030ad54dba762fb to your computer and use it in GitHub Desktop.
Save thsutton/aa5ca030ad54dba762fb to your computer and use it in GitHub Desktop.
Compare sets by inclusion
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