Reputation: 28490
Since this is (at least it seems to me) tightly related to programming, I'm asking here rather than on math or cs, but if you it think it best fits there or in another side, please just give your opinion.
At the end of Chapter 2 of Bartosz Milewski's Category Theory for Programmers, there's this question:
How many different functions are there from
Bool
toBool
? Can you implement them all?
This is my reasoning:
Bool
has only two elements in it, True
and False
;Int
s as arg1 + arg2
and arg2 + arg1
respectively, would be the same function from Int
to Int
);Bool
s to another of the two Bool
s:
T
to T
T
to F
F
to T
F
to F
Is my reasoning correct?
Upvotes: 3
Views: 447
Reputation: 227
The fact that you asked on Programming, rather than math or CS, is important.
On Math, they'd tell you there are four such functions, listed by the other answers.
On CS, they'd tell you there are 27: one for each of three possible inputs T F and ⊥ to each of three possible outputs T F and ⊥.
Here in programming, I can tell you there are eleven:
(This answer is quite tongue-in-cheek: I think in reality most scholarly CS types would say either 4 or 11.)
Upvotes: 3
Reputation: 665122
the different functions are those going from one of the two
Bool
s to another of the twoBool
s
No. A function does map every value from its domain to one value from its codomain. You need to consider all possible combinations of mappings. For this, it might make sense to look at the function as a relation, and list them all:
f -> f
, t -> f
f -> f
, t -> t
f -> t
, t -> f
f -> t
, t -> t
These correspond to the 4 functions
x => f
(constant false)x => x
(identity)x => not(x)
(negation)x => t
(constant true)Upvotes: 4
Reputation: 76905
There are four functions:
false -> false
true -> false
false -> false
true -> true
false -> true
true -> false
false -> true
true -> true
Your reasoning is mostly correct. The functions are blackboxes, we view them as values. Since the input is a boolean and has two possible values and the function might have two separate values, basically the number if 2^2 = 4
Upvotes: 2