{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE Trustworthy #-}
{-# OPTIONS_HADDOCK not-home #-}

-----------------------------------------------------------------------------
-- |
-- Module      :  GHC.Enum
-- Copyright   :  (c) The University of Glasgow, 1992-2002
-- License     :  see libraries/base/LICENSE
--
-- Maintainer  :  cvs-ghc@haskell.org
-- Stability   :  internal
-- Portability :  non-portable (GHC extensions)
--
-- The 'Enum' and 'Bounded' classes.
--
-----------------------------------------------------------------------------

#include "MachDeps.h"

module GHC.Enum(
        Bounded(..), Enum(..),
        boundedEnumFrom, boundedEnumFromThen,
        toEnumError, fromEnumError, succError, predError,

        -- Instances for Bounded and Enum: (), Char, Int

   ) where

import GHC.Base hiding ( many )
import GHC.Char
import GHC.Num.Integer
import GHC.Num
import GHC.Show
default ()              -- Double isn't available yet

-- | The 'Bounded' class is used to name the upper and lower limits of a
-- type.  'Ord' is not a superclass of 'Bounded' since types that are not
-- totally ordered may also have upper and lower bounds.
--
-- The 'Bounded' class may be derived for any enumeration type;
-- 'minBound' is the first constructor listed in the @data@ declaration
-- and 'maxBound' is the last.
-- 'Bounded' may also be derived for single-constructor datatypes whose
-- constituent types are in 'Bounded'.

class  Bounded a  where
    minBound, maxBound :: a

-- | Class 'Enum' defines operations on sequentially ordered types.
--
-- The @enumFrom@... methods are used in Haskell's translation of
-- arithmetic sequences.
--
-- Instances of 'Enum' may be derived for any enumeration type (types
-- whose constructors have no fields).  The nullary constructors are
-- assumed to be numbered left-to-right by 'fromEnum' from @0@ through @n-1@.
-- See Chapter 10 of the /Haskell Report/ for more details.
--
-- For any type that is an instance of class 'Bounded' as well as 'Enum',
-- the following should hold:
--
-- * The calls @'succ' 'maxBound'@ and @'pred' 'minBound'@ should result in
--   a runtime error.
--
-- * 'fromEnum' and 'toEnum' should give a runtime error if the
--   result value is not representable in the result type.
--   For example, @'toEnum' 7 :: 'Bool'@ is an error.
--
-- * 'enumFrom' and 'enumFromThen' should be defined with an implicit bound,
--   thus:
--
-- >    enumFrom     x   = enumFromTo     x maxBound
-- >    enumFromThen x y = enumFromThenTo x y bound
-- >      where
-- >        bound | fromEnum y >= fromEnum x = maxBound
-- >              | otherwise                = minBound
--
class  Enum a   where
    -- | the successor of a value.  For numeric types, 'succ' adds 1.
    succ                :: a -> a
    -- | the predecessor of a value.  For numeric types, 'pred' subtracts 1.
    pred                :: a -> a
    -- | Convert from an 'Int'.
    toEnum              :: Int -> a
    -- | Convert to an 'Int'.
    -- It is implementation-dependent what 'fromEnum' returns when
    -- applied to a value that is too large to fit in an 'Int'.
    fromEnum            :: a -> Int

    -- | Used in Haskell's translation of @[n..]@ with @[n..] = enumFrom n@,
    --   a possible implementation being @enumFrom n = n : enumFrom (succ n)@.
    --   For example:
    --
    --     * @enumFrom 4 :: [Integer] = [4,5,6,7,...]@
    --     * @enumFrom 6 :: [Int] = [6,7,8,9,...,maxBound :: Int]@
    enumFrom            :: a -> [a]
    -- | Used in Haskell's translation of @[n,n'..]@
    --   with @[n,n'..] = enumFromThen n n'@, a possible implementation being
    --   @enumFromThen n n' = n : n' : worker (f x) (f x n')@,
    --   @worker s v = v : worker s (s v)@, @x = fromEnum n' - fromEnum n@ and
    --   @f n y
    --     | n > 0 = f (n - 1) (succ y)
    --     | n < 0 = f (n + 1) (pred y)
    --     | otherwise = y@
    --   For example:
    --
    --     * @enumFromThen 4 6 :: [Integer] = [4,6,8,10...]@
    --     * @enumFromThen 6 2 :: [Int] = [6,2,-2,-6,...,minBound :: Int]@
    enumFromThen        :: a -> a -> [a]
    -- | Used in Haskell's translation of @[n..m]@ with
    --   @[n..m] = enumFromTo n m@, a possible implementation being
    --   @enumFromTo n m
    --      | n <= m = n : enumFromTo (succ n) m
    --      | otherwise = []@.
    --   For example:
    --
    --     * @enumFromTo 6 10 :: [Int] = [6,7,8,9,10]@
    --     * @enumFromTo 42 1 :: [Integer] = []@
    enumFromTo          :: a -> a -> [a]
    -- | Used in Haskell's translation of @[n,n'..m]@ with
    --   @[n,n'..m] = enumFromThenTo n n' m@, a possible implementation
    --   being @enumFromThenTo n n' m = worker (f x) (c x) n m@,
    --   @x = fromEnum n' - fromEnum n@, @c x = bool (>=) (<=) (x > 0)@
    --   @f n y
    --      | n > 0 = f (n - 1) (succ y)
    --      | n < 0 = f (n + 1) (pred y)
    --      | otherwise = y@ and
    --   @worker s c v m
    --      | c v m = v : worker s c (s v) m
    --      | otherwise = []@
    --   For example:
    --
    --     * @enumFromThenTo 4 2 -6 :: [Integer] = [4,2,0,-2,-4,-6]@
    --     * @enumFromThenTo 6 8 2 :: [Int] = []@
    enumFromThenTo      :: a -> a -> a -> [a]

    succ = Int -> a
forall a. Enum a => Int -> a
toEnum (Int -> a) -> (a -> Int) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (Int -> Int) -> (a -> Int) -> a -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Int
forall a. Enum a => a -> Int
fromEnum

    pred = Int -> a
forall a. Enum a => Int -> a
toEnum (Int -> a) -> (a -> Int) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int -> Int -> Int
forall a. Num a => a -> a -> a
subtract Int
1) (Int -> Int) -> (a -> Int) -> a -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Int
forall a. Enum a => a -> Int
fromEnum

    -- See Note [Stable Unfolding for list producers]
    {-# INLINABLE enumFrom #-}
    enumFrom a
x = (Int -> a) -> [Int] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map Int -> a
forall a. Enum a => Int -> a
toEnum [a -> Int
forall a. Enum a => a -> Int
fromEnum a
x ..]

    -- See Note [Stable Unfolding for list producers]
    {-# INLINABLE enumFromThen #-}
    enumFromThen a
x a
y = (Int -> a) -> [Int] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map Int -> a
forall a. Enum a => Int -> a
toEnum [a -> Int
forall a. Enum a => a -> Int
fromEnum a
x, a -> Int
forall a. Enum a => a -> Int
fromEnum a
y ..]

    -- See Note [Stable Unfolding for list producers]
    {-# INLINABLE enumFromTo #-}
    enumFromTo a
x a
y = (Int -> a) -> [Int] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map Int -> a
forall a. Enum a => Int -> a
toEnum [a -> Int
forall a. Enum a => a -> Int
fromEnum a
x .. a -> Int
forall a. Enum a => a -> Int
fromEnum a
y]

    -- See Note [Stable Unfolding for list producers]
    {-# INLINABLE enumFromThenTo #-}
    enumFromThenTo a
x1 a
x2 a
y = (Int -> a) -> [Int] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map Int -> a
forall a. Enum a => Int -> a
toEnum [a -> Int
forall a. Enum a => a -> Int
fromEnum a
x1, a -> Int
forall a. Enum a => a -> Int
fromEnum a
x2 .. a -> Int
forall a. Enum a => a -> Int
fromEnum a
y]

-- See Note [Stable Unfolding for list producers]
{-# INLINABLE boundedEnumFrom #-}
-- Default methods for bounded enumerations
boundedEnumFrom :: (Enum a, Bounded a) => a -> [a]
boundedEnumFrom :: forall a. (Enum a, Bounded a) => a -> [a]
boundedEnumFrom a
n = (Int -> a) -> [Int] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map Int -> a
forall a. Enum a => Int -> a
toEnum [a -> Int
forall a. Enum a => a -> Int
fromEnum a
n .. a -> Int
forall a. Enum a => a -> Int
fromEnum (a
forall a. Bounded a => a
maxBound a -> a -> a
forall a. a -> a -> a
`asTypeOf` a
n)]

-- See Note [Stable Unfolding for list producers]
{-# INLINABLE boundedEnumFromThen #-}
boundedEnumFromThen :: (Enum a, Bounded a) => a -> a -> [a]
boundedEnumFromThen :: forall a. (Enum a, Bounded a) => a -> a -> [a]
boundedEnumFromThen a
n1 a
n2
  | Int
i_n2 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
i_n1  = (Int -> a) -> [Int] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map Int -> a
forall a. Enum a => Int -> a
toEnum [Int
i_n1, Int
i_n2 .. a -> Int
forall a. Enum a => a -> Int
fromEnum (a
forall a. Bounded a => a
maxBound a -> a -> a
forall a. a -> a -> a
`asTypeOf` a
n1)]
  | Bool
otherwise     = (Int -> a) -> [Int] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map Int -> a
forall a. Enum a => Int -> a
toEnum [Int
i_n1, Int
i_n2 .. a -> Int
forall a. Enum a => a -> Int
fromEnum (a
forall a. Bounded a => a
minBound a -> a -> a
forall a. a -> a -> a
`asTypeOf` a
n1)]
  where
    i_n1 :: Int
i_n1 = a -> Int
forall a. Enum a => a -> Int
fromEnum a
n1
    i_n2 :: Int
i_n2 = a -> Int
forall a. Enum a => a -> Int
fromEnum a
n2

{-
Note [Stable Unfolding for list producers]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The INLINABLE/INLINE pragmas ensure that we export stable (unoptimised)
unfoldings in the interface file so we can do list fusion at usage sites.
-}

------------------------------------------------------------------------
-- Helper functions
------------------------------------------------------------------------

{-# NOINLINE toEnumError #-}
toEnumError :: (Show a) => String -> Int -> (a,a) -> b
toEnumError :: forall a b. Show a => String -> Int -> (a, a) -> b
toEnumError String
inst_ty Int
i (a, a)
bnds =
    String -> b
forall a. String -> a
errorWithoutStackTrace (String -> b) -> String -> b
forall a b. (a -> b) -> a -> b
$ String
"Enum.toEnum{" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
inst_ty String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"}: tag (" String -> String -> String
forall a. [a] -> [a] -> [a]
++
            Int -> String
forall a. Show a => a -> String
show Int
i String -> String -> String
forall a. [a] -> [a] -> [a]
++
            String
") is outside of bounds " String -> String -> String
forall a. [a] -> [a] -> [a]
++
            (a, a) -> String
forall a. Show a => a -> String
show (a, a)
bnds

{-# NOINLINE fromEnumError #-}
fromEnumError :: (Show a) => String -> a -> b
fromEnumError :: forall a b. Show a => String -> a -> b
fromEnumError String
inst_ty a
x =
    String -> b
forall a. String -> a
errorWithoutStackTrace (String -> b) -> String -> b
forall a b. (a -> b) -> a -> b
$ String
"Enum.fromEnum{" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
inst_ty String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"}: value (" String -> String -> String
forall a. [a] -> [a] -> [a]
++
            a -> String
forall a. Show a => a -> String
show a
x String -> String -> String
forall a. [a] -> [a] -> [a]
++
            String
") is outside of Int's bounds " String -> String -> String
forall a. [a] -> [a] -> [a]
++
            (Int, Int) -> String
forall a. Show a => a -> String
show (Int
forall a. Bounded a => a
minBound::Int, Int
forall a. Bounded a => a
maxBound::Int)

{-# NOINLINE succError #-}
succError :: String -> a
succError :: forall a. String -> a
succError String
inst_ty =
    String -> a
forall a. String -> a
errorWithoutStackTrace (String -> a) -> String -> a
forall a b. (a -> b) -> a -> b
$ String
"Enum.succ{" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
inst_ty String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"}: tried to take `succ' of maxBound"

{-# NOINLINE predError #-}
predError :: String -> a
predError :: forall a. String -> a
predError String
inst_ty =
    String -> a
forall a. String -> a
errorWithoutStackTrace (String -> a) -> String -> a
forall a b. (a -> b) -> a -> b
$ String
"Enum.pred{" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
inst_ty String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"}: tried to take `pred' of minBound"

------------------------------------------------------------------------
-- Tuples
------------------------------------------------------------------------

-- | @since 2.01
deriving instance Bounded ()

-- | @since 2.01
instance Enum () where
    succ :: () -> ()
succ ()
_      = String -> ()
forall a. String -> a
errorWithoutStackTrace String
"Prelude.Enum.().succ: bad argument"
    pred :: () -> ()
pred ()
_      = String -> ()
forall a. String -> a
errorWithoutStackTrace String
"Prelude.Enum.().pred: bad argument"

    toEnum :: Int -> ()
toEnum Int
x | Int
x Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0    = ()
             | Bool
otherwise = String -> ()
forall a. String -> a
errorWithoutStackTrace String
"Prelude.Enum.().toEnum: bad argument"

    fromEnum :: () -> Int
fromEnum () = Int
0
    enumFrom :: () -> [()]
enumFrom ()         = [()]
    enumFromThen :: () -> () -> [()]
enumFromThen () ()  = let many :: [()]
many = ()() -> [()] -> [()]
forall a. a -> [a] -> [a]
:[()]
many in [()]
many
    enumFromTo :: () -> () -> [()]
enumFromTo () ()    = [()]
    enumFromThenTo :: () -> () -> () -> [()]
enumFromThenTo () () () = let many :: [()]
many = ()() -> [()] -> [()]
forall a. a -> [a] -> [a]
:[()]
many in [()]
many

-- Report requires instances up to 15
-- | @since 2.01
deriving instance (Bounded a, Bounded b)
        => Bounded (a,b)
-- | @since 2.01
deriving instance (Bounded a, Bounded b, Bounded c)
        => Bounded (a,b,c)
-- | @since 2.01
deriving instance (Bounded a, Bounded b, Bounded c, Bounded d)
        => Bounded (a,b,c,d)
-- | @since 2.01
deriving instance (Bounded a, Bounded b, Bounded c, Bounded d, Bounded e)
        => Bounded (a,b,c,d,e)
-- | @since 2.01
deriving instance (Bounded a, Bounded b, Bounded c, Bounded d, Bounded e,
          Bounded f)
        => Bounded (a,b,c,d,e,f)
-- | @since 2.01
deriving instance (Bounded a, Bounded b, Bounded c, Bounded d, Bounded e,
          Bounded f, Bounded g)
        => Bounded (a,b,c,d,e,f,g)
-- | @since 2.01
deriving instance (Bounded a, Bounded b, Bounded c, Bounded d, Bounded e,
          Bounded f, Bounded g, Bounded h)
        => Bounded (a,b,c,d,e,f,g,h)
-- | @since 2.01
deriving instance (Bounded a, Bounded b, Bounded c, Bounded d, Bounded e,
          Bounded f, Bounded g, Bounded h, Bounded i)
        => Bounded (a,b,c,d,e,f,g,h,i)
-- | @since 2.01
deriving instance (Bounded a, Bounded b, Bounded c, Bounded d, Bounded e,
          Bounded f, Bounded g, Bounded h, Bounded i, Bounded j)
        => Bounded (a,b,c,d,e,f,g,h,i,j)
-- | @since 2.01
deriving instance (Bounded a, Bounded b, Bounded c, Bounded d, Bounded e,
          Bounded f, Bounded g, Bounded h, Bounded i, Bounded j, Bounded k)
        => Bounded (a,b,c,d,e,f,g,h,i,j,k)
-- | @since 2.01
deriving instance (Bounded a, Bounded b, Bounded c, Bounded d, Bounded e,
          Bounded f, Bounded g, Bounded h, Bounded i, Bounded j, Bounded k,
          Bounded l)
        => Bounded (a,b,c,d,e,f,g,h,i,j,k,l)
-- | @since 2.01
deriving instance (Bounded a, Bounded b, Bounded c, Bounded d, Bounded e,
          Bounded f, Bounded g, Bounded h, Bounded i, Bounded j, Bounded k,
          Bounded l, Bounded m)
        => Bounded (a,b,c,d,e,f,g,h,i,j,k,l,m)
-- | @since 2.01
deriving instance (Bounded a, Bounded b, Bounded c, Bounded d, Bounded e,
          Bounded f, Bounded g, Bounded h, Bounded i, Bounded j, Bounded k,
          Bounded l, Bounded m, Bounded n)
        => Bounded (a,b,c,d,e,f,g,h,i,j,k,l,m,n)
-- | @since 2.01
deriving instance (Bounded a, Bounded b, Bounded c, Bounded d, Bounded e,
          Bounded f, Bounded g, Bounded h, Bounded i, Bounded j, Bounded k,
          Bounded l, Bounded m, Bounded n, Bounded o)
        => Bounded (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o)

------------------------------------------------------------------------
-- Bool
------------------------------------------------------------------------

-- | @since 2.01
deriving instance Bounded Bool

-- | @since 2.01
instance Enum Bool where
  succ :: Bool -> Bool
succ Bool
False = Bool
True
  succ Bool
True  = String -> Bool
forall a. String -> a
errorWithoutStackTrace String
"Prelude.Enum.Bool.succ: bad argument"

  pred :: Bool -> Bool
pred Bool
True  = Bool
False
  pred Bool
False  = String -> Bool
forall a. String -> a
errorWithoutStackTrace String
"Prelude.Enum.Bool.pred: bad argument"

  toEnum :: Int -> Bool
toEnum Int
n | Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0    = Bool
False
           | Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1    = Bool
True
           | Bool
otherwise = String -> Bool
forall a. String -> a
errorWithoutStackTrace String
"Prelude.Enum.Bool.toEnum: bad argument"

  fromEnum :: Bool -> Int
fromEnum Bool
False = Int
0
  fromEnum Bool
True  = Int
1

  -- Use defaults for the rest
  enumFrom :: Bool -> [Bool]
enumFrom     = Bool -> [Bool]
forall a. (Enum a, Bounded a) => a -> [a]
boundedEnumFrom
  enumFromThen :: Bool -> Bool -> [Bool]
enumFromThen = Bool -> Bool -> [Bool]
forall a. (Enum a, Bounded a) => a -> a -> [a]
boundedEnumFromThen

------------------------------------------------------------------------
-- Ordering
------------------------------------------------------------------------

-- | @since 2.01
deriving instance Bounded Ordering
-- | @since 2.01
instance Enum Ordering where
  succ :: Ordering -> Ordering
succ Ordering
LT = Ordering
EQ
  succ Ordering
EQ = Ordering
GT
  succ Ordering
GT = String -> Ordering
forall a. String -> a
errorWithoutStackTrace String
"Prelude.Enum.Ordering.succ: bad argument"

  pred :: Ordering -> Ordering
pred Ordering
GT = Ordering
EQ
  pred Ordering
EQ = Ordering
LT
  pred Ordering
LT = String -> Ordering
forall a. String -> a
errorWithoutStackTrace String
"Prelude.Enum.Ordering.pred: bad argument"

  toEnum :: Int -> Ordering
toEnum Int
n | Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = Ordering
LT
           | Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1 = Ordering
EQ
           | Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
2 = Ordering
GT
  toEnum Int
_ = String -> Ordering
forall a. String -> a
errorWithoutStackTrace String
"Prelude.Enum.Ordering.toEnum: bad argument"

  fromEnum :: Ordering -> Int
fromEnum Ordering
LT = Int
0
  fromEnum Ordering
EQ = Int
1
  fromEnum Ordering
GT = Int
2

  -- Use defaults for the rest
  enumFrom :: Ordering -> [Ordering]
enumFrom     = Ordering -> [Ordering]
forall a. (Enum a, Bounded a) => a -> [a]
boundedEnumFrom
  enumFromThen :: Ordering -> Ordering -> [Ordering]
enumFromThen = Ordering -> Ordering -> [Ordering]
forall a. (Enum a, Bounded a) => a -> a -> [a]
boundedEnumFromThen

------------------------------------------------------------------------
-- Char
------------------------------------------------------------------------

-- | @since 2.01
instance  Bounded Char  where
    minBound :: Char
minBound =  Char
'\0'
    maxBound :: Char
maxBound =  Char
'\x10FFFF'

-- | @since 2.01
instance  Enum Char  where
    succ :: Char -> Char
succ (C# Char#
c#)
       | Int# -> Bool
isTrue# (Char# -> Int#
ord# Char#
c# Int# -> Int# -> Int#
/=# Int#
0x10FFFF#) = Char# -> Char
C# (Int# -> Char#
chr# (Char# -> Int#
ord# Char#
c# Int# -> Int# -> Int#
+# Int#
1#))
       | Bool
otherwise             = String -> Char
forall a. String -> a
errorWithoutStackTrace (String
"Prelude.Enum.Char.succ: bad argument")
    pred :: Char -> Char
pred (C# Char#
c#)
       | Int# -> Bool
isTrue# (Char# -> Int#
ord# Char#
c# Int# -> Int# -> Int#
/=# Int#
0#) = Char# -> Char
C# (Int# -> Char#
chr# (Char# -> Int#
ord# Char#
c# Int# -> Int# -> Int#
-# Int#
1#))
       | Bool
otherwise                = String -> Char
forall a. String -> a
errorWithoutStackTrace (String
"Prelude.Enum.Char.pred: bad argument")

    toEnum :: Int -> Char
toEnum   = Int -> Char
chr
    fromEnum :: Char -> Int
fromEnum = Char -> Int
ord

    -- See Note [Stable Unfolding for list producers]
    {-# INLINE enumFrom #-}
    enumFrom :: Char -> String
enumFrom (C# Char#
x) = Int# -> Int# -> String
eftChar (Char# -> Int#
ord# Char#
x) Int#
0x10FFFF#
        -- Blarg: technically I guess enumFrom isn't strict!

    -- See Note [Stable Unfolding for list producers]
    {-# INLINE enumFromTo #-}
    enumFromTo :: Char -> Char -> String
enumFromTo (C# Char#
x) (C# Char#
y) = Int# -> Int# -> String
eftChar (Char# -> Int#
ord# Char#
x) (Char# -> Int#
ord# Char#
y)

    -- See Note [Stable Unfolding for list producers]
    {-# INLINE enumFromThen #-}
    enumFromThen :: Char -> Char -> String
enumFromThen (C# Char#
x1) (C# Char#
x2) = Int# -> Int# -> String
efdChar (Char# -> Int#
ord# Char#
x1) (Char# -> Int#
ord# Char#
x2)

    -- See Note [Stable Unfolding for list producers]
    {-# INLINE enumFromThenTo #-}
    enumFromThenTo :: Char -> Char -> Char -> String
enumFromThenTo (C# Char#
x1) (C# Char#
x2) (C# Char#
y) = Int# -> Int# -> Int# -> String
efdtChar (Char# -> Int#
ord# Char#
x1) (Char# -> Int#
ord# Char#
x2) (Char# -> Int#
ord# Char#
y)

-- See Note [How the Enum rules work]
{-# RULES
"eftChar"       [~1] forall x y.        eftChar x y       = build (\c n -> eftCharFB c n x y)
"efdChar"       [~1] forall x1 x2.      efdChar x1 x2     = build (\ c n -> efdCharFB c n x1 x2)
"efdtChar"      [~1] forall x1 x2 l.    efdtChar x1 x2 l  = build (\ c n -> efdtCharFB c n x1 x2 l)
"eftCharList"   [1]  eftCharFB  (:) [] = eftChar
"efdCharList"   [1]  efdCharFB  (:) [] = efdChar
"efdtCharList"  [1]  efdtCharFB (:) [] = efdtChar
 #-}


-- We can do better than for Ints because we don't
-- have hassles about arithmetic overflow at maxBound
{-# INLINE [0] eftCharFB #-} -- See Note [Inline FB functions] in GHC.List
eftCharFB :: (Char -> a -> a) -> a -> Int# -> Int# -> a
eftCharFB :: forall a. (Char -> a -> a) -> a -> Int# -> Int# -> a
eftCharFB Char -> a -> a
c a
n Int#
x0 Int#
y = Int# -> a
go Int#
x0
                 where
                    go :: Int# -> a
go Int#
x | Int# -> Bool
isTrue# (Int#
x Int# -> Int# -> Int#
># Int#
y) = a
n
                         | Bool
otherwise        = Char# -> Char
C# (Int# -> Char#
chr# Int#
x) Char -> a -> a
`c` Int# -> a
go (Int#
x Int# -> Int# -> Int#
+# Int#
1#)

{-# NOINLINE [1] eftChar #-}
eftChar :: Int# -> Int# -> String
eftChar :: Int# -> Int# -> String
eftChar Int#
x Int#
y | Int# -> Bool
isTrue# (Int#
x Int# -> Int# -> Int#
># Int#
y ) = []
            | Bool
otherwise         = Char# -> Char
C# (Int# -> Char#
chr# Int#
x) Char -> String -> String
forall a. a -> [a] -> [a]
: Int# -> Int# -> String
eftChar (Int#
x Int# -> Int# -> Int#
+# Int#
1#) Int#
y


-- For enumFromThenTo we give up on inlining
{-# INLINE [0] efdCharFB #-} -- See Note [Inline FB functions] in GHC.List
efdCharFB :: (Char -> a -> a) -> a -> Int# -> Int# -> a
efdCharFB :: forall a. (Char -> a -> a) -> a -> Int# -> Int# -> a
efdCharFB Char -> a -> a
c a
n Int#
x1 Int#
x2
  | Int# -> Bool
isTrue# (Int#
delta Int# -> Int# -> Int#
>=# Int#
0#) = (Char -> a -> a) -> a -> Int# -> Int# -> Int# -> a
forall a. (Char -> a -> a) -> a -> Int# -> Int# -> Int# -> a
go_up_char_fb Char -> a -> a
c a
n Int#
x1 Int#
delta Int#
0x10FFFF#
  | Bool
otherwise              = (Char -> a -> a) -> a -> Int# -> Int# -> Int# -> a
forall a. (Char -> a -> a) -> a -> Int# -> Int# -> Int# -> a
go_dn_char_fb Char -> a -> a
c a
n Int#
x1 Int#
delta Int#
0#
  where
    !delta :: Int#
delta = Int#
x2 Int# -> Int# -> Int#
-# Int#
x1

{-# NOINLINE [1] efdChar #-}
efdChar :: Int# -> Int# -> String
efdChar :: Int# -> Int# -> String
efdChar Int#
x1 Int#
x2
  | Int# -> Bool
isTrue# (Int#
delta Int# -> Int# -> Int#
>=# Int#
0#) = Int# -> Int# -> Int# -> String
go_up_char_list Int#
x1 Int#
delta Int#
0x10FFFF#
  | Bool
otherwise              = Int# -> Int# -> Int# -> String
go_dn_char_list Int#
x1 Int#
delta Int#
0#
  where
    !delta :: Int#
delta = Int#
x2 Int# -> Int# -> Int#
-# Int#
x1

{-# INLINE [0] efdtCharFB #-} -- See Note [Inline FB functions] in GHC.List
efdtCharFB :: (Char -> a -> a) -> a -> Int# -> Int# -> Int# -> a
efdtCharFB :: forall a. (Char -> a -> a) -> a -> Int# -> Int# -> Int# -> a
efdtCharFB Char -> a -> a
c a
n Int#
x1 Int#
x2 Int#
lim
  | Int# -> Bool
isTrue# (Int#
delta Int# -> Int# -> Int#
>=# Int#
0#) = (Char -> a -> a) -> a -> Int# -> Int# -> Int# -> a
forall a. (Char -> a -> a) -> a -> Int# -> Int# -> Int# -> a
go_up_char_fb Char -> a -> a
c a
n Int#
x1 Int#
delta Int#
lim
  | Bool
otherwise              = (Char -> a -> a) -> a -> Int# -> Int# -> Int# -> a
forall a. (Char -> a -> a) -> a -> Int# -> Int# -> Int# -> a
go_dn_char_fb Char -> a -> a
c a
n Int#
x1 Int#
delta Int#
lim
  where
    !delta :: Int#
delta = Int#
x2 Int# -> Int# -> Int#
-# Int#
x1

{-# NOINLINE [1] efdtChar #-}
efdtChar :: Int# -> Int# -> Int# -> String
efdtChar :: Int# -> Int# -> Int# -> String
efdtChar Int#
x1 Int#
x2 Int#
lim
  | Int# -> Bool
isTrue# (Int#
delta Int# -> Int# -> Int#
>=# Int#
0#) = Int# -> Int# -> Int# -> String
go_up_char_list Int#
x1 Int#
delta Int#
lim
  | Bool
otherwise              = Int# -> Int# -> Int# -> String
go_dn_char_list Int#
x1 Int#
delta Int#
lim
  where
    !delta :: Int#
delta = Int#
x2 Int# -> Int# -> Int#
-# Int#
x1

go_up_char_fb :: (Char -> a -> a) -> a -> Int# -> Int# -> Int# -> a
go_up_char_fb :: forall a. (Char -> a -> a) -> a -> Int# -> Int# -> Int# -> a
go_up_char_fb Char -> a -> a
c a
n Int#
x0 Int#
delta Int#
lim
  = Int# -> a
go_up Int#
x0
  where
    go_up :: Int# -> a
go_up Int#
x | Int# -> Bool
isTrue# (Int#
x Int# -> Int# -> Int#
># Int#
lim) = a
n
            | Bool
otherwise          = Char# -> Char
C# (Int# -> Char#
chr# Int#
x) Char -> a -> a
`c` Int# -> a
go_up (Int#
x Int# -> Int# -> Int#
+# Int#
delta)

go_dn_char_fb :: (Char -> a -> a) -> a -> Int# -> Int# -> Int# -> a
go_dn_char_fb :: forall a. (Char -> a -> a) -> a -> Int# -> Int# -> Int# -> a
go_dn_char_fb Char -> a -> a
c a
n Int#
x0 Int#
delta Int#
lim
  = Int# -> a
go_dn Int#
x0
  where
    go_dn :: Int# -> a
go_dn Int#
x | Int# -> Bool
isTrue# (Int#
x Int# -> Int# -> Int#
<# Int#
lim) = a
n
            | Bool
otherwise          = Char# -> Char
C# (Int# -> Char#
chr# Int#
x) Char -> a -> a
`c` Int# -> a
go_dn (Int#
x Int# -> Int# -> Int#
+# Int#
delta)

go_up_char_list :: Int# -> Int# -> Int# -> String
go_up_char_list :: Int# -> Int# -> Int# -> String
go_up_char_list Int#
x0 Int#
delta Int#
lim
  = Int# -> String
go_up Int#
x0
  where
    go_up :: Int# -> String
go_up Int#
x | Int# -> Bool
isTrue# (Int#
x Int# -> Int# -> Int#
># Int#
lim) = []
            | Bool
otherwise          = Char# -> Char
C# (Int# -> Char#
chr# Int#
x) Char -> String -> String
forall a. a -> [a] -> [a]
: Int# -> String
go_up (Int#
x Int# -> Int# -> Int#
+# Int#
delta)

go_dn_char_list :: Int# -> Int# -> Int# -> String
go_dn_char_list :: Int# -> Int# -> Int# -> String
go_dn_char_list Int#
x0 Int#
delta Int#
lim
  = Int# -> String
go_dn Int#
x0
  where
    go_dn :: Int# -> String
go_dn Int#
x | Int# -> Bool
isTrue# (Int#
x Int# -> Int# -> Int#
<# Int#
lim) = []
            | Bool
otherwise          = Char# -> Char
C# (Int# -> Char#
chr# Int#
x) Char -> String -> String
forall a. a -> [a] -> [a]
: Int# -> String
go_dn (Int#
x Int# -> Int# -> Int#
+# Int#
delta)


------------------------------------------------------------------------
-- Int
------------------------------------------------------------------------

{-
Be careful about these instances.
        (a) remember that you have to count down as well as up e.g. [13,12..0]
        (b) be careful of Int overflow
        (c) remember that Int is bounded, so [1..] terminates at maxInt
-}

-- | @since 2.01
instance  Bounded Int where
    minBound :: Int
minBound =  Int
minInt
    maxBound :: Int
maxBound =  Int
maxInt

-- | @since 2.01
instance  Enum Int  where
    succ :: Int -> Int
succ Int
x
       | Int
x Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
forall a. Bounded a => a
maxBound  = String -> Int
forall a. String -> a
errorWithoutStackTrace String
"Prelude.Enum.succ{Int}: tried to take `succ' of maxBound"
       | Bool
otherwise      = Int
x Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
    pred :: Int -> Int
pred Int
x
       | Int
x Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
forall a. Bounded a => a
minBound  = String -> Int
forall a. String -> a
errorWithoutStackTrace String
"Prelude.Enum.pred{Int}: tried to take `pred' of minBound"
       | Bool
otherwise      = Int
x Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1

    toEnum :: Int -> Int
toEnum   Int
x = Int
x
    fromEnum :: Int -> Int
fromEnum Int
x = Int
x

    -- See Note [Stable Unfolding for list producers]
    {-# INLINE enumFrom #-}
    enumFrom :: Int -> [Int]
enumFrom (I# Int#
x) = Int# -> Int# -> [Int]
eftInt Int#
x Int#
maxInt#
        where !(I# Int#
maxInt#) = Int
maxInt
        -- Blarg: technically I guess enumFrom isn't strict!

    -- See Note [Stable Unfolding for list producers]
    {-# INLINE enumFromTo #-}
    enumFromTo :: Int -> Int -> [Int]
enumFromTo (I# Int#
x) (I# Int#
y) = Int# -> Int# -> [Int]
eftInt Int#
x Int#
y

    -- See Note [Stable Unfolding for list producers]
    {-# INLINE enumFromThen #-}
    enumFromThen :: Int -> Int -> [Int]
enumFromThen (I# Int#
x1) (I# Int#
x2) = Int# -> Int# -> [Int]
efdInt Int#
x1 Int#
x2

    -- See Note [Stable Unfolding for list producers]
    {-# INLINE enumFromThenTo #-}
    enumFromThenTo :: Int -> Int -> Int -> [Int]
enumFromThenTo (I# Int#
x1) (I# Int#
x2) (I# Int#
y) = Int# -> Int# -> Int# -> [Int]
efdtInt Int#
x1 Int#
x2 Int#
y


-----------------------------------------------------
-- eftInt and eftIntFB deal with [a..b], which is the
-- most common form, so we take a lot of care
-- In particular, we have rules for deforestation

{-# RULES
"eftInt"        [~1] forall x y. eftInt x y = build (\ c n -> eftIntFB c n x y)
"eftIntList"    [1] eftIntFB  (:) [] = eftInt
 #-}

{- Note [How the Enum rules work]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Phase 2: eftInt ---> build . eftIntFB
* Phase 1: inline build; eftIntFB (:) --> eftInt
* Phase 0: optionally inline eftInt
-}

{-# NOINLINE [1] eftInt #-}
eftInt :: Int# -> Int# -> [Int]
-- [x1..x2]
eftInt :: Int# -> Int# -> [Int]
eftInt Int#
x0 Int#
y | Int# -> Bool
isTrue# (Int#
x0 Int# -> Int# -> Int#
># Int#
y) = []
            | Bool
otherwise         = Int# -> [Int]
go Int#
x0
               where
                 go :: Int# -> [Int]
go Int#
x = Int# -> Int
I# Int#
x Int -> [Int] -> [Int]
forall a. a -> [a] -> [a]
: if Int# -> Bool
isTrue# (Int#
x Int# -> Int# -> Int#
==# Int#
y)
                               then []
                               else Int# -> [Int]
go (Int#
x Int# -> Int# -> Int#
+# Int#
1#)

{-# INLINE [0] eftIntFB #-} -- See Note [Inline FB functions] in GHC.List
eftIntFB :: (Int -> r -> r) -> r -> Int# -> Int# -> r
eftIntFB :: forall r. (Int -> r -> r) -> r -> Int# -> Int# -> r
eftIntFB Int -> r -> r
c r
n Int#
x0 Int#
y | Int# -> Bool
isTrue# (Int#
x0 Int# -> Int# -> Int#
># Int#
y) = r
n
                  | Bool
otherwise         = Int# -> r
go Int#
x0
                 where
                   go :: Int# -> r
go Int#
x = Int# -> Int
I# Int#
x Int -> r -> r
`c` if Int# -> Bool
isTrue# (Int#
x Int# -> Int# -> Int#
==# Int#
y)
                                   then r
n
                                   else Int# -> r
go (Int#
x Int# -> Int# -> Int#
+# Int#
1#)
                        -- Watch out for y=maxBound; hence ==, not >
        -- Be very careful not to have more than one "c"
        -- so that when eftInfFB is inlined we can inline
        -- whatever is bound to "c"


-----------------------------------------------------
-- efdInt and efdtInt deal with [a,b..] and [a,b..c].
-- The code is more complicated because of worries about Int overflow.

-- See Note [How the Enum rules work]
{-# RULES
"efdtInt"       [~1] forall x1 x2 y.
                     efdtInt x1 x2 y = build (\ c n -> efdtIntFB c n x1 x2 y)
"efdtIntUpList" [1]  efdtIntFB (:) [] = efdtInt
 #-}

efdInt :: Int# -> Int# -> [Int]
-- [x1,x2..maxInt]
efdInt :: Int# -> Int# -> [Int]
efdInt Int#
x1 Int#
x2
 | Int# -> Bool
isTrue# (Int#
x2 Int# -> Int# -> Int#
>=# Int#
x1) = case Int
maxInt of I# Int#
y -> Int# -> Int# -> Int# -> [Int]
efdtIntUp Int#
x1 Int#
x2 Int#
y
 | Bool
otherwise           = case Int
minInt of I# Int#
y -> Int# -> Int# -> Int# -> [Int]
efdtIntDn Int#
x1 Int#
x2 Int#
y

{-# NOINLINE [1] efdtInt #-}
efdtInt :: Int# -> Int# -> Int# -> [Int]
-- [x1,x2..y]
efdtInt :: Int# -> Int# -> Int# -> [Int]
efdtInt Int#
x1 Int#
x2 Int#
y
 | Int# -> Bool
isTrue# (Int#
x2 Int# -> Int# -> Int#
>=# Int#
x1) = Int# -> Int# -> Int# -> [Int]
efdtIntUp Int#
x1 Int#
x2 Int#
y
 | Bool
otherwise           = Int# -> Int# -> Int# -> [Int]
efdtIntDn Int#
x1 Int#
x2 Int#
y

{-# INLINE [0] efdtIntFB #-} -- See Note [Inline FB functions] in GHC.List
efdtIntFB :: (Int -> r -> r) -> r -> Int# -> Int# -> Int# -> r
efdtIntFB :: forall r. (Int -> r -> r) -> r -> Int# -> Int# -> Int# -> r
efdtIntFB Int -> r -> r
c r
n Int#
x1 Int#
x2 Int#
y
 | Int# -> Bool
isTrue# (Int#
x2 Int# -> Int# -> Int#
>=# Int#
x1) = (Int -> r -> r) -> r -> Int# -> Int# -> Int# -> r
forall r. (Int -> r -> r) -> r -> Int# -> Int# -> Int# -> r
efdtIntUpFB Int -> r -> r
c r
n Int#
x1 Int#
x2 Int#
y
 | Bool
otherwise           = (Int -> r -> r) -> r -> Int# -> Int# -> Int# -> r
forall r. (Int -> r -> r) -> r -> Int# -> Int# -> Int# -> r
efdtIntDnFB Int -> r -> r
c r
n Int#
x1 Int#
x2 Int#
y

-- Requires x2 >= x1
efdtIntUp :: Int# -> Int# -> Int# -> [Int]
efdtIntUp :: Int# -> Int# -> Int# -> [Int]
efdtIntUp Int#
x1 Int#
x2 Int#
y    -- Be careful about overflow!
 | Int# -> Bool
isTrue# (Int#
y Int# -> Int# -> Int#
<# Int#
x2) = if Int# -> Bool
isTrue# (Int#
y Int# -> Int# -> Int#
<# Int#
x1) then [] else [Int# -> Int
I# Int#
x1]
 | Bool
otherwise = -- Common case: x1 <= x2 <= y
               let !delta :: Int#
delta = Int#
x2 Int# -> Int# -> Int#
-# Int#
x1 -- >= 0
                   !y' :: Int#
y' = Int#
y Int# -> Int# -> Int#
-# Int#
delta  -- x1 <= y' <= y; hence y' is representable

                   -- Invariant: x <= y
                   -- Note that: z <= y' => z + delta won't overflow
                   -- so we are guaranteed not to overflow if/when we recurse
                   go_up :: Int# -> [Int]
go_up Int#
x | Int# -> Bool
isTrue# (Int#
x Int# -> Int# -> Int#
># Int#
y') = [Int# -> Int
I# Int#
x]
                           | Bool
otherwise         = Int# -> Int
I# Int#
x Int -> [Int] -> [Int]
forall a. a -> [a] -> [a]
: Int# -> [Int]
go_up (Int#
x Int# -> Int# -> Int#
+# Int#
delta)
               in Int# -> Int
I# Int#
x1 Int -> [Int] -> [Int]
forall a. a -> [a] -> [a]
: Int# -> [Int]
go_up Int#
x2

-- Requires x2 >= x1
{-# INLINE [0] efdtIntUpFB #-} -- See Note [Inline FB functions] in GHC.List
efdtIntUpFB :: (Int -> r -> r) -> r -> Int# -> Int# -> Int# -> r
efdtIntUpFB :: forall r. (Int -> r -> r) -> r -> Int# -> Int# -> Int# -> r
efdtIntUpFB Int -> r -> r
c r
n Int#
x1 Int#
x2 Int#
y    -- Be careful about overflow!
 | Int# -> Bool
isTrue# (Int#
y Int# -> Int# -> Int#
<# Int#
x2) = if Int# -> Bool
isTrue# (Int#
y Int# -> Int# -> Int#
<# Int#
x1) then r
n else Int# -> Int
I# Int#
x1 Int -> r -> r
`c` r
n
 | Bool
otherwise = -- Common case: x1 <= x2 <= y
               let !delta :: Int#
delta = Int#
x2 Int# -> Int# -> Int#
-# Int#
x1 -- >= 0
                   !y' :: Int#
y' = Int#
y Int# -> Int# -> Int#
-# Int#
delta  -- x1 <= y' <= y; hence y' is representable

                   -- Invariant: x <= y
                   -- Note that: z <= y' => z + delta won't overflow
                   -- so we are guaranteed not to overflow if/when we recurse
                   go_up :: Int# -> r
go_up Int#
x | Int# -> Bool
isTrue# (Int#
x Int# -> Int# -> Int#
># Int#
y') = Int# -> Int
I# Int#
x Int -> r -> r
`c` r
n
                           | Bool
otherwise         = Int# -> Int
I# Int#
x Int -> r -> r
`c` Int# -> r
go_up (Int#
x Int# -> Int# -> Int#
+# Int#
delta)
               in Int# -> Int
I# Int#
x1 Int -> r -> r
`c` Int# -> r
go_up Int#
x2

-- Requires x2 <= x1
efdtIntDn :: Int# -> Int# -> Int# -> [Int]
efdtIntDn :: Int# -> Int# -> Int# -> [Int]
efdtIntDn Int#
x1 Int#
x2 Int#
y    -- Be careful about underflow!
 | Int# -> Bool
isTrue# (Int#
y Int# -> Int# -> Int#
># Int#
x2) = if Int# -> Bool
isTrue# (Int#
y Int# -> Int# -> Int#
># Int#
x1) then [] else [Int# -> Int
I# Int#
x1]
 | Bool
otherwise = -- Common case: x1 >= x2 >= y
               let !delta :: Int#
delta = Int#
x2 Int# -> Int# -> Int#
-# Int#
x1 -- <= 0
                   !y' :: Int#
y' = Int#
y Int# -> Int# -> Int#
-# Int#
delta  -- y <= y' <= x1; hence y' is representable

                   -- Invariant: x >= y
                   -- Note that: z >= y' => z + delta won't underflow
                   -- so we are guaranteed not to underflow if/when we recurse
                   go_dn :: Int# -> [Int]
go_dn Int#
x | Int# -> Bool
isTrue# (Int#
x Int# -> Int# -> Int#
<# Int#
y') = [Int# -> Int
I# Int#
x]
                           | Bool
otherwise         = Int# -> Int
I# Int#
x Int -> [Int] -> [Int]
forall a. a -> [a] -> [a]
: Int# -> [Int]
go_dn (Int#
x Int# -> Int# -> Int#
+# Int#
delta)
   in Int# -> Int
I# Int#
x1 Int -> [Int] -> [Int]
forall a. a -> [a] -> [a]
: Int# -> [Int]
go_dn Int#
x2

-- Requires x2 <= x1
{-# INLINE [0] efdtIntDnFB #-} -- See Note [Inline FB functions] in GHC.List
efdtIntDnFB :: (Int -> r -> r) -> r -> Int# -> Int# -> Int# -> r
efdtIntDnFB :: forall r. (Int -> r -> r) -> r -> Int# -> Int# -> Int# -> r
efdtIntDnFB Int -> r -> r
c r
n Int#
x1 Int#
x2 Int#
y    -- Be careful about underflow!
 | Int# -> Bool
isTrue# (Int#
y Int# -> Int# -> Int#
># Int#
x2) = if Int# -> Bool
isTrue# (Int#
y Int# -> Int# -> Int#
># Int#
x1) then r
n else Int# -> Int
I# Int#
x1 Int -> r -> r
`c` r
n
 | Bool
otherwise = -- Common case: x1 >= x2 >= y
               let !delta :: Int#
delta = Int#
x2 Int# -> Int# -> Int#
-# Int#
x1 -- <= 0
                   !y' :: Int#
y' = Int#
y Int# -> Int# -> Int#
-# Int#
delta  -- y <= y' <= x1; hence y' is representable

                   -- Invariant: x >= y
                   -- Note that: z >= y' => z + delta won't underflow
                   -- so we are guaranteed not to underflow if/when we recurse
                   go_dn :: Int# -> r
go_dn Int#
x | Int# -> Bool
isTrue# (Int#
x Int# -> Int# -> Int#
<# Int#
y') = Int# -> Int
I# Int#
x Int -> r -> r
`c` r
n
                           | Bool
otherwise         = Int# -> Int
I# Int#
x Int -> r -> r
`c` Int# -> r
go_dn (Int#
x Int# -> Int# -> Int#
+# Int#
delta)
               in Int# -> Int
I# Int#
x1 Int -> r -> r
`c` Int# -> r
go_dn Int#
x2


------------------------------------------------------------------------
-- Word
------------------------------------------------------------------------

-- | @since 2.01
instance Bounded Word where
    minBound :: Word
minBound = Word
0

    -- use unboxed literals for maxBound, because GHC doesn't optimise
    -- (fromInteger 0xffffffff :: Word).
#if WORD_SIZE_IN_BITS == 32
    maxBound = W# 0xFFFFFFFF##
#elif WORD_SIZE_IN_BITS == 64
    maxBound :: Word
maxBound = Word# -> Word
W# Word#
0xFFFFFFFFFFFFFFFF##
#else
#error Unhandled value for WORD_SIZE_IN_BITS
#endif

-- | @since 2.01
instance Enum Word where
    succ :: Word -> Word
succ Word
x
        | Word
x Word -> Word -> Bool
forall a. Eq a => a -> a -> Bool
/= Word
forall a. Bounded a => a
maxBound = Word
x Word -> Word -> Word
forall a. Num a => a -> a -> a
+ Word
1
        | Bool
otherwise     = String -> Word
forall a. String -> a
succError String
"Word"
    pred :: Word -> Word
pred Word
x
        | Word
x Word -> Word -> Bool
forall a. Eq a => a -> a -> Bool
/= Word
forall a. Bounded a => a
minBound = Word
x Word -> Word -> Word
forall a. Num a => a -> a -> a
- Word
1
        | Bool
otherwise     = String -> Word
forall a. String -> a
predError String
"Word"
    toEnum :: Int -> Word
toEnum i :: Int
i@(I# Int#
i#)
        | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0        = Word# -> Word
W# (Int# -> Word#
int2Word# Int#
i#)
        | Bool
otherwise     = String -> Int -> (Word, Word) -> Word
forall a b. Show a => String -> Int -> (a, a) -> b
toEnumError String
"Word" Int
i (Word
forall a. Bounded a => a
minBound::Word, Word
forall a. Bounded a => a
maxBound::Word)
    fromEnum :: Word -> Int
fromEnum x :: Word
x@(W# Word#
x#)
        | Word
x Word -> Word -> Bool
forall a. Ord a => a -> a -> Bool
<= Word
maxIntWord = Int# -> Int
I# (Word# -> Int#
word2Int# Word#
x#)
        | Bool
otherwise       = String -> Word -> Int
forall a b. Show a => String -> a -> b
fromEnumError String
"Word" Word
x

    {-# INLINE enumFrom #-}
    enumFrom :: Word -> [Word]
enumFrom (W# Word#
x#)      = Word# -> Word# -> [Word]
eftWord Word#
x# Word#
maxWord#
        where !(W# Word#
maxWord#) = Word
forall a. Bounded a => a
maxBound
        -- Blarg: technically I guess enumFrom isn't strict!

    {-# INLINE enumFromTo #-}
    enumFromTo :: Word -> Word -> [Word]
enumFromTo (W# Word#
x) (W# Word#
y) = Word# -> Word# -> [Word]
eftWord Word#
x Word#
y

    {-# INLINE enumFromThen #-}
    enumFromThen :: Word -> Word -> [Word]
enumFromThen (W# Word#
x1) (W# Word#
x2) = Word# -> Word# -> [Word]
efdWord Word#
x1 Word#
x2

    {-# INLINE enumFromThenTo #-}
    enumFromThenTo :: Word -> Word -> Word -> [Word]
enumFromThenTo (W# Word#
x1) (W# Word#
x2) (W# Word#
y) = Word# -> Word# -> Word# -> [Word]
efdtWord Word#
x1 Word#
x2 Word#
y

maxIntWord :: Word
-- The biggest word representable as an Int
maxIntWord :: Word
maxIntWord = Word# -> Word
W# (case Int
maxInt of I# Int#
i -> Int# -> Word#
int2Word# Int#
i)

-----------------------------------------------------
-- eftWord and eftWordFB deal with [a..b], which is the
-- most common form, so we take a lot of care
-- In particular, we have rules for deforestation

{-# RULES
"eftWord"        [~1] forall x y. eftWord x y = build (\ c n -> eftWordFB c n x y)
"eftWordList"    [1] eftWordFB  (:) [] = eftWord
 #-}

-- The Enum rules for Word work much the same way that they do for Int.
-- See Note [How the Enum rules work].

{-# NOINLINE [1] eftWord #-}
eftWord :: Word# -> Word# -> [Word]
-- [x1..x2]
eftWord :: Word# -> Word# -> [Word]
eftWord Word#
x0 Word#
y | Int# -> Bool
isTrue# (Word#
x0 Word# -> Word# -> Int#
`gtWord#` Word#
y) = []
             | Bool
otherwise                = Word# -> [Word]
go Word#
x0
                where
                  go :: Word# -> [Word]
go Word#
x = Word# -> Word
W# Word#
x Word -> [Word] -> [Word]
forall a. a -> [a] -> [a]
: if Int# -> Bool
isTrue# (Word#
x Word# -> Word# -> Int#
`eqWord#` Word#
y)
                                then []
                                else Word# -> [Word]
go (Word#
x Word# -> Word# -> Word#
`plusWord#` Word#
1##)

{-# INLINE [0] eftWordFB #-} -- See Note [Inline FB functions] in GHC.List
eftWordFB :: (Word -> r -> r) -> r -> Word# -> Word# -> r
eftWordFB :: forall r. (Word -> r -> r) -> r -> Word# -> Word# -> r
eftWordFB Word -> r -> r
c r
n Word#
x0 Word#
y | Int# -> Bool
isTrue# (Word#
x0 Word# -> Word# -> Int#
`gtWord#` Word#
y) = r
n
                   | Bool
otherwise                = Word# -> r
go Word#
x0
                  where
                    go :: Word# -> r
go Word#
x = Word# -> Word
W# Word#
x Word -> r -> r
`c` if Int# -> Bool
isTrue# (Word#
x Word# -> Word# -> Int#
`eqWord#` Word#
y)
                                    then r
n
                                    else Word# -> r
go (Word#
x Word# -> Word# -> Word#
`plusWord#` Word#
1##)
                        -- Watch out for y=maxBound; hence ==, not >
        -- Be very careful not to have more than one "c"
        -- so that when eftInfFB is inlined we can inline
        -- whatever is bound to "c"


-----------------------------------------------------
-- efdWord and efdtWord deal with [a,b..] and [a,b..c].
-- The code is more complicated because of worries about Word overflow.

-- See Note [How the Enum rules work]
{-# RULES
"efdtWord"       [~1] forall x1 x2 y.
                     efdtWord x1 x2 y = build (\ c n -> efdtWordFB c n x1 x2 y)
"efdtWordUpList" [1]  efdtWordFB (:) [] = efdtWord
 #-}

efdWord :: Word# -> Word# -> [Word]
-- [x1,x2..maxWord]
efdWord :: Word# -> Word# -> [Word]
efdWord Word#
x1 Word#
x2
 | Int# -> Bool
isTrue# (Word#
x2 Word# -> Word# -> Int#
`geWord#` Word#
x1) = case Word
forall a. Bounded a => a
maxBound of W# Word#
y -> Word# -> Word# -> Word# -> [Word]
efdtWordUp Word#
x1 Word#
x2 Word#
y
 | Bool
otherwise                 = case Word
forall a. Bounded a => a
minBound of W# Word#
y -> Word# -> Word# -> Word# -> [Word]
efdtWordDn Word#
x1 Word#
x2 Word#
y

{-# NOINLINE [1] efdtWord #-}
efdtWord :: Word# -> Word# -> Word# -> [Word]
-- [x1,x2..y]
efdtWord :: Word# -> Word# -> Word# -> [Word]
efdtWord Word#
x1 Word#
x2 Word#
y
 | Int# -> Bool
isTrue# (Word#
x2 Word# -> Word# -> Int#
`geWord#` Word#
x1) = Word# -> Word# -> Word# -> [Word]
efdtWordUp Word#
x1 Word#
x2 Word#
y
 | Bool
otherwise                 = Word# -> Word# -> Word# -> [Word]
efdtWordDn Word#
x1 Word#
x2 Word#
y

{-# INLINE [0] efdtWordFB #-} -- See Note [Inline FB functions] in GHC.List
efdtWordFB :: (Word -> r -> r) -> r -> Word# -> Word# -> Word# -> r
efdtWordFB :: forall r. (Word -> r -> r) -> r -> Word# -> Word# -> Word# -> r
efdtWordFB Word -> r -> r
c r
n Word#
x1 Word#
x2 Word#
y
 | Int# -> Bool
isTrue# (Word#
x2 Word# -> Word# -> Int#
`geWord#` Word#
x1) = (Word -> r -> r) -> r -> Word# -> Word# -> Word# -> r
forall r. (Word -> r -> r) -> r -> Word# -> Word# -> Word# -> r
efdtWordUpFB Word -> r -> r
c r
n Word#
x1 Word#
x2 Word#
y
 | Bool
otherwise                 = (Word -> r -> r) -> r -> Word# -> Word# -> Word# -> r
forall r. (Word -> r -> r) -> r -> Word# -> Word# -> Word# -> r
efdtWordDnFB Word -> r -> r
c r
n Word#
x1 Word#
x2 Word#
y

-- Requires x2 >= x1
efdtWordUp :: Word# -> Word# -> Word# -> [Word]
efdtWordUp :: Word# -> Word# -> Word# -> [Word]
efdtWordUp Word#
x1 Word#
x2 Word#
y    -- Be careful about overflow!
 | Int# -> Bool
isTrue# (Word#
y Word# -> Word# -> Int#
`ltWord#` Word#
x2) = if Int# -> Bool
isTrue# (Word#
y Word# -> Word# -> Int#
`ltWord#` Word#
x1) then [] else [Word# -> Word
W# Word#
x1]
 | Bool
otherwise = -- Common case: x1 <= x2 <= y
               let !delta :: Word#
delta = Word#
x2 Word# -> Word# -> Word#
`minusWord#` Word#
x1 -- >= 0
                   !y' :: Word#
y' = Word#
y Word# -> Word# -> Word#
`minusWord#` Word#
delta  -- x1 <= y' <= y; hence y' is representable

                   -- Invariant: x <= y
                   -- Note that: z <= y' => z + delta won't overflow
                   -- so we are guaranteed not to overflow if/when we recurse
                   go_up :: Word# -> [Word]
go_up Word#
x | Int# -> Bool
isTrue# (Word#
x Word# -> Word# -> Int#
`gtWord#` Word#
y') = [Word# -> Word
W# Word#
x]
                           | Bool
otherwise                = Word# -> Word
W# Word#
x Word -> [Word] -> [Word]
forall a. a -> [a] -> [a]
: Word# -> [Word]
go_up (Word#
x Word# -> Word# -> Word#
`plusWord#` Word#
delta)
               in Word# -> Word
W# Word#
x1 Word -> [Word] -> [Word]
forall a. a -> [a] -> [a]
: Word# -> [Word]
go_up Word#
x2

-- Requires x2 >= x1
{-# INLINE [0] efdtWordUpFB #-} -- See Note [Inline FB functions] in GHC.List
efdtWordUpFB :: (Word -> r -> r) -> r -> Word# -> Word# -> Word# -> r
efdtWordUpFB :: forall r. (Word -> r -> r) -> r -> Word# -> Word# -> Word# -> r
efdtWordUpFB Word -> r -> r
c r
n Word#
x1 Word#
x2 Word#
y    -- Be careful about overflow!
 | Int# -> Bool
isTrue# (Word#
y Word# -> Word# -> Int#
`ltWord#` Word#
x2) = if Int# -> Bool
isTrue# (Word#
y Word# -> Word# -> Int#
`ltWord#` Word#
x1) then r
n else Word# -> Word
W# Word#
x1 Word -> r -> r
`c` r
n
 | Bool
otherwise = -- Common case: x1 <= x2 <= y
               let !delta :: Word#
delta = Word#
x2 Word# -> Word# -> Word#
`minusWord#` Word#
x1 -- >= 0
                   !y' :: Word#
y' = Word#
y Word# -> Word# -> Word#
`minusWord#` Word#
delta  -- x1 <= y' <= y; hence y' is representable

                   -- Invariant: x <= y
                   -- Note that: z <= y' => z + delta won't overflow
                   -- so we are guaranteed not to overflow if/when we recurse
                   go_up :: Word# -> r
go_up Word#
x | Int# -> Bool
isTrue# (Word#
x Word# -> Word# -> Int#
`gtWord#` Word#
y') = Word# -> Word
W# Word#
x Word -> r -> r
`c` r
n
                           | Bool
otherwise                = Word# -> Word
W# Word#
x Word -> r -> r
`c` Word# -> r
go_up (Word#
x Word# -> Word# -> Word#
`plusWord#` Word#
delta)
               in Word# -> Word
W# Word#
x1 Word -> r -> r
`c` Word# -> r
go_up Word#
x2

-- Requires x2 <= x1
efdtWordDn :: Word# -> Word# -> Word# -> [Word]
efdtWordDn :: Word# -> Word# -> Word# -> [Word]
efdtWordDn Word#
x1 Word#
x2 Word#
y    -- Be careful about underflow!
 | Int# -> Bool
isTrue# (Word#
y Word# -> Word# -> Int#
`gtWord#` Word#
x2) = if Int# -> Bool
isTrue# (Word#
y Word# -> Word# -> Int#
`gtWord#` Word#
x1) then [] else [Word# -> Word
W# Word#
x1]
 | Bool
otherwise = -- Common case: x1 >= x2 >= y
               let !delta :: Word#
delta = Word#
x2 Word# -> Word# -> Word#
`minusWord#` Word#
x1 -- <= 0
                   !y' :: Word#
y' = Word#
y Word# -> Word# -> Word#
`minusWord#` Word#
delta  -- y <= y' <= x1; hence y' is representable

                   -- Invariant: x >= y
                   -- Note that: z >= y' => z + delta won't underflow
                   -- so we are guaranteed not to underflow if/when we recurse
                   go_dn :: Word# -> [Word]
go_dn Word#
x | Int# -> Bool
isTrue# (Word#
x Word# -> Word# -> Int#
`ltWord#` Word#
y') = [Word# -> Word
W# Word#
x]
                           | Bool
otherwise                = Word# -> Word
W# Word#
x Word -> [Word] -> [Word]
forall a. a -> [a] -> [a]
: Word# -> [Word]
go_dn (Word#
x Word# -> Word# -> Word#
`plusWord#` Word#
delta)
   in Word# -> Word
W# Word#
x1 Word -> [Word] -> [Word]
forall a. a -> [a] -> [a]
: Word# -> [Word]
go_dn Word#
x2

-- Requires x2 <= x1
{-# INLINE [0] efdtWordDnFB #-} -- See Note [Inline FB functions] in GHC.List
efdtWordDnFB :: (Word -> r -> r) -> r -> Word# -> Word# -> Word# -> r
efdtWordDnFB :: forall r. (Word -> r -> r) -> r -> Word# -> Word# -> Word# -> r
efdtWordDnFB Word -> r -> r
c r
n Word#
x1 Word#
x2 Word#
y    -- Be careful about underflow!
 | Int# -> Bool
isTrue# (Word#
y Word# -> Word# -> Int#
`gtWord#` Word#
x2) = if Int# -> Bool
isTrue# (Word#
y Word# -> Word# -> Int#
`gtWord#` Word#
x1) then r
n else Word# -> Word
W# Word#
x1 Word -> r -> r
`c` r
n
 | Bool
otherwise = -- Common case: x1 >= x2 >= y
               let !delta :: Word#
delta = Word#
x2 Word# -> Word# -> Word#
`minusWord#` Word#
x1 -- <= 0
                   !y' :: Word#
y' = Word#
y Word# -> Word# -> Word#
`minusWord#` Word#
delta  -- y <= y' <= x1; hence y' is representable

                   -- Invariant: x >= y
                   -- Note that: z >= y' => z + delta won't underflow
                   -- so we are guaranteed not to underflow if/when we recurse
                   go_dn :: Word# -> r
go_dn Word#
x | Int# -> Bool
isTrue# (Word#
x Word# -> Word# -> Int#
`ltWord#` Word#
y') = Word# -> Word
W# Word#
x Word -> r -> r
`c` r
n
                           | Bool
otherwise                = Word# -> Word
W# Word#
x Word -> r -> r
`c` Word# -> r
go_dn (Word#
x Word# -> Word# -> Word#
`plusWord#` Word#
delta)
               in Word# -> Word
W# Word#
x1 Word -> r -> r
`c` Word# -> r
go_dn Word#
x2

------------------------------------------------------------------------
-- Integer
------------------------------------------------------------------------

-- | @since 2.01
instance  Enum Integer  where
    succ :: Integer -> Integer
succ Integer
x               = Integer
x Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
1
    pred :: Integer -> Integer
pred Integer
x               = Integer
x Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
1
    toEnum :: Int -> Integer
toEnum (I# Int#
n)        = Int# -> Integer
IS Int#
n
    fromEnum :: Integer -> Int
fromEnum Integer
n           = Integer -> Int
integerToInt Integer
n

    -- See Note [Stable Unfolding for list producers]
    {-# INLINE enumFrom #-}
    enumFrom :: Integer -> [Integer]
enumFrom Integer
x = Integer -> Integer -> [Integer]
enumDeltaInteger Integer
x Integer
1

    -- See Note [Stable Unfolding for list producers]
    {-# INLINE enumFromThen #-}
    enumFromThen :: Integer -> Integer -> [Integer]
enumFromThen Integer
x Integer
y = Integer -> Integer -> [Integer]
enumDeltaInteger Integer
x (Integer
yInteger -> Integer -> Integer
forall a. Num a => a -> a -> a
-Integer
x)

    -- See Note [Stable Unfolding for list producers]
    {-# INLINE enumFromTo #-}
    enumFromTo :: Integer -> Integer -> [Integer]
enumFromTo Integer
x Integer
lim = Integer -> Integer -> Integer -> [Integer]
enumDeltaToInteger Integer
x Integer
1 Integer
lim

    -- See Note [Stable Unfolding for list producers]
    {-# INLINE enumFromThenTo #-}
    enumFromThenTo :: Integer -> Integer -> Integer -> [Integer]
enumFromThenTo Integer
x Integer
y Integer
lim = Integer -> Integer -> Integer -> [Integer]
enumDeltaToInteger Integer
x (Integer
yInteger -> Integer -> Integer
forall a. Num a => a -> a -> a
-Integer
x) Integer
lim

-- See Note [How the Enum rules work]
{-# RULES
"enumDeltaInteger"      [~1] forall x y.   enumDeltaInteger x y         = build (\c _ -> enumDeltaIntegerFB c x y)
"efdtInteger"           [~1] forall x d l. enumDeltaToInteger x d l     = build (\c n -> enumDeltaToIntegerFB  c n x d l)
"efdtInteger1"          [~1] forall x l.   enumDeltaToInteger x 1 l     = build (\c n -> enumDeltaToInteger1FB c n x l)

"enumDeltaToInteger1FB" [1] forall c n x.  enumDeltaToIntegerFB c n x 1 = enumDeltaToInteger1FB c n x

"enumDeltaInteger"      [1] enumDeltaIntegerFB    (:)     = enumDeltaInteger
"enumDeltaToInteger"    [1] enumDeltaToIntegerFB  (:) []  = enumDeltaToInteger
"enumDeltaToInteger1"   [1] enumDeltaToInteger1FB (:) []  = enumDeltaToInteger1
 #-}

{- Note [Enum Integer rules for literal 1]
The "1" rules above specialise for the common case where delta = 1,
so that we can avoid the delta>=0 test in enumDeltaToIntegerFB.
Then enumDeltaToInteger1FB is nice and small and can be inlined,
which would allow the constructor to be inlined and good things to happen.

We match on the literal "1" both in phase 2 (rule "efdtInteger1") and
phase 1 (rule "enumDeltaToInteger1FB"), just for belt and braces

We do not do it for Int this way because hand-tuned code already exists, and
the special case varies more from the general case, due to the issue of overflows.
-}

{-# INLINE [0] enumDeltaIntegerFB #-}
-- See Note [Inline FB functions] in GHC.List
enumDeltaIntegerFB :: (Integer -> b -> b) -> Integer -> Integer -> b
enumDeltaIntegerFB :: forall b. (Integer -> b -> b) -> Integer -> Integer -> b
enumDeltaIntegerFB Integer -> b -> b
c Integer
x0 Integer
d = Integer -> b
go Integer
x0
  where go :: Integer -> b
go Integer
x = Integer
x Integer -> b -> b
`seq` (Integer
x Integer -> b -> b
`c` Integer -> b
go (Integer
xInteger -> Integer -> Integer
forall a. Num a => a -> a -> a
+Integer
d))

{-# NOINLINE [1] enumDeltaInteger #-}
enumDeltaInteger :: Integer -> Integer -> [Integer]
enumDeltaInteger :: Integer -> Integer -> [Integer]
enumDeltaInteger Integer
x Integer
d = Integer
x Integer -> [Integer] -> [Integer]
`seq` (Integer
x Integer -> [Integer] -> [Integer]
forall a. a -> [a] -> [a]
: Integer -> Integer -> [Integer]
enumDeltaInteger (Integer
xInteger -> Integer -> Integer
forall a. Num a => a -> a -> a
+Integer
d) Integer
d)
-- strict accumulator, so
--     head (drop 1000000 [1 .. ]
-- works

{-# INLINE [0] enumDeltaToIntegerFB #-}
-- See Note [Inline FB functions] in GHC.List
-- Don't inline this until RULE "enumDeltaToInteger" has had a chance to fire
enumDeltaToIntegerFB :: (Integer -> a -> a) -> a
                     -> Integer -> Integer -> Integer -> a
enumDeltaToIntegerFB :: forall a.
(Integer -> a -> a) -> a -> Integer -> Integer -> Integer -> a
enumDeltaToIntegerFB Integer -> a -> a
c a
n Integer
x Integer
delta Integer
lim
  | Integer
delta Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
>= Integer
0 = (Integer -> a -> a) -> a -> Integer -> Integer -> Integer -> a
forall a.
(Integer -> a -> a) -> a -> Integer -> Integer -> Integer -> a
up_fb Integer -> a -> a
c a
n Integer
x Integer
delta Integer
lim
  | Bool
otherwise  = (Integer -> a -> a) -> a -> Integer -> Integer -> Integer -> a
forall a.
(Integer -> a -> a) -> a -> Integer -> Integer -> Integer -> a
dn_fb Integer -> a -> a
c a
n Integer
x Integer
delta Integer
lim

{-# INLINE [0] enumDeltaToInteger1FB #-}
-- See Note [Inline FB functions] in GHC.List
-- Don't inline this until RULE "enumDeltaToInteger" has had a chance to fire
enumDeltaToInteger1FB :: (Integer -> a -> a) -> a
                      -> Integer -> Integer -> a
enumDeltaToInteger1FB :: forall a. (Integer -> a -> a) -> a -> Integer -> Integer -> a
enumDeltaToInteger1FB Integer -> a -> a
c a
n Integer
x0 Integer
lim = Integer -> a
go (Integer
x0 :: Integer)
                      where
                        go :: Integer -> a
go Integer
x | Integer
x Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Integer
lim   = a
n
                             | Bool
otherwise = Integer
x Integer -> a -> a
`c` Integer -> a
go (Integer
xInteger -> Integer -> Integer
forall a. Num a => a -> a -> a
+Integer
1)

{-# NOINLINE [1] enumDeltaToInteger #-}
enumDeltaToInteger :: Integer -> Integer -> Integer -> [Integer]
enumDeltaToInteger :: Integer -> Integer -> Integer -> [Integer]
enumDeltaToInteger Integer
x Integer
delta Integer
lim
  | Integer
delta Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
>= Integer
0 = Integer -> Integer -> Integer -> [Integer]
up_list Integer
x Integer
delta Integer
lim
  | Bool
otherwise  = Integer -> Integer -> Integer -> [Integer]
dn_list Integer
x Integer
delta Integer
lim

{-# NOINLINE [1] enumDeltaToInteger1 #-}
enumDeltaToInteger1 :: Integer -> Integer -> [Integer]
-- Special case for Delta = 1
enumDeltaToInteger1 :: Integer -> Integer -> [Integer]
enumDeltaToInteger1 Integer
x0 Integer
lim = Integer -> [Integer]
go (Integer
x0 :: Integer)
                      where
                        go :: Integer -> [Integer]
go Integer
x | Integer
x Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Integer
lim   = []
                             | Bool
otherwise = Integer
x Integer -> [Integer] -> [Integer]
forall a. a -> [a] -> [a]
: Integer -> [Integer]
go (Integer
xInteger -> Integer -> Integer
forall a. Num a => a -> a -> a
+Integer
1)

up_fb :: (Integer -> a -> a) -> a -> Integer -> Integer -> Integer -> a
up_fb :: forall a.
(Integer -> a -> a) -> a -> Integer -> Integer -> Integer -> a
up_fb Integer -> a -> a
c a
n Integer
x0 Integer
delta Integer
lim = Integer -> a
go (Integer
x0 :: Integer)
                      where
                        go :: Integer -> a
go Integer
x | Integer
x Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Integer
lim   = a
n
                             | Bool
otherwise = Integer
x Integer -> a -> a
`c` Integer -> a
go (Integer
xInteger -> Integer -> Integer
forall a. Num a => a -> a -> a
+Integer
delta)
dn_fb :: (Integer -> a -> a) -> a -> Integer -> Integer -> Integer -> a
dn_fb :: forall a.
(Integer -> a -> a) -> a -> Integer -> Integer -> Integer -> a
dn_fb Integer -> a -> a
c a
n Integer
x0 Integer
delta Integer
lim = Integer -> a
go (Integer
x0 :: Integer)
                      where
                        go :: Integer -> a
go Integer
x | Integer
x Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
lim   = a
n
                             | Bool
otherwise = Integer
x Integer -> a -> a
`c` Integer -> a
go (Integer
xInteger -> Integer -> Integer
forall a. Num a => a -> a -> a
+Integer
delta)

up_list :: Integer -> Integer -> Integer -> [Integer]
up_list :: Integer -> Integer -> Integer -> [Integer]
up_list Integer
x0 Integer
delta Integer
lim = Integer -> [Integer]
go (Integer
x0 :: Integer)
                    where
                        go :: Integer -> [Integer]
go Integer
x | Integer
x Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Integer
lim   = []
                             | Bool
otherwise = Integer
x Integer -> [Integer] -> [Integer]
forall a. a -> [a] -> [a]
: Integer -> [Integer]
go (Integer
xInteger -> Integer -> Integer
forall a. Num a => a -> a -> a
+Integer
delta)
dn_list :: Integer -> Integer -> Integer -> [Integer]
dn_list :: Integer -> Integer -> Integer -> [Integer]
dn_list Integer
x0 Integer
delta Integer
lim = Integer -> [Integer]
go (Integer
x0 :: Integer)
                    where
                        go :: Integer -> [Integer]
go Integer
x | Integer
x Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
lim   = []
                             | Bool
otherwise = Integer
x Integer -> [Integer] -> [Integer]
forall a. a -> [a] -> [a]
: Integer -> [Integer]
go (Integer
xInteger -> Integer -> Integer
forall a. Num a => a -> a -> a
+Integer
delta)

------------------------------------------------------------------------
-- Natural
------------------------------------------------------------------------

-- | @since 4.8.0.0
instance Enum Natural where
    succ :: Natural -> Natural
succ Natural
n = Natural
n Natural -> Natural -> Natural
forall a. Num a => a -> a -> a
+ Natural
1
    pred :: Natural -> Natural
pred Natural
n = Natural
n Natural -> Natural -> Natural
forall a. Num a => a -> a -> a
- Natural
1
    toEnum :: Int -> Natural
toEnum i :: Int
i@(I# Int#
i#)
      | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0    = Word# -> Natural
naturalFromWord# (Int# -> Word#
int2Word# Int#
i#)
      | Bool
otherwise = String -> Natural
forall a. String -> a
errorWithoutStackTrace String
"toEnum: unexpected negative Int"

    fromEnum :: Natural -> Int
fromEnum (NS Word#
w)
      | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0    = Int
i
      | Bool
otherwise = String -> Int
forall a. String -> a
errorWithoutStackTrace String
"fromEnum: out of Int range"
      where
        i :: Int
i = Int# -> Int
I# (Word# -> Int#
word2Int# Word#
w)
    fromEnum Natural
n = Integer -> Int
forall a. Enum a => a -> Int
fromEnum (Natural -> Integer
integerFromNatural Natural
n)

    enumFrom :: Natural -> [Natural]
enumFrom Natural
x        = Natural -> Natural -> [Natural]
enumDeltaNatural      Natural
x Natural
1
    enumFromThen :: Natural -> Natural -> [Natural]
enumFromThen Natural
x Natural
y
      | Natural
x Natural -> Natural -> Bool
forall a. Ord a => a -> a -> Bool
<= Natural
y        = Natural -> Natural -> [Natural]
enumDeltaNatural      Natural
x (Natural
yNatural -> Natural -> Natural
forall a. Num a => a -> a -> a
-Natural
x)
      | Bool
otherwise     = Natural -> Natural -> Natural -> [Natural]
enumNegDeltaToNatural Natural
x (Natural
xNatural -> Natural -> Natural
forall a. Num a => a -> a -> a
-Natural
y) Natural
0

    enumFromTo :: Natural -> Natural -> [Natural]
enumFromTo Natural
x Natural
lim  = Natural -> Natural -> Natural -> [Natural]
enumDeltaToNatural    Natural
x Natural
1 Natural
lim
    enumFromThenTo :: Natural -> Natural -> Natural -> [Natural]
enumFromThenTo Natural
x Natural
y Natural
lim
      | Natural
x Natural -> Natural -> Bool
forall a. Ord a => a -> a -> Bool
<= Natural
y        = Natural -> Natural -> Natural -> [Natural]
enumDeltaToNatural    Natural
x (Natural
yNatural -> Natural -> Natural
forall a. Num a => a -> a -> a
-Natural
x) Natural
lim
      | Bool
otherwise     = Natural -> Natural -> Natural -> [Natural]
enumNegDeltaToNatural Natural
x (Natural
xNatural -> Natural -> Natural
forall a. Num a => a -> a -> a
-Natural
y) Natural
lim

-- Helpers for 'Enum Natural'; TODO: optimise & make fusion work

enumDeltaNatural :: Natural -> Natural -> [Natural]
enumDeltaNatural :: Natural -> Natural -> [Natural]
enumDeltaNatural !Natural
x Natural
d = Natural
x Natural -> [Natural] -> [Natural]
forall a. a -> [a] -> [a]
: Natural -> Natural -> [Natural]
enumDeltaNatural (Natural
xNatural -> Natural -> Natural
forall a. Num a => a -> a -> a
+Natural
d) Natural
d

enumDeltaToNatural :: Natural -> Natural -> Natural -> [Natural]
enumDeltaToNatural :: Natural -> Natural -> Natural -> [Natural]
enumDeltaToNatural Natural
x0 Natural
delta Natural
lim = Natural -> [Natural]
go Natural
x0
  where
    go :: Natural -> [Natural]
go Natural
x | Natural
x Natural -> Natural -> Bool
forall a. Ord a => a -> a -> Bool
> Natural
lim   = []
         | Bool
otherwise = Natural
x Natural -> [Natural] -> [Natural]
forall a. a -> [a] -> [a]
: Natural -> [Natural]
go (Natural
xNatural -> Natural -> Natural
forall a. Num a => a -> a -> a
+Natural
delta)

enumNegDeltaToNatural :: Natural -> Natural -> Natural -> [Natural]
enumNegDeltaToNatural :: Natural -> Natural -> Natural -> [Natural]
enumNegDeltaToNatural Natural
x0 Natural
ndelta Natural
lim = Natural -> [Natural]
go Natural
x0
  where
    go :: Natural -> [Natural]
go Natural
x | Natural
x Natural -> Natural -> Bool
forall a. Ord a => a -> a -> Bool
< Natural
lim     = []
         | Natural
x Natural -> Natural -> Bool
forall a. Ord a => a -> a -> Bool
>= Natural
ndelta = Natural
x Natural -> [Natural] -> [Natural]
forall a. a -> [a] -> [a]
: Natural -> [Natural]
go (Natural
xNatural -> Natural -> Natural
forall a. Num a => a -> a -> a
-Natural
ndelta)
         | Bool
otherwise   = [Natural
x]


-- Instances from GHC.Types

-- | @since 4.16.0.0
deriving instance Bounded Levity
-- | @since 4.16.0.0
deriving instance Enum Levity

-- | @since 4.10.0.0
deriving instance Bounded VecCount
-- | @since 4.10.0.0
deriving instance Enum VecCount

-- | @since 4.10.0.0
deriving instance Bounded VecElem
-- | @since 4.10.0.0
deriving instance Enum VecElem