diff options
| -rw-r--r-- | doc/logic.md | 60 | ||||
| -rw-r--r-- | docs/doc/logic.html | 106 |
2 files changed, 132 insertions, 34 deletions
diff --git a/doc/logic.md b/doc/logic.md index 89312e92..7895dfa5 100644 --- a/doc/logic.md +++ b/doc/logic.md @@ -2,23 +2,39 @@ # Logic functions: And, Or, Not (also Span) -BQN uses the mathematical symbols `∧` and `∨` for logical *and* and *or*, and `¬` for *not* (APL's `~` is discarded since it looks like `˜`, and is less common in mathematics today). That is, on two booleans `∧` is 1 if both are 1, and `∨` is if either is 1. `¬` flips its argument, returning 1 if the argument is 0 and 0 if it's 1. The logic functions are also considered [arithmetic](arithmetic.md) and thus are [pervasive](arithmetic.md#pervasion). +BQN uses the mathematical symbols `∧` for logical *and*, `∨` for *or*, and `¬` for *not*. That is, on booleans the result of `∧` is 1 if both arguments are 1, and `∨` is 1 if any argument is 1. `¬` flips its argument, returning 1 if the argument is 0 and 0 if it's 1. The logic functions are also considered [arithmetic](arithmetic.md) and thus are [pervasive](arithmetic.md#pervasion). -These boolean functions are arithmetically extended to apply to all numbers. Not returns `1-𝕩`, And returns `𝕨×𝕩`, and Or does a more complicated computation `𝕨×⌾¬𝕩`. +| `𝕨` | `𝕩` | `𝕨∧𝕩` | `𝕨∨𝕩` | `¬𝕩` +|:---:|:---:|:-----:|:-----:|:----: +| 0 | 0 | 0 | 0 | 1 +| 0 | 1 | 0 | 1 | 0 +| 1 | 0 | 0 | 1 | +| 1 | 1 | 1 | 1 | -Both valences of `¬` are equivalent to the fork `1+-`. The dyadic valence, called "Span", computes the number of integers in the range from `𝕩` to `𝕨`, inclusive, when both arguments are integers and `𝕩≤𝕨` (note the reversed order, which is used for consistency with subtraction). This function has many uses, and in particular is relevant to the [Windows](windows.md) function. +The three logic functions are extended linearly to apply to all numbers. This means Not returns `1-𝕩`, and And returns `𝕨×𝕩`. Or does a more complicated computation `𝕨×⌾¬𝕩` or `𝕨(+-×)𝕩`. + +Both valences of `¬` can be written as a [fork](train.md) `1+-`. The dyadic one, Span, computes the number of integers in the range from `𝕩` to `𝕨`, inclusive, when both arguments are integers and `𝕩≤𝕨` (the reversed order is used for consistency with subtraction). It often shows up in connection with the [Windows](windows.md) function. ## Examples -We can form truth [tables](map.md#table) including the non-integer value one-half: +And, Or, and Not can often be thought of as connecting logical statements together. So `(n<1) ∨ n>3` tests whether one of the two statements `n<1` or `n>3` holds. - ¬ 0‿0.5‿1 + n ← 4 - ∧⌜˜ 0‿0.5‿1 + (n<1) ⋈ n>3 # One false, one true - ∨⌜˜ 0‿0.5‿1 + (n<1) ∨ n>3 + +Of course, what actually happens is that those expressions are evaluated and the primitive acts on the results (both sides are always evaluated: there's nothing like the shortcutting of `&&` in some languages). Functions can be used more flexibly: for example, the [fold](fold.md) `∧´` indicates whether all values in a list are true, while `∨´` indicates if any is true. -As with logical And and Or, any value and 0 is 0, while any value or 1 is 1. The other boolean values give the identity values for the two functions: 1 and any value gives that value, as does 0 or the value. + ∧´ 1‿1‿1‿1‿1 + ∧´ 1‿1‿1‿0‿1 + +And the [scans](scan.md) `` ∧` `` and `` ∨` `` extend this notion to prefixes, switching permanently off at the first 0, or on at the first 1. + + ∧` 1‿1‿0‿0‿1‿0‿1 + + ∨` 0‿1‿0‿0‿1‿0‿1 ## Definitions @@ -28,24 +44,40 @@ We define And ← × Or ← ×⌾¬ -using a [train](train.md) for Not and [Under](under.md) for Or. The latter expands to `Or ← ¬∘×○¬`, since Not is a self-inverse `¬⁼ ←→ ¬`: when applying `¬` twice the first added 1 will be negated but the second won't; the two 1s cancel leaving two subtractions, and `-⁼ ←→ -`. An alternate definition of Or that matches the typical formula from probability theory is +using a [train](train.md) for Not and [Under](under.md) for Or. The latter expands to `Or ← ¬∘×○¬`, since Not is a self-inverse `¬⁼ ←→ ¬`: when applying `¬` twice, the first added 1 will be negated but the second won't; the two 1s cancel leaving two subtractions, and `-⁼ ←→ -`. An alternate definition of Or that matches the typical formula from probability theory is Or ← +-× +Building these definitions from arithmetic components makes it look like they should apply to any numbers, not just booleans. Well, they do. + +## Extension + The logic functions are extended to all numbers by making them linear in every argument. In the case of Not, that means the linear function `1⊸-`. The two-argument functions have bilinear extensions: And is identical to Times (`×`), while Or is `×⌾¬`, following De Morgan's laws (other ways of obtaining a function for Or give an equivalent result—there is only one bilinear extension). +Here are truth [tables](map.md#table) of these extensions including the non-integer value one-half: + + ¬ 0‿0.5‿1 + + ∧⌜˜ 0‿0.5‿1 + + ∨⌜˜ 0‿0.5‿1 + +As in logic, any value And 0 is 0, while any value Or 1 is 1. The other boolean values give the identity values for the two functions: 1 and any value gives that value, as does 0 or the value. + If the arguments are probabilities of independent events, then an extended function gives the probability of the boolean function on their outcomes. For example, if *A* occurs with probability `a` and *B* with probability `b` independent of *A*, then at least one of *A* or *B* occurs with probability `a∨b`. These extensions have also been used in complexity theory, because they allow mathematicians to transfer a logical circuit from the discrete to the continuous domain in order to use calculus on it. -## Identity values +### Identity values -It's common to apply a [fold](fold.md) `∧´` or `∨´` to a list (checking whether all elements are true and whether any are true, respectively), and so it's important for extensions to And and Or to share their [identity](fold.md#identity-values) value. [Minimum and Maximum](arithmetic.md#additional-arithmetic) do match And and Or when restricted to booleans, but they have different identity values. It would be dangerous to use Maximum to check whether any element of a list is true because `⌈´⟨⟩` yields `¯∞` instead of `0`—a bug waiting to happen. To avoid this the programmer would have to use an initial value `𝕨` of `0`, which is easy to forget. +The [folds](fold.md) `∧´` or `∨´` ought to work on empty lists, so And and Or should have the expected [identity](fold.md#identity-values) values 1 (an empty list *is* all 1s) and 0 (and yet has no 1s). [Minimum and Maximum](arithmetic.md#additional-arithmetic) do match And and Or when restricted to booleans, but they have different identity values. It would be dangerous to use Maximum to check whether any element of a list is true because `⌈´⟨⟩` yields `¯∞` instead of `0`—a bug waiting to happen. To avoid this you'd have to always use an initial value `𝕨` of `0`, which is easy to forget. -It's not hard to prove that the bilinear extensions have the identity values we want. Of course `1∧x` is `1×x`, or `x`, and `0∨x` is `0×⌾¬x`, or `¬1׬x`, giving `¬¬x` or `x` again. Both functions are commutative, so these values are identities on the right as well. +It's not hard to prove that the bilinear extensions have these identity values. Of course `1∧x` is `1×x`, or `x`, and `0∨x` is `0×⌾¬x`, or `¬1׬x`, giving `¬¬x` or `x` again. Both functions are commutative, so these values are identities on the right as well. -Other logical identities do not necessarily hold. For example, in boolean logic And distributes over Or and vice-versa: `a∧b∨c ←→ (a∧b)∨(a∧c)`. But substituting `×` for `∧` and `+-×` for `∨` we find that the left hand side is `(a×b)+(a×c)+(a×b×c)` while the right gives `(a×b)+(a×c)+(a×b×a×c)`. These are equivalent for arbitrary `b` and `c` only if `a=a×a`, that is, `a` is 0 or 1. In terms of probabilities the difference when `a` is not boolean is caused by failure of independence. On the left hand side, the two arguments of every logical function are independent. On the right hand side, each pair of arguments to `∧` are independent, but the two arguments to `∨`, `a∧b` and `a∧c`, are not. The relationship between these arguments means that logical equivalences no longer apply. +Some other logical identities don't always hold. For example, in boolean logic And distributes over Or and vice-versa: `a∧b∨c ←→ (a∧b)∨(a∧c)`. But substituting `×` for `∧` and `+-×` for `∨` we find that the left hand side is `(a×b)+(a×c)+(a×b×c)` while the right gives `(a×b)+(a×c)+(a×b×a×c)`. These are equivalent for arbitrary `b` and `c` only if `a=a×a`, that is, `a` is 0 or 1. In terms of probabilities the difference when `a` is not boolean is caused by failure of independence. On the left hand side, the two arguments of every logical function are independent. On the right hand side, each pair of arguments to `∧` are independent, but the two arguments to `∨`, `a∧b` and `a∧c`, are not. The relationship between these arguments means that logical equivalences no longer apply. -## Why not GCD and LCM? +### Why not GCD and LCM? APL provides [GCD](https://aplwiki.com/wiki/GCD) and [LCM](https://aplwiki.com/wiki/LCM) as extensions of And and Or, while BQN doesn't make these functions primitives. The main reason for omitting them functions is that they are complicated and, when applied to real or complex numbers, require a significant number of design decisions where there's no obvious choice (for example, whether to use comparison tolerance). On the other hand, these functions are fairly easy to implement, which allows the programmer to control the details, and also add functionality such as the extended GCD. Possible implementations for GCD and LCM are shown in [bqncrate](https://mlochbaum.github.io/bqncrate) ([GCD](https://mlochbaum.github.io/bqncrate/?q=gcd), [LCM](https://mlochbaum.github.io/bqncrate/?q=lcm)). A secondary reason is that the GCD falls short as an extension of Or, because its identity value 0 is not total. `0∨x`, for a real number `x`, is actually equal to `|x` and not `x`: for example, `0∨¯2` is `2` in APL. This means the identity `0∨x ←→ x` isn't reliable in APL. + +Unrelatedly, the reason BQN discards APL's `~` for negation is that it looks like `˜`, and is less common in mathematics today. diff --git a/docs/doc/logic.html b/docs/doc/logic.html index 18cf1add..6a69d995 100644 --- a/docs/doc/logic.html +++ b/docs/doc/logic.html @@ -5,11 +5,86 @@ </head> <div class="nav">(<a href="https://github.com/mlochbaum/BQN">github</a>) / <a href="../index.html">BQN</a> / <a href="index.html">doc</a></div> <h1 id="logic-functions-and-or-not-also-span"><a class="header" href="#logic-functions-and-or-not-also-span">Logic functions: And, Or, Not (also Span)</a></h1> -<p>BQN uses the mathematical symbols <code><span class='Function'>∧</span></code> and <code><span class='Function'>∨</span></code> for logical <em>and</em> and <em>or</em>, and <code><span class='Function'>¬</span></code> for <em>not</em> (APL's <code><span class='Value'>~</span></code> is discarded since it looks like <code><span class='Modifier'>˜</span></code>, and is less common in mathematics today). That is, on two booleans <code><span class='Function'>∧</span></code> is 1 if both are 1, and <code><span class='Function'>∨</span></code> is if either is 1. <code><span class='Function'>¬</span></code> flips its argument, returning 1 if the argument is 0 and 0 if it's 1. The logic functions are also considered <a href="arithmetic.html">arithmetic</a> and thus are <a href="arithmetic.html#pervasion">pervasive</a>.</p> -<p>These boolean functions are arithmetically extended to apply to all numbers. Not returns <code><span class='Number'>1</span><span class='Function'>-</span><span class='Value'>𝕩</span></code>, And returns <code><span class='Value'>𝕨</span><span class='Function'>×</span><span class='Value'>𝕩</span></code>, and Or does a more complicated computation <code><span class='Value'>𝕨</span><span class='Function'>×</span><span class='Modifier2'>⌾</span><span class='Function'>¬</span><span class='Value'>𝕩</span></code>.</p> -<p>Both valences of <code><span class='Function'>¬</span></code> are equivalent to the fork <code><span class='Number'>1</span><span class='Function'>+-</span></code>. The dyadic valence, called "Span", computes the number of integers in the range from <code><span class='Value'>𝕩</span></code> to <code><span class='Value'>𝕨</span></code>, inclusive, when both arguments are integers and <code><span class='Value'>𝕩</span><span class='Function'>≤</span><span class='Value'>𝕨</span></code> (note the reversed order, which is used for consistency with subtraction). This function has many uses, and in particular is relevant to the <a href="windows.html">Windows</a> function.</p> +<p>BQN uses the mathematical symbols <code><span class='Function'>∧</span></code> for logical <em>and</em>, <code><span class='Function'>∨</span></code> for <em>or</em>, and <code><span class='Function'>¬</span></code> for <em>not</em>. That is, on booleans the result of <code><span class='Function'>∧</span></code> is 1 if both arguments are 1, and <code><span class='Function'>∨</span></code> is 1 if any argument is 1. <code><span class='Function'>¬</span></code> flips its argument, returning 1 if the argument is 0 and 0 if it's 1. The logic functions are also considered <a href="arithmetic.html">arithmetic</a> and thus are <a href="arithmetic.html#pervasion">pervasive</a>.</p> +<table> +<thead> +<tr> +<th align="center"><code><span class='Value'>𝕨</span></code></th> +<th align="center"><code><span class='Value'>𝕩</span></code></th> +<th align="center"><code><span class='Value'>𝕨</span><span class='Function'>∧</span><span class='Value'>𝕩</span></code></th> +<th align="center"><code><span class='Value'>𝕨</span><span class='Function'>∨</span><span class='Value'>𝕩</span></code></th> +<th align="center"><code><span class='Function'>¬</span><span class='Value'>𝕩</span></code></th> +</tr> +</thead> +<tbody> +<tr> +<td align="center">0</td> +<td align="center">0</td> +<td align="center">0</td> +<td align="center">0</td> +<td align="center">1</td> +</tr> +<tr> +<td align="center">0</td> +<td align="center">1</td> +<td align="center">0</td> +<td align="center">1</td> +<td align="center">0</td> +</tr> +<tr> +<td align="center">1</td> +<td align="center">0</td> +<td align="center">0</td> +<td align="center">1</td> +<td align="center"></td> +</tr> +<tr> +<td align="center">1</td> +<td align="center">1</td> +<td align="center">1</td> +<td align="center">1</td> +<td align="center"></td> +</tr> +</tbody> +</table> +<p>The three logic functions are extended linearly to apply to all numbers. This means Not returns <code><span class='Number'>1</span><span class='Function'>-</span><span class='Value'>𝕩</span></code>, and And returns <code><span class='Value'>𝕨</span><span class='Function'>×</span><span class='Value'>𝕩</span></code>. Or does a more complicated computation <code><span class='Value'>𝕨</span><span class='Function'>×</span><span class='Modifier2'>⌾</span><span class='Function'>¬</span><span class='Value'>𝕩</span></code> or <code><span class='Value'>𝕨</span><span class='Paren'>(</span><span class='Function'>+-×</span><span class='Paren'>)</span><span class='Value'>𝕩</span></code>.</p> +<p>Both valences of <code><span class='Function'>¬</span></code> can be written as a <a href="train.html">fork</a> <code><span class='Number'>1</span><span class='Function'>+-</span></code>. The dyadic one, Span, computes the number of integers in the range from <code><span class='Value'>𝕩</span></code> to <code><span class='Value'>𝕨</span></code>, inclusive, when both arguments are integers and <code><span class='Value'>𝕩</span><span class='Function'>≤</span><span class='Value'>𝕨</span></code> (the reversed order is used for consistency with subtraction). It often shows up in connection with the <a href="windows.html">Windows</a> function.</p> <h2 id="examples"><a class="header" href="#examples">Examples</a></h2> -<p>We can form truth <a href="map.html#table">tables</a> including the non-integer value one-half:</p> +<p>And, Or, and Not can often be thought of as connecting logical statements together. So <code><span class='Paren'>(</span><span class='Value'>n</span><span class='Function'><</span><span class='Number'>1</span><span class='Paren'>)</span> <span class='Function'>∨</span> <span class='Value'>n</span><span class='Function'>></span><span class='Number'>3</span></code> tests whether one of the two statements <code><span class='Value'>n</span><span class='Function'><</span><span class='Number'>1</span></code> or <code><span class='Value'>n</span><span class='Function'>></span><span class='Number'>3</span></code> holds.</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=biDihpAgNAoKKG48MSkg4ouIIG4+MyAgIyBPbmUgZmFsc2UsIG9uZSB0cnVlCgoobjwxKSDiiKggbj4z">↗️</a><pre> <span class='Value'>n</span> <span class='Gets'>←</span> <span class='Number'>4</span> + + <span class='Paren'>(</span><span class='Value'>n</span><span class='Function'><</span><span class='Number'>1</span><span class='Paren'>)</span> <span class='Function'>⋈</span> <span class='Value'>n</span><span class='Function'>></span><span class='Number'>3</span> <span class='Comment'># One false, one true +</span>⟨ 0 1 ⟩ + + <span class='Paren'>(</span><span class='Value'>n</span><span class='Function'><</span><span class='Number'>1</span><span class='Paren'>)</span> <span class='Function'>∨</span> <span class='Value'>n</span><span class='Function'>></span><span class='Number'>3</span> +1 +</pre> +<p>Of course, what actually happens is that those expressions are evaluated and the primitive acts on the results (both sides are always evaluated: there's nothing like the shortcutting of <code><span class='Value'>&&</span></code> in some languages). Functions can be used more flexibly: for example, the <a href="fold.html">fold</a> <code><span class='Function'>∧</span><span class='Modifier'>´</span></code> indicates whether all values in a list are true, while <code><span class='Function'>∨</span><span class='Modifier'>´</span></code> indicates if any is true.</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oinwrQgMeKAvzHigL8x4oC/MeKAvzEK4oinwrQgMeKAvzHigL8x4oC/MOKAvzE=">↗️</a><pre> <span class='Function'>∧</span><span class='Modifier'>´</span> <span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>1</span> +1 + <span class='Function'>∧</span><span class='Modifier'>´</span> <span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span> +0 +</pre> +<p>And the <a href="scan.html">scans</a> <code><span class='Function'>∧</span><span class='Modifier'>`</span></code> and <code><span class='Function'>∨</span><span class='Modifier'>`</span></code> extend this notion to prefixes, switching permanently off at the first 0, or on at the first 1.</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oinYCAx4oC/MeKAvzDigL8w4oC/MeKAvzDigL8xCgriiKhgIDDigL8x4oC/MOKAvzDigL8x4oC/MOKAvzE=">↗️</a><pre> <span class='Function'>∧</span><span class='Modifier'>`</span> <span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span> +⟨ 1 1 0 0 0 0 0 ⟩ + + <span class='Function'>∨</span><span class='Modifier'>`</span> <span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span> +⟨ 0 1 1 1 1 1 1 ⟩ +</pre> +<h2 id="definitions"><a class="header" href="#definitions">Definitions</a></h2> +<p>We define</p> +<pre><span class='Function'>Not</span> <span class='Gets'>←</span> <span class='Number'>1</span><span class='Function'>+-</span> <span class='Comment'># also Span +</span><span class='Function'>And</span> <span class='Gets'>←</span> <span class='Function'>×</span> +<span class='Function'>Or</span> <span class='Gets'>←</span> <span class='Function'>×</span><span class='Modifier2'>⌾</span><span class='Function'>¬</span> +</pre> +<p>using a <a href="train.html">train</a> for Not and <a href="under.html">Under</a> for Or. The latter expands to <code><span class='Function'>Or</span> <span class='Gets'>←</span> <span class='Function'>¬</span><span class='Modifier2'>∘</span><span class='Function'>×</span><span class='Modifier2'>○</span><span class='Function'>¬</span></code>, since Not is a self-inverse <code><span class='Function'>¬</span><span class='Modifier'>⁼</span> <span class='Gets'>←→</span> <span class='Function'>¬</span></code>: when applying <code><span class='Function'>¬</span></code> twice, the first added 1 will be negated but the second won't; the two 1s cancel leaving two subtractions, and <code><span class='Function'>-</span><span class='Modifier'>⁼</span> <span class='Gets'>←→</span> <span class='Function'>-</span></code>. An alternate definition of Or that matches the typical formula from probability theory is</p> +<pre><span class='Function'>Or</span> <span class='Gets'>←</span> <span class='Function'>+-×</span> +</pre> +<p>Building these definitions from arithmetic components makes it look like they should apply to any numbers, not just booleans. Well, they do.</p> +<h2 id="extension"><a class="header" href="#extension">Extension</a></h2> +<p>The logic functions are extended to all numbers by making them linear in every argument. In the case of Not, that means the linear function <code><span class='Number'>1</span><span class='Modifier2'>⊸</span><span class='Function'>-</span></code>. The two-argument functions have bilinear extensions: And is identical to Times (<code><span class='Function'>×</span></code>), while Or is <code><span class='Function'>×</span><span class='Modifier2'>⌾</span><span class='Function'>¬</span></code>, following De Morgan's laws (other ways of obtaining a function for Or give an equivalent result—there is only one bilinear extension).</p> +<p>Here are truth <a href="map.html#table">tables</a> of these extensions including the non-integer value one-half:</p> <a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=wqwgMOKAvzAuNeKAvzEKCuKIp+KMnMucIDDigL8wLjXigL8xCgriiKjijJzLnCAw4oC/MC414oC/MQ==">↗️</a><pre> <span class='Function'>¬</span> <span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>0.5</span><span class='Ligature'>‿</span><span class='Number'>1</span> ⟨ 1 0.5 0 ⟩ @@ -27,22 +102,13 @@ 1 1 1 ┘ </pre> -<p>As with logical And and Or, any value and 0 is 0, while any value or 1 is 1. The other boolean values give the identity values for the two functions: 1 and any value gives that value, as does 0 or the value.</p> -<h2 id="definitions"><a class="header" href="#definitions">Definitions</a></h2> -<p>We define</p> -<pre><span class='Function'>Not</span> <span class='Gets'>←</span> <span class='Number'>1</span><span class='Function'>+-</span> <span class='Comment'># also Span -</span><span class='Function'>And</span> <span class='Gets'>←</span> <span class='Function'>×</span> -<span class='Function'>Or</span> <span class='Gets'>←</span> <span class='Function'>×</span><span class='Modifier2'>⌾</span><span class='Function'>¬</span> -</pre> -<p>using a <a href="train.html">train</a> for Not and <a href="under.html">Under</a> for Or. The latter expands to <code><span class='Function'>Or</span> <span class='Gets'>←</span> <span class='Function'>¬</span><span class='Modifier2'>∘</span><span class='Function'>×</span><span class='Modifier2'>○</span><span class='Function'>¬</span></code>, since Not is a self-inverse <code><span class='Function'>¬</span><span class='Modifier'>⁼</span> <span class='Gets'>←→</span> <span class='Function'>¬</span></code>: when applying <code><span class='Function'>¬</span></code> twice the first added 1 will be negated but the second won't; the two 1s cancel leaving two subtractions, and <code><span class='Function'>-</span><span class='Modifier'>⁼</span> <span class='Gets'>←→</span> <span class='Function'>-</span></code>. An alternate definition of Or that matches the typical formula from probability theory is</p> -<pre><span class='Function'>Or</span> <span class='Gets'>←</span> <span class='Function'>+-×</span> -</pre> -<p>The logic functions are extended to all numbers by making them linear in every argument. In the case of Not, that means the linear function <code><span class='Number'>1</span><span class='Modifier2'>⊸</span><span class='Function'>-</span></code>. The two-argument functions have bilinear extensions: And is identical to Times (<code><span class='Function'>×</span></code>), while Or is <code><span class='Function'>×</span><span class='Modifier2'>⌾</span><span class='Function'>¬</span></code>, following De Morgan's laws (other ways of obtaining a function for Or give an equivalent result—there is only one bilinear extension).</p> +<p>As in logic, any value And 0 is 0, while any value Or 1 is 1. The other boolean values give the identity values for the two functions: 1 and any value gives that value, as does 0 or the value.</p> <p>If the arguments are probabilities of independent events, then an extended function gives the probability of the boolean function on their outcomes. For example, if <em>A</em> occurs with probability <code><span class='Value'>a</span></code> and <em>B</em> with probability <code><span class='Value'>b</span></code> independent of <em>A</em>, then at least one of <em>A</em> or <em>B</em> occurs with probability <code><span class='Value'>a</span><span class='Function'>∨</span><span class='Value'>b</span></code>. These extensions have also been used in complexity theory, because they allow mathematicians to transfer a logical circuit from the discrete to the continuous domain in order to use calculus on it.</p> -<h2 id="identity-values"><a class="header" href="#identity-values">Identity values</a></h2> -<p>It's common to apply a <a href="fold.html">fold</a> <code><span class='Function'>∧</span><span class='Modifier'>´</span></code> or <code><span class='Function'>∨</span><span class='Modifier'>´</span></code> to a list (checking whether all elements are true and whether any are true, respectively), and so it's important for extensions to And and Or to share their <a href="fold.html#identity-values">identity</a> value. <a href="arithmetic.html#additional-arithmetic">Minimum and Maximum</a> do match And and Or when restricted to booleans, but they have different identity values. It would be dangerous to use Maximum to check whether any element of a list is true because <code><span class='Function'>⌈</span><span class='Modifier'>´</span><span class='Bracket'>⟨⟩</span></code> yields <code><span class='Number'>¯∞</span></code> instead of <code><span class='Number'>0</span></code>—a bug waiting to happen. To avoid this the programmer would have to use an initial value <code><span class='Value'>𝕨</span></code> of <code><span class='Number'>0</span></code>, which is easy to forget.</p> -<p>It's not hard to prove that the bilinear extensions have the identity values we want. Of course <code><span class='Number'>1</span><span class='Function'>∧</span><span class='Value'>x</span></code> is <code><span class='Number'>1</span><span class='Function'>×</span><span class='Value'>x</span></code>, or <code><span class='Value'>x</span></code>, and <code><span class='Number'>0</span><span class='Function'>∨</span><span class='Value'>x</span></code> is <code><span class='Number'>0</span><span class='Function'>×</span><span class='Modifier2'>⌾</span><span class='Function'>¬</span><span class='Value'>x</span></code>, or <code><span class='Function'>¬</span><span class='Number'>1</span><span class='Function'>׬</span><span class='Value'>x</span></code>, giving <code><span class='Function'>¬¬</span><span class='Value'>x</span></code> or <code><span class='Value'>x</span></code> again. Both functions are commutative, so these values are identities on the right as well.</p> -<p>Other logical identities do not necessarily hold. For example, in boolean logic And distributes over Or and vice-versa: <code><span class='Value'>a</span><span class='Function'>∧</span><span class='Value'>b</span><span class='Function'>∨</span><span class='Value'>c</span> <span class='Gets'>←→</span> <span class='Paren'>(</span><span class='Value'>a</span><span class='Function'>∧</span><span class='Value'>b</span><span class='Paren'>)</span><span class='Function'>∨</span><span class='Paren'>(</span><span class='Value'>a</span><span class='Function'>∧</span><span class='Value'>c</span><span class='Paren'>)</span></code>. But substituting <code><span class='Function'>×</span></code> for <code><span class='Function'>∧</span></code> and <code><span class='Function'>+-×</span></code> for <code><span class='Function'>∨</span></code> we find that the left hand side is <code><span class='Paren'>(</span><span class='Value'>a</span><span class='Function'>×</span><span class='Value'>b</span><span class='Paren'>)</span><span class='Function'>+</span><span class='Paren'>(</span><span class='Value'>a</span><span class='Function'>×</span><span class='Value'>c</span><span class='Paren'>)</span><span class='Function'>+</span><span class='Paren'>(</span><span class='Value'>a</span><span class='Function'>×</span><span class='Value'>b</span><span class='Function'>×</span><span class='Value'>c</span><span class='Paren'>)</span></code> while the right gives <code><span class='Paren'>(</span><span class='Value'>a</span><span class='Function'>×</span><span class='Value'>b</span><span class='Paren'>)</span><span class='Function'>+</span><span class='Paren'>(</span><span class='Value'>a</span><span class='Function'>×</span><span class='Value'>c</span><span class='Paren'>)</span><span class='Function'>+</span><span class='Paren'>(</span><span class='Value'>a</span><span class='Function'>×</span><span class='Value'>b</span><span class='Function'>×</span><span class='Value'>a</span><span class='Function'>×</span><span class='Value'>c</span><span class='Paren'>)</span></code>. These are equivalent for arbitrary <code><span class='Value'>b</span></code> and <code><span class='Value'>c</span></code> only if <code><span class='Value'>a</span><span class='Function'>=</span><span class='Value'>a</span><span class='Function'>×</span><span class='Value'>a</span></code>, that is, <code><span class='Value'>a</span></code> is 0 or 1. In terms of probabilities the difference when <code><span class='Value'>a</span></code> is not boolean is caused by failure of independence. On the left hand side, the two arguments of every logical function are independent. On the right hand side, each pair of arguments to <code><span class='Function'>∧</span></code> are independent, but the two arguments to <code><span class='Function'>∨</span></code>, <code><span class='Value'>a</span><span class='Function'>∧</span><span class='Value'>b</span></code> and <code><span class='Value'>a</span><span class='Function'>∧</span><span class='Value'>c</span></code>, are not. The relationship between these arguments means that logical equivalences no longer apply.</p> -<h2 id="why-not-gcd-and-lcm"><a class="header" href="#why-not-gcd-and-lcm">Why not GCD and LCM?</a></h2> +<h3 id="identity-values"><a class="header" href="#identity-values">Identity values</a></h3> +<p>The <a href="fold.html">folds</a> <code><span class='Function'>∧</span><span class='Modifier'>´</span></code> or <code><span class='Function'>∨</span><span class='Modifier'>´</span></code> ought to work on empty lists, so And and Or should have the expected <a href="fold.html#identity-values">identity</a> values 1 (an empty list <em>is</em> all 1s) and 0 (and yet has no 1s). <a href="arithmetic.html#additional-arithmetic">Minimum and Maximum</a> do match And and Or when restricted to booleans, but they have different identity values. It would be dangerous to use Maximum to check whether any element of a list is true because <code><span class='Function'>⌈</span><span class='Modifier'>´</span><span class='Bracket'>⟨⟩</span></code> yields <code><span class='Number'>¯∞</span></code> instead of <code><span class='Number'>0</span></code>—a bug waiting to happen. To avoid this you'd have to always use an initial value <code><span class='Value'>𝕨</span></code> of <code><span class='Number'>0</span></code>, which is easy to forget.</p> +<p>It's not hard to prove that the bilinear extensions have these identity values. Of course <code><span class='Number'>1</span><span class='Function'>∧</span><span class='Value'>x</span></code> is <code><span class='Number'>1</span><span class='Function'>×</span><span class='Value'>x</span></code>, or <code><span class='Value'>x</span></code>, and <code><span class='Number'>0</span><span class='Function'>∨</span><span class='Value'>x</span></code> is <code><span class='Number'>0</span><span class='Function'>×</span><span class='Modifier2'>⌾</span><span class='Function'>¬</span><span class='Value'>x</span></code>, or <code><span class='Function'>¬</span><span class='Number'>1</span><span class='Function'>׬</span><span class='Value'>x</span></code>, giving <code><span class='Function'>¬¬</span><span class='Value'>x</span></code> or <code><span class='Value'>x</span></code> again. Both functions are commutative, so these values are identities on the right as well.</p> +<p>Some other logical identities don't always hold. For example, in boolean logic And distributes over Or and vice-versa: <code><span class='Value'>a</span><span class='Function'>∧</span><span class='Value'>b</span><span class='Function'>∨</span><span class='Value'>c</span> <span class='Gets'>←→</span> <span class='Paren'>(</span><span class='Value'>a</span><span class='Function'>∧</span><span class='Value'>b</span><span class='Paren'>)</span><span class='Function'>∨</span><span class='Paren'>(</span><span class='Value'>a</span><span class='Function'>∧</span><span class='Value'>c</span><span class='Paren'>)</span></code>. But substituting <code><span class='Function'>×</span></code> for <code><span class='Function'>∧</span></code> and <code><span class='Function'>+-×</span></code> for <code><span class='Function'>∨</span></code> we find that the left hand side is <code><span class='Paren'>(</span><span class='Value'>a</span><span class='Function'>×</span><span class='Value'>b</span><span class='Paren'>)</span><span class='Function'>+</span><span class='Paren'>(</span><span class='Value'>a</span><span class='Function'>×</span><span class='Value'>c</span><span class='Paren'>)</span><span class='Function'>+</span><span class='Paren'>(</span><span class='Value'>a</span><span class='Function'>×</span><span class='Value'>b</span><span class='Function'>×</span><span class='Value'>c</span><span class='Paren'>)</span></code> while the right gives <code><span class='Paren'>(</span><span class='Value'>a</span><span class='Function'>×</span><span class='Value'>b</span><span class='Paren'>)</span><span class='Function'>+</span><span class='Paren'>(</span><span class='Value'>a</span><span class='Function'>×</span><span class='Value'>c</span><span class='Paren'>)</span><span class='Function'>+</span><span class='Paren'>(</span><span class='Value'>a</span><span class='Function'>×</span><span class='Value'>b</span><span class='Function'>×</span><span class='Value'>a</span><span class='Function'>×</span><span class='Value'>c</span><span class='Paren'>)</span></code>. These are equivalent for arbitrary <code><span class='Value'>b</span></code> and <code><span class='Value'>c</span></code> only if <code><span class='Value'>a</span><span class='Function'>=</span><span class='Value'>a</span><span class='Function'>×</span><span class='Value'>a</span></code>, that is, <code><span class='Value'>a</span></code> is 0 or 1. In terms of probabilities the difference when <code><span class='Value'>a</span></code> is not boolean is caused by failure of independence. On the left hand side, the two arguments of every logical function are independent. On the right hand side, each pair of arguments to <code><span class='Function'>∧</span></code> are independent, but the two arguments to <code><span class='Function'>∨</span></code>, <code><span class='Value'>a</span><span class='Function'>∧</span><span class='Value'>b</span></code> and <code><span class='Value'>a</span><span class='Function'>∧</span><span class='Value'>c</span></code>, are not. The relationship between these arguments means that logical equivalences no longer apply.</p> +<h3 id="why-not-gcd-and-lcm"><a class="header" href="#why-not-gcd-and-lcm">Why not GCD and LCM?</a></h3> <p>APL provides <a href="https://aplwiki.com/wiki/GCD">GCD</a> and <a href="https://aplwiki.com/wiki/LCM">LCM</a> as extensions of And and Or, while BQN doesn't make these functions primitives. The main reason for omitting them functions is that they are complicated and, when applied to real or complex numbers, require a significant number of design decisions where there's no obvious choice (for example, whether to use comparison tolerance). On the other hand, these functions are fairly easy to implement, which allows the programmer to control the details, and also add functionality such as the extended GCD. Possible implementations for GCD and LCM are shown in <a href="https://mlochbaum.github.io/bqncrate">bqncrate</a> (<a href="https://mlochbaum.github.io/bqncrate/?q=gcd">GCD</a>, <a href="https://mlochbaum.github.io/bqncrate/?q=lcm">LCM</a>).</p> <p>A secondary reason is that the GCD falls short as an extension of Or, because its identity value 0 is not total. <code><span class='Number'>0</span><span class='Function'>∨</span><span class='Value'>x</span></code>, for a real number <code><span class='Value'>x</span></code>, is actually equal to <code><span class='Function'>|</span><span class='Value'>x</span></code> and not <code><span class='Value'>x</span></code>: for example, <code><span class='Number'>0</span><span class='Function'>∨</span><span class='Number'>¯2</span></code> is <code><span class='Number'>2</span></code> in APL. This means the identity <code><span class='Number'>0</span><span class='Function'>∨</span><span class='Value'>x</span> <span class='Gets'>←→</span> <span class='Value'>x</span></code> isn't reliable in APL.</p> +<p>Unrelatedly, the reason BQN discards APL's <code><span class='Value'>~</span></code> for negation is that it looks like <code><span class='Modifier'>˜</span></code>, and is less common in mathematics today.</p> |
