aboutsummaryrefslogtreecommitdiff
path: root/docs/doc/tacit.html
diff options
context:
space:
mode:
authorMarshall Lochbaum <mwlochbaum@gmail.com>2022-06-11 16:55:33 -0400
committerMarshall Lochbaum <mwlochbaum@gmail.com>2022-06-11 16:55:33 -0400
commit383298b70274c5ac22eb2100aad7f6cd8eeca02d (patch)
tree87a3b0c2f05a8b1a284df79b6d9966bf8c0aab95 /docs/doc/tacit.html
parentf469c6f9bd4c9cf3c2b8ce93c3f2331cdcdd589a (diff)
Add discussions of the important tacit modifiers to tacit.md
Diffstat (limited to 'docs/doc/tacit.html')
-rw-r--r--docs/doc/tacit.html30
1 files changed, 24 insertions, 6 deletions
diff --git a/docs/doc/tacit.html b/docs/doc/tacit.html
index 51d0d82d..52a9eed3 100644
--- a/docs/doc/tacit.html
+++ b/docs/doc/tacit.html
@@ -1,11 +1,11 @@
<head>
<link href="../favicon.ico" rel="shortcut icon" type="image/x-icon"/>
<link href="../style.css" rel="stylesheet"/>
- <title>BQN: Tacit (point-free) programming</title>
+ <title>BQN: Tacit programming</title>
</head>
<div class="nav">(<a href="https://github.com/mlochbaum/BQN">github</a>) / <a href="../index.html">BQN</a> / <a href="index.html">doc</a></div>
-<h1 id="tacit-point-free-programming"><a class="header" href="#tacit-point-free-programming">Tacit (point-free) programming</a></h1>
-<p><a href="https://en.wikipedia.org/wiki/Tacit_programming">Tacit programming</a> (<a href="https://aplwiki.com/wiki/Tacit_programming">APL Wiki</a>) is a general term used to refer to ways to define functions that don't refer to arguments directly (say, with identifiers). Instead, tacit programs are built up by combining smaller functions together; we'll discuss the ways BQN offers to combine functions on this page. Since primitive functions like those returning the left (<code><span class='Function'>⊣</span></code>) and right (<code><span class='Function'>⊢</span></code>) arguments, and selection functions (<code><span class='Function'>⊏⊑</span></code>), are available as building blocks, tacit programming doesn't keep the programmer from pinpointing a specific part of the input, as the description might lead you to believe. Nonetheless, it has its limitations. In larger tacit programs, moving values to the right place is tedious and error-prone because of the lack of a convenient labelling mechanism, and important context tends to disappear in a sea of symbols.</p>
+<h1 id="tacit-programming"><a class="header" href="#tacit-programming">Tacit programming</a></h1>
+<p><a href="https://aplwiki.com/wiki/Tacit_programming">Tacit programming</a> (or &quot;point-free&quot; in some other languages) is a term for defining functions without referring to arguments directly, which in BQN means programming without <a href="block.html">blocks</a>. Instead, tacit programs are built up by combining smaller functions together; we'll discuss the ways BQN offers to combine functions on this page. Since primitive functions like those returning the left (<code><span class='Function'>⊣</span></code>) and right (<code><span class='Function'>⊢</span></code>) arguments, and selection functions (<code><span class='Function'>⊏⊑</span></code>), are available as building blocks, tacit programming doesn't keep the programmer from pinpointing a specific part of the input, as the description might lead you to believe. Nonetheless, it has its limitations. In larger tacit programs, moving values to the right place is tedious and error-prone because of the lack of a convenient labelling mechanism, and important context tends to disappear in a sea of symbols.</p>
<p>In smaller amounts—portions of a line—tacit programming can be the clearest way to express an idea, particularly when just one or two variables are used a few times. Consider the following three expressions to filter only the positive values from a list:</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=bCDihpAgMOKAvzXigL/CrzLigL8x4oC/wq8z4oC/wq80CgooMDxsKS9sCnsoMDzwnZWpKS/wnZWpfSBsCjDiirg84oq4LyBs">↗️</a><pre> <span class='Value'>l</span> <span class='Gets'>←</span> <span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>5</span><span class='Ligature'>‿</span><span class='Number'>¯2</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>¯3</span><span class='Ligature'>‿</span><span class='Number'>¯4</span>
@@ -16,11 +16,11 @@
<span class='Number'>0</span><span class='Modifier2'>⊸</span><span class='Function'>&lt;</span><span class='Modifier2'>⊸</span><span class='Function'>/</span> <span class='Value'>l</span>
⟨ 5 1 ⟩
</pre>
-<p>The first of these expressions is the most direct, but with the variable name buried inside, it can't be used on an intermediate value and its input will have to be named. The other two forms stand alone as functions, so they can easily be placed anywhere in a program, even as an operand. But with even the small amount of structure added by a BQN anonymous function, the second method has more organization than action! The third, tacit, version strips away most of the organizing syntax to leave us with the essential pieces <code><span class='Number'>0</span></code>, <code><span class='Function'>&lt;</span></code>, and <code><span class='Function'>/</span></code> joined by combinators. The explicit function uses <code><span class='Value'>𝕩</span></code> as a sort of pronoun (&quot;I want the elements of it where it's greater than zero&quot;), while the tacit one elides it (&quot;give me the elements greater than zero&quot;).</p>
-<p>The ability to easily combine tacit and &quot;explicit&quot; programming such as statements or anonymous functions, far from being only a way to mitigate the disadvantages of these two methods, brings new advantages that no single paradigm could accomplish. Purely tacit programming <em>requires</em> programs to use <em>no</em> local variable names, but partly tacit programming <em>allows</em> them to use <em>fewer</em> names. That means names can be used only for the parts of a program that represent clean, human-understandable concepts. Another possible strategic choice is to use the fact that variables in a tacit expression are expanded as it's formed but those inside a block aren't. So <code><span class='Function'>F</span><span class='Gets'>←</span><span class='Value'>a</span><span class='Modifier2'>⊸</span><span class='Function'>+</span></code> can be chosen to &quot;freeze&quot; the value of <code><span class='Value'>a</span></code> in <code><span class='Function'>F</span></code> without having to use an extra variable, while <code><span class='Function'>F</span><span class='Gets'>←</span><span class='Brace'>{</span><span class='Value'>a</span><span class='Function'>+</span><span class='Value'>𝕩</span><span class='Brace'>}</span></code> uses the current value of <code><span class='Value'>a</span></code> each time <code><span class='Function'>F</span></code> is called.</p>
+<p>The first of these expressions is the most direct, but with the variable name buried inside, it can't be used on an intermediate value—its input will have to be named. The other two forms stand alone as functions, so they can easily be placed anywhere in a program, even as an operand. But with even the small amount of structure added by a BQN anonymous function, the second method has more organization than action! The third, tacit, version strips away most of the organizing syntax to leave us with the essential pieces <code><span class='Number'>0</span></code>, <code><span class='Function'>&lt;</span></code>, and <code><span class='Function'>/</span></code> joined by combinators. The explicit function uses <code><span class='Value'>𝕩</span></code> as a sort of pronoun (&quot;I want the elements of it where it's greater than zero&quot;), while the tacit one elides it (&quot;give me the elements greater than zero&quot;).</p>
+<p>The ability to easily combine tacit and &quot;explicit&quot; programming in blocks isn't only a way to mitigate the disadvantages of these two methods, but brings new advantages that no single paradigm could accomplish. Purely tacit programming <em>requires</em> programs to use <em>no</em> named local variables, but partly tacit programming <em>allows</em> them to use <em>fewer</em> names. That means names can be used only for the parts of a program that represent clean, human-understandable concepts. Another possible strategic choice is to use the fact that variables in a tacit expression are expanded as it's formed but those inside a block aren't. So <code><span class='Function'>F</span><span class='Gets'>←</span><span class='Value'>a</span><span class='Modifier2'>⊸</span><span class='Function'>+</span></code> can be chosen to &quot;freeze&quot; the value of <code><span class='Value'>a</span></code> in <code><span class='Function'>F</span></code> without having to use an extra variable, while <code><span class='Function'>F</span><span class='Gets'>←</span><span class='Brace'>{</span><span class='Value'>a</span><span class='Function'>+</span><span class='Value'>𝕩</span><span class='Brace'>}</span></code> uses the current value of <code><span class='Value'>a</span></code> each time <code><span class='Function'>F</span></code> is called.</p>
<p>The rest of this page describes BQN's tacit programming facilities. Deciding when to use them is a matter of taste, and experience.</p>
<h2 id="trains"><a class="header" href="#trains">Trains</a></h2>
-<p>In modern APL and its relatives, the backbone of tacit infrastructure is the <em>function train</em>. Trains can take some practice to understand and use well, so they're described in more depth on <a href="train.html">a dedicated page</a>. The idea of trains is that you can &quot;apply&quot; a function to other functions, forming a composed function where it will actually apply to their results. So a typical use is to pair two functions as shown below: the pair <code><span class='Function'>»</span><span class='Ligature'>‿</span><span class='Function'>«</span></code> is never formed, but the result of applying <code><span class='Function'>T</span></code> is a pair.</p>
+<p>In modern APL and its relatives, the backbone of tacit infrastructure is the <em>function train</em>. Trains can take some practice to understand and use well, so they're described in more depth on <a href="train.html">a dedicated page</a>. The idea of trains is that you can &quot;apply&quot; a function to other functions, forming a composed function where it will actually apply to their results. So a typical use is to <a href="pair.html">pair</a> two functions as shown below: the pair <code><span class='Function'>»</span><span class='Ligature'>‿</span><span class='Function'>«</span></code> is never formed, but the result of applying <code><span class='Function'>T</span></code> is a pair.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=VCDihpAgwrsg4ouIIMKrICAgICMgUGFpciBib3RoIHNoaWZ0IGZ1bmN0aW9ucwpUICAgICAgICAgICAgIyBOb3RoaW5nIGhhcHBlbnMgeWV0Li4uCgpUICJhYmMiICAgICAgIyBOb3cgaXQgZm9ybXMgYSBwYWlyCgonWCcgVCAiYWJjIiAgIyBFYWNoIHNoaWZ0IGdldHMgYm90aCBhcmd1bWVudHM=">↗️</a><pre> <span class='Function'>T</span> <span class='Gets'>←</span> <span class='Function'>»</span> <span class='Function'>⋈</span> <span class='Function'>«</span> <span class='Comment'># Pair both shift functions
</span> <span class='Function'>T</span> <span class='Comment'># Nothing happens yet...
</span>»⋈«
@@ -41,6 +41,7 @@
</pre>
<p>The functions <code><span class='Function'>⊣⊢</span></code> are as simple as they come, but are discussed quite a bit on <a href="identity.html">their own page</a>. A definition is that <code><span class='Function'>⊢</span></code> is <code><span class='Brace'>{</span><span class='Value'>𝕩</span><span class='Brace'>}</span></code> and <code><span class='Function'>⊣</span></code> is <code><span class='Brace'>{</span><span class='Value'>𝕩</span><span class='Head'>;</span><span class='Value'>𝕨</span><span class='Brace'>}</span></code>, so that <code><span class='Function'>⊢</span></code> returns its right argument, and <code><span class='Function'>⊣</span></code> returns its left argument but will settle for the right one if there's just one.</p>
<h2 id="combinators"><a class="header" href="#combinators">Combinators</a></h2>
+<p>Here's a table of all the typical tacit combinators for reference. The yellow lines show which values are passed to functions in order to evaluate each combinator: we start with values <code><span class='Value'>𝕩</span></code> and maybe <code><span class='Value'>𝕨</span></code> at the bottom, and the result is the single value that comes out at the top, after applying the functions.</p>
<svg viewBox='0 0 850 530'>
<g font-size='20px' text-anchor='middle' transform='translate(145,20)'>
<rect class='code' stroke-width='1' rx='12' x='-120.4' y='1' width='240.8' height='205'/>
@@ -211,3 +212,20 @@
</g>
</svg>
+<p>Each of these combinators has its role to play in tacit code. To start with, <a href="constant.html">Constant</a> (<code><span class='Modifier'>˙</span></code>) is the subtlest one, because it's used everywhere, in a sense, but rarely needs to be made explicit. This is because data values already behave like constant functions. For example, the train <code><span class='Number'>4</span><span class='Function'>+×</span></code> might more precisely be written <code><span class='Number'>4</span><span class='Modifier'>˙</span><span class='Function'>+×</span></code>, but trains conveniently handle this part already. However, it <em>is</em> often used at the end of a train, to force the last part into a function role. <code><span class='Function'>×-</span><span class='Number'>1</span></code> isn't a train—it just evaluates to ¯1. But <code><span class='Function'>×-</span><span class='Number'>1</span><span class='Modifier'>˙</span></code> is the train that multiplies its arguments and subtracts 1.</p>
+<p>Using <a href="swap.html">Self and Swap</a> is pretty straightforward. Swap in particular is often useful if your arguments happen to be the wrong way around. For example, <code><span class='Function'>⌊</span><span class='Modifier2'>∘</span><span class='Function'>÷</span><span class='Modifier'>˜</span><span class='Function'>⋈|</span></code> gives you the integer part and remainder when dividing <code><span class='Value'>𝕨</span></code> by <code><span class='Value'>𝕩</span></code>. For compatibility with math, <code><span class='Function'>÷</span></code> is &quot;backwards&quot; from a BQN perspective, but <code><span class='Modifier'>˜</span></code> will fix that right up. And while we're at it, let's note that <a href="compose.html#atop">Atop</a> is just another way to write a 2-train, so that <code><span class='Function'>⌊</span><span class='Modifier2'>∘</span><span class='Function'>÷</span><span class='Modifier'>˜</span></code> is <code><span class='Paren'>(</span><span class='Function'>⌊÷</span><span class='Paren'>)</span><span class='Modifier'>˜</span></code> but avoids the parentheses.</p>
+<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MyAo4oyK4oiYw7fLnOKLiHwpIDEzICAjIDEzID0gMSszw5c0">↗️</a><pre> <span class='Number'>3</span> <span class='Paren'>(</span><span class='Function'>⌊</span><span class='Modifier2'>∘</span><span class='Function'>÷</span><span class='Modifier'>˜</span><span class='Function'>⋈|</span><span class='Paren'>)</span> <span class='Number'>13</span> <span class='Comment'># 13 = 1+3×4
+</span>⟨ 4 1 ⟩
+</pre>
+<p><a href="hook.html">Before and After</a> are tacit powerhouses. Yes, <code><span class='Function'>F</span><span class='Modifier2'>⊸</span><span class='Function'>G</span></code> is just <code><span class='Function'>F</span><span class='Modifier2'>∘</span><span class='Function'>⊣G⊢</span></code> and <code><span class='Function'>G</span><span class='Modifier2'>⟜</span><span class='Function'>F</span></code> is <code><span class='Function'>⊣G</span> <span class='Function'>F</span><span class='Modifier2'>∘</span><span class='Function'>⊢</span></code>. But the symmetric symbols make these cases a lot easier to read and manipulate as a programmer (just remember, pointy-side function is applied first!). It's common to write big tacit functions with the pattern <code><span class='Paren'>(</span><span class='Value'>…</span><span class='Paren'>)</span><span class='Modifier2'>⊸</span><span class='Function'>Fn</span> <span class='Value'>𝕩</span></code>, so a lot of processing is applied to <code><span class='Value'>𝕩</span></code> and then the result of this is passed along with <code><span class='Value'>𝕩</span></code> to <code><span class='Function'>Fn</span></code>. And of course binds like <code><span class='Function'>÷</span><span class='Modifier2'>⟜</span><span class='Number'>2</span></code>, dividing by 2, or <code><span class='Number'>¯1</span><span class='Modifier2'>⊸</span><span class='Function'>»</span></code>, shifting in a ¯1, are very often helpful.</p>
+<p><a href="compose.html#over">Over</a> is a bit weirder, and you'll have to learn when to recognize this pattern (of course, a repeated function is a strong hint). Perhaps you'd compare the first element of two lists with <code><span class='Function'>≡</span><span class='Modifier2'>○</span><span class='Function'>⊑</span></code>, for example.</p>
+<h2 id="conditionals"><a class="header" href="#conditionals">Conditionals</a></h2>
+<p>There are two main ways to perform conditional logic in tacit code. Although, a note first: for arithmetic, array operations that use booleans as numbers can often fill in for conditionals, and they're a lot faster. So, to replace every space character with a hyphen, try this function that multiplies the difference between those characters by a mask that's 1 where <code><span class='Value'>𝕩</span></code> is a space, then subtracts that back from <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=IiAtIiAo4oqiLS3CtOKImOKKo8OX4oqR4oq4PSkgImFiIGNkZSAgZiAi">↗️</a><pre> <span class='String'>&quot; -&quot;</span> <span class='Paren'>(</span><span class='Function'>⊢--</span><span class='Modifier'>´</span><span class='Modifier2'>∘</span><span class='Function'>⊣×⊑</span><span class='Modifier2'>⊸</span><span class='Function'>=</span><span class='Paren'>)</span> <span class='String'>&quot;ab cde f &quot;</span>
+"ab-cde--f-"
+</pre>
+<p>The <a href="repeat.html">Repeat</a> (<code><span class='Modifier2'>⍟</span></code>) modifier makes a nice &quot;if&quot; conditional. <code><span class='Function'>F</span><span class='Modifier2'>⍟</span><span class='Function'>G</span></code>, assuming the result of <code><span class='Function'>G</span></code> is a boolean, is equivalent to <code><span class='Brace'>{</span><span class='Value'>𝕨</span><span class='Function'>G</span><span class='Value'>𝕩</span><span class='Head'>?</span><span class='Value'>𝕨</span><span class='Function'>F</span><span class='Value'>𝕩</span><span class='Head'>;</span><span class='Value'>𝕩</span><span class='Brace'>}</span></code>. Note how <code><span class='Value'>𝕨</span></code> gets passed in to both functions. Often you'll want <code><span class='Function'>𝔽</span></code> to apply to <code><span class='Value'>𝕩</span></code> only, and in this case, you need to make this explicit with <code><span class='Function'>⊢</span></code> or similar.</p>
+<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MyAoMsO3y5ziiqIp4o2fPCA3ICAjIGhhbHZlIPCdlakgaWYgZ3JlYXRlciB0aGFuIPCdlag=">↗️</a><pre> <span class='Number'>3</span> <span class='Paren'>(</span><span class='Number'>2</span><span class='Function'>÷</span><span class='Modifier'>˜</span><span class='Function'>⊢</span><span class='Paren'>)</span><span class='Modifier2'>⍟</span><span class='Function'>&lt;</span> <span class='Number'>7</span> <span class='Comment'># halve 𝕩 if greater than 𝕨
+</span>3.5
+</pre>
+<p>For more complicated &quot;if-else&quot; or &quot;select&quot; type conditionals, use <a href="choose.html">Choose</a> (<code><span class='Modifier2'>◶</span></code>). Watch for ordering here: <code><span class='Function'>F</span><span class='Modifier2'>◶</span><span class='Bracket'>⟨</span><span class='Function'>G0</span><span class='Separator'>,</span><span class='Function'>G1</span><span class='Bracket'>⟩</span></code> puts the two parts in the opposite order to Repeat, and list element 1 comes after element 0 even though it might seem more intuitive for the &quot;true&quot; value to come first.</p>