Every program in this tutorial so far has been a closed world: it computed something and printed it, and the only thing that crossed its boundary was the answer. Real programs are not like that. They are handed parameters by whoever ran them, they drive other programs that already do a job well, and they read back what those programs said. A data pipeline is exactly this shape: one tool's output is the next tool's input. And most working software is a data pipeline wearing a coat.
This chapter builds a small one. It counts how often each word appears in a passage, the way the classic shell line sort | uniq -c | sort does. But it works from M9, and without a shell. Each stage is its own program, run through System.Exec, fed the previous stage's output on its standard input. It reads an optional --top=N option, checks that every stage *worked*, and parses the columns that come back.
MODULE C17Pipe ;
IMPORT Text ;
(* Chapter 17 is about living in the real world.
A program that only ever talks to itself is a demo. A useful one
takes PARAMETERS from whoever ran it and DRIVES OTHER PROGRAMS,
then reads back what they said. This one counts word frequencies
the way a shell line would: sort, then uniq -c, then sort again.
It does so from M9, and without a shell. Each stage is its OWN
program, run through System.Exec, fed the previous stage's output
on its stdin and INSISTED to have succeeded.
This chapter is really about three things:
* A stage's output comes back as a VALUE, and so does its status.
System.Exec answers a Result: the exit status, everything the
program wrote to stdout, and everything it wrote to stderr. So
the pipe between two tools is an M9 string, not a temp file, and
a stage that FAILED is a status you look at rather than a thing
you hope did not happen. A shell one-liner hides the middle of
the pipe; here every stage is checked.
* The environment is a boundary, and boundaries lie unless they
are pinned. Sort order depends on the locale, so the SAME
pipeline gives a different answer on a French machine -- unless
LC_ALL=C nails it to bytes. Exec sets it as an environment
OVERRIDE, merged over the inherited environment, so PATH still
finds the program. It is the process-level echo of why Fmt owns
its float printing instead of borrowing a locale-dependent
printf.
* A parameter is a DECISION, not an accident. The count of rows
to show is a NAMED option, --top=N, the convention System.Value
defines. It is never a bare number that could be mistaken for a
filename. A value that is not a count is reported before the
default is used.
With no arguments it shows the top DefaultTop; run it as
`./c17pipe --top=3` to ask for three. *)
IMPORT System ;
IMPORT DynStr ;
IMPORT Io ;
CONST
DefaultTop = 5 ;
(* Passage answers the text to count, fixed so the whole run is
reproducible. A CONST cannot be a `+` expression, so the passage
is a procedure that composes its literals into HEAP and answers
the whole. *)
PROCEDURE Passage () : STR =
BEGIN
RETURN
'the program reads the data and the program checks the data '
+ 'a program that checks its data and checks its result is a '
+ 'program you can trust the data does not lie and neither '
+ 'does the result the program reads the data the program '
+ 'writes the result'
END Passage ;
PROCEDURE IsLetter (c: CHAR) : BOOL =
BEGIN
RETURN ((c >= 'a') AND (c <= 'z')) OR ((c >= 'A') AND (c <= 'Z'))
END IsLetter ;
PROCEDURE Lower (c: CHAR) : CHAR RAISES ValueRange =
BEGIN
IF (c >= 'A') AND (c <= 'Z') THEN
RETURN CHR (ORD (c) + 32)
END ;
RETURN c
END Lower ;
(* WordsOf answers the passage as one lowercase word per line, the
input a sort | uniq pipeline wants. Anything that is not a letter
is a word break. *)
PROCEDURE WordsOf (RO text: STR) : STR
RAISES ValueRange, IndexError =
VAR
pool : POOL ;
d : PTR DynStr.DString IN pool ;
i : I64 ;
inWord : BOOL ;
BEGIN
d := DynStr.New (pool) ;
inWord := FALSE ;
FOR i := 0 TO Text.BLen (text) - 1 DO
IF IsLetter (Text.BChar (text, i)) THEN
DynStr.AppendChar (d, Lower (Text.BChar (text, i))) ;
inWord := TRUE
ELSIF inWord THEN
DynStr.AppendChar (d, 0AC) ; (* end this word *)
inWord := FALSE
END
END ;
IF inWord THEN DynStr.AppendChar (d, 0AC) END ;
RETURN DynStr.View (d)
END WordsOf ;
(* ParseInt answers a non-negative integer that is the WHOLE of s,
or -1 if it is not one. *)
PROCEDURE ParseInt (RO s: STR) : I64 RAISES IndexError =
VAR
i, v : I64 ;
BEGIN
IF Text.BLen (s) = 0 THEN RETURN 0 - 1 END ;
v := 0 ;
FOR i := 0 TO Text.BLen (s) - 1 DO
IF (Text.BAt (s, i) < '0') OR (Text.BAt (s, i) > '9') THEN RETURN 0 - 1 END ;
v := v * 10 + (I64 (Text.BAt (s, i)) - ORD ('0'))
END ;
RETURN v
END ParseInt ;
(* TopWanted answers how many rows to show: --top=N, or DefaultTop.
System.Value reads the text after --top= in the options, or
answers the default we name here. A number the parser rejects is
reported before the default is used, so a mistyped option is
never mistaken for a deliberate omission. *)
PROCEDURE TopWanted () : I64 RAISES ValueRange, IndexError =
VAR
s : STR ;
n : I64 ;
BEGIN
s := System.Value ('--top', '') ;
IF Text.BLen (s) = 0 THEN RETURN DefaultTop END ;
n := ParseInt (s) ;
IF n >= 0 THEN RETURN n END ;
Io.ErrLine ($'--top is not a count; showing ${DefaultTop}') ;
RETURN DefaultTop
END TopWanted ;
(* Stage runs one pipeline stage: prog with args, fed `input` on its
stdin. It pins the stage to LC_ALL=C so the byte ordering does
not depend on the machine's locale, and INSISTS that it succeeded.
A stage that exits nonzero stops the whole run, naming its status
and whatever it wrote to stderr. A pipeline that RAN is not a
pipeline that WORKED. *)
PROCEDURE Stage (VAR pool: POOL ; RO prog: STR ; RO args: SLICE OF STR ;
RO input: STR) : STR
RAISES ValueRange, Io.IOError =
VAR
r : System.Result ;
BEGIN
r := System.Exec (pool, prog, args, input, ['LC_ALL=C']) ;
IF r.status # 0 THEN
Io.ErrLine ($'${prog} exited with status ${r.status}') ;
IF Text.BLen (r.err) > 0 THEN Io.ErrLine (r.err) END ;
Io.Halt (1)
END ;
RETURN r.out
END Stage ;
(* uniq -c prints " COUNT word", the count right-justified in a
field of blanks. Most of living in the real world is parsing a
tool's REAL output, padded columns and all. Report shows the
first `top` rows as a ranked table. *)
PROCEDURE Report (RO counts: STR ; top: I64)
RAISES ValueRange, IndexError =
VAR
i, j, shown, count : I64 ;
word : STR ;
BEGIN
Io.WriteLine ($'the ${top} most frequent words in the passage:') ;
i := 0 ;
shown := 0 ;
WHILE (i < Text.BLen (counts)) AND (shown < top) DO
WHILE (i < Text.BLen (counts)) AND (Text.BIs (counts, i, ' ')) DO i := i + 1 END ;
count := 0 ;
WHILE (i < Text.BLen (counts)) AND (Text.BAt (counts, i) >= '0')
AND (Text.BAt (counts, i) <= '9') DO
count := count * 10 + (I64 (Text.BAt (counts, i)) - ORD ('0')) ;
i := i + 1
END ;
IF (i < Text.BLen (counts)) AND (Text.BIs (counts, i, ' ')) THEN i := i + 1 END ;
j := i ;
WHILE (j < Text.BLen (counts)) AND (NOT Text.BIs (counts, j, 0AC)) DO j := j + 1 END ;
word := SLICE (counts, i, j - i) ;
IF Text.BLen (word) > 0 THEN
shown := shown + 1 ;
Io.WriteLine ($' ${shown}. ${word} (${count})')
END ;
i := j ;
IF (i < Text.BLen (counts)) AND (Text.BIs (counts, i, 0AC)) THEN i := i + 1 END
END
END Report ;
VAR
pool : POOL ;
top : I64 ;
words, sorted, counted, ranked : STR ;
BEGIN
BEGIN
top := TopWanted () ;
(* Build the passage as one lowercase word per line, in memory.
The first stage reads it on its stdin. *)
words := WordsOf (Passage ()) ;
(* Run three stages, each its own program, each checked. The
output of one is the stdin of the next: an M9 string, not a
temp file. The second sort ranks by count (numeric,
descending) then by the word, so ties have a defined order and
every run is identical down to the byte. *)
sorted := Stage (pool, 'sort', NEW (STR, 0), words) ;
counted := Stage (pool, 'uniq', ['-c'], sorted) ;
ranked := Stage (pool, 'sort', ['-k1,1nr', '-k2,2'], counted) ;
Report (ranked, top)
EXCEPT
| Io.IOError (p) :
Io.ErrLine ('could not start a pipeline stage:') ;
Io.ErrLine (p) ;
Io.Halt (1)
| ValueRange :
Io.ErrLine ('a value was out of range') ; Io.Halt (1)
| IndexError :
Io.ErrLine ('an index was out of range') ; Io.Halt (1)
END
END C17Pipe.
C17Pipe.m9
With no arguments it shows the five most frequent words. The counts and their order are fixed, because the passage is fixed and the pipeline is pinned (more on that below).
expected output:
the 5 most frequent words in the passage:
1. the (10)
2. program (6)
3. data (5)
4. and (3)
5. checks (3)
Run it with a number, ./c17pipe --top=3, and it shows three. Run it with a --top= that is not a number and it says so and falls back to the default, rather than guessing.
The output is a value, and so is the status
System.Exec runs a program directly, with no shell, and answers a Result: the exit status, everything the program wrote to out, and everything it wrote to err. So a stage's output is an ordinary M9 string, and the pipe between two tools is that string handed to the next stage as its input:
A stage's arguments are a slice of strings, and each call writes its own in brackets: ['-c'] is a slice of one string, made for this call and gone after it. Its elements are any expressions of one type. It may be handed to a parameter that only reads it (RO args here) and nowhere else, so nothing can keep a slice that is about to go. The first sort takes no arguments. The brackets need an element to know the type from, so the empty slice is written NEW (STR, 0). (Before 0.29 each list was a variable, a NEW and an assignment for every element.)
There is no counts.txt on disk. sorted and counted are values, and you can see the handoff in the program instead of inferring it from a shell line.
And every stage's status is checked, inside Stage:
r := System.Exec (pool, prog, args, input, ['LC_ALL=C']) ;
IF r.status # 0 THEN
Io.ErrLine ($'${prog} exited with status ${r.status}') ;
IF Text.BLen (r.err) > 0 THEN Io.ErrLine (r.err) END ;
Io.Halt (1)
END ;
A pipeline that *ran* is not a pipeline that *worked*. sort fails if the count of key fields is wrong. uniq fails on a bad option. A program with a typo in its name fails to start at all. A reader of a failed stage's output would parse whatever it managed to write before it died, and report a confident wrong answer. The status check is the same discipline this language applies to a slice index or a conversion, now at the process boundary. And because each stage is run *as itself*, its status and its stderr are separate values you can look at.
That is the difference from running the whole line through the shell. M9 could hand sort | uniq -c | sort to Io.Run as one string, and it would work. But Io.Run answers one status, the shell's, which is the *last* stage's. A sort that failed in the middle of the pipe is invisible if the final sort succeeds, because the shell throws away the statuses in between. Each stage run as its own program makes every failure a value rather than a thing you hope did not happen.
A program that never finishes
System.Exec waits as long as the stage takes, which is right for a pipeline: sort on a large file is slow, not stuck, and no number you could write down would tell the two apart. But sometimes the program is one *you did not write*: a cell a reader typed, a tool that may hang on a network. Then waiting for ever is a decision too, and usually the wrong one. System.ExecWithin is Exec with a deadline:
r := System.ExecWithin (pool, prog, args, input, env, 10.0) ;
IF r.stopped THEN
Io.ErrLine ($'${prog} was still running after ten seconds') ;
Io.ErrLine (r.out) (* what it had printed by then *)
END ;
Three things are worth knowing about it. The deadline covers the whole run, not just the program's exit. Otherwise a stage that has finished while something *it* started still holds its output would wait for ever. When the time runs out, the program and everything it started are stopped together. And a stopped run still reports: out and err hold what was written before the deadline. So you can say what it was doing when it was stopped instead of showing a blank page.
The tutorial you may be reading this in uses it: on Windows it runs your cells with a limit, because a cell that loops for ever would otherwise take the whole tutorial down with it.
The environment is a boundary, and boundaries lie unless you pin them
Look at the environment Stage hands over:
r := System.Exec (pool, prog, args, input, ['LC_ALL=C']) ;
Sort order is locale-dependent: on a machine set to a French or Swedish locale, sort orders letters by that locale's rules. So the *same pipeline over the same data* produces a different order. A program whose output depends on an environment variable nobody set on purpose will disagree with itself between two machines and waste an afternoon.
LC_ALL=C nails the ordering to raw byte order, everywhere. The env argument is a list of NAME=VALUEoverrides, merged over the environment the program already has. So this one pins LC_ALL and leaves PATH alone, and PATH lets System.Exec find sort by its bare name in the first place. A replacement of the whole environment would have thrown PATH away with it.
This is the same reason chapter 2's Fmt owns its float printing instead of borrowing C's printf: printf is locale-dependent too, and would write 3,14 where you meant 3.14 in half the world. It is one rule, *a boundary that can lie must be pinned*, and it applies to the process environment exactly as it applies to the numeric one.
The final sort is pinned in another way: -k1,1nr sorts by the first field (the count) numerically and in reverse, and -k2,2 adds the word as a tiebreaker. Without that second key, two words with the same count could come back in either order, and the output would be stable only by luck. With it, every run is identical down to the byte. That lets this chapter's expected output be an equality, not an approximation.
A parameter is a decision
The count of rows to show comes in as a named option, and System.Value reads it:
s := System.Value ('--top', '') ;
IF LEN (s) = 0 THEN RETURN DefaultTop END ;
n := ParseInt (s) ;
IF n >= 0 THEN RETURN n END ;
Io.ErrLine ($'--top is not a count; showing ${DefaultTop}') ;
RETURN DefaultTop
System.Value ('--top', '') reads the text after --top= in the options, or answers the default you name. Here the default is the empty string, which TopWanted then reads as "no option given" and turns into DefaultTop. The option is --top=3, attached with =, never a bare 3 sitting on the command line where nothing could say whether it was a count or a filename. That is System's whole argument convention: an option begins with -, a value is attached with =, and a bare -- ends the options. It is small enough to state in full, which is the point.
And a --top= whose value is not a count is reported *before* the default is used, so a mistyped option is never silently mistaken for a deliberate omission. "No option" is a default the program *decided*, not one it drifted into.
Parsing what a tool actually prints
uniq -c does not print clean two-column data. It prints the count right-justified in a field of blanks, then a space, then the word. That format survives the final sort, so the first lines of the ranked output look like:
10 the
6 program
5 data
So Report skips the leading blanks, reads the digits as the count, steps over the one separating space, and takes the rest of the line as the word. Most of living in the real world is parsing a real tool's real output, padding, separators and all, instead of wishing it were tidier. The bytes are what the tool sends; the program adapts to them, not the other way around.
The digits are read as bytes, since a tool's output is bytes:
WHILE (i < Text.BLen (counts)) AND (Text.BAt (counts, i) >= '0')
AND (Text.BAt (counts, i) <= '9') DO
count := count * 10 + (I64 (Text.BAt (counts, i)) - ORD ('0')) ;
Text.BAt answers a BYTE, and '0' is a character. The two are different types, and M9 converts nothing by itself. A comparison is the one place they meet: a character literal that is ASCII stands for its byte beside a BYTE, in =, #, the four orders and a CASE label. 'é' there is refused, since it is two bytes in a file. The arithmetic on the next line still says what it does, I64 (...) and ORD ('0'): a byte is not a number until the program says so.
The pipeline is a program you can read
words, sorted, counted and ranked are M9 strings, and the three Stage calls are the pipe. A shell hides inside a single line which program runs, what it is fed, whether it succeeded and what it complained about. Here those are four named values and three checked calls, in the open where a reader can see them.
That visibility is the whole idea, and this tutorial started there. M9 is built so that the person reading the program, and the data crossing its edges, can *see* what happened. And it is built so that the compiler refuses the ways a boundary might quietly lie. A real program lives among other programs, arguments and locales, and all of them will hand it something unexpected eventually. The last seventeen chapters worked toward one result: when they do, the program says so, at the line where it happened, instead of computing a confident wrong answer and printing it as if it were true.