phantomics / april

The APL programming language (a subset thereof) compiling to Common Lisp.
Apache License 2.0
602 stars 31 forks source link

⍣ operator, non-deterministic and seems to create new scopes for each iteration? #260

Closed carkat closed 1 year ago

carkat commented 2 years ago

Data does not persist / new scope per iteration?

LINK TO OFFENDING CODE EXAMPLE

Here is a run that uses the power operator. As you can see on line 99, rm is called, which returns a table without the values of the first column in accum. This returns the same value as if I had called parse¨table a single time Calling multiple times accumulates more and more values into the table.

accumulation doesn't seem to persist

image

accumulation should be persisting, except for the power operation?

This list will continue to populate as more values are accumulated. But you can see in the first screenshot the number of values remains constant, even with integer right operands to power.

image


Non Determinism

To test the above I created a small example:

        accum←1 2⍴'default' 0
        update←{accum⍪←(⊃⍵) (⊃⌽⍵)}
        {_←{update 'new' ⍵}¨⍵⋄⌽¯1↓⍵}⍣5⊢⍳5
        accum

Output should be numbers bouncing from end of range to end of range, settling at the middle.

image

image Above are a few runs of day8.

Below are the counts of several more runs, which produce different length results, despite being a deterministic algorithm. image

phantomics commented 1 year ago

This has been temporarily remedied by disabling threading for the ¨ operator. Threading for operators should be inhibited when they are given functions with side effects, but the logic for doing this is complicated. Stay tuned for a better solution to this issue.

phantomics commented 1 year ago

This has now been properly fixed. Functions defined within the scope of a function will now have side effects properly accounted for, and functions with side effects in such a scope will produce non-threaded functions with composed with operators like ¨.