What is $\mathcal O(S\times \mathbb A^n_\mathbb Z)$? After many edits, here is my question:
Is it true for a scheme $S$ that $\mathcal O(S\times \mathbb A_\mathbb Z^n) = \mathcal O(S)\otimes_\mathbb Z \mathcal O(\mathbb A^n)$ or not?
Let $\mathbb A$ be the forgetful functor on the category of commutative rings. It is a ring object internal to the category of functors on $\text{cRing}$. For each functor $X:\text{cRing}\to \text{Set}$, let $\mathcal O(X) = \text{Hom}(X,\mathbb A)$ be the set of natural transformations form $X$ to $\mathbb A$. Each $\mathcal O(X)$ get a ring structure from the internal ring structure of $\mathbb A$.
Is it true that $$\mathcal O(S\times \mathbb A^n) = \mathcal O(S)\otimes_\mathbb Z \mathbb Z[x_1,...,x_n] = \mathcal O(S)[x_1,...,x_n]$$
as commutative rings?
I read lecture notes from Marc Nieper-Wißkirchen which claim this, but I am not able to compute it myself. I feel really stupid. There is an adjunction $\text{Hom}_{\text{Pr}(\text{cRing}^{op})}(X,yA) = \text{Hom}_{\text{cRing}}(A,\mathcal OX)$, but it only implies that $\mathcal O$ sends colimits to limits. I know that the equation is true for representable $S$. Do I need to write the functor $S$ as a colimit of representables?
Edit. It seems like the result is not true for a general functor. We can pass to the smaller subcategories of sheaves in the Zariski topology and of (functorial) schemes. Colimits in the big Zariski topos Zar of sheaves in the Grothendieck topology are computed differently than in the presheaf category (sheafification). The ring $\mathcal O(S\times \mathbb A^n)$ does not depend on the surrounding category though. Does the equation $\mathcal O(S\times \mathbb A^n) = \mathcal O(S)[x_1,...,x_n]$ at least hold if $S$ is a scheme?
Edit. Edit. Okay, now I am totally confused. Qiaochu Yuan claims below that $\mathcal O(S\times \mathbb A^n) \neq \mathcal O(S)[x_1,...,x_n]$ for general schemes, but I have the following argument.
According to the stacks project the projection map $\mathbb A^n\times S\to S$ is up to isomorphism $\underline{Spec}_S(\mathcal O_S[T_1,...,T_n])\to S$. The Spec thing here is the relative spec construction and it acts on a $\mathcal O_S$-algebra! Let me denote the structure map by $$p:\underline{Spec}_S(\mathcal O_S[T_1,...,T_n])\to S$$ Now the relative Spec construction satisfies in general that $p_*\mathcal O_{\underline{Spec}_S\mathcal A} = \mathcal A$ for any $\mathcal O_S$-algebra $\mathcal A$. So I can compute the global functions on the scheme over $S$ as follows.
\begin{align}\mathcal O(S\times \mathbb A^n) &= \mathcal O(\underline{Spec}_S(\mathcal O_S[T_1,...,T_n])) \\&= \mathcal O_{\underline{Spec}_S(\mathcal O_S[T_1,...,T_n])}(p^{-1}S)\\
&= p_*\underline{Spec}_S(\mathcal O_S[T_1,...,T_n])(S)
\\&= \mathcal O_S[T_1,...,T_n](S)\\
&= \mathcal O(S)[x_1,...,x_n]
\end{align}
Here $\mathcal O_S$ is a sheaf of algebras and $\mathcal O(S)$ is a ring! What is wrong with my calculation? The step where I am not sure about is what the global sections of the sheaf $\mathcal O_S[T_1,...,T_n]$ are. Is the counterexample incorrect? (I must admit that I do not understand it completely).
The original lecture notes can be found at the end of the article as a pdf, and the claim by M. Nieper-Wißkirchen is on page 58 equation (4.5.12)
Also here is an argument which I believe does work for schemes which can be covered by finitely many open affines. Write $S$ as a colimit $S = \text{colim}_iU_i$ of finitely many open affine subfunctors. Make the following computation
\begin{align}
\mathcal O(\mathbb A^n\times S) &= \mathcal O(\mathbb A^n \times (\operatorname{colim}_{i}U_i))\\
&= \mathcal O(\operatorname{colim}_i(\mathbb A^n\times U_i)) \\
&= \operatorname{lim}_i\mathcal O(\mathbb A^n\times U_i) \\
&= \operatorname{lim}_i(\mathcal O\mathbb A^n \otimes_\mathbb Z \mathcal OU_i) \\
&= \mathcal O\mathbb A^n \otimes_\mathbb Z \operatorname{lim}_i\mathcal OU_i \\
&= \mathcal O\mathbb A^n\otimes_\mathbb Z\mathcal OS
\end{align}
where I now used that $-\otimes_\mathbb Z\mathcal O\mathbb A^n$ is exact and commutes with finite limits.
 A: Edit #2: The counterexample below continues to be valid for schemes. We just take the coproduct $S = \bigsqcup_{i \in I} \text{Spec } \mathbb{Z}$ in the category of schemes instead. The only additional step is to verify that product distributes over infinite coproducts in the category of schemes, since this is no longer true by cartesian closure. Your calculation of the global sections of $\mathcal{O}_S[x_1, \dots x_n]$ is incorrect; if you do this calculation for $S$ as defined above you'll get $\mathbb{Z}[x_1, \dots x_n]^I$ as below, not $\mathcal{O}_S(S)[x_1, \dots x_n] = \mathbb{Z}^I[x_1, \dots x_n]$.

