Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix doctests #165

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .github/workflows/doctest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: doctest

on:
pull_request:
branches:
- master

jobs:
doctest:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- run: cabal update

# NOTE: We can't use `cabal doctest` here, as `cabal doctest` uses
# `--build-depends=QuickCheck`, which results in a dependency cycle.
- run: cabal install doctest --ignore-project --overwrite-policy=always && cabal build && cabal repl --build-depends=unliftio --with-compiler=doctest --repl-options='-w -Wdefault'
lehins marked this conversation as resolved.
Show resolved Hide resolved
7 changes: 4 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
stack.yaml.lock
.stack-work/
cabal.project.local
/dist-newstyle/
/stack.yaml.lock
/.stack-work/
/cabal.project.local
16 changes: 0 additions & 16 deletions random.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -123,22 +123,6 @@ test-suite legacy-test
containers >=0.5 && <0.7,
random

test-suite doctests
type: exitcode-stdio-1.0
main-is: doctests.hs
hs-source-dirs: test
default-language: Haskell2010
build-depends:
base,
doctest >=0.15 && <0.23
if impl(ghc >= 8.2) && impl(ghc < 8.10)
build-depends:
primitive >=0.6 && <0.8,
random,
stm,
unliftio >=0.2 && <0.3,
vector >= 0.10 && <0.14

test-suite spec
type: exitcode-stdio-1.0
main-is: Spec.hs
Expand Down
19 changes: 10 additions & 9 deletions src/System/Random.hs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ import qualified System.Random.SplitMix as SM
--
-- You can use type applications to disambiguate the type of the generated numbers:
--
-- >>> :set -XTypeApplications
-- >>> :seti -XTypeApplications
-- >>> uniform @Bool pureGen
-- (True,StdGen {unStdGen = SMGen 11285859549637045894 7641485672361121627})
--
Expand Down Expand Up @@ -204,7 +204,7 @@ uniform g = runStateGen g uniformM
--
-- You can use type applications to disambiguate the type of the generated numbers:
--
-- >>> :set -XTypeApplications
-- >>> :seti -XTypeApplications
-- >>> uniformR @Int (1, 4) pureGen
-- (4,StdGen {unStdGen = SMGen 11285859549637045894 7641485672361121627})
--
Expand Down Expand Up @@ -354,7 +354,7 @@ class Random a where
-- independently:
--
-- >>> fst $ randomR (('a', 5.0), ('z', 10.0)) $ mkStdGen 2021
-- ('t',6.240232662366563)
-- ('t',6.240232662366564)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@idontgetoutmuch I am more curious to see floating point number generation be affected by the change of how doctests are being executed. I suspect this is coming from some ghc optimizations, so I don't think we'll be able to do anything about it, but it could be worth investigating where exactly it is coming from.

--
-- In case when a lawful range is desired `uniformR` should be used
-- instead.
Expand Down Expand Up @@ -642,7 +642,7 @@ newStdGen = liftIO $ atomicModifyIORef' theStdGen splitGen
--
-- >>> rollDice = getStdRandom (randomR (1, 6))
-- >>> replicateM 10 (rollDice :: IO Int)
-- [5,6,6,1,1,6,4,2,4,1]
-- [1,1,1,4,5,6,1,2,2,5]
--
-- This is an outdated function and it is recommended to switch to its
-- equivalent 'System.Random.Stateful.applyAtomicGen' instead, possibly with the
Expand All @@ -652,7 +652,7 @@ newStdGen = liftIO $ atomicModifyIORef' theStdGen splitGen
-- >>> import System.Random.Stateful
-- >>> rollDice = applyAtomicGen (uniformR (1, 6)) globalStdGen
-- >>> replicateM 10 (rollDice :: IO Int)
-- [4,6,1,1,4,4,3,2,1,2]
-- [2,1,1,5,4,3,6,6,3,2]
--
-- @since 1.0.0
getStdRandom :: MonadIO m => (StdGen -> (a, StdGen)) -> m a
Expand All @@ -664,7 +664,7 @@ getStdRandom f = liftIO $ atomicModifyIORef' theStdGen (swap . f)
-- pseudo-random number generator 'System.Random.Stateful.globalStdGen'
--
-- >>> randomRIO (2020, 2100) :: IO Int
-- 2040
-- 2028
--
-- Similar to 'randomIO', this function is equivalent to @'getStdRandom'
-- 'randomR'@ and is included in this interface for historical reasons and
Expand All @@ -675,7 +675,7 @@ getStdRandom f = liftIO $ atomicModifyIORef' theStdGen (swap . f)
--
-- >>> import System.Random.Stateful
-- >>> uniformRM (2020, 2100) globalStdGen :: IO Int
-- 2079
-- 2044
--
-- @since 1.0.0
randomRIO :: (Random a, MonadIO m) => (a, a) -> m a
Expand All @@ -686,7 +686,7 @@ randomRIO range = getStdRandom (randomR range)
--
-- >>> import Data.Int
-- >>> randomIO :: IO Int32
-- -1580093805
-- 114794456
--
-- This function is equivalent to @'getStdRandom' 'random'@ and is included in
-- this interface for historical reasons and backwards compatibility. It is
Expand All @@ -696,7 +696,7 @@ randomRIO range = getStdRandom (randomR range)
--
-- >>> import System.Random.Stateful
-- >>> uniformM globalStdGen :: IO Int32
-- -1649127057
-- -1768545016
--
-- @since 1.0.0
randomIO :: (Random a, MonadIO m) => m a
Expand Down Expand Up @@ -842,3 +842,4 @@ randomIO = getStdRandom random
--
-- >>> import Control.Monad (replicateM)
-- >>> import Data.List (unfoldr)
-- >>> setStdGen (mkStdGen 0)
2 changes: 1 addition & 1 deletion src/System/Random/GFinite.hs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ instance Integral Cardinality where
-- Users are not supposed to write instances of 'Finite' manually.
-- There is a default implementation in terms of 'Generic' instead.
--
-- >>> :set -XDeriveGeneric -XDeriveAnyClass
-- >>> :seti -XDeriveGeneric -XDeriveAnyClass
-- >>> import GHC.Generics (Generic)
-- >>> data MyBool = MyTrue | MyFalse deriving (Generic, Finite)
-- >>> data Action = Code MyBool | Eat (Maybe Bool) | Sleep deriving (Generic, Finite)
Expand Down
6 changes: 3 additions & 3 deletions src/System/Random/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,7 @@ class Uniform a where
--
-- There is a default implementation via 'Generic':
--
-- >>> :set -XDeriveGeneric -XDeriveAnyClass
-- >>> :seti -XDeriveGeneric -XDeriveAnyClass
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Bodigrim :set sets options for source files and ghci expressions, while :seti only affects ghci expressions.

  1. With :set those three tests were failing, I think due to a reincarnation of https://gitlab.haskell.org/ghc/ghc/-/issues/20670.
  2. For doctests you usually want :seti anyway.

