metaeducation / rebol-issues

6 stars 1 forks source link

Can't CUSTOMize / #2516

Closed giuliolunati closed 4 years ago

giuliolunati commented 4 years ago

My CUSTOM module let me write e.g. (2 / 3) + (1 / 2) = (7 / 6) and (1 +i 1) / (1 -i 1) = i

That worked when / was a WORD, but now it's a PATH and I can't find no way to work around.

hostilefork commented 4 years ago

I'm still not sure exactly how to balance the design of /, as I feel having it be a WORD! is a bad idea.

I'd wanted to make it so that a / b would run path dispatch like a/b, and so customizing division would just be customizing path behavior for that type. That ran into some problems.

What I made it do as a temporary workaround is that a lone / runs the PATH0 native:

https://github.com/metaeducation/ren-c/blob/9d6434674c3e6faf730489551d61cf4b5caec602/src/core/c-path.c#L718

So overriding that is a possibility.

I still do not think / makes a good WORD!, e.g. it shouldn't appear inside PATH!s... and I'd like //x to mean something different from (:/)/x. But dealing with the issues is still experimental.

giuliolunati commented 4 years ago

The main issue is that I need to customize the behaviour of math ops locally for a block, and I do that by rebinding words.

I see no way to accomplish the same thing for / ...

... Maybe I should pre-process the code and substitute all the / with a word, say DIV ?

hostilefork commented 4 years ago

The main issue is that I need to customize the behaviour of math ops locally for a block, and I do that by rebinding words.

There are quite a lot of open issues about "overloading" or "methodization" in general...that no matter how they would be solved, will be difficult to control on a case-by-case basis.

Does Python have a way to overload the division of a type, but only in a certain part of the code?

Maybe I should pre-process the code and substitute all the / with a word, say DIV ?

If you can change your keyboard mapping to let you say ÷ in an easy way (Ctrl-'/'?) that might be something you would think looked good at a source level, and not have to rewrite.

giuliolunati commented 4 years ago

In Python i don't need that customization workaround, because it has ops overloading for objects.

The ÷ solution is partial, but doesn't look good for numerical fraction: 2 / 3 is much better than 2 ÷ 3 . However worth of a try.

hostilefork commented 4 years ago

As described in chat, here is a very experimental solution of binding / as -slash-1-. If it seems to generally work out, then it can be made more rigorous...and add the other -slash-N- values for things like // and so on.

code: [1 / 2]
obj: make object! [
    -slash-1-: enfix func [a b] [
        return reduce '(b a)
    ]
]
0.5 = do code
bind code obj
'(2 1) = do code

It uses some very weird untested properties, so there might be strange bugs. Run debug builds whenever possible.

Adding more tests would be ideal!!

giuliolunati commented 4 years ago

Many thanks for your care! I run to test it!

giuliolunati commented 4 years ago

It works!