diff options
Diffstat (limited to 'docs/doc/expression.html')
| -rw-r--r-- | docs/doc/expression.html | 50 |
1 files changed, 27 insertions, 23 deletions
diff --git a/docs/doc/expression.html b/docs/doc/expression.html index 067eed73..81f2a048 100644 --- a/docs/doc/expression.html +++ b/docs/doc/expression.html @@ -8,7 +8,7 @@ <p>BQN expressions are the part of <a href="syntax.html">syntax</a> that describes computations to perform. Programs are mainly made up of expressions with a little organizing material like <a href="block.html">blocks</a> and <a href="namespace.html">namespaces</a> around them. This page explains how functions, modifiers, and assignment combine with their inputs. It doesn't describe <a href="syntax.html#constants">constant</a> and <a href="arrayrepr.html#array-literals">array</a> literals, which each form a single subject for grammatical purposes.</p> <p>The <a href="../tutorial/expression.html">first tutorial</a> also covers how to build and read BQN expressions.</p> <h2 id="overview"><a class="header" href="#overview">Overview</a></h2> -<p>BQN expressions consist of subjects, functions, and modifiers arranged in sequence, with parentheses to group parts into subexpressions. Assignment arrows <code><span class='Gets'>←</span></code> and <code><span class='Gets'>↩</span></code> can also be present and mostly behave similar to functions. Functions can be applied to subjects or grouped into trains, while modifiers can be applied to subjects or functions. The most important kinds of application are:</p> +<p>BQN expressions consist of subjects, functions, and modifiers arranged in sequence, with parentheses to group parts into subexpressions. Assignment arrows <code><span class='Gets'>←</span></code> and <code><span class='Gets'>↩</span></code> can also be present and mostly act like functions. <a href="ops.html#functions">Functions</a> can be applied to subjects or grouped into <a href="train.html">trains</a>, while <a href="ops.html#modifiers">modifiers</a> can be applied to subjects or functions. The most important kinds of application are:</p> <table> <thead> <tr> @@ -55,11 +55,9 @@ </tr> </tbody> </table> -<p>The four roles (subject, function, two kinds of modifier) describe expressions, not values. When an expression is evaluated, the value's <a href="types.html">type</a> doesn't have to correspond to its role, and can even change from one evaluation to another. An expression's role is determined entirely by its source code, so it's fixed.</p> +<p>The four <a href="#syntactic-role">roles</a> (subject, function, two kinds of modifier) describe expressions, not values. When an expression is evaluated, the value's <a href="types.html">type</a> doesn't have to correspond to its role, and can even change from one evaluation to another. An expression's role is determined entirely by its source code, so it's fixed.</p> <p>In the table, <code><span class='Head'>?</span></code> marks an optional left argument. If there isn't a value in that position, or it's <a href="#nothing">Nothing</a> (<code><span class='Nothing'>·</span></code>), the middle function will be called with only one argument.</p> <p>If you're comfortable reading <a href="https://en.wikipedia.org/wiki/Backus%E2%80%93Naur_form">BNF</a> and want to understand things in more detail than described below, you might check the <a href="../spec/grammar.html">grammar specification</a> as well.</p> -<h2 id="parentheses"><a class="header" href="#parentheses">Parentheses</a></h2> -<p>As in most programming languages, parentheses <code><span class='Paren'>()</span></code> are for grouping. The code inside a balanced set of parentheses is a single expression, which produces one value to be used by the expression that contains it—for example, in <code><span class='Paren'>(</span><span class='Number'>2</span><span class='Function'>×</span><span class='Number'>3</span><span class='Paren'>)</span><span class='Function'>+</span><span class='Number'>4</span></code>, <code><span class='Number'>2</span><span class='Function'>×</span><span class='Number'>3</span></code> is a subexpression evaluating to <code><span class='Number'>6</span></code>, so that larger expression is equivalent to <code><span class='Number'>6</span><span class='Function'>+</span><span class='Number'>4</span></code>. The syntactic role of a set of parentheses is also the same as that of the expression inside.</p> <h2 id="syntactic-role"><a class="header" href="#syntactic-role">Syntactic role</a></h2> <p><em>This issue is approached from a different angle in <a href="context.html">Context free grammar</a>.</em></p> <p>In APL, the way one part of an expression interacts with others is determined by its value. That means that to parse an expression, in general you would have to evaluate that part, get a value, check its type, and then figure out how it fits in with the rest of the expression. This is a lot of work. BQN changes things so that you can determine how to parse an expression just by looking at its source code. But because it still needs to support expressions that can evaluate to more than one possible <a href="types.html">type</a>, BQN has to introduce a new and independent concept, called <strong>syntactic role</strong>, in order to support APL-like expressions.</p> @@ -72,7 +70,7 @@ ┘ </pre> <h3 id="role-spellings"><a class="header" href="#role-spellings">Role spellings</a></h3> -<p>The four roles are <strong>subject</strong>, <strong>function</strong>, <strong>1-modifier</strong>, and <strong>2-modifier</strong>, as shown in the table below. Each type has an associated role (with non-operation types all corresponding to subjects), and the value of an expression will often have a matching type, but it doesn't have to.</p> +<p>The four roles are <strong>subject</strong>, <strong>function</strong>, <strong>1-modifier</strong>, and <strong>2-modifier</strong>, as shown in the table below. Each type has an associated role (non-operation types all correspond to subjects), and the value of an expression will often have a type that fits the role, but it doesn't have to.</p> <table> <thead> <tr> @@ -104,10 +102,12 @@ </tr> </tbody> </table> -<p>Primitive tokens, since they have a fixed value, always have a role that matches their type. They are functions, unless they fall into one of the two modifier patterns. 1-modifiers have superscript glyphs, and 2-modifiers have glyphs with an unbroken circle—that is, one without a line through it, excluding functions <code><span class='Function'>⌽</span></code> and <code><span class='Function'>⍉</span></code>.</p> +<p>Primitive tokens, since they have a fixed value, always have a role that matches their type. They're functions by default, as the modifiers have glyphs that fit specific patterns. 1-modifiers have superscript glyphs, and 2-modifiers have glyphs with an unbroken circle—that is, one without a line through it, excluding functions <code><span class='Function'>⌽</span></code> and <code><span class='Function'>⍉</span></code>.</p> <p>Variable names can be written in any case and with underscores added, and these changes don't affect what <a href="lexical.html">identifier</a> the name refers to. <code><span class='Value'>ab</span></code>, <code><span class='Value'>aB</span></code>, <code><span class='Function'>AB</span></code>, and <code><span class='Modifier2'>_a_B_</span></code> are all the same variable. However, the spelling—specifically the first and last characters—determine the variable's role. A lowercase first letter indicates a subject, and an uppercase first letter makes it a function. A leading underscore (regardless of the following character) indicates a 1-modifier, and both leading and trailing underscores makes a 2-modifier.</p> -<p>Besides these, character, string, and <a href="arrayrepr.html#array-literals">array literals</a> always have a subject role, and the role of a <a href="block.html">block</a> is determined by its type, which depends either on the header it has or which special variables it uses.</p> -<p>The role of a compound expression, formed by applying an operation to some inputs, depends on the operation applied. This system is discussed in the remaining sections below.</p> +<p>Besides these, character, string, and <a href="arrayrepr.html#array-literals">array literals</a> always have a subject role, and the role of a <a href="block.html">block</a> is determined by its type, which depends either on the header it has or which special variables it uses. If headerless, a block is a subject if it has no special names, but a <code><span class='Value'>𝕨</span></code> or <code><span class='Value'>𝕩</span></code> makes it at least a function, an <code><span class='Function'>𝔽</span></code> makes it a 1- or 2-modifier, and a <code><span class='Function'>𝔾</span></code> always makes it a 2-modifier.</p> +<p>The role of a compound expression, formed by applying an operation to some inputs, depends on the operation applied. This system is covered in the remaining sections below.</p> +<h2 id="parentheses"><a class="header" href="#parentheses">Parentheses</a></h2> +<p>As in most programming languages, parentheses <code><span class='Paren'>()</span></code> are for grouping. The code inside a balanced set of parentheses is a single expression, which produces one value to be used by the expression that contains it—for example, in <code><span class='Paren'>(</span><span class='Number'>2</span><span class='Function'>×</span><span class='Number'>3</span><span class='Paren'>)</span><span class='Function'>+</span><span class='Number'>4</span></code>, <code><span class='Number'>2</span><span class='Function'>×</span><span class='Number'>3</span></code> is a subexpression evaluating to <code><span class='Number'>6</span></code>, so that larger expression is equivalent to <code><span class='Number'>6</span><span class='Function'>+</span><span class='Number'>4</span></code>. The syntactic role of a set of parentheses is also the same as that of the expression inside.</p> <h2 id="nothing"><a class="header" href="#nothing">Nothing</a></h2> <p>The character <code><span class='Nothing'>·</span></code> is called Nothing. While it can be easier to think of it as a value, it can't be passed around in variables, and so can also be interpreted as an element of syntax. The special name <code><span class='Value'>𝕨</span></code> also functions as Nothing if the block that contains it is called with one argument (the uppercase spelling <code><span class='Function'>𝕎</span></code> doesn't, but instead immediately causes an error). Both <code><span class='Nothing'>·</span></code> and <code><span class='Value'>𝕨</span></code> have a subject role.</p> <p>The following rules apply to Nothing:</p> @@ -115,10 +115,10 @@ <li>If it's the left argument in a function call, the function is called with no left argument.</li> <li>If it's the right argument, the function isn't called, and "returns" Nothing.</li> </ul> -<p>For example, the expression <code><span class='Paren'>(</span><span class='Function'>F</span> <span class='Number'>2</span> <span class='Function'>G</span> <span class='Nothing'>·</span><span class='Paren'>)</span> <span class='Function'>H</span> <span class='Function'>I</span> <span class='Value'>j</span></code> is equivalent to <code><span class='Function'>H</span> <span class='Function'>I</span> <span class='Value'>j</span></code>. But functions and arguments that would be discarded by the second rule are still evaluated, so that for example <code><span class='Paren'>(</span><span class='Value'>a</span><span class='Function'>+</span><span class='Gets'>↩</span><span class='Number'>1</span><span class='Paren'>)</span> <span class='Function'>F</span> <span class='Nothing'>·</span></code> increments <code><span class='Value'>a</span></code> when run.</p> -<p>Nothing can only be used as an argument to a function, or the left argument in a train (it can't be the right argument in a train because a train ends with a function by definition). In another position where a subject could appear, like as an operand or in a list, it causes an error: either at compile time, for <code><span class='Nothing'>·</span></code>, or when the function is called with no left argument, for <code><span class='Value'>𝕨</span></code>.</p> +<p>For example, the expression <code><span class='Paren'>(</span><span class='Function'>F</span> <span class='Number'>2</span> <span class='Function'>G</span> <span class='Nothing'>·</span><span class='Paren'>)</span> <span class='Function'>H</span> <span class='Function'>I</span> <span class='Value'>j</span></code> is equivalent to <code><span class='Function'>H</span> <span class='Function'>I</span> <span class='Value'>j</span></code>. But functions and arguments that will be discarded by the second rule are still evaluated, so that for example <code><span class='Paren'>(</span><span class='Value'>a</span><span class='Function'>+</span><span class='Gets'>↩</span><span class='Number'>1</span><span class='Paren'>)</span> <span class='Function'>F</span> <span class='Nothing'>·</span></code> increments <code><span class='Value'>a</span></code> when run.</p> +<p>Nothing can only be used as an argument to a function, or the left argument in a train (it can't be the right—a train ends with a function by definition). In another position where a subject could appear, like as an operand or in a list, it causes an error: either at compile time, for <code><span class='Nothing'>·</span></code>, or when the function is called with no left argument, for <code><span class='Value'>𝕨</span></code>.</p> <h2 id="kinds-of-application"><a class="header" href="#kinds-of-application">Kinds of application</a></h2> -<p>Here is a table of the modifier and function application rules:</p> +<p>Here is a table of the <a href="ops.html">function and modifier</a> application rules:</p> <table> <thead> <tr> @@ -174,15 +174,20 @@ </tr> </tbody> </table> -<p>A function with an asterisk indicates that a subject can also be used. Since the role doesn't exist after parsing, function and subject spellings are indistinguishable in these positions. Modifier applications bind more tightly than functions, and associate left-to-right while functions associate right-to-left.</p> +<p>An asterisk <code><span class='Value'>*</span></code> indicates that a subject can also be used. Since the role doesn't exist after parsing, function and subject spellings are indistinguishable in these positions. Modifier applications bind more tightly than functions, and associate left-to-right while functions associate right-to-left.</p> <h2 id="assignment"><a class="header" href="#assignment">Assignment</a></h2> -<p>Another element that can be included in expressions is assignment, which is written with <code><span class='Gets'>←</span></code> to <em>define</em> (also called "declare" in many other languages) a variable and <code><span class='Gets'>↩</span></code> to <em>change</em> its definition. A variable can only be defined once within a <a href="lexical.html">scope</a>, and can only be changed if it has already been defined. However, it can be shadowed, meaning that it is defined again in an inner scope even though it has a definition in an outer scope already.</p> +<p>Expressions may also include assignment, which is written with <code><span class='Gets'>←</span></code> or <code><span class='Gets'>⇐</span></code> to <em>define</em> (similar to "declare" in many other languages) a variable and <code><span class='Gets'>↩</span></code> to <em>change</em> its definition. Assignment has a left-hand side (<code><span class='Value'>name</span></code> below) which is usually a variable name, and a right-hand side (<code><span class='Function'>↕</span><span class='Number'>4</span></code>) which can be any expression. The roles of the two sides have to match. It sets the value of the variable to be the result of the expression.</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=bmFtZSDihpAg4oaVNApuYW1l">↗️</a><pre> <span class='Value'>name</span> <span class='Gets'>←</span> <span class='Function'>↕</span><span class='Number'>4</span> + <span class='Value'>name</span> +⟨ 0 1 2 3 ⟩ +</pre> +<p>A variable can only be defined once within a <a href="lexical.html">scope</a>, and can only be changed if it has already been defined. However, it can be shadowed, meaning that an inner scope can define it even if it has a definition in an outer scope already.</p> <a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=eOKGkDEg4ouEIHt44oaQMiDii4QgeOKGqTMg4ouEIHh9Cng=">↗️</a><pre> <span class='Value'>x</span><span class='Gets'>←</span><span class='Number'>1</span> <span class='Separator'>⋄</span> <span class='Brace'>{</span><span class='Value'>x</span><span class='Gets'>←</span><span class='Number'>2</span> <span class='Separator'>⋄</span> <span class='Value'>x</span><span class='Gets'>↩</span><span class='Number'>3</span> <span class='Separator'>⋄</span> <span class='Value'>x</span><span class='Brace'>}</span> 3 <span class='Value'>x</span> 1 </pre> -<p>Assignment can be used inline in an expression, and its result is always the value being assigned. The role of the identifier used must match the value being assigned.</p> +<p>Assignment can be used inline in an expression, and its result is always the new value of the assignment target. Function or modifier assignment must be parenthesized, while subject assignment doesn't have to be: in a subject expression, assignment arrows have the same precedence as functions.</p> <a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MsOXYeKGkChOZWfihpAtKTMKYQ==">↗️</a><pre> <span class='Number'>2</span><span class='Function'>×</span><span class='Value'>a</span><span class='Gets'>←</span><span class='Paren'>(</span><span class='Function'>Neg</span><span class='Gets'>←</span><span class='Function'>-</span><span class='Paren'>)</span><span class='Number'>3</span> ¯6 <span class='Value'>a</span> @@ -218,7 +223,7 @@ <span class='Value'>s</span> ⟨ 0 1 2 3 ⟩ </pre> -<p>Array destructuring using <code><span class='Bracket'>[]</span></code> is also possible: it's equivalent to splitting the right-hand side with <code><span class='Function'><</span><span class='Modifier'>˘</span></code> and then applying list destructuring.</p> +<p>Array destructuring using <a href="arrayrepr.html#high-rank-arrays">array notation</a> <code><span class='Bracket'>[]</span></code> is also possible: it's equivalent to splitting the right-hand side with <code><span class='Function'><</span><span class='Modifier'>˘</span></code> and then applying list destructuring.</p> <a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=W3QsdV0g4oaQIOKGlTLigL8zCgp1">↗️</a><pre> <span class='Bracket'>[</span><span class='Value'>t</span><span class='Separator'>,</span><span class='Value'>u</span><span class='Bracket'>]</span> <span class='Gets'>←</span> <span class='Function'>↕</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>3</span> ┌─ ╵ ⟨ 0 0 ⟩ ⟨ 0 1 ⟩ ⟨ 0 2 ⟩ @@ -228,7 +233,7 @@ <span class='Value'>u</span> ⟨ ⟨ 1 0 ⟩ ⟨ 1 1 ⟩ ⟨ 1 2 ⟩ ⟩ </pre> -<p>Namespace destructuring uses an overlapping syntax, fully described in <a href="namespace.html#imports">its own section</a>. The left hand side is a list of names or aliases <code><span class='Value'>to</span><span class='Gets'>⇐</span><span class='Value'>from</span></code>.</p> +<p>Namespace destructuring uses an overlapping syntax, fully described in <a href="namespace.html#imports">its own section</a>. The left hand side is a list of names, or aliases <code><span class='Value'>to</span><span class='Gets'>⇐</span><span class='Value'>from</span></code>.</p> <a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=ceKAv3Ig4oapIHtx4oeQMity4oeQMC41fSDii4QgcQ==">↗️</a><pre> <span class='Value'>q</span><span class='Ligature'>‿</span><span class='Value'>r</span> <span class='Gets'>↩</span> <span class='Brace'>{</span><span class='Value'>q</span><span class='Gets'>⇐</span><span class='Number'>2</span><span class='Function'>+</span><span class='Value'>r</span><span class='Gets'>⇐</span><span class='Number'>0.5</span><span class='Brace'>}</span> <span class='Separator'>⋄</span> <span class='Value'>q</span> 2.5 </pre> @@ -237,12 +242,11 @@ </span>6 </pre> <h3 id="exports"><a class="header" href="#exports">Exports</a></h3> -<p>The double arrow <code><span class='Gets'>⇐</span></code> is used to export variables from a block or program, causing the result to be a <a href="namespace.html">namespace</a>. There are two ways to export variables. First, <code><span class='Gets'>←</span></code> in the variable definition can be replaced with <code><span class='Gets'>⇐</span></code> to export the variable as it's defined. Second, an export statement consisting of an assignment target followed by <code><span class='Gets'>⇐</span></code> with nothing to the right exports the variables in the assignment target and does nothing else. Export statements can be placed anywhere in the relevant program or body, including before declaration or on the last line, and a given variable can be exported any number of times.</p> -<pre><span class='Bracket'>⟨</span><span class='Value'>alias</span><span class='Gets'>⇐</span><span class='Value'>a</span><span class='Separator'>,</span> <span class='Value'>b</span><span class='Separator'>,</span> <span class='Value'>c0</span><span class='Ligature'>‿</span><span class='Value'>c1</span><span class='Gets'>⇐</span><span class='Value'>c</span><span class='Separator'>,</span> <span class='Value'>b2</span><span class='Gets'>⇐</span><span class='Value'>b</span><span class='Bracket'>⟩</span><span class='Gets'>←</span><span class='Brace'>{</span> - <span class='Value'>b</span><span class='Ligature'>‿</span><span class='Value'>c</span><span class='Gets'>⇐</span> <span class='Comment'># Non-definition exports can go anywhere -</span> <span class='Value'>a</span><span class='Gets'>⇐</span><span class='Number'>2</span> <span class='Comment'># Define and export -</span> <span class='Value'>b</span><span class='Gets'>←</span><span class='Number'>1</span><span class='Function'>+</span><span class='Value'>a</span> - <span class='Value'>c</span><span class='Gets'>←</span><span class='Value'>b</span><span class='Ligature'>‿</span><span class='String'>"str"</span> +<p><em><a href="namespace.html#exports">Full documentation</a></em></p> +<p>The double arrow <code><span class='Gets'>⇐</span></code> exports variables from a block or program, causing the result to be a namespace. It can be used either in place of normal definition <code><span class='Gets'>←</span></code>, or as a stand-alone statement with nothing to the right; in either case all the variables to its left are exported. An example with both uses, as well as namespace destructuring that uses <code><span class='Gets'>⇐</span></code> to define variable aliases, is shown below.</p> +<pre><span class='Bracket'>⟨</span><span class='Value'>alias</span><span class='Gets'>⇐</span><span class='Value'>a</span><span class='Separator'>,</span> <span class='Value'>b</span><span class='Bracket'>⟩</span> <span class='Gets'>←</span> <span class='Brace'>{</span> + <span class='Value'>b</span><span class='Ligature'>‿</span><span class='Value'>c</span><span class='Gets'>⇐</span> + <span class='Value'>a</span><span class='Gets'>⇐</span><span class='Number'>2</span> + <span class='Value'>c</span><span class='Gets'>←</span><span class='Function'>÷</span><span class='Value'>b</span><span class='Gets'>←</span><span class='Number'>1</span><span class='Function'>+</span><span class='Value'>a</span> <span class='Brace'>}</span> </pre> -<p>Fields of the resulting namespace can be accessed either directly using <code><span class='Value'>namespace.field</span></code> syntax, or with a destructuring assignment as shown above. This assignment's target is a list where each element specifies one of the names exported by the block and what it should be assigned to. The element can be either a single name (such as <code><span class='Value'>b</span></code> above), which gives both, or a combination of the assignment target, then <code><span class='Gets'>⇐</span></code>, then a name. If <code><span class='Gets'>⇐</span></code> is never used, the names can be given as a strand with <code><span class='Ligature'>‿</span></code>. To use <code><span class='Gets'>⇐</span></code> for aliases, bracket syntax <code><span class='Bracket'>⟨⟩</span></code> is needed. Imported names can be repeated and can be spelled with any role (the role is ignored).</p> |
