Background
Mathematicians are interested in all kinds of algebraic structures, e.g. groups, rings, boolean algebras. Generally, we have several operations (addition, multiplication, meet, join, etc.), and some equational relationships on the operations (e.g. associativity of multiplication). This makes up an algebraic theory. Formally, we want \$k\$ functions, the \$i\$-th of which takes \$s_i\$ arguments. We also want some equalities to hold between the functions. In our case, we only consider equalities between expressions of depth at most 2, e.g. \$f(g(x), h(x, y), h(y, z), f(x, y, w, z))\$. The equalities should hold whatever values you plug in for the variables.
Let's take a quick example. A semigroup is such an algebraic structure. We need to have one function \$f_1\$, with \$s_1=2\$. There is just one equational relationship: \$f_1(f_1(x, y), z) = f_1(x, f_1(y, z))\$.
We are interested in models of the algebraic theories. A model, simply put, is just an implementation of the functions. We choose an underlying set \$M\$, and we implement the required functions \$f_i\$ as taking \$s_i\$ arguments from the set \$M\$, and returning an element of \$M\$, so that the equations hold when you plug in any element of \$M\$ for the variables. For example, if we take \$M = \{0,1,2\}\$, and \$f_1\$ to be the addition modulo 3, we get a model for the semigroup. The order of a model is simply the size of \$M\$. So the model above has order 3.
Suppose that, for a fixed algebraic theory, there are two models, with underlying set \$M\$ and \$N\$, and functions \$f_i\$ and \$g_i\$, respectively. If, after some relabeling of the elements, two models become exactly the same, we say they are isomorphic. In that case, the two models are essentially the same, and usually we don't care about their difference.
More formally, if we can find a mapping \$\sigma: M \to N\$ such that \$g_i(\sigma(m_1), \sigma(m_2), \dots, \sigma(m_{s_i})) = \sigma(f_i(m_1, \dots, m_{s_i}))\$, we say that \$\sigma\$ is a morphism. Furthermore, if there is another morphism \$\pi : N \to M\$, and \$\sigma(\pi(x)) = x\$, \$\pi(\sigma(y)) = y\$ holds for every \$x \in N, y\in M\$, we say that the two models are isomorphic.
Task
Your challenge is a very natural question that arises when studying algebraic theories.
Given an algebraic theory (whose equations involve only expressions of depth at most 2) and a natural number \$n\$, compute the number of distinct models of order \$n\$, up to isomorphism.
You may write a function or a complete program. You can suppose the input is reasonably parsed for you. See the examples. Standard loopholes apply. This is code-golf, so the program with the shortest byte length wins.
Example Cases
Input: The theory of groupoids. There is one function f taking 2 arguments.
f(f(x, y), z) = f(x, f(y, z))
n = 2.
Output: 5.
We'll fully work this out. For the rest of the example cases the basic idea is the same.
Let's take M = {0, 1}
, the actual labelling of the elements obviously doesn't affect the outcome. We have four values to decide on, f(0, 0)
, f(0, 1)
, f(1, 0)
, f(1, 1)
. Let's case split on f(0, 1)
.
Case 1:
f(0, 1) = 0
. So0 = f(0, 1) = f(f(0, 1), 1) = f(0, f(1, 1))
. We further case split onf(1, 1)
.- Case A:
f(1, 1) = 0
. Then0 = f(0, f(1, 1)) = f(0, 0)
. If further,f(1, 0) = 0
, thenf
is a constant function, and obviously satisfies the equation. Iff(1, 0) = 1
, then1 = f(1, 0) = f(1, f(1, 1)) = f(f(1, 1), 1) = f(0, 1) = 0
, contradiction. - Case B:
f(1, 1) = 1
. Let's considerf(0, 0)
.- Case i:
f(0, 0) = 0
. Thenf(1, 0) = f(f(1, 1), 0) = f(1, f(1, 0))
. Iff(1, 0) = 1
, plugging that in yields1 = f(1, 1) = 0
, contradiction. SOf(1, 0) = 0
. Sof(x, y) = x and y
, which satisfies the equation, as you learned early in discrete mathematics. - Case ii:
f(0, 0) = 1
. Thenf(1, 0) = f(f(0, 0), 0) = f(0, f(0, 0)) = f(0, 1) = 0
. So the whole thing is just thexnor
function.
- Case i:
- Case A:
Now let's look back: we've already got the constant function 0
, the boolean and
, and xnor
. We proceed:
- Case 2:
f(0, 1) = 1
. We could do the case analysis all over again. But note that everything exactly mirrors case 1, except that0
and1
are inverted, and the two arguments off
is swapped. Since swapping the arguments doesn't affect associativity, we immediately get what we wanted: the constant function1
, the booleannand
andxor
.
Now we need to make sure they are all non-isomorphic. In fact, the two constant functions are isomorphic, since the relabelling that swaps 0
and 1
converts between them. So we've got 5 different possibilities.
Input: There are three functions f, g, h taking 1, 2 and 0 arguments, respectively.
The equations are:
g(g(x, y), z) = g(x, g(y, z))
g(x, f(x)) = h()
g(f(x), x) = h()
g(h(), x) = x
g(x, h()) = x
n = 3
Output: 1.
You may assume that the functions are labeled with natural numbers, here we use letters to make it more readable. Note that the expressions on both sides of the equalities should be implemented as trees or nested lists. You can assume that such trees are passed to your function as argument, so you can skip the parsing part. But in case you want to fiddle with eval
-like stuff you can also accept string input.
The model of this algebraic theory is exactly the groups. And all groups of order 3 are isomorphic to \$\mathbb Z_3\$, which takes the underlying set to be \$\{0,1,2\}\$, and g
is the addition modulo 3, f
is negation, h()
equals 0. Then the equations translate to:
- \$(x + y) + z = x + (y + z)\$;
- \$x + (-x) = 0\$;
- \$(-x) + x = 0\$;
- \$0 + x = x\$;
- \$x + 0 = x\$.
These are just the familiar properties of addition.
Input: Same as previous, except `n = 6`.
Output: 2.
In fact, for this algebraic theory, the answer should be the very first sequence of OEIS, which demonstrates the importance of such sequences.
Input: There are 4 functions f, g, h, i, that takes 2, 2, 0, 1 arguments, repectively. The equations are:
f(f(x, y), z) = f(x, f(y, z))
g(g(x, y), z) = g(x, g(y, z))
f(x, y) = f(y, x)
f(i(x), x) = h()
f(h(), x) = x
g(x, f(y, z)) = f(g(x, y), g(x, z))
g(f(y, z), x) = f(g(y, x), g(z, x))
n = 10.
Output: 4.
This is OEIS A027623.
Input: There is just 1 function f taking 1 argument. The equations are:
f(x) = f(x)
f(f(y)) = f(f(y))
n = 30
Output: 10712682919341.
Note that the equations are redundant.
f
is simply any function. We draw a directed edge froma
tob
iff(a) = b
, this produces a graph where every vertex's out-degree is 1. Since we consider things up to isomorphism, we need to count such graphs with unlabeled nodes. We first consider a connected component. Since the out-degree is always 1, you can uniquely follow the edges until you hit a previously visited vertex. This produces a cycle (possibly with length 1 or 2). We consider the rest of the vertices in this connected component. They have to be on trees rooted on the cycle. Counting such graphs should be standard practice. The number of connected component is A002861, and the total number is A001372.
Input: 2 functions f, g both taking 2 arguments.
f(x, y) = f(y, x)
g(x, y) = g(y, x)
f(f(x, y), z) = f(x, f(y, z))
g(g(x, y), z) = g(x, g(y, z))
f(x, g(x, y)) = x
g(x, f(x, y)) = x
Output: OEIS A006966.
It is clear that this challenge is a very general generalization of various counting problems.
Below are some degenerate corner cases.
Input: There are no functions. There are no equalities. n = 7.
Output: 1.
This is just finding sets with 7 elements, and if there are bijections between two sets they count as the same. Of course, there are bijections between every two sets with 7 elements. So the answer is 1.
Input: There are no functions. The only equality is x = y. n = 7.
Output: 0.
The equalities should hold for all x
and y
. So there can only be at most 1 distinct element, but we require 7.
Input: Same as above, except `n = 1`.
Output: 1.
Input: Any algebraic theory, n = 0.
Output: 1. (Regardless of the theory.)
and
andnand
are not isomorphic: the relabelling swaps both inputs and outputs. The isomorphic function toand
isor
; the isomorphic function tonand
isnor
(and(1,1)=1
←→or(0,0)=0
) \$\endgroup\$