Skip to content

Commit 9b4fe50

Browse files
initial stab at speeding up maps with more optimistic first assumption
1 parent df5f8f4 commit 9b4fe50

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

src/Constrained/Conformance.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module Constrained.Conformance (
1313
monitorSpec,
1414
conformsToSpec,
1515
conformsToSpecE,
16+
allConformToSpec,
1617
satisfies,
1718
checkPredE,
1819
checkPredsE,
@@ -30,6 +31,8 @@ import Constrained.Syntax
3031
import Data.List (intersect, nub)
3132
import Data.List.NonEmpty qualified as NE
3233
import Data.Maybe
34+
import Data.Set (Set)
35+
import Data.Set qualified as Set
3336
import Data.Semigroup (sconcat)
3437
import Prettyprinter hiding (cat)
3538
import Test.QuickCheck (Property, Testable, property)
@@ -171,6 +174,11 @@ conformsToSpec a x = case conformsToSpecE a x (pure "call to conformsToSpecE") o
171174
Nothing -> True
172175
Just _ -> False
173176

177+
allConformToSpec :: (HasSpec a, Ord a) => Set a -> Specification a -> Bool
178+
allConformToSpec xs (MemberSpec ys) = null $ xs Set.\\ Set.fromList (NE.toList ys)
179+
allConformToSpec _ TrueSpec = True
180+
allConformToSpec xs spec = all (`conformsToSpec` spec) xs
181+
174182
-- | Embed a `Specification` in a `Pred`. Useful for re-using `Specification`s
175183
satisfies :: forall a. HasSpec a => Term a -> Specification a -> Pred
176184
satisfies e (ExplainSpec [] s) = satisfies e s

src/Constrained/Spec/Map.hs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,19 @@ instance
184184
, geqSpec 0
185185
]
186186
n <- genFromSpecT size'
187-
let go 0 _ m = pure m
188-
go n' kvs' m = do
187+
let go sz 0 slow kvs' m
188+
| fromInteger sz == Map.size m = pure m
189+
| not slow = go sz (sz - fromIntegral (Map.size m)) True (kvs' <> typeSpec (Cartesian (notMemberSpec (Map.keys m)) mempty)) m
190+
| otherwise = fatalError "The impossible happened"
191+
go sz n' slow kvs' m = do
189192
mkv <- inspect $ genFromSpecT kvs'
190193
case mkv of
191194
Result (k, v) ->
192195
go
196+
sz
193197
(n' - 1)
194-
(kvs' <> typeSpec (Cartesian (notEqualSpec k) mempty))
198+
slow
199+
(kvs' <> if slow then typeSpec (Cartesian (notEqualSpec k) mempty) else mempty)
195200
(Map.insert k v m)
196201
GenError msgs ->
197202
if sizeOf m `conformsToSpec` size
@@ -210,15 +215,11 @@ instance
210215
, "The computed target size " ++ show n
211216
, "Fatal error messages"
212217
, "<<<---"
213-
-- , "The kvs Spec"
214-
-- , " >>>> "++ show $ " kvs' = " <> pretty kvs'
215-
-- , "The simplified kvs Spec"
216-
-- , " >>>> "++ show(pretty (simplifySpec kvs'))
217218
]
218219
<> catMessageList msgs
219220
<> (pure "--->>>")
220221
)
221-
explain (" The number we are trying for: n = " ++ show n) $ go n kvs mempty
222+
explain (" The number we are trying for: n = " ++ show n) $ go n n False kvs mempty
222223
genFromTypeSpec (MapSpec mHint mustKeys mustVals size (simplifySpec -> kvs) foldSpec) = do
223224
!mustMap <- explain "Make the mustMap" $ forM (Set.toList mustKeys) $ \k -> do
224225
let vSpec = constrained $ \v -> satisfies (pair_ (Lit k) v) kvs

src/Constrained/Spec/Set.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ instance (Ord a, HasSpec a) => HasSpec (Set a) where
130130
]
131131

132132
genFromTypeSpec (SetSpec must e _)
133-
| any (not . (`conformsToSpec` e)) must =
133+
| not $ allConformToSpec must e =
134134
genErrorNE
135135
( NE.fromList
136136
[ "Failed to generate set"

0 commit comments

Comments
 (0)