Fix parsing of $

Needs to flatten applications so that elaboration does the right thing.
Fixes #1020