aboutsummaryrefslogtreecommitdiff
path: root/doc
diff options
context:
space:
mode:
authorMarshall Lochbaum <mwlochbaum@gmail.com>2022-06-29 22:38:29 -0400
committerMarshall Lochbaum <mwlochbaum@gmail.com>2022-06-29 22:39:07 -0400
commit47c0a52e614d01eb251da9301c2961338141ab6c (patch)
treed0ac94596d81b2680b09fb7abc924fec1ca10a50 /doc
parenta18eadf8df214b52ebb90c9bbbc433a3ad7c6ced (diff)
Editing continues, with some deletions this time
Diffstat (limited to 'doc')
-rw-r--r--doc/context.md8
-rw-r--r--doc/depth.md33
-rw-r--r--doc/embed.md10
-rw-r--r--doc/enclose.md20
-rw-r--r--doc/expression.md56
-rw-r--r--doc/syntax.md2
6 files changed, 63 insertions, 66 deletions
diff --git a/doc/context.md b/doc/context.md
index 65360935..023461e5 100644
--- a/doc/context.md
+++ b/doc/context.md
@@ -40,13 +40,7 @@ BQN's [expression grammar](expression.md) is a simplified version of the typical
| 1-modifier | Monadic operator | Adverb
| 2-modifier | Dyadic operator | Conjunction
-Unlike variables, BQN primitives have only one spelling, and a fixed role (but their values can be used in a different role by storing them in variables). Superscript glyphs `` ˙˜˘¨⌜⁼´˝` `` are used for 1-modifiers, and glyphs `∘○⊸⟜⌾⊘◶⎉⚇⍟⎊` with an unbroken circle are 2-modifiers. Other primitives are functions. String and numeric literals are subjects.
-
-BQN's variables use another system, where the spelling indicates how the variable's value is used. A variable spelled with a lowercase first letter, like `var`, is a subject. Spelled with an uppercase first letter, like `Var`, it is a function. Underscores are placed where operands apply to indicate a 1-modifier `_var` or 2-modifier `_var_`. Other than the first letter or underscore, variables are case-insensitive.
-
-The associations between spelling and syntactic role are considered part of BQN's [token formation rules](../spec/token.md).
-
-One rule for typing is also best considered to be a pre-parsing rule like the spelling system: the role of a headerless [block](block.md) `{}` is determined by which special arguments it uses: it's a subject if there aren't any, but a `𝕨` or `𝕩` makes it at least a function, an `𝔽` makes it a 1- or 2-modifier, and a `𝔾` always makes it a 2-modifier.
+BQN uses [a few rules](expression.md#role-spellings) to determine what role various parts of the grammar have. Primitive glyphs follow patterns: 1-modifiers like `˝` are superscripts and 2-modifiers like `○` use circles. A variable can be spelled with different casing or underscores to indicate the role each time it's used.
The syntactic role is a property of an expression, and BQN's grammar determines how roles interact in expressions. But [type](types.md) is a property of a value, and evaluation rules control what types can be used. This means that roles exist statically in the code (context-free grammar!) while values can change between or within runs of the program. This is necessary to have a context-free grammar with unrestricted dynamic types. Are unrestricted dynamic types useful? Read on…
diff --git a/doc/depth.md b/doc/depth.md
index ae15e879..88473d58 100644
--- a/doc/depth.md
+++ b/doc/depth.md
@@ -41,7 +41,7 @@ dim ← ⟨1.2+wd,1.3+dp⟩ ⋄ sh ← ⟨-2÷˜⊑dim,¯0.8⟩
-->
-The depth of an array is the greatest level of array nesting it attains, or, put another way, the greatest number of times you can pick an element starting from the original array before reaching an atom. The monadic function Depth (`≡`) returns the depth of its argument, while the 2-modifier Depth (`⚇`) can control the way its left operand is applied based on the depth of its arguments. Several primitive functions also use the depth of the left argument to decide whether it applies to a single axis of the right argument or to several axes.
+The depth of an array is the greatest level of array nesting it attains, or, put another way, the greatest number of times you can pick an element starting from the original array before reaching an atom. The monadic function Depth (`≡`) returns the depth of its argument, while the 2-modifier Depth (`⚇`) controls the way its left operand is applied based on the depth of its arguments. Several primitive functions also check the depth of the left argument to decide whether it applies to a single axis of the right argument or to several axes.
## The Depth function
@@ -50,27 +50,26 @@ To find the depth of an array, use Depth (`≡`). For example, the depth of a li
≡ 2‿3‿4
≡ "a string is a list of characters"
-Depth is somewhat analogous to an array's [rank](shape.md) `=𝕩`, and in fact rank can be "converted" to depth by splitting rows with `<⎉1`, reducing the rank by 1 and increasing the depth. Unlike rank, Depth doesn't care at all about its argument's shape:
+Depth is somewhat analogous to an array's [rank](shape.md) `=𝕩`, and in fact rank can be "converted" to depth by splitting rows with `<⎉1` ([Enclose](enclose.md) [Rank](rank.md) 1), reducing the rank by 1 and increasing the depth. Unlike rank, Depth doesn't care at all about its argument's shape:
≡ 3‿4⥊"characters"
≡ (1+↕10)⥊"characters"
-Also unlike rank, Depth *does* care about the elements of its argument: in fact, to find the depth of an array, every element must be inspected.
+Also unlike rank, Depth *does* care about the elements of its argument: in fact, to find the depth of an array, every element must be inspected recursively.
≡ ⟨2,3,4,5⟩
≡ ⟨2,<3,4,5⟩
≡ ⟨2,<3,4,<<<5⟩
-As the above expressions suggest, the depth of an array is the maximum of its elements' depths, plus one. The base case, an atom (including a function or modifier), has depth 0.
+The depth of an array is the maximum of its elements' depths, plus one. The base case, an atom (including a function or modifier), has depth 0.
- ≡'c'
- F←+⋄≡f
- ≡⟨'c',f,2⟩
- ≡⟨5,⟨'c',f,2⟩⟩
+ ≡ 'c'
+ F←+ ⋄ ≡f
+ ≡ ⟨'c',f,2⟩
-If the function `IsArray` indicates whether its argument is an array, then we can write a recursive definition of Depth using the Choose modifier.
+Using `0=•Type` to test whether `𝕩` is an array, as well as the [Choose](choose.md) modifier, we can write a recursive definition of Depth.
- Depth←IsArray◶0‿{1+0⌈´Depth¨⥊𝕩}
+ Depth ← (0=•Type)◶0‿{1+0⌈´Depth¨⥊𝕩}
The minimum element depth of 0 implies that an empty array's depth is 1.
@@ -79,27 +78,27 @@ The minimum element depth of 0 implies that an empty array's depth is 1.
## Testing depth for multiple-axis primitives
-Several primitive functions use the left argument to manipulate the right argument along one or more axes, using [the leading axis convention](leading.md#multiple-axes).
+Several primitive functions manipulate `𝕩` along one or more axes based on `𝕨`, according to [the leading axis convention](leading.md#multiple-axes).
| Single-axis depth | Functions
|-------------------|----------
| 0 | `↑↓↕⌽⍉`
| 1 | `/⊏⊔`
-Functions such as [Take and Drop](take.md) use a single number per axis. When the left argument is a list of numbers, they apply to initial axes. But for convenience, a single number is also accepted, and applied to the first axis only. This is equivalent to [deshaping](reshape.md) the left argument before applying the function.
+Functions such as [Take and Drop](take.md) accept a single number per axis in `𝕨`. If given a list of numbers, they apply to initial axes. But for convenience, a single number is also accepted, and applied to the first axis only. This is equivalent to [deshaping](reshape.md) the left argument before applying the function.
≢2↑7‿7‿7‿7⥊"abc"
≢2‿1‿1↑7‿7‿7‿7⥊"abc"
-In these cases the flexibility seems trivial because the left argument has depth 1 or 0: it is an array or isn't, and it's obvious what a plain number should do. But for the second row in the table, the left argument is always an array. The general case ([Select](select.md) below) is that the left argument is a list and its elements correspond to right argument axes:
+In these cases the flexibility seems trivial because `𝕨` has depth 1 or 0: it is an array or isn't, and it's obvious what a plain number should do. But for the second row in the table, `𝕨` is always an array. The general case ([Select](select.md) below) is that its elements are lists, each corresponding to one axis of `𝕩`:
⟨3‿2,1‿4‿1⟩ ⊏ ↕6‿7
-This means the left argument is homogeneous of depth 2. What should an argument of depth 1, that is, an array of atoms, do? One option is to continue to require the left argument to be a list, and convert any atom argument into an array by enclosing it:
+This means `𝕨` is homogeneous of depth 2. What should an argument of depth 1, that is, an array of atoms, do? One option is to continue to require the left argument to be a list, and convert any atom argument into an array by enclosing it:
⟨3‿2,1⟩ <⍟(0=≡)¨⊸⊏ ↕6‿7
-While very consistent, this extension represents a small convenience and makes it difficult to act on a single axis, which for [Replicate](replicate.md) and [Group](group.md) is probably the most common way the primitive is used:
+While very consistent, this extension represents a small convenience and makes it difficult to act on a single axis, which (particularly for [Replicate](replicate.md) and [Group](group.md)) is probably the most common way the primitive is used:
3‿2‿1‿2‿3 / "abcde"
@@ -111,9 +110,9 @@ For Select, the depth-1 case is still quite useful, but it may also be desirable
## The Depth modifier
-The Depth 2-modifier (`⚇`) is a generalization of [Each](map.md) that allows diving deeper into an array. To illustrate it we'll use a shape `4‿3` array of lists of lists.
+The Depth 2-modifier (`⚇`) is a generalization of [Each](map.md) that allows diving deeper into an array. To illustrate it we'll use a shape `4‿2` array of lists of lists.
- ⊢ n ← <⎉1⍟2 4‿3‿2‿2⥊↕48
+ ⊢ n ← <⎉1⍟2 4‿2‿2‿3⥊↕48
≡ n
Reversing `n` swaps all the rows:
diff --git a/doc/embed.md b/doc/embed.md
index d75275a0..ad17dac7 100644
--- a/doc/embed.md
+++ b/doc/embed.md
@@ -2,7 +2,7 @@
# Using embedded BQN
-The Javascript implementation of BQN, [docs/bqn.js](../docs/bqn.js), can be [used](https://mlochbaum.github.io/BQN/try.html) as a standalone interpreter, but it can also be called from JS, which in combination with BQN's first-class functions allows the two language to interoperate well. Similar functionality will most likely be brought to other host languages in the future. Languages that (like JS) allow functions and arrays to be tagged with extra properties can host a full BQN implementation with good interoperability. Other languages would either require functions and arrays to be stored in specialized data structures, making interoperability a little harder, or would miss out on some inferred properties like function inverses and array fills.
+The Javascript implementation of BQN, [docs/bqn.js](../docs/bqn.js), can be [used](https://mlochbaum.github.io/BQN/try.html) as a standalone interpreter, but it can also be called from JS, which in combination with BQN's first-class functions allows the two languages to interoperate well. Similar functionality will most likely be brought to other host languages in the future (and there's a [Rust binding](https://detegr.github.io/cbqn-rs/cbqn/) to CBQN that works a lot like an embedding). Languages that (like JS) allow functions and arrays to be tagged with extra properties can host a full BQN implementation with good interoperability. Other languages would either require functions and arrays to be stored in specialized data structures, making interoperability a little harder, or would miss out on some inferred properties like function [inverses](undo.md) and array [fills](fill.md).
There is only one mechanism to interface between the host language and BQN: the function `bqn` evaluates a string containing a BQN program and returns the result. Doesn't sound like much, especially considering these programs can't share any state such as global variables (BQN doesn't have those). But taking first-class functions and closures into account, it's all you could ever need!
@@ -14,7 +14,7 @@ Instead, return a function from BQN and call it: `bqn("{×´1+↕𝕩}")(n)`. Th
BQN can also call JS functions, to use functionality that isn't native to BQN or interact with a program written in JS. For example, `bqn("{𝕏'a'+↕26}")(alert)` calls the argument `alert` from within BQN. The displayed output isn't quite right here, because a BQN string is stored as a JS array, not a string. See the next section for more information.
-Cool, but none of these examples really use closures, just self-contained functions. [Closures](lexical.md#closures) are functions that use outside state, which is maintained over the course of the program. Here's an example program that defines `i` and then returns a function that manipulates `i` and returns its new value.
+Cool, but none of these examples really use closures, just self-contained functions. [Closures](lexical.md#closures) are functions that use outside state, which is maintained over the course of the program. Here's an example program that defines `i`, and then returns a function that manipulates `i` and returns its new value.
let push = bqn(`
i←4⥊0
@@ -24,7 +24,7 @@ Cool, but none of these examples really use closures, just self-contained functi
push(-2); // [1,3,0,0]
push(4); // [5,4,3,0]
-Note that this program doesn't have any outer braces. It's only run once, and it initializes `i` and returns a function. Just putting braces around it wouldn't have any effect—it just changes it from a program that does something to a program that runs a block that does the same thing—but adding braces and using `𝕨` or `𝕩` inside them would turn it into a function that could be run multiple times to create different closures. For example, `pushGen = bqn("{i←4⥊𝕩⋄{i+↩𝕩»i}}")` causes `pushGen(n)` to create a new closure with `i` initialized to `4⥊n`.
+Note that this program doesn't have any outer braces. It's only run once, and it initializes `i` and returns a function. Just putting braces around it wouldn't have any effect—it just changes it from a program that does something to a program that runs a block that does the same thing—but adding braces and using `𝕨` or `𝕩` inside would turn it into a function that could be run multiple times to create different closures. For example, `pushGen = bqn("{i←4⥊𝕩⋄{i+↩𝕩»i}}")` causes `pushGen(n)` to create a new closure with `i` initialized to `4⥊n`.
The program also returns only one function, which can be limiting. But it's possible to get multiple closures out of the same program by returning a list of functions. For example, the following program defines three functions that manipulate a shared array in different ways.
@@ -32,11 +32,11 @@ The program also returns only one function, which can be limiting. But it's poss
a ← 3‿2⥊↕6
RotX ← {a↩𝕩⌽˘a}
RotY ← {a↩𝕩⌽a}
- Flip ← {𝕤⋄a↩⍉a}
+ Flip ← {𝕊:a↩⍉a}
RotX‿RotY‿Flip
`);
-When defining closures for their side effects like this, make sure they are actually functions! For example, since `flip` ignores its argument (you can call it with `flip()`, because a right argument of `undefined` isn't valid but will just be ignored), it needs an extra `𝕤` in the definition to be a function instead of an immediate block.
+When defining closures for their side effects like this, make sure they are actually functions! For example, since `flip` ignores its argument (you can call it with `flip()`, because a right argument of `undefined` isn't valid but will just be ignored), it needs an `𝕊:` in the definition to be a function instead of an immediate block.
You can also use an array to pass multiple functions or other values from JS into BQN all at once. However, a JS array can't be used directly in BQN because its shape isn't known. The function `list()` converts a JS array into a BQN list by using its length for the shape; the next section has a few more details.
diff --git a/doc/enclose.md b/doc/enclose.md
index e43dffc3..459146a7 100644
--- a/doc/enclose.md
+++ b/doc/enclose.md
@@ -2,7 +2,7 @@
# Enclose
-The function enclose creates a unit array whose only element is `𝕩`.
+The function Enclose creates a unit array whose only element is `𝕩`.
< "xyz"
@@ -21,7 +21,7 @@ If there are no axes, what use is an array? Rank 0 certainly qualifies as an edg
This contrasts with an atom like `137`, which is considered a unit but not a unit *array*. An atom has no axes just because it doesn't have axes. But because it has no axes, it has the same shape `⟨⟩` as a unit array, by convention.
-Some unit arrays are made by removing an axis from an existing array. First Cell (`⊏`) or [Insert](fold.md) (`˝`) might do this:
+Some unit arrays are made by removing an axis from an existing array. [First Cell](select.md#first-cell) (`⊏`) or [Insert](fold.md) (`˝`) might do this:
l ← 2‿7‿1‿8‿2‿8
⊏ l
@@ -31,11 +31,11 @@ Usually this is unwanted. You'd prefer to use `⊑` or `+´` in order to get an
+˝˘ 3‿4⥊↕12
-In this case each call to `+˝` returns a cell of the result. The result is a list, so its cells are units! Here, [Cells](rank.md) (`˘`) "hides" one axis from its operand, and the operand `+˝` reduces out an axis, leaving zero axes—until Cells assembles the results, putting its axis back. In this case, `+´` would also be tolerated. But it's wrong, because each result really should be a zero-axis array. We can reveal this by making an array whose elements aren't atoms.
+In this case each call to `+˝` returns a cell of the result. The result is a list, so its cells are units! Here, [Cells](rank.md) (`˘`) "hides" one axis from its operand, and the operand `+˝` reduces out an axis, leaving zero axes—until Cells assembles the results, putting its axis back. Here, `+´` would also be tolerated. But it's wrong, because each result really should be a zero-axis array. We can reveal this by making an array whose elements aren't atoms.
- +´˘ ⟨↕2,"ab"⟩≍⟨↕3,"ABC"⟩
+ +´˘ [⟨↕2,"ab"⟩,⟨↕3,"ABC"⟩]
- +˝˘ ⟨↕2,"ab"⟩≍⟨↕3,"ABC"⟩
+ +˝˘ [⟨↕2,"ab"⟩,⟨↕3,"ABC"⟩]
The function `+´˘` tries to mix together the result elements into one big array, causing an error because they have different lengths, but `+˝˘` keeps them as elements.
@@ -51,13 +51,13 @@ Let's take a look at the following program, which uses [Table](map.md#table) (`
(<⟨⟩) <⊸∾⌜´ ⟨""‿"anti", "red"‿"blue"‿"green", "up"‿"down"⟩
-One use is in the function `<⊸∾`, which encloses the left argument before (`⊸`) [joining](join.md) (`∾`) it to the right argument. This is different from Join on its own because it treats the left argument as a single element.
+One use is in the function `<⊸∾`, which encloses the left argument [before](hook.md) (`⊸`) [joining](join.md) (`∾`) it to the right argument. This is different from Join on its own because it treats the left argument as a single element.
"start" ∾ "middle"‿"end"
"start" <⊸∾ "middle"‿"end"
-For this purpose `{⟨𝕩⟩}⊸∾`, which turns the left argument into a 1-element list, also works. But maybe it doesn't really capture the intended meaning: it makes `𝕨` into a whole new list to be added when all that's needed is to add one cell. This cell will be placed along the first axis, but it doesn't have an axis of its own. A similar example, showing how units are used as part of a computation, is to join each row of a matrix to the corresponding item of a list.
+For this purpose `⋈⊸∾`, which [enlists](pair.md) the left argument giving the list `⟨𝕨⟩`, also works. But maybe it doesn't really capture the intended meaning: it makes `𝕨` into a whole new list to be added when all that's needed is to add one cell. This cell will be placed along the first axis, but it doesn't have an axis of its own. A similar example, showing how units are used as part of a computation, is to join each row of a matrix to the corresponding item of a list.
(=⌜˜↕4) ∾˘ ↕4
@@ -71,20 +71,20 @@ The other use of `<` in the original example is `(<⟨⟩)`, which is the left a
<⊸∾⌜´ ⟨"up"‿"down"⟩
-But this is only an array of strings, and not an array of lists of strings: the right result is `⟨⟨"up"⟩,⟨"down"⟩⟩`. And that's not the extend of our troubles: without an initial value we'll get the wrong result on longer arguments too, because the elements of the rightmost array get joined to the result lists as lists, not as elements.
+But this is only an array of strings, and not an array of lists of strings: the right result is `⟨⟨"up"⟩,⟨"down"⟩⟩`. And that's not the extent of our troubles: without an initial value we'll get the wrong result on longer arguments too, because the elements of the rightmost array get joined to the result lists as lists, not as elements.
<⊸∾⌜´ ⟨"red"‿"blue"‿"green", "up"‿"down"⟩
To make things right, we need an array of lists for an initial value. Since it shouldn't add anything to the result, any lists it contains need to be empty. But what should its shape be? The result shape from Table is always the argument shapes joined together (`𝕨∾○≢𝕩`). The initial value shouldn't contribute the result shape, so it needs to have empty shape, or rank 0! We use Enclose to create the array `<⟨⟩` with no axes, because the result *will* have axes but the initial element needs to start without any. All the axes come from the list of choices.
-It goes deeper! The following (pretty tough) example uses arrays with various ranks in the argument, and they're handled quite well. The last one isn't really a choice, so it has no axes. If it were a one-element list then the result would have a meaningless length-1 axis. But not enclosing it would cause each character to be treated as an option, with unpleasant results.
+It goes deeper! The following (pretty tough) example uses arrays with various ranks in the argument, and they're handled just fine. The last one isn't really a choice, so it has no axes. If it were a one-element list then the result would have a meaningless length-1 axis. But not enclosing it would cause each character to be treated as an option, with unpleasant results.
flavor ← ⍉ ∘‿2 ⥊ "up"‿"down"‿"charm"‿"strange"‿"top"‿"bottom"
(<⟨⟩) <⊸∾⌜´ ⟨"red"‿"blue"‿"green", flavor, <"quark"⟩
### Broadcasting
-Table isn't the only mapping function that gets along well with units. Here's an example with Each (`¨`).
+Table isn't the only mapping function that gets along well with units. Here's an example with [Each](map.md#each) (`¨`).
=‿≠‿≡‿≢ {𝕎𝕩}¨ < 3‿2⥊"abcdef"
diff --git a/doc/expression.md b/doc/expression.md
index d5ca2035..5fa6cdc7 100644
--- a/doc/expression.md
+++ b/doc/expression.md
@@ -8,7 +8,7 @@ The [first tutorial](../tutorial/expression.md) also covers how to build and rea
## Overview
-BQN expressions consist of subjects, functions, and modifiers arranged in sequence, with parentheses to group parts into subexpressions. Assignment arrows `←` and `↩` 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:
+BQN expressions consist of subjects, functions, and modifiers arranged in sequence, with parentheses to group parts into subexpressions. Assignment arrows `←` and `↩` can also be present and mostly act like functions. [Functions](ops.md#functions) can be applied to subjects or grouped into [trains](train.md), while [modifiers](ops.md#modifiers) can be applied to subjects or functions. The most important kinds of application are:
| left | main | right | output | name | binding
|-------|-------|-------|------------|------------|---------
@@ -17,16 +17,12 @@ BQN expressions consist of subjects, functions, and modifiers arranged in sequen
| `F` | `_m` | | Function | 1-Modifier | LtR, tighter
| `F` | `_c_` | `G` | Function | 2-Modifier |
-The four roles (subject, function, two kinds of modifier) describe expressions, not values. When an expression is evaluated, the value's [type](types.md) 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.
+The four [roles](#syntactic-role) (subject, function, two kinds of modifier) describe expressions, not values. When an expression is evaluated, the value's [type](types.md) 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.
In the table, `?` marks an optional left argument. If there isn't a value in that position, or it's [Nothing](#nothing) (`·`), the middle function will be called with only one argument.
If you're comfortable reading [BNF](https://en.wikipedia.org/wiki/Backus%E2%80%93Naur_form) and want to understand things in more detail than described below, you might check the [grammar specification](../spec/grammar.md) as well.
-## Parentheses
-
-As in most programming languages, parentheses `()` 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 `(2×3)+4`, `2×3` is a subexpression evaluating to `6`, so that larger expression is equivalent to `6+4`. The syntactic role of a set of parentheses is also the same as that of the expression inside.
-
## Syntactic role
*This issue is approached from a different angle in [Context free grammar](context.md).*
@@ -41,7 +37,7 @@ Below, the function `{𝕎𝕩}` treats its left argument `𝕎` as a function a
### Role spellings
-The four roles are **subject**, **function**, **1-modifier**, and **2-modifier**, 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.
+The four roles are **subject**, **function**, **1-modifier**, and **2-modifier**, 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.
| BQN | Names | Primitives
|-------------|-------------|-----------------
@@ -50,13 +46,17 @@ The four roles are **subject**, **function**, **1-modifier**, and **2-modifier**
| 1-modifier | `_leading` | `` ˙˜˘¨⌜⁼´˝` ``
| 2-modifier | `_both_` | `∘○⊸⟜⌾⊘◶⎉⚇⍟⎊`
-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 `⌽` and `⍉`.
+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 `⌽` and `⍉`.
Variable names can be written in any case and with underscores added, and these changes don't affect what [identifier](lexical.md) the name refers to. `ab`, `aB`, `AB`, and `_a_B_` 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.
-Besides these, character, string, and [array literals](arrayrepr.md#array-literals) always have a subject role, and the role of a [block](block.md) is determined by its type, which depends either on the header it has or which special variables it uses.
+Besides these, character, string, and [array literals](arrayrepr.md#array-literals) always have a subject role, and the role of a [block](block.md) 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 `𝕨` or `𝕩` makes it at least a function, an `𝔽` makes it a 1- or 2-modifier, and a `𝔾` always makes it a 2-modifier.
+
+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.
+
+## Parentheses
-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.
+As in most programming languages, parentheses `()` 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 `(2×3)+4`, `2×3` is a subexpression evaluating to `6`, so that larger expression is equivalent to `6+4`. The syntactic role of a set of parentheses is also the same as that of the expression inside.
## Nothing
@@ -66,13 +66,13 @@ The following rules apply to Nothing:
- If it's the left argument in a function call, the function is called with no left argument.
- If it's the right argument, the function isn't called, and "returns" Nothing.
-For example, the expression `(F 2 G ·) H I j` is equivalent to `H I j`. But functions and arguments that would be discarded by the second rule are still evaluated, so that for example `(a+↩1) F ·` increments `a` when run.
+For example, the expression `(F 2 G ·) H I j` is equivalent to `H I j`. But functions and arguments that will be discarded by the second rule are still evaluated, so that for example `(a+↩1) F ·` increments `a` when run.
-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 `·`, or when the function is called with no left argument, for `𝕨`.
+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 `·`, or when the function is called with no left argument, for `𝕨`.
## Kinds of application
-Here is a table of the modifier and function application rules:
+Here is a table of the [function and modifier](ops.md) application rules:
| left | main | right | output | name
|-------|-------|-------|------------|------
@@ -83,16 +83,21 @@ Here is a table of the modifier and function application rules:
| `F*` | `_m` | | Function | 1-Modifier
| `F*` | `_c_` | `G*` | Function | 2-Modifier
-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.
+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.
## Assignment
-Another element that can be included in expressions is assignment, which is written with `←` to *define* (also called "declare" in many other languages) a variable and `↩` to *change* its definition. A variable can only be defined once within a [scope](lexical.md), 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.
+Expressions may also include assignment, which is written with `←` or `⇐` to *define* (similar to "declare" in many other languages) a variable and `↩` to *change* its definition. Assignment has a left-hand side (`name` below) which is usually a variable name, and a right-hand side (`↕4`) 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.
+
+ name ← ↕4
+ name
+
+A variable can only be defined once within a [scope](lexical.md), 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.
x←1 ⋄ {x←2 ⋄ x↩3 ⋄ x}
x
-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.
+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.
2×a←(Neg←-)3
a
@@ -114,13 +119,13 @@ The left hand side of assignment in a subject expression can be *compound*, so t
s
-Array destructuring using `[]` is also possible: it's equivalent to splitting the right-hand side with `<˘` and then applying list destructuring.
+Array destructuring using [array notation](arrayrepr.md#high-rank-arrays) `[]` is also possible: it's equivalent to splitting the right-hand side with `<˘` and then applying list destructuring.
[t,u] ← ↕2‿3
u
-Namespace destructuring uses an overlapping syntax, fully described in [its own section](namespace.md#imports). The left hand side is a list of names or aliases `to⇐from`.
+Namespace destructuring uses an overlapping syntax, fully described in [its own section](namespace.md#imports). The left hand side is a list of names, or aliases `to⇐from`.
q‿r ↩ {q⇐2+r⇐0.5} ⋄ q
@@ -130,13 +135,12 @@ With destructuring, you might want to discard some values from the right hand si
### Exports
-The double arrow `⇐` is used to export variables from a block or program, causing the result to be a [namespace](namespace.md). There are two ways to export variables. First, `←` in the variable definition can be replaced with `⇐` to export the variable as it's defined. Second, an export statement consisting of an assignment target followed by `⇐` 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.
+*[Full documentation](namespace.md#exports)*
- ⟨alias⇐a, b, c0‿c1⇐c, b2⇐b⟩←{
- b‿c⇐ # Non-definition exports can go anywhere
- a⇐2 # Define and export
- b←1+a
- c←b‿"str"
- }
+The double arrow `⇐` exports variables from a block or program, causing the result to be a namespace. It can be used either in place of normal definition `←`, 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 `⇐` to define variable aliases, is shown below.
-Fields of the resulting namespace can be accessed either directly using `namespace.field` 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 `b` above), which gives both, or a combination of the assignment target, then `⇐`, then a name. If `⇐` is never used, the names can be given as a strand with `‿`. To use `⇐` for aliases, bracket syntax `⟨⟩` is needed. Imported names can be repeated and can be spelled with any role (the role is ignored).
+ ⟨alias⇐a, b⟩ ← {
+ b‿c⇐
+ a⇐2
+ c←÷b←1+a
+ }
diff --git a/doc/syntax.md b/doc/syntax.md
index bbde476e..cb57ea10 100644
--- a/doc/syntax.md
+++ b/doc/syntax.md
@@ -17,7 +17,7 @@ Glyph(s) | Meaning
`·` | [Nothing](expression.md#nothing)
`()` | [Expression grouping](expression.md#parentheses)
`←` | [Define](expression.md#assignment)
-`⇐` | [Export](expression.md#exports)
+`⇐` | [Export](namespace.md#exports)
`↩` | [Change](expression.md#assignment)
`⋄,` or newline | Statement or element [separator](#separators)
`⟨⟩` | [List](#list-and-array-notation)