Reduction meta-operator inconsistency
Asked Answered
B

1

8

When we examine the reduce function:

    my $result = reduce &reduction_procedure, @array;

we conclude with the following simple rules to the inner workings:

    Reduction Rules
    ---------------
    1. For the first pair of objects from our input (@array)
       we apply the reduction procedure (&reduction_procedure) and we get our first result.

    2. We apply the reduction procedure (&reduction_procedure) to the result (object)
       of the previous rule and the next input (object) from our input (@array),
       and we get an intermediate result (object).

    3. We run rule.2 for every of the remaining objects from our input (@array)

This simple rules work also the same for the reduction metaoperator []. For example:

    example.1
    ---------
    say [+] [1,2,3,4]; # result : 10

For example.1 the reduction rules apply as is:

    Step.1 use Rule.1 : 1 + 2 = 3     1(first value)     + 2(second value)  = 3
    Step.2 use Rule.2 : 3 + 3 = 6     3(previous result) + 3(current value) = 6
    Step.3 use Rule.2 : 6 + 4 = 10    6(previous result) + 4(current value) = 10

but NOT for the following example:

    example.2
    ----------
    say [<] 1,2,3,4;   # result : True

For example.2 we observe an inconsistency:

    Step.1 use Rule.1 : 1 < 2 = True    1(first value)         <  2(second value)      = True
    Step.2 use Rule.2 : 2 < 3 = True    True(previous result) &&  True(current result) = True
    Step.3 use Rule.2 : 3 < 4 = True    True(previous result) &&  True(current result) = True(result)

The inconsistency is that from Step.2 and onwards we can NOT use the result of the previous step as the first parameter of subsequent reduce operations, but instead we calculate a logical intermediate result, with use of the actual values and finally we add as a final step the use of "logical AND" upon the intermediate logical results of each step:

    Reduction Result = True && True && True = True (use of logical AND as "meta-reduction" operator)

Sort of speak we have a "meta-reduction" metaoperator!

Questions:

    1. Is that an inconsistency with a purpose?

    2. Can we exploit this behavior? For instance instead of use of && (logical AND)
       as "meta-reduction" operator can we use || (logical OR) or ?^ (logical XOR) ?
Bolding answered 18/12, 2019 at 20:28 Comment(1)
BTW, with the reduction metaoperator you can precede the operator with a backslash to get the reduction's intermediate values (e.g., say [\+] [1,2,3,4]; outputs (1 3 6 10)).Sandstrom
M
16

It is not an inconsistency but and example of how operator associativity works in Raku.

Writing :

[<] 1,2,3,4

Is the same as writing :

1 < 2 < 3 < 4

In most languages this wouldn't work but the < operator has chain associativity so it effectively treats this as :

(1 < 2) and (2 < 3) and (3 < 4)

Which is True.

Misapply answered 18/12, 2019 at 21:3 Comment(8)
In the following example code the "foo" operator has "chain" associativity but it behaves NOT as I expected: sub infix:<foo>(Int $a, Int $b --> Bool) is assoc<chain> { my $p = ($a, $b).pick; $p.so } say 'Result: ' ~ [foo] 0,1,0,0; code Resolving the flow of execution: [foo] 0,1,0,0 Is the same as writing : 0 foo 1 foo 0 foo 0 the foo operator has chain associativity so it must effectively treat this as : (0 foo 1) and (1 foo 0) and (0 foo 0) so it must be always False but this is NOT the case! I am a bit confused!Bolding
If you want to create your own chaining operator you need a version that accepts a slurpy array of values and then knows how to handle that.Misapply
Can you give an exampe of a custom chain associative operator?Bolding
I'm doing some further investigation into this. I think the build in chain ops have some special magic applied that user generated ones don't get. But you can define a List Op and deal with them that way.Misapply
github.com/rakudo/rakudo/issues/3370 : Raised an issue about user generated Chaining operators.Misapply
Also note that the standard chain operators do all work as expeced, it's just custom ones that don't :(Misapply
Yes I've noticed! Probably a bug! Let's see the outcome of your raised issue.Bolding
@Bolding I believe the most recent Rakudo release includes the this for this issue :)Misapply

© 2022 - 2024 — McMap. All rights reserved.