diff options
Diffstat (limited to 'docs/doc/undo.html')
| -rw-r--r-- | docs/doc/undo.html | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/docs/doc/undo.html b/docs/doc/undo.html new file mode 100644 index 00000000..973a942a --- /dev/null +++ b/docs/doc/undo.html @@ -0,0 +1,53 @@ +<head> + <link href="../favicon.ico" rel="shortcut icon" type="image/x-icon"/> + <link href="../style.css" rel="stylesheet"/> + <title>BQN: Undo</title> +</head> +<div class="nav"><a href="https://github.com/mlochbaum/BQN">BQN</a> / <a href="../index.html">main</a> / <a href="index.html">doc</a></div> +<h1 id="undo">Undo</h1> +<p>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 <em>don't</em> lose information. This capability is also used by <a href="repeat.html">Repeat</a> (<code><span class='Modifier2'>⍟</span></code>) to repeat a negative number of times.</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MiDijL0gImFiY2RlIgoKMiDijL3igbwgMiDijL0gImFiY2RlIg==">↗️</a><pre> <span class='Number'>2</span> <span class='Function'>⌽</span> <span class='String'>"abcde"</span> +"cdeab" + + <span class='Number'>2</span> <span class='Function'>⌽</span><span class='Modifier'>⁼</span> <span class='Number'>2</span> <span class='Function'>⌽</span> <span class='String'>"abcde"</span> +"abcde" +</pre> +<p>Above is one example of Undo (<code><span class='Modifier'>⁼</span></code>) in action. <a href="reverse.html">Rotate</a> 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:</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=KEZuIOKGkCAt4p+cMeKMvijCrzHiirjiipEpKSAiQlFOIgoKRm7igbwgRm4gIkJRTiI=">↗️</a><pre> <span class='Paren'>(</span><span class='Function'>Fn</span> <span class='Gets'>←</span> <span class='Function'>-</span><span class='Modifier2'>⟜</span><span class='Number'>1</span><span class='Modifier2'>⌾</span><span class='Paren'>(</span><span class='Number'>¯1</span><span class='Modifier2'>⊸</span><span class='Function'>⊑</span><span class='Paren'>))</span> <span class='String'>"BQN"</span> +"BQM" + + <span class='Function'>Fn</span><span class='Modifier'>⁼</span> <span class='Function'>Fn</span> <span class='String'>"BQN"</span> +"BQN" +</pre> +<p>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.</p> +<h2 id="the-rules">The rules</h2> +<p>If <code><span class='Function'>𝔽</span></code> can be inverted exactly, then Undo just does that. However, there are also some other functions that BQN inverts. For example, the squaring function <code><span class='Function'>×</span><span class='Modifier'>˜</span></code> has both a positive and a negative inverse, and yet:</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=w5fLnCDCrzMKw5fLnOKBvCDDl8ucIMKvMyAgIyBJdCdzIG5vdCB0aGUgc2FtZSE=">↗️</a><pre> <span class='Function'>×</span><span class='Modifier'>˜</span> <span class='Number'>¯3</span> +9 + <span class='Function'>×</span><span class='Modifier'>˜⁼</span> <span class='Function'>×</span><span class='Modifier'>˜</span> <span class='Number'>¯3</span> <span class='Comment'># It's not the same! +</span>3 +</pre> +<p>Don't worry, this isn't 'nam. Undo doesn't always satisfy <code><span class='Value'>𝕩</span> <span class='Function'>≡</span> <span class='Function'>𝔽</span><span class='Modifier'>⁼</span> <span class='Function'>𝔽</span> <span class='Value'>𝕩</span></code>, but it <em>does</em> obey <code><span class='Value'>𝕩</span> <span class='Function'>≡</span> <span class='Function'>𝔽</span> <span class='Function'>𝔽</span><span class='Modifier'>⁼</span> <span class='Value'>𝕩</span></code>. That is, it gives one possible argument that could have been passed to <code><span class='Function'>𝔽</span></code>, 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.</p> +<p>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).</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=NiAtIOKImuKBvOKImjY=">↗️</a><pre> <span class='Number'>6</span> <span class='Function'>-</span> <span class='Function'>√</span><span class='Modifier'>⁼</span><span class='Function'>√</span><span class='Number'>6</span> +8.881784197001252e¯16 +</pre> +<h2 id="whats-supported">What's supported?</h2> +<p>For the full list, see <a href="../spec/inferred.html#undo">the specification</a>. 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.</p> +<p>Arithmetic and simple combinators are usually invertible. A compound function that refers to its argument just once, like <code><span class='Number'>6</span><span class='Function'>+⌽</span><span class='Modifier2'>∘</span><span class='Function'>⍉</span></code>, can typically be undone, but one that uses the argument in two different ways, such as <code><span class='Function'>⊢+⋆</span></code>, probably can't.</p> +<p>A few notable inverses are the <a href="arithmetic.html#basic-arithmetic">logarithm</a> <code><span class='Function'>⋆</span><span class='Modifier'>⁼</span></code>, <a href="transpose.html">un-Transpose</a> <code><span class='Function'>⍉</span><span class='Modifier'>⁼</span></code>, and <a href="replicate.html#inverse">Indices inverse</a> <code><span class='Function'>/</span><span class='Modifier'>⁼</span></code>. <a href="enclose.html">Enclose</a> inverse, <code><span class='Function'><</span><span class='Modifier'>⁼</span></code>, is an alternative to <a href="pick.html#first">First</a> that requires its argument to be a unit.</p> +<p>Structural functions like <a href="take.html">Take</a> and <a href="shift.html">shifts</a> that remove elements from <code><span class='Value'>𝕩</span></code> 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 <code><span class='Function'>⊣</span><span class='Modifier'>⁼</span></code> and <code><span class='Value'>k</span><span class='Modifier'>⁼</span></code> where <code><span class='Value'>k</span></code> is a constant (a data type, or <code><span class='Value'>k</span><span class='Modifier'>˙</span></code>). For these, <code><span class='Value'>𝕩</span></code> is required to <a href="match.html">match</a> the always returned value <code><span class='Value'>𝕨</span></code> or <code><span class='Value'>k</span></code>, and this value is also used for the result—even though any result would be valid, as these functions ignore <code><span class='Value'>𝕩</span></code>.</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MyDiiqPigbwgNAozIOKKo+KBvCAz">↗️</a><pre> <span class='Number'>3</span> <span class='Function'>⊣</span><span class='Modifier'>⁼</span> <span class='Number'>4</span> +ERROR + <span class='Number'>3</span> <span class='Function'>⊣</span><span class='Modifier'>⁼</span> <span class='Number'>3</span> +3 +</pre> +<h2 id="undo-headers">Undo headers</h2> +<p>Undo headers are currently supported only by dzaima/BQN.</p> +<p>Of course BQN will never be able to invert all the functions you could write (if it could you could earn a <em>lot</em> of bitcoins, among other feats). But it does recognize some <a href="block.html#block-headers">header</a> 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.</p> +<pre><span class='Brace'>{</span> + <span class='Function'>𝕊</span><span class='Value'>𝕩:</span> <span class='Value'>𝕩</span><span class='Function'>÷</span><span class='Value'>𝕩</span><span class='Function'>+</span><span class='Number'>1</span> + <span class='Function'>𝕊</span><span class='Modifier'>⁼</span><span class='Value'>𝕩:</span> <span class='Value'>𝕩</span><span class='Function'>÷</span><span class='Value'>𝕩</span><span class='Function'>-</span><span class='Number'>1</span> +<span class='Brace'>}</span> +</pre> +<p>The above function could also be defined with the automatically invertible <code><span class='Number'>1</span><span class='Modifier2'>⊸</span><span class='Function'>+</span><span class='Modifier2'>⌾</span><span class='Function'>÷</span></code>, 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 <code><span class='Modifier'>⁼</span></code> and possibly <code><span class='Modifier'>˜</span></code> addition to the function and arguments.</p> |
