formform.expr
API for the expr
module of formform
.
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
to access the interpretation functionmake-op
-> constructor (either uses the providedargs
or a custom constructor function via the option:constructor
)simplify-op
-> simplifier (either defaults to the given interpretation function or uses a custom reducer via the option:reducer
)valid-op?
-> validator (provided by the:predicate
option)op-data
-> returns a key-value map of the operator argumentsop-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:
interpret-sym
-> to access the interpretation functionsimplify-sym
-> simplifier (either defaults to the given interpretation function or uses a custom reducer via the option:reducer
)
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
.
- ordering of variable names in formDNA matters, see
find-vars
- stricter than
equiv
, which compares bycalc/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
.
- ordering of variable names in formDNA is irrelevant
- looser than
equal
, which compares bycalc/equal-dna
- can be slow on expressions with 6+ variables
eval->expr
(eval->expr expr)
(eval->expr expr env)
Evaluates a FORM expression with an optional env
and returns a constant expression with attached metadata including the maximally reduced expression in :expr
and the environment in :env
.
env
must be a map with a content/variable inexpr
as a key
eval->expr-all
(eval->expr-all expr)
(eval->expr-all expr env)
(eval->expr-all opts expr env)
Evaluates a FORM expression for all possible interpretations of any occurring variable in the expression. Returns a formDNA expression by default.
- if
to-fdna?
is false, returns a seq of results as returned by=>
in the order of the correspondingvspace
ordering
eval-all
(eval-all expr)
(eval-all expr env)
(eval-all opts expr env)
Evaluates a FORM expresson for all possible interpretations of any occurring variable in the expresson. Returns a map with a :results
key whose value is a sequence of [<interpretation> <result>]
tuples and with a :varorder
key whose value is the reading order for variable results in the interpretations. This output is particularly suited for value tables.
evaluate
(evaluate expr)
(evaluate expr env)
Evaluates a FORM expresson with an optional env
and returns either a constant or the simplified expression if it could not be determined to a value.
expr->const
(expr->const expr)
Given an expression, returns the corresponding constant value.
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
interpret
(interpret expr)
(interpret env expr)
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
/:vars
designate 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
--focus
and--defocus
can cancel each other out if they contain the same item so you usually pick one or the other
interpret*
(interpret* expr)
(interpret* env expr)
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-sym
multimethod
Interprets a registered symbol.
Note: default to use interpret
instead
interpret-walk
(interpret-walk expr)
(interpret-walk env expr)
Recursively calls interpret
on given expression and all its subexpressions with a depth-first walk.
interpret-walk*
(interpret-walk* expr)
(interpret-walk* env expr)
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-op
multimethod
Constructs a symbolic expression given a registered operator and parameters.
Note: default to use make
instead of make-op
mark-exprs
(mark-exprs opts & exprs)
Chains expressions like ((a)(b)…)
or (a)(b)…
if {:unmarked? true}`
- group expressions with arrangements:
[:- x y …]
memory
(memory rem-pairs & exprs)
Constructs a memory FORM from a given list of rem-pair
s (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-pair
s by one or more given extension pairs.
memory-replace
(memory-replace mem & repl-pairs)
Takes a memory FORM and replaces its rem-pair
s by one or more given replacement pairs.
nest-exprs
(nest-exprs opts & exprs)
Nests expressions leftwards (((…)a)b)
or rightwards (a(b(…)))
if {:ltr? true}
- use
nil
for empty expressions - use an arrangement
(make x y …)
to add multiple exprs. to the same level
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.
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-signature
or 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)
Simplifies a FORM recursively until it cannot be further simplified. All deductions are justified by the axioms of FORM logic.
- if
x
is a complex FORM, callssimplify-context
onx
- if no simplification applies, tries to retrieve the value from given
env
- if retrieval was unsuccessful, returns
x
as is
simplify-expr-chain
(simplify-expr-chain chain env)
(simplify-expr-chain opts chain env)
Reduces a sequence of expressions, intended to be linked in a chain
, to a sequence of simplified expressions, possibly spliced or shortened via inference.
- assumes rightward-nesting, e.g.
(…(…(…)))
- for leftward-nesting, e.g.
(((…)…)…)
, pass{:rtl? true}
simplify-in
(simplify-in ctx)
(simplify-in ctx env)
Simplifies a context/sequence of FORMs recursively until it cannot be further simplified. All deductions are justified by the axioms of FORM logic.
- for complex expressions, calls
expr.core/simplify-content
on every unique element
simplify-op
multimethod
Simplifies a symbolic expression with a registered operator given an optional environment.
Note: default to use simplify
instead
simplify-sym
multimethod
Simplifies a registered symbol given an optional environment.
Note: default to use simplify
instead
valid-op?
multimethod
Validates the shape of a symbolic expression with a registered operator.