@@ -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
390391sizeT :: 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
399404inspect :: 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
405408tryGenT :: 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
414416catchGenT :: 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
423424catchGen :: GenT GE a -> Gen (Either (NonEmpty (NonEmpty String )) a )
0 commit comments