In group theory, the free group with \$n\$ generators can be obtained by taking \$n\$ distinct symbols (let's call them \$a, b, c ...\$ etc), along with their inverses \$ a^{-1},b^{-1},c^{-1} ...\$ . Then the free group is the set of all finite words formed by concatenating these, subject to \$aa^{-1}, bb^{-1}, cc^{-1} \$etc being compressible to an empty string - for example, \$acbb^{-1}ca^{-1} = acca^{-1}\$, and some members of this group are \$ab^{-1}, cacb^{-1}a^{-1},b\$ etc.
The subgroup of the free group generated by a word \$w\$, denoted as \$\langle w \rangle\$, is the set of all words that can be formed by concatenating \$w\$ and \$w^{-1}\$, where \$w^{-1}\$ is \$w\$ reversed and with each element inverted - for example, \$(cb^{-1}c^{-1}a)^{-1} = a^{-1}cbc^{-1}\$. For example, \$\langle ab^{-1} \rangle\ = \{e, ab^{-1}, ba^{-1}, ab^{-1}ab^{-1}, ba^{-1}ba^{-1}...\}\$ and so on, where \$e\$ is the empty string and is included in the set.
Then, the subgroup generated by a set of words \$\{w_1,w_2,w_3...\}\$ (denoted \$\langle w_1,w_2,w_3...\rangle\$ is the set of all concatenations of \$w_1,w_1^{-1},w_2,w_2^{-1},w_3,w_3^{-1}\$ etc. For example, \$\langle aa,ba\rangle\$ is the set of all concatenations of \$aa, a^{-1}a^{-1}, ba, a^{-1}b^{-1}\$, which is \$\{e, aa, a^{-1}a^{-1}, ba, a^{-1}b^{-1}, baa^{-1}a^{-1} = ba^{-1}, ab^{-1} ...\}\$ etc.
Your challenge is to, given a set of words \$\{w_1,w_2,w_3...\}\$, output the subgroup of the relevant free group generated by them. You may use any set of distinct values to represent the symbols and their inverses, as long as there are at least 5 distinct symbols that can be represented this way. For example, having \$a = 1, a^{-1} = -1, b = 2, b^{-1} = -2 ... e^{-1} = -5\$ and representing words as arrays of these would be fine, and so would taking \$a = \text{"a"}, a^{-1} = \text{"A"} ... e^{-1} = \text{"E"}\$ and using string I/O. You may additionally take a list of all symbols used in the input, with or without their inverses.
Standard sequence output rules apply - you may output all generated elements infinitely, or take an 0/1-indexed n
and output the n
th / first n
terms. The output can be in any order and may include duplicates, but every word that can be built from the input words should eventually appear at some finite index in the output. The one exception to this is the empty string (which occurs in the output for any input), which you may omit.
This is code-golf, shortest wins!
Testcases
These use the aAbBcCdDeE...
format from before. Each of these is a potential first 10 terms of the generated sequence, and includes the empty string (see before the ,
)
a -> , a, A, aa, AA, aaa, AAA, aaaa, AAAA, aaaaa
a, b -> , a, b, A, B, aa, ab, ba, bb, Ab
aa, ba -> , aa, ba, AA, AB, aB, bA, aaba, AAba, baaa
acc, CC ->, a, A, aa, AA, cc, CC, acc, CCA, aaa
abcbac, dCBA, CAB -> , d, D, dd, DD, ddd, DDD, bac, dbac, CAB
a
,b
,c
, etc; and because of that contains all possible concatenations of symbols and is a (non-strict) superset of any subgroup generated from words of the same set of symbols. Another way of defining subgroups of the free group is placing restrictions on them by declaring certain words equal to the empty string, but I don't really want to get into that for this challenge. \$\endgroup\$