The number of values for a given type is called the cardinality of that type, and that of type T is written as
Haskell and a few other languages have a certain set of enum types, each of which has a small finite number of values (the exact names vary, so this challenge uses some arbitrarily chosen names).
Name | Cardinality ------+------------- Never | 0 Unit | 1 Bool | 2 (true or false) Order | 3 (LT, EQ, or GT)
And they also have some derived types which have one or more type parameters. Their cardinality depends on which types they get as parameters (written as
U in the table below).
Func(T,U) represents the function commonly written as
T -> U, i.e. a function that takes a parameter of type T and returns a value of type U.
Name(Params) | Cardinality -------------+------------- Option(T) | |T| + 1 (some value from T, or absence) Either(T,U) | |T| + |U| (some value from T or some value from U) Pair(T,U) | |T| * |U| (any combination of values from T and U) Func(T,U) | |U| ** |T| (any combination of U for every value of T)
Note: A "function" here is to be understood as a mathematical concept rather than a programming one. A mathematical function
Func(T,U) maps every possible value of T to some value of U, disregarding the "how". For programmers, it is OK to think of it as functions of the form of (in Haskell-like pseudocode):
\(x :: T) -> case x of value1OfT -> someValue1OfU value2OfT -> someValue2OfU ... valueXOfT -> someValueXOfU
with all cases provided.
Option(Never) has cardinality 1, and
Func(Bool,Order) has cardinality
3**2 = 9.
Func(Never,Never) has cardinality 1;
0**0 is defined to be 1 in this system.
A type parameter can itself be a derived type, so
Pair(Func(Never,Never),Pair(Either(Bool,Bool),Option(Order))) is also a valid type, which has cardinality of
(0**0) * ((2+2) * (3+1)) = 16.
For this challenge, assume that no types other than the 8 presented above are available.
Given a string that represents a valid type in this system, output its cardinality. You can assume the input does not contain spaces.
Standard code-golf rules apply. The shortest code in bytes wins.
Never -> 0 Unit -> 1 Bool -> 2 Order -> 3 Func(Never,Never) -> 1 Func(Unit,Never) -> 0 Option(Unit) -> 2 Option(Order) -> 4 Either(Bool,Bool) -> 4 Either(Bool,Order) -> 5 Pair(Bool,Order) -> 6 Pair(Func(Never,Never),Pair(Either(Bool,Bool),Option(Order))) -> 16 Func(Func(Order,Order),Order) -> 7625597484987