Skip to content

Commit 927f40e

Browse files
reduce the number of binds
1 parent 0ca479a commit 927f40e

File tree

2 files changed

+11
-23
lines changed

2 files changed

+11
-23
lines changed

src/Constrained/Generation.hs

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -119,29 +119,16 @@ genFromSpecT (simplifySpec -> spec) = case spec of
119119
env <- genFromPreds mempty p
120120
Env.find env x
121121
TypeSpec s cant -> do
122-
mode <- getMode
123-
explainNE
124-
( NE.fromList
125-
[ "genFromSpecT on (TypeSpec tspec cant) at type " ++ showType @a
126-
, "tspec = "
127-
, show s
128-
, "cant = " ++ show cant
129-
, "with mode " ++ show mode
130-
]
131-
)
132-
$
133-
-- TODO: we could consider giving `cant` as an argument to `genFromTypeSpec` if this
134-
-- starts giving us trouble.
135-
case cant of
136-
[] -> genFromTypeSpec s
137-
_ -> genFromTypeSpec s `suchThatT` (`notElem` cant)
122+
-- TODO: we could consider giving `cant` as an argument to `genFromTypeSpec` if this
123+
-- starts giving us trouble.
124+
case cant of
125+
[] -> genFromTypeSpec s
126+
_ -> genFromTypeSpec s `suchThatT` (`notElem` cant)
138127
ErrorSpec e -> genErrorNE e
139128

140129
-- | A version of `genFromSpecT` that simply errors if the generator fails
141130
genFromSpec :: forall a. (HasCallStack, HasSpec a) => Specification a -> Gen a
142-
genFromSpec spec = do
143-
res <- catchGen $ genFromSpecT @a @GE spec
144-
either (error . ('\n' :) . catMessages) pure res
131+
genFromSpec spec = either (error . ('\n' :) . catMessages) id <$> catchGen (genFromSpecT @a @GE spec)
145132

146133
-- | A version of `genFromSpecT` that takes a seed and a size and gives you a result
147134
genFromSpecWithSeed ::

src/Constrained/NumOrd.hs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,9 @@ genFromNumSpec ::
313313
GenT m n
314314
genFromNumSpec (NumSpecInterval ml mu) = do
315315
n <- sizeT
316-
interval <- constrainInterval (ml <|> lowerBound) (mu <|> upperBound) (fromIntegral n)
317-
pureGen $ choose interval
316+
case constrainInterval (ml <|> lowerBound) (mu <|> upperBound) (fromIntegral n) of
317+
Just interval -> pureGen $ choose interval
318+
Nothing -> genError $ "bad interval: " ++ show ml ++ " " ++ show mu
318319

319320
-- TODO: fixme
320321

@@ -329,7 +330,7 @@ fixupWithNumSpec _ = listToMaybe . shrink
329330

330331
{-# SCC constrainInterval #-}
331332
constrainInterval ::
332-
(MonadGenError m, Ord a, Num a, Show a) => Maybe a -> Maybe a -> Integer -> m (a, a)
333+
(Ord a, Num a) => Maybe a -> Maybe a -> Integer -> Maybe (a, a)
333334
constrainInterval ml mu r =
334335
case (ml, mu) of
335336
(Nothing, Nothing) -> pure (-r', r')
@@ -340,7 +341,7 @@ constrainInterval ml mu r =
340341
| u > 0 -> pure (negate r', min u r')
341342
| otherwise -> pure (u - 2 * r', u)
342343
(Just l, Just u)
343-
| l > u -> genError ("bad interval: " ++ show l ++ " " ++ show u)
344+
| l > u -> Nothing
344345
| u < 0 -> pure (safeSub l (safeSub l u r') r', u)
345346
| l >= 0 -> pure (l, safeAdd u l r')
346347
-- TODO: this is a bit suspect if the bounds are lopsided

0 commit comments

Comments
 (0)