flang the compiler proves your program cannot hang 0.7.22 GitHub Русский

Write-ahead log

docs/examples/wal/ holds two flang programs: parsing, printing and recovery of a write-ahead log after truncation, and a plan that appends one record to the log through input-output orders. Both are written entirely in flang; neither writes to disk — the host that carries out the plan's orders does.

What is in the directory

There are 2 .flang files: | file | what it is | lines | |---|---|---:| | docs/examples/wal/write-ahead-log.flang | module «Write ahead log»: the record format, character-by-character parsing, printing, recovery up to the last whole record, the next number | 579 | | docs/examples/wal/append-plan.flang | module «Append plan»: the plan «Дописать в журнал» — read the file, append a record, confirm with its number. Uses the first module | 95 |

The two files together carry 110 examples ; they are declared inside the functions and run with bootstrap/flang test.

Record format

#<номер>:<длина>:<тело>;

A record is printed by «Напечатать запись». The log is read one character at a time by «Шаг чтения» — a state machine with the states «Между записями», «Читаем номер», «Читаем длину», «Читаем тело», «Ждём метку конца» and «Сбились». There is no way out of «Сбились»: everything after the first damaged frame is discarded; the records read before it are kept.

The length comes before the body because the body may contain the same characters as the frame: a reader driven by separators goes wrong on such a body, a reader driven by the length does not. The end mark ; is still needed: without it a record with an understated length would be read as whole, and the tail of its body as the start of the next record.

How to run

bootstrap/flang check docs/examples/wal/write-ahead-log.flang --proof
bootstrap/flang check docs/examples/wal/append-plan.flang --proof
bootstrap/flang test  docs/examples/wal/write-ahead-log.flang
bootstrap/flang test  docs/examples/wal/append-plan.flang

The first line of check prints the number of functions and the number with a proved termination; --proof adds a report on every assertion; test prints the number of examples and how many passed.

The plan runs with bootstrap/flang io. The plan resolves the path to the file журнал.wal relative to the directory of the program, and the file must exist: on a missing file the plan answers «Провал» with the code FLANG_IO_READ (exit code 1). To avoid writing into the tree, copy both files to a separate directory:

mkdir -p /tmp/wal && cp docs/examples/wal/*.flang /tmp/wal/ && : > /tmp/wal/журнал.wal
bootstrap/flang io /tmp/wal/append-plan.flang

Every run prints, as JSON, two orders — «Прочитать файл» and «Записать файл» — with the host's replies, and appends one record with the body «выдача товара» and the next number. A record with a damaged frame at the end of the file (for instance, without its end mark) is dropped on the next run: the file is rewritten as the whole records plus the new one.

What is checked and what is proved

The main statement about reading: a log truncated at any character is read up to the last whole record and no further. It is written as four postconditions:

postconditionfunction--proof verdict
«съедено не больше, чем подано»«Прочитать журнал»grid
«съеденное — это ровно печать прочитанного, знак в знак»«Прочитать журнал»grid
«съеденное и остаток дают вход, ни знаком больше и ни знаком меньше»«Прочитать журнал»grid
«в остатке целой записи с начала нет»«Остаток пуст для читателя»grid

"Grid" means: the assertion is checked on the function's examples, and there is no proof of it for all inputs — the report says so in plain words. The kernel does not take these four because they relate the result of a fold over the whole input string to the string itself (the print equals a prefix of the input); the kernel has no rule of that shape. What the kernel takes and what it does not is named on which promises the kernel takes.

Besides these four, three small ones are on the grid too: «цифра не больше девяти» («Цифра числом»), «печать не короче шести знаков» («Напечатать запись») and «разрез не теряет и не добавляет ни знака» («Разрез сходится») — run of check --proof on 11 September 2026 at commit 2c40752d0: 17 claims, 10 proved, 7 on the grid.

What the kernel proved for all inputs:

There are no assertions "declared, not proved" and none taken on faith: the report prints zeros for both. How many assertions there are, how many are proved and how many are on the grid is not given on this page — the last line of the --proof report prints those numbers, and they change together with the kernel.

Confirmation of the write in the plan: the variant «Конец работы» is built in one place of the module «Append plan» — the «Записано» branch of «После записи журнала». Any other host reply gives «Провал»: «Сбой» with its own code, anything else with the code FLANG_IO_ORDER. This is visible by reading the module and its examples.

What the program does not guarantee

Nearby