Reputation: 1535
Nothing
's type could be Maybe Int
and why couldn't it be compared with another Maybe Int
?
And why Nothing< Just 4
pass?
Upvotes: 0
Views: 119
Reputation: 116174
Yes, Nothing
can be of type Maybe Int
(or Maybe String
, or Maybe AnythingElse
).
Any two values of type Maybe Int
can be compared.
Equality ==
is defined as one might expect: Nothing
is only equal to Nothing
, and Just x
is only equal to Just y
if x==y
. This is defined in the Eq
instance for Maybe a
, which is automatically imported since it is in the Haskell Prelude
.
Similarly, there is an Ord (Maybe a)
instance as well in the Prelude
. This instance defines <
between Maybe Int
s so that Nothing
is the minimum element (<
anything else), while two values Just x
and Just y
are compared according to whether x < y
.
So, Nothing < Just 4
is true.
The code Nothing<(4::Maybe Int)
gives you an error since 4
is an Int
(technically, it is any type in the Num
type class), but 4
is not a Maybe Int
. If you write 4 :: String
or 4 :: [Int]
or 4 :: Int -> Int
you will get a similar type error.
The Maybe Int
type contains only the values
Nothing
Just 0
Just 1
Just -1
Just 2
Just -2
...
(and some bottoms, which I omit.)
The value 4
is not a value of type Maybe Int
. Just 4
is instead one of such values.
Upvotes: 5
Reputation: 137987
There's a simple type error in your expression: 4
does not have type Maybe Int
.
Upvotes: 7