From 8aa389aa1e6665df6b23fcbc0586e57972b668f9 Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Fri, 26 Mar 2021 22:45:11 -0400 Subject: Update VM docs for SetPrims system --- docs/implementation/vm.html | 65 +++++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 32 deletions(-) (limited to 'docs/implementation') diff --git a/docs/implementation/vm.html b/docs/implementation/vm.html index 46a8aa06..bb99151c 100644 --- a/docs/implementation/vm.html +++ b/docs/implementation/vm.html @@ -380,7 +380,7 @@

SETM additionally needs to get the current value of a reference. For a variable reference this is its current value (with an error if it's not defined yet); for a reference list it's a list of the values of each reference in the list.

Runtime

Primitive functions and modifiers used in a program are stored in its consts array. The compiler needs to be passed a runtime with the value of every primitive so that these functions and modifiers are available.

-

While it's perfectly possible to implement the runtime from scratch, the pseudo-BQN file r.bqn implements the full runtime in terms of a core runtime consisting of a smaller number of much simpler functions. pr.bqn converts this file so that it can be compiled. It changes values in the core runtime to primitives and primitives to generated identifiers, so that the first 21 values in the output's consts array are exactly the core runtime, and no other primitives are required.

+

While it's perfectly possible to implement the runtime from scratch, the pseudo-BQN file r.bqn implements the full runtime in terms of a core runtime consisting of a smaller number of much simpler functions. pr.bqn converts this file so that it can be compiled. It changes values in the core runtime to primitives and primitives to generated identifiers, so that the first 22 values in the output's consts array are exactly the core runtime, and no other primitives are required. The result is a list of two elements: first the list of all primitive values, and then a function that can be called to pass in two additional core functions used for inferred properties.

The contents of a core runtime are given below. The names given are those used in r.bqn; the environment only provides a list of values and therefore doesn't need to use the same names. For named functions a description is given. For primitives, the implementation should match the BQN specification for that primitive on the specified domain, or the entire domain if left empty. They won't be called outside that domain except if there are bugs in r.bqn.

@@ -398,122 +398,123 @@ - - - - - - - - - - - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + + + + + + + + + + +
1Decomposeβ€’Decompose
2Glyph(Unused) β€’Glyph for primitive 𝕩
3 Fill Get or set the fill value for array 𝕩
42 Log ⋆⁼ (natural or base-𝕨 logarithm) for atomic arguments
53 GroupLen β‰ Β¨βŠ”π•© for a valid list 𝕩
64 GroupOrd βˆΎβŠ”π•© provided 𝕨 is GroupLen 𝕩
75 !
86 + On two atoms
97 - On one or two atoms
108 Γ— On two atoms
119 Γ· On one or two atoms
1210 ⋆ On one or two atoms
1311 ⌊ On one atom
1412 = On one value or two atoms
1513 ≀ On two atoms
1614 β‰’ For array 𝕩
1715 β₯Š For array 𝕩 with no 𝕨 or 𝕨=β—‹(Γ—Β΄)≒𝕩
1816 βŠ‘ For atom 𝕨 and list 𝕩
1917 ↕ For natural number 𝕩
2018 ⌜ On arrays
2119 `
2220 _fillBy_ 𝔽 with result fill computed using 𝔾
2321 ⊘
β€”Decomposeβ€’Decompose
β€”PrimIndIndex for primitive 𝕩
-

Remember that + and - can also work on characters in some circumstances, under the rules of affine characters. Other arithmetic functions should only accept numbers. = must work on numbers, characters, and primitives, and should give 0 without causing an error if the arguments have different types or one is a primitive and the other isn't. ≀ must work on numbers and characters.

+

To define the final two functions, call the second returned element as a function, with argument Decomposeβ€ΏPrimInd. The function PrimInd gives the index of 𝕩 in the list of all primitives (defined as glyphs in pr.bqn), or the length of the runtime if 𝕩 is not a primitive. The two functions are only needed for computing inferred properties, and are defined by default so that every value is assumed to be a primitive, and PrimInd performs a linear search over the returned runtime. If the runtime is used directly, then this means that without setting Decomposeβ€ΏPrimInd, function inferred properties will work slowly and for primitives only; if values from the runtime are wrapped then function inferred properties will not work at all.

+

Remember that + and - can also work on characters in some circumstances, under the rules of affine characters. Other arithmetic functions should only accept numbers. = must work on any atoms including functions and modifiers. ≀ must work on numbers and characters.

GroupLen and GroupOrd

GroupLen and GroupOrd, short for Group length and Group order, are used to implement Group (βŠ”) and also to grade small-range lists and invert permutations (the inverse of a permutation p is 1¨⊸GroupOrd p). Each of these only needs to work on lists of integers. Shown below are efficient implementations using BQN extended with the notation (iβŠ‘l) Fn↩ x meaning l ↩ Fn⟜x⌾(iβŠΈβŠ‘) l, where Fn is ⊒ if omitted. Since these special assignments only change one element of l, each can be a fast constant-time operation.

GroupLen ← {
-- 
cgit v1.2.3