From ed90d447a86c203d72d5c4b9d6731eaf71043b3f Mon Sep 17 00:00:00 2001 From: Peter Achten Date: Fri, 15 May 2020 09:45:42 +0200 Subject: [PATCH] replace instance == with isEqualBy to ensure consistency in using a single ordering function to determine equivalence --- src/libraries/OS-Independent/Data/SetBy.dcl | 11 +++++++++-- src/libraries/OS-Independent/Data/SetBy.icl | 12 ++++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/libraries/OS-Independent/Data/SetBy.dcl b/src/libraries/OS-Independent/Data/SetBy.dcl index 184d25e7..014da7ea 100644 --- a/src/libraries/OS-Independent/Data/SetBy.dcl +++ b/src/libraries/OS-Independent/Data/SetBy.dcl @@ -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 diff --git a/src/libraries/OS-Independent/Data/SetBy.icl b/src/libraries/OS-Independent/Data/SetBy.icl index d6e37ae6..b2be3b74 100644 --- a/src/libraries/OS-Independent/Data/SetBy.icl +++ b/src/libraries/OS-Independent/Data/SetBy.icl @@ -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) -- GitLab