Lecture 13 · Decision Procedures for Arrays, Strings and Sequences

Introduction to Automated Reasoning · Lecture 13。个人学习讲义与配套实验,按主题整理并持续修订。

课程源码 ZIP ↓

个人整理的学习笔记,非官方讲义;课程来源与整理说明见课程介绍。配套示例未逐一运行验证。

本篇目录

Maps to: Stanford CS 257 Introduction to Automated Reasoning (Winter 2026), Lecture 13 · Tue 02/17/2026 · Decision procedures for arrays, strings and sequences (guest lecture by Clark Barrett). Reading: none listed on the course page. Basis: The Stanford slides are on Canvas. The public Iowa decks cover two of the three topics:

  • First-Order Theories: the theory of arrays TAT_A, its axioms and examples;
  • Theory Solvers III: the theory of strings, the proof system with L-INTRO, L-VALID, C-SPLIT and friends, extended functions as macros, regular-expression reasoning and the inclusion rules.

The rest comes from:

  • Bradley and Manna, The Calculus of Computation, Chapter 11: arrays and the array property fragment;
  • Kroening and Strichman, Decision Procedures, Chapter 7: arrays;
  • Stump, Barrett, Dill and Levitt (2001), extensional arrays;
  • Brummayer and Biere (2009), lemmas on demand for arrays;
  • de Moura and Bjørner (2009), generalized arrays;
  • Liang, Reynolds, Tinelli, Barrett and Deters (CAV 2014), the DPLL(T) string solver behind the slides;
  • Brzozowski (1964), derivatives of regular expressions;
  • Backes et al. (FMCAD 2018), Zelkova;
  • Sheng et al. (IJCAR 2022), sequences.

These notes add two modules:

  • arrays.py: eager and lazy decision procedures for arrays, with array models as certificates;
  • strings.py: regular expressions by derivatives, the slides’ inclusion rules, and the slides’ string calculus with memberships and an optional letter-count abstraction.

The experiments were designed for these notes and do not reflect what was said in class. Sequences get an overview (§10) without code. Code: code/lecture13/ (4 scripts + 2 modules; Python standard library only; under a second to 23 seconds each) Reading time: 3 hours including running the code


0. How to read this lecture

0.1 Learning goals

After this lecture you should be able to:

  1. State the axioms of the theory of arrays, explain what extensionality adds, and describe an array model as a finite map with a default.
  2. Reduce a quantifier-free array formula to EUF eagerly (write elimination, index-set instantiation, extensionality witnesses), and explain why the index set suffices.
  3. Explain lemmas on demand: which axiom instances a model can violate, and why the loop terminates.
  4. Say why string constraints are hard (the decidability map) and how the slides’ proof system works: L-INTRO, L-VALID, C-SPLIT, A-CONF, CONST-CONF.
  5. Explain why the calculus does not terminate on some inputs, and how finer abstractions (letter counts) and fair search help.
  6. Use Brzozowski derivatives for membership, emptiness and inclusion, and compare them with the slides’ incomplete inclusion rules.
  7. Turn a security question (sanitizers, access patterns) into string constraints and read the answer.

0.2 Prerequisites and scope

AssumedIntroduced hereDeveloped later
EUF, congruence closure, CDCL(T) (Lecture 10)the theory of arrays; eager and lazy procedurestheory combination (Lecture 18)
quantifier instantiation as an idea (Lecture 8)index-set instantiationquantifier instantiation in SMT (Lecture 17)
branch and bound, the gcd test (Lecture 12)length and letter-count abstractions—
regular expressions (informally)derivatives, inclusion, length sets—
—symbolic execution (motivation)symbolic execution (Lecture 14)

0.3 Map

§1  why arrays and strings                                     
§2  the theory of arrays: axioms, models, extensionality       ← code 01
§3  the eager approach: write elimination + index set           ← code 01, 02
§4  the lazy approach: lemmas on demand                         ← code 01, 02
§5  certificates for array answers                              ← code 01, 02
§6  strings: the theory and its difficulty
§7  the slides' proof system for word equations                 ← code 03
§8  regular expressions: derivatives, inclusion, memberships   ← code 04
§9  applications: sanitizers, access policies                  ← code 04
§10 sequences (overview)
§11 summary   §12 common pitfalls   §13 exercises   §14 further reading   §15 next lecture

0.4 Notation

SymbolMeaning
a[i]a[i] = read(a,i)(a, i)the element of array aa at index ii
a⟨i◃v⟩a\langle i \triangleleft v\rangle = write(a,i,v)(a, i, v)the array equal to aa except that it holds vv at ii
δ\deltathe default value of an array model (a fresh element)
x⋅yx \cdot y, ϵ\epsilon, ∣x∣\lvert x\rvertconcatenation, the empty word, length
∣x∣c\lvert x\rvert_cthe number of occurrences of the letter cc in xx
x∈Rx \in R, L(R)L(R)membership; the language of the regular expression RR
∂cR\partial_c R, ν(R)\nu(R)the derivative of RR by cc; ”RR accepts ϵ\epsilon”
A, [0..9], ~R, R & Sany character, a range, complement, intersection (the slides’ notation, plus ~ and &)
⟨S; A⟩a proof state: string constraints S, arithmetic constraints A

0.5 The code of this lecture

Two modules.

arrays.py:

  • sort inference array_terms and the printer show;
  • ArrayValue and ArrayModel (array semantics);
  • the eager reduction reduce_to_euf / solve_eager;
  • the lazy loop violated_lemmas / solve_lazy;
  • certificates model_from_euf, check_model_arrays, lemma_kind, check_unsat_arrays, certify;
  • benchmarks swaps, store_commute, ALIASING, BM_EXAMPLE, random_array_formula.

strings.py:

  • regex smart constructors, deriv / rderiv / nullable, witness, included, word_of_length, length_set;
  • the slides’ inclusion rules rule_proof;
  • the parser parse_constraints (with the macros), check_model and the independent matcher naive_match;
  • the solver StringSolver / solve_strings, with lia_check for the arithmetic part;
  • brute_force for bounded ground truth.

1. Why arrays and strings

Lecture 10’s slides already showed the pipeline of symbolic execution for security (the Iowa deck repeats it for strings):

code + security policy  →  symbolic execution  →  SMT formula  →  SMT solver  →  "unsafe: here is an input"
                                                                              or "safe"

Lecture 14 builds the left half. This lecture is about what the formulas contain once programs do more than arithmetic:

  • Memory is an array. A heap, a stack frame, a hash table or a C array is a function from addresses to values. *p = 1 is a write, *q is a read, and the question “can a write through q change what p points to?” is a question about arrays (script 01, Part 4).
  • Strings are everywhere in security. Queries, paths, URLs and policies are built by concatenation and checked by regular expressions. Bugs look like “some input makes this string look like that” (script 04, Parts 4–5).

Both theories have a feature that EUF and arithmetic do not: their natural axioms talk about all indices or all positions. The decision procedures in this lecture are, at heart, clever ways of instantiating those universal statements only where needed.


2. The theory of arrays

2.1 Signature and axioms

