From 47c0a52e614d01eb251da9301c2961338141ab6c Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Wed, 29 Jun 2022 22:38:29 -0400 Subject: Editing continues, with some deletions this time --- docs/doc/context.html | 5 +-- docs/doc/depth.html | 82 +++++++++++++++++++++++------------------------- docs/doc/embed.html | 10 +++--- docs/doc/enclose.html | 20 ++++++------ docs/doc/expression.html | 50 +++++++++++++++-------------- docs/doc/syntax.html | 2 +- 6 files changed, 84 insertions(+), 85 deletions(-) (limited to 'docs/doc') diff --git a/docs/doc/context.html b/docs/doc/context.html index 23422fac..6a093c43 100644 --- a/docs/doc/context.html +++ b/docs/doc/context.html @@ -55,10 +55,7 @@ -

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.

-

One rule for typing is also best considered to be a pre-parsing rule like the spelling system: the role of a headerless block {} 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 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 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…

Mixing roles

BQN's value types align closely with its syntactic roles: functions, 1-modifiers, and 2-modifiers are all types (operation types) as well as roles, while the other types (data types) are split into numbers, characters, and arrays. This is no accident, and usually values will be used in roles that correspond to their underlying type. However, the ability to use a role that doesn't match the type is also useful.

diff --git a/docs/doc/depth.html b/docs/doc/depth.html index 6d043d8d..53518c35 100644 --- a/docs/doc/depth.html +++ b/docs/doc/depth.html @@ -64,7 +64,7 @@ -

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

To find the depth of an array, use Depth (). For example, the depth of a list of numbers or characters is 1:

↗️
     234
@@ -72,13 +72,13 @@
      "a string is a list of characters"
 1
 
-

Depth is somewhat analogous to an array's rank =𝕩, 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 =𝕩, and in fact rank can be "converted" to depth by splitting rows with <1 (Enclose Rank 1), reducing the rank by 1 and increasing the depth. Unlike rank, Depth doesn't care at all about its argument's shape:

↗️
     34"characters"
 1
      (1+↕10)"characters"
 1
 
-

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
 1
      2,<3,4,5
@@ -86,18 +86,16 @@
      2,<3,4,<<<5
 4
 
-

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.

-↗️
    'c'
+

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'
 0
-    F+f
+    F+  f
 0
-    'c',f,2
+     'c',f,2
 1
-    5,'c',f,2⟩⟩
-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.

-
DepthIsArray0{1+0´Depth¨𝕩}
+

Using 0=•Type to test whether 𝕩 is an array, as well as the Choose modifier, we can write a recursive definition of Depth.

+
Depth  (0=•Type)0{1+0´Depth¨𝕩}
 

The minimum element depth of 0 implies that an empty array's depth is 1.

↗️
    ⟨⟩
@@ -106,7 +104,7 @@
 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.

+

Several primitive functions manipulate 𝕩 along one or more axes based on 𝕨, according to the leading axis convention.

@@ -125,24 +123,24 @@
-

Functions such as Take and Drop 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 the left argument before applying the function.

+

Functions such as Take and Drop 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 the left argument before applying the function.

↗️
    27777"abc"
 ⟨ 2 7 7 7 ⟩
     2117777"abc"
 ⟨ 2 1 1 7 ⟩
 
-

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 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 below) is that its elements are lists, each corresponding to one axis of 𝕩:

↗️
    32,141  67
 ┌─                         
 ╵ ⟨ 3 1 ⟩ ⟨ 3 4 ⟩ ⟨ 3 1 ⟩  
   ⟨ 2 1 ⟩ ⟨ 2 4 ⟩ ⟨ 2 1 ⟩  
                           ┘
 
-

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:

↗️
    32,1 <(0=≡)¨ 67
 ⟨ ⟨ 3 1 ⟩ ⟨ 2 1 ⟩ ⟩
 
-

While very consistent, this extension represents a small convenience and makes it difficult to act on a single axis, which for Replicate and Group 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 and Group) is probably the most common way the primitive is used:

↗️
    32123 / "abcde"
 "aaabbcddeee"
 
@@ -152,41 +150,41 @@ ⟨ ⟨ 2 1 4 0 ⟩ ⟨ 2 1 4 1 ⟩ ⟩

The Depth modifier

-

The Depth 2-modifier () is a generalization of Each that allows diving deeper into an array. To illustrate it we'll use a shape 43 array of lists of lists.

-↗️
     n  <12 4322⥊↕48
