I have recently started diving into Haskell. Its quite interesting, but the definition of Nothing baffles me and is not sinking in. On GHCI
Prelude> :t Nothing
Nothing :: Maybe a
Shouldn’t Nothing be Just Nothing, why Maybe a?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Here is the definition of the
Maybetype:This states that for some type
a, the typeMaybe ahas two constructors:NothingandJust a(abeing the same type variable thatMaybeis applied to on the LHS).Hopefully it’s clear how
Just "foo"has typeMaybe StringandJust 'c'has typeMaybe Char. But what type isNothing? SinceNothingdoesn’t contain any value, its constructor doesn’t use theatype variable, and therefore there’s nothing that constrains its type to a particular form ofMaybe.That makes it a “polymorphic constant”.
Nothingcan be used in a context in which anyMaybetype is expected, because it works equally well in all of them. That’s why GHCi reports its type as simplyMaybe a: theais a variable which could represent any type, and which type it refers to will be determined by inference based on the context in which theNothingvalue is used. But when you giveNothingby itself, there are no additional clues about its specific type, so you just see the variable.And by the way,
Just Nothing :: Maybe (Maybe a); it’s aMaybewrapped in anotherMaybe.