計算機科学のブログ

型の紹介 型クラス Enum、Boundedインスタンス、succ関数

入門Haskellプログラミング (Will Kurt(著)、株式会社クイープ(監修、翻訳)、翔泳社)のUNIT2(型の紹介)、LESSON 13(型クラス)、13.8(練習問題)Q13-2の解答を求めてみる。

maxBoundでの計算結果が違う。

実際に確認。

入出力結果(Terminal, Zsh)

% ghci
GHCi, version 8.10.7: https://www.haskell.org/ghc/  :? for help
macro 'doc' overwrites builtin command.  Use ':def!' to overwrite.
(0.00 secs, 0 bytes)
(0.00 secs, 0 bytes)
Loaded GHCi configuration from /Users/…/.ghc/ghci.conf
Prelude
λ> :info Enum
type Enum :: * -> Constraint
class Enum a where
  succ :: a -> a
  pred :: a -> a
  toEnum :: Int -> a
  fromEnum :: a -> Int
  enumFrom :: a -> [a]
  enumFromThen :: a -> a -> [a]
  enumFromTo :: a -> a -> [a]
  enumFromThenTo :: a -> a -> a -> [a]
  {-# MINIMAL toEnum, fromEnum #-}
  	-- Defined in ‘GHC.Enum’
instance Enum Word -- Defined in ‘GHC.Enum’
instance Enum Ordering -- Defined in ‘GHC.Enum’
instance Enum Integer -- Defined in ‘GHC.Enum’
instance Enum Int -- Defined in ‘GHC.Enum’
instance Enum Char -- Defined in ‘GHC.Enum’
instance Enum Bool -- Defined in ‘GHC.Enum’
instance Enum () -- Defined in ‘GHC.Enum’
instance Enum Float -- Defined in ‘GHC.Float’
instance Enum Double -- Defined in ‘GHC.Float’
Prelude
λ> :info Bounded
type Bounded :: * -> Constraint
class Bounded a where
  minBound :: a
  maxBound :: a
  {-# MINIMAL minBound, maxBound #-}
  	-- Defined in ‘GHC.Enum’
instance Bounded Word -- Defined in ‘GHC.Enum’
instance Bounded Ordering -- Defined in ‘GHC.Enum’
instance Bounded Int -- Defined in ‘GHC.Enum’
instance Bounded Char -- Defined in ‘GHC.Enum’
instance Bounded Bool -- Defined in ‘GHC.Enum’
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)
  -- Defined in ‘GHC.Enum’
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)
  -- Defined in ‘GHC.Enum’
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)
  -- Defined in ‘GHC.Enum’
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)
  -- Defined in ‘GHC.Enum’
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)
  -- Defined in ‘GHC.Enum’
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)
  -- Defined in ‘GHC.Enum’
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)
  -- Defined in ‘GHC.Enum’
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)
  -- Defined in ‘GHC.Enum’
instance (Bounded a, Bounded b, Bounded c, Bounded d, Bounded e,
          Bounded f, Bounded g) =>
         Bounded (a, b, c, d, e, f, g)
  -- Defined in ‘GHC.Enum’
instance (Bounded a, Bounded b, Bounded c, Bounded d, Bounded e,
          Bounded f) =>
         Bounded (a, b, c, d, e, f)
  -- Defined in ‘GHC.Enum’
instance (Bounded a, Bounded b, Bounded c, Bounded d, Bounded e) =>
         Bounded (a, b, c, d, e)
  -- Defined in ‘GHC.Enum’
instance (Bounded a, Bounded b, Bounded c, Bounded d) =>
         Bounded (a, b, c, d)
  -- Defined in ‘GHC.Enum’
instance (Bounded a, Bounded b, Bounded c) => Bounded (a, b, c)
  -- Defined in ‘GHC.Enum’
instance (Bounded a, Bounded b) => Bounded (a, b)
  -- Defined in ‘GHC.Enum’
instance Bounded () -- Defined in ‘GHC.Enum’
Prelude
λ> :info Int
type Int :: *
data Int = GHC.Types.I# GHC.Prim.Int#
  	-- Defined in ‘GHC.Types’
instance Eq Int -- Defined in ‘GHC.Classes’
instance Ord Int -- Defined in ‘GHC.Classes’
instance Enum Int -- Defined in ‘GHC.Enum’
instance Num Int -- Defined in ‘GHC.Num’
instance Real Int -- Defined in ‘GHC.Real’
instance Show Int -- Defined in ‘GHC.Show’
instance Read Int -- Defined in ‘GHC.Read’
instance Bounded Int -- Defined in ‘GHC.Enum’
instance Integral Int -- Defined in ‘GHC.Real’
Prelude
λ> n = maxBound :: Int
n :: Int
(0.00 secs, 65,176 bytes)
Prelude
λ> n + 1
-9223372036854775808
it :: Int
(0.01 secs, 77,712 bytes)
Prelude
λ> succ n
*** Exception: Prelude.Enum.succ{Int}: tried to take `succ' of maxBound
Prelude
λ> :quit
Leaving GHCi.
%