aboutsummaryrefslogtreecommitdiff
path: root/spec/primitive.md
diff options
context:
space:
mode:
authorMarshall Lochbaum <mwlochbaum@gmail.com>2020-12-21 15:07:21 -0500
committerMarshall Lochbaum <mwlochbaum@gmail.com>2020-12-21 15:07:21 -0500
commite3174f1a67e626b6ebfe8889a466cc9630eea3a5 (patch)
treefcada3741594986c63b4bd83a40e093ee35d9bbc /spec/primitive.md
parent9c04cd5285572698f8393cb2f1110bb92b3ea5d3 (diff)
Add Floor and comparison function specifications
Diffstat (limited to 'spec/primitive.md')
-rw-r--r--spec/primitive.md35
1 files changed, 28 insertions, 7 deletions
diff --git a/spec/primitive.md b/spec/primitive.md
index d9542ef4..859bfe27 100644
--- a/spec/primitive.md
+++ b/spec/primitive.md
@@ -4,17 +4,19 @@
Most primitives are specified by the BQN-based implementation in [reference.bqn](reference.bqn). This document specifies the basic functionality required by those definitions. Descriptions of other primitives are for informational purposes only.
-## Arithmetic
+## Pervasive primitives
-Functions here are defined for atoms only; the reference implementations extend them to arrays.
+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](types.md).
-- Add `+`
-- Negate `-` and Subtract `-` invert addition, with `-๐•ฉ` equivalent to `0-๐•ฉ` and `๐•จ-๐•ฉ` equivalent to `๐•จ+-๐•ฉ`
-- Multiply `ร—` generalizes repeated addition.
-- Divide `รท` inverts multiplication, with `รท๐•ฉ` equivalent to `1รท๐•ฉ` and `๐•จรท๐•ฉ` equivalent to `๐•จร—รท๐•ฉ`
-- Power `โ‹†` generalizes repeated multiplication, and Exponential `โ‹†` is Power with Euler's number *e* as the base.
+- **Add** `+`
+- **Negate** and **Subtract** `-` invert addition, with `-๐•ฉ` equivalent to `0-๐•ฉ`.
+- **Multiply** `ร—` generalizes repeated addition.
+- **Divide** and **Reciprocal** `รท` invert multiplication, with `รท๐•ฉ` equivalent to `1รท๐•ฉ`.
+- **Power** `โ‹†` generalizes repeated multiplication, and **Exponential** `โ‹†` is Power with Euler's number *e* as the base.
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:
@@ -23,3 +25,22 @@ The three higher functions `ร—`, `รท`, and `โ‹†` apply to numbers and no other a
- If both arguments to `-` are characters, the result is the difference of their respective code points.
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:
+- Equality of numbers is specified by the number type.
+- Characters are equal if they have the same code point.
+
+Operations are split into subtypes depending on how they were created.
+- Primitives are equal if they have the same token spelling.
+- Derived operations are equal if they are derived by the same rule and each corresponding component is the same.
+- Block instances are equal if they are the same instance.
+
+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`.