From 5cf01779b86ed6808ca336cdc87e98be8497404b Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Fri, 12 Aug 2022 08:02:42 -0400 Subject: Corrections --- docs/commentary/overload.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'docs') diff --git a/docs/commentary/overload.html b/docs/commentary/overload.html index 5004d34c..f45771f1 100644 --- a/docs/commentary/overload.html +++ b/docs/commentary/overload.html @@ -20,13 +20,13 @@

Equivalent overloading

APL isn't really a "one obvious way to do it" in the sense that Python is, but it does follow a principle I'd describe as "one way is enough". That means that if APL already has a way to represent some data or a computation, it won't add another without a concrete benefit like shorter or faster code. This is why APL booleans are a kind of integer (I defend this decision here), and why it has one array datatype instead of various kinds of collection or a separate string type.

This means that something like the number 1 can mean many things, like an index or a count or a boolean, and the replicate function / might mean repeating or filtering. It's overloading, but it's a very consistent form because the mathematical description of what's going on in either case is the same. But it's not the only wayβ€”some statically-typed languages like Java and Haskell prefer to declare classes to split things up, so that the type system can check things for the user. An extreme example is a system that takes user input but needs to sanitize or escape it before passing it to certain functions. The APL way would be to represent both unsafe and safe input as strings, which is obviously dangerous.

-

However, the advantage of representing everything in a consistent format is that methods that work on one thing tend to work on many things. Want to reverse a string? It's just ⌽. Defining boolean negation ¬𝕩 more generally as 1-𝕩 makes some arithmetic more obvious, for example +´¬l is (β‰ l)-+Β΄l. And connections can be made at a higher level too: if you learn a rule like a⊏b⊏c ←→ a⊏(b⊏c), that applies for every meaning of ⊏. As long as a and b are flat arrays, that is, which highlights a conflict between this sort of compatible overloading and other sorts of extension.

+

However, the advantage of representing everything in a consistent format is that methods that work on one thing tend to work on many things. Want to reverse a string? It's just ⌽. Defining boolean negation ¬𝕩 more generally as 1-𝕩 makes some arithmetic more obvious, for example +´¬l is (β‰ l)-+Β΄l. And connections can be made at a higher level too: if you learn a rule like a⊏b⊏c ←→ (a⊏b)⊏c, that applies for every meaning of ⊏. As long as a and b are flat arrays, that is, which highlights a conflict between this sort of compatible overloading and other sorts of extension.

Extensions

Heading further into the woods, we see that the APL family extends functions in ways that don't follow strictly from the definition. Usually this happens within a single primitive. For example, First (monadic βŠ‘) is only natural to apply to an array, since otherwise there's no first element. But it's defined to return an atom with no changes, effectively treating it as a unit array. This sort of implicit promotion is used just about everywhere it could be, since in all cases it's obvious what needs to be done and inconvenient to require explicit conversion.

If that was all, it would hardly be worth mentioning. A more significant family of extensions is the use of depth to allow a primitive to work on multiple axes in general but also to have a convenient one-axis form. Then there's character arithmetic, allowing 'a' + 3. In fact, isn't array arithmetic itself a big extension?

There are examples outside the array world that I find worse than anything in BQN. + for string catenation. This no longer obeys commutativity or distributivity: it's not safe to rearrange a + b to b + a or (a+b)*c to (a*c)+(b*c) in languages that do this! NumPy and MATLAB allow a boolean array to be used as an index, performing filtering. This one doesn't obey the rule that the length of a[b] is the length of b, or any other length-based rules really.

Sometimes what seems like an extension can be unified into a single more general primitive. For example, APL has scalar extension to allow you to add, say, a scalar to a list in 1 + 2β€Ώ3β€Ώ4. J and BQN use the more general leading axis agreement, which has this extension as a special case (although incidentally, BQN removes some unprincipled extension of list-like functions like Reverse to rank-0 arguments). Character arithmetic can also be viewed in this way considering numbers and characters to be pairs of "characterness" 0 or 1, and a numeric value.

-

I think many primitive pairs, such as -, ⋆, Β«, β₯Š, and ⍉, fall into this category too. Primitives like can be described as a general dyadic function, and a monadic case that comes from a default left argument (sometimes dependent on the left argument: it's (=𝕩)-1 for ⍉). The primitive β‹ˆ is so tight it might be considered a fully compatible overload, returning a list of all arguments. Such primitive pairs can sometimes be used ambivalently in simple ways (⋆⁼ is pretty nice), but more often the usefulness is just that it's easier to think about each pair as one thing rather than two. It's just two views of the same idea.

+

I think many primitive pairs, such as -, ⋆, Β«, β₯Š, and ⍉, fall into this category too. Primitives like can be described as a general dyadic function, and a monadic case that comes from a default left argument (sometimes dependent on the right argument: it's (=𝕩)-1 for ⍉). The primitive β‹ˆ is so tight it might be considered a fully compatible overload, returning a list of all arguments. Such primitive pairs can sometimes be used ambivalently in simple ways (⋆⁼ is pretty nice), but more often the usefulness is just that it's easier to think about each pair as one thing rather than two. It's just two views of the same idea.

Mnemonic overloading

Okay, we are slipping down the slope nicely, now what about the primitives where the two halves don't quite do the same thing? Take ∾ to start smoothly. The dyadic form joins two lists and the monadic form joins a list of lists. Well, this is really one function that takes its arguments in a slightly unusual way, since dyadic ∾ is βˆΎβˆ˜β‹ˆ. The primitive ↑ for Prefixes/Take (↓ too) is similar, but in a trickier way: if 𝕨 is between 0 and ≠𝕩, 𝕨↑𝕩 is a prefix of 𝕩. Then ↑ is the list of all these prefixes, so π•¨βŠ‘β†‘π•© is 𝕨↑𝕩. It's almost a kind of partial application.

These primitives are easier to remember in the same way that it's much easier to memorize just ⊏ instead of a select function and a separate first-cell function. If I weren't allowed to overload them together, I probably just wouldn't include monadic β†‘β†“βŠ (or βˆšβ‹†, even -?), and maybe not even dyadic ∾.

-- cgit v1.2.3