formform.calc

API for the calc module of formform.

Concepts

Values

constant
→ representation of a value in FORM logic

  • value → element of a value system
  • value → state indicated by an expression
  • value system → system of differences

A sort-code specifies a numeric ordering for constants, which is useful for conversion to integers and formDNA interpretation order. It is always assumed to be in nuim-code by default:

0 = :n → unmarked
1 = :u → undetermined
2 = :i → imaginary
3 = :m → marked

Make sure you convert to/from nuim-code when using different codes!

Value Structures

formDNA
→ representation of a value structure in FORM logic

  • becomes a quaternary number when reversed and converted to digits
  • value structure → specific structure in value system

formDNA perspective
→ permutation of formDNA
→ representation of a different perspective on the value structure

vpoint
→ relate values as a point
→ vector of constant-coordinates in a vspace

vspace
→ relate vpoints as a space
→ vector of all n-dimensional vpoints

vdict
→ map vpoints to values in a dictionary
→ (sorted) key-value map from vspace to formDNA

  • for value table generation
  • like a flat vmap

vmap
→ map recursively decomposed vspace to value structure
→ mapping from vspace topology to formDNA

--

Alias to rel.

char->const

(char->const c)(char->const sort-code c)

Coerces a char to a corresponding constant.

chars->dna

(chars->dna dna-seq)(chars->dna from-sort-code dna-seq)

Converts a seqable? of chars to formDNA.

Note that nuim-code is the default ordering and is always assumed for formDNA. If a different sort-code is specified, the input sequence (expected in this code) will be reordered to match nuim-code.

compare-consts

Comparator for constants using the default nuim-code.

const->digit

(const->digit c)(const->digit sort-code c)

Converts a constant to a digit corresponding to an optional sort-code or the default nuim-code.

const?

Checks if the argument is a valid constant.

consts

Set of all 4 constants.

digit->const

(digit->const n)(digit->const sort-code n)

Converts a digit to its corresponding constant representation.

digits->dna

(digits->dna dna-seq)(digits->dna from-sort-code dna-seq)

Converts a seqable? of digits (as string/char or integer) to formDNA.

Note that nuim-code is the default ordering and is always assumed for formDNA. If a different sort-code is specified, the input sequence (expected in this code) will be mapped and reordered to match nuim-code. If you just want the mapping without reordering, use something like (mapv digit->const dna).

dna->digits

(dna->digits dna)(dna->digits to-sort-code dna)

Converts formDNA to a sequence of digits corresponding to a sort-code.

Note that nuim-code is the default ordering and is always assumed for formDNA. If a different sort-code is specified, the input dna (expected in nuim-code) will be mapped and reordered to match this code.

dna->vdict

(dna->vdict dna)(dna->vdict dna opts)

Generates a vdict from a given formDNA (dna).

  • optional sorted? defaults to false since sorting large vspace dimensions can be expensive

dna->vmap

(dna->vmap dna)

FIXME: write docs

dna-dimension

(dna-dimension xs)

Calculates the dimension of a formDNA/dna-seq (corresponds to the number of variables in a FORM). The length of a dna-seq is 4^d for its dimension d.

  • the input sequence can have any type of elements

dna-dimension?

FIXME: write docs

dna-get

(dna-get dna vpoint)

Extracts a single value from a dna according to a given vpoint index, which is a sequence of constants corresponding to each term→value association.

dna-perspectives

(dna-perspectives dna)(dna-perspectives dna opts)

Given a formDNA (dna), generates all of its permutations and returns a map from permuted term order to the corresponding formDNA.

dna?

FIXME: write docs

equal-dna?

(equal-dna? & dnas)

Equality check for formDNA. Two formDNAs are considered equal, if they contain the same constants in the same order. Stricter than equiv-dna?, where permutations are considered equal.

Note: partial formDNA (which includes holes (:_)) cannot be compared and thus are not valid input. If you know/assume equality for holes or just want to ignore them in comparison, use equal-partial-dna?.

equal-partial-dna?

(equal-partial-dna? & dnas)

Equality check for partial formDNA (derived from equal-dna?), under the assumption that all holes (:_) originate from the same expression and thus their supposed value would be equal (which is the same as just ignoring them).

equiv-dna?

(equiv-dna? & dnas)

Equivalence check for formDNA. Two formDNAs are considered equivalent, if they belong to the same equivalence-class of dna-perspectives (i.e. if they are permutations of each other).

Note: partial formDNA (which includes holes (:_)) cannot be compared and thus are not valid input. If you know/assume equality for holes or just want to ignore them in comparison, use equiv-partial-dna?.

equiv-partial-dna?

(equiv-partial-dna? & dnas)

Equivalence check for partial formDNA (derived from equiv-dna?), under the assumption that all holes (:_) originate from the same expression and thus their supposed value would be equal (which is the same as just ignoring them).

expand-dna

(expand-dna dna-seq ext-dim)(expand-dna dna-seq dim ext-dim)

Expands a dna-seq to a given target dimension by repeating elements.

Note: dna-seq can have any type of elements (not only constants)

expand-dna-seq

Obsolete → please use expand-dna instead.

filter-dna

(filter-dna dna vpoint)

Filters a dna by selecting specific parts corresponding to a given vpoint, which acts as a coordinate vector in its value space.

  • use holes :_ in vpoint to indicate a variable selection

inv

(inv & consts-or-dnas)

Inverts the value of a given constant or formDNA. With multiple arguments, will relate all arguments (via rel) and then invert the result.

Note: value holes (:_) will invert to value holes.

make-compare-consts

(make-compare-consts sort-code)

Given a sort-code (try nuim-code or nmui-code), returns a comparator function to sort single constants, formDNA or arbitrary sequences of constants (can be mixed).

  • can also compare map-entries by keys of comparable types

