Skip to main content

Arithmetic & Comparisons

FlowLog supports arithmetic expressions and comparison expressions inside rule bodies and heads.

Arithmetic expressions

Arithmetic expressions combine variables and constants with operators. They are parsed left-to-right without precedencex + y * z is evaluated as (x + y) * z.

Operators

Integer operators

These operators apply to integer types (int8, int16, int32, int64):

OperatorDescriptionExample
+Additionx + 1
-Subtractionx - y
*Multiplicationx * 2
/Integer divisionx / 3
%Modulo (remainder)x % 2

String operator

The cat operator concatenates two string values:

OperatorDescriptionExample
catString concatenationx cat y

Usage in rule heads

Arithmetic expressions can appear in the head of a rule:

.decl Arc(src: int32, dst: int32, weight: int32)
.decl DoubleWeight(src: int32, dst: int32, w: int32)

DoubleWeight(x, y, w * 2) :- Arc(x, y, w).

Usage in rule bodies

Arithmetic expressions are used in comparisons within rule bodies:

// Nodes ordered by a composite key
laterChild(pCtr, pN, c2Ctr, c2N) :-
insert(c1Ctr, c1N, pCtr, pN),
insert(c2Ctr, c2N, pCtr, pN),
c1Ctr * 10 + c1N > c2Ctr * 10 + c2N.

Comparison expressions

Comparisons test a condition between two arithmetic expressions. They appear as predicates in rule bodies.

Operators

OperatorDescriptionExample
=Equalx = y
!=Not equalx != y
>Greater thanx > 0
<Less thanx < 100
>=Greater or equalx >= y
<=Less or equalx <= y

Example

.decl Arc(src: int32, dest: int32)
.decl Sg(src: int32, dest: int32)

// Same-generation query: x and y share a common ancestor, and x ≠ y
Sg(x, y) :- Arc(a, x), Arc(a, y), x != y.
Sg(x, y) :- Arc(a, x), Sg(a, b), Arc(b, y).