The Doctor Moon was an artificial moon which was built by Felman Lux, Strackman Lux's grandfather, to protect CAL, his youngest daughter who became the Library's computer and was given an imaginary world in which to play while in control of the Library.
In Charlotte's imaginary world Doctor Moon was in human form and was treating her. He protected her and gave her advice to save the others in the Library. That was until she was in an overload and temporarily switched him off. When the Doctor saved River Song into CAL's imaginary world, the Doctor Moon was back online once more, protecting CAL, River and the rest of the team, minus Strackman Lux who had not died in the Library. (DW: Silence in the Library/ Forest of the Dead)
Doctor Moon's function in the virtual world was apparently to keep both Charlotte and others downloaded into it from realizing the unreality of the world they were in, possibly to help them keep sane. The virtual world was modeled after early 21st Century Earth (or something similar to it).