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

Back to README · Documentation index

How to write flang

What does good flang code look like? Below are six habits the standard library is written by. All of them are shown on one function. Save it as price.flang (неотрицательное is the non-negative integer type, требует is requires, обеспечивает is ensures, пример is a test):

модуль «Цены»

тотальная функция «Цена после скидки»
  принимает цена: неотрицательное, скидка: неотрицательное
  возвращает число
  требует «скидка не больше цены» скидка не больше цена
  обеспечивает «цена не уходит в минус» результат не меньше 0
  пример «сто минус двадцать»
    дано цена равно 100
    дано скидка равно 20
    ожидается 80
  цена минус скидка
$ flang check price.flang
модуль «Цены»: функций 1, из них с доказанным завершением 1; типов 0
price.flang: проверено — разбор, типы, завершаемость, ядро и примеры; замечаний нет

The second line lists the checks that passed: syntax, types, termination, proofs and tests. «замечаний нет» means "no errors".

1. The name says what the function does

Four things explain a function: its name, its types, обеспечивает and пример. If they do not show what it does, fix them. An explanation beside the code will not help.

2. A postcondition and a test instead of a comment

The compiler skips // and /* */ comments. Nothing checks them, so they drift away from the code unnoticed. What other languages put in a comment is written here as a line that is checked:

What you want to sayWhere it goes
"the result is never negative"обеспечивает «цена не уходит в минус» результат не меньше 0
"the discount must not exceed the price"требует «скидка не больше цены» скидка не больше цена
"100 and 20 give 80"пример «сто минус двадцать»

обеспечивает is a postcondition, требует is a precondition, пример is a test. The name of each is a whole sentence: it shows up in reports and in error messages.

3. The narrowest type

Declare неотрицательное wherever a value is never negative. The type gives the compiler bounds. The postcondition of the function above is proved from the types and the требует line alone:

$ flang check price.flang --proof
…
  постусловие «цена не уходит в минус» функции «Цена после скидки» — доказано по объявленным типам аргументов: цель сведена правилом «неотрицательность по построению» — утверждение обо ВСЕХ входах, а не о написанных; теоремы при нём нет и не нужно
…

The line says: proved from the declared argument types, for all inputs. «теорема» is a proof written by hand; none was needed here.

Remove the требует line, and flang check finds arguments on which the postcondition is false. The exit code is 1:

FLANG_EXAMPLE: FLANG_PROPERTY: постусловие «цена не уходит в минус» функции «Цена после скидки» нарушено на входе цена = 0, скидка = 1 — посчитано вычислителем на этом входе

4. тотальная the cheapest way

The word тотальная before a function demands that it terminates on any arguments, and the compiler proves it. The ways, from cheap to costly:

  1. no recursion, a свёртка (fold) or a цикл (loop) over a list — nothing to prove;
  2. recursion on a part of the value (the tail of a list, a field of a variant) — proved from the shape of the data;
  3. counting down on a неотрицательное parameter by a constant step — proved, no run-time check;
  4. an убывает … line — a check stays in the program and costs time. Use it when the first three are not enough.

Details: How the compiler proves that a function terminates.

5. Actions as data

A function does not read a file or touch the network. It returns an effect — a value such as "read this file". The command flang io carries it out. The logic therefore always terminates and is checked by ordinary tests, with no files and no network.

How it works: The boundary between flang and the host.

6. Check before you run

flang check price.flang          # syntax, types, termination, tests
flang check price.flang --proof  # what is proved and what is not
flang run price.flang --function "Цена после скидки" --args '{"цена": 100, "скидка": 20}'

flang check without options answers «замечаний нет» even when a postcondition is checked on tests only. What is proved is shown by --proof. Its exit code 3 says that some postconditions are left without a proof.

File names

A source file has four equal extensions: .flang, .fp, .фп, .фланг. A Russian file name is written in Cyrillic (налог.фп), an English one in Latin letters (tax.flang). A Russian word in Latin letters (nalog.flang) is not written: neither a Russian reader nor a foreigner can read it.

Next