How a Semantifyr Model Compiles
A Semantifyr model describes a system by its structure: instances that contain other instances, and transitions that call other transitions. A backend checks a flat model: variables, an init transition, a main transition, and a property, without classes or instances. The compiler turns a class into such a model.
It takes three phases. Instantiation puts one instance of the class into the universe of the global features, and inflates it into an instance tree. Inlining expands the init, the main transition, and the property of that instance into one of each, replacing every call and inline operation with what it stands for. Last, the compiler flattens the inlined model into a flat model.
The compiler evaluates every compile-time expression during these phases, separately for each instance, and leaves the rest to the backend (Compile Time and Run Time). Any class can be compiled. A verification case is a class that is compiled and then verified.
This page follows one model. Its verification case, BothLampsCanBeOn, asks whether both lamps of a hall can be on at the same time:
class Fuse {
var ok: bool := true
}
class Lamp {
@Shared
contains fuse: Fuse
var on: bool := false
tran toggle() {
assume(fuse.ok)
on := !on
}
prop isOn(): bool {
return on
}
}
class Spot : Lamp {
redefine tran toggle() {
assume(fuse.ok)
on := true
}
}
class Room {
contains top: Lamp
contains bottom: Lamp
var last: Lamp[0..1] := nothing
redefine tran {
choice {
assume(last != top)
inline top.toggle()
last := top
} or {
assume(last != bottom)
inline bottom.toggle()
last := bottom
}
}
}
class Hall : Room {
redefine contains bottom: Spot
}
@VerificationCase
class BothLampsCanBeOn {
contains hall: Hall
redefine tran {
inline hall.main()
}
redefine prop {
return EF hall.top.isOn() && hall.bottom.isOn()
}
}
Instantiation
Instantiation creates the instances of the model. It always starts from one class, the root class, and any class can be the root class.
The universe holds one instance of the root class, the root instance, as a feature named root. Beside it are the global features, the ones declared outside any class, which every class can use by name.
Next, the compiler inflates the root instance: it creates the parts of each instance, and the parts of those parts, until none is left. Each contains of multiplicity [1] creates one instance of its type. If a subclass redefines a containment with a more specific type, the instance has that type. A containment marked @Shared creates a single instance, shared by every instance that holds it.
In the room model, the root gets a hall, and the hall gets its top and bottom lamps. root.hall.top is a Lamp, and root.hall.bottom is a Spot, because Hall redefines bottom. Both lamps share one fuse:
No other feature creates instances. A contains of any other multiplicity holds the instances of the features that subset it. A refers gets its instances from its binding or its subsetters, and a derived refers from its opposite. So a Shelf has exactly two items:
class Item
class Shelf {
contains items: Item[0..*] // holds first and second
contains first: Item subsets items
contains second: Item subsets items
}
Inlining
Inlining expands the model of the instance tree. It starts from the init, the main transition, and the property of the root instance, and replaces every compile-time construct with what it stands for. What remains is one init, one main transition, and one property over the instances of the tree.
An inline call is replaced by the body of the called transition, and a property call by the expression the property returns. The body comes from the class of the called instance, so a redefinition replaces what it redefines. The names in the body become paths from the root instance, such as hall.top.on.
An inline if keeps the branch its condition selects, and an inline for repeats its body for each instance or value of its range. A call through a feature-typed variable becomes a choice, with one branch for each instance the variable can hold. A bound feature used as a value is replaced by its value.
Each instance gets its own result, so two instances of one class can behave differently. When the compiler compares instances with ==, it compares sets: the two sides are equal when they hold the same instances, and nothing is the empty set.
In the room model, the main transition of BothLampsCanBeOn only calls the main transition of its hall:
redefine tran {
inline hall.main()
}
After inlining, the call is replaced by the main transition of the hall, and its calls by the bodies of toggle. The bottom lamp is a Spot, so its branch gets the body of Spot:
redefine tran {
choice {
assume(hall.last != hall.top)
assume(hall.top.fuse.ok)
hall.top.on := !hall.top.on
hall.last := hall.top
} or {
assume(hall.last != hall.bottom)
assume(hall.bottom.fuse.ok)
hall.bottom.on := true
hall.last := hall.bottom
}
}
After inlining, the verification property must have AG or EF at its root, or compiling it is an error.
Flattening
Finally, the compiler flattens the inlined model into a model without instances. This is the model a backend receives. Before model checking, the backend checks what it supports on this flat model, so a model that it cannot handle, such as one with a string variable, is refused.
To check the intermediate phases yourself, run semantifyr compile --artifacts=debug.