Function cardinality
Given two sets (types) a
and b
, the number of functions from a
to b
is |b|
to the power of |a|
.
If φ ~ (a -> b)
, n
= |a|
and m
= |b|
, then |φ|
is mⁿ
.
For example
if
f :: (Bool -> Ordering)
, then|f|
is 3² = 9if
g :: (Ordering -> Bool)
, then|g|
is 2³ = 8
Extended example
Intuitively, for every output value, y
, of type b
, we can set each input value, x
, of type a
.
A function g :: Ordering -> Bool
the cardinality of the input type is 3,
Ordering = {LT,EQ, GT}
the cardinality of the output type is 2,
Bool = {True, False}
the total number of such functions: 2³ = 8
The card. of output type, Bool, is 2, which means there will be 2 constant functions that ignore the input: one that always returns True and another that always returns False. Plus 6 more functions whose output depends on the input. In this case, of Bool output type, it is easy to identify them, but this case can serve as the guidance for any function type.
We know there will be 8 functions total. We know that each function will consist of 3 equations because the cardinality of the input type is 3 (each equation does a pattern match against the input type).
So all functions will have the same LHS:
The return type is Bool so the returned value is either True or False. There is no obvious way in which the input is associated with the output, so we can just enumerate all the possible ways we can have True and False as the LHS output.
We can start with F (False) and state that the returns of the first function are {F,F,F}, that is:
This completes function #0. The next function has to return {F,F,T}:
and so on. We need to iterate through all the permutations of 3 Boolean values, from {F,F,F} to {T,T,T}. If T is 1 and F is 0 then just enumerate the return values as increasing binary numbers, from 0 (F,F,F ≡ 000) to 8 (T,T,T ≡ 111).
fn #
7
6
5
4
3
2
1
0
g# LT =
T
T
T
T
F
F
F
F
g# EQ =
T
T
F
F
T
T
F
F
g# GT =
T
F
T
F
T
F
T
F
Last updated
Was this helpful?