It is on days like this that I can empathize with people who are are wary of the restrictions that static typing can place on them. In particular I've kind of coded myself into a corner in the development of InforML where I need: use polymorphic recursion (which Standard ML does not provide), duplicate a nontrivial amount of code at least half a dozen times, or attempt to manually manage the recursion myself. I just spent several hours on the third approach. I think it may still be possible yet, but I'll need to go back an revise just all the code I had just changed to use thunks as well.
It is almost to the point that I'm seriously considering rewriting a large portion of InforML in Haskell or Scala. I would probably keep the parser, by making it output XML or s-expressions that can be easily parsed back into an AST. But this would mean I would get proper support for monads, and the ability to write a number of things in a type-directed fashion, cutting out huge amounts of boilerplate.
However, it may be just more practical to duplicate the code that is causing the problem and hope I don't need to revise it that much more. Anyway, I am going to take a break for at least a few hours before moving forward. It is just so frustrating that I have to waste time on this sort of stuff when I have plenty of other »real« technical problems to be solving, instead of having to make up for the fact that Standard ML is antiquated.