make-dna

(make-dna & xs)

Creates a formDNA from arguments, which may be valid chars, keywords, integers or sequences thereof.

  • valid chars are: \n \u \i \m (upper- or lowercase) and \0 \1 \2 \3
  • valid integers are: 0 1 2 3
  • valid keywords are: :n :u :i :m
  • total argument count (including count of sequence args) must match a valid formDNA length, which is 4^d, where d is a natural number

nmui-code

FIXME: write docs

nuim-code

FIXME: write docs

partial-dna?

FIXME: write docs

permute-dna

(permute-dna dna perm-order)(permute-dna dna perm-order opts)

Given a formDNA (dna), generates its permutation (called a “perspective”) that matches the given perm-order.

perm-order must be a sequence of indices that correspond to each term/subdna of the dna. Think of each place in the sequence as a depth (shallowest → deepest) and each index as an identifier of the term in that depth.

For example, [0 1 2] is the default order of terms in a 3-dimensional formDNA. [1 2 0] would permute the terms, such that in the resulting formDNA term 0 is in depth 2, term 1 in depth 0 and term 2 in depth 1.

rand-const

(rand-const)(rand-const seed)

Generates a random constant. A seed (an integer) can be provided as a second argument for reproducability.

rand-const-weighted

(rand-const-weighted const-weights)(rand-const-weighted const-weights seed)

Same as rand-const, but takes a weights argument to specify the relative probability of each of the four constants to be randomly chosen.

Weights can be provided either as:

  • a sequence of 4 non-negative numbers (e.g. [1 0 2 5]) in n-u-i-m order
  • a map (e.g. {:i 1 :u 2}), where missing weights are 0
  • a single number in the interval 0.0, 1.0 that represents the ratio of :u/:i/m against :n (whose weight is 1 - x)

rand-dna

(rand-dna dim)(rand-dna dim seed)

Generates a random formDNA of dimension dim. A seed (an integer) can be provided as a second argument for reproducability.

rand-dna-weighted

(rand-dna-weighted dim const-weights)(rand-dna-weighted dim const-weights seed)

Same as rand-dna, but takes a weights argument to specify the relative probability of each of the four constants to be randomly chosen.

Weights can be provided either as:

  • a sequence of 4 non-negative numbers (e.g. [1 0 2 5]) in n-u-i-m order
  • a map (e.g. {:i 1 :u 2}), where missing weights are 0
  • a single number in the interval 0.0, 1.0 that represents the ratio of :u/:i/m against :n (whose weight is 1 - x)

rand-vpoint

(rand-vpoint dim)(rand-vpoint dim seed)

Generates a random vpoint with given dimension dim (= length of the vpoint). A seed (an integer) can be provided as a second argument for reproducability.

rand-vpoint-weighted

(rand-vpoint-weighted dim const-weights)(rand-vpoint-weighted dim const-weights seed)

Same as rand-vpoint, but takes a weights argument to specify the relative probability of each of the four constants to be randomly chosen.

Weights can be provided either as:

  • a sequence of 4 non-negative numbers (e.g. [1 0 2 5]) in n-u-i-m order
  • a map (e.g. {:i 1 :u 2}), where missing weights are 0
  • a single number in the interval 0.0, 1.0 that represents the ratio of :u/:i/m against :n (whose weight is 1 - x)

reduce-dna

(reduce-dna dna-seq)(reduce-dna dna-seq terms)

Reduces a dna-seq by eliminating redundant/contingent terms.

  • returns a tuple [terms dna-seq], where terms is a sequence that represents the remaining terms after reduction
  • takes an optional terms sequence of any kind of items that will be used instead of the default arithmetic sequence [0 1 2 …] to represent each term (length has to match the formDNA dimension)

Note: dna-seq can have any type of elements (not only constants)

reduce-dna-seq

Obsolete → please use reduce-dna instead.

rel

(rel & consts-or-dnas)

Relates the values of all given arguments (which must either be all constants or all formDNAs) to each other.

Note: relations with value holes (:_) can only be dominated by the mark (:m) and will otherwise result in value holes.

reorder-dna

(reorder-dna dna-seq sort-code-from sort-code-to)

Reorders given formDNA/dna-seq from sort-code-from to sort-code-to.

Note:

  • dna-seq can have any type of elements (not only constants)
  • does NOT change the encoding of the elements, just their ordering

reorder-dna-seq

Obsolete → please use reorder-dna instead.

sort-code?

FIXME: write docs

val-hole

“Value hole” – placeholder for an unknown/missing result. Denotes a constant, but like a black box, we cannot know which one.

vdict

(vdict vpoint->result)(vdict vpoint->result opts)

Generates a vdict given a map vpoint->result (result is a constant).

  • if the corresponding vspace is not a subset of the set of keys from vp->r, the remaining results will be filled with :n or a given default constant
  • optional sorted? defaults to false since sorting large vspace dimensions can be expensive

vdict->vmap

(vdict->vmap vdict)

Generates a vmap from a given vdict.

vdict?

FIXME: write docs

vmap-dimension

(vmap-dimension vmap)

Returns the dimension of a vmap (equivalent to dna-dimension of the corresponding formDNA).

vmap-perspectives

(vmap-perspectives dna-psps)

Given a group of all perspectives from a formDNA, returns these perspectives as vmaps.

vmap?

FIXME: write docs

vpoint?

FIXME: write docs

vspace

(vspace dim)(vspace sort-code dim)

Generates a vspace of dimension dim, optionally with custom sort-code.

  • returns a lazy-seq which may be too memory-expensive to fully realize for dimensions greater than 11 (> 200 Mio. elements in total!)

vspace?

FIXME: write docs

|

Alias to inv.