Skip to content

Commit 5cf6fdd

Browse files
fewer binds = faster code
1 parent 927f40e commit 5cf6fdd

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

src/Constrained/GenT.hs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ module Constrained.GenT (
2828
tryGenT,
2929
chooseT,
3030
sizeT,
31+
sizedT,
3132
withMode,
3233
frequencyT,
3334
oneofT,
@@ -388,7 +389,11 @@ chooseT (a, b)
388389

389390
-- | Get the size provided to the generator
390391
sizeT :: Monad m => GenT m Int
391-
sizeT = GenT $ \mode msgs -> sized $ \n -> runGenT (pure n) mode msgs
392+
sizeT = sizedT pure
393+
394+
-- | Get the size provided to the generator
395+
sizedT :: (Int -> GenT m a) -> GenT m a
396+
sizedT c = GenT $ \mode msgs -> sized $ \n -> runGenT (c n) mode msgs
392397

393398
-- ==================================================================
394399
-- Reflective analysis of the internal GE structure of (GenT GE x)
@@ -397,27 +402,23 @@ sizeT = GenT $ \mode msgs -> sized $ \n -> runGenT (pure n) mode msgs
397402

398403
-- | Always succeeds, but returns the internal GE structure for analysis
399404
inspect :: forall m x. MonadGenError m => GenT GE x -> GenT m (GE x)
400-
inspect (GenT f) = GenT g
401-
where
402-
g mode msgs = do geThing <- f mode msgs; pure @Gen (pure @m geThing)
405+
inspect (GenT f) = GenT (\ mode msgs -> pure <$> f mode msgs)
403406

404407
-- | Ignore all kinds of Errors, by squashing them into Nothing
405408
tryGenT :: MonadGenError m => GenT GE a -> GenT m (Maybe a)
406-
tryGenT g = do
407-
r <- inspect g
408-
case r of
409-
FatalError _ -> pure Nothing
410-
GenError _ -> pure Nothing
411-
Result a -> pure $ Just a
409+
tryGenT g = cont <$> inspect g
410+
where cont r = case r of
411+
FatalError _ -> Nothing
412+
GenError _ -> Nothing
413+
Result a -> Just a
412414

413415
-- Pass on the error messages of both kinds of Errors, by squashing and combining both of them into Left constructor
414416
catchGenT :: MonadGenError m => GenT GE a -> GenT m (Either (NonEmpty (NonEmpty String)) a)
415-
catchGenT g = do
416-
r <- inspect g
417-
case r of
418-
FatalError es -> pure $ Left es
419-
GenError es -> pure $ Left es
420-
Result a -> pure $ Right a
417+
catchGenT g = cont <$> inspect g
418+
where cont r = case r of
419+
FatalError es -> Left es
420+
GenError es -> Left es
421+
Result a -> Right a
421422

422423
-- | Pass on the error messages of both kinds of Errors in the Gen (not the GenT) monad
423424
catchGen :: GenT GE a -> Gen (Either (NonEmpty (NonEmpty String)) a)

src/Constrained/NumOrd.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -311,11 +311,11 @@ genFromNumSpec ::
311311
(MonadGenError m, Show n, Random n, Ord n, Num n, MaybeBounded n) =>
312312
NumSpec n ->
313313
GenT m n
314-
genFromNumSpec (NumSpecInterval ml mu) = do
315-
n <- sizeT
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
314+
genFromNumSpec (NumSpecInterval ml mu) =
315+
sizedT $ \ n ->
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
319319

320320
-- TODO: fixme
321321

0 commit comments

Comments
 (0)