From 5c9db20249ebcf18640af8aa73c309833ba2bbb1 Mon Sep 17 00:00:00 2001 From: Maximilian Algehed Date: Fri, 14 Nov 2025 08:32:38 +0100 Subject: [PATCH 1/2] Use nubOrd when possible --- src/Constrained/Core.hs | 11 +++++++++++ src/Constrained/NumOrd.hs | 15 ++------------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Constrained/Core.hs b/src/Constrained/Core.hs index 034e174..3d6c92b 100644 --- a/src/Constrained/Core.hs +++ b/src/Constrained/Core.hs @@ -22,6 +22,7 @@ module Constrained.Core ( NonEmpty ((:|)), Evidence (..), unionWithMaybe, + nubOrd, ) where import Constrained.List ( @@ -130,3 +131,13 @@ instance Typeable c => Show (Evidence c) where unionWithMaybe :: (a -> a -> a) -> Maybe a -> Maybe a -> Maybe a unionWithMaybe f ma ma' = (f <$> ma <*> ma') <|> ma <|> ma' +-- | Strip out duplicates (in n-log(n) time, by building an intermediate Set) +nubOrd :: Ord a => [a] -> [a] +nubOrd = + loop mempty + where + loop _ [] = [] + loop s (a : as) + | a `Set.member` s = loop s as + | otherwise = + let s' = Set.insert a s in s' `seq` a : loop s' as diff --git a/src/Constrained/NumOrd.hs b/src/Constrained/NumOrd.hs index 7ae77ea..b737ae0 100644 --- a/src/Constrained/NumOrd.hs +++ b/src/Constrained/NumOrd.hs @@ -56,7 +56,7 @@ module Constrained.NumOrd ( import Constrained.AbstractSyntax import Constrained.Base import Constrained.Conformance -import Constrained.Core (Value (..), unionWithMaybe) +import Constrained.Core (Value (..), unionWithMaybe, nubOrd) import Constrained.FunctionSymbol import Constrained.GenT import Constrained.Generic @@ -362,17 +362,6 @@ conformsToNumSpec i (NumSpecInterval ml mu) = maybe True (<= i) ml && maybe True -- implementations are found here -- ===================================================================== --- | Strip out duplicates (in n-log(n) time, by building an intermediate Set) -nubOrd :: Ord a => [a] -> [a] -nubOrd = - loop mempty - where - loop _ [] = [] - loop s (a : as) - | a `Set.member` s = loop s as - | otherwise = - let s' = Set.insert a s in s' `seq` a : loop s' as - -- | Builds a MemberSpec, but returns an Error spec if the list is empty nubOrdMemberSpec :: Ord a => String -> [a] -> Specification a nubOrdMemberSpec message xs = @@ -936,7 +925,7 @@ instance Logic IntW where propagateMemberSpec AddW (HOLE :<: i) es = memberSpec - (nub $ mapMaybe (safeSubtract i) (NE.toList es)) + (nubOrd $ mapMaybe (safeSubtract i) (NE.toList es)) ( NE.fromList [ "propagateSpecFn on (" ++ show i ++ " +. HOLE)" , "The Spec is a MemberSpec = " ++ show es -- show (MemberSpec @HasSpec @TS es) From e60e86c9a68e49455b8baf3f2055a41830bfd366 Mon Sep 17 00:00:00 2001 From: Maximilian Algehed Date: Thu, 20 Nov 2025 14:01:42 +0100 Subject: [PATCH 2/2] use containers nubOrd --- src/Constrained/Core.hs | 12 ------------ src/Constrained/NumOrd.hs | 3 ++- 2 files changed, 2 insertions(+), 13 deletions(-) diff --git a/src/Constrained/Core.hs b/src/Constrained/Core.hs index 3d6c92b..e4f8497 100644 --- a/src/Constrained/Core.hs +++ b/src/Constrained/Core.hs @@ -22,7 +22,6 @@ module Constrained.Core ( NonEmpty ((:|)), Evidence (..), unionWithMaybe, - nubOrd, ) where import Constrained.List ( @@ -130,14 +129,3 @@ instance Typeable c => Show (Evidence c) where -- | Take the union of two `Maybe` values with a given union operator unionWithMaybe :: (a -> a -> a) -> Maybe a -> Maybe a -> Maybe a unionWithMaybe f ma ma' = (f <$> ma <*> ma') <|> ma <|> ma' - --- | Strip out duplicates (in n-log(n) time, by building an intermediate Set) -nubOrd :: Ord a => [a] -> [a] -nubOrd = - loop mempty - where - loop _ [] = [] - loop s (a : as) - | a `Set.member` s = loop s as - | otherwise = - let s' = Set.insert a s in s' `seq` a : loop s' as diff --git a/src/Constrained/NumOrd.hs b/src/Constrained/NumOrd.hs index b737ae0..8314392 100644 --- a/src/Constrained/NumOrd.hs +++ b/src/Constrained/NumOrd.hs @@ -56,7 +56,7 @@ module Constrained.NumOrd ( import Constrained.AbstractSyntax import Constrained.Base import Constrained.Conformance -import Constrained.Core (Value (..), unionWithMaybe, nubOrd) +import Constrained.Core (Value (..), unionWithMaybe) import Constrained.FunctionSymbol import Constrained.GenT import Constrained.Generic @@ -64,6 +64,7 @@ import Constrained.List import Constrained.PrettyUtils import Control.Applicative ((<|>)) import Control.Arrow (first) +import Data.Containers.ListUtils import Data.Foldable import Data.Kind import Data.List (nub)