papa-0.7.0: src/Papa/Base/Data/Enum.hs
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wall #-}
-- | Total enumeration.
--
-- The 'Enum' methods @succ@ and @pred@ of "Prelude" throw at the ends of the
-- range of a type. These return 'Data.Maybe.Nothing' there instead.
--
-- * 'succ' is the second element of @enumFrom@, which stops at the last value
-- of a bounded type and does not stop for an unbounded one, so it needs only
-- 'Enum', and works at 'Prelude.Integer', 'Numeric.Natural.Natural' and
-- 'Prelude.Double'.
-- * 'pred' needs the lower end of the range, so it is defined for types that
-- are 'Bounded' as well as 'Enum'. @enumFrom@ has no counterpart that goes
-- down without already knowing the predecessor.
--
-- 'Enum' is exported without its methods, for its constraint. @toEnum@ and
-- @fromEnum@ are not exported: base's instances throw for a value that does
-- not fit in an 'Data.Int.Int' (a @Word@ or @Word64@ above
-- @'maxBound' :: Int@), and a total version cannot be written for every
-- 'Enum' type. Use 'Papa.Base.Data.Integral.toIntegralSized' between integral
-- types, and 'Data.Char.ord' and 'Papa.Base.Data.Char.chr' for 'Data.Char.Char'.
--
-- == Range syntax
--
-- @[a ..]@, @[a, b ..]@, @[a .. c]@ and @[a, b .. c]@ are syntax for
-- The methods of 'Enum' (@enumFrom@, …), which GHC calls whether or not they are
-- in scope, so not exporting them does not take the syntax away. It is total
-- at every type of base, but at 'Prelude.Float' and 'Prelude.Double' it is
-- not what it looks like: a range goes on to half a step past its end, and
-- its elements are sums of the step, with their rounding errors.
--
-- >>> [1.0 .. 2.5 :: Double]
-- [1.0,2.0,3.0]
--
-- >>> [0, 0.1 .. 0.3 :: Double]
-- [0.0,0.1,0.2,0.30000000000000004]
--
-- A step of zero repeats the first element without end:
--
-- >>> take 3 [1, 1 .. 5 :: Int]
-- [1,1,1]
--
-- For fractional values, build the range over an integral type, and divide:
--
-- >>> (\d -> fmap (\n -> divideNonZero (integralToFloating n) d) [0 .. 3 :: Int]) <$> preview nonZero (10 :: Double)
-- Just [0.0,0.1,0.2,0.3]
--
-- @since 0.6.0
module Papa.Base.Data.Enum (
Bounded (minBound, maxBound),
Enum,
succ,
pred,
) where
import Control.Category ((.))
import Data.Bool (bool)
import Data.Eq (Eq ((==)))
import Data.Maybe (Maybe (Just, Nothing))
import Prelude (Bounded (maxBound, minBound), Enum)
import qualified Prelude as P
-- $setup
-- >>> :set -XNoImplicitPrelude
-- >>> import Papa
-- >>> import Hedgehog
-- >>> import qualified Hedgehog.Gen as Gen
-- >>> import qualified Hedgehog.Range as Range
-- >>> import qualified Prelude as P
-- | Total 'P.succ': 'Nothing' at the last value of a bounded type.
--
-- ==== __Examples__
--
-- >>> succ False
-- Just True
--
-- >>> succ True
-- Nothing
--
-- >>> succ (maxBound :: Int)
-- Nothing
--
-- >>> succ (3 :: Integer)
-- Just 4
--
-- >>> succ (1.5 :: Double)
-- Just 2.5
--
-- >>> check $ withTests 100 $ property $ do n <- forAll (Gen.int Range.linearBounded); succ n === bool (Just (P.succ n)) Nothing (n == maxBound)
-- ...passed 100 tests.
-- True
--
-- >>> check $ withTests 100 $ property $ do n <- forAll (Gen.integral (Range.linearFrom 0 (-1000) 1000) :: Gen Integer); succ n === Just (n + 1)
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
succ :: (Enum a) => a -> Maybe a
succ =
second . P.enumFrom
{-# INLINE succ #-}
-- | The second element of a list. A right fold, so that it fuses with
-- @enumFrom@ and allocates no list.
second :: [a] -> Maybe a
second xs =
P.foldr (\x k i -> bool (k (1 :: P.Int)) (Just x) (i == 1)) (P.const Nothing) xs 0
{-# INLINE second #-}
-- | Total 'P.pred': 'Nothing' at 'minBound'.
--
-- ==== __Examples__
--
-- >>> pred True
-- Just False
--
-- >>> pred False
-- Nothing
--
-- >>> pred (minBound :: Int)
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do n <- forAll (Gen.int Range.linearBounded); (pred n >>= succ) === if n == minBound then Nothing else Just n
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
pred :: (Bounded a, Enum a, Eq a) => a -> Maybe a
pred a =
bool (Just (P.pred a)) Nothing (a == minBound)
{-# INLINEABLE pred #-}