計算機科学のブログ

型の紹介 - 型クラス - Word型, Int型, Boundedクラス, maxBound, minBound

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

コード

sample.hs

main :: IO ()
main = do
    print "maxBound Int, Word"
    print (maxBound :: Int)
    print (maxBound :: Word)
    print "minBound Int, Word"
    print (minBound :: Int)
    print (minBound :: Word)

入出力結果(Terminal, Zsh)

% ghci
GHCi, version 9.4.8: 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 /.../.ghc/ghci.conf
Prelude
λ> :info Word
type Word :: *
data Word = GHC.Types.W# GHC.Prim.Word#
  	-- Defined in ‘GHC.Types’
instance Bounded Word -- Defined in ‘GHC.Enum’
instance Read Word -- Defined in ‘GHC.Read’
instance Enum Word -- Defined in ‘GHC.Enum’
instance Integral Word -- Defined in ‘GHC.Real’
instance Num Word -- Defined in ‘GHC.Num’
instance Real Word -- Defined in ‘GHC.Real’
instance Show Word -- Defined in ‘GHC.Show’
instance Eq Word -- Defined in ‘GHC.Classes’
instance Ord Word -- Defined in ‘GHC.Classes’
Prelude
λ> :info Inte

<interactive>:1:1: error: Not in scope: ‘Inte’
Prelude
λ> :info Int
type Int :: *
data Int = GHC.Types.I# GHC.Prim.Int#
  	-- Defined in ‘GHC.Types’
instance Read Int -- Defined in ‘GHC.Read’
instance Bounded Int -- Defined in ‘GHC.Enum’
instance Enum Int -- Defined in ‘GHC.Enum’
instance Integral Int -- Defined in ‘GHC.Real’
instance Num Int -- Defined in ‘GHC.Num’
instance Ord Int -- Defined in ‘GHC.Classes’
instance Real Int -- Defined in ‘GHC.Real’
instance Show Int -- Defined in ‘GHC.Show’
instance Eq Int -- Defined in ‘GHC.Classes’
Prelude
λ> :q
Leaving GHCi.
%
...
% runghc sample1.hs 
"maxBound Int, Word"
9223372036854775807
18446744073709551615
"minBound Int, Word"
-9223372036854775808
0
%