Modules, tests and laws
Slide 43 of 85
43lawModules, tests and laws

Laws

Grammar: law-decl
lawIDENT(params)blocktable-law
law-decl = "law" IDENT "(" [ params ] ")" ( block | table-law ) ;

A law is a claim the compiler proves while it compiles: law name(params) { … }. Its parameters take every value of their types, and every assert its body reaches has to hold for all of them. A law it cannot prove is a compile error, and nothing lets a program accept one anyway. A law is never emitted and never runs.

  • An assert under an if claims something only where the if holds, and nothing the law reaches may panic — so a law with no assert claims exactly that.
  • Int wraps round as it does at run time, so a + 1 > a is refused with a = 9223372036854775807.
  • A law may only reach code that ends and answers the same every time: no hive.* call, no async, no loop but for each and a counting loop the bounds pass trusts.

Only a law may call a law. Calling another one hands over its claim for those arguments; calling itself, on a part a pattern took from a parameter, is an induction. Most laws need neither: the compiler tries edge values first, then every value of a small type, then unknowns split into cases, and then an induction on each vector parameter, all before anything is written. A refusal names the values that break the law, or the goal it could not reach, the case it was in, and what it knew there.

A law written inside a type, with one parameter of that type, is what every value of the type keeps. Each place that builds one or writes a field of one has to keep it, proved from what that function knows there: its guards, its early returns, and the laws of the values it holds. Wherever a value is read, its laws are known, so a function law can rely on them and an index they guarantee needs no guard. A decoder checks them as it runs, since nothing the compiler saw built what it read, and refuses a value that breaks one as Broken.

A law can be about a database table too, and every query on it has to carry what the law says. That one comes once SQL has, in “Laws on a table”.

A cancelled booking stays cancelled
type Status {
	Requested
	Confirmed
	Cancelled
}

type Event {
	Confirm
	Cancel
}

func apply(status: Status, event: Event): Status {
	if status is Status.Cancelled {
		return status
	}
	if event is Event.Cancel {
		return Status.Cancelled()
	}
	return Status.Confirmed()
}

law cancelledIsFinal(status: Status, event: Event) {
	if status is Status.Cancelled {
		assert apply(status, event) == status
	}
}

proc main(): void {
	echo apply(Status.Requested(), Event.Confirm()) == Status.Confirmed()
}
nights.hive, wrong on purpose
func nights(checkIn: Int, checkOut: Int): Int {
	return checkOut - checkIn
}

law aStayHasNights(checkIn: Int, checkOut: Int) {
	if checkIn < checkOut {
		assert nights(checkIn, checkOut) > 0
	}
}

proc main(): void {
	echo nights(4, 6)
}
A booking always has a night
type Booking {
	room: Str
	checkInDay: Int
	checkOutDay: Int

	law atLeastOneNight(b: Booking) {
		assert b.checkInDay >= 0 && b.checkInDay < b.checkOutDay
	}

}

func book(room: Str, firstDay: Int, nights: Int): Result<Booking, Str> {
	if firstDay < 0 || firstDay > 100000 || nights < 1 || nights > 365 {
		return Result.Error("those dates are not a stay")
	}
	return Result.Ok(Booking(room, firstDay, firstDay + nights))
}

proc main(): void {
	echo book("12", 4, 3)
	echo book("14", 9, 0)
}
What a refusal looks like
$ hive check nights.hive
nights.hive:7: in law `aStayHasNights`: it does not hold for checkIn = -9223372036854775808, checkOut = 0: `nights(checkIn, checkOut) > 0` is false