Re: [stack] Jon Purdy: Why Concatenative Programming Matters
2012-02-13 21:23:17 GMT
On 02/13/2012 03:20 PM, William Tanksley, Jr wrote:
> You're right that any arbitrary function must be able to access
> all of the state; but it need not be true that any specific
> function must be able to access all the state. Modern
> concatenative languages use typechecking to make sure that no
> function attempts to consume stack state that doesn't match the
> function's declaration;
This is certainly true; or at least it would be if the languages
actually excited. With types, something like 'bi' can be
restricted so that the second function uses no more than one
element and thus you can consider both functions independently.
(For the record, I mistakenly referred to 'bi <at> ' in a previous
email. I meant 'bi'.)
Even without types, this is possible if you have 'infra' to run a
function on a stack-on-the-stack and some sort of 'concat'
operation to join a stack-on-the-stack onto the main stack
itself. Assuming curly braces denote stacks, we need only the
following primitives:
{A} [B] infra == {A B}
A {B} concat == A B
{A} B push == {A B}
This is enough to write a "safe" 'bi':
bi = [keep] dip [{} swap push] dip infra concat
With this version of 'bi', you'll get a stack underflow error if
the second quotation to 'bi' attempts to use more than one
element. Since we're not using types, you might as well pass an
argument to some generic version of 'bi' that says how many
arguments to reserve for the second function; just push that
many onto the stack before calling 'infra'.
> Were you the one who designed a concatenative language with
> side-effect checking?)
Yes, although it is just a little trick of attaching a variable
to function types that can either be "effectful" or "not
effectful". You can infer it via unification. Not much to it.
It's a bit coarse though because it doesn't offer any form of
local effects with a pure interface as you can get with the ST
monad or with linear or uniqueness types (though they could be
added if you wanted).
- jn
RSS Feed