[stack] Code Transformation Problem (possibly monadic?)
"Christopher Diggins" <[email protected]>
| Newsgroups | gmane.comp.lang.concatenative |
|---|---|
| Message-ID | <[email protected]> |
I have the following problem, and I would be most grateful for any help: Given the following sequence of instruction: a b m [c m d e] f g I want an automated transform (no matter how inefficient, it just has to be algorithmic) to rewrite the code so that each instruction applies to the stack below the top value, except for "m". The naive transformation would be: [a b m [c m d e] f g] dip However, we have a problem because the "m" instruction can't see the top of the stack. The next obvious transformation (which doesn't work) would then be: [a] dip [b] dip m [[[c] dip m [d] dip [e] dip]] dip [f] dip [g] dip However, consider if "f" is really "apply". [a] dip [b] dip m [[[c] dip m [d] dip [e] dip]] dip [apply] dip [g] dip You can see this won't work for the nested quotation. Informally there is an undesirable nesting of "dip"s. There is a very close correspondance between this problem and monads (http://homepages.inf.ed.ac.uk/wadler/topics/monads.html). This leads me to suspect that the solution has the form: [a] unit [b] unit bind [m] bind [[c] unit [m] bind [d] unit bind [e] unit bind] bind [apply] unit bind [g] unit bind For some value of unit (a monadic transform), and bind (a monadic composition operator). I would be most grateful for a solution. Thanks in advance, Christopher