-┌─                                                                         
-╵ ⟨ ⟨ 0 1 ⟩ ⟨ 2 3 ⟩ ⟩     ⟨ ⟨ 4 5 ⟩ ⟨ 6 7 ⟩ ⟩     ⟨ ⟨ 8 9 ⟩ ⟨ 10 11 ⟩ ⟩    
-  ⟨ ⟨ 12 13 ⟩ ⟨ 14 15 ⟩ ⟩ ⟨ ⟨ 16 17 ⟩ ⟨ 18 19 ⟩ ⟩ ⟨ ⟨ 20 21 ⟩ ⟨ 22 23 ⟩ ⟩  
-  ⟨ ⟨ 24 25 ⟩ ⟨ 26 27 ⟩ ⟩ ⟨ ⟨ 28 29 ⟩ ⟨ 30 31 ⟩ ⟩ ⟨ ⟨ 32 33 ⟩ ⟨ 34 35 ⟩ ⟩  
-  ⟨ ⟨ 36 37 ⟩ ⟨ 38 39 ⟩ ⟩ ⟨ ⟨ 40 41 ⟩ ⟨ 42 43 ⟩ ⟩ ⟨ ⟨ 44 45 ⟩ ⟨ 46 47 ⟩ ⟩  
-                                                                          ┘
+

The Depth 2-modifier () is a generalization of Each that allows diving deeper into an array. To illustrate it we'll use a shape 42 array of lists of lists.

+↗️
     n  <12 4223⥊↕48
+┌─                                                             
+╵ ⟨ ⟨ 0 1 2 ⟩ ⟨ 3 4 5 ⟩ ⟩       ⟨ ⟨ 6 7 8 ⟩ ⟨ 9 10 11 ⟩ ⟩      
+  ⟨ ⟨ 12 13 14 ⟩ ⟨ 15 16 17 ⟩ ⟩ ⟨ ⟨ 18 19 20 ⟩ ⟨ 21 22 23 ⟩ ⟩  
+  ⟨ ⟨ 24 25 26 ⟩ ⟨ 27 28 29 ⟩ ⟩ ⟨ ⟨ 30 31 32 ⟩ ⟨ 33 34 35 ⟩ ⟩  
+  ⟨ ⟨ 36 37 38 ⟩ ⟨ 39 40 41 ⟩ ⟩ ⟨ ⟨ 42 43 44 ⟩ ⟨ 45 46 47 ⟩ ⟩  
+                                                              ┘
      n
 3
 

Reversing n swaps all the rows:

↗️
     n
-┌─                                                                         
-╵ ⟨ ⟨ 36 37 ⟩ ⟨ 38 39 ⟩ ⟩ ⟨ ⟨ 40 41 ⟩ ⟨ 42 43 ⟩ ⟩ ⟨ ⟨ 44 45 ⟩ ⟨ 46 47 ⟩ ⟩  
-  ⟨ ⟨ 24 25 ⟩ ⟨ 26 27 ⟩ ⟩ ⟨ ⟨ 28 29 ⟩ ⟨ 30 31 ⟩ ⟩ ⟨ ⟨ 32 33 ⟩ ⟨ 34 35 ⟩ ⟩  
-  ⟨ ⟨ 12 13 ⟩ ⟨ 14 15 ⟩ ⟩ ⟨ ⟨ 16 17 ⟩ ⟨ 18 19 ⟩ ⟩ ⟨ ⟨ 20 21 ⟩ ⟨ 22 23 ⟩ ⟩  
-  ⟨ ⟨ 0 1 ⟩ ⟨ 2 3 ⟩ ⟩     ⟨ ⟨ 4 5 ⟩ ⟨ 6 7 ⟩ ⟩     ⟨ ⟨ 8 9 ⟩ ⟨ 10 11 ⟩ ⟩    
-                                                                          ┘
+┌─                                                             
+╵ ⟨ ⟨ 36 37 38 ⟩ ⟨ 39 40 41 ⟩ ⟩ ⟨ ⟨ 42 43 44 ⟩ ⟨ 45 46 47 ⟩ ⟩  
+  ⟨ ⟨ 24 25 26 ⟩ ⟨ 27 28 29 ⟩ ⟩ ⟨ ⟨ 30 31 32 ⟩ ⟨ 33 34 35 ⟩ ⟩  
+  ⟨ ⟨ 12 13 14 ⟩ ⟨ 15 16 17 ⟩ ⟩ ⟨ ⟨ 18 19 20 ⟩ ⟨ 21 22 23 ⟩ ⟩  
+  ⟨ ⟨ 0 1 2 ⟩ ⟨ 3 4 5 ⟩ ⟩       ⟨ ⟨ 6 7 8 ⟩ ⟨ 9 10 11 ⟩ ⟩      
+                                                              ┘
 

