diff options
Diffstat (limited to 'doc')
| -rw-r--r-- | doc/README.md | 1 | ||||
| -rw-r--r-- | doc/identity.md | 2 | ||||
| -rw-r--r-- | doc/primitive.md | 2 | ||||
| -rw-r--r-- | doc/repeat.md | 2 | ||||
| -rw-r--r-- | doc/transpose.md | 2 | ||||
| -rw-r--r-- | doc/undo.md | 56 |
6 files changed, 61 insertions, 4 deletions
diff --git a/doc/README.md b/doc/README.md index 682edb92..74d70ef4 100644 --- a/doc/README.md +++ b/doc/README.md @@ -61,6 +61,7 @@ Primitives: - [Solo, Couple, and Merge](couple.md) (`≍>`) - [Take and Drop](take.md) (`↑`) - [Transpose](transpose.md) (`⍉`) +- [Undo](undo.md) (`⁼`) - [Windows](windows.md) (`↕`) Environment: diff --git a/doc/identity.md b/doc/identity.md index a9916bcb..fe9ad2e9 100644 --- a/doc/identity.md +++ b/doc/identity.md @@ -14,7 +14,7 @@ Here are the simplest functions in BQN: Right (`⊢`) always returns its right a Depending on your past experiences, this could cause some confusion: built-in support for functions that do nothing? Documentation should say why a feature's there and how to use it, not just what it does, so we'll try to address this below. The most important single use is for tacit programming, but there are a variety of other uses as well. -Of course, it's easy to write block functions `{𝕩}` and `{𝕨}` that return particular arguments. While I would already make `⊣` and `⊢` primitives just because they are common and important, there are also specific disadvantages to using blocks. They fail to indicate that there are no side effects, as primitives would, and they also need special casing for the interpreter to manipulate them when applying Undo (`⁼`) or making other inferences. +Of course, it's easy to write block functions `{𝕩}` and `{𝕨}` that return particular arguments. While I would already make `⊣` and `⊢` primitives just because they are common and important, there are also specific disadvantages to using blocks. They fail to indicate that there are no side effects, as primitives would, and they also need special casing for the interpreter to manipulate them when applying [Undo](undo.md) (`⁼`) or making other inferences. ## Filling arrays diff --git a/doc/primitive.md b/doc/primitive.md index 9d4378c0..4cb8f13c 100644 --- a/doc/primitive.md +++ b/doc/primitive.md @@ -85,7 +85,7 @@ Other modifiers control array traversal and iteration. In three cases a simpler `˘` | Cells | `⎉` | [Rank](https://aplwiki.com/wiki/Rank_(operator)) `¨` | [Each](map.md) | `⚇` | [Depth](depth.md#the-depth-modifier) `⌜` | [Table](map.md) | -`⁼` | Undo | `⍟` | [Repeat](repeat.md) +`⁼` | [Undo](undo.md) | `⍟` | [Repeat](repeat.md) `´` | [Fold](fold.md) | `˝` | [Insert](fold.md) | `` ` `` | [Scan](scan.md) | diff --git a/doc/repeat.md b/doc/repeat.md index 26afe7e9..bb7000ea 100644 --- a/doc/repeat.md +++ b/doc/repeat.md @@ -15,7 +15,7 @@ In mathematics (which unsurpisingly tends to use complicated terms to talk about `F⍟0` repeats `F` zero times, that is, does nothing. Like `n⋆0` gives the multiplicative identity `1`, `F⍟0` is the compositional [identity](identity.md), `⊢`. Since `F⍟1` applies `F` and `F⍟0` doesn't, Repeat might be pronounced "if" or "conditional" when `𝔾` is boolean. -BQN's Repeat modifier has some extra functionality relative to the mathematical version. It allows a left argument, and some extensions to the right operand `𝔾`. As usual for 2-modifiers, `𝔾` is actually a function that applies to the arguments to give a result. The result can be a natural number as shown above, or a negative number to Undo (`⁼`) `𝔽`, or an array of values. +BQN's Repeat modifier has some extra functionality relative to the mathematical version. It allows a left argument, and some extensions to the right operand `𝔾`. As usual for 2-modifiers, `𝔾` is actually a function that applies to the arguments to give a result. The result can be a natural number as shown above, or a negative number to [Undo](undo.md) (`⁼`) `𝔽`, or an array of values. ## Left argument diff --git a/doc/transpose.md b/doc/transpose.md index c3d0807b..7f91ad2d 100644 --- a/doc/transpose.md +++ b/doc/transpose.md @@ -37,7 +37,7 @@ But, ignoring the whitespace and going in reading order, the argument and result ≍○<⟜⍉ ⥊˘ a322 -To exchange multiple axes, use the [Repeat](repeat.md) modifier. A negative power moves axes in the other direction, just like how [Rotate](reverse.md#rotate) handles negative left arguments. In particular, to move the last axis to the front, use Undo (as you might expect, this exactly inverts `⍉`). +To exchange multiple axes, use the [Repeat](repeat.md) modifier. A negative power moves axes in the other direction, just like how [Rotate](reverse.md#rotate) handles negative left arguments. In particular, to move the last axis to the front, use [Undo](undo.md) (as you might expect, this exactly inverts `⍉`). ≢ ⍉⍟3 a23456 diff --git a/doc/undo.md b/doc/undo.md new file mode 100644 index 00000000..e8f1605e --- /dev/null +++ b/doc/undo.md @@ -0,0 +1,56 @@ +*View this file with results and syntax highlighting [here](https://mlochbaum.github.io/BQN/doc/undo.html).* + +# Undo + +Oh no, you've deleted a function after spending half an hour writing it! Well… hopefully your editor can help with that. But maybe you'll be interested to hear that BQN can reverse the action of some functions—ones that *don't* lose information. This capability is also used by [Repeat](repeat.md) (`⍟`) to repeat a negative number of times. + + 2 ⌽ "abcde" + + 2 ⌽⁼ 2 ⌽ "abcde" + +Above is one example of Undo (`⁼`) in action. [Rotate](reverse.md) shifts the elements of a list left, and undoing shifts them to the right. BQN's ability to undo functions covers a lot of cases and can seem somewhat magical: + + (Fn ← -⟜1⌾(¯1⊸⊑)) "BQN" + + Fn⁼ Fn "BQN" + +Here it undoes a function to decrement the last character by incrementing that character. In part this is enabled by the clean design of BQN primitives, because better-behaved functions like those using structural Under are easier to invert. + +## The rules + +If `𝔽` can be inverted exactly, then Undo just does that. However, there are also some other functions that BQN inverts. For example, the squaring function `ט` has both a positive and a negative inverse, and yet: + + ט ¯3 + ט⁼ ט ¯3 # It's not the same! + +Don't worry, this isn't 'nam. Undo doesn't always satisfy `𝕩 ≡ 𝔽⁼ 𝔽 𝕩`, but it *does* obey `𝕩 ≡ 𝔽 𝔽⁼ 𝕩`. That is, it gives one possible argument that could have been passed to `𝔽`, just not necessarily the one that actually was. BQN is conservative in how it uses this freedom, so that it won't for example make up new elements entirely to find an inverse. It's usually used when there's an obvious or standard way to pick which of the possible values should be returned. + +In a BQN with floating-point numbers, computations are approximate, so the inverse is allowed to be approximate as well (any error should still be very small though). + + 6 - √⁼√6 + +## What's supported? + +For the full list, see [the specification](../spec/inferred.md#undo). An individual implementation might support a lot more functionality than is required, so if you're not concerned about portability just try out whatever function you're interested in. + +Arithmetic and simple combinators are usually invertible. A compound function that refers to its argument just once, like `6+⌽∘⍉`, can typically be undone, but one that uses the argument in two different ways, such as `⊢+⋆`, probably can't. + +A few notable inverses are the [logarithm](arithmetic.md#basic-arithmetic) `⋆⁼`, [un-Transpose](transpose.md) `⍉⁼`, and [Indices inverse](replicate.md#inverse) `/⁼`. [Enclose](enclose.md) inverse, `<⁼`, is an alternative to [First](pick.md#first) that requires its argument to be a unit. + +Structural functions like [Take](take.md) and [shifts](shift.md) that remove elements from `𝕩` can't be inverted, because given the result there's no way to know what the elements should be. However, there are two special cases that have inverses defined despite losing data: these are `⊣⁼` and `k⁼` where `k` is a constant (a data type, or `k˙`). For these, `𝕩` is required to [match](match.md) the always returned value `𝕨` or `k`, and this value is also used for the result—even though any result would be valid, as these functions ignore `𝕩`. + + 3 ⊣⁼ 4 + 3 ⊣⁼ 3 + +## Undo headers + +Undo headers are currently supported only by dzaima/BQN. + +Of course BQN will never be able to invert all the functions you could write (if it could you could earn a *lot* of bitcoins, among other feats). But it does recognize some [header](block.md#block-headers) forms that you can use to specify the inverse of a block function. BQN will trust you and won't verify the results your specified inverse gives. + + { + 𝕊𝕩: 𝕩÷𝕩+1 + 𝕊⁼𝕩: 𝕩÷𝕩-1 + } + +The above function could also be defined with the automatically invertible `1⊸+⌾÷`, but maybe there's a numerical reason to use the definition above. Like a normal header, an undo header reflects the normal use, but it includes `⁼` and possibly `˜` addition to the function and arguments. |
