Episode 4 Momo's Transformation Factory

The Life of Code 05

Tinker and the Long-Awaited 3

When does the actual calculating happen?

Momo's cards are still on the shelf.

Now it's Tinker's turn to run a program.

What shows up on the screen, and when?

Today's odd question

What record will Tinker get from topaz run with the 1 + 2 file from Episode 4?

So that's your guess.

Let's read the records with Tinker.

Comic

Scene 1 By the shelf

MomoAs promised. Go and find out with Tinker.

LineyTinker, what shows up on the screen?

TinkerLet's leave Momo's cards on the shelf.I'll take the main.tpz file you're written in.

Scene 2 Checks first

TinkerAt my bench, checking comes first.Running comes after.

TamiThis file passed its checks!

Scene 3 A blank window

LineyHuh? No 3!

TinkerThe window's blank.What do the note tray and the finish light tell us?

Scene 4 Finally, 3

TinkerLet's put the same addition in a new program.This one has a line to announce the start and a line to print the result.

LineyFinally, 3!

TinkerYou get only what you asked to have written, in the order you asked.

Scene 5 The twin

TwinI'm almost the same as Liney. I just have "apple" where the 2 was.I'll at least get to print start, right?

TinkerHmm. Let's read the record together.

What did the three files really leave behind?

Let's read it on Tinker's Run Board.

Experiment

Tinker's Run Board

How far did each program get?

Tinker tried topaz run on each of the three files and kept the records.

Read the window, the note tray and the finish light of the three slips to see how far each program got.

A 0 on the finish light usually means the command finished successfully.

The window and note tray show the text saved from that command.

Topaz 5.21.1 run records

Pick a slip

A The file from Episode 4

Byte for byte the same file Momo copied onto cards.

Program
  1. 1 + 2

Command topaz run main.tpz

Window

Empty

Note tray

Empty

Finish light

0

Did this program get as far as running?

Tinker used topaz run main.tpz. The file passed its checks and finished running. Nothing in it asks for output, so the window is blank. The note tray is empty too, and the finish light shows 0.

The window is empty. So what's in the note tray, and what does the finish light say?

The same file, checked with topaz check alone

Command topaz check main.tpz

Window
main.tpz: types-ok (1 module)
main.tpz: resolve-ok (1 module)
Note tray

Empty

Finish light

0

The check alone finished with 0 too. Its window held two lines of check results instead of the program's text.

Open the original run files for this slip

"Empty" is a label on the board. The original file contains zero bytes.

Input info
Input bytes
6
Input sha256
cb898749d76e51fdaf6c6636920ccdb4f415fc1cffd1e2497636b38cc2469807
Topaz version
5.21.1
Executable sha256
8dbc4723d2c243bfde6f13f4e23ccbd33f409d24173d6b99a71d957581183cef

We followed the same steps twice with the same version. Both runs gave us identical bytes in standard output and standard error, and the exit codes matched.

We also tried the same command on this input with versions 5.21.0 and 5.21.1. The bytes in each output stream matched across versions, as did the exit code.

B The program that prints

The top line brings in io, the tool that writes to the screen. Inside "{result}", the value stored in result goes in as text.

Program
  1. import std.io
  2. io.writeLine("start")
  3. let result = 1 + 2
  4. io.writeLine("{result}")

Command topaz run main.tpz

Window
start
3
Note tray

Empty

Finish light

0

Did this program get as far as running?

The window shows start and then 3. The program wrote them while it ran.

The window shows start and 3. If the program never ran, who wrote them?

The same file, checked with topaz check alone

Command topaz check main.tpz

Window
main.tpz: types-ok (2 modules)
main.tpz: resolve-ok (2 modules)
Note tray

Empty

Finish light

0

The check alone finished with 0 too. Its window held two lines of check results instead of the program's text.

Open the original run files for this slip

"Empty" is a label on the board. The original file contains zero bytes.

Input info
Input bytes
81
Input sha256
5f4c63f0f43f717035c441ab5c99388451a7660d7be07d32441a3cece60bf713
Topaz version
5.21.1
Executable sha256
8dbc4723d2c243bfde6f13f4e23ccbd33f409d24173d6b99a71d957581183cef

We followed the same steps twice with the same version. Both runs gave us identical bytes in standard output and standard error, and the exit codes matched.

We also tried the same command on this input with versions 5.21.0 and 5.21.1. The bytes in each output stream matched across versions, as did the exit code.

C The twin

Only line 4 differs from B. Where the 2 was, it says "apple".

Program
  1. import std.io
  2. io.writeLine("start")
  3. let result = 1 + "apple"
  4. io.writeLine("{result}")

Command topaz run main.tpz

Window

Empty

