TITLE: Constructively, is the unit of the “free abelian group” monad on sets injective?
QUESTION [19 upvotes]: Classically, we can explicitly construct the free Abelian group $\newcommand{\Z}{\mathbb{Z}}\Z[X]$ on a set $X$ as the set of finitely-supported functions $X \to \Z$, and so easily see that the unit map $X \to \Z[X]$ (inclusion of generators) is injective.
Constructively, this construction of $\Z[X]$ doesn’t work for arbitrary $X$. The unit map $X \to \Z[X]$ should send $x \in X$ to the function $1_x : X \to \Z$, sending $y \in X$ to $1$ if $y = x$ and $0$ otherwise; but this definition-by-cases requires that $X$ has decidable equality (i.e. for every $x,y \in X$, either $y = x$ or $y \neq x$).
Similarly, most other classical explicit constructions of $\Z[X]$ rely at some point on decidable equality of $X$.  The abstract-nonsense syntactic construction of $\Z[X]$ as a free model of an algebraic theory works fine constructively; but from this construction, it’s not clear that the unit map $X \to \Z[X]$ is injective.
Is there some constructive proof that the unit map $X \to \Z[X]$ is injective for arbitrary $X$, or can it fail?
[Note this is a self-answered question. It came up since the fact was required here; asking several experts, the answer seems to be not well-known. After a couple of days, I worked out an answer via an explicit construction, and then also got a reply pointing to a slightly less explicit answer in the literature; so I’m writing it up here to make the answer and construction more prominently available.]

REPLY [2 votes]: Here's yet another construction of the free abelian group on an arbitrary set, and a proof of the claim. I haven't seen the proof in Mines, Richman, Ruitenburg so I don't know how similar this is to that.
We define the free abelian group on a set $A$ to be a quotient $X/\sim$. The set $X$ is the set of all triplets $(n, f, x)$ where $n$ is a natural number, $f : [n] \to A$, where $[n] = \{0, 1, \dots, n-1\}$, and $x \in \mathbb {Z}^n$. The equivalence $(n, f, x) \sim (m, g, y)$ holds if there is some $k \in \mathbb {N}$, $r : [k] \to A$ and maps $i : [n] \to [k]$, $j : [m] \to [k]$ such that $f = r \circ i$, $g = r \circ j$, $i^* (x) = j^* (y)$, where $i^* : \mathbb {Z}^n \to \mathbb {Z}^k$ is the linear map that takes the basis element $e^{(n)}_t \in \mathbb {Z}^n$ to $e^{(k)}_{i (t)} \in \mathbb {Z}^k$, and similarly for $j^*$. This relation is transitive since if also $(m, g, y) \sim (p, h, z)$ via $[m] \xrightarrow {i'} [\ell] \xleftarrow {j'} [p]$, then the pushout of $(j, i')$ is some finite set $[u]$ with maps $[k] \xrightarrow {i''} [u] \xleftarrow {j''} [\ell]$, and then $i'' \circ i : [n] \to [u]$, $j'' \circ j' : [p] \to [u]$ exhibit an equivalence $(n, f, x) \sim (p, h, z)$. Note the crucial use of the existence of pushouts in the category of finite sets. Sums are defined by $(n, f, x) + (m, g, y) = (n+m, f \oplus g, (x,y))$, where $f \oplus g : [n+m] \simeq [n] \sqcup [m] \to A$ is the concatenation of $f$ and $g$, and similarly $(x, y) \in \mathbb {Z}^{n+m}$ is concatenation. I won't bother verifying the abelian group laws.
More abstractly, the free abelian group generated by $A$ as a set is the colimit over the category of finite multisets in $A$ of the free abelian groups generated by them, where a finite multiset in $A$ is a map $[n] \to A$. This suggests a generalization to arbitrary Lawvere theories.
To prove the claim that $A \to \mathbb {Z} [A]$ is injective, note that the generator corresponding to $a \in A$ is given by $t (a) = (1, (0 \mapsto a), (1)) \in X$. If $t (a) \sim t (b)$ via the maps $[1] \xrightarrow {i} [k] \xleftarrow {j} [1]$ and $h : [k] \to A$, then $i^* (1) = j^* (1) \in \mathbb {Z}^k$ implies that $i (0) = j (0)$, and hence $a = h (i (0)) = h (j (0)) = b$.