Specification: BQN primitives

Most primitives are specified by the BQN-based implementation in reference.bqn. This document specifies the basic functionality required by those definitions. Descriptions of other primitives are for informational purposes only.

Pervasive primitives

Functions in this section are defined for atoms only; the reference implementations extend them to arrays.

Arithmetic

BQN uses five arithmetic functions that are standard in mathematics. The precision of these operations should be specified by the number type.

The three higher functions ร—, รท, and โ‹† apply to numbers and no other atomic types. + and - apply to numbers, and possibly also to characters, according to the rules of the affine character type:

In the first two cases, if the result would not be a valid Unicode code point, then an error results. The remaining cases of + and - (adding two characters; negating a character or subtracting it from a number) are not allowed.

Additionally, the Floor function โŒŠ returns the largest integer smaller than the argument, or the argument itself if it is ยฏโˆž or โˆž. It's needed because the arithmetic operations give no fixed-time way to determine if a value is an integer. Floor gives an error if the argument is an atom other than a number.

Comparison

Two kinds of comparison are needed to define BQN's primitives: equality comparison and ordered comparison.

Ordered comparison is simpler and is provided by the dyadic Less than or Equal to (โ‰ค) function. This function gives an error if either argument is an operation, so it needs to be defined only for numbers and characters. For numbers it is defined by the number system, and for characters it returns 1 if the left argument's code point is less than that of the right argument. Characters are considered greater than numbers, so that nโ‰คc is 1 and cโ‰คn is 0 if c is a character and n is a number.

The dyadic function =, representing equality comparison, can be applied to any two atoms without an error. Roughly speaking, it returns 1 if they are indistinguishable within the language and 0 otherwise. If the two arguments have different types, the result is 0; if they have the same type, the comparison depends on type:

Operations are split into subtypes depending on how they were created.

This means that block instance equality indicates identity in the context of mutability: two block instances are equal if any change of state in one would be reflected in the other as well. The concept of identity holds even if the blocks in question have no way of changing or accessing state. For example, =โ—‹{๐•ฉโ‹„{๐•ฉ}}หœ@ is 0 while =หœโ—‹{๐•ฉโ‹„{๐•ฉ}}@ is 1.

Array functionality

Several subsets of primitives, or dedicated operations, are used to manipulate arrays in the reference implementation.

The following functions translate between arrays and the two lists that define them: the shape and ravel.

The following functions manipulate lists. In these functions, a valid index for list l is a natural number less than the length of l.

Inferred functionality

Inferred properties are specified in their own document, not in the reference implementation.

Other provided functionality

Commentary on other primitives

As noted above, see reference.bqn for the authoritative definitions. Commentary here gives an overall description and highlights implementation subtleties and edge cases.

Combinators

There's little to say about BQN's true combinators, since each is simply a pattern of function application. All primitive combinators use their operands as functions, and thus treat a data operand as a constant function.

The somewhat complicated definition of Valences could be replaced with {๐”ฝ๐•ฉ;๐•จ๐”พ๐•ฉ} using headers. However, reference.bqn uses a simple subset of BQN's syntax that doesn't include headers. Instead, the definition relies on the fact that ๐•จ works like ยท if no left argument is given: (1ห™๐•จ)-0 is 1-0 or 1 if ๐•จ is present and (1ห™ยท)-0 otherwise: this reduces to ยท-0 or 0.

Array properties

The reference implementations extend Shape (โ‰ข) to atoms as well as arrays, in addition to implementing other properties. In all cases, an atom behaves as if it has shape โŸจโŸฉ. The functions in this section never cause an error.