Depth ¯1 is equivalent to Each, and reverses the larger lists, while depth ¯2 applies Each twice to reverse the smaller lists:

↗️
    ¯1 n
-┌─                                                                         
-╵ ⟨ ⟨ 2 3 ⟩ ⟨ 0 1 ⟩ ⟩     ⟨ ⟨ 6 7 ⟩ ⟨ 4 5 ⟩ ⟩     ⟨ ⟨ 10 11 ⟩ ⟨ 8 9 ⟩ ⟩    
-  ⟨ ⟨ 14 15 ⟩ ⟨ 12 13 ⟩ ⟩ ⟨ ⟨ 18 19 ⟩ ⟨ 16 17 ⟩ ⟩ ⟨ ⟨ 22 23 ⟩ ⟨ 20 21 ⟩ ⟩  
-  ⟨ ⟨ 26 27 ⟩ ⟨ 24 25 ⟩ ⟩ ⟨ ⟨ 30 31 ⟩ ⟨ 28 29 ⟩ ⟩ ⟨ ⟨ 34 35 ⟩ ⟨ 32 33 ⟩ ⟩  
-  ⟨ ⟨ 38 39 ⟩ ⟨ 36 37 ⟩ ⟩ ⟨ ⟨ 42 43 ⟩ ⟨ 40 41 ⟩ ⟩ ⟨ ⟨ 46 47 ⟩ ⟨ 44 45 ⟩ ⟩  
-                                                                          ┘
+┌─                                                             
+╵ ⟨ ⟨ 3 4 5 ⟩ ⟨ 0 1 2 ⟩ ⟩       ⟨ ⟨ 9 10 11 ⟩ ⟨ 6 7 8 ⟩ ⟩      
+  ⟨ ⟨ 15 16 17 ⟩ ⟨ 12 13 14 ⟩ ⟩ ⟨ ⟨ 21 22 23 ⟩ ⟨ 18 19 20 ⟩ ⟩  
+  ⟨ ⟨ 27 28 29 ⟩ ⟨ 24 25 26 ⟩ ⟩ ⟨ ⟨ 33 34 35 ⟩ ⟨ 30 31 32 ⟩ ⟩  
+  ⟨ ⟨ 39 40 41 ⟩ ⟨ 36 37 38 ⟩ ⟩ ⟨ ⟨ 45 46 47 ⟩ ⟨ 42 43 44 ⟩ ⟩  
+                                                              ┘
     ¯2 n
-┌─                                                                         
-╵ ⟨ ⟨ 1 0 ⟩ ⟨ 3 2 ⟩ ⟩     ⟨ ⟨ 5 4 ⟩ ⟨ 7 6 ⟩ ⟩     ⟨ ⟨ 9 8 ⟩ ⟨ 11 10 ⟩ ⟩    
-  ⟨ ⟨ 13 12 ⟩ ⟨ 15 14 ⟩ ⟩ ⟨ ⟨ 17 16 ⟩ ⟨ 19 18 ⟩ ⟩ ⟨ ⟨ 21 20 ⟩ ⟨ 23 22 ⟩ ⟩  
-  ⟨ ⟨ 25 24 ⟩ ⟨ 27 26 ⟩ ⟩ ⟨ ⟨ 29 28 ⟩ ⟨ 31 30 ⟩ ⟩ ⟨ ⟨ 33 32 ⟩ ⟨ 35 34 ⟩ ⟩  
-  ⟨ ⟨ 37 36 ⟩ ⟨ 39 38 ⟩ ⟩ ⟨ ⟨ 41 40 ⟩ ⟨ 43 42 ⟩ ⟩ ⟨ ⟨ 45 44 ⟩ ⟨ 47 46 ⟩ ⟩  
-                                                                          ┘
+┌─                                                             
+╵ ⟨ ⟨ 2 1 0 ⟩ ⟨ 5 4 3 ⟩ ⟩       ⟨ ⟨ 8 7 6 ⟩ ⟨ 11 10 9 ⟩ ⟩      
+  ⟨ ⟨ 14 13 12 ⟩ ⟨ 17 16 15 ⟩ ⟩ ⟨ ⟨ 20 19 18 ⟩ ⟨ 23 22 21 ⟩ ⟩  
+  ⟨ ⟨ 26 25 24 ⟩ ⟨ 29 28 27 ⟩ ⟩ ⟨ ⟨ 32 31 30 ⟩ ⟨ 35 34 33 ⟩ ⟩  
+  ⟨ ⟨ 38 37 36 ⟩ ⟨ 41 40 39 ⟩ ⟩ ⟨ ⟨ 44 43 42 ⟩ ⟨ 47 46 45 ⟩ ⟩  
+                                                              ┘
 

