JohnEarnest / ok

An open-source interpreter for the K5 programming language.
MIT License
585 stars 73 forks source link

flip of a scalar #77

Closed ngn closed 4 years ago

ngn commented 5 years ago

Given +1 1 is ,1 1, shouldn't +1 be ,,1 instead of 1?

shape of x   ⍴⍉x in APL   "shape" of +x in Kona   "shape" of +x in oK
a b c d      d c b a      b a c d                 b a c d
a b c        c b a        b a c                   b a c
a b          b a          b a                     b a
a            a            a                       1 a
empty        empty        empty                   1 1  <- or so it should be

I know k arrays can be ragged, but that's irrelevant to my argument, so let's ignore them for a moment and talk about "shapes", "ranks", and "dimensions".

In APL transpose () reverses the shape, so it makes sense for the transpose of a scalar (which is of course empty-shaped) to be itself a scalar.

In k flip (+) is supposed to swap the 2 leading "dimensions", so we run into a problem when there are fewer than 2.

Kona (and k3?) resolves this by reversing only the leading min(2,rank(x)) "dimensions":

$ ./k
kona      \ for help. \\ to exit.

  +1 1
1 1
  +1
1

oK (and k5/k6?) appears to be treating the "shape" as if it's followed by implicit 1s (which is very reasonable): +1 1 evaluates to ,1 1, but there's a discontinuity for +1:

$ ./repl.js
oK v0.1
 +1 1
,1 1
 +1
1
JohnEarnest commented 4 years ago

In k3, you get (+1)~1 and (+1 2)~1 2. In k5, and the last version of k6 I ever got, you get a rank error for both of those cases. In k7, Arthur appeared not to bother implementing +: for atoms at all.

The behavior of k5/k6/k7 suggests to me that Arthur considers this irrelevant. I consider myself more than free to improve upon edge-cases which throw errors, and you make a reasonably convincing case for this generalization. Done!