-- >>> import GHC.Generics (Generic)
-- >>> import System.Random.Stateful
-- >>> data MyBool = MyTrue | MyFalse deriving (Show, Generic, Finite, Uniform)
Expand Down Expand Up @@ -1015,7 +1015,7 @@ finiteUniformM = fmap toGFinite . case gcardinality (proxy# :: Proxy# f) of
-- If your data has several fields of sub-'Word' cardinality,
-- this instance may be more efficient than one, derived via 'Generic' and 'GUniform'.
--
-- >>> :set -XDeriveGeneric -XDeriveAnyClass
-- >>> :seti -XDeriveGeneric -XDeriveAnyClass
-- >>> import GHC.Generics (Generic)
-- >>> import System.Random.Stateful
-- >>> data Triple = Triple Word8 Word8 Word8 deriving (Show, Generic, Finite)
Expand Down Expand Up @@ -1055,7 +1055,7 @@ class UniformRange a where
--
-- There is a default implementation via 'Generic':
--
-- >>> :set -XDeriveGeneric -XDeriveAnyClass
-- >>> :seti -XDeriveGeneric -XDeriveAnyClass
-- >>> import GHC.Generics (Generic)
-- >>> import Data.Word (Word8)
-- >>> import Control.Monad (replicateM)
Expand Down
16 changes: 8 additions & 8 deletions src/System/Random/Stateful.hs
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,9 @@ withMutableGen_ fg action = thawGen fg >>= action
--
-- You can use type applications to disambiguate the type of the generated numbers:
--
-- >>> :set -XTypeApplications
-- >>> :seti -XTypeApplications
-- >>> randomM @Double g
-- 0.6268211351114487
-- 0.6268211351114488
--
-- @since 1.2.0
randomM :: forall a g m. (Random a, RandomGen g, FrozenGen g m) => MutableGen g m -> m a
Expand All @@ -327,7 +327,7 @@ randomM = flip modifyGen random
--
-- You can use type applications to disambiguate the type of the generated numbers:
--
-- >>> :set -XTypeApplications
-- >>> :seti -XTypeApplications
-- >>> randomRM @Int (1, 100) g
-- 2
--
Expand Down Expand Up @@ -854,10 +854,10 @@ applyTGen f (TGenM tvar) = do
-- $setup
-- >>> writeIORef theStdGen $ mkStdGen 2021
--
-- >>> :set -XFlexibleContexts
-- >>> :set -XFlexibleInstances
-- >>> :set -XMultiParamTypeClasses
-- >>> :set -XTypeFamilies
-- >>> :set -XUndecidableInstances
-- >>> :seti -XFlexibleContexts
-- >>> :seti -XFlexibleInstances
-- >>> :seti -XMultiParamTypeClasses
-- >>> :seti -XTypeFamilies
-- >>> :seti -XUndecidableInstances
--
--
2 changes: 0 additions & 2 deletions stack-old.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ packages:
- .
extra-deps:
- splitmix-0.1@sha256:d50c4d0801a35be7875a040470c09863342514930c82a7d25780a6c2efc4fda9,5249
- doctest-0.16.2@sha256:2f96e9bbe9aee11b47453c82c24b3dc76cdbb8a2a7c984dfd60b4906d08adf68,6942
- cabal-doctest-1.0.8@sha256:34dff6369d417df2699af4e15f06bc181d495eca9c51efde173deae2053c197c,1491
- rdtsc-1.3.0.1@sha256:0a6e8dc715ba82ad72c7e2b1c2f468999559bec059d50540719a80b00dcc4e66,1557
- smallcheck-1.2.0@sha256:8b431572e6a0503223e0e52014d41084c1b01f2aeea3bd499f6f529b3f6dfa89,1482
- tasty-1.3.1@sha256:01e35c97f7ee5ccbc28f21debea02a38cd010d53b4c3087f5677c5d06617a507,2520
Expand Down
18 changes: 0 additions & 18 deletions test/doctests.hs

This file was deleted.

Loading