TorosFanny
TorosFanny

Reputation: 1732

Why is the () in Haskell a Enum type but haven't implemented the succ function

I find

Prelude> :i ()
data () = ()    -- Defined in `GHC.Tuple'
instance Bounded () -- Defined in `GHC.Enum'
instance Enum () -- Defined in `GHC.Enum'
instance Eq () -- Defined in `GHC.Classes'
instance Ord () -- Defined in `GHC.Classes'
instance Read () -- Defined in `GHC.Read'
instance Show () -- Defined in `GHC.Show'

So,that mean () is an instance of Enum, and should have implemented the succ function. However, when I tried succ (),I got *** Exception: Prelude.Enum.().succ: bad argument

I searched the source code of GHC.Tuple where the type of () should be defined but GHC.Tuple

Upvotes: 2

Views: 962

Answers (1)

Dietrich Epp
Dietrich Epp

Reputation: 213388

The succ function is only defined for arguments which have a successor.

Prelude> succ False
True
Prelude> succ True
*** Exception: Prelude.Enum.Bool.succ: bad argument

Prelude> succ 0
1
Prelude> succ 1
2
Prelude> succ ((2^63 - 1) :: Int)
*** Exception: Prelude.Enum.succ{Int}: tried to take `succ' of maxBound

Prelude> succ ()
*** Exception: Prelude.Enum.().succ: bad argument

So the answer is: the function is implemented, it just (correctly) returns an error, always.

Upvotes: 11

Related Questions