Note tray
error[TPZ5001]: `+` needs matching int, float, string, BigInt, or Decimal operands, found `int` and `string`
 --> main.tpz:4:14
  |
4 | let result = 1 + "apple"
  |              ^^^^^^^^^^^

main.tpz: 1 type diagnostic

The diagnostic points to the whole expression 1 + "apple" on line 4. The eleven carets start under the 1.

Finish light

1

Did this program get as far as running?

The last line of the note says type diagnostic, a diagnostic from the type check. That is why even the start on line 3 never appeared.

An empty window alone can't tell you. Try reading the first and last lines of the note.

The same file, checked with topaz check alone

Command topaz check main.tpz

Window

Empty

Note tray
error[TPZ5001]: `+` needs matching int, float, string, BigInt, or Decimal operands, found `int` and `string`
 --> main.tpz:4:14
  |
4 | let result = 1 + "apple"
  |              ^^^^^^^^^^^

main.tpz: 1 type diagnostic
Finish light

1

Checking the same file with topaz check alone put the same note in the tray.

Open the original run files for this slip

"Empty" is a label on the board. The original file contains zero bytes.

Input info
Input bytes
87
Input sha256
2b65f5c87a2d4a89eea4901f8962567a44b9f614dcbe7e4ec3f37521fd0ecd46
Topaz version
5.21.1
Executable sha256
8dbc4723d2c243bfde6f13f4e23ccbd33f409d24173d6b99a71d957581183cef

We followed the same steps twice with the same version. Both runs gave us identical bytes in standard output and standard error, and the exit codes matched.

We also tried the same command on this input with versions 5.21.0 and 5.21.1. The bytes in each output stream matched across versions, as did the exit code.

Tag all three slips to open the discovery card.

Two blank windows, two reasons

A and C both left the window empty.

A ran to the end but had no line asking for output. C was stopped at the check and never started running.

Read the note tray and the finish light along with the window.

The bench, the lever and the lights are part of the story. The text in the window and the note tray comes from real records made in advance.

The real story

Writing to the screen happens when the program runs

The text in the window is standard output. The text in the note tray is standard error.

Standard output and standard error are two streams that programs and tools can write to. Results usually go to standard output, and diagnostics or notices to standard error. The two streams can be collected separately.

The number on the finish light is the exit code of the topaz run command. Usually 0 reports success, and a nonzero number reports failure. In these records, A and B finished with 0. C stopped at the check and finished with 1.

An interpreter reads a program's instructions and carries out the operations they describe. It may use another form of the program internally.

In Topaz 5.21.1, topaz run checks the source and runs the checked program with an interpreter. When the command finished, the working folder held just main.tpz.

In this version, topaz run checks the source first. When it finds a type error like the one on slip C, it reports the error and stops. The program never reaches the line that prints start.

When does the actual calculating happen?

The program wrote 3 to the screen when it ran.

The addition gives result its value. The last line puts that value into a string and writes 3 to standard output.

We can tell which line printed 3. Some compilers can work out a value like this before the program runs. This run record doesn't tell us exactly when the addition was first worked out.

Where did Momo's cards go?

We also observed the program on slip B. Here are three operations from its 44-line record. The addition points first to 1, then to 2. Here the addition sits inside the let statement that gives its value the name result.

Three operations taken from the record · Topaz compiler 5.21.1 record
What it doesThe record's wordsRepresentationSits inside
Two-value calculationbinaryaddi64The statement that names a value
Integerinteger1i64Two-value calculation
Integerinteger2i64Two-value calculation

Open all 44 lines of slip B's operation record, opens in a new tab

We gave topaz run the same source file. Its record shows what the command wrote and how it finished. It doesn't show the internal forms used along the way.

Does every language work this way?

No. In the Language Tour, the Python program printed 시작 ("start") on its first line. It stopped with a TypeError when it reached "3" + 4 on the next line.

When a tool checks and when it runs differs from tool to tool.

In one line

What we found out today

The text a program writes on the screen appears when that program runs.

topaz run checks the whole program first and starts running it only if the check passes.

Does a blank window mean the program never ran? Find one clue on slip A and one on slip C.

Show the answer

Not necessarily.

On slip A, the note tray is empty and the finish light shows 0. The file passed its checks and finished running, but no line asked for output.

On slip C, the note tray ends with type diagnostic and the finish light shows 1. The check stopped it before it started running.

Now you know what else to read when the window stays blank.

Field GuideRevisit these wordsInterpreterStandard Output and Standard Error

Next story

Episode 6Who Made the Compiler?

LineySo who made the tool that checked me?

At the end of the path, a tiny sprout pokes up out of a flowerpot.

Is a compiler a program someone wrote, too?

Next time, let's follow where the compiler came from.