Skip to content

Formulas

A formula is a set of equations between named properties. Give it any properties you know and it works out the rest.

formula Trip(speed, time):
  distance = speed * time
Trip(speed = 60 km/hour, time = 2 hours)

The result is a record. Read one property with ::.

formula Trip(speed, time):
  distance = speed * time
Trip(speed = 60 km/hour, time = 2 hours)::distance

Any property can be the unknown. Supply the distance and the time, and smoot solves for the speed.

formula Trip(speed, time):
  distance = speed * time
Trip(distance = 120 km, time = 2 hour)::speed
Trip(distance = 240 km, speed = 80 km/hour)::time

Arguments are always named. Positional arguments are an error, because a formula’s inputs have no natural order.

Several equations in several unknowns are solved together by elimination, whichever way they depend on each other.

formula Trio(x, y, z):
  x + y + z = 6
  x + 2 * y = 5
  y + 2 * z = 8
Trio()::x
Trio()::z

A formula with no argument list promotes every free name in its equations to a property.

formula Pythagoras:
  a^2 = b^2 + c^2
Pythagoras(b = 3, c = 4)::a
formula Motion:
  speed = distance / time
Motion(distance = 240 km, speed = 80 km/hour)::time

Defaults, hints, private values, and guards

Section titled “Defaults, hints, private values, and guards”

A parameter can carry a default and a hint. let inside a body is a working value that does not appear in the result. A when guard is checked against the inputs and, after solving, against the outputs.

formula Loan(principal: Currency, rate, years = 30):
  let monthly rate = rate / 12
  let n = years * 12
  payment = principal * monthly rate / (1 - (1 + monthly rate) ^ (0 - n))
Loan(principal = $250,000, rate = 6%)::payment
Loan(payment = $1,500, rate = 6%)::principal
formula Pyramid(base: Length, angle: Angle) when base > 0 m:
  height = (base / 2) * tan(angle)
Pyramid(base = 4 m, angle = 45 deg)::height
Pyramid(base = -1 m, angle = 45 deg)

Long calls can span lines inside the parentheses.

A property can be a whole other formula. Its equations join the outer system, so a back-solve reaches inside.

formula Rect(w, h):
  area = w * h
formula Box(height):
  base = Rect(h = 3 m)
  volume = base::area * height
Box(height = 4 m, volume = 24 m^3)::base::w

A formula call standing alone in a body merges its properties into the host. That is how shapes/2d defines Square from Rectangle.

The shapes live in the bundled shapes/2d and shapes/3d modules (see Imports); the formulas below need no import.

FormulaProperties
Density(mass, volume)density
CompoundInterest(principal, rate, periods)amount
Percentage(part, whole)rate
PercentChange(original, rate)result
Proportion(a, b, c)d
ReturnOnInvestment(invested, returned)roi
Duration(start, end, midpoint, duration, half duration)any two give the other three
CompoundInterest(principal = 1000, rate = 5%, periods = 10)::amount to 2 dp
CompoundInterest(amount = 2000, rate = 5%, periods = 10)::principal to 2 dp
Duration(start = 9am on 22 Jun 2026, end = 5:30pm on 22 Jun 2026)::midpoint

to and as apply to every field that fits.

import "shapes/2d"
rect = Rectangle(width = 3 m, height = 4 m)
rect to ft to 1 dp