Given $\mathbb{E}[X|Y] = Y$ a.s. and $\mathbb{E}[Y|X] = X$ a.s. show $X = Y$ a.s. Given $X,Y \in L^2(\Omega,\mathscr{F},\Bbb{P})$ such that 
$\mathbb{E}[X|Y] = Y$ a.s.
$\mathbb{E}[Y|X] = X$ a.s.
show that $\Bbb{P}(X = Y ) = 1.$
$Attempt: $
I can see that $\mathbb{E}[X|Y] = Y$ means 
$$ \int_{\{Y\in{A}\}}X \, d\Bbb{P} = \int_{\{Y\in{A}\}}Y \, d\Bbb{P} $$ for any
$A \subset{\Bbb{R}} $ Borel, so
$$ \int_{\{Y\in{A}\}}X - Y \, d\Bbb{P} = 0 $$ over any such sets. Similarly for sets of the form $\{X \in A \}$.
Now if I could show that $ \int_U X - Y \, d\Bbb{P} = 0$ for any set $U \subset \Omega$ of the form $\{X \lt a, Y \lt b\}$ for $a,b \in \Bbb{R}$ then I'd be done, because sets of that form are a $\pi$-system that generates $\sigma(X,Y)$, so I'd have (by a lemma) that the integral of $X-Y$ vanishes on all $\sigma(X,Y)$ sets - so of course, it would be zero. But I can't work out how to do that.
I'm given the hint that 
$$ \int_{ \{ X \gt c, Y \le c \} } X - Y \, d\Bbb{P} + \int_{ \{ X \le c, Y \le c \} } X - Y \, d\Bbb{P} = 0 \; \text{for all} \; c $$
because that's the integral over $\{Y\le c\}$, a set of the above form. With the condition that $X$ and $Y$ are integrable, this is exercise 9.2 in Williams' "Probability with Martingales". Driving me up the wall to get so stuck on what seems such a simple exercise!
 A: Since 
$\mathbb E[X^2] = \mathbb E[X\mathbb E[Y|X]] = \mathbb E[\mathbb E[XY|X]] = \mathbb E[XY]= \mathbb E[\mathbb E[XY|Y]] = \mathbb E[Y\mathbb E[X|Y]] = \mathbb E[Y^2], $ 
observe that 
$$
\mathbb E[(X-Y)^2]=\mathbb E[X^2+Y^2-2XY]=0,
$$
which implies that $X$ and $Y$ differ on a set of measure zero.

For the weaker condition where $X$ and $Y$ are merely integrable we proceed as follows. Choose an arbitrary rational number $c\in\mathbb Q$. Note that
$$
\mathbb E[(X-Y){\bf 1}_{Y\leqslant c}]=\mathbb E[(X-Y){\bf 1}_{Y\leqslant c}{\bf 1}_{X\leqslant c}]+\mathbb E[(X-Y){\bf 1}_{Y\leqslant c}{\bf 1}_{X> c}]\tag{1}
$$
Reversing the roles of $X$ and $Y$ gives 
$$
\mathbb E[(X-Y){\bf 1}_{X\leqslant c}]=\mathbb E[(X-Y){\bf 1}_{X\leqslant c}{\bf 1}_{Y\leqslant c}]+\mathbb E[(X-Y){\bf 1}_{X\leqslant c}{\bf 1}_{Y> c}]\tag{2}
$$
 The lhs of each of (1) and (2) is zero, since $\mathbb E[(X-Y){\bf 1}_{X\leqslant c}]=\mathbb E[(X-\mathbb E[X|Y]){\bf 1}_{X\leqslant c}]=$$\mathbb E[X{\bf 1}_{X\leqslant c}]-\mathbb E[X{\bf 1}_{X\leqslant c}]=0$, for instance. This, in turn, implies from (1) and (2) that
$$
\mathbb E[(X-Y){\bf 1}_{X\leqslant c}{\bf 1}_{Y> c}]=\mathbb E[(X-Y){\bf 1}_{Y\leqslant c}{\bf 1}_{X> c}],\tag{3}
$$
a proposition that equates a surely non-positive number to a surely non-negative number. Thus, the lhs and rhs of (3) are surely zero. The events $[X\leqslant c]\cap[Y> c]$ and $[Y\leqslant c]\cap[X> c]$, therefore, must each be null events. But this holds for all $c$, so that (by countable unions) the events 
$$[X<Y]=\bigcup_{c\in\mathbb Q}[X\leqslant c]\cap[Y> c]\ \ \text{and}\  \ [Y<X]=\bigcup_{c\in\mathbb Q}[Y\leqslant c]\cap[X> c]$$
are also null events. That is, $[X\ne Y]$ is a null event. This completes the proof.
