Modules, tests and lawsSlide 43 of 86
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 the compiler 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. Nothing a law reaches may panic, so a law with no assert claims exactly that.
  • Int wraps as it does at run time, so a + 1 > a is refused with a = 9223372036854775807.
  • A law may only reach code that ends: its only loops are for each and counting loops the bounds pass trusts.
  • The compiler never runs what a law reaches. A hive.* call that answers the same for the same arguments is worked out where they are known, and is one unknown where they are not. One that reads or changes the world — the clock, a file, the network — may answer anything its type allows, so the law has to hold whatever it says. async counts as the call itself, and with timeout as both the value and the wait running out.

Only a law may call a law. Calling another 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 else 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, states what every value of the type keeps. Every place that builds one or writes one of its fields 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 also be about a database table, and then every query on it has to carry what the law says. That comes after SQL, 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