The slides’ TAT_A has three sorts: A (arrays), I (indices) and E (elements). It has two function symbols, read : A × I → E and write : A × I × E → A, and these axioms:

  1. Read-over-write, same index: ∀a,i,v. read(write(a,i,v),i)=v\forall a, i, v.\ \text{read}(\text{write}(a, i, v), i) = v
  2. Read-over-write, other index: ∀a,i,j,v. i≠j⇒read(write(a,i,v),j)=read(a,j)\forall a, i, j, v.\ i \neq j \Rightarrow \text{read}(\text{write}(a, i, v), j) = \text{read}(a, j)
  3. Extensionality: ∀a,b. (∀i. read(a,i)=read(b,i))⇒a=b\forall a, b.\ (\forall i.\ \text{read}(a, i) = \text{read}(b, i)) \Rightarrow a = b

Dropping axiom 3 gives the theory without extensionality.

Decidability. Full first-order TAT_A is undecidable. The quantifier-free fragment, with arbitrary Boolean structure, is decidable: NP-complete, like SAT itself. A larger decidable fragment allows some universal quantifiers over indices, guarded by index ranges: Bradley and Manna’s array property fragment. It can say ”aa is sorted between ll and uu”:

∀i,j. l≤i≤j≤u⇒a[i]≤a[j]\forall i, j.\ l \le i \le j \le u \Rightarrow a[i] \le a[j]

That needs integer indices, i.e. arrays combined with arithmetic, which is Lecture 18’s topic. This lecture’s code keeps indices uninterpreted (one scalar sort for indices and elements, so a[a[i]]a[a[i]] is allowed).

2.2 What an array model is

An array is a function from indices to elements. A quantifier-free formula mentions only finitely many indices, though, so every array we ever need is

a default value everywhere, except at finitely many places.

ArrayValue stores exactly that: a finite map and a default δ (a fresh element). Two array values are equal when their maps and defaults are equal, which is equality as functions: entries equal to the default are dropped.

Script 01, Part 1 evaluates terms in the model a=[1↦x,else δ]a = [1 \mapsto x, \text{else } \delta], i=1i = 1, j=2j = 2, e=ye = y:

termvalue
a[i]a[i]xx
a[j]a[j]δ
a⟨i◃e⟩a\langle i \triangleleft e\rangle[1↦y,else δ][1 \mapsto y, \text{else } \delta]
a⟨i◃e⟩[i]a\langle i \triangleleft e\rangle[i]yy
a⟨i◃e⟩[j]a\langle i \triangleleft e\rangle[j]δ
a⟨i◃e⟩⟨i◃a[i]⟩a\langle i \triangleleft e\rangle\langle i \triangleleft a[i]\rangle[1↦x,else δ][1 \mapsto x, \text{else } \delta] (equal to aa)

2.3 What extensionality buys

The slides ask whether two formulas are valid. We check validity by showing the negation unsatisfiable:

claimnegationclaim is
read(write(a, i, e), i) = e (slides’ Ex. 1)UNSATvalid
write(a, i, a[i]) = a, with extensionalityUNSATvalid
write(a, i, a[i]) = a, without extensionalitySATnot valid

