Language Reference¶
perlchecker supports a strict subset of Perl designed for SMT-based verification. This page documents every supported construct.
Types¶
Type |
Description |
|---|---|
|
64-bit signed integer |
|
UTF-8 string (bounded to 32 characters in the SMT model) |
|
Ordered sequence of integers |
|
Ordered sequence of strings |
|
String-keyed map with integer values |
|
String-keyed map with string values |
There is no implicit coercion between I64 and Str. Using an operator
that expects one type with a value of the other is a type error.
Variables and Assignment¶
Declaration:
my $x; # declare without initializing
my $x = 42; # declare with initial value
Assignment:
$x = $x + 1;
Compound assignment operators:
+=, -=, *=, /=, %=, **=,
&=, |=, ^=, <<=, >>=, .=
These desugar to the equivalent binary operation. For example,
$x += 1 becomes $x = $x + 1.
Increment and decrement:
$x++ and $x-- desugar to $x = $x + 1 and $x = $x - 1.
Arithmetic Operators¶
Operator |
Example |
Notes |
|---|---|---|
|
|
Addition |
|
|
Subtraction |
|
|
Multiplication |
|
|
Truncating division. Paths where the divisor is 0 are discarded. |
|
|
Modulo. Paths where the divisor is 0 are discarded. |
|
|
Exponentiation (right-associative) |
|
|
Negation |
Comparison Operators¶
Numeric¶
Operator |
Description |
|---|---|
|
Less than |
|
Less than or equal |
|
Greater than |
|
Greater than or equal |
|
Equal |
|
Not equal |
|
Spaceship (three-way compare): returns -1, 0, or 1 |
String¶
Operator |
Description |
|---|---|
|
Equal |
|
Not equal |
|
Less than (lexicographic) |
|
Greater than (lexicographic) |
|
Less than or equal (lexicographic) |
|
Greater than or equal (lexicographic) |
|
String compare: returns -1, 0, or 1 |
Logical Operators¶
Operator |
Description |
|---|---|
|
Logical AND ( |
|
Logical OR ( |
|
Logical NOT ( |
Bitwise Operators¶
Operator |
Example |
Description |
|---|---|---|
|
|
Bitwise AND |
|
|
Bitwise OR |
|
|
Bitwise XOR |
|
|
Bitwise complement |
|
|
Left shift |
|
|
Right shift |
String Operators¶
Operator |
Example |
Description |
|---|---|---|
|
|
Concatenation |
|
|
String repetition (count must be a constant) |
Control Flow¶
if / elsif / else¶
if ($x > 0) {
return 1;
} elsif ($x == 0) {
return 0;
} else {
return -1;
}
unless¶
Equivalent to if with a negated condition:
unless ($x == 0) {
return $x;
}
while¶
while ($x > 0) {
$x = $x - 1;
}
Loops are unrolled to a bounded depth (default: 5 iterations).
Use --max_loop_unroll to adjust.
until¶
Equivalent to while with a negated condition:
until ($x == 0) {
$x = $x - 1;
}
for (C-style)¶
for (my $i = 0; $i < $n; $i++) {
$sum = $sum + $arr[$i];
}
do-while / do-until¶
do {
$x = $x - 1;
} while ($x > 0);
do {
$x = $x - 1;
} until ($x == 0);
Statement Modifiers¶
A statement can be followed by if or unless to make it conditional:
return $x if ($x > 0);
return 0 unless ($found == 1);
$x = $x + 1 if ($flag == 1);
die "negative" if ($x < 0);
die "bad" unless ($x >= 0);
last if ($done == 1);
next unless ($valid == 1);
Loop Control¶
Statement |
Description |
|---|---|
|
Exit the innermost loop (like |
|
Skip to the next iteration (like |
Both support statement modifiers: last if ($cond);,
next unless ($cond);.
Built-in Functions¶
String Functions¶
Signature |
Description |
|---|---|
|
Number of characters |
|
Substring starting at |
|
Substring from |
|
Position of |
|
Same, searching from |
|
Remove trailing newline |
|
Reverse the string |
|
1 if |
|
1 if |
|
1 if |
|
Replace all occurrences of |
|
Character at position |
|
ASCII code of the first character |
|
Character from ASCII code |
Numeric Functions¶
Signature |
Description |
|---|---|
|
Absolute value |
|
Minimum of two integers |
|
Maximum of two integers |
|
Truncate to integer |
Array Functions¶
Signature |
Description |
|---|---|
|
Number of elements in the array |
|
Append |
|
Remove and return the last element |
Array and Hash Operations¶
Array access:
my $val = $arr[$i]; # read element at index $i
$arr[$i] = $new_val; # write element at index $i
Hash access:
my $val = $h{"key"}; # read value for key
$h{$key} = $new_val; # write value for key
Arrays and hashes are modeled using Z3’s Array theory (select/store). Reading a hash key that was never written returns an unconstrained symbolic value.
Function Calls¶
Functions can call other annotated functions in the same file:
my $result = other_function($x, $y);
Calls are inlined during verification. The callee must be defined before the caller in the source file. Recursion is not allowed.
Ternary Operator¶
my $result = ($x > 0) ? $x : -$x;
Nested ternary expressions are supported.
Error and Output Statements¶
Statement |
Description |
|---|---|
|
Terminate execution with an error. If reachable, verification fails. |
|
Alias for |
|
Alias for |
|
No-op for verification |
|
No-op for verification |
|
No-op for verification |
Integer Literals¶
42 # decimal
0xFF # hexadecimal
0o77 # octal
0b1010 # binary
1_000_000 # underscores allowed as visual separators
String Literals¶
Double-quoted (supports escape sequences):
"hello\nworld" # \n, \t, \\, \" supported
Single-quoted (minimal escaping):
'hello' # only \\ and \' are escape sequences
Special Variables¶
These variables have special meaning in annotation expressions and are not user-declarable.
Variable |
Description |
|---|---|
|
The function’s return value (available in |
|
Boolean: true if any arithmetic on the current path overflowed I64 range |
$overflow is typically used in assertions to prove arithmetic safety:
my $sum = $x + $y;
# assert: !$overflow
Without the assertion, arithmetic wraps silently (two’s complement BV64). This section may be extended with additional special variables in the future.