restructuring of pileup logic

Introduced appropriate state monad, a handful of primitives, and an
outline of the code.  Doesn't even compile so far.