Verified Commit e2336345 authored by Peter Achten's avatar Peter Achten Committed by Camil Staps

replace instance == with isEqualBy to ensure consistency in using a single...

replace instance == with isEqualBy to ensure consistency in using a single ordering function to determine equivalence
parent 33a89156
......@@ -12,7 +12,7 @@ definition module Data.SetBy
* For all documentation, please consult Data.Set.
*
* The `morally equivalent` function from Data.Set is added in the comment. This is not
* strictly equivalent function because of the different types.
* a strictly equivalent function because of the different types.
*
* When using the functions in Data.SetBy, make sure to use the same higher-order
* function parameter for the same data structure to ensure internal integrity.
......@@ -37,7 +37,14 @@ from Data.Foldable import class Foldable
:: SetBy a = TipBy
| BinBy !Int !a !(SetBy a) !(SetBy a)
instance == (SetBy a) | == a
/**
* True iff the two sets have the same number of elements, and these elements
* are pairwise 'equal' as described above, so the higher-order function
* parameter represents < on a, *not* == on a(!)
*
* Morally equivalent function: instance == (Set a) | == a
*/
isEqualBy :: !(a a -> Bool) !(SetBy a) !(SetBy a) -> Bool
/**
* True iff first set is `smaller` than second set, according to
......
......@@ -23,8 +23,16 @@ mapSetByMonotonic f (BinBy n x l r) = BinBy n (f x) (mapSetByMonotonic f l) (map
:: SetBy a = TipBy
| BinBy !Int !a !(SetBy a) !(SetBy a)
instance == (SetBy a) | == a
where (==) t1 t2 = size t1 == size t2 && toAscList t1 == toAscList t2
isEqualBy :: !(a a -> Bool) !(SetBy a) !(SetBy a) -> Bool
isEqualBy comp s1 s2 = size s1 == size s2 && equalEltsBy comp (toAscList s1) (toAscList s2)
where
equalEltsBy :: !(a a -> Bool) ![a] ![a] -> Bool
equalEltsBy _ [] [] = True
equalEltsBy _ [] _ = False
equalEltsBy _ [_:_] [] = False
equalEltsBy comp [a:as] [b:bs]
| comp a b || comp b a = False
| otherwise = equalEltsBy comp as bs
isOrderedBy :: !(a a -> Bool) !(SetBy a) !(SetBy a) -> Bool
isOrderedBy comp s1 s2 = compare comp (toAscList s1) (toAscList s2)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment