Your first program
How do you write a function in flang, check it and run it? Four steps: a file, a check, the tests, a run. You need flang installed — how to install it.
Write
File hello.flang:
module «Hello»
total function «Twice»
accepts n: nat
returns number
ensures «the doubled value is at least the original» result is at least n
example «twice two»
given n equals 2
expected 4
n plus n
What each part does:
| Part | Familiar name | What the compiler does with it |
|---|---|---|
total | a function that always terminates | proves termination; if it cannot, the file fails the check |
accepts / returns | parameter and result types | checks them before anything runs |
ensures | postcondition | tries to prove it for every input; the name in «…» is required, messages refer to the postcondition by it |
example | a unit test inside the function | runs it on every check |
| last line | the function body | — |
Names of modules, functions and tests are written in guillemets «…». The quotes " and ' do not work for names: guillemets are how the compiler tells your name from a keyword.
Check
flang check hello.flang
модуль «Hello»: функций 1, из них с доказанным завершением 1; типов 0
hello.flang: проверено — доказано 1, проверено только на тестах 0, не доказано 0 (постусловий и утверждений 1); замечаний нет
Exit code 0. The compiler writes its messages in Russian with either keyword spelling. The first line reads "module «Hello»: 1 function, 1 of them proved to terminate; 0 types". The second reads "checked — proved 1, tested only 0, not proved 0 (1 postcondition); no errors".
Why the parameter has the type nat
Replace the parameter type with number in hello.flang and check again:
flang check hello.flang
The compiler answers:
модуль «Hello»: функций 1, из них с доказанным завершением 1; типов 0
FLANG_BOUND_ON_NAN в файле hello.flang, строка 6, столбец 3: постусловие
«the doubled value is at least the original» функции «Twice» ЛОЖНО, и контрпример назван:
«n» объявлен типом «число», а «не число» живёт в этом типе и стоит ВНЕ ПОРЯДКА
…
hello.flang: не проверено — замечаний 1
Exit code 1. The message reads: line 6, column 3, the postcondition is false, and the counterexample is NaN. The type number is a floating-point number and includes NaN. NaN is neither greater nor less than any number, so the postcondition fails for it. The type nat is an integer from 0 to 2⁵³−1 and has no NaN. Put nat back.
Run the tests
flang test hello.flang
hello.flang: примеров 1, прошло 1, не прошло 0
Exit code 0. The line reads "1 test, 1 passed, 0 failed".
Run a function
flang run hello.flang --function Twice --args '{"n": 21}'
доказано: утверждений 1
42
Exit code 0. The first line reads "proved: 1 postcondition". The second is the result. When a file has a postcondition without a proof, run computes nothing.
On the command line the function name is written without guillemets. --args takes a JSON object whose keys are the parameter names from the accepts line. Each argument is checked against the declared type before the computation:
flang run hello.flang --function Twice --args '{"n": -3}'
доказано: утверждений 1
FLANG_TYPE: вызов функции «Twice»: аргумент «n»: -3 вне неотрицательное
Exit code 1. The second line reads "argument «n»: -3 is outside nat". The function was not computed.
See what is proved
The --proof flag adds a proof report to the check:
flang check hello.flang --proof
модуль «Hello»: функций 1, завершение доказано у 1
постусловие «the doubled value is at least the original» функции «Twice» — доказано
итог: постусловий и утверждений 1 — доказано 1, проверено только на тестах 0, не доказано 0
…
hello.flang: проверено — доказано 1, проверено только на тестах 0, не доказано 0, ложно 0 (постусловий и утверждений 1); независимая проверка повторила 0 из 1 — код возврата 0
Exit code 0. The report names each postcondition and the answer about it. The second line reads "postcondition «…» of function «Twice» — proved": it holds for all inputs, and the test with 2 is one case. The line "итог" and the last line count the postconditions of the file: proved 1, tested only 0, not proved 0, false 0. "Независимая проверка" is the independent check, a separate program that repeats proofs; it did not repeat this one ("0 из 1"), and that does not change the exit code. The playground in the browser cannot start it and prints two more lines about that; the page leaves them out with "…".
What to do when the compiler does not prove a postcondition by itself is shown in Your first proof.
Code in another language
The command flang emit hello.flang --target c --out ./out generates a C project from the program; make -C ./out builds it. There are ten target languages: c, cpp, csharp, elixir, go, java, js, python, rust, ts. Step by step: Generating C and JavaScript.
Next
- Your first proof — the compiler finds a bug, you fix it and write a proof
- Language tour — the language in order, six pages with exercises
- Which construct to use when — what to write instead of enum, Optional, Result, map, filter, reduce