Back to README · Documentation index
Known limits
What flang does not do and does not guarantee, and what to write instead. This page is a reference: find your case by the headings.
What flang does not guarantee
The compiler is not proved as a whole
CompCert is a C compiler with a proof that every program it outputs behaves like its source. flang has no such proof. The proofs of flang are about the flang program, and the code generator can be wrong.
In exchange, each generated C program is checked. The command translation:validate compares it with a reference, a description of the meaning of the language in Lean, and gives a verdict for every function:
| Verdict | What it means |
|---|---|
| equal on every input | proved: by trying all argument values, or by comparing two decision trees |
| equal on samples | the answers agree on chosen inputs; nothing is known about the rest |
| outside the check | the function uses something the reference does not describe yet |
The reference does not describe machine words yet. Every function of the example docs/examples/reference/ru/machine-words.flang gets the third verdict:
$ flang run-script translation:validate -- --path docs/examples/reference/ru/machine-words.flang
СООТВЕТСТВИЕ ПРОВЕРЕНО для 47 функций, вне валидации 7
…
причины вне валидации: вид типа u8 — 1; вид типа u32 — 6
…
расхождений 0
The first line: 47 functions are equal on every input, 7 are outside the check («вне валидации»). The 47 are the functions of eight service files that the command always checks together with yours. The seven are all the functions of the example. The line about reasons names the argument type: u8 for one function and u32 for six. The last line: no differences between the C and the reference were found.
The command runs from a clone of the repository and needs Lean installed. How many functions of the examples it proves, and how it works, is in the description of translation validation for C. The other nine target languages have no such check.
What is taken without proof
The verdict "equal on every input" is about the text of a C function. Below it are parts that nobody proved:
| Part | What is in it |
|---|---|
| the C runtime | memory, strings, lists, the process scheduler |
| the C compiler | the cc of your system |
| libc and threads | malloc, pthread_create, fopen and others |
| the Lean reference | it is written by hand and can differ from the documentation |
total does not mean "no errors"
The keyword тотальная (total) promises that the computation ends. Since version 0.9.0 it also promises that no builtin in the function body stops with a run-time error. If a builtin can fail — divided by with a divisor nothing is known about, substring past the end of a string, head of an empty list — flang check rejects the function with the error FLANG_NOT_TOTAL and exit code 1. File docs/examples/reference/en/word-division-total.flang:
module «Word division total»
total function «Packets in the buffer»
accepts buffer: u64, packet: u64
returns u64
example «two packets of 1500 fit in 4096 bytes»
given buffer equals u64 4096
given packet equals u64 1500
expected u64 2
buffer divided by packet
$ flang check docs/examples/reference/en/word-division-total.flang
модуль «Word division total»: функций 1, из них с доказанным завершением 1; типов 0
FLANG_NOT_TOTAL в файле docs/examples/reference/en/word-division-total.flang, строка 10, столбец 10: функция «Packets in the buffer» объявлена тотальной, а встроенная операция «делить на» может отказать при работе: делитель может оказаться нулём; добавьте «требует» с условием на аргументы, проверку перед вызовом или снимите пометку «тотальная»
docs/examples/reference/en/word-division-total.flang: не проверено — замечаний 1
$ echo $?
1
A function without the total mark may fail: the check accepts it, and the error happens at run time. File docs/examples/reference/en/word-division.flang:
module «Word division»
function «Packets in the buffer»
accepts buffer: u64, packet: u64
returns u64
example «two packets of 1500 fit in 4096 bytes»
given buffer equals u64 4096
given packet equals u64 1500
expected u64 2
buffer divided by packet
total function «Packets in the buffer or zero»
accepts buffer: u64, packet: u64
returns u64
example «a packet of zero length gives zero packets»
given buffer equals u64 4096
given packet equals u64 0
expected u64 0
if packet equals 0
then u64 0
else buffer divided by packet
$ flang check docs/examples/reference/en/word-division.flang
модуль «Word division»: функций 2, из них с доказанным завершением 1; типов 0
без доказанного завершения: «Packets in the buffer»
docs/examples/reference/en/word-division.flang: проверено — доказано 0, проверено только на тестах 0, не доказано 0 (постусловий и утверждений 0); замечаний нет
A run of the first function with a divisor of 0:
$ flang run docs/examples/reference/en/word-division.flang --function "Packets in the buffer" --args '{"buffer":4096,"packet":0}'
доказано: утверждений 0
FLANG_OVERFLOW: 4096 «div» 0: деление на ноль
$ echo $?
1
The line with FLANG_OVERFLOW is the division-by-zero error (div is the internal name of divided by), exit code 1. The second function is total: the division stands in an if branch after the divisor is checked, and the check sees it. substring, element, head and the other builtins with a condition are closed the same way: with an if branch, a requires precondition or a match pattern.
One exception is left. An addition, subtraction or multiplication of machine words that does not fit the type is not caught by the check in a total function: it stops at run time with the error FLANG_OVERFLOW. If the result can leave the type, write with overflow or check the operands before the operation.
flang check does not tell whether postconditions are proved
flang check checks syntax, types, termination and examples. It does not complain about a postcondition (обеспечивает, ensures) that is not proved, and it returns exit code 0. flang check --proof tells: it writes the proof report and returns exit code 3 if at least one postcondition is not proved. flang run does not start such a program and returns code 3 unless you pass --trust.
A whole operating system cannot be written in flang
| The language has no | What follows |
|---|---|
| pointers and addresses | you cannot touch a device register or a page table |
| interrupts and waiting | the handler behind a vector and the waiting loop are written in C |
| inline assembly | the context switch is written in C or assembly |
| writes in place | a buffer cannot be changed, only a new one built |
The deciding part can be written in flang: parsing headers, permission checks, scheduling policy, a driver's state machine. These are pure functions, and their postconditions are proved. How such a function is joined with C code: The boundary between flang and the host.
The generated C needs libc and threads
A C program generated from a module with no effects at all refers to 64 external symbols: malloc, pthread_create, fopen, fork and others. The number was measured on the example machine-words.flang: flang emit --target c --no-cli, then cc -O2 -c (gcc 15.2) and nm -u, not counting the runtime's own symbols. Such code does not link into firmware without an operating system. For that there is the --freestanding flag: on the same example flang emit --target c --no-cli --freestanding generates three files that build with -ffreestanding -nostdlib and refer to no external symbol.
How to read the proof report
flang check --proof gives every postcondition one of three results, one line per postcondition:
$ flang check docs/examples/reference/en/machine-words.flang --proof
модуль «Machine words»: функций 7, завершение доказано у 7
постусловие «at most sixty bytes» функции «Header length in bytes» — проверено только на тестах (примеров 1)
постусловие «fits in a byte» функции «Byte as a number» — доказано
итог: постусловий и утверждений 2 — доказано 1, проверено только на тестах 1, не доказано 0
docs/examples/reference/en/machine-words.flang: доказано не всё — доказано 1, проверено только на тестах 1, не доказано 0, ложно 0 (постусловий и утверждений 2); независимая проверка повторила 0 из 1 — код возврата 3
подробный отчёт: flang check docs/examples/reference/en/machine-words.flang --proof --explain
| The report says | What it means |
|---|---|
доказано (proved) | true for all inputs; a separate program, checker, can re-check the proof |
проверено только на тестах (примеров N) (tested only, N examples) | tested only on N examples, like unit tests; nothing is known about other inputs |
не доказано (not proved) | written down, but there is neither a proof nor an example |
The example has two postconditions: one is proved, one is tested on one example. So the last line begins with «доказано не всё» (not everything is proved), and the exit code is 3. The detailed report under --explain calls the second result сетка (grid) and the third объявлено, не доказано (declared, not proved).
Types and exhaustive разбор (pattern matching, match) are always checked. Termination of a function with the keyword тотальная is proved, except when the decrease named by an убывает line is accepted without proof; the first line of the report counts such functions separately.
The program checker does not re-check every proof. How many proofs it repeated is said by the words «независимая проверка повторила 0 из 1» (the independent check repeated 0 of 1) in the last line.
What you cannot prove
| Does not work | What to do instead |
|---|---|
| "there exists x such that …" when the value of x is not written in the postcondition itself | name the value: есть такой м, а именно н, что …; a wrong value gives the error FLANG_PROOF_STEP |
| a postcondition about the result of a mask, a shift or an addition of machine words | test it with examples; about a word only the range of its type is known |
an order from the иначе (else) branch over число: не (х меньше 0) does not give х не меньше 0, because for NaN both comparisons are false | declare the argument целое if it is whole; for число write a теорема with steps |
| a property of state over time, of effects or of concurrency | test it: пример for functions, прогон scenarios for processes |
| a property of the generated code in Go, Rust and the other targets except C | test the generated program on its own |
| a postcondition the compiler did not prove by itself | write a теорема or rephrase it: What is proved automatically |
No external SMT solver is connected. flang is not fit for medicine, aviation or space: certification is a process, and the language does not have one.
The language
There is no writing into a list by index. You can read an element: элемент N в список. Values are immutable, so a list with one element replaced is a new list. Algorithms built on an updatable table do not carry over directly. For a dictionary use a library one: flang/stdlib/dictionary.flang (a list of pairs), flang/stdlib/tree.flang (a search tree) or flang/stdlib/hashmap.flang.
Only machine words have bitwise operations. число has none. Declare the value as u8 … u64 and write побитовое и, сдвиг вправо на: Machine words.
There are no hexadecimal literals. 0xFF gives the error FLANG_PARSE. Write a decimal number: 255.
There is no byte array type and no fixed-size array. Use список u8 (list u8) and a postcondition about the length.
There is no division on точное целое (exact integer). It has плюс, минус, умножить на and остаток от. For fields up to 64 bits use u64.
Counting up needs a measure. A recursive call on н плюс 1 without an убывает … line gives the error FLANG_NOT_TOTAL: "the recursive call does not decrease". Write убывает предел минус н over two parameters of type неотрицательное, and the kernel proves the descent (Counting up toward a bound). Over число the measure is accepted without a proof. Counting down needs no measure (docs/examples/measure/natural.flang).
The compiler does not find a measure with a changing step. It finds recursion on a part of a value (the tail of a list, a subtree) and a decrease by a constant step. For the rest write a line убывает <expression>: binary search uses убывает верх минус низ плюс 1 (docs/examples/measure/). The generated code then checks the measure at run time and stops with FLANG_MEASURE.
A variant named like a keyword is not matched. случай Да is read as the literal да, and the error FLANG_TYPE points at the pattern. Write случай вариант «Да», or rename the variant.
There is no I/O monad. A function returns an effect and the next step as an ordinary value.
flang emit does not run the examples. flang check and flang test run them. Run flang test <file> before generating code.
Not every compiler message is translated into English. With language = en in .flangrc the summary of flang check is in English, and some errors stay Russian. The FLANG_* code is the same in every language.
Effects
Effects are available through план only. A function does not read a file. It returns a description of the action, flang io performs it and calls the function again with the reply. The list of effects is closed: files, directories, network, processes, screen, time, random numbers, environment variables, command-line arguments. The full table: The boundary between flang and the host.
Text is read as UTF-8 only. Invalid UTF-8 gives the error FLANG_IO_NOT_TEXT. Read binary files as bytes.
https is TLS 1.3 with one cipher suite. The client is built into flang; curl is not needed. It knows TLS_AES_128_GCM_SHA256, x25519, rsa_pss_rsae_sha256 and an sha256WithRSAEncryption chain. A server without them gives the error FLANG_IO_TLS. Certificate revocation is not checked. A response is limited to 1,048,576 bytes. Datagrams are IPv4 only.
A program with план is not generated into every language. c, go, js, python, rust and ts work. The targets cpp, csharp, elixir and java give the error FLANG_PLAN_UNSUPPORTED and write no files.
A generated program does not perform every effect. In c, go, python and rust a plan is started with flang_cli --plan <name>. It performs the effects for files, directories, environment variables and arguments. For network, processes, time, random numbers and the screen it answers with the error FLANG_IO_NOT_WIRED or FLANG_IO_NO_SCREEN. Run such a plan with flang io.
Category declarations
The compiler tests the laws of категория and изоморфизм on values from the examples and does not prove them. The laws of a моноид over число and строка are only tested too, and flang check --proof returns exit code 3. The laws of a монада and the laws of отобразить over a list are proved. Details: Categories and functors.
Processes
| Does not work | What to do instead |
|---|---|
processes in cpp, csharp, go, java, python, rust: flang emit returns exit code 1 | generate into c, elixir, js or ts |
породить (spawn) in Elixir and JavaScript | use the c target |
| a bounded mailbox in Elixir | use c, js, ts, or an unbounded mailbox |
| sending to a spawned process by a name computed at run time | give it its work in the first message, and reply to a process with a declared name |
| running on several machines | run on one node |
proving freedom from deadlock: прогон scenarios try a finite number of message orders | design the protocol so that no process waits on another in a cycle |
The scheduler in the generated C has two modes. A single thread orders messages by the number seed: the same seed gives the same delivery log. A pool of worker threads (the field workers) is faster only on a program with parallel work.