While a negative depth tells how many levels to go down, a non-negative depth gives the maximum depth of the argument before applying the left operand. On a depth-3 array like above, depth 2 is equivalent to ¯1 and depth 1 is equivalent to ¯2. A depth of 0 means to descend all the way to the level of atoms, that is, apply pervasively, like an arithmetic function.

↗️
    'a',"bc" 0 23,4
diff --git a/docs/doc/embed.html b/docs/doc/embed.html
index b911046d..ea1d8e5c 100644
--- a/docs/doc/embed.html
+++ b/docs/doc/embed.html
@@ -5,13 +5,13 @@
 
 
 

Using embedded BQN

-

The Javascript implementation of BQN, docs/bqn.js, can be used 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, can be used 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 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 and array fills.

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!

Passing closures

Probably you can figure out the easy things like calling bqn("×´1+↕6") to compute six factorial. But how do you get JS and BQN to talk to each other, for example to compute the factorial of a number n? Constructing a source string with bqn("×´1+↕"+n) isn't the best way—in fact I would recommend you never use this strategy.

Instead, return a function from BQN and call it: bqn("{×´1+↕𝕩}")(n). This strategy also has the advantage that you can store the function, so that it will only be compiled once. Define let fact = bqn("{×´1+↕𝕩}"); at the top of your program and use it as a function elsewhere.

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 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 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(`
     i40
     {i+𝕩»i}
@@ -20,17 +20,17 @@
 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 4n.

+

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 4n.

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.

let [rotx, roty, flip] = bqn(`
     a  32⥊↕6
     RotX  {a𝕩˘a}
     RotY  {a𝕩a}
-    Flip  {𝕤aa}
+    Flip  {𝕊:aa}
     RotXRotYFlip
 `);
 
-

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.

JS encodings

In the programs above we've used numbers and functions of one argument, which mean the same thing in BQN and JS. This isn't the case for all types: although every BQN value is stored as some JS value, the way it's represented may not be obvious and there are many JS values that don't represent any BQN value and could cause errors. BQN operations don't verify that their inputs are valid BQN values (this would have a large performance cost), so it's up to the JS programmer to make sure that values passed in are valid. To do this, you need to know the encodings for each of the seven BQN types you're going to use.

diff --git a/docs/doc/enclose.html b/docs/doc/enclose.html index e00d168b..5d117405 100644 --- a/docs/doc/enclose.html +++ b/docs/doc/enclose.html @@ -5,7 +5,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"
 ┌·       
 · "xyz"  
@@ -22,7 +22,7 @@
 

If there are no axes, what use is an array? Rank 0 certainly qualifies as an edge case, as there's no rank -1 below it. Most often when a unit array is used it's because there are relevant axes, but we want an array that doesn't include them (sound cryptic? Just keep reading…).

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 (˝) might do this:

+

Some unit arrays are made by removing an axis from an existing array. First Cell () or Insert (˝) might do this:

↗️
    l  271828
      l
 ┌·   
@@ -37,11 +37,11 @@
 ↗️
    +˝˘ 34⥊↕12
 ⟨ 6 22 38 ⟩
 
-

In this case each call to +˝ returns a cell of the result. The result is a list, so its cells are units! Here, Cells (˘) "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.

-↗️
    +´˘ 2,"ab"3,"ABC"
+

In this case each call to +˝ returns a cell of the result. The result is a list, so its cells are units! Here, Cells (˘) "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"⟩]
 Error: >: Elements didn't have equal shapes (contained shapes ⟨2⟩ and ⟨3⟩)
 
-    +˝˘ 2,"ab"3,"ABC"
+    +˝˘ [⟨2,"ab",3,"ABC"⟩]
 ⟨ "ac" "ACE" ⟩
 

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.

@@ -61,14 +61,14 @@ ⟨ "anti" "green" "up" ⟩ ⟨ "anti" "green" "down" ⟩ ┘
-

One use is in the function <, which encloses the left argument before () joining () 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 () joining () 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"
 ⟨ 's' 't' 'a' 'r' 't' "middle" "end" ⟩
 
     "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 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
 ┌─           
 ╵ 1 0 0 0 0  
@@ -90,7 +90,7 @@
 ↗️
    <⌜´ "up""down"
 ⟨ "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"
 ┌─                                                 
 ╵ ⟨ "red" 'u' 'p' ⟩   ⟨ "red" 'd' 'o' 'w' 'n' ⟩    
@@ -99,7 +99,7 @@
                                                   ┘
 

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"
 ┌─                                                                                       
@@ -114,7 +114,7 @@
                                                                                         ┘
 

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 (¨).

↗️
    = {𝕎𝕩}¨ < 32"abcdef"
 ⟨ 2 3 1 ⟨ 3 2 ⟩ ⟩
 
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 @@

BQN expressions are the part of syntax that describes computations to perform. Programs are mainly made up of expressions with a little organizing material like blocks and namespaces around them. This page explains how functions, modifiers, and assignment combine with their inputs. It doesn't describe constant and array literals, which each form a single subject for grammatical purposes.

The first tutorial also covers how to build and read BQN expressions.

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 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:

@@ -55,11 +55,9 @@
-

The four roles (subject, function, two kinds of modifier) describe expressions, not values. When an expression is evaluated, the value's type 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 (subject, function, two kinds of modifier) describe expressions, not values. When an expression is evaluated, the value's type 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 (·), the middle function will be called with only one argument.

If you're comfortable reading BNF and want to understand things in more detail than described below, you might check the grammar specification 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.

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 type, BQN has to introduce a new and independent concept, called syntactic role, in order to support APL-like expressions.

@@ -72,7 +70,7 @@ ┘

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.

@@ -104,10 +102,12 @@
-

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 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 always have a subject role, and the role of a block is determined by its type, which depends either on the header it has or which special variables it uses.

-

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.

+

Besides these, character, string, and array literals always have a subject role, and the role of a block 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

+

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

The character · 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 𝕨 also functions as Nothing if the block that contains it is called with one argument (the uppercase spelling 𝕎 doesn't, but instead immediately causes an error). Both · and 𝕨 have a subject role.

The following rules apply to Nothing:

@@ -115,10 +115,10 @@
  • 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.

    -

    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 𝕨.

    +

    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—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 application rules:

    @@ -174,15 +174,20 @@
    -

    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, 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
    +⟨ 0 1 2 3 ⟩
    +
    +

    A variable can only be defined once within a scope, 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.

    ↗️
        x1  {x2  x3  x}
     3
         x
     1
     
    -

    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
     ¯6
         a
    @@ -218,7 +223,7 @@
         s
     ⟨ 0 1 2 3 ⟩
     
    -

    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 [] is also possible: it's equivalent to splitting the right-hand side with <˘ and then applying list destructuring.

    ↗️
        [t,u]  23
     ┌─                         
     ╵ ⟨ 0 0 ⟩ ⟨ 0 1 ⟩ ⟨ 0 2 ⟩  
    @@ -228,7 +233,7 @@
         u
     ⟨ ⟨ 1 0 ⟩ ⟨ 1 1 ⟩ ⟨ 1 2 ⟩ ⟩
     
    -

    Namespace destructuring uses an overlapping syntax, fully described in its own section. The left hand side is a list of names or aliases tofrom.

    +

    Namespace destructuring uses an overlapping syntax, fully described in its own section. The left hand side is a list of names, or aliases tofrom.

    ↗️
        qr  {q2+r0.5}  q
     2.5
     
    @@ -237,12 +242,11 @@ 6

    Exports

    -

    The double arrow is used to export variables from a block or program, causing the result to be a namespace. 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.

    -
    aliasa, b, c0c1c, b2b{
    -  bc   # Non-definition exports can go anywhere
    -  a2    # Define and export
    -  b1+a
    -  cb"str"
    +

    Full documentation

    +

    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.

    +
    aliasa, b  {
    +  bc
    +  a2
    +  c÷b1+a
     }
     
    -

    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).

    diff --git a/docs/doc/syntax.html b/docs/doc/syntax.html index a5c75b6e..431156d3 100644 --- a/docs/doc/syntax.html +++ b/docs/doc/syntax.html @@ -46,7 +46,7 @@ -Export +Export -- cgit v1.2.3