formform.expr
API for the expr module of formform.
Concepts
Basic Expressions
expression
→ interpret the representation as an indicator of a value
- representation → form {syntax} and intentionality {semantics}
- form → relate structure and code
- value → calc/value
- value → calc/constant {determined} or formDNA {contingent}
variable
→ the interpretation of the expression symbol is undetermined
form expression {FORM}
→ expression of form
→ invert the value of the relation
- relation → relate the values of the content of the expression
Symbolic Expressions
symbolic expression
→ expression symbol or operator
expression symbol
→ interpret the symbol as a specific expression
operator
→ interpret the structure by its symbol as an expression pattern
Types
arrangement {:-}
→ operator to construct relations
unclear FORM {:uncl}
→ operator to construct unclear FORMs
seq-reentry FORM {:seq-re}
→ operator to construct self-equivalent re-entry FORMs
memory FORM {:mem}
→ operator to construct memory FORMs
- rem pair → observe and remember equality between the two expressions
formDNA {:fdna}
→ operator to construct calc/formDNA expressions
defoperator
macro
(defoperator k args interpretation & params)Defines a new operator by its symbol (a keyword), a vector of arguments and an interpretation function. Takes additional key-value pairs for options.
Registers various methods for the operator:
interpret-op(use interpret) to access the interpretation functionmake-op(use make) → constructor (either uses the providedargsor a custom constructor function via the option:constructor)simplify-op(use simplify) → simplifier (either defaults to the given interpretation function or uses a custom reducer via the option:reducer)- valid-op? → validator (provided by the
:predicateoption) - op-data → returns a key-value map of the operator arguments
- op-get → returns a specific value by a given argument-key
defsymbol
macro
(defsymbol k interpretation & params)Defines a new expression symbol by its symbol (a keyword) and an interpretation function. Takes additional key-value pairs for options.
Registers various methods for the expression symbol:
equal?
(equal? & exprs)Equality check for expressions. Two expressions are considered equal, if their formDNAs are equal. Compares formDNAs from evaluation results of each expression by calling calc/equal-dna?.
WARNING: Procedure and assumptions are being reassessed. Use with caution!
- ordering of variable names in formDNA matters, see find-vars
- stricter than equiv, which compares by calc/equiv-dna?
equiv?
(equiv? & exprs)Equivalence check for expressions. Two expressions are considered equivalent, if their formDNAs are equivalent. Compares formDNAs from evaluation results of each expression by calling calc/equiv-dna?.
WARNING: Procedure and assumptions are being reassessed. Use with caution!
- ordering of variable names in formDNA is irrelevant
- looser than equal, which compares by calc/equal-dna?
- can be slow on expressions with 6+ variables
eval->expr
(eval->expr expr)(eval->expr expr env)Evaluates a FORM expr with an optional env and returns an expression: either a constant or the simplified input expression, if it could not be determined to a value.
envmust be a map from variables to expressions
eval->expr-all
(eval->expr-all expr)(eval->expr-all expr env)(eval->expr-all expr env opts)Like eval->expr, but evaluates all possible interpretations of any occurring variable in the expr. Returns an expression: either a constant, a formDNA expression (which collects all interpretation results) or the simplified input expression, if it could not be determined to a value in any of its interpretations.
* env must be a map from variables to expressions
An opts map can be provided with the following keys:
:varorder→ sets the variable interpretation order for the resulting formDNA:pre-simplify?→ (default:true) simplifies the expression before interpretation, which might reduce terms and therefore evaluation time:reduce-dna?→ (default:true) if result is a formDNA expression which can be reduced to fewer terms, reduces it to further simplify the output:allow-hole-results?→ (default:false) sets a “value hole” (:_) in place of an uninterpretable result:allow-hole-exprs?→ (default:false) allows “value holes” (:_) in the input expression (each instance will be treated like a different variable)
eval->val
(eval->val expr)(eval->val expr env)Evaluates a FORM expr with an optional env and returns a value: either a constant or a “value hole” :_ (in case a value cannot be determined, which usually happens when variables remain uninterpreted).
envmust be a map from variables to expressions
eval->val-all
(eval->val-all expr)(eval->val-all expr env)(eval->val-all expr env opts)Like eval->val, but evaluates all possible interpretations of any occurring variable in the expr. Returns a complex value called formDNA, which collects all interpretation results.
envmust be a map from variables to expressions
Note: interpretation results might (in very rare cases) include “value holes” :_, if a value cannot be determined. This applies to unregistered symbols (like :pie) or uninterpretable results from custom, user-defined operators. To avoid holes, wrap them in unclear FORMs, e.g. (make :uncl :pie).
An opts map can be provided with the following keys:
:varorder→ sets the variable interpretation order for the resulting formDNA:pre-simplify?→ (default:false) simplifies the expression before interpretation, which might reduce terms and therefore evaluation time:reduce-dna?→ (default:false) if result is a formDNA expression which can be reduced to fewer terms, reduces it to further simplify the output:allow-hole-exprs?→ (default:false) allows “value holes” (:_) in the input expression (each instance will be treated like a different variable)
eval-all
(eval-all expr)(eval-all expr env)(eval-all expr env opts)Like evaluate, but evaluates all possible interpretations of any occurring variable in the expr, with an optional env.
* env must be a map from variables to expressions
Returns a map with the following entries:
:results→ vector of[<interpretation> <result>]tuples (withresultlike inevaluate), think of it as a kind of value table:varorder→ the reading order of variable interpretations
An opts map can be provided with the following keys:
:varorder→ sets the variable interpretation order for the results:rich-results?→ each results value will be a map as if returned by evaluate:pre-simplify?→ (default:false) simplifies the expression before interpretation, which might reduce terms and therefore evaluation time:allow-hole-results?→ (default:false) sets a “value hole” (:_) in place ofnilfor an uninterpretable result:allow-hole-exprs?→ (default:false) allows “value holes” (:_) in the input expression (each instance will be treated like a different variable)
eval-tsds->val-all
(eval-tsds->val-all & selection)Convenience function that takes a 6-digit binary selection (as a vector) for a triple-selective decision system (TsDS) and returns the formDNA for the (as by eval->val-all) evaluated expression.
Note: contrary to eval->val-all, the resulting formDNA does not get reduced.
evaluate
(evaluate expr)(evaluate expr env)Evaluates a FORM expr with an optional env and returns a map with a :result entry that is either a constant or nil, if it could not be determined to a value. It also contains a :simplified entry with the simplified expression.
* env must be a map from variables to expressions
expr->const
(expr->const expr)Returns the constant value/expression that corresponds to its simplest (as per simplify) input expr (including itself):
nil → :n
[] → :m
[:seq-re :<r nil nil] → :u
[[:seq-re :<r nil nil]] → :i
[:u] → :i
find-subexprs
(find-subexprs expr subexprs)Finds all subexpressions in expr that match any element of the given set subexprs.
find-vars
(find-vars expr opts)Finds all variables in an expresson or returns the expression itself if it is a variable.
Options:
{:ordered true}to return variables in: type order → alphanumeric order{:vars #{…}}can be given a set of specific variables to find
form-marked
(form-marked & exprs)Returns a FORM with each exprs argument marked, e.g. ((a) (b) …).
- group expressions with arrangements:
(make x y …)
form-nested-l
(form-nested-l & exprs)Nests exprs leftwards in a FORM, e.g. ((((…) y) z).
- use
nilfor empty expressions - use an arrangement
(make x y …)to add multiple exprs. to the same level
form-nested-r
(form-nested-r & exprs)Nests exprs rightwards in a FORM, e.g. (a (b (…))).
- use
nilfor empty expressions - use an arrangement
(make x y …)to add multiple exprs. to the same level
formDNA-perspectives
(formDNA-perspectives fdna)Takes a formDNA expression and returns its formDNA perspective group.
interpret
(interpret expr)(interpret expr env)Interprets an expression of any kind. Returns the original expression if it cannot be interpreted.
Can be given an env map to interpret variables (as keys). This map can have an optional --defocus entry whose value is a set of items that should not be interpreted and a complementary --focus entry to only interpret the items specified in its set and nothing else.
- the keywords
:ops/:syms/:varsdesignate all operations / expression symbols / variables - an operator symbol can provided to designate a specific operator
- any other expression (like a variable) can be designated as itself
--focusand--defocuscan cancel each other out if they contain the same item so you usually pick one or the other
interpret*
(interpret* expr)(interpret* expr env)Like interpret, but repeats substitution on interpreted expressions until they cannot be interpreted any further.
interpret-op
multimethod
Interprets a symbolic expression with a registered operator.
Note: default to use interpret instead
interpret-walk
(interpret-walk expr)(interpret-walk expr env)Recursively calls interpret on given expression and all its subexpressions with a depth-first walk.
interpret-walk*
(interpret-walk* expr)(interpret-walk* expr env)Like interpret-walk, but repeats substitution on interpreted (sub-)expressions until they cannot be interpreted any further.
isolator
(isolator c)Given a constant, returns the corresponding FORM from the isolator class.
make
(make & args)Constructor for expressions of any kind. Validates its input.
If the first argument (or the first after the options map) is a keyword of a registered operator, will call the constructor for that operator
Can be given an options map as first argument:
mark?(default: false) marks the whole expression, creating a FORMsplice?(default: true) dissolves all top-level arrangements
make-marked
(make-marked & exprs)Returns an arrangement with each exprs argument marked, e.g. (a) (b) ….
- group expressions with arrangements:
(make x y …)
make-nested-l
(make-nested-l & exprs)Nests exprs leftwards in an arrangement, e.g. ((…) y) z.
- use
nilfor empty expressions - use an arrangement
(make x y …)to add multiple exprs. to the same level
make-nested-r
(make-nested-r & exprs)Nests exprs rightwards in an arrangement, e.g. a (b (…)).
- use
nilfor empty expressions - use an arrangement
(make x y …)to add multiple exprs. to the same level
make-op
multimethod
Constructs a symbolic expression given a registered operator and parameters.
Note: default to use make instead of make-op
memory
(memory rem-pairs & exprs)Constructs a memory FORM from a given list of rem-pairs (key-value pairs, where both the key and the value is an expression) and one or more expressions which are in their scope.
memory-extend
(memory-extend mem & ext-pairs)Takes a memory FORM and extends its rem-pairs by one or more given extension pairs.
memory-replace
(memory-replace mem & repl-pairs)Takes a memory FORM and replaces its rem-pairs by one or more given replacement pairs.
nest-exprs
(nest-exprs opts & exprs)Obsolete → use make-nested-l/make-nested-r or form-nested-l/form-nested-r instead.
op-data
multimethod
Gets all parameters from a symbolic expression with a registered operator as a map.
op-get
multimethod
Gets a specified part from a symbolic expression with a registered operator.
permute-vars
(permute-vars varorder)Generates all permutations of a variable order (a sequence of variables).
selector
(selector vars->consts)(selector vars->consts simplify?)Given a map variable->constant, returns a FORM from the selector class.
seq-re
(seq-re specs & nested-exprs)Constructs a self-equivalent re-entry FORM given the arguments:
specs: either aseq-reentry-signatureor an options mapnested-exprs: zero or more expressions intended as a nested sequence
seq-reentry-opts->sign
(seq-reentry-opts->sign opt-map)Inverse map of seq-reentry-sign->opts with default args.
seq-reentry-sign->opts
(seq-reentry-sign->opts sign)Maps signatures for self-equivalent re-entry FORMs to their corresponding option-maps.
simplify
(simplify x)(simplify x env)(simplify x env opts)Simplifies a FORM recursively until it cannot be further simplified. All deductions are justified by the axioms of FORM logic.
- if no simplification applies, tries to retrieve the value from given
env - if retrieval was unsuccessful, returns
xas is
An opts map can be provided with the following keys:
:allow-hole-exprs?→ (default:false) allows “value holes” (:_) in the input expression (each instance will be treated like a different variable)
simplify-expr-chain
(simplify-expr-chain chain env)(simplify-expr-chain opts chain env)Obsolete → use simplify-nested-l or simplify-nested-r instead!
simplify-in
(simplify-in ctx)(simplify-in ctx env)(simplify-in ctx env opts)Simplifies a context/sequence of FORMs recursively until it cannot be further simplified. All deductions are justified by the axioms of FORM logic.
An opts map can be provided with the following keys:
:allow-hole-exprs?→ (default:false) allows “value holes” (:_) in the input expression (each instance will be treated like a different variable)
simplify-nested-l
(simplify-nested-l nesting-chain)(simplify-nested-l nesting-chain env)(simplify-nested-l nesting-chain env opts)Reduces a leftward nesting-chain, a sequence of expressions ( … x y z ) whose interpretation is ( [[[…] x] y] z ), to a simplified nesting chain, possibly spliced or shortened via inference.
- takes an optional
envthat gets applied to the nested expansion
An opts map can be provided with the following keys:
:allow-hole-exprs?→ (default:false) allows “value holes” (:_) in the input expression (each instance will be treated like a different variable)
simplify-nested-r
(simplify-nested-r nesting-chain)(simplify-nested-r nesting-chain env)(simplify-nested-r nesting-chain env opts)Reduces a rightward nesting-chain, a sequence of expressions ( a b c … ) whose interpretation is ( a [b [c […]]] ), to a simplified nesting chain, possibly spliced or shortened via inference.
- takes an optional
envthat gets applied to the nested expansion
An opts map can be provided with the following keys:
:allow-hole-exprs?→ (default:false) allows “value holes” (:_) in the input expression (each instance will be treated like a different variable)
valid-op?
multimethod
Validates the shape of a symbolic expression with a registered operator.