What-if scenarios
A what-if tries a different input and shows what changes, without touching
your document. You write the new value on a what if line, and the answers
that depend on it appear in a column of their own beside the ledger.
rate = 5%
loan = £200,000
interest = loan * rate
what if rates up: rate = 6%The rate = 6% on the last line does not change rate above. The ledger
keeps the first column, “base”, and adds a column called rates up.
In that column, rate shows 6% and interest shows £12,000.00.
Two ways to write one
Section titled “Two ways to write one”On one line, put the changes after the colon, separated by commas. For more than a few, start a block: put the changes on the lines below, indented. In the app, Enter after the colon indents the next line for you, and Tab and Shift-Tab indent and dedent a line (to leave the editor, press Escape, then Tab).
budget = £900
nights = 4
hotel = £110
stay = nights * hotel
spare = budget - stay
what if short trip: nights = 3
what if posh:
hotel = £160
nights = 4Each change replaces the right-hand side of the line that gives that name its value. The new value can use the other changes in the same what-if, and any name the what-if leaves alone.
base = 10
double = base * 2
what if both: base = 20, double = base + 1
what if one: base = 20In both, double is 21, because it reads the new base. In one, nothing
replaces double, so it is 40.
A change can start from the value you wrote. On the right of its own name, a
name means the value as written, so peak = peak * 3 triples your peak.
Every other name still means the what-if’s value.
peak = 10
foo = 100
baz = 5
what if triple: peak = peak * 3
what if shrink: foo = foo * 80%, baz = foo * 80%In shrink, foo is 80, which is 80% of the foo you wrote. baz reads the
new foo, so it is 64.
A what-if name can have several words. Put it in backticks if it has characters like an apostrophe. A scenario name is a name in your document, like any other. If it repeats the name of a line, you get a warning and the later one wins, so give each what-if a name of its own.
hotel = £110
nights = 4
stay = nights * hotel
what if `Dad's treat`: hotel = £0
what if cheap hotel: hotel = £90Reading a what-if back
Section titled “Reading a what-if back”A what-if is also a value. Its name holds a record with one field for each
named line in the what-if’s column: a line that uses a changed name, directly
or through other lines, in document order. The what-if line shows it, like
late(pace = 3 km/hour, time = 10 hour). Read a field with ::, as you would
on any record.
dist: km = 30
pace = 5 km/hour
time = dist / pace
what if late: pace = 3 km/hour
late::pacelate::time reads the slower time. late::dist is an error, because dist
has no cell in late’s column. The message lists the fields the record does
have. A what-if that sets nothing that works shows as late().
Which lines get a cell
Section titled “Which lines get a cell”A line gets a cell in a what-if’s column if it uses a changed name, directly or through other lines. Lines that do not use one are left blank in that column. Each cell is one of three kinds:
- Set. The line you changed. It shows its new value in bold with a dotted underline.
- Changed. A line that reads a set line, directly or through others. It is drawn plain.
- Same. A line the what-if looked at but did not change. It is dimmed.
Totals are included. A total adds up the lines above it, so it gets a cell whenever any of them does.
rate = 5%
loan = £200,000
fee = £500
interest = loan * rate
fee * 2
interest
interest * 0
total
what if rates up: rate = 6%Here fee, loan, and fee * 2 are blank in the rates up column. The
interest lines and the total have cells. The rate cell is set. The
interest * 0 cell is £0.00, the same as in base, but it still reads a
changed line, so it is drawn as changed.
The what-if line’s own row in the column shows the scenario’s name.
Breaking and fixing a line
Section titled “Breaking and fixing a line”A what-if can make a line fail. That line’s cell in the column shows the error. Your document as written stays clean, and its list of errors does not change.
bill = £90
diners = 3
each = bill / diners
what if nobody: diners = 0
what if six: diners = 6It works the other way too. If a line fails as written, a what-if that supplies a working value makes it succeed in its own column. The errors in the first column stay, because that is how you wrote it.
loan = £200,000
rate = rte
pay = rate * loan
what if up: rate = 6%When the what-if line is wrong
Section titled “When the what-if line is wrong”The what-if line itself can be wrong, like any other line. The error shows on that line, and the what-if’s column leaves out the change that failed.
- Setting a name your document does not have:
"rte" isn't defined. Name it first: rte = …. - Setting a unit, function, formula, record,
solve foranswer, or a name that comes from the prelude or an import:a what-if sets only bindings: "x" is a unit here. - Changes that use each other in a circle, like
a = b + 1, b = a + 1:These lines depend on each other. - Two what-ifs with one name: each line warns (
sis also a what-if at line 3, which replaces this one), and the later is used.
rate = 5%
other = 1
unit bag = 25 kg
solve for n: n + 1 = 5
what if typo: rte = 6%
what if bad unit: bag = 30 kg
what if bad solve: n = 3
what if loop: rate = other + 1, other = rate + 1Setting the same name twice in one what-if is a warning, and the later one wins.
rate = 5%
what if twice: rate = 6%, rate = 7%Hints and let
Section titled “Hints and let”A changed value follows the rules of the line it replaces. If that line has a unit hint, a plain number takes the unit. A value of the wrong kind is the same error the line would give.
dist: km = 5
time = dist / 10 km/hour
what if far: dist = 8
what if wrong: dist = 8 kgA name made with let stays private, and a what-if on it does not make it
public. Do not write let on a what-if line.
let rate = 5%
loan = £1,000
interest = loan * rate
what if up: rate = 6%In the app
Section titled “In the app”Each column is as wide as its answers, and nothing scrolls sideways. Each what-if’s heading has a Hide button. Tap it to fold the column into a narrow strip with its name set vertically; tap the strip, which reads Show, to open it again. When the columns do not all fit, the rightmost what-if folds first, never the one you just opened. On a narrow screen one column is open at a time: opening one folds the others. The app remembers which columns are folded for each document.
Every open column has a handle at its left edge; for “base” it is the edge between the editor and the ledger. Drag it to set that column’s width, or double click it to go back to the width of its content. With the handle focused, the left and right arrow keys change the width. Names show as you typed them.
Click a cell to put the focus on it. The arrow keys move the focus from cell to cell, and Escape puts it back on the line. While the focus is in a what-if, the old right-hand side of each name it sets is struck through in the editor.
To jump from a cell to where it comes from, press g,
use the button in the bar, or on a page with no bar use the Go to button
that appears on the focused cell. On a set cell it takes you to the line in the
what-if that sets it. On any other cell it takes you to the line it reads.
On a shared page with several blocks, a what-if in one block reaches the blocks above it as well.
On the command line
Section titled “On the command line”Plain output prints the document’s last answer, then one line for each what-if with the last answer in its column.
$ smoot trip.smoot£460.00short trip: £570.00With --json, the result has a scenarios list. Each entry holds the name,
the names it sets, and its cells.
What a what-if cannot do
Section titled “What a what-if cannot do”- It changes inputs only. There are no ranges, such as
rate = 4% to 8%, and no lists of values. - It cannot fix an answer and ask what input gives it. Use
solve forfor that. - It works in the document you are editing. A module you import cannot contain one.
- It cannot change a name from the prelude or from an import. Name your own line first, then set that.