summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOleg Grenrus <oleg.grenrus@iki.fi>2021-01-08 19:15:35 +0200
committerMarge Bot <ben+marge-bot@smart-cactus.org>2021-01-17 05:54:58 -0500
commit907f1e4a8bb159c082c30d436f4555110e1055c2 (patch)
treef84577ac95c9ce81ae8acc9000af27ba5f748444
parent62cac31cd20708d7dd77131e0f822a3eed0d0661 (diff)
downloadhaskell-907f1e4a8bb159c082c30d436f4555110e1055c2.tar.gz
Third pass on doctest corrections.
With `-K500K` rts option stack overflows are more deterministic
-rw-r--r--libraries/base/Data/Foldable.hs2
-rw-r--r--libraries/base/Data/Traversable.hs30
-rw-r--r--libraries/base/GHC/List.hs6
3 files changed, 27 insertions, 11 deletions
diff --git a/libraries/base/Data/Foldable.hs b/libraries/base/Data/Foldable.hs
index a103d19d4a..82fcd874b0 100644
--- a/libraries/base/Data/Foldable.hs
+++ b/libraries/base/Data/Foldable.hs
@@ -117,8 +117,10 @@ import GHC.Generics
import GHC.Num ( Num(..) )
-- $setup
+-- >>> :set -XDeriveFoldable
-- >>> import Prelude
-- >>> import Data.Monoid (Product (..), Sum (..))
+-- >>> data Tree a = Empty | Leaf a | Node (Tree a) a (Tree a) deriving (Show, Foldable)
infix 4 `elem`, `notElem`
diff --git a/libraries/base/Data/Traversable.hs b/libraries/base/Data/Traversable.hs
index 4d87cec29c..35f2c066b8 100644
--- a/libraries/base/Data/Traversable.hs
+++ b/libraries/base/Data/Traversable.hs
@@ -81,6 +81,9 @@ import qualified GHC.List as List ( foldr )
-- $setup
-- >>> import Prelude
+-- >>> import Data.Maybe (catMaybes, mapMaybe)
+-- >>> import Data.Either (rights)
+-- >>> import Data.Foldable (traverse_)
-- XXX: Missing haddock feature. Links to anchors in other modules
-- don't have a sensible way to name the link within the module itself.
@@ -497,10 +500,17 @@ foldMapDefault = coerce (traverse :: (a -> Const m ()) -> t a -> Const m (t ()))
-- over a list of file names produces an IO action that evaluates to a list
-- of __@(fileName, lineCount)@__ pairs:
--
--- >>> nameAndLineCount :: FilePath -> IO (FilePath, Int)
--- >>> nameAndLineCount fn = ...
--- >>> traverse nameAndLineCount ["/etc/passwd","/etc/hosts"]
+-- @
+-- nameAndLineCount :: FilePath -> IO (FilePath, Int)
+-- nameAndLineCount fn = ...
+-- @
+--
+-- Then @traverse nameAndLineCount ["/etc/passwd","/etc/hosts"]@
+-- could return
+--
+-- @
-- [("/etc/passwd",56),("/etc/hosts",32)]
+-- @
--
-- The specialisation of 'traverse' to the case when __@f@__ is a monad is
-- called 'mapM'. The two are otherwise generally identical:
@@ -522,7 +532,7 @@ foldMapDefault = coerce (traverse :: (a -> Const m ()) -> t a -> Const m (t ()))
-- replaced by each element of __@f a@__ in turn:
--
-- >>> mapM (\n -> [0..n]) $ Just 2
--- [Just 0, Just 1, Just 2]
+-- [Just 0,Just 1,Just 2]
-- >>> mapM (\n -> [0..n]) [0..2]
-- [[0,0,0],[0,0,1],[0,0,2],[0,1,0],[0,1,1],[0,1,2]]
--
@@ -558,14 +568,18 @@ foldMapDefault = coerce (traverse :: (a -> Const m ()) -> t a -> Const m (t ()))
-- #validation#
-- A hypothetical application of the above is to validate a structure:
--
--- >>> validate :: Int -> Either (String, Int) Int
--- >>> validate i = if odd i then Left ("That's odd", i) else Right i
+-- >>> :{
+-- validate :: Int -> Either (String, Int) Int
+-- validate i = if odd i then Left ("That's odd", i) else Right i
+-- :}
+--
-- >>> traverse validate [2,4,6,8,10]
-- Right [2,4,6,8,10]
-- >>> traverse validate [2,4,6,8,9]
-- Left ("That's odd",9)
--
--- >>> -- Since 'Nothing' is an empty structure, none of its elements are odd.
+-- Since 'Nothing' is an empty structure, none of its elements are odd.
+--
-- >>> traverse validate Nothing
-- Right Nothing
-- >>> traverse validate (Just 42)
@@ -585,7 +599,7 @@ foldMapDefault = coerce (traverse :: (a -> Const m ()) -> t a -> Const m (t ()))
-- >>> traverse_ validate [2,4,6,8,9]
-- Left ("That's odd",9)
--
--- The @Foldable@ instance should be defined in a manner that avoids
+-- The 'Foldable' instance should be defined in a manner that avoids
-- construction of an unnecesary copy of the container.
--
-- The @Foldable@ method 'mapM_' and its flipped version 'forM_' can be used
diff --git a/libraries/base/GHC/List.hs b/libraries/base/GHC/List.hs
index 5f6935d770..07f65fc987 100644
--- a/libraries/base/GHC/List.hs
+++ b/libraries/base/GHC/List.hs
@@ -536,8 +536,8 @@ foldr1 f = go
-- [98,-97,99,-96,100]
-- >>> scanr (\nextChar reversedString -> nextChar : reversedString) "foo" ['a', 'b', 'c', 'd']
-- ["abcdfoo","bcdfoo","cdfoo","dfoo","foo"]
--- >>> scanr (+) 0 [1..]
--- * Hangs forever *
+-- >>> force $ scanr (+) 0 [1..]
+-- *** Exception: stack overflow
{-# NOINLINE [1] scanr #-}
scanr :: (a -> b -> b) -> b -> [a] -> [b]
scanr _ q0 [] = [q0]
@@ -602,7 +602,7 @@ remove the cause for the chain of evaluations, and all is well.
-- >>> scanr1 (||) [True, True, False, False]
-- [True,True,False,False]
-- >>> force $ scanr1 (+) [1..]
--- * Hangs forever *
+-- *** Exception: stack overflow
scanr1 :: (a -> a -> a) -> [a] -> [a]
scanr1 _ [] = []
scanr1 _ [x] = [x]