This is my theory cobbled together from various sources.
1/ Cheese is a source of the amino acid Tryptophan. A diet containing Tryptophan is required to enable the production of the neuro-transmitters serotonin and thus in turn melatonin required for regular sleeping patterns.
2/ Drugs that work on the serotonin system are often cited as having the side effects of vivid dreams and nightmares (e.g. anti-depressants like Mirtazapine)
In both cases serotonin levels become elevated and it doesn’t seem too unreasonable therefore to infer that in some people eating cheese at night will lead to vivid dreams and nightmares.
The conversion of Tryptophan to Serotonin is aided by the addition of a carbohydrate. So eat cheese and chocolate for a good nights sleep!
I guess there is some theory behind Horlicks and Ovaltine