There’s an hypothesis that states we dream so we don’t lose visual processing neural connections. Similar to what happens in blind people: visual processing neurons are recruited to other sensory tasks due to lack of stimulation. My ed. guess is that dreaming probably serves multiple purposes

There was an early visual neural network demonstration — strong feeling it was called "Yorick" or somesuch as it was built in a plaster skull — that had a square grid of red LEDs to show its output state as a simple picture; when its camera was unplugged the neural network appeared to "dream" in the sense that things it had seen would flicker and swirl in the output.

I saw this in a video in the early 90s and cannot remember where.