This site is version 0.0.1. Most of the code on this site is drafted by an agent and needs review. I apologize for the AI-speak, we are working on rewriting it!
viv-lang.org

Notebooks · lib/cas/store

The expression store of lib/cas, one node per distinct expression

An expression is a number, the id of a node in one store. The store keeps one node per distinct (kind, payload, children): making a node looks its key up in a map and returns the id already there, so two equal expressions have the same id and equality is a == b.

| kind | payload | children | |---|---|---| | K_NUM | the rational p/q | none | | K_SYM | the name's id | none | | K_ADD | | the terms, numeric term first, then by the term order | | K_MUL | | the factors, numeric coefficient first, then by the term order | | K_POW | | base, exponent | | K_FN | the head's name id | the argument | | K_FAIL | the reason's id | none |

Subtraction is a + (-1) b and division a b^{-1}, so there is one form to match. sqrt(u) is u^{1/2}. A K_FAIL node is a refusal with its reason: every constructor given one returns it, so a refusal deep inside an expression reaches the top with its words, never as a wrong number.

Automatic normal form, applied by every constructor and nothing else: flatten nested sums and products; add and multiply the numbers exactly; collect 2x + 3x = 5x and x^2 x^3 = x^5 (integer exponents, or any rational exponent of a positive number); drop x + 0, 1 x, x^1, x^0; order the arguments; and the exact values exp(0) = 1, log(1) = 0, sine and cosine at multiples of and , log(exp(u)) = u. Nothing expands: 2 (x + 1) stays a product until cas_expand (lib/cas/calculus) is asked.

Term order. Numbers first, by value; every other node by its base and then its exponent (x is x^1), bases compared by kind (symbol, function, power, product, sum) and then by name or children. It is a total order on these keys, so a sum or a product built in any order sorts to the same children and the same node.

Why the tables are maps. Viv has no arrays of records, and a module's array cannot grow inside a def (append rebinds a name, and a def cannot rebind a module's name), so each field of a node is a map from id to value and the children of all nodes sit in one map, CAS_CH, by offset. An integer type, records in arrays and maps with structural keys would replace this.

Used with import cas.store.

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

Shift+Enter in a cell runs it and moves to the next. Running a cell runs every cell above it too, as one program.

[1]lines 49–839