Edit: I was careless. In fact this appears to me to be false. The issue is exactly what you said, that tensor products don't distribute over infinite limits in general. More precisely, write $S$ as a colimit $\text{colim}_i \text{Spec } R_i$ of representables. Then
$$\mathcal{O}(S) \cong \lim_i R_i$$
while, using cartesian closure as below, we have
$$\mathcal{O}(S \times \mathbb{A}^n) \cong \lim_i R_i[x_1, \dots x_n]$$
and we have a natural map
$$(\lim_i R_i)[x_1, \dots x_n] \to \lim_i R_i[x_1, \dots x_n]$$
which is not an isomorphism in general. The issue is that an element of the LHS is a polynomial of fixed degree. To be explicit, suppose $S = \bigsqcup_{i \in I} \text{Spec } \mathbb{Z}$ is an infinite disjoint union of points, so the index set $I$ is infinite. Then we are considering the natural map
$$\mathbb{Z}^I[x_1, \dots x_n] \to \mathbb{Z}[x_1, \dots x_n]^I$$
and the image of this map does not contain any sequence $f_i : I \mapsto \mathbb{Z}[x_1, \dots x_n]$ of polynomials whose degrees get arbitrarily large.

You need to use the fact that presheaf categories are cartesian closed. So if $Y, Z : \text{CRing} \to \text{Set}$ are two presheaves on $\text{Aff}$ then there exists an exponential presheaf $[Y, Z]$ determined by the adjunction
$$\text{Hom}(X \times Y, Z) \cong \text{Hom}(X, [Y, Z]).$$
It follows that $\times$ is a left adjoint and hence preserves colimits in both variables, which lets you reduce to the representable case. What this argument does is actually compute the exponential $[\mathbb{A}^n, \mathbb{A}^1]$ to be the presheaf sending a commutative ring $R$ to the polynomial ring $R[x_1, \dots x_n]$, by reducing the verification of the universal property for all presheaves $X$ to representable presheaves only.
A: **The calculation here does not work, the correct answer is the accepted one.**I deleted the answer because I do not see why the tensor product in the calculation below commutes with the limit.
Write $S$ as a colimit
$$S = \operatorname{colim}_{(R,r)\in \text{Aff/S}} \operatorname{Spec}R$$
Here Aff/S is the category of representables above $S$. I write $\operatorname{Spec }(R)$ instead of $\text{Hom}(R,-)$. The functor $-\times \mathbb A^n$ is left adjoint, so it preserves colimits. The same is true for the functor $-\otimes_\mathbb Z\mathcal O(\mathbb A^n)$. We can compute
\begin{align}
\mathcal O(S\times \mathbb A^n) &= \mathcal O((\operatorname{colim}_{(R,r)\in \text{Aff/S}} \operatorname{Spec }R) \times \mathbb A^n) 
\\&=\mathcal O(\operatorname{colim}_{(R,r)\in \text{Aff/S}} (\operatorname{Spec} R \times \mathbb A^n)) 
\\&= \operatorname{lim}_{(R,r)\in \text{Aff/S}} \mathcal O(\operatorname{Spec}R\times \mathbb A^n)
\\&=\operatorname{lim}_{(R,r)\in \text{Aff/S}} ((\mathcal O\operatorname{Spec}R)\otimes_\mathbb Z \mathcal O\mathbb A^n) \\&= (\operatorname{lim}_{(R,r)\in \text{Aff/S}} (\mathcal O\operatorname{Spec}R))\otimes_\mathbb Z \mathcal O\mathbb A^n 
\\&=\mathcal O(\operatorname{colim}_{(R,r)\in \text{Aff/S}} \operatorname{Spec }R) \otimes_\mathbb Z \mathcal O\mathbb A^n
\\&=\mathcal OS\otimes_\mathbb Z\mathcal O\mathbb A^n\\&=\mathcal OS\otimes_\mathbb Z\mathbb Z[x_1,...,x_n]
\end{align}
So it follows from moving brackets the right way -.-
