SaTeX v0.2.7

Why TeX is hard to analyze

satex analyzes TeX, and LaTeX on top of it, by running it with unknown or partly known values. This page lists what makes that harder than for other languages. Under each problem: what SaTeX does about it, and the research behind that. The problems come first, what TeX does is the next chapter. Every example is a complete document. Below each one, a table puts the text one pdflatex run ships (read from the boxes \tracingoutput logs, random seed 7) next to the text satex computes (satex query typeset), paragraph by paragraph. If a row is not equal, a short list says why: which conditional stayed undecided. Two collapsed blocks show the commands that ran, with the computed text and with a trace of what each call inserted, what each primitive set and how each conditional went.

How to read a table. In the satex column the notation is bold, so a literal {, | or … in the text stays plain:

Notation Meaning
⟦x⟧ text in a box satex cannot place: never shipped, or placed only on some paths. A box that is placed (\box, \copy, \unhbox, \raise and relatives) reads as plain text in order. A box that is never shipped does not count against equality, its row keeps the mark ⊇
{a|b} a choice: an undecided conditional or another place paths part, one arm each
⟨?⟩ an unknown character
c… c repeated one or more times by a summarized loop
de·pen · is a hyphenation point where the line breaker may set a hyphen
a ¶ b a paragraph end some paths make and others do not: a may go on into b
✓ equal
⊇ covers: pdflatex's text is one of satex's choices, so the result is sound but less precise
✗ differs: unsound, a bug

A frame: an abstract machine with reflection

λ-calculus is close in one respect: there are no real calls or return values, only rewriting, and loops are tail recursion through \expandafter. But TeX has no binding and no hygiene, and arguments are token streams. A better frame is an abstract machine:

TeX machine
the input stack, the token stream the continuation
eqtb, registers, macros the store
the save stack, groups dynamic extent
\def, \let at run time reflection, as in 3-Lisp or fexprs

λ-calculus tools still apply to control flow: k-CFA style contexts and pushdown precision for calls and returns. The machine view shows how to make the analysis stop: keep only finitely many places for the pending input and the stored values.

Relations to other formalisms

The problems

1. The syntax is not fixed

\catcode changes what a character means while the document runs. There is no AST before execution. A graph of tokens fits better than a tree. Each token is a node. An edge leads to the tokens that can come next: the next character in the file, the body a macro expands to, the arm a conditional takes. A \catcode change alters the edges for every token read after it, and a loop is a cycle. satex analyzes this graph rather than a parse tree, and terminates because it keeps finitely many abstract input positions per site, so every cycle is finite (see the frame).

\documentclass{article}
\begin{document}
\catcode`\$=0
$def$foo{bar}
$foo
\end{document}
pdflatex shipssatex computes
barbar✓
Computed text
$ satex query typeset -f 01-catcode.tex --set limits.seconds=60
5:5  paragraph  bar
6:15  page  page 1
assumption  page-one-arm
assumption  clock-assumed
Trace
$ satex trace --format compact -f 01-catcode.tex --set limits.seconds=60
l.5 \foo ⇒ bar

2. Code is data

Macros take token lists, build names with \csname, extend themselves (\g@addto@macro), redefine themselves, and read their own text (\meaning).

\documentclass{article}
\makeatletter
\def\hook{A}
\g@addto@macro\hook{B}
\def\once{\global\let\once\relax first}
\begin{document}
\hook, \once\once, \csname h\@gobble Xook\endcsname, \meaning\hook
\end{document}
pdflatex shipssatex computes
AB, first, AB, macro:->ABAB, first, AB, macro:->AB✓
Computed text
$ satex query typeset -f 02-code-is-data.tex --set limits.seconds=60
7:6  paragraph  AB, first, AB, macro:->AB
8:15  page  page 1
assumption  page-one-arm
assumption  clock-assumed
Trace
$ satex trace --format compact -f 02-code-is-data.tex --set limits.seconds=60
l.4 \g@addto@macro ⇒ \begingroup \toks@ \expandafter {\hook B… (defines \hook)
l.7 \hook ⇒ AB
l.7 \once ⇒ \global \let \once \relax first (defines \once)
l.7 \@gobble ⇒ nothing
l.7 \hook ⇒ AB
l.7 \meaning\hook ⇒ macro:->

What SaTeX does. Code is data in satex too: a definition changes the stored macros, and the run goes on with the changed store. This follows Dalla Preda, Giacobazzi and Debray, who track the versions of self-modifying code as abstract values (phase semantics).

\documentclass{article}
\makeatletter
\def\list@{}
\def\add#1{\g@addto@macro\list@{[#1]}}
\begin{document}
\add a\add b\add c\list@
\end{document}
pdflatex shipssatex computes
[a][b][c][a][b][c]✓
Computed text
$ satex query typeset -f r4-self-modifying.tex --set limits.seconds=60
6:25  paragraph  [a][b][c]
7:15  page  page 1
assumption  page-one-arm
assumption  clock-assumed
Trace
$ satex trace --format compact -f r4-self-modifying.tex --set limits.seconds=60
l.6 \add ⇒ \g@addto@macro \list@ {[a]} (defines \list@)
l.6 \add ⇒ \g@addto@macro \list@ {[b]} (defines \list@)
l.6 \add ⇒ \g@addto@macro \list@ {[c]} (defines \list@)
l.6 \list@ ⇒ [a][b][c]

3. Arguments are found at run time

How many arguments a command takes depends on how it reads them: \@ifnextchar, xparse, delimited parameters. Many macros also push their own end markers into the stream, or use \aftergroup.

\documentclass{article}
\makeatletter
\def\opt{\@ifnextchar[\opt@with\opt@without}
\def\opt@with[#1]{(#1)}
\def\opt@without{(none)}
\def\upto#1.{[#1]}
\begin{document}
\opt[a] \opt b \upto everything up to the dot. after
\end{document}
pdflatex shipssatex computes
(a) (none)b [everything up to the dot] after(a) (none)b [everything up to the dot] after✓
Computed text
$ satex query typeset -f 03-arguments.tex --set limits.seconds=60
8:8  paragraph  (a) (none)b [everything up to the dot] after
9:15  page  page 1
assumption  page-one-arm
assumption  clock-assumed
Trace
$ satex trace --format compact -f 03-arguments.tex --set limits.seconds=60
l.8 \opt ⇒ \@ifnextchar [\opt@with \opt@without 
l.3 \@ifnextchar ⇒ \let \reserved@d =[\def \reserved@a {\op… (defines \reserved@d \reserved@a \reserved@b … 1 more)
l.3 \opt@with ⇒ (a)
l.8 \opt ⇒ \@ifnextchar [\opt@with \opt@without 
l.3 \@ifnextchar ⇒ \let \reserved@d =[\def \reserved@a {\op… (defines \reserved@d \reserved@a \reserved@b … 1 more)
l.3 \opt@without ⇒ (none)
l.8 \upto ⇒ [everything up to the dot]

What SaTeX does. An unknown token list is abstracted as a regular language over token classes, as Christensen, Møller and Schwartzbach do for strings and Unevalizer for JavaScript's eval. So \ifx and argument reading still decide what they can, and a catcode write to an unknown character never touches a structural one.

\documentclass{article}
\begin{document}
\edef\x{\ifodd\pdfuniformdeviate 2 ab\else\fi}
\ifx\x\empty empty\else nonempty\fi
\end{document}
pdflatex shipssatex computes
nonempty{empty|nonempty}⊇

Why:

Computed text
$ satex query typeset -f r5-token-domain.tex --set limits.seconds=60
4:15  paragraph  {empty|nonempty}
5:15  page  page 1
assumption  page-one-arm
assumption  clock-assumed
Trace
$ satex trace --format compact -f r5-token-domain.tex --set limits.seconds=60
l.3 \ifodd\pdfuniformdeviate ⇒ undecided
l.4 \ifx\x\empty ⇒ undecided

4. Typesetting feeds back into control flow

Conditionals branch on box sizes and page state. Whether a box has zero width can depend on fonts, kerning and hundreds of other things. Floats and breakable boxes read \pagegoal and \pagetotal, so the page builder's state is part of the control flow. Without a model of it, every float may land here or later, and every breakable box may split anywhere.

\documentclass{article}
\begin{document}
\setbox0\hbox{\textit{f}}
\ifdim\wd0>3pt wide\else narrow\fi,
\ifdim\pagegoal=\maxdimen empty page\else page started\fi
\end{document}
pdflatex shipssatex computes
wide, empty page⟦f⟧wide, {empty page|page started}⊇

Why:

Computed text
$ satex query typeset -f 04-typesetting.tex --set limits.seconds=60
3:25  paragraph  ⟦f⟧wide, {empty page|page started}
6:15  page  page 1
assumption  page-one-arm
assumption  clock-assumed
Trace
$ satex trace --format compact -f 04-typesetting.tex --set limits.seconds=60
l.3 \textit ⇒ \protect \textit   (defines \reserved@a \check@icl \check@icr … 40 more)
l.4 \ifdim\wd0>3pt ⇒ true
l.5 \ifdim\pagegoal=\maxdimen ⇒ undecided

What SaTeX does. Typesetting is modelled only as far as a decision reads it. Relations between values matter here: octagons and zonotopes keep that a dimension minus itself is zero, where intervals forget it, so impossible branches survive and paths multiply, as in pgf's math.

\documentclass{article}
\begin{document}
\dimen0=\pdfuniformdeviate 1000 sp
\dimen2=-\dimen0
\advance\dimen2\dimen0
\ifdim\dimen2=0pt always\else never\fi
\end{document}
pdflatex shipssatex computes
always{always|never}⊇

Why:

Computed text
$ satex query typeset -f r6-relational.tex --set limits.seconds=60
6:20  paragraph  {always|never}
7:15  page  page 1
assumption  page-one-arm
assumption  clock-assumed
Trace
$ satex trace --format compact -f r6-relational.tex --set limits.seconds=60
l.6 \ifdim\dimen2=0pt ⇒ undecided

Where the model stands. The line breaker is tex.web's (§§ 813–890) for a paragraph whose items, metrics and parameters are known. Anything else keeps an interval of lines. A forced break mid-paragraph keeps one active node per fitness class (§ 836), so with \adjdemerits the lines after it may choose the lines before it. satex breaks such a segment only when one class ends it, as after \\, and keeps the interval otherwise. The page builder runs \output at forced breaks (analysis.assume_forced_page_breaks, an assumption, on by default, reported as forced-page-breaks where a page may fill). With the assumption off, a page that may have filled parts the run: one path runs \output, the other reads on. TeX's choice of the best break (§§ 1005–1010) is not modelled yet, so floats that TeX places on the next pages wait for \clearpage under the assumption. The float loops of LaTeX's output routine then run at the end, where unknown box sizes make them converge only by widening.

A rounded node around a paragraph that breaks.

The height of a node with text width depends on the line breaker, and pgf's rounded corners read it. satex marks each possible hyphen (·) but does not choose the breaks, so the height stays unknown, and pgf's math leaves a long tail of unknown boxes.

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node[rounded corners=10pt,draw,text width=3cm,align=center] {files, dependencies, more};
\end{tikzpicture}
\end{document}
pdflatex shipssatex computes
files, de-pen-den-cies, morefiles, de·pen·den·cies, more⊇
Computed text
$ satex query typeset -f s2-rounded-paragraph.tex --set limits.seconds=60
5:64  paragraph  files, de·pen·den·cies, more
7:15  page  page 1
assumption  page-one-arm
assumption  clock-assumed
Trace
$ satex trace --format compact -f s2-rounded-paragraph.tex --set limits.seconds=60
l.4 \tikzpicture ⇒ \begingroup \tikz@startup@env \tikz@coll… (defines ; : | … 327 more)
l.5 \node ⇒ \tikz@path@overlay {node} (defines \tikz@signal@path \pgfutil@reserved@d \pgfutil@reserved@a … 587 more)

5. Packages are interpreters

pgf computes with macro loops, tcolorbox splits boxes, hyperref and biblatex run hooks per page. A small document executes hundreds of thousands of lines of library code.

\documentclass{article}
\usepackage{tikz}
\begin{document}
\pgfmathparse{veclen(3,4)}\pgfmathresult
\tikz\draw[->] (0,0) -- (1,1);
\end{document}
pdflatex shipssatex computes
4.999944.99994✓
Computed text
$ satex query typeset -f 05-packages.tex --set limits.seconds=60
4:41  paragraph  4.99994
6:15  page  page 1
assumption  page-one-arm
assumption  clock-assumed
Trace
$ satex trace --format compact -f 05-packages.tex --set limits.seconds=60
l.4 \pgfmathparse ⇒ \begingroup \pgfmath@catcodes \pgfmath@q… (defines " \ifpgfmath@quickparse \pgfmath@parse@next … 202 more)
l.4 \pgfmathresult ⇒ 4.99994
l.5 \tikz ⇒ \begingroup \tikz@startup@env \tikz@coll… (defines ; : | … 341 more)
l.5 \draw ⇒ \path [draw] (defines \tikz@signal@path \pgfutil@reserved@d \pgfutil@reserved@a … 2827 more)

6. Unknown inputs everywhere

The .aux file is missing on a first run, and the clock, \pdfelapsedtime and random numbers change every run. Without knowing what a macro contains, SaTeX cannot read the tokens after it. One unknown spreads: forks multiply, and arms with different code cannot be merged.

\documentclass{article}
\begin{document}
\ref{later}
\pdfuniformdeviate 100

\def\foo{\ifx\bar\undefined\else\bar\fi}
\def\bar#1{baz}
\foo{x}

\def\possibilityA{1}
\def\possibilityB{12}
\def\doubletaker#1#2{#2#1#2}
\expandafter\expandafter\expandafter\doubletaker
  \csname possibility\ifcsname pdftexversion\endcsname A\else B\fi\endcsname{X}%
Hehe
\section{Later}\label{later}
\end{document}
pdflatex shipssatex computes
1 501 ⟨?⟩⊇
bazxbazx✓
X1XHeheX1XHehe✓
1 Later1 Later✓
assumes forced-page-breaks

Why:

Computed text
$ satex query typeset -f 06-unknown.tex --set limits.seconds=60
3:12  paragraph  1 ⟨?⟩
8:5  page  page 1 → 2 maybe by here
8:5  paragraph  bazx
14:80  paragraph  X1XHehe
16:16  paragraph  1 Later
17:15  page  page 1–?
8:5  assumption  forced-page-breaks
assumption  page-one-arm
assumption  clock-assumed
recognized gaps
  unbalanced-conditional 1× first at 06-unknown.tex:0
Trace
$ satex trace --format compact -f 06-unknown.tex --set limits.seconds=60
l.3 \ref ⇒ \__cmd_start:nNNnnn {s}\ref  \ref code {… (defines \l__cmd_environment_bool \l__cmd_args_tl \l__cmd_fn_tl … 18 more)
l.4 \pdfuniformdeviate ⇒ 􏿿
l.5 \par ⇒ \scan_stop: \mode_if_horizontal:TF {\mod…
l.8 \foo ⇒ \ifx \bar \undefined \else \bar \fi 
l.6 \bar ⇒ baz
l.9 \par ⇒ \scan_stop: \mode_if_horizontal:TF {\mod…
l.14 \ifcsname ⇒ true
… 4 more

What SaTeX does. Unknown conditions stay as choices, as in TypeChef, SuperC and variational execution, which keep presence conditions and values as choices. One token stream with a choice node replaces two forked paths, and the rest is shared. After a bounded number of forks at one conditional, the arms are skipped to \fi and their effects over-approximated on one path.

\documentclass{article}
\begin{document}
\ifdefined\unknownmacro 1\else 12\fi X
\end{document}
pdflatex shipssatex computes
12X12X✓
Computed text
$ satex query typeset -f r3-choice.tex --set limits.seconds=60
3:33  paragraph  12X
4:15  page  page 1
assumption  page-one-arm
assumption  clock-assumed
Trace
$ satex trace --format compact -f r3-choice.tex --set limits.seconds=60
l.3 \ifdefined\unknownmacro ⇒ false

Unknown must stay cheap. Soundness alone is not enough: an unknown value must not cost more than a known one. Library code reads text one token at a time. pgfmath reads a number digit by digit, expl3 breaks a message into lines by counting its characters, and LaTeX's output routine reads every mark. Fed one unknown value, each such loop forks at every step and hits a limit instead of ending. satex keeps unknowns small instead: digits typed with an interval, a value that is one of a few texts, a mark that is one of the texts given to its class. A small interval is split into its values where a name is spelled from it, so each path builds the name TeX builds. Where such a loop still forks at every step, that is a bug to fix, not a cost to accept.

A reference in math inside positioned nodes.

A \ref to an unknown label in math in a tikz node, with the nodes placed by positioning, is sound but far too imprecise: the text of each node becomes a large choice of unknown characters.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{hyperref}
\begin{document}
\begin{tikzpicture}
  \node (a) {$B_{\ref{cell:B-a}}$};
  \node[below=of a] (c) {$B_{\ref{cell:B-c}}$};
  \node[below right=of a] (d) {$B_{\ref{cell:B-d}}$};
  \node[below left=of a] (b) {$B_{\ref{cell:B-b}}$};
  \draw[->] (a) -- (b);
\end{tikzpicture}
\end{document}
pdflatex shipssatex computes
B?? B?? B?? B??iB??iB??iB??iB??✓
Computed text
$ satex query typeset -f s1-tikz-ref.tex --set limits.seconds=60
7:16  paragraph  B??B??B??B??
13:15  page  page 1
assumption  page-one-arm
assumption  clock-assumed
Trace
$ satex trace --format compact -f s1-tikz-ref.tex --set limits.seconds=60
l.3 \usetikzlibrary ⇒ \pgfutil@ifnextchar [{\use@tikzlibrary }… (defines \pgfutil@reserved@d \pgfutil@reserved@a \pgfutil@reserved@b … 16 more)
l.6 \tikzpicture ⇒ \begingroup \tikz@startup@env \tikz@coll… (defines ; : | … 327 more)
l.7 \node ⇒ \tikz@path@overlay {node} (defines \tikz@signal@path \pgfutil@reserved@d \pgfutil@reserved@a … 487 more)
l.7 \ref ⇒ \__cmd_start:nNNnnn {s}\ref  \ref code {… (defines \l__cmd_environment_bool \l__cmd_args_tl \l__cmd_fn_tl … 71 more)
l.8 \node ⇒ \tikz@path@overlay {node} (defines \tikz@signal@path \pgfutil@reserved@d \pgfutil@reserved@a … 474 more)
l.8 \ref ⇒ \__cmd_start:nNNnnn {s}\ref  \ref code {… (defines \l__cmd_environment_bool \l__cmd_args_tl \l__cmd_fn_tl … 51 more)
l.9 \node ⇒ \tikz@path@overlay {node} (defines \tikz@signal@path \pgfutil@reserved@d \pgfutil@reserved@a … 474 more)
… 2 more

7. Termination is not structural

Loops are tail recursion through \expandafter, error recovery, or expansion cuts. Their length can depend on an unknown value.

\documentclass{article}
\newcount\n
\n=\pdfuniformdeviate 8
\def\down{\ifnum\n>0 .\advance\n-1 \expandafter\down\fi}
\begin{document}
\down
\end{document}
pdflatex shipssatex computes
....{.…|}⊇

Why:

Computed text
$ satex query typeset -f 07-termination.tex --set limits.seconds=60
6:6  paragraph  {.…|}
7:15  page  page 1 maybe
assumption  page-one-arm
assumption  clock-assumed
Trace
$ satex trace --format compact -f 07-termination.tex --set limits.seconds=60
l.2 \newcount ⇒ \e@alloc \count \countdef {\count 10}\in… (defines \n)
l.6 \down ⇒ \ifnum \n >0 .\advance \n -1 \expandafte…
l.4 \down ⇒ \ifnum \n >0 .\advance \n -1 \expandafte…

What SaTeX does. Van Horn and Might derive an analysis from the concrete machine and keep the continuation in the store under finitely many addresses, so the analysis ends by construction. Pushdown variants (PDCFA) keep calls and returns matched. For TeX the pending input of a recursion grows with its depth, so satex bounds it. How many passes a loop on an unknown gets before a summary replaces it is a cost question, as in state merging by Kuznetsov et al., who merge when an estimate says it pays.

\documentclass{article}
\def\nest#1{\ifnum#1>0 (\nest{\numexpr#1-1\relax})\fi}
\begin{document}
\nest{3}
\nest{\pdfuniformdeviate 4}
\end{document}
pdflatex shipssatex computes
((())) ()((())) {(…)…|}⊇

Why:

Computed text
$ satex query typeset -f r1-input-stack.tex --set limits.seconds=60
4:9  paragraph  ((())) {(…)…|}
6:15  page  page 1
assumption  page-one-arm
assumption  clock-assumed
Trace
$ satex trace --format compact -f r1-input-stack.tex --set limits.seconds=60
l.4 \nest ⇒ \ifnum 3>0 (\nest {\numexpr 3-1\relax })…
l.5 \nest ⇒ \ifnum \pdfuniformdeviate 4>0 (\nest {\n…
\documentclass{article}
\newcount\n \n=\pdfuniformdeviate 8
\def\cheap{\ifnum\n>0 \advance\n-1 \expandafter\cheap\fi}
\def\costly{\ifnum\n>0 \setbox0\hbox{\tiny long body}\advance\n-1 \expandafter\costly\fi}
\begin{document}
\cheap\costly done
\end{document}
pdflatex shipssatex computes
donedone✓
Computed text
$ satex query typeset -f r7-merge-cost.tex --set limits.seconds=60
6:16  paragraph  done
7:15  page  page 1
assumption  page-one-arm
assumption  clock-assumed
Trace
$ satex trace --format compact -f r7-merge-cost.tex --set limits.seconds=60
l.2 \newcount ⇒ \e@alloc \count \countdef {\count 10}\in… (defines \n)
l.6 \cheap ⇒ \ifnum \n >0 \advance \n -1 \expandafter…
l.6 \costly ⇒ \ifnum \n >0 \setbox 0\hbox {\tiny long …
l.3 \cheap ⇒ \ifnum \n >0 \advance \n -1 \expandafter…
l.6 \costly ⇒ \ifnum \n >0 \setbox 0\hbox {\tiny long …

8. Global, untyped state

Registers, \aftergroup, and groups that one macro opens and another closes. Paragraph and page state change only when the output routine runs.

\documentclass{article}
\def\open{\begingroup\itshape}
\def\close{\endgroup}
\begin{document}
{\gdef\x{ after the group}\aftergroup\x inside}
\open italic\close{} upright
\end{document}
pdflatex shipssatex computes
inside after the group italic uprightinside after the group italic upright✓
Computed text
$ satex query typeset -f 08-global-state.tex --set limits.seconds=60
5:42  paragraph  inside after the group italic upright
7:15  page  page 1
assumption  page-one-arm
assumption  clock-assumed
Trace
$ satex trace --format compact -f 08-global-state.tex --set limits.seconds=60
l.5 \x ⇒  after the group
l.6 \open ⇒ \begingroup \itshape 
l.2 \itshape ⇒ \not@math@alphabet \itshape \mathit \fon… (defines \delayed@f@adjustment \f@shape@saved \f@series@saved … 26 more)
l.6 \close ⇒ \endgroup 

9. Progressive recursion

Macros call each other in a cycle and define the macros that continue it. Names are built from a counter, so the cycle never reaches the same macro twice.

\documentclass{article}
\def\a#1{\ifnum#1<3 \expandafter\def\csname b#1\endcsname{\c{#1}}\csname b#1\endcsname\else end#1\fi}
\def\c#1{\expandafter\def\csname a\the\numexpr#1+1\endcsname{\a{\the\numexpr#1+1}}\csname a\the\numexpr#1+1\endcsname}
\begin{document}
\a{0}
\end{document}
pdflatex shipssatex computes
end3end3✓
Computed text
$ satex query typeset -f 09-progressive.tex --set limits.seconds=60
5:6  paragraph  end3
6:15  page  page 1
assumption  page-one-arm
assumption  clock-assumed
Trace
$ satex trace --format compact -f 09-progressive.tex --set limits.seconds=60
l.5 \a ⇒ \ifnum 0<3 \expandafter \def \csname b0\… (defines \b0 \a1 \b1 … 3 more)

What SaTeX does. Supercompilers (Sørensen and Glück, Leuschel) stop when a new configuration embeds an earlier one (homeomorphic embedding) and replace both by their most specific generalization (anti-unification). This catches a loop whose key grows every pass, which an equality test misses. Together with the self-modifying view of problem 2, each new definition is a new version of the program, not a new program.

\documentclass{article}
\def\grow#1{\ifnum\numexpr#1<5 .\grow{\numexpr#1+1\relax}\fi}
\begin{document}
\grow{0}
\end{document}
pdflatex shipssatex computes
..........✓
Computed text
$ satex query typeset -f r2-growing-key.tex --set limits.seconds=60
4:9  paragraph  .....
5:15  page  page 1
assumption  page-one-arm
assumption  clock-assumed
Trace
$ satex trace --format compact -f r2-growing-key.tex --set limits.seconds=60
l.4 \grow ⇒ \ifnum \numexpr 0<5 .\grow {\numexpr 0+1…

10. The engine is not fixed

pdfTeX, XeTeX and LuaTeX differ in fonts, line breaking and Lua callbacks.

\documentclass{article}
\begin{document}
\ifdefined\directlua LuaTeX\else\ifdefined\XeTeXversion XeTeX\else pdfTeX\fi\fi
\end{document}
pdflatex shipssatex computes
pdfTeXpdfTeX✓
Computed text
$ satex query typeset -f 10-engine.tex --set limits.seconds=60
3:69  paragraph  pdfTeX
4:15  page  page 1
assumption  page-one-arm
assumption  clock-assumed
Trace
$ satex trace --format compact -f 10-engine.tex --set limits.seconds=60
l.3 \ifdefined\directlua ⇒ false
l.3 \ifdefined\XeTeXversion ⇒ false

11. A document is a fixpoint computation

One run is not the program. A build runs pdflatex, bibtex or biber, makeindex, and pdflatex again, until the auxiliary files stop changing. latexmk compares the files after each run and stops at the fixpoint or after a fixed number of runs. Each run reads what the last one wrote: labels and page numbers from the .aux, the table of contents, the bibliography, longtable's column widths, tikz's remember picture positions. A run writes from what it read. A \ref that turns ?? into 12 changes a line's width, which moves a page break, which changes a \pageref. The cycle need not converge. A \pageref whose own text pushes its target onto the next page oscillates between two values, and varioref reports such a loop. Inside one run the output routine is a cycle of its own: it runs when a page fills, and what it puts back decides the next break.

In this example the label moves away from the page the last run recorded. The kept .aux says page 1, so this run breaks the page and writes page 2. The next run reads 2, does not break, and writes 1 again. latexmk gives up after its maximum number of runs.

\documentclass{article}
\usepackage{refcount}
\begin{document}
The label was on page \pageref{x}.
\ifnum\getpagerefnumber{x}=1 \newpage\fi
\label{x}Here.
\end{document}
pdflatex shipssatex computes
The label was on page 1.The label was on page ⟨?⟩.⊇
Here.Here.✓

Why:

Computed text
$ satex query typeset -f 11-fixpoint.tex --set limits.seconds=60
4:2  paragraph  The label was on page ⟨?⟩.
5:38  page  page 1 → 2 maybe
6:11  paragraph  Here.
7:15  page  page 1–2 → next maybe
7:15  page  page 1–3 maybe
assumption  page-one-arm
assumption  clock-assumed
Trace
$ satex trace --format compact -f 11-fixpoint.tex --set limits.seconds=60
l.4 \pageref ⇒ \__cmd_start:nNNnnn {s}\pageref  \pagere… (defines \l__cmd_environment_bool \l__cmd_args_tl \l__cmd_fn_tl … 18 more)
l.5 \getpagerefnumber ⇒ \romannumeral \ltx@ifundefined {r@x}{\ex…
l.5 \ifnum\getpagerefnumber{x}=1 ⇒ undecided
l.5 \getpagerefnumber ⇒ \romannumeral \ltx@ifundefined {r@x}{\ex…
l.5 \ifnum\getpagerefnumber{x}=1 ⇒ undecided
l.5 \newpage ⇒ \if@noskipsec \ifx \@nodocument \relax \… (defines \par \@outputbox@reinsertbskip \@elt … 147 more)
l.6 \label ⇒ \@bsphack \begingroup \UseHookWithArgume… (defines \thepage \protect \reserved@a)

What SaTeX does. satex runs the build itself, as latexmk does, and never needs a file an engine wrote. The files a run writes live in a virtual file system, and the first run starts without any. When a run ends, a file it read back starts another round only where a changed line names a key the run read (a reference, a citation, a key it used) or a macro it expanded or defined (\@abspage@last at each shipout, a note column's \slnc@note), up to limits.build_runs rounds. A line nobody refers to, or one that only changes what a table of contents sets, needs no other round: what such a round would refine is typesetting alone. A round resumes from the last checkpoint before the first read of a changed file, so the part of the document before it is not analyzed again. A file in which a round changed a line already there (a label's page, a counter written back) is from then on the join of what the rounds wrote, so its text only grows and the build settles. Lines only added, as \bibcite after the bibliography's first round, stay exact. Past the last round, what still changes is joined the same way: a widening of the Kleene iteration the build is. The files an earlier engine run left on disk are read only with load.build_files.

Where satex is still wrong or stuck

Each example runs under a 60 second limit. This list is computed when the page is built: every example above whose table has a row marked ✗.

None: every example above is computed exactly or covered soundly.