In some Haskell code I came across:
put :: s -> m ()
What does the ()
mean here?
I'd use a search engine, but I can't find one that handles ()
correctly.
Haskell uses parentheses only in the way they are used in high school mathematics: for grouping sub-expressions so that you get a different call structure. For example, in maths 1 + 2 * 3 would call * on 2 and 3, and call + on 1 and the result of the * call.
(->) is often called the "function arrow" or "function type constructor", and while it does have some special syntax, there's not that much special about it. It's essentially an infix type operator. Give it two types, and it gives you the type of functions between those types.
Using @t as a type indicator. Besides the argument pattern matching usage described in the answer of @Sibi, in Haskell the "at" character ('@', also known as an arobase character) can be used in some contexts to force a typing decision. This is mentioned in the comments by @Josh. F.
They do exactly the same thing. >> is specialized to Monad while *> to Applicative . Having both is a historical quirk. While semantically every monad is also an applicative, this was historically not guaranteed in the Haskell standard library, in large part because the Applicative class was added later than Monad .
()
means "Boring". It means the boring type which contains one thing, also boring. There is nothing interesting to be gained by comparing one element of the boring type with another, because there is nothing to learn about an element of the boring type by giving it any of your attention.
It is very different from the empty type, called (by people I wish had chosen a better name like the one I suggested) in Haskell Void
. The empty type is very exciting, because if somebody ever gives you a value belonging to it, you know that you are already dead and in Heaven and that anything you want is yours.
But if somebody gives you a value in ()
, don't get excited. Just throw it away.
Sometimes it's fun to take type constructors parametrised by an "element type" and fill the parameter in with ()
. You can see just what information is inherent in the type constructor, rather than coming from the elements. E.g, Maybe ()
is a version of Bool
, with Just ()
or Nothing
. Also, [()]
amounts to the (possibly infinite) natural numbers: the only information you have is a length.
So, ()
means "Boring", but it's often a clue that something interesting is happening somewhere else.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With