Skip to main content

Week 7 Studio: Building the backtracking search

Literal, concatenation, and alternation as pattern values, matched against a subject with an explicit backtracking stack — or OCaml's own call stack, your choice

5 April 2027

Before the session

Bring Movement II’s complete statement executor: labeled statements, the goto field, and DEFINEd functions with call frames. This session starts a new movement and a new value type rather than extending anything from Movement I or II directly — a pattern doesn’t need statements or function calls to exist, only a subject string and a cursor.

In the session

Sample transcript

Genuine captured output from this course’s own week-7 reference matcher, run against an alternation pattern (once plain, once with a --trace flag added for this session) and a DEFINEd predicate function built from alternation:

$ dune exec bin/main.exe -- examples/alt.sno
MATCHED

$ dune exec bin/main.exe -- examples/alt.sno --trace
matching against "ZZBA"
  trying match at position 0
    at 0: trying first alternative, recording second on the history list
    "A" fails at 0
    backtrack: resuming recorded alternative at 0
    "B" fails at 0
  trying match at position 1
    at 1: trying first alternative, recording second on the history list
    "A" fails at 1
    backtrack: resuming recorded alternative at 1
    "B" fails at 1
  trying match at position 2
    at 2: trying first alternative, recording second on the history list
    "A" fails at 2
    backtrack: resuming recorded alternative at 2
    "B" matches at 2
    matched, cursor now at 3
MATCHED

$ dune exec bin/main.exe -- examples/define_match.sno
NO
YES

The trace is worth reading line by line: at every position from 0 to 2, "A" is tried and fails before "B" is ever tried — even though 'B' occurs earlier in the subject "ZZBA" than 'A' does — which is exactly the “both alternatives tried at this position before the cursor advances” behaviour this week’s spec asks you to demonstrate, not a coincidence of this particular subject.

This week’s real starter — the pattern type in place, with the matcher’s backtracking left as the gap to fill in — is downloadable at week-07-starter.zip.

Afterwards

You now have a pattern value, a cursor that behaves correctly under the three-condition contract, and a working backtracking search over concatenation and alternation. Next week adds the primitives that make patterns actually useful against real text — ANY, NOTANY, SPAN, BREAK, ARB, ARBNO, BAL — on top of exactly this search, with no change to how backtracking itself works.

The spec

What you bring to the session. Written so a reader can tell whether it has been met without asking you.

Teaching team