diff options
author | Oleg Grenrus <oleg.grenrus@iki.fi> | 2020-12-26 00:28:12 +0200 |
---|---|---|
committer | Marge Bot <ben+marge-bot@smart-cactus.org> | 2021-01-02 07:36:33 -0500 |
commit | aa17b84dab91408090f079f85d62ef3221f4ab88 (patch) | |
tree | d018205fc9de5d52039d3f2be1041d297efc4c29 /libraries/base/Control | |
parent | 4c178374983dddb75341352aef1d15658a82463b (diff) | |
download | haskell-aa17b84dab91408090f079f85d62ef3221f4ab88.tar.gz |
Correct doctests
It's simpler to assume that base is NoImplicitPrelude,
otherwise running doctest on `GHC.*` modules would be tricky.
OTOH, most `GHC.List` (where the most name clashes are) examples
could be changed to use `import qualified Data.List as L`.
(GHC.List examples won't show for Foldable methods...).
With these changes majority of doctest examples are GHCi-"faithful",
my WIP GHC-independent doctest runner reports nice summary:
Examples: 582; Tried: 546; Skipped: 34; Success: 515; Errors: 33; Property Failures 2
Most error cases are *Hangs forever*.
I have yet to figure out how to demonstrate that in GHCi.
Some of divergences are actually stack overflows, i.e. caught by
runtime.
Few errorful cases are examples of infinite output, e.g.
>>> cycle [42]
[42,42,42,42,42,42,42,42,42,42...
while correct, they confuse doctest.
Another erroneous cases are where expected output has line comment, like
>>> fmap show (Just 1) -- (a -> b) -> f a -> f b
Just "1" -- (Int -> String) -> Maybe Int -> Maybe String
I think I just have to teach doctest to strip comments from expected
output.
This is a first patch in a series.
There is plenty of stuff already.
Diffstat (limited to 'libraries/base/Control')
-rw-r--r-- | libraries/base/Control/Applicative.hs | 7 | ||||
-rw-r--r-- | libraries/base/Control/Monad.hs | 10 |
2 files changed, 12 insertions, 5 deletions
diff --git a/libraries/base/Control/Applicative.hs b/libraries/base/Control/Applicative.hs index 6c05eec843..93022cb29c 100644 --- a/libraries/base/Control/Applicative.hs +++ b/libraries/base/Control/Applicative.hs @@ -63,6 +63,9 @@ import GHC.List (repeat, zipWith, drop) import GHC.Read (Read) import GHC.Show (Show) +-- $setup +-- >>> import Prelude + newtype WrappedMonad m a = WrapMonad { unwrapMonad :: m a } deriving ( Generic -- ^ @since 4.7.0.0 , Generic1 -- ^ @since 4.7.0.0 @@ -146,7 +149,9 @@ instance Alternative ZipList where -- -- ==== __Examples__ -- --- Using the 'Alternative' instance of `Control.Monad.Except`, the following functions: +-- Using the 'Alternative' instance of "Control.Monad.Except", the following functions: +-- +-- >>> import Control.Monad.Except -- -- >>> canFail = throwError "it failed" :: Except String Int -- >>> final = return 42 :: Except String Int diff --git a/libraries/base/Control/Monad.hs b/libraries/base/Control/Monad.hs index c906014cd0..b4f2cc022d 100644 --- a/libraries/base/Control/Monad.hs +++ b/libraries/base/Control/Monad.hs @@ -85,6 +85,10 @@ import GHC.Base hiding ( mapM, sequence ) import GHC.List ( zipWith, unzip ) import GHC.Num ( (-) ) +-- $setup +-- >>> import Prelude +-- >>> let safeDiv x y = guard (y /= 0) >> Just (x `div` y :: Int) + -- ----------------------------------------------------------------------------- -- Functions mandated by the Prelude @@ -106,12 +110,11 @@ import GHC.Num ( (-) ) -- 'Nothing' when the denominator @y@ is zero and @'Just' (x \`div\` -- y)@ otherwise. For example: -- --- @ -- >>> safeDiv 4 0 -- Nothing +-- -- >>> safeDiv 4 2 -- Just 2 --- @ -- -- A definition of @safeDiv@ using guards, but not 'guard': -- @@ -360,12 +363,11 @@ f <$!> m = do -- -- An example using 'mfilter' with the 'Maybe' monad: -- --- @ -- >>> mfilter odd (Just 1) -- Just 1 -- >>> mfilter odd (Just 2) -- Nothing --- @ +-- mfilter :: (MonadPlus m) => (a -> Bool) -> m a -> m a {-# INLINABLE mfilter #-} mfilter p ma = do |