![]() |
The experimental evaluator in Style/ |
![]() |
The directory
src/src/Style/
contains a second implementation of the evaluator, written with the aim of computing the style rewriting of a whole document (the tree obtained by expanding all macros) incrementally, through systematic memoization. It is a prototype:
It is only compiled when TeXmacs is configured with the CMake option ENABLE_EXPERIMENTAL (or the corresponding CONFIG_EXPERIMENTAL setting of
makefile.in
), which defines the preprocessor symbol EXPERIMENTAL; the default is off.
It is not used by the typesetter. When enabled, the editor keeps a clean copy cct of the document (created in the constructor of edit_main_rep with copy_ip, and kept up to date by copy_announce in edit_done), a persistent environment ste (rebuilt from the typesetting environment by edit_typeset_rep::environment_update, which calls primitive (ste, h)) and a memorizer mem. After each change of the tree, edit_interface_rep recomputes mem= evaluate (ste, cct) and prints the rewritten tree on the console.
The main routines evaluate and rewrite print a trace of every call unconditionally, and drd-props is not implemented (evaluate_drd_props returns the empty string).
It is nevertheless useful to know about it, both because its code is easy to confuse with the real evaluator (the function names differ only in evaluate_* versus exec_*) and because it illustrates a different design.
Style/Environment/environment.hpp
defines an abstract class environment_rep with methods contains, read, write, remove and print, keyed by integers: variable names are converted to integers with make_tree_label. The implementations are
A small array of key/value pairs, used for the bindings of one with, assign or macro application.
An open hash table stored in an array of hash_nodes whose size is a power of two.
A linked list of basic environments, looked up from the head; it counts lookup misses and flattens itself with compress when there are too many.
(in
std_environment.cpp
) The environment actually used by the evaluator. It has a flag pure, a list of local variables env, a link next to the enclosing environment, an accelerated lookup list accel and a list args of macro argument frames.
Environments are never modified in place by the evaluator. Instead, the functions assign, begin_with and end_with (and macro_down, macro_redown, macro_up in the classical variant, see below) replace the global std_env by a new environment which shares most of its structure with the old one. A with pushes a pure environment; an assign inside it produces an impure one; end_with_environment computes which assignments must survive the end of the with and re-applies them to the enclosing environment. Each of these operations is itself memoized, so that applying the same change to the same environment returns the very same environment object.
A memorizer (
Style/Memorizer/memorizer.hpp
) represents one computation: its inputs, its outputs and the memorizers of its sub-computations. Memorizers are hash-consed in a global table: the constructor memorizer (memorizer_rep*) looks for an existing memorizer of the same type which is equal to the new one, and uses it instead. For evaluation, the key is the pair formed by the identities (pointers, see weak_hash and weak_equal) of the input environment and of the input tree:
tree
evaluate (tree t) {
if (is_atomic (t)) return t;
…
memorizer mem= evaluate_memorizer (std_env, t);
if (is_memorized (mem)) {
…
std_env= mem->get_environment ();
return mem->get_tree ();
}
memorize_start ();
tree r= evaluate_impl (t);
decorate_ip (t, r);
mem->set_tree (r);
mem->set_environment (std_env);
memorize_end ();
…
return mem->get_tree ();
} |
Because the environments are persistent and shared, and because the clean copy of the document shares all unmodified subtrees between versions, evaluating a slightly modified document with evaluate (environment env, tree t) finds most sub-computations in the table and only recomputes the parts on the path of the modification. Whether a memorizer counts as already computed is decided by is_memorized, a test on its reference count. The functions memorize_initialize, memorize_start, memorize_end and memorize_finalize maintain the stack from which the tree of sub-computations is built.
The results receive inverse paths through decorate_ip (t, r), which attaches decorate_right (obtain_ip (t)) to all detached parts of the result, and through transfer_ip for componentwise evaluation.
environment.hpp
defines ALTERNATIVE_MACRO_EXPANSION (the alternative, CLASSICAL_MACRO_EXPANSION, is commented out). In this mode evaluate_compound does not push argument frames. It builds an assoc_environment binding the parameters to the argument trees and substitutes them into the body with expand (tree t, assoc_environment env) (
Style/Evaluate/evaluate_macro.cpp
):
(arg x i1 … ik) is replaced by the corresponding subtree of the argument (the indices are evaluated);
(quote-arg x …) becomes a quote of it;
map-args is expanded directly into the children of the argument;
nested macro and xmacro definitions shadow the parameters they rebind;
unchanged subtrees are shared with the body (weak_equal tests), which is essential for memoization.
The substituted body is then decorated with the inverse path of the application and evaluated. The classical variant, with macro_down and macro_up managing a stack of argument frames inside std_environment, mirrors the implementation in Typeset/Env/.
The two evaluators implement the same primitives with the same names (compare evaluate_with and edit_env_rep::exec_with, rewrite and edit_env_rep::rewrite, the re-entrancy variable reenter_rewrite_env and current_rewrite_env), but they do not share code, and the Style evaluator has not been kept in sync with all later additions to
env_exec.cpp
(themes, animations, several graphical primitives). The typesetter only uses edit_env_rep::exec. If you fix a bug in the semantics of a primitive, the place to fix it is Typeset/Env/ (together with the corresponding typesetting code in
Typeset/Concat/
and
Typeset/Bridge/
).