Solution (source code)

= Solution

A <martingale> with respect to a <filtration> $(\mathcal F_n)$ is an adapted process: $X_n$ is $\mathcal F_n$-measurable for every $n$. It must satisfy $\mathbb E|X_n|<\infty$ and
$$
\boxed{\mathbb E[X_{n+1}\mid\mathcal F_n]=X_n\quad\text{almost surely}}.
$$
By the tower property this is equivalent to $\mathbb E[X_m\mid\mathcal F_n]=X_n$ for all $m\ge n$. Integrability and adaptation are part of the definition, not consequences of writing a formal conditional-mean identity.