Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

27 different Bool to Bool values in Haskell

Tags:

logic

haskell

Could someone explain why there is 27 different Bool->Bool values, from which 11 can be definied in Haskell?

like image 385
Machiaweliczny Avatar asked Sep 10 '13 10:09

Machiaweliczny


People also ask

How many different functions are there from Bool to Bool?

There are 27 functions from a three-value set to a three-value set.

How do you check for equality in Haskell?

We can also compare two numerical values to see which one is larger. Haskell provides a number of tests including: < (less than), > (greater than), <= (less than or equal to) and >= (greater than or equal to). These tests work comparably to == (equal to).

What is a Boolean Haskell?

The boolean type Bool is an enumeration. The basic boolean functions are (&&) (and), (||) (or), and not. The name is defined as True to make guarded expressions more readable. Definition: data Bool = False | True deriving (Read, Show, Eq, Ord, Enum, Bounded)


1 Answers

There are three values of type Bool: True, False and bottom (expressions for which the evaluation doesn't finish or expressions for which the evaluation turns into errors).

Then, there are an exponential number of functions from A to B. More exactly |B| ^ |A|.

Thus, there are 3^3 = 27 functions of type Bool -> Bool.

Now, for the second part of the question: function starting from bottom can be only 2: the one constantly returning True and the one constantly returning False. Then you have to add the number of functions from {True, False} to {True, False, bottom} which is 3^2. So, in total you'll have 9+2=11 functions.

Edit: Here are the 11 possible functions:

11 functions

B is bottom, T is True, F is False. The last row represents the const True and the const False functions while the first three rows represent functions testing the value of the argument. That is why the first three rows map B to B: testing the value of bottom cannot result in anything else but bottom.

I hope it is clearer now.

like image 93
Mihai Maruseac Avatar answered Sep 30 '22 13:09

Mihai Maruseac