How do display the number of elements in a type

I have seen in the documentation |Bool|=2
indicating that this type has two elements, which of course is true. However, when I go to the interpreter and type

|Bool|

I get a parse error on |
Is there a way to display the number of elements in a type?

You can use

:info Bool

to get information about a data type. There, you can also see that Bool belongs to the Bounded and Enum type classes. Hence, you can get all elements of the data type using

[minBound ..] :: [ Bool]

Then, you can get the cardinality using length ([minBound ..] :: [Bool]). There may be easier ways to get the cardinality of a bounded, enumerated data type.

In any case, and judging from your other questions, I advice you to read an introductiory book about Haskell. See here: Documentation.

I think it is important to mention that |Bool| is not real Haskell syntax. It is just some borrowed notation from mathematics where |X| stands for the size of a set (among other things).

3 Likes

It’s unfortunate that the documentation you read didn’t explain fully that this is the Cardinality notation. Could you point us to the part that talks about it?