Expression caching

improved version of #9006 (which was an improved version of #8942)
I removed this dupe.

the other copy does contain the "constant" assertion. I kept the other assertions as they were. I think that they are useful.