If R3-Alpha REDUCE were in Revolt Today

Let Us Unify ARGuments and REFinements

:five: :robot:

//
//  reduce: native [
//      "Evaluates expressions and returns multiple results."
//      value
//      /no-set "Keep set-words as-is. Do not set them."
//      /only "Only evaluate words and paths, optionally just in a list"
//          [block! logic!]
//      /into "Output results into a block with no intermediate storage"
//          [any-block!]
//   ]
//
DECLARE_NATIVE(REDUCE)
{
    INCLUDE_PARAMS_OF_REDUCE;

    if (Is_Block(ARG(VALUE))) {
        Flex* flex = Series_Flex(ARG(VALUE));
        Index index = Series_Index(ARG(VALUE));
        Option(Value*) val = ARG(INTO);

        if (ARG(NO_SET))
            Reduce_Block_No_Set(flex, index, val);
        else if (ARG(ONLY))
            Reduce_Only(flex, index, unwrap ARG(ONLY), val);
        else
            Reduce_Block(flex, index, val);
        return R_TOS;
    }

    return R_ARG1;
}

We've saved two Cells of space on every call to REDUCE. That's 32 bytes on 32-bit machines, and 64 bytes on on 64-bit machines. Not only do we save the space, but we also save the CPU cycles to fill that space. Plus people don't have to come up with names for the refinement arguments!

(Not a day has passed where I've missed multi-argument refinements.)

Notice that REF() is gone. Instead, the ARG() macro is informed by the expansion of INCLUDE_PARAMS_OF_REDUCE as to which arguments are refinements. It would look something like this:

#define INCLUDE_PARAMS_OF_REDUCE \
    DECLARE_PARAM(Need(Value*), VALUE, 1, false); \
    DECLARE_PARAM(Option(Value*), NO_SET, 2, true) \
    DECLARE_PARAM(Option(Value*), ONLY, 3, true); \
    DECLARE_PARAM(Option(Value*), INTO, 4, true)

So what's happening here is that the ARG() macro knows whether to produce something that's either a null pointer or a Cell* (hence logically testable with C's if()) or whether to just give back the Cell*. The true or false guides the extraction.

In the C++ build you are protected from writing if (ARG(VALUE))... because it's not a refinement, the Need() wrapper enforces that you can't convert it to boolean.

In a C build Option(Value*) and Need(Value*) are both just Value* so you're just on your honor to know that it would be a bad idea to write if (ARG(VALUE)) because since it's not a refinement, ARG(VALUE) will never interpret a "none!" as a C NULL. But I think the protection is good to have!

(Note that because /ONLY is a refinement, whatever the "no refinement" state is can't be used as a value, so I changed the assumption that you use a LOGIC!... you could use anything that's not none here...)


Optimized Generalized Returns: Direct To OUT!

:six: :racing_car:

// ...SPEC...
//
DECLARE_NATIVE(REDUCE)
{
    INCLUDE_PARAMS_OF_REDUCE;

    if (Is_Block(ARG(VALUE))) {
        Flex* flex = Series_Flex(ARG(VALUE));
        Index index = Series_Index(ARG(VALUE));
        Option(Value*) val = ARG(INTO);

        if (ARG(NO_SET))
            Reduce_Block_No_Set(OUT, flex, index, val);
        else if (ARG(ONLY))
            Reduce_Only(OUT, flex, index, unwrap ARG(ONLY), val);
        else
            Reduce_Block(OUT, flex, index, val);
        return OUT;
    }

    return COPY_TO_OUT(ARG(VALUE));
}

R3-Alpha returned an enum value to say where to look to find an output value. The full-on generic way of saying this would be to write to the output cell... whose name was D_RET. And if you wanted the interpreter to look in the D_RET cell you would return the R_RET enum value.

Hence the way to implement the COPY_TO_OUT() I show in R3-Alpha would be with a macro:

#define COPY_TO_OUT(cell) (*D_RET = *cell, R_RET)  // R3-Alpha version

Revolt doesn't use a return enum, it uses a void pointer... and due to the various values in the system having "detectability" it's possible to discern what type you returned.

But one very special pointer you can return is the OUT pointer. So it's like being both D_RET and R_RET... it's the place to write to and the signal that's where you wrote.

Just to demonstrate, let's show how to write R_TOS (return value on top of stack) in Revolt:

#define R_TOS (Copy_Cell(OUT, TOP), DROP(), OUT)

But there is no R_TOS in Revolt, and I re-styled the code above to pass in OUT to the Reduce_Xxx() functions. Why would I do that when it seems to make the code more verbose?*

The reason is that in Revolt, you don't have to use the stack...or even a frame Cell...to act as a waystation to move cells to where they're ultimately going to land. OUT is a direct pointer to where the evaluator was asked to write the output.

This means that if you're building a frame for a function call, the evaluation product of another function is receiving the literal address where the data is to be written.

So not only is making the OUT parameter explicit in Reduce good for documentation that it has a product, it also means you're writing things direct to where they should be.


Further Requires Isotopes, CHAIN!s, librebol, etc.

:infinity: :atom_symbol:

I just wanted to show off how the baseline comprehensibility of Revolt code has gotten much better even if you stayed in the R3-Alpha headspace.

In particular: I was motivated to show off how I got rid of the REF(...) vs. ARG(...) distinction. This is helpful if you ever change the spec in a way that turns something from a refinement into an argument or vice versa.

It would be fully possible to keep R3-Alpha more or less "as is" and adopt these design improvements. But if you see these things as improvements, why would you stop here?

Wouldn't you imagine even greater heights have been achieved using this baseline?

:mountain: :man_climbing:

1 Like