2 · A Name and a Number
What changes, what stays put, and which numbers fit?
Bring with you: the single-file program and run command from chapter 1.
Puck's first line worked. At the box office, five of the eight seats have sold. A customer buys another ticket, and the clerk needs to update the sign that says how many seats are left. Puck reaches for an abacus large enough to take up one of them.
To print how many seats will be left after the sale, track two numbers. Call the total number of seats capacity and the number of tickets already sold sold.
The number changes. The name can stay. This saves a great deal on stationery.
A name is not a prophecy
A declaration introduces a name and the kind of value it holds. An assignment gives a mutable object a new value. Think of a name on a box: replacing the contents need not replace the label.
Predict first: after one more sale, how many seats remain? Does the assignment say that a number is equal to itself plus one?
Example 2-1 · Count the house
Replace the file from chapter 1, or copy the complete matching example project into a fresh folder.
Vada · Ada · Example 2-1
main.vadb · Link to this version
with Ada.Text_IO;
procedure Main is
Capacity : constant Natural := 8;
Sold : Natural := 5;
begin
Sold := Sold + 1;
Ada.Text_IO.Put_Line ("Seats left:" & Natural'Image (Capacity - Sold));
end Main;
Varc · C-family · Example 2-1
main.va · Link to this version
import Ada.Text_IO;
fn Main() {
let capacity: Natural = 8;
var sold: Natural = 5;
sold = sold + 1;
Ada.Text_IO.Put_Line("Seats left:" ++ Natural::Image(capacity - sold));
}
Vibe · Python · Example 2-1
main.vb · Link to this version
from verada import Natural
def main() -> None:
capacity: Natural = 8
sold: Natural = 5
sold = sold + 1
print("Seats left:", capacity - sold)
if __name__ == "__main__":
main()Syntax note: Vada's constant and Varc's let make the capacity binding immutable; Varc's var permits assignment. Vibe's local annotation capacity: Natural gives a type, not immutability. In this program we simply do not reassign capacity. That difference matters; a familiar-looking annotation is not a hidden constant.
Run the file with the same veradic run main.vadb, veradic run main.va, or veradic run main.vb command as in chapter 1. If you copied the project, prove it first, then run it from its folder:
veradic proveproved 4 of 4 checks in 1 subprogram (3 statically, 1 by the solver)
veradic runSeats left: 2
The single file prints the same line. The counts in the summary differ a little between the three syntaxes; the verdict doesn't. prove records what it proved in a hidden .verada folder beside your sources, the project's proof cache. run uses the proofs prove left behind; skip prove and Verada still runs the program, but keeps the unproved checks at run time and says so (B8012).
The assignment reads sold, adds one, and stores the result. The sold count goes from 5 to 6. Then the subtraction uses the updated count. The theatre has 8 seats and 6 are sold, leaving 2 free.
Vada writes assignment as :=. Varc and Vibe use =. Equality comparisons have their own spelling: = in Vada and == in Varc and Vibe. Context matters, but the underlying questions remain separate: change this value versus are these values equal?
Numbers have a type, and a limit
Natural means a whole number from zero to a finite maximum: 0 through 9,223,372,036,854,775,807. The related Integer also permits negative values, down to −9,223,372,036,854,775,808. Both fit in 64 bits. These are useful machine-sized numbers, not an endless supply of mathematical integers.
The declared type tells Verada which operations and values are appropriate. More precisely, Natural is a constrained subtype of Integer: it keeps the same integer type identity but narrows the permitted stored values. We will make our own such ranges in chapter 5. For now, read Natural as “a count, at least zero.”
Before accepting the addition, Verada must establish that its result fits the underlying integer arithmetic; storing a result in Natural must also respect its range. Here all the numbers are known and small, so those facts are straightforward.
A note for Python readers: in Vibe, int has Verada's finite Integer meaning. It does not grow automatically like a CPython integer. Vibe borrows Python's spelling, not its habits: a Vibe program follows Verada's rules, whatever Python would have done.
Intermission. Values wander off; names keep their seats.
Turning a number into dialogue
Printing text and printing a number are slightly different jobs. Vada's Natural'Image and Varc's Natural::Image turn a number into text; a nonnegative Ada numeric image begins with a space. That is why the literal is "Seats left:" without a trailing space. & in Vada and ++ in Varc join the two text pieces.
Vibe's print separates its arguments with a space, so its version prints the same line. You can treat these output forms as a recipe while we concentrate on the counts.
What happens when all the seats are sold?
When seven tickets are sold, there's room for one more. When eight are sold, the same addition produces nine. Nine fits Natural, but it exceeds the theatre's capacity. Natural gives us a nonnegative count. Puck still needs to say when the clerk may sell a ticket. Next chapter, that rule becomes a decision.
Your cue
- Start with seven tickets sold. Predict the output, then run the program.
- Start with eight tickets sold. Why does Verada accept the arithmetic even though the theatre would be oversold?
- In Vada or Varc, try assigning a new value to the capacity constant. In Vibe, try the same edit to its ordinary local variable. Explain the different outcomes.
Prompter's notes · hints and answers
Start with 7 sold. The sale brings the count to 8 sold, leaving 0 seats.
Start with 8 sold. The sale brings the count to 9 sold, one beyond capacity. The program prints the subtraction directly as a signed integer expression. The Natural variable sold still holds 9. Vada and Varc print Seats left:-1 because a negative numeric image begins with the minus sign. Vibe prints Seats left: -1 because print supplies the space.
Vada's constant and Varc's immutable binding cannot be assigned again. Vibe's local type annotation does not forbid reassignment. This is precisely why the book states the convention explicitly instead of calling all three declarations immutable.
You can now follow a changing value, recognize a constant, and explain why “a legal count” and “a sensible count for this theatre” are different requirements. When the eighth ticket is gone, Puck will need a way to turn away the next customer.
← Previous: Curtain Up · Contents · Next: To Branch, or Not to Branch →