![]() |
The experimental style rewriter | ![]() |
The directory
src/src/Style/
contains a second, independent implementation of the evaluation of TeXmacs documents. Its goal is to compute the style rewriting of a whole document, that is the tree obtained by expanding all macros and evaluating all primitives, incrementally: after a small change of the document, only the computations which depend on the change should be redone. To this end it combines three ideas:
persistent environments, which are never modified in place, so that two evaluations in the same environment can be recognized by comparing pointers;
a clean copy of the document, which is updated functionally after each modification and therefore shares all unmodified subtrees with its previous version;
systematic memoization of every evaluation step, indexed by the identities of the input environment and of the input tree.
The code is a prototype. It is not compiled by default, it is not used
by the typesetter, and in its present state it does not even compile
when it is enabled (see pitfalls). The short overview in
the experimental evaluator in
Style/
describes the design; this chapter documents the code in more detail,
so that it can be repaired, evaluated or removed with full knowledge
of what it does. The semantics of the primitives themselves are those
of the real evaluator described in macro expansion and
evaluation.
File names below are relative to
src/src/
.
Off by default. The CMake option ENABLE_EXPERIMENTAL (
CMakeLists.txt:466
) adds all of Style/*.cpp to the sources and defines EXPERIMENTAL; without it, TeXmacs_Style_SRCS is empty. With the autotools build, configure --enable-experimental sets CONFIG_EXPERIMENTAL to "Memorizer Environment Evaluate" and defines EXPERIMENTAL;
makefile.in
then compiles those three subdirectories (style_src). In a default build, no file of
Style/
is compiled at all.
When enabled, the editor maintains a clean copy of its document and re-evaluates it after every change, but the result is only printed on the console; nothing in the typesetter, the editor or Scheme reads it. See integration with the editor.
The evaluator handles 126 of the 175 primitives known to edit_env_rep::exec. Several of the handled ones are placeholders (fixed lengths, drd-props, bindings). See the evaluator.
Three files of
Style/Evaluate/
no longer compile, because functions they use have moved to headers they do not include. Every call of the main routines also prints a trace on the console.
editor (EXPERIMENTAL)
et[rp] --- edit_done / copy_announce ---> cct (clean copy)
typesetting env --- environment_update / primitive ---> ste
apply_changes: mem= evaluate (ste, cct)
evaluate (env, t) Style/Evaluate/evaluate_main.cpp
|-- memorizer lookup by (env pointer, tree pointer)
|-- evaluate_impl: dispatch on the label evaluate_*.cpp
|-- assign / begin_with / end_with std_environment.cpp
| (new persistent environments, memoized too)
‘-- result tree + output environment stored in the memorizer
memorizers: hash-consed in a global table Style/Memorizer/memorizer.cpp |
Style/Environment/environment.hpp
The abstract environment_rep (integer keys obtained with make_tree_label), the handle environment, the identity functions weak_hash and weak_equal for trees and environments, and the choice ALTERNATIVE_MACRO_EXPANSION versus CLASSICAL_MACRO_EXPANSION.
Small association arrays, open hash tables, and linked lists of hash tables with automatic compression.
The environment used by the evaluator, primitive, and the memoized operations assign, begin_with, end_with (and macro_down, macro_redown, macro_up for the classical macro expansion).
The memorizer classes, the global hash-consing table and the stack of sub-computations.
Maintenance of the clean copy: copy_ip and copy_announce.
The memoized evaluate, the dispatcher evaluate_impl and the declarations of all evaluate_* routines.
evaluate_macro.cpp
Assignments, with, values, macro application by substitution (expand), drd-props.
The families of primitives:
evaluate_control.cpp
,
evaluate_boolean.cpp
,
evaluate_numeric.cpp
,
evaluate_textual.cpp
,
evaluate_length.cpp
and
evaluate_quote.cpp
.
evaluate_rewrite.cpp,
evaluate_inactive.cpp
The memoized rewriting of extern, include, with-package and the rendering of inactive markup.
evaluate_misc.cpp
Formatting tags, tables, hard-id, scripts, bindings, patterns and points.
The hooks in the editor are in
Edit/editor.hpp
,
Edit/Editor/edit_main.cpp
,
Edit/Editor/edit_typeset.cpp
,
Edit/Modify/edit_modify.cpp
and
Edit/Interface/edit_interface.cpp
, all under #ifdef EXPERIMENTAL.
The experimental build does not compile. Checked with clang++ -fsyntax-only -DEXPERIMENTAL=1 on every file of
Style/
:
Style/Evaluate/evaluate_numeric.cpp:214-215 uses is_color_name and named_color, now declared in
Graphics/Colors/colors.hpp
, which is not included;
Style/Evaluate/evaluate_rewrite.cpp:142
calls exec_string, which is a member of edit_env_rep (
Typeset/env.hpp:445
), not a free function (evaluate_string is meant), and line 143 uses with_package_definitions from
Texmacs/Data/new_buffer.hpp
, which is not included;
Style/Evaluate/evaluate_textual.cpp:128
uses get_date from
System/Language/locale.hpp
, which is not included.
The other files of
Style/
pass the syntax check. The editor files could not be checked this way because they need the Qt headers.
Environment changes are never memoized. assign and begin_with are memoized on the pointer of their assoc_environment argument (assign_memorizer_rep::hash,
std_environment.cpp:138
), but evaluate_assign and evaluate_with build a fresh assoc_environment at each call (
evaluate_macro.cpp:24
, 37). Hence each assignment or with on the path of a modification produces a new environment object, and every evaluation in that environment (the body of the with, the siblings following the assignment) misses the memo table, which is keyed on the environment pointer. Incrementality therefore only holds for subtrees which are evaluated in an unchanged environment object. (Inferred from the code; not measured.)
Dangling entry in the memo table. When the constructor memorizer (memorizer_rep*) overwrites a slot of the sub-computation stack and the previous occupant loses its last reference, it is deleted with tm_delete but not removed from the global table (Style/Memorizer/memorizer.cpp:296-298); the destructor and the assignment operator call bigmem_remove first. A later lookup of an equal memorizer would then compare against freed memory. Whether this situation is reachable has not been determined.
evaluate (environment, tree) on an atomic tree returns before any memorizer is created, so memorize_finalize returns a null memorizer and the caller in edit_interface.cpp:846-847 would dereference it. The document body is always compound, so this is latent.
Creating a memorizer outside of memorize_initialize / memorize_finalize dereferences the null stack (
memorizer.cpp:293
). All current constructions happen inside evaluate (environment, tree).
The main routines print unconditionally (cout at evaluate_main.cpp:417-436, evaluate_rewrite.cpp:186-202, evaluate_inactive.cpp:478-493), and the editor prints the whole rewritten document after each change; on large documents this dominates the run time.
The two evaluators share no code. A semantic fix made in Typeset/Env/ must be made a second time here if this code is ever revived.