aboutsummaryrefslogtreecommitdiff
path: root/doc/context.md
diff options
context:
space:
mode:
authorMarshall Lochbaum <mwlochbaum@gmail.com>2020-06-20 20:26:06 -0400
committerMarshall Lochbaum <mwlochbaum@gmail.com>2020-06-20 20:26:06 -0400
commit750298bccbe90cb6fed2dbb885bb6ff5d99561a6 (patch)
tree837987182eec40405654f15c2d4c71426536a1d4 /doc/context.md
parentde66dd40c15c4ca15461c108f9c91ea660cfb2d7 (diff)
Add page on context-freedom
Diffstat (limited to 'doc/context.md')
-rw-r--r--doc/context.md82
1 files changed, 82 insertions, 0 deletions
diff --git a/doc/context.md b/doc/context.md
new file mode 100644
index 00000000..7a9e218d
--- /dev/null
+++ b/doc/context.md
@@ -0,0 +1,82 @@
+# BQN's context-free grammar
+
+APL has a problem. To illustrate, let's look at an APL expression:
+
+ a b c d e
+
+It is impossible to say anything about this sentence! Is `c` a dyadic operator being applied to `b` and `d`, or are `b` and `d` two dyadic functions being applied to arrays? In contrast, expressions in C-like or Lisp-like languages show their structure of application:
+
+ b(a, d(c)(e))
+ (b a ((d c) e))
+
+In each case, some values are used as inputs to functions while others are the functions being applied. The result of a function can be used either as an input or as a function again. These expressions correspond to the APL expression where `a` and `e` are arrays, `b` and `c` are functions, and `d` is a monadic operator. However, these syntactic classes have to be known to see what the APL expression is doing—they are a form of context that is required for a reader to know the grammatical structure of the expression. In a context-free grammar like that of simple C or Lisp expressions, a value's grammatical role is part of the expression itself, indicated with parentheses: they come after the function in C and before it in Lisp. Of course, a consequence of using parentheses in this way is having a lot of parentheses. BQN uses a different method to annotate grammatical role:
+
+ a B C _d e
+
+Here, the lowercase spelling indicates that `a` and `e` are to be treated as values ("arrays" in APL) while uppercase variables `B` and `C` are used as functions and `_d` is a modifier ("monadic operator"). Like parentheses for function application, the spelling is not inherent to the variable values used, but instead indicates their grammatical role in this particular expression. While we still don't know anything about what values `a`, `b`, `c`, and so on have, we know how they interact in this line of code.
+
+## Is grammatical context really a problem?
+
+Yes, in the sense of [problems with BQN](../problems.md). A grammar that uses context is harder for humans to read and machines to execute. A particular difficulty is that parts of an expression you don't yet understand can interfere with parts you do, making it difficult to work through an unknown codebase.
+
+One difficulty beginners to APL will encounter is that code in APL at first appears like a string of undifferentiated symbols. For example, a tacit Unique Mask implementation `⍳⍨=⍳∘≢` consists of six largely unfamiliar characters with little to distinguish them (in fact, the one obvious bit of structure, the repeated `⍳`, is misleading as it means different things in each case!). Simply placing parentheses into the expression, like `(⍳⍨)=(⍳∘≢)`, can be a great help to a beginner, and part of learning APL is to naturally see where the parentheses should go. The equivalent BQN expression, `⊐˜=↕∘≠`, will likely appear equally intimidating at first, but the path to learning which things apply to which is much shorter: rather than learning the entire list of APL primitives, a beginner just needs to know that superscript characters like `˜` are modifiers and characters like `∘` with unbroken circles are compositions before beginning to learn the BQN grammar that will explain how to tie the various parts together.
+
+This sounds like a distant concern to a master of APL or a computer that has no difficulty memorizing a few dozen glyphs. Quite the opposite: the same concern applies whenever you begin work with an unfamiliar codebase! Many APL programmers even enforce variable name conventions to ensure they know the class of a variable. By having such a system built in, BQN keeps you from having to rely on programmers following a style guide, and also allows greater flexibility, as we'll see later.
+
+Shouldn't a codebase define all the variables it uses, so we can see their class from the definition? Not always: consider that in a language with libraries, code might be imported from dependencies. Many APLs also have some dynamic features that can allow a variable to have more than one class, such as the `⍺←⊢` pattern in a dfn that makes `⍺` an array in the dyadic case but a function in the monadic case. Regardless, searching for a definition somewhere in the code is certainly a lot more work than knowing the class right away! One final difficulty is that even one unknown can delay understanding of an entire expression. Suppose in `A B c`, `B` is a function and `c` is an array, and both values are known to be constant. If `A` is known to be a function (even if its value is not yet known), its right argument `B c` can be evaluated ahead of time. But if `A`'s type isn't known, it's impossible to know if this optimization is worth it, because if it is an array, `B` will instead be called dyadically.
+
+## BQN's spelling system
+
+BQN's expression grammar is a simplified version of the typical APL, removing some oddities like niladic functions and the two-glyph Outer Product operator. Values can be used in four syntactic roles:
+
+| BQN | APL | J
+|-------------|------------------|------
+| Value | Array | Noun
+| Function | Function | Verb
+| Modifier | Monadic operator | Adverb
+| Composition | Dyadic operator | Conjunction
+
+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 modifiers, and glyphs `∘○⊸⟜⌾⚇⎉⍟◶⊘` with an unbroken circle are compositions. Other primitives are functions. String and numeric literals are values.
+
+BQN's variables use another system. Unlike primitives, variables can be spelled as any of the four syntactic types. Its value remains the same, as the spelling only indicates how this value is used. A variable spelled with a lowercase first letter, like `var`, is a value. Spelled with an uppercase first letter, like `Var`, it is a function. Underscores are placed where operands apply to indicate a modifier `_var` or composition `_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).
+
+## BQN's grammar
+
+A formal treatment is included in [the spec](../spec/grammar.md). BQN's grammar—the ways syntactic roles interact—follows the original APL model (plus trains) closely, with allowances for new features like list notation. In order to keep BQN's syntax context-free, the syntactic role of any expression must be known, just like tokens.
+
+BQN fails to be context-free in one fairly mild way: the role of a brace construct `{}` is determined by which special arguments it uses. This means the grammar is not context-free in the technical sense, but since the "context" in this case is carried between the braces and cannot be left out it's not harmful in the same way as variable values. Informally it still makes sense to call BQN "context-free".
+
+Here is a table of the APL-derived operator and function application rules:
+
+| left | main | right | output | name
+|-------|-------|-------|----------|------
+| | `F` | `x` | Value | Monadic function
+| `w` | `F` | `x` | Value | Dyadic function
+| | `F` | `G` | Function | 2-train
+| `F*` | `G` | `H` | Function | 3-train
+| `F*` | `_m` | | Function | Modifier
+| `F*` | `_c_` | `G*` | Function | Composition
+| | `_c_` | `G*` | Modifier | Partial application
+| `F*` | `_c_` | | Modifier | Partial application
+
+A function with an asterisk indicates that a value can also be used: in these positions there is no difference between function and value spellings. Operator applications bind more tightly than functions, and associate left-to-right while functions associate right-to-left.
+
+BQN lists can be written with angle brackets `⟨elt0,elt1,…⟩` or ligatures `elt0‿elt1‿…`. In either case the elements can have any type, and the result is a value.
+
+The statements in a brace block, function, or operator can also be any role, including the return value at the end. These roles have no effect: outside of braces, a function always returns an array regardless of how it was defined.
+
+## Mixing roles
+
+BQN's basic types align closely with its syntactic roles: functions, modifiers, and compositions are all basic types, while values are split into numbers, characters, and arrays. This is no accident, and usually values will be used in roles that match their underlying type. However, the ability to use a role that doesn't match the type is very useful.
+
+Any type can be passed as an argument to a function, or as an operand, by treating it as an array. This means that BQN fully supports Lisp-style functional programming, where functions can be used as values.
+
+It can also be useful to treat an array as a function, in which case it applies as a constant function. This rule is useful with most built-in operators. For example, `F⎉1` uses a constant for the rank even though in general a function can be given, and `a⌾(b⊸/)` inserts the values in `a` into the positions selected by `b`, ignoring the old values rather than applying a function to them.
+
+Other mixes of roles are generally not useful. While a combination such as treating a function as a modifier is allowed, attempting to apply it to an operand will fail. Only a modifier can be applied as a modifier and only a composition can be applied as a composition. Only a function or array can be applied as a function.
+
+It's also worth noting that something that appears to be an array may actually be a function! For example, the result of `𝕨˜𝕩` may not always be `𝕨`. `𝕨˜𝕩` is exactly identical to `𝕎˜𝕩`, which gives `𝕩𝕎𝕩`. If `𝕎` is a number, character, or array, that's the same as `𝕨`, but if it is a function, then it will be applied.
+
+The primary way to change the role of a value in BQN is to use a name, including one of the arguments to a brace function. In particular, you can use `{𝔽}` to convert a value operand into a function. Converting a function to a value is more difficult. Often an array of functions is wanted, in which case they can be stranded together; otherwise it's probably best to give the function a name. Picking a function out of a list, for example `⊑⟨+⟩` will give it as a value.