diff options
| author | Marshall Lochbaum <mwlochbaum@gmail.com> | 2020-12-21 15:07:21 -0500 |
|---|---|---|
| committer | Marshall Lochbaum <mwlochbaum@gmail.com> | 2020-12-21 15:07:21 -0500 |
| commit | e3174f1a67e626b6ebfe8889a466cc9630eea3a5 (patch) | |
| tree | fcada3741594986c63b4bd83a40e093ee35d9bbc | |
| parent | 9c04cd5285572698f8393cb2f1110bb92b3ea5d3 (diff) | |
Add Floor and comparison function specifications
| -rw-r--r-- | docs/spec/primitive.html | 31 | ||||
| -rw-r--r-- | spec/primitive.md | 35 |
2 files changed, 52 insertions, 14 deletions
diff --git a/docs/spec/primitive.html b/docs/spec/primitive.html index 36a22cfb..f2bd43bb 100644 --- a/docs/spec/primitive.html +++ b/docs/spec/primitive.html @@ -6,15 +6,16 @@ <div class="nav"><a href="https://github.com/mlochbaum/BQN">BQN</a> / <a href="../index.html">main</a> / <a href="index.html">spec</a></div> <h1 id="specification-bqn-primitives">Specification: BQN primitives</h1> <p>Most primitives are specified by the BQN-based implementation in <a href="https://github.com/mlochbaum/BQN/blob/master/spec/reference.bqn">reference.bqn</a>. This document specifies the basic functionality required by those definitions. Descriptions of other primitives are for informational purposes only.</p> -<h2 id="arithmetic">Arithmetic</h2> -<p>Functions here are defined for atoms only; the reference implementations extend them to arrays.</p> +<h2 id="pervasive-primitives">Pervasive primitives</h2> +<p>Functions in this section are defined for atoms only; the reference implementations extend them to arrays.</p> +<h3 id="arithmetic">Arithmetic</h3> <p>BQN uses five arithmetic functions that are standard in mathematics. The precision of these operations should be specified by the number <a href="types.html">type</a>.</p> <ul> -<li>Add <code><span class='Function'>+</span></code></li> -<li>Negate <code><span class='Function'>-</span></code> and Subtract <code><span class='Function'>-</span></code> invert addition, with <code><span class='Function'>-</span><span class='Value'>๐ฉ</span></code> equivalent to <code><span class='Number'>0</span><span class='Function'>-</span><span class='Value'>๐ฉ</span></code> and <code><span class='Value'>๐จ</span><span class='Function'>-</span><span class='Value'>๐ฉ</span></code> equivalent to <code><span class='Value'>๐จ</span><span class='Function'>+-</span><span class='Value'>๐ฉ</span></code></li> -<li>Multiply <code><span class='Function'>ร</span></code> generalizes repeated addition.</li> -<li>Divide <code><span class='Function'>รท</span></code> inverts multiplication, with <code><span class='Function'>รท</span><span class='Value'>๐ฉ</span></code> equivalent to <code><span class='Number'>1</span><span class='Function'>รท</span><span class='Value'>๐ฉ</span></code> and <code><span class='Value'>๐จ</span><span class='Function'>รท</span><span class='Value'>๐ฉ</span></code> equivalent to <code><span class='Value'>๐จ</span><span class='Function'>รรท</span><span class='Value'>๐ฉ</span></code></li> -<li>Power <code><span class='Function'>โ</span></code> generalizes repeated multiplication, and Exponential <code><span class='Function'>โ</span></code> is Power with Euler's number <em>e</em> as the base.</li> +<li><strong>Add</strong> <code><span class='Function'>+</span></code></li> +<li><strong>Negate</strong> and <strong>Subtract</strong> <code><span class='Function'>-</span></code> invert addition, with <code><span class='Function'>-</span><span class='Value'>๐ฉ</span></code> equivalent to <code><span class='Number'>0</span><span class='Function'>-</span><span class='Value'>๐ฉ</span></code>.</li> +<li><strong>Multiply</strong> <code><span class='Function'>ร</span></code> generalizes repeated addition.</li> +<li><strong>Divide</strong> and <strong>Reciprocal</strong> <code><span class='Function'>รท</span></code> invert multiplication, with <code><span class='Function'>รท</span><span class='Value'>๐ฉ</span></code> equivalent to <code><span class='Number'>1</span><span class='Function'>รท</span><span class='Value'>๐ฉ</span></code>.</li> +<li><strong>Power</strong> <code><span class='Function'>โ</span></code> generalizes repeated multiplication, and <strong>Exponential</strong> <code><span class='Function'>โ</span></code> is Power with Euler's number <em>e</em> as the base.</li> </ul> <p>The three higher functions <code><span class='Function'>ร</span></code>, <code><span class='Function'>รท</span></code>, and <code><span class='Function'>โ</span></code> apply to numbers and no other atomic types. <code><span class='Function'>+</span></code> and <code><span class='Function'>-</span></code> apply to numbers, and possibly also to characters, according to the rules of the affine character type:</p> <ul> @@ -23,3 +24,19 @@ <li>If both arguments to <code><span class='Function'>-</span></code> are characters, the result is the difference of their respective code points.</li> </ul> <p>In the first two cases, if the result would not be a valid Unicode code point, then an error results. The remaining cases of <code><span class='Function'>+</span></code> and <code><span class='Function'>-</span></code> (adding two characters; negating a character or subtracting it from a number) are not allowed.</p> +<p>Additionally, the <strong>Floor</strong> function <code><span class='Function'>โ</span></code> returns the largest integer smaller than the argument, or the argument itself if it is <code><span class='Number'>ยฏโ</span></code> or <code><span class='Number'>โ</span></code>. 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.</p> +<h3 id="comparison">Comparison</h3> +<p>Two kinds of comparison are needed to define BQN's primitives: <em>equality</em> comparison and <em>ordered</em> comparison.</p> +<p>Ordered comparison is simpler and is provided by the dyadic Less than or Equal to (<code><span class='Function'>โค</span></code>) 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 <code><span class='Number'>1</span></code> if the left argument's code point is less than that of the right argument. Characters are considered greater than numbers, so that <code><span class='Value'>n</span><span class='Function'>โค</span><span class='Value'>c</span></code> is <code><span class='Number'>1</span></code> and <code><span class='Value'>c</span><span class='Function'>โค</span><span class='Value'>n</span></code> is <code><span class='Number'>0</span></code> if <code><span class='Value'>c</span></code> is a character and <code><span class='Value'>n</span></code> is a number.</p> +<p>The dyadic function <code><span class='Function'>=</span></code>, representing equality comparison, can be applied to any two atoms without an error. Roughly speaking, it returns <code><span class='Number'>1</span></code> if they are indistinguishable within the language and <code><span class='Number'>0</span></code> otherwise. If the two arguments have different types, the result is <code><span class='Number'>0</span></code>; if they have the same type, the comparison depends on type:</p> +<ul> +<li>Equality of numbers is specified by the number type.</li> +<li>Characters are equal if they have the same code point.</li> +</ul> +<p>Operations are split into subtypes depending on how they were created.</p> +<ul> +<li>Primitives are equal if they have the same token spelling.</li> +<li>Derived operations are equal if they are derived by the same rule and each corresponding component is the same.</li> +<li>Block instances are equal if they are the same instance.</li> +</ul> +<p>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, <code><span class='Function'>=</span><span class='Modifier2'>โ</span><span class='Brace'>{</span><span class='Value'>๐ฉ</span><span class='Separator'>โ</span><span class='Brace'>{</span><span class='Value'>๐ฉ</span><span class='Brace'>}}</span><span class='Modifier'>ห</span><span class='String'>@</span></code> is <code><span class='Number'>0</span></code> while <code><span class='Function'>=</span><span class='Modifier'>ห</span><span class='Modifier2'>โ</span><span class='Brace'>{</span><span class='Value'>๐ฉ</span><span class='Separator'>โ</span><span class='Brace'>{</span><span class='Value'>๐ฉ</span><span class='Brace'>}}</span><span class='String'>@</span></code> is <code><span class='Number'>1</span></code>.</p> 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`. |
