Bugs
----
Script →#.math← does a poor job of collecting "like terms":

      math script until'→'

    x+y+x+y         ⍝ OK
2*(y+x)

    x+y+x+y+x+y     ⍝ not so good
2*(y+x)+x+y

    x+y+z+x+y+z     ⍝ abysmal
x+y+z+x+y+z

    →

In  general, to collect terms that are arbitrarily far apart, we would need arb-
itrarily complex reduction templates.

One solution would be to install a separate "collection" phase alongside reduce,
to do the job.

      numb──←─┐   ┌────┐   ┌────────┐   ┌─────┐
              ├─←─┤show├─←─┤evaluate├─←─┤parse├───←──expr
      expr──←─┘   └────┘   └─┬────┬─┘   └─────┘
                             ↓    ↑
                           ┌─┴────┴─┐
                           │ reduce │
                           └─┬────┬─┘
                             ↓    ↑
                           ┌─┴────┴─┐
                           │collect │
                           └────────┘

The  collector  would need to know whether each infix function is commutative or
associative. The information could be indicated either in the function declarat-
ion itself:

    ← *(×)⍨≡    /(÷)            ⍝ mul div
          │└──────────────────  associative
          └───────────────────  commutative

or by ancilliary declarations of the form:

    :commutative + *
    :associative + *

Note that the _distribution_ of functions with respect to each other (* distrib-
utes  over  +  etc) is already handled adequately by the reduction subsystem, as
distribution is a _local_ effect within the expression tree.

The  collector  would  identify like terms and try to bring them close enough in
the expression tree for the reduction rules to take effect.

    ((((x+y)+z)+x)+y)+z     ⍝ fully parenthesised starting expression.

    ((((x+y)+z)+x)+y)+z     ⍝ like terms
        ¯       ¯
    ((((x+y)+z)+x)+y)+z     ⍝ + associative
         ¯  ¯
    (((x+(y+z))+x)+y)+z     ⍝ + commutative
        ¯
    ((((y+z)+x)+x)+y)+z     ⍝ + associative
            ¯  ¯
    (((y+z)+(x+x))+y)+z     ⍝ reduction rule
             ¯¯¯
    (((y+z)+(2*x))+y)+z     ⍝ like terms
       ¯           ¯
    ...

    ((2*z)+(2*y))+(2*x)

    ...

    2*((z+x)+y)

Back to: →Contents