summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOleg Grenrus <oleg.grenrus@iki.fi>2021-01-08 19:15:35 +0200
committerHécate Moonlight <hecate+gitlab@glitchbra.in>2021-02-17 09:18:28 +0100
commitc78867a186c2188537c1c8f05c92a90e7d9db094 (patch)
tree97bb7b6e2deb90a4912718f74f11bc53f3c6627b
parent21ab97adeaad0b79d317fa05703b09a00ff5c36d (diff)
downloadhaskell-c78867a186c2188537c1c8f05c92a90e7d9db094.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.hs33
-rw-r--r--libraries/base/GHC/List.hs6
3 files changed, 30 insertions, 11 deletions
diff --git a/libraries/base/Data/Foldable.hs b/libraries/base/Data/Foldable.hs
index 923963e678..ab28e4eaa7 100644
--- a/libraries/base/Data/Foldable.hs
+++ b/libraries/base/Data/Foldable.hs
@@ -118,8 +118,10 @@ import GHC.Tuple (Solo (..))
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 a17930dbc5..d18b0ed98d 100644
--- a/libraries/base/Data/Traversable.hs
+++ b/libraries/base/Data/Traversable.hs
@@ -80,6 +80,12 @@ import GHC.Generics
import qualified GHC.List as List ( foldr )
import GHC.Tuple (Solo (..))
+-- $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.
-- Thus, the below "Data.Traversable#overview" works well when shown as
@@ -498,10 +504,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:
@@ -523,7 +536,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]]
--
@@ -559,14 +572,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)
@@ -586,7 +603,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 34f338d67f..5894910b35 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]