Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 567
TorosFanny Avatar asked Jan 28 '26 04:01

TorosFanny


1 Answers

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.

like image 51
Dietrich Epp Avatar answered Jan 30 '26 00:01

Dietrich Epp