flang the compiler proves your program cannot hang 0.9.0 GitHub Try in the browserTry it Русский

What a proof is in flang

This page answers the question: what does the word «доказано» (proved) in the compiler output mean, and when do you need it. Five minutes to read. To try the same by hand step by step, see the lesson First proof.

A test and a proof

A test checks the inputs you thought of. A proof answers for all inputs at once. In flang both are written inside the function:

LineWhat it isIn other languages
пример (example)a test with concrete argumentsa unit test
требует (requires)a precondition: what the caller must ensurerequires in Dafny
обеспечивает (ensures)a postcondition: what holds for the resultensures in Dafny

File docs/examples/proofs/discount.flang:

модуль «Discount»

тотальная функция «Цена со скидкой»
  принимает цена: неотрицательное, скидка: неотрицательное
  требует «скидка не больше цены» скидка не больше цена
  возвращает число
  обеспечивает «в минус не уходим» результат не меньше 0
  пример «сто минус десять»
    дано цена равно 100
    дано скидка равно 10
    ожидается 90
  цена минус скидка
flang check discount.flang --proof
модуль «Discount»: функций 1, завершение доказано у 1
постусловие «в минус не уходим» функции «Цена со скидкой» — доказано
итог: постусловий и утверждений 1 — доказано 1, проверено только на тестах 0, не доказано 0
discount.flang: проверено — доказано 1, … независимая проверка повторила 1 из 1 — код возврата 0

The compiler writes its messages in Russian. Line one: the module has one function, and it always terminates. Line two is the answer to the postcondition: «доказано» means "proved". Lines three and four are the counters for the file (proved 1, tested only 0, not proved 0) and the exit code 0.

The test speaks about one input: 100 and 10. The word «доказано» speaks about any price and any discount not above the price. The compiler found the proof. All it needed from you was the argument types and the требует line.

The part of the compiler that looks for proofs is called the proof kernel, «the kernel» from here on. It works by a closed list of rules. The kernel has 20 rules; a new rule appears only with a new compiler version.

The words «независимая проверка повторила 1 из 1» mean: a separate program, checker, checked this proof again and agreed with the kernel.

Four answers of the compiler

For every postcondition the compiler gives one of four answers:

In the outputWhat it meansWhat to do
доказаноproved: true for all inputs of the declared typesnothing
проверено только на тестах (примеров N)tested only: passed on N tests, no proofprove it or keep it as a check
не доказаноnot proved: neither a proof nor teststhe same
FLANG_PROPERTY … нарушено на входеan input was found on which the postcondition is falsefix the code or the postcondition

The last answer is an error: the file does not pass the check, exit code 1. How the compiler finds such an input and how a precondition fixes it: sections 1 and 2 of the lesson First proof.

A precondition is checked at compile time at every call site. At run time it is checked once, where data comes in from outside. An unproved postcondition is checked on every return from the function. flang run starts a program with such a postcondition only with the key --trust; without it the exit code is 3.

A proved postcondition does not get into the generated code if the function has at least one пример. For a function without tests the check stays even for a proved postcondition.

When you need a proof

Write a postcondition where a mistake is expensive and the condition is short:

Make do with a test where the property is long or the kernel does not derive it: the order after sorting, properties of floating-point numbers, work with the outside world.

What «доказано» does not mean

A proof ties the code to the postcondition. Whether the postcondition describes the task correctly is for you to decide: a function that always returns 0 also passes the check «the result is at least 0».

Behind the word «доказано» stand the kernel, the compiler and the C compiler. A second, independent program repeats part of the proofs. Which part, and where the check has a known error: What you are trusting.

flang does not replace Lean or Coq. It proves pre- and postconditions of programs; arbitrary theorems are out of its reach. The comparison: flang for Lean and Dafny users.

Next