An application in the browser
"Hailstones" is an example of an application in a browser tab, written in flang. This page shows how to build and run it and how a flang program works with the tab.
The application counts by the Collatz rule: an even number is halved, an odd one is multiplied by three and one is added. You type the number with the digit buttons. «пуск» starts the count, «шаг» takes one step, «бег» keeps counting until it is stopped.
A browser does not run flang. The program is first generated into JavaScript by flang emit. The sources are in the repository, in the directory docs/examples/web/browser-app/.
Run it
From the root of a clone of the repository:
sh docs/examples/web/build.sh
flang io docs/examples/web/stand.flang --max-orders 100000 --trust
# open http://127.0.0.1:8908/docs/examples/web/browser-app/index.html
The first command generates hailstone.js from hailstone.flang next to the page (inside it calls flang emit --target js --no-cli). The second starts the stand, a flang program that serves files over HTTP on port 8908. Neither Node nor npm is needed.
--trust is needed because some of the stand's postconditions are only tested. Without it flang io first checks the proofs, which takes about 45 seconds, and then refuses to run the program with exit code 3. --max-orders raises the limit on the number of effects; the default is 10,000.
What was checked for this page. Both commands run. The stand starts answering 10–15 seconds after start. It serves index.html and hailstone.js with status 200 and answers 404 for a file that does not exist. The 176 KB host file takes the stand about 43 seconds to serve, so the first page load is slow. The application was not opened in a browser.
What it is made of
| what | where |
|---|---|
| the application: state, steps, screen | docs/examples/web/browser-app/hailstone.flang |
| the page markup; it has no JavaScript code of its own | docs/examples/web/browser-app/index.html |
| the tab host, shared by every page | flang_host_browser.js from the compiler sources |
| the stand that serves the page | docs/examples/web/stand.flang |
The host here is a JavaScript program that carries out in the tab what the flang program asks for. More on this split: The boundary between flang and the host.
The application has 16 functions and 31 examples:
$ flang check docs/examples/web/browser-app/hailstone.flang
модуль «Hailstone»: функций 16, из них с доказанным завершением 16; типов 7
docs/examples/web/browser-app/hailstone.flang: проверено — доказано 17, проверено только на тестах 0, не доказано 0 (постусловий и утверждений 17); замечаний нет
$ flang test docs/examples/web/browser-app/hailstone.flang
docs/examples/web/browser-app/hailstone.flang: примеров 31, прошло 31, не прошло 0
The first line: all 16 functions are proved to terminate. The second: all 17 postconditions are proved. The third: all 31 examples pass.
The page attaches the host with one tag and names the application module for it:
<script type="module" src="../../../../flang/src/emit/js/flang_host_browser.js"
данные-модуль="./hailstone.js"></script>
How the program works with the tab
The program is declared as a план (plan): a state type, a starting function and a function that handles the host's responses.
план «Градины»
состояние «Полёт»
начинает с «Начать»
обрабатывает «Дальше»
The function «Дальше» prints nothing and waits for nothing itself. It receives the host's response and returns the next effect, a description of an action for the host to carry out:
| host response | what the program returns |
|---|---|
Пока ничего (nothing yet), Показано (shown) | show the screen, then wait for an event |
Случилось (happened) with fields откуда and значение | apply the button press to the state and show the screen |
Срок вышел (time is up) | take one step and show the screen |
Сбой (failure) with fields код and сообщение | finish with an error carrying the same code |
There are two effects. «Показать» с место равным "экран" и текст равным … writes text into the page element with the attribute данные-место="экран". «Ждать событие» с срок равным 350 means: return control to the program when the person presses a button or 350 ms pass. A term of 0 means wait for the person only. So with one effect the program waits for both the buttons and the clock.
What follows from this:
- The program does not work with the DOM. It names a place with a string and hands over text. The host does not build markup from the text: it does not use
innerHTML. - Every function of the program always terminates, although it is unknown whether the Collatz sequence is finite for every number. The function «Шаг градины» takes one step. The host repeats the steps: on a button press or on the clock.
- The tab does not freeze. While the program waits for an event, none of its code is running.
Which targets generate a plan
A program with a план declaration is generated by the targets c, go, js, python, rust and ts. The targets cpp, csharp, elixir and java return the error FLANG_PLAN_UNSUPPORTED.
Next: URL shortener — a service with the same host; The boundary between flang and the host.