Why. write(a,i,a[i])\text{write}(a, i, a[i]) and aa agree at every index: at ii by axiom 1, everywhere else by axiom 2. But “equal at every index” becomes “equal” only through axiom 3. Without it, the solver may keep the two arrays apart. Its “countermodel” (a=[i↦#2,else δ]a = [i \mapsto \#2, \text{else } \delta], with the write in another class) is then no array model in the intended sense: the certificate check, which compares arrays as functions, rejects it.

The slides’ Ex. 2, ∀i. read(a,i)=read(a′,i)⇒a=a′\forall i.\ \text{read}(a, i) = \text{read}(a', i) \Rightarrow a = a', is axiom 3 itself. It is valid in TAT_A and not in the theory without extensionality.


3. The eager approach: instantiate the axioms up front

3.1 The idea

Lecture 10’s eager approach to EUF (Ackermann) replaced functions by constants plus consistency constraints. Here we do the same with arrays: turn the array formula into an EUF formula in which read is an ordinary binary function and arrays are constants. reduce_to_euf has three steps.

  1. Write elimination. Replace each write term b⟨i◃v⟩b\langle i \triangleleft v\rangle, innermost first, by a fresh array constant AA. What the formula needs to know about AA is axioms 1 and 2: A[i]=vand∀j. j≠i⇒A[j]=b[j].A[i] = v \qquad\text{and}\qquad \forall j.\ j \neq i \Rightarrow A[j] = b[j].
  2. Extensionality witnesses. For every equality s=ts = t between arrays, add s=t ∨ s[k]≠t[k]s = t \ \vee\ s[k] \neq t[k] with a fresh index kk: if two arrays differ, they differ somewhere (axiom 3, Skolemized). The other direction, s=t⇒s[j]=t[j]s = t \Rightarrow s[j] = t[j], is congruence, which EUF already knows.
  3. Index-set instantiation. Instantiate the ∀j of step 1 for every term used as an index: the index set, witnesses included. For each write this adds i=j∨A[j]=b[j]i = j \vee A[j] = b[j] for each index term j≠ij \neq i.

3.2 Why the index set is enough

A model has to get right only the reads that the formula actually performs, and every read uses an index from the set. Two reads at indices outside the set never meet, so the universally quantified axiom can be “true elsewhere” for free.

Bradley and Manna prove this for the whole array property fragment. arrays.py checks it empirically:

  • every SAT model is turned into an array model and evaluated on the original formula with array semantics;
  • eager and lazy answers are compared on random formulas.

That check caught a real bug in the first version (§5).

3.3 The example

Script 01, Part 2, on an example in the style of Bradley and Manna:

φ:i1=j∧i1≠i2∧a[j]=e1∧a⟨i1◃e1⟩⟨i2◃e2⟩[j]≠a[j]\varphi:\quad i_1 = j \wedge i_1 \neq i_2 \wedge a[j] = e_1 \wedge a\langle i_1 \triangleleft e_1\rangle\langle i_2 \triangleleft e_2\rangle[j] \neq a[j]

write elimination: A1 stands for a⟨i1◁e1⟩
write elimination: A2 stands for A1⟨i2◁e2⟩
index set: {j, i1, i2}

instances added:
    A1[i1] = e1
    i1 = j ∨ A1[j] = a[j]
    i1 = i2 ∨ A1[i2] = a[i2]
    A2[i2] = e2
    i2 = j ∨ A2[j] = A1[j]
    i2 = i1 ∨ A2[i1] = A1[i1]

EUF result: UNSAT; 6 instances, 11 EUF atoms, 1 conflict(s), 0 decisions; refutation ok

Reading the refutation. i2≠ji_2 \neq j (because j=i1≠i2j = i_1 \neq i_2), so A2[j]=A1[j]A_2[j] = A_1[j]. Also A1[j]=A1[i1]=e1A_1[j] = A_1[i_1] = e_1 by congruence (j=i1j = i_1), so A2[j]=e1=a[j]A_2[j] = e_1 = a[j], contradicting the last conjunct.

3.4 The cost

There are (number of writes) × (size of the index set) instances, a quadratic number, all added before the search starts, needed or not. Each is a case split i=j∨…i = j \vee \ldots for the SAT solver.

Run it: python3 01_arrays.py (under a second).


4. The lazy approach: lemmas on demand

4.1 The idea

Treat read and write as uninterpreted functions and solve the formula in EUF. The EUF solver knows nothing about arrays, so its model may be nonsense. For example, it may say a⟨i◃e⟩[i]≠ea\langle i \triangleleft e\rangle[i] \neq e. Then:

repeat:
    solve  formula ∧ lemmas  in EUF (read, write uninterpreted)
    UNSAT → UNSAT        (the lemmas are valid in T_A, so the formula is T_A-unsatisfiable)
    SAT   → look for axiom instances the model VIOLATES
            none → the model is an array model: SAT
            some → add them as lemmas, and go round again

This is the Lecture 10 story again: the very lazy loop, with theory lemmas that are axiom instances. Boolector’s “lemmas on demand” for arrays (Brummayer and Biere 2009) and the array solvers in Z3 and cvc5 work this way, but online: they add the lemmas inside one CDCL(T) run. Our loop restarts CDCL(T) each round, because Lecture 5’s SAT solver cannot grow new variables mid-search.

4.2 Which instances can a model violate?

violated_lemmas reads the EUF model as arrays. For each array class CC, the reads x[j]x[j] with x∈Cx \in C give a finite map MCM_C from index classes to element classes. Then for every write w=b⟨i◃v⟩w = b\langle i \triangleleft v\rangle, with ww in class WW and bb in class BB:

axiomwhat the model must satisfylemma added if not
(1)MW[i]=vM_W[i] = vw[i]=vw[i] = v
(2)MW[J]=MB[J]M_W[J] = M_B[J] for every index class J≠[i]J \neq [i] read in WW or BBi=j∨w[j]=b[j]i = j \vee w[j] = b[j] (jj a term of JJ)
(3)for every array equality s=ts = t that is false in the model: MSM_S and MTM_T differ somewheres=t∨s[k]≠t[k]s = t \vee s[k] \neq t[k], kk fresh

Two details matter.

  • The lemma speaks about the write term ww, not about the array xx that was actually read. Congruence carries it to every array equal to ww, which is how lemmas reach both down (from a read of a write to its base) and up (from a read of the base to the write).
  • Termination. Every lemma is built from the input’s writes, its index terms and one witness per array equality, so there are finitely many. A lemma, once added, holds in every later model.

Why “no violation” means “array model”. Define each array class’s value as MCM_C with default δ elsewhere. Then:

  • axiom 1 holds at ii;
  • axiom 2 holds at every index read in WW or BB, and at all other indices both sides are δ;
  • axiom 3 holds because unequal classes differ somewhere.

So reads, writes and equalities all evaluate as EUF said. model_from_euf builds exactly this model, and check_model_arrays verifies it.

4.3 The example, round by round

Script 01, Part 3, on the same φ\varphi:

round 1: EUF says SAT
    add lemma  a⟨i1◁e1⟩[i1] = e1
    add lemma  a⟨i1◁e1⟩⟨i2◁e2⟩[i2] = e2
    add lemma  i2 = j ∨ a⟨i1◁e1⟩⟨i2◁e2⟩[j] = a⟨i1◁e1⟩[j]

round 2: EUF says UNSAT

UNSAT after 2 rounds and 3 lemmas (eager: 6 instances); refutation ok

The lazy approach never adds i1=i2∨…i_1 = i_2 \vee \ldots or the instances at i1i_1: no model ever violated them.

4.4 Memory and aliasing

Script 01, Part 4 asks whether *p = c1; *q = c2; assert(*p == c1) can fail (with c1≠c2c_1 \neq c_2):

caseprocedureresultEUF callslemmas
no assumption about p, qlazySAT23
no assumption about p, qeagerSAT110
assume p ≠ qlazyUNSAT23
assume p ≠ qeagerUNSAT110

The failing execution is m1 = [p ↦ c1, else δ]; m2 = [p ↦ c2, else δ]; p = q: the assertion fails exactly when the pointers alias. Program verifiers and symbolic executors ask such questions constantly.

4.5 Eager vs lazy, measured

Script 02 compares the two on three families, with every answer certified.

Swaps. nn random swaps followed by the same swaps in reverse restore the array. As a formula: a1=swap(a,…),…,a2n≠aa_1 = \text{swap}(a, \ldots), \ldots, a_{2n} \neq a, which is UNSAT. Each step is a named array, as a symbolic executor would produce.

formulaindicesapproachinstances / lemmasEUF callsconflictstime
swaps(16)3eager2,3371241.23 s
swaps(16)3lazy25734440.73 s
swaps(4)6eager23311340.29 s
swaps(4)6lazy97101770.14 s
swaps(8)6eager75314085.41 s
swaps(8)6lazy225183631.01 s

With 6 distinct indices, the eager formula carries many useless case splits i=j∨…i = j \vee \ldots that the SAT solver explores anyway. At n=8n = 8 the lazy approach is about 5 times faster, and the gap grows with nn (see figures/02_eager_vs_lazy.png).

Store commutation (the SMT-LIB “storecomm” family). Writes at pairwise different indices commute:

formulaapproachinstances / lemmasEUF callstime
storecomm(8)eager14510.02 s
storecomm(8)lazy89100.06 s
storecomm(16)eager54510.20 s
storecomm(16)lazy305180.80 s

Here almost every instance is needed: the extensionality witness must be compared with every index, through every write. The lazy loop rediscovers them over n+2n + 2 rounds, restarting each time, so eager wins on this family. Without the assumption i1≠i2i_1 \neq i_2 the formula is SAT, and the counterexample has i1=i2i_1 = i_2.

Random formulas. On 100 random array formulas (61 SAT, 39 UNSAT):

  • the approaches agree on all 100, and 200/200 answers are certified;
  • lazy adds 982 lemmas against 2,454 eager instances, but its 257 EUF calls make it slower on these small inputs (0.42 s vs 0.27 s).

The lesson. Neither approach dominates. Real solvers mix them, for instance adding axiom (1) eagerly and (2)–(3) lazily, and they add lemmas online. Later refinements avoid most instances altogether, by reasoning about “weak equivalence” of arrays (Christ and Hoenicke 2015).

Run it: python3 02_array_benchmarks.py (about 23 s), with figures/02_eager_vs_lazy.png.


5. Certificates for array answers

As in Lectures 10–12, no answer is reported on trust.

  • SAT. model_from_euf builds an array model from the EUF classes. check_model_arrays evaluates the original formula in it, with array semantics: reads look up, writes update, arrays are equal iff they are the same function. This shares no code with congruence closure.

  • UNSAT. Every added lemma must be an instance of an axiom. lemma_kind checks the shape:

    • row1 is w[i]=vw[i] = v for w=b⟨i◃v⟩w = b\langle i \triangleleft v\rangle;
    • row2 is i=j∨w[j]=b[j]i = j \vee w[j] = b[j];
    • ext is s=t∨s[k]≠t[k]s = t \vee s[k] \neq t[k], with a kk that occurs nowhere else and serves one pair only.

    The final EUF refutation is then checked by Lecture 10’s check_unsat: its theory lemmas are re-checked by an independent congruence procedure, and the propositional part by DRUP.

A bug the certificate caught. The first eager reduction built the index set from the reads that remain after write elimination. In b⟨i1◃a[e1]⟩b\langle i_1 \triangleleft a[e_1]\rangle, the read a[e1]a[e_1] disappears from the formula together with the write, so the index e1e_1 was forgotten. The EUF model was then not an array model, and the array-semantics evaluation failed on random formula 19. The fix collects index terms from the eliminated writes’ arguments too.


6. Strings: the theory and its difficulty

6.1 The basic theory

The slides’ basic theory of strings has:

  • sorts: String, Int, RegEx;
  • constants: a fixed finite alphabet (here printable ASCII), the empty string ε, character strings, numerals;
  • operators: concatenation x⋅yx \cdot y, length ∣x∣|x|, membership x∈Rx \in R, plus integer ++ and >>.

The decidability map (the slides):

constraintsstatusreference
concatenation + equality (word equations)decidable, in PSPACEMakanin 1977; Plandowski 2004; Jeż 2016
+ length constraintsdecidability open—
+ replace-allundecidableLin and Barceló 2016

Even the decidable case is far too expensive to implement directly: Makanin’s algorithm is famously complicated.

6.2 The pragmatic approach

The slides’ answer, which is cvc5’s and Z3’s:

  • a rule-based proof system, whose rules are each easy to check;
  • the existing arithmetic solver for everything about lengths;
  • embrace incompleteness: the procedure may run forever on some unsatisfiable inputs, and real solvers return “unknown” at a budget.

strings.py implements this for conjunctions of constraints. In cvc5 the same rules run as a theory solver inside DPLL(T), and the case splits become new literals for the SAT solver.


7. The proof system for word equations

7.1 Proof states

A proof state is SAT, UNSAT, or a pair ⟨S; A⟩:

  • S holds the string constraints, flattened so that every literal is simple;
  • A holds the arithmetic constraints;
  • every string variable xx has a length variable ℓx=∣x∣\ell_x = |x|.

The length normalization ∣⋅∣ ⁣↓|\cdot|\!\downarrow is ∣ϵ∣ ⁣↓=0,∣c∣ ⁣↓=1,∣s1⋯sn∣ ⁣↓=∣s1∣ ⁣↓+⋯+∣sn∣ ⁣↓.|\epsilon|\!\downarrow = 0, \qquad |c|\!\downarrow = 1, \qquad |s_1 \cdots s_n|\!\downarrow = |s_1|\!\downarrow + \cdots + |s_n|\!\downarrow.

7.2 The rules

Core rules (the slides, with the names StringSolver logs):

rulewheneffect
L-INTROan equation s=ts = t in Sadd ∣s∣ ⁣↓=∣t∣ ⁣↓\lvert s\rvert\!\downarrow = \lvert t\rvert\!\downarrow to A
L-VALIDa variable xxbranch: x=ϵx = \epsilon | ℓx>0\ell_x > 0
A-CONFA is unsatisfiable (over Z\mathbb{Z})close: UNSAT
S-CONF / CONST-CONFS is contradictory; two different characters must be equalclose: UNSAT
A-PROPA entails an equality between length termsadd it to S (here: ℓx=0\ell_x = 0 gives x:=ϵx := \epsilon)
S-PROPS entails an equality between length termsadd it to A (here: an equation x=wx = w with x∉wx \notin w is solved by substitution)
SATno other rule appliesa model exists

Concatenation rules.

  • C-EQ: two variables that expand to the same concatenation are equal.
  • C-SPLIT: two expansions of one variable, x=w⋅u⋅zx = w \cdot u \cdot z and x=w⋅v⋅z′x = w \cdot v \cdot z', agree up to ww and then meet different variables uu and vv. Compare their lengths, in three branches:
    • ℓu>ℓv\ell_u > \ell_v: u=v⋅ku = v \cdot k for a fresh kk;
    • ℓu<ℓv\ell_u < \ell_v: v=u⋅kv = u \cdot k;
    • ℓu=ℓv\ell_u = \ell_v: u=vu = v.

StringSolver works on equations between words rather than variable expansions; after substitution and stripping of common prefixes, the two views coincide. A variable meeting a character, x⋯=c⋯x \cdots = c \cdots, is split with L-VALID: x=ϵx = \epsilon or x=c⋅kx = c \cdot k.

One deviation from the slides. The solver adds ℓx≥0\ell_x \ge 0 for every variable up front. That is the arithmetic content of L-VALID, and it saves branches.

7.3 The slides’ derivation

The slides derive UNSAT for x=y⋅x⋅z∧y="a"x = y \cdot x \cdot z \wedge y = \text{"a"}. L-INTRO gives ℓx=ℓy+ℓx+ℓz\ell_x = \ell_y + \ell_x + \ell_z and ℓy=1\ell_y = 1, so ℓz=−1\ell_z = -1. The slides then split with L-VALID (z=ϵz = \epsilon | ℓz>0\ell_z > 0) and close both branches by A-CONF. Script 03 prints:

x = y·x·z ∧ y = "a"
    S-PROP     y := "a"
    A-CONF     the arithmetic constraints are unsatisfiable
-> UNSAT after 1 proof states

The same argument with non-negativity built in closes the root at once.

C-SPLIT at work (x=u⋅z∧x=v⋅w∧∣u∣=2∧∣v∣=1x = u \cdot z \wedge x = v \cdot w \wedge |u| = 2 \wedge |v| = 1):

S-PROP     x := u·z
C-SPLIT    |u| = |v|: u := v
  S-PROP     z := w
  A-CONF     the arithmetic constraints are unsatisfiable
C-SPLIT    |u| > |v|: u := v·_k1
  S-PROP     w := _k1·z
  SAT        x = 'aa', u = 'aa', z = '', v = 'a', w = 'a'

Every model is checked on the input (check_model, with an independent regex matcher) before it is reported.

7.4 Properties: sound, but not terminating

The slides: the system is

  • refutation sound (easy: check each rule);
  • solution sound (hard to prove; we check every model anyway);
  • not terminating (“for pathological unsat cases, C-SPLIT can be applied infinitely often”);
  • hence incomplete.

Script 03, Part 2 shows non-termination on x⋅"a"="b"⋅xx \cdot \text{"a"} = \text{"b"} \cdot x:

L-VALID    x = ε
  CONST-CONF "a" = "b"
L-VALID    |x| > 0: x := "b"·_k1
  L-VALID    _k1 = ε
    CONST-CONF "a" = "b"
  L-VALID    |_k1| > 0: _k1 := "b"·_k2
    ...
state budgetresultstatesdepth
25UNKNOWN2613
50UNKNOWN5117
100UNKNOWN10133
200UNKNOWN20149

Why it loops. x=ϵx = \epsilon fails, so x=b⋅kx = b \cdot k. After removing the common b, the equation is k⋅a=b⋅kk \cdot a = b \cdot k: the same equation again. Length reasoning cannot help, because ∣x∣+1=1+∣x∣|x| + 1 = 1 + |x| always holds.

7.5 A finer abstraction: counting letters

Length is one number per variable. A finer abstraction keeps one number per letter: ∣x∣a,∣x∣b,…|x|_a, |x|_b, \ldots, the Parikh image of the word. Every equation gives one linear equation per letter, and length is their sum, so the new abstraction is never weaker. On x⋅a=b⋅xx \cdot a = b \cdot x the a-count says ∣x∣a+1=∣x∣a|x|_a + 1 = |x|_a: A-CONF at the root (counts=True).

Script 03, Part 3, on 100 random word equations (2 variables, alphabet {a, b}, budget 200 states):

abstractionSATUNSATUNKNOWNstatesUNSAT confirmed by brute force
lengths only5245377445/45
letter counts5248016348/48

“Confirmed by brute force” means no solution with words of length ≤ 4 over {a, b}: bounded evidence, not a proof. String solvers do not yet produce checkable UNSAT certificates the way SAT solvers do; cvc5’s string proof rules are recent research.

Counting is not enough either. Part 4 shows "aa"⋅y⋅"b"=y⋅"b"⋅x\text{"aa"} \cdot y \cdot \text{"b"} = y \cdot \text{"b"} \cdot x. The counts force x=aax = \text{aa}, but the search splits only at the left end: y=ϵ,a,aa,…y = \epsilon, a, aa, \ldots each fails, and yy can always grow. It is UNKNOWN after 101 states; brute force finds no solution up to length 5. Reading from the right would end it: the left side ends in b, so xx would have to end in b, but xx has no b. Real solvers compare both ends and much more (cvc5’s normal forms), and still remain incomplete.

7.6 Lengths are integers

Script 03, Part 5:

constraintsresultwhy
x⋅x=a⋅y⋅yx \cdot x = a \cdot y \cdot yUNSAT at the root2ℓx=1+2ℓy2\ell_x = 1 + 2\ell_y: the gcd test (Lecture 12)
x⋅y=y⋅x∧∣x∣=2∧∣y∣=3x \cdot y = y \cdot x \wedge \lvert x\rvert = 2 \wedge \lvert y\rvert = 3SAT: x=aax = aa, y=aaay = aaacommuting words are powers of one word (Lyndon–Schützenberger)
x⋅ab=ba⋅y∧∣x∣=1x \cdot ab = ba \cdot y \wedge \lvert x\rvert = 1SAT: x=bx = b, y=by = bA-PROP: ℓk1=0\ell_{k_1} = 0 is entailed, so k1:=ϵk_1 := \epsilon

The first row would be satisfiable over the reals: the arithmetic part must be integer arithmetic.

Equality elimination. lia_check first eliminates every equality with a coefficient ±1, by substitution. Branch and bound alone runs forever on ℓx=2m∧ℓx=2n+1\ell_x = 2m \wedge \ell_x = 2n + 1: it is Lecture 12’s endless strip again. After eliminating ℓx\ell_x, the gcd test sees 2m−2n=12m - 2n = 1 at once. That is the first step of the Omega test (Lecture 12, §5).

A plain depth-first search can dive forever into one branch (x:=a⋅kx := a \cdot k, k:=a⋅k′k := a \cdot k', …) and never look at the branch that has the solution. StringSolver uses iterative deepening: depth limit 16, then 32, 64, and so on. Every branch is reached eventually, so every satisfiable input is eventually solved; only unsatisfiable inputs can still run forever.

Run it: python3 03_word_equations.py (about 3 s).


8. Regular expressions

8.1 Derivatives

The slides note that the automata-based approach to x∈R1∧x∉R2x \in R_1 \wedge x \notin R_2 must build R1∩comp(R2)R_1 \cap \text{comp}(R_2), and that complement and intersection of automata are expensive (a product and a subset construction).

Brzozowski derivatives (1964) avoid building automata up front. The derivative ∂cR\partial_c R is the language of what may follow cc: ∂cR={ w:c⋅w∈L(R) }.\partial_c R = \{\,w : c \cdot w \in L(R)\,\}. It is again a regular expression, computed by structural rules:

RR∂cR\partial_c R
∅, ε∅
a character class SSε if c∈Sc \in S, else ∅
R1∣R2R_1 \mid R_2, R1 & R2R_1 \,\&\, R_2, ∼R\sim R∂cR1∣∂cR2\partial_c R_1 \mid \partial_c R_2, ∂cR1 & ∂cR2\partial_c R_1 \,\&\, \partial_c R_2, ∼∂cR\sim\partial_c R
R∗R^*(∂cR)⋅R∗(\partial_c R) \cdot R^*
R1⋅R2R_1 \cdot R_2(∂cR1)⋅R2∣(∂cR2 if ν(R1))(\partial_c R_1) \cdot R_2 \mid (\partial_c R_2 \text{ if } \nu(R_1))

Here ν(R)\nu(R) (“nullable”) says whether ϵ∈L(R)\epsilon \in L(R). Membership is a loop: w∈L(R)w \in L(R) iff ν(∂wn⋯∂w1R)\nu(\partial_{w_n} \cdots \partial_{w_1} R).

Script 04, Part 1, on R=[0..9]∗⋅"a"⋅A∗R = [0..9]^* \cdot \text{"a"} \cdot A^*:

readderivativeν
‘1’[0..9]∗⋅"a"⋅A∗[0..9]^* \cdot \text{"a"} \cdot A^*no
‘2’[0..9]∗⋅"a"⋅A∗[0..9]^* \cdot \text{"a"} \cdot A^*no
‘a’A∗A^*yes
’!’A∗A^*yes

The word ‘12b’ dies at ‘b’ (the derivative becomes ∅).

Why derivatives are finite. The derivatives are the states of an automaton built on demand. Brzozowski proved that, up to simplification (associativity, commutativity and idempotence of |), there are finitely many. The smart constructors in strings.py keep regexes in such a normal form.

  • Emptiness: witness explores derivatives breadth first and returns a shortest word of the language, or None.
  • Inclusion: L(R1)⊆L(R2)L(R_1) \subseteq L(R_2) iff L(R1 & ∼R2)=∅L(R_1 \,\&\, \sim R_2) = \emptyset, so the same search decides it and returns a counterexample when it fails.
  • Finite alphabet: the alphabet (95 characters) is cut into minterms, pieces on which every character class agrees, and only one representative per piece is explored.

8.2 The slides’ inclusion rules

The slides propose proving L(R1)⊆L(R2)L(R_1) \subseteq L(R_2) with a “fast, incomplete procedure”, using rules such as:

  • (1) L(R)⊆L(R)L(R) \subseteq L(R);
  • (4) L(R)⊆L(A∗)L(R) \subseteq L(A^*);
  • (6) from L(R1)⊆L(R2)L(R_1) \subseteq L(R_2) infer L(R1∗)⊆L(R2∗)L(R_1^*) \subseteq L(R_2^*);
  • (8) concatenation, componentwise;
  • (9) ranges: [c1..c2]⊆[c3..c4][c_1..c_2] \subseteq [c_3..c_4] when c3⪯c1c_3 \preceq c_1 and c2⪯c4c_2 \preceq c_4.

rule_proof searches with all of them except (7), transitivity, which needs a guessed intermediate expression. It reproduces the slides’ exercise:

L([0..1]*·A*·"b"·A*) ⊆ L([0..9]*·A*)   by (8)
    L([0..1]*) ⊆ L([0..9]*)   by (6)
        L([0..1]) ⊆ L([0..9])   by (9)
    L(A*·"b"·A*) ⊆ L(A*)   by (4)

Script 04, Part 2 compares it with the exact check:

R1R_1R2R_2slides’ rules⊆\subseteq?counterexample
[0..1]*·A*·“b”·A*[0..9]*·A*provedyes—
A*·“ab”·A*A*·“b”·A*provedyes—
(A·A)*A*provedyes—
[a..b]*(“a”*·“b”*)*no proofyes—
[0..9]·[0..9]*[0..9]*no proofyes—
“a”·(“ba”)*(“ab”)*·“a”no proofyes—
[0..9]*[0..9]·[0..9]*no proofnoε
A*·“ab”·A*A*·“ba”·A*no proofno“ab”

Why the rules fail. They have no rule for:

  • an alternative on the left (a∣ba \mid b is the class [a..b][a..b], and [a..b]⊆a∗b∗[a..b] \subseteq a^* b^* needs one);
  • R⋅R∗⊆R∗R \cdot R^* \subseteq R^*;
  • regrouping a(ba)∗a(ba)^* as (ab)∗a(ab)^*a.

They are sound and cheap, and a solver uses them first. The exact check is the fallback.

8.3 Memberships in the string solver

Memberships enter StringSolver through these rules:

ruleeffect
M-DERIVc⋅w∈Rc \cdot w \in R becomes w∈∂cRw \in \partial_c R; w⋅c∈Rw \cdot c \in R uses the right derivative {w:w⋅c∈L(R)}\{w : w \cdot c \in L(R)\}
M-EMPTYthe memberships of one variable intersect to ∅: close the branch
M-APPROXw∈Rw \in R is impossible even with every variable of ww replaced by A∗A^* (or its own regex): close the branch
M-UNFOLDy⋅w∈Ry \cdot w \in R with several variables: branch y=ϵy = \epsilon | y=c⋅ky = c \cdot k for each first character cc of RR (the slides’ “unfolding”, which may not terminate)
M-LENthe set of lengths of L(R)L(R) is ultimately periodic; when it is one progression a+d⋅ma + d\cdot m, tell the arithmetic solver
M-PICKat the end, a variable needs a word of the chosen length; if there is none, branch ∣x∣<n\lvert x\rvert < n | ∣x∣>n\lvert x\rvert > n

Script 04, Part 3:

constraintsresultmodel
x∈[0..9]∗⋅"a"⋅A∗∧x∉[0..9]∗⋅"a"⋅A∗⋅"b"⋅A∗x \in [0..9]^*\cdot\text{"a"}\cdot A^* \wedge x \notin [0..9]^*\cdot\text{"a"}\cdot A^*\cdot\text{"b"}\cdot A^*SATx=ax = \text{a}
x∈[0..9]+∧x∉[0..4]∗∧∣x∣=2x \in [0..9]^+ \wedge x \notin [0..4]^* \wedge \lvert x\rvert = 2SATx=05x = 05
y="id"⋅x∧y∈A∗⋅[0..9]∧∣x∣=3∧xy = \text{"id"}\cdot x \wedge y \in A^*\cdot[0..9] \wedge \lvert x\rvert = 3 \wedge x has at most one digitSATx=aa0x = \text{aa0}
x∈("aa")∗∧∣x∣=2n+1x \in (\text{"aa"})^* \wedge \lvert x\rvert = 2n + 1UNSAT—
the same without M-LENUNKNOWN after 61 states—

Why M-LEN matters. The lengths of a regular language form an ultimately periodic set: a unary automaton, the subset sequence S0,S1,…S_0, S_1, \ldots of length_set. For (aa)∗(aa)^* they are {0+2m}\{0 + 2m\}, and the gcd test refutes 2m=2n+12m = 2n + 1. Without M-LEN, the solver tries length 1, 3, 5, …, excludes each and tries the next, forever.

8.4 Extended functions as macros

The slides extend the theory with substr, contains, index_of and replace, and implement them by reduction to the core. For example, contains(y,z)\text{contains}(y, z) is ∃k. 0≤k≤∣y∣−∣z∣∧substr(y,k,∣z∣)=z\exists k.\ 0 \le k \le |y| - |z| \wedge \text{substr}(y, k, |z|) = z. parse_constraints implements the simplest such macros:

  • positive occurrences: contains(w1,w2)(w_1, w_2) becomes w1=m1⋅w2⋅m2w_1 = m_1 \cdot w_2 \cdot m_2 (fresh m1,m2m_1, m_2), and prefixof and suffixof likewise;
  • negations with a constant needle become memberships: ¬\negcontains(x,"c")(x, \text{"c"}) is x∈∼(A∗⋅"c"⋅A∗)x \in \sim(A^* \cdot \text{"c"} \cdot A^*).

A negated contains with a variable needle would need a universal quantifier, so it is rejected.

Performance. The slides’ later sections show that cvc5 keeps the extended functions and simplifies them as knowledge accumulates: contains(l1,l2⋅t)→⊥(l_1, l_2 \cdot t) \to \bot when the constant l1l_1 does not contain l2l_2. It also uses length-based conditional rewrites, such as t=s⋅r⋅q→t=s⋅q∧r=ϵt = s \cdot r \cdot q \to t = s \cdot q \wedge r = \epsilon when ∣s∣+∣q∣≥∣t∣|s| + |q| \ge |t|. These notes do not implement those rewrites.

Run it: python3 04_regex_and_security.py (about half a second).


9. Applications

9.1 Does a sanitizer stop an injection?

A query is built as q = "SELECT * FROM t WHERE name='" · x · "'" from an input xx. Say the query is injected when:

  • the string literal (no quote inside: uu) ends at the first quote;
  • what follows (vv) starts a new OR condition.

q=prefix⋅u⋅"’"⋅v ∧ u∉A∗⋅"’"⋅A∗ ∧ v∈" "∗⋅(OR∣or∣Or∣oR)⋅" "⋅A∗q = \text{prefix}\cdot u\cdot\text{"'"}\cdot v \ \wedge\ u \notin A^*\cdot\text{"'"}\cdot A^* \ \wedge\ v \in \text{" "}^*\cdot(\text{OR} \mid \text{or} \mid \text{Or} \mid \text{oR})\cdot\text{" "}\cdot A^*

Script 04, Part 4 adds candidate sanitizers:

sanitizerinjection possible?input xx
noneSAT'or
blacklist ORSAT'or
blacklist OR, orSAT'oR
blacklist all four spellingsUNSAT—
no quote in xxUNSAT—
letters and digits onlyUNSAT—

Reading the answers.

  • SAT is a concrete input that gets through, checked on the constraints. Blacklisting spellings falls to the next spelling.
  • UNSAT is relative to this one pattern. Blocking the four spellings of OR leaves other injections open (AND, semicolons, …).
  • The robust fixes. Forbidding the quote, or allowing only letters and digits, removes any way out of the literal. Better still are parameterized queries, where the input never becomes part of the query text.

9.2 Comparing access patterns (in the style of Zelkova)

Access policies match resource names with wildcards: * is any word, ? is one character. “Does the new policy allow anything the old one did not?” is an inclusion question, and a failed check returns a concrete name (script 04, Part 5):

P1P2P1 allows more?a name only P1 allows
files/public/*files/*no—
files/*files/public/*yesfiles/
files/team-?/report-*.pdffiles/team-*/report-*no—
files/team-*/report-*files/team-?/report-*.pdfyesfiles/team-/report-
files/*/report-*files/*report*no—

Amazon’s Zelkova (Backes et al., FMCAD 2018, in the slides’ reading list) answers such questions for real access policies, by translating them to SMT with strings and regular expressions.

9.3 Symbolic execution

The slides open the strings deck with symbolic execution: program inputs become SMT variables, and each path becomes a constraint. Lecture 14 builds a symbolic executor; its path conditions are exactly the kind of formulas this lecture’s procedures decide.


10. Sequences (overview)

A sequence is a string whose elements come from an arbitrary sort (integers, records, other sequences) instead of a finite alphabet. The theory of sequences (as in cvc5; Sheng et al., IJCAR 2022) has:

  • concatenation and length, as for strings;
  • the array-like operations nth(s,i)(s, i) and update(s,i,v)(s, i, v).

What it models. It is the natural model of vectors and lists in programming languages. It was motivated by verifying the Move language’s vector operations.

How it is decided. Its solver combines the two halves of this lecture:

  • the concatenation and length reasoning of §7, without letter-level tricks (the alphabet is infinite);
  • array-style read-over-write reasoning for nth and update.

11. Summary

  1. Arrays are functions from indices to elements. Their axioms are read-over-write (twice) and extensionality. A quantifier-free formula only needs arrays that are “default everywhere except at finitely many places”.
  2. Eager: eliminate writes, add extensionality witnesses, and instantiate axiom (2) for every index term. The result is an EUF formula of quadratic size.
  3. Lazy: treat read and write as uninterpreted, add only the axiom instances a model violates, and repeat. It wins when most instances are useless (swaps over many indices) and loses when nearly all are needed (storecomm), at least with restarts.
  4. Every array answer is certified: models by array semantics, refutations by axiom-instance checks plus DRUP. The certificate caught a real bug.
  5. Strings: word equations are decidable but impractical, length makes decidability open, and replace-all makes it undecidable. Solvers use a rule-based calculus with an arithmetic solver for lengths and accept incompleteness.
  6. The calculus (L-INTRO, L-VALID, C-SPLIT, A-CONF, CONST-CONF) is sound but not terminating: x⋅a=b⋅xx \cdot a = b \cdot x splits forever. Letter counts refute it at once, but not every equation; iterative deepening guarantees that satisfiable inputs are solved.
  7. Derivatives decide membership, emptiness and inclusion of regular expressions, with witnesses, and make complement and intersection cheap. The slides’ inclusion rules are a fast, incomplete first try.
  8. String solving answers security questions: whether a sanitizer stops an injection, and whether a policy change opens up anything new.

12. Common pitfalls

  • Forgetting extensionality. Without axiom 3, write(a,i,a[i])≠a\text{write}(a, i, a[i]) \neq a is satisfiable. Decide which theory you mean.
  • Computing the index set too late. Indices hidden inside eliminated writes still count (§5).
  • Instantiating at index terms only up to syntax. i=ji = j may hold in the model even when ii and jj are different terms; the lemma i=j∨…i = j \vee \ldots lets the solver decide.
  • Expecting termination from string solvers. UNKNOWN is a normal answer; budgets are part of the design.
  • Reading UNSAT from a bounded search as a proof. “No solution up to length 4” is evidence, not a refutation.
  • Real arithmetic for lengths. 2ℓx=2ℓy+12\ell_x = 2\ell_y + 1 is satisfiable over R\mathbb{R}.
  • Depth-first search without fairness. One infinite branch can hide a solution in the next one.
  • Blacklists as sanitizers. The solver finds the spelling you forgot.

13. Exercises

1. (array semantics) In the model a=[1↦x,else δ]a = [1 \mapsto x, \text{else } \delta], evaluate a⟨2◃y⟩⟨1◃z⟩[2]a\langle 2 \triangleleft y\rangle\langle 1 \triangleleft z\rangle[2] and decide whether a⟨1◃x⟩=aa\langle 1 \triangleleft x\rangle = a.

Answer

The read. a⟨2◃y⟩=[1↦x,2↦y]a\langle 2 \triangleleft y\rangle = [1 \mapsto x, 2 \mapsto y], and then ⟨1◃z⟩\langle 1 \triangleleft z\rangle gives [1↦z,2↦y][1 \mapsto z, 2 \mapsto y]. Its value at 2 is yy.

The equality. a⟨1◃x⟩=[1↦x,else δ]=aa\langle 1 \triangleleft x\rangle = [1 \mapsto x, \text{else } \delta] = a: yes, as functions (extensionality).

2. (eager reduction) Reduce a⟨i◃e⟩[j]=e∧a[j]≠e∧i≠ja\langle i \triangleleft e\rangle[j] = e \wedge a[j] \neq e \wedge i \neq j to EUF by hand. How many instances, and why is it UNSAT?

Answer

The reduction. One write, A=a⟨i◃e⟩A = a\langle i \triangleleft e\rangle; the index set is {j,i}\{j, i\}. The instances are A[i]=eA[i] = e and i=j∨A[j]=a[j]i = j \vee A[j] = a[j]: two in all.

The refutation.

  • Since i≠ji \neq j, the second instance gives A[j]=a[j]A[j] = a[j].
  • The formula says A[j]=eA[j] = e, so a[j]=ea[j] = e.
  • That contradicts a[j]≠ea[j] \neq e.

3. (lazy lemmas) For the same formula, what does the first EUF model look like, and which lemma does violated_lemmas add?

Answer

The first model. In EUF, w[j]w[j] (with w=a⟨i◃e⟩w = a\langle i \triangleleft e\rangle) and a[j]a[j] are unrelated terms, so the model can have w[j]=e≠a[j]w[j] = e \neq a[j] with i≠ji \neq j.

The violated instance. The class map of ww has index class [j]≠[i][j] \neq [i], but MW[j]=eM_W[j] = e while MB[j]=a[j]M_B[j] = a[j]. Lemma: i=j∨w[j]=a[j]i = j \vee w[j] = a[j]. Possibly also w[i]=ew[i] = e, if ww‘s map lacks ii.

The next round. It is UNSAT.

4. (extensionality) Why is the witness lemma s=t∨s[k]≠t[k]s = t \vee s[k] \neq t[k] sound even though it is not valid for a fixed kk?

Answer

Not valid, but equisatisfiable. For a fixed index kk the lemma is not valid: two different arrays may agree at that kk. But kk is fresh, so the lemma is the Skolemization of s=t∨∃k. s[k]≠t[k]s = t \vee \exists k.\ s[k] \neq t[k], which is axiom 3.

Why that suffices. Adding a Skolemized consequence preserves satisfiability: any model can interpret kk as an index where ss and tt differ, if they differ at all.

The proviso. It holds only if kk occurs nowhere else, which is why lemma_kind checks that each witness serves one pair only.

5. (eager vs lazy) Explain from the tables of §4.5 why lazy wins on swaps over 6 indices and loses on storecomm.

Answer

Swaps over 6 indices. Most eager instances concern index pairs that never interact, yet each is a case split for the SAT solver: 408 conflicts at n=8n = 8. The lazy loop adds 225 of 753, and the time drops about 5×.

Storecomm. Nearly every instance is used by the proof. The lazy loop adds most of them anyway (305 of 545 at n=16n = 16), and it needs n+2n + 2 restarts to do so.

6. (word equations) Run the calculus by hand on x⋅"ab"="ba"⋅y∧∣x∣=1x \cdot \text{"ab"} = \text{"ba"} \cdot y \wedge |x| = 1.

Answer

L-VALID on xx vs b.

  • x=ϵx = \epsilon gives ab=ba⋅y\text{ab} = \text{ba} \cdot y: CONST-CONF (a ≠ b).
  • x=b⋅kx = b \cdot k gives, after stripping b, k⋅ab=a⋅yk \cdot ab = a \cdot y. With ∣x∣=1|x| = 1, A entails ∣k∣=0|k| = 0, so A-PROP sets k:=ϵk := \epsilon.

The rest. That leaves ab=a⋅yab = a \cdot y, so y=by = b (S-PROP). The model is x=bx = b, y=by = b, which is script 03’s answer.

7. (letter counts) Show that x⋅y⋅"a"="b"⋅y⋅xx \cdot y \cdot \text{"a"} = \text{"b"} \cdot y \cdot x is unsatisfiable using counts, and say whether lengths alone suffice.

Answer

Counts. The a-count gives ∣x∣a+∣y∣a+1=∣y∣a+∣x∣a|x|_a + |y|_a + 1 = |y|_a + |x|_a, i.e. 1=01 = 0: unsatisfiable.

Lengths alone. They give ∣x∣+∣y∣+1=1+∣y∣+∣x∣|x| + |y| + 1 = 1 + |y| + |x|, which always holds, so they do not suffice.

8. (derivatives) Compute ∂a\partial_a and ∂b\partial_b of R=(ab)∗R = (ab)^*. Is aba∈L(R)aba \in L(R)?

Answer

The derivatives. ∂a(ab)∗=(∂a ab)⋅(ab)∗=b⋅(ab)∗\partial_a (ab)^* = (\partial_a\, ab)\cdot(ab)^* = b\cdot(ab)^*, and ∂b(ab)∗=∅\partial_b (ab)^* = \emptyset.

Membership of aba.

  • After a: b(ab)∗b(ab)^*.
  • After b: ∂b(b(ab)∗)=(ab)∗\partial_b (b(ab)^*) = (ab)^*.
  • After a: b(ab)∗b(ab)^*, which is not nullable.

So aba∉L(R)aba \notin L(R).

9. (inclusion rules) Prove L("a"⋅[0..3]∗)⊆L(A∗)L(\text{"a"}\cdot[0..3]^*) \subseteq L(A^*) and L([1..2])⊆L([0..9]∗)L([1..2]) \subseteq L([0..9]^*) with the slides’ rules, or explain why the second needs a rule the list lacks.

Answer

The first. It follows by (4) directly: anything is in A∗A^*.

The second. (5) gives L([0..9])⊆L([0..9]∗)L([0..9]) \subseteq L([0..9]^*), and (9) gives L([1..2])⊆L([0..9])L([1..2]) \subseteq L([0..9]). Combining them needs (7), transitivity, which our search does not use. With transitivity: L([1..2])⊆L([0..9])⊆L([0..9]∗)L([1..2]) \subseteq L([0..9]) \subseteq L([0..9]^*). rule_proof fails here; included succeeds.

10. (programming) Add right-end splitting to StringSolver.branches: when the first equation’s last tokens differ, split there instead of (or before) the left end. Does "aa"·y·"b" = y·"b"·x become UNSAT with counts=True? What happens to the 100 random equations of script 03?

Answer sketch

The split. Mirror the two cases: a variable vs a character at the end gives x=ϵx = \epsilon | x=k⋅cx = k \cdot c; variable vs variable gives the three C-SPLIT branches with suffixes.

The example. The last tokens are b vs xx. With x=k⋅bx = k \cdot b, the b-count of xx becomes 1, contradicting ∣x∣b=0|x|_b = 0; with x=ϵx = \epsilon, the lengths fail. So it is UNSAT.

A strategy. Choose the end with a constant first. Expect no regressions and fewer states.

11. (programming) Implement online lemmas on demand: instead of restarting CDCL(T) each round, pre-register the candidate atoms (all i=ji = j and w[j]=b[j]w[j] = b[j] for writes and index terms) in the abstraction, and let a theory solver add violated instances as theory lemmas at final checks. Compare EUF conflicts and time with solve_lazy on script 02’s families.

Answer sketch

The atoms. Pre-registration makes the atom set finite and fixed, which Lecture 5’s solver needs. It is the same set the eager approach uses, but the clauses are not added until violated.

The theory solver. A subclass of EUF whose check(final=True) runs violated_lemmas on the current classes. It returns one violated instance as a conflict clause, which is valid in TAT_A and therefore a legal theory lemma.

What to expect. A single CDCL(T) run, with the lazy approach’s small lemma count and no restarts. On storecomm it should approach eager’s time.


14. Further reading

  • A. R. Bradley, Z. Manna, The Calculus of Computation, Chapter 11 (arrays; the array property fragment).
  • D. Kroening, O. Strichman, Decision Procedures, Chapter 7 (arrays).
  • A. Stump, C. W. Barrett, D. L. Dill, J. Levitt, “A Decision Procedure for an Extensional Theory of Arrays”, LICS 2001.
  • R. Brummayer, A. Biere, “Lemmas on Demand for the Extensional Theory of Arrays”, JSAT 2009.
  • L. de Moura, N. Bjørner, “Generalized, Efficient Array Decision Procedures”, FMCAD 2009.
  • J. Christ, J. Hoenicke, “Weakly Equivalent Arrays”, FroCoS 2015.
  • T. Liang, A. Reynolds, C. Tinelli, C. Barrett, M. Deters, “A DPLL(T) Theory Solver for a Theory of Strings and Regular Expressions”, CAV 2014 (the slides’ calculus), and the later papers in the slides’ list (FMSD 2016, CAV 2017, CAV 2019, CAV 2022).
  • J. A. Brzozowski, “Derivatives of Regular Expressions”, JACM 1964; S. Owens, J. Reppy, A. Turon, “Regular-expression derivatives re-examined”, JFP 2009.
  • W. Plandowski, “Satisfiability of Word Equations with Constants is in PSPACE”, JACM 2004; A. Jeż, “Recompression: a simple and powerful technique for word equations”, JACM 2016.
  • A. W. Lin, P. Barceló, “String Solving with Word Equations and Transducers: Towards a Logic for Analysing Mutation XSS”, POPL 2016.
  • J. Backes et al., “Semantic-based Automated Reasoning for AWS Access Policies using SMT”, FMCAD 2018.
  • Y. Sheng et al., “Reasoning About Vectors Using an SMT Theory of Sequences”, IJCAR 2022.

15. Next lecture

Lecture 14: symbolic execution (Rachel Cleaveland).

  • How a program becomes formulas. Run a program on symbolic inputs; each path gives a path condition, and the solver turns feasible path conditions into concrete test inputs.
  • Path explosion and concolic testing. Many paths, and how mixing concrete and symbolic execution (DART, KLEE) gets past what the solver cannot handle.

See Lecture 14 (notes only, no code module).

← 返回课程目录