We assume existance of Borel function $f:\mathbb{R}\rightarrow\mathbb{R}$ with the condition that for all $x,y\in\mathbb{R}$ we have $$x\sim y\iff f(x)=f(y).$$ How to show then an existance of countable family $\{A_n\}_{n\in \mathbb{N}}$ of Borel subsets $\mathbb{R}$, such that $$x\sim y \iff \forall_n \ (x \in A_n \iff y \in A_n).$$ It seems to be logical but I have no idea how to write it down formally. This is my first problem.
The second is strongly connected with the first one, namely I need to show the opposite implication, i.e. if exists countable family $\{A_n\}_{n\in \mathbb{N}}$ of Borel subsets $\mathbb{R}$, such that $$x\sim y \iff \forall_n \ (x \in A_n \iff y \in A_n),$$ then we can show that there exists borel function $f:\mathbb{R}\rightarrow\mathbb{R}$ with the condition that for all $x,y\in\mathbb{R}$ we have $$x\sim y\iff f(x)=f(y).$$
Every advice and help would be much appreciated, because I don't have any sensible ideas.