Lara
Lara

Reputation: 3164

What does () mean in Haskell

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.

Upvotes: 14

Views: 7478

Answers (4)

dfeuer
dfeuer

Reputation: 48580

If not for the weird special syntax, it could be defined as

data () = ()

It's about the most boring type there is. The syntax is supposed to make you think of tuples: (a,b) is a pair, (a,b,c) is a triple, etc., and () is a 0-tuple. The only thing missing is a 1-tuple, which can't have that syntax because it would clash with the usual use of parentheses.

() is very often used as the result of something that has no interesting result. For example, an IO action that is supposed to perform some I/O and terminate without producing a result will typically have type IO (). It can also be used when you need some uninteresting input; GHC has special syntax for this, but in Haskell 98 the way to mimic Lisp's cond is like this:

case () of
  () | c1 -> e1
     | c2 -> e2
     ...
     | otherwise -> e3

It's perfectly valid, but also perfectly boring, to ask what value of type () you have; there's only one legitimate one that you could have.

From the "Haskell-as-almost-category" standpoint, () is a final object. That is, for any type X, there is exactly one legitimate function of type X -> (), namely const (). From the other direction, the Void type pigworker mentions is an initial object. For any type X, there is exactly one legitimate function of type Void -> X, namely absurd. If you're in the mood for categorical reasoning, initial and final objects can be useful to have around.

Upvotes: 22

pigworker
pigworker

Reputation: 43373

() 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.

Upvotes: 40

leftaroundabout
leftaroundabout

Reputation: 120711

There are excellent search engines specialised for Haskell.

http://hayoo.fh-wedel.de/?query=() will give you immediately the correct answer:

Data ()
ghc-prim -GHC.Tuple


The unit datatype () has one non-undefined member, the nullary constructor ().

Upvotes: 6

mipadi
mipadi

Reputation: 410542

It's called the "unit type". You can think of it as Haskell's equivalent of void, in some respects. It's a type that has only one value (also ()).

Upvotes: 6

Related Questions