Skip to content

Commit 5c9db20

Browse files
Use nubOrd when possible
1 parent 139b21f commit 5c9db20

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

src/Constrained/Core.hs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module Constrained.Core (
2222
NonEmpty ((:|)),
2323
Evidence (..),
2424
unionWithMaybe,
25+
nubOrd,
2526
) where
2627

2728
import Constrained.List (
@@ -130,3 +131,13 @@ instance Typeable c => Show (Evidence c) where
130131
unionWithMaybe :: (a -> a -> a) -> Maybe a -> Maybe a -> Maybe a
131132
unionWithMaybe f ma ma' = (f <$> ma <*> ma') <|> ma <|> ma'
132133

134+
-- | Strip out duplicates (in n-log(n) time, by building an intermediate Set)
135+
nubOrd :: Ord a => [a] -> [a]
136+
nubOrd =
137+
loop mempty
138+
where
139+
loop _ [] = []
140+
loop s (a : as)
141+
| a `Set.member` s = loop s as
142+
| otherwise =
143+
let s' = Set.insert a s in s' `seq` a : loop s' as

src/Constrained/NumOrd.hs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ module Constrained.NumOrd (
5656
import Constrained.AbstractSyntax
5757
import Constrained.Base
5858
import Constrained.Conformance
59-
import Constrained.Core (Value (..), unionWithMaybe)
59+
import Constrained.Core (Value (..), unionWithMaybe, nubOrd)
6060
import Constrained.FunctionSymbol
6161
import Constrained.GenT
6262
import Constrained.Generic
@@ -362,17 +362,6 @@ conformsToNumSpec i (NumSpecInterval ml mu) = maybe True (<= i) ml && maybe True
362362
-- implementations are found here
363363
-- =====================================================================
364364

365-
-- | Strip out duplicates (in n-log(n) time, by building an intermediate Set)
366-
nubOrd :: Ord a => [a] -> [a]
367-
nubOrd =
368-
loop mempty
369-
where
370-
loop _ [] = []
371-
loop s (a : as)
372-
| a `Set.member` s = loop s as
373-
| otherwise =
374-
let s' = Set.insert a s in s' `seq` a : loop s' as
375-
376365
-- | Builds a MemberSpec, but returns an Error spec if the list is empty
377366
nubOrdMemberSpec :: Ord a => String -> [a] -> Specification a
378367
nubOrdMemberSpec message xs =
@@ -936,7 +925,7 @@ instance Logic IntW where
936925

937926
propagateMemberSpec AddW (HOLE :<: i) es =
938927
memberSpec
939-
(nub $ mapMaybe (safeSubtract i) (NE.toList es))
928+
(nubOrd $ mapMaybe (safeSubtract i) (NE.toList es))
940929
( NE.fromList
941930
[ "propagateSpecFn on (" ++ show i ++ " +. HOLE)"
942931
, "The Spec is a MemberSpec = " ++ show es -- show (MemberSpec @HasSpec @TS es)

0 commit comments

Comments
 (0)