A number known only when the program runs, as a literal: it takes the
width of what it meets, like 1 in x + 1. v.x.lt(Solve.num(limit))
compares with a U64 variable.
Solve
:= []
Proofs and counterexamples about fixed-width integers. Describe some numbers not yet chosen, state a claim about them, and ask either for values that make the claim true or for a proof that it holds for every value.
import fuzz.Solve
## The usual midpoint. Is it always between its bounds?
expect Solve.prove(
{ lo: Solve.u8, hi: Solve.u8 }.Solve,
|v| {
mid = (v.lo + v.hi) // 2
v.lo.lte(v.hi).implies(Solve.all([v.lo.lte(mid), mid.lte(v.hi)]))
},
) == Ok(Crashes({ lo: 1, hi: 255 }))
The answer covers all 65,536 pairs of bytes, and it says the addition
overflows on lo = 1, hi = 255. Written as v.lo + (v.hi - v.lo) // 2 the
same claim is Proved.
Unknowns, terms and claims
{ lo: Solve.u8, hi: Solve.u8 }.Solve declares two unknowns. Inside the
claim each one is a Uint: a term standing for a number not yet chosen. A
claim is a Claim: a fact about such numbers.
+ - * // % and number literals work on a Uint as they do on a U8.
Comparisons are methods (eq, neq, lt, lte, gt, gte) because
Roc's < must return a Bool, and Solve.all, Solve.any, implies and
Solve.if_else stand in for and, or and if. A function written
without a type annotation, like |lo, hi| lo + (hi - lo) // 2, runs on
U32s and builds a term on Uints, so the code you test and the term you
prove can be one definition.
What a term means
A term means what the same expression means in Roc.
- Every
Uinthas a width in bits, from 1 to 64. Both sides of an operator must have the same width. A literal takes the width of what it meets and must fit in it. +,-and*crash on overflow, and//and%crash on a zero divisor, exactly as Roc's unsigned integers do. The_wrapforms wrap around instead, and the_overflowsforms ask whether the crash would happen.- A
Uintis a row of bits, and the forms above read it as an unsigned number. The_signedforms (lt_signed,plus_signed,plus_overflows_signed,to_width_signed, ...) read the same bits as a two's complement integer, which is whatI8toI64are, and crash where those crash. There is no signed division or remainder. Solve.all,Solve.any,impliesandSolve.if_elseevaluate left to right and stop early likeand,orandif. A guard written first protects what follows:b.neq(0).implies((a // b).lte(a))never divides by zero.
A claim holds on some values when it evaluates to true without crashing.
find looks for values where it holds. prove shows it holds everywhere,
or returns values where it is false (Refuted) or where it crashes
(Crashes), so a proof is also a proof that nothing overflowed.
Sharing
A Roc name is not a name inside a term. After m = a + b, every use of
m puts the whole of a + b into the term again. That is usually
harmless, but a value worked out step by step from the step before, like a
running maximum, then doubles at every step: twenty steps make a term of a
million parts. x.shared() is the fix. It means exactly what x means,
guards and crashes included, and is worked out once wherever it appears.
$highest = Solve.if_else(item.gt($highest), item, $highest).shared()
Shared terms are told apart by a 64-bit fingerprint of their structure. Two different shared terms with the same fingerprint would be taken for one, silently. Nothing checks for it, because checking would mean walking the terms that sharing exists to avoid walking. With a thousand shared terms the chance is about one in 10^13.
How long a query runs
Every search has a budget, counted in conflicts: the dead ends the solver meets and learns from. Conflicts are the solver's own unit of work. They do not depend on the machine or the build, so a question that fits a budget once fits it every time, and a budget is not a timeout.
find_within,find_near_within,prove_withinandenumerate_withintake the budget and answerGaveUp(effort)when it runs out. That says nothing about the claim, only what was spent.find,find_near,proveandenumerateusedefault_budget, a million conflicts, and report running out asErr(GaveUp(effort)).- A search that ends inside its budget is the same search whatever the budget was, so a larger budget never changes an answer.
Multiplication is where questions get hard: proving a * b == b * a takes
about a thousand conflicts at 5 bits, about 900,000 at 10 bits, and more
than the default budget at 16. Comparisons, addition, subtraction,
division by a constant power of two and the bitwise forms stay cheap at 64
bits.
roc test runs on the default backend, which runs the solver thirty to a
hundred and fifty times slower than --opt=speed. Claims that need a few
thousand conflicts are comfortable there. For anything larger use
--opt=speed, or give the test a small budget of its own: a million
conflicts on the default backend is tens of minutes.
What to trust
Every set of values this module returns has been run through a plain
evaluator of terms before it is returned, so Found, Refuted and
Crashes never depend on the solver being right. Proved and None do:
they say the solver found nothing. GaveUp claims nothing. roc-fuzz's own
tests compare the solver with brute-force enumeration at small widths: the
verdicts, the order find_near promises, the answers under a budget, and
shared terms against the same terms written out.
A Bool as a claim: Solve.truth(strict).implies(...) switches part
of a claim on with an ordinary value.
Every claim is true. Later claims are only evaluated while the earlier
ones are true, as with and: Solve.all([v.y.neq(0), (v.x // v.y).eq(3)]).
At least one claim is true. Later claims are only evaluated while the
earlier ones are false, as with or: Solve.any([v.x.eq(7), v.x.eq(12)]).
if_else : Claim, Uint, Uint -> Uint
if condition { then } else { otherwise } on numbers. Only the chosen
side is evaluated: Solve.if_else(v.a.gt(v.b), v.a, v.b) is the larger.
if_else_claim : Claim, Claim, Claim -> Claim
if condition { then } else { otherwise } on claims.
map2 : Vars(a), Vars(b), (a, b -> c) -> Vars(c)
What the record builder { ... }.Solve calls to combine two fields.
It is rarely called by name.
map : Vars(a), (a -> b) -> Vars(b)
Change the value a description gives, for example to a tuple or to a
nominal type. A single unknown needs it to become a record:
Solve.map(Solve.u8, |x| { x: x }).
An unknown of width bits, anywhere from lo to hi inclusive.
A narrow range in a wide type is cheap: Solve.uint_in(64, 0, 999)
behaves like a U64 in arithmetic but gives the solver only ten bits to
choose.
An unknown of width bits, for widths Roc has no type for:
Solve.uint(4) is a number from 0 to 15.
u8 : Vars(Uint)
An unknown that behaves like a U8: { lo: Solve.u8, hi: Solve.u8 }.Solve.
u16 : Vars(Uint)
An unknown that behaves like a U16.
u32 : Vars(Uint)
An unknown that behaves like a U32.
u64 : Vars(Uint)
An unknown that behaves like a U64.
An 8-bit unknown from lo to hi inclusive: Solve.u8_in(1, 100).
A 16-bit unknown from lo to hi inclusive.
A 32-bit unknown from lo to hi inclusive.
A 64-bit unknown from lo to hi inclusive: Solve.u64_in(0, 999_999)
is a counter that stays below a million and costs the solver 20 bits.
count unknowns of the same kind, declared one after another, as a
list: Solve.several(Solve.u8, 8) is eight bytes.
bool : Vars(Claim)
An unknown truth value, a Claim: { urgent: Solve.bool }.Solve.
The budget of the queries that do not take one: a million conflicts, which an optimised build spends in about a minute. When the number was chosen, the hardest proof in roc-fuzz's own tests needed 22,562.
A budget that never runs out, for prove_within and the other
_within queries: the query returns when the solver does, which for a
hard question may be never.
Values that make the claim hold, or None when there are none. Which
values, when there are several, is the solver's choice: the same every
time, but nothing more is promised. find_near promises more.
Solve.find({ x: Solve.u8, y: Solve.u8 }.Solve, |v| Solve.all([v.x.lt(v.y), v.y.lt(v.x)])) == Ok(None)
Gives up after default_budget conflicts with Err(GaveUp(effort)).
find_within : Vars(v), (v -> Claim), U64 -> Try([Found(v), None, GaveUp(Effort)], Problem)
find within a budget of conflicts. GaveUp means the budget ran out
before the solver found values or showed there are none.
Like find, steered towards the proposed values: one per unknown, in
the order the unknowns were declared. A fuzzer's bytes can propose and
the solver repairs what does not fit.
Solve.find_near({ x: Solve.u16, y: Solve.u16 }.Solve, |v| v.x.lt(v.y), [1000, 2000]) == Ok(Found({ x: 1000, y: 2000 }))
The answer is fixed by the claim and the proposals, by this rule. Of all the values that make the claim hold:
- Earlier unknowns win. The first unknown gets the value nearest its proposal that any solution has. The second gets the nearest value it can have beside that one, and so on through the proposals.
- Within a number, high bits win. The nearest value is the one whose highest bit that differs from the proposal is as low as possible; among those, the one whose next differing bit is as low as possible; and so on. In one line:
value xor proposal, read as a number, is as small as it can be.
What follows from the rule:
- Proposals that already make the claim hold come back unchanged, and so does any leading part of them that some solution agrees with.
- For an unknown whose range starts at 0, proposing 0 gives the smallest value there is: all zeros give the smallest first unknown, then the smallest second unknown beside it, and so on. For an unknown that uses its whole width, proposing its largest number gives the largest. Neither holds for other ranges: there the bits of the end of the range decide.
- Each answer keeps the longest run of leading bits of its proposal that it can. In numbers: it lies in the smallest aligned power-of-two block around the proposal that holds a value the unknown can still take.
- It is not the solution at the smallest distance. With only 7 and 12 possible, a proposal of 8 gives 12, which shares its top bit, though 7 is one away.
A proposal outside an unknown's range is first moved to the nearest end
of the range. A truth value is proposed as 0 or anything else. Unknowns
past the end of the proposals are the solver's choice, as in find.
The solver has to settle the proposed bits in this order, so a steered
search can take many times longer than find, or less. Gives up after
default_budget conflicts with Err(GaveUp(effort)).
find_near_within : Vars(v), (v -> Claim), List(U64), U64 -> Try([Found(v), None, GaveUp(Effort)], Problem)
find_near within a budget of conflicts.
Does the claim hold for every value of its unknowns?
Proved means it does. Refuted carries values that make it false,
and Crashes values on which evaluating it overflows or divides by
zero. The example at the top of this module is a Crashes.
Gives up after default_budget conflicts with Err(GaveUp(effort)):
the claim is then neither proved nor refuted.
prove_within : Vars(v), (v -> Claim), U64 -> Try([Proved, Refuted(v), Crashes(v), GaveUp(Effort)], Problem)
prove within a budget of conflicts. GaveUp means the budget ran out
first: nothing is known about the claim, and effort says what was
spent. The budget is exact: the solver stops at the conflict that uses
it up, so effort.conflicts is the budget.
Solve.prove_within({ a: Solve.uint(10), b: Solve.uint(10) }.Solve, |v| v.a.times_wrap(v.b).eq(v.b.times_wrap(v.a)), 1_000)
gives Ok(GaveUp({ conflicts: 1000, .. })) quickly. The whole proof
takes 913,801 conflicts, about forty seconds of an optimised build.
Up to limit different sets of values that make the claim hold. Fewer
than limit means these are all there are.
Solve.enumerate({ x: Solve.uint(4), y: Solve.uint(4) }.Solve, |v| (v.x * v.y).eq(12), 100)
lists the six ways to make 12 from two 4-bit numbers. Gives up after
default_budget conflicts in all with Err(GaveUp(effort)).
enumerate_within : Vars(v), (v -> Claim), U64, U64 -> Try([Listed(List(v)), GaveUp(List(v), Effort)], Problem)
enumerate within a budget of conflicts for the whole list. GaveUp
carries the sets found before the budget ran out, which may not be all.
Build the claim's formula and load it into a solver. Every answer ask
gives from it makes the claim hold.
ordered = Solve.prepare({ x: Solve.u8, y: Solve.u8 }.Solve, |v| v.x.lt(v.y))
prepare_with : Vars(v), (v -> Claim), (v -> List(Claim)) -> Try(Prepared(v), Problem)
prepare, with further claims over the same unknowns that each query
may switch on by their positions in the list. They are built into the
formula once and cost a query nothing until it assumes them.
Solve.prepare_with({ x: Solve.u8, y: Solve.u8 }.Solve, |v| v.x.lt(v.y), |v| [(v.x + v.y).eq(100)])
Values that make a prepared claim hold, and every assumed extra claim
with it. The promises of find_near hold among the values that do.
None means no values satisfy the claim together with these extras.
Solve.ask(prepared, { near: [60, 60], assuming: [0], within: 1000 }) == Ok(Found({ x: 48, y: 52 }))
Every query starts from the claim as it was prepared: no query sees what another one did, so the same query always gives the same answer.
Run the claim on ordinary values, one per unknown in declaration order (0 or 1 for a truth value). This is the plain evaluator: no solver is involved, so it is the way to test a term against real Roc code.
Solve.check({ x: Solve.u8, y: Solve.u8 }.Solve, |v| (v.x + v.y).eq(44), [200, 100]) == Err(Overflow)
Err(Overflow) and Err(DivByZero) mean the claim crashes on these
values. Declared ranges are not enforced here.
measure : Vars(v), (v -> Claim) -> Try({ unknowns : U64, bits : U64, variables : U64, clauses : U64 }, Problem)
How large the problem handed to the solver is: the unknowns declared, the bits the solver is free to choose, and the variables and clauses of the formula. A formula of a few thousand clauses loads in milliseconds; the number of clauses says little about how long a search takes.
What answering the question once costs, for choosing a budget: Find
asks what find asks and Prove what prove asks. Gives up after
default_budget conflicts like those do.
Solve.effort({ a: Solve.uint(5), b: Solve.uint(5) }.Solve, |v| v.a.times_wrap(v.b).eq(v.b.times_wrap(v.a)), Prove)
Uint
Solve.Uint :: # (opaque)
A number not yet chosen, or a term built from such numbers: a row of
bits of a fixed width, read as an unsigned number except by the
_signed forms. Unknowns come from declarations like Solve.u32, and
known numbers from literals and the from_ functions.
Solve.find(Solve.map(Solve.u8, |x| { x: x }), |v| (v.x * 3).eq(51)) == Ok(Found({ x: 17 }))
from_numeral : Numeral -> Try(Uint, [InvalidNumeral(Str)])
Lets a number literal stand for a Uint, as in v.x + 1. The
literal takes the width of the term it meets.
A U8 as a term 8 bits wide: Solve.Uint.from_u8(250) + 6
crashes, as 250.U8 + 6 does.
A U16 as a term 16 bits wide.
A U32 as a term 32 bits wide.
A U64 as a term 64 bits wide.
A number as a term of the given width in bits, for widths Roc has
no type for: Solve.Uint.of_width(5, 4) is 5 in four bits.
An I8 as its 8 bits: Solve.Uint.from_i8(-56).value() == 200.
An I16 as its 16 bits.
An I32 as its 32 bits.
An I64 as its 64 bits.
plus : Uint, Uint -> Uint
a + b. Crashes when the sum does not fit the width.
minus : Uint, Uint -> Uint
a - b. Crashes when b is larger than a.
times : Uint, Uint -> Uint
a * b. Crashes when the product does not fit the width. A product
of two unknowns is the costly part of most questions: see "How long
a query runs" at the top of this module.
div_trunc_by : Uint, Uint -> Uint
a // b. Crashes when b is zero. A divisor that is a constant
power of two costs nothing; any other divisor costs a multiplier.
rem_by : Uint, Uint -> Uint
a % b. Crashes when b is zero.
plus_wrap : Uint, Uint -> Uint
a + b wrapping around, as U8.plus_wrap does: never crashes.
minus_wrap : Uint, Uint -> Uint
a - b wrapping around below zero: never crashes.
times_wrap : Uint, Uint -> Uint
a * b keeping the low bits of the product: never crashes.
plus_overflows : Uint, Uint -> Claim
Would a + b crash? a.plus_overflows(b).not().implies(...) is a
claim about the sum that only looks at sums that fit.
minus_overflows : Uint, Uint -> Claim
Would a - b crash? True exactly when b is larger than a.
times_overflows : Uint, Uint -> Claim
Would a * b crash?
bitwise_and : Uint, Uint -> Uint
The bits set in both, as U32.bitwise_and does.
bitwise_or : Uint, Uint -> Uint
The bits set in either.
bitwise_xor : Uint, Uint -> Uint
The bits set in exactly one.
bitwise_not : Uint -> Uint
Every bit flipped.
Shift left by a fixed count, taken modulo the width as in Roc:
x.shl_wrap(3) is x * 8 keeping the low bits.
Shift right by a fixed count, taken modulo the width as in Roc:
x.shr_wrap(1) is x // 2.
The same number at another width. Crashes when it does not fit.
Widening first is how to compute with a bit to spare:
(a.to_width(33) + b.to_width(33)) // 2 never overflows.
to_width_wrap : Uint, U8 -> Uint
The low bits of the number at another width, as U32.to_u8_wrap
does.
shared : Uint -> Uint
The same number, to be worked out once however often it is used. See "Sharing" at the top of this module.
plus_signed : Uint, Uint -> Uint
a + b on signed integers. Crashes when the sum does not fit the
width as a signed number, as I32's + does.
minus_signed : Uint, Uint -> Uint
a - b on signed integers. Crashes when the difference does not
fit: Solve.num(0).minus_signed(x) is -x, and crashes at the
lowest number of the width.
times_signed : Uint, Uint -> Uint
a * b on signed integers. Crashes when the product does not fit.
Built from a multiplier of twice the width, so it costs several
times what times costs.
plus_overflows_signed : Uint, Uint -> Claim
Would a + b on signed integers crash?
minus_overflows_signed : Uint, Uint -> Claim
Would a - b on signed integers crash?
times_overflows_signed : Uint, Uint -> Claim
Would a * b on signed integers crash?
to_width_signed : Uint, U8 -> Uint
The same signed number at another width. Widening copies the sign
bit, as I32.to_i64 does. Narrowing crashes when it does not fit;
to_width_wrap keeps the low bits instead, as to_i8_wrap does.
lt_signed : Uint, Uint -> Claim
a < b on signed integers: 200 in 8 bits is -56, so it is less
than 3 here and greater than 3 under lt.
lte_signed : Uint, Uint -> Claim
a <= b on signed integers.
gt_signed : Uint, Uint -> Claim
a > b on signed integers.
gte_signed : Uint, Uint -> Claim
a >= b on signed integers.
eq : Uint, Uint -> Claim
a == b, as a claim: (v.x + v.y).eq(44).
neq : Uint, Uint -> Claim
a != b, as a claim.
lt : Uint, Uint -> Claim
a < b, as a claim.
lte : Uint, Uint -> Claim
a <= b, as a claim.
gt : Uint, Uint -> Claim
a > b, as a claim.
gte : Uint, Uint -> Claim
a >= b, as a claim.
Run a term that has no unknowns left in it:
(Solve.Uint.from_u8(250) + 6).eval() == Err(Overflow).
The number a solved unknown stands for. The records inside Found,
Refuted and Crashes hold nothing but such numbers, so
v.x.value() is how to take them out as U64s.
Crashes on a term that still has unknowns in it or that overflows;
use eval when that can happen.
eval_signed : Uint -> Try(I64, Problem)
eval, with the number read as a signed integer of its width.
value_signed : Uint -> I64
value, with the number read as a signed integer of its width: a
solved 8-bit unknown holding 255 is -1. Crashes like value.
Two terms are equal when they are the same term. A solved number
equals a literal of the same value, so an expected answer can be
written Ok(Refuted({ awaited: 65_536, frame: 0 })).
to_inspect : Uint -> Str
How Str.inspect and a failing expect show a term: a solved
number as its digits.
Claim
Solve.Claim :: # (opaque)
A fact about numbers not yet chosen, built from comparisons such as
v.lo.lte(v.hi) and combined with implies, Solve.all and the
rest. A claim is what find and prove take, and an unknown declared
with Solve.bool is one too.
Two claims are equal when they are the same term. A solved truth
value equals Solve.truth of the same Bool.
not : Claim -> Claim
!a.
implies : Claim, Claim -> Claim
!a or b: whenever a is true, b is too. b is only evaluated
when a is true, so v.y.neq(0).implies((v.x // v.y).lte(v.x))
never divides by zero.
iff : Claim, Claim -> Claim
Both are true or both are false: trick(x).iff(definition(x))
says two ways of stating a rule agree.
both : Claim, Claim -> Claim
a and b. Solve.all takes any number of claims.
either : Claim, Claim -> Claim
a or b. Solve.any takes any number of claims.
shared : Claim -> Claim
The same claim, to be worked out once however often it is used. See "Sharing" at the top of this module.
Run a claim that has no unknowns left in it:
Solve.Uint.from_u8(3).lt(4).eval() == Ok(Bool.True).
The answer a solved truth value stands for: v.urgent.value() on
a Found record. Crashes like Uint.value.
to_inspect : Claim -> Str
How Str.inspect and a failing expect show a claim: a solved
truth value as True or False.
Problem : [
Overflow,
DivByZero,
WidthMismatch({ left : U8, right : U8 }),
LiteralTooLarge({ literal : U64, width : U8 }),
BadWidth(U8),
BadRange({ lo : U64, hi : U64, width : U8 }),
Unbound(U64),
NoSuchExtra(U64),
Malformed,
GaveUp(Effort),
Solver([ZeroLiteral, LiteralOutOfRange(I32), UnknownVariable(I32), VariableLimit]),
Disagreement,
]
Why a query could not be answered.
GaveUpmeans a query without a budget of its own spentdefault_budgetand stopped: the claim is neither proved nor refuted, and the record says what was spent.OverflowandDivByZerocome only from running a term on values (check,eval): Roc would crash there.WidthMismatch,LiteralTooLarge,BadWidth,BadRange,UnboundandMalformedsay the terms themselves are ill-formed, whatever the values:v.x.lt(v.y)with an 8-bitxand a 16-bityisWidthMismatch({ left: 8, right: 16 }).NoSuchExtrameans anaskassumed an extra claim the prepared claim does not have.SolverandDisagreementare bugs in roc-fuzz, not in the claim.
Vars
Solve.Vars(v) :: # (opaque)
A description of the unknowns of a problem, giving a value of type v
to state claims about. Combine several with the record builder:
{ start: Solve.u16, count: Solve.u16_in(1, 512), urgent: Solve.bool }.Solve
The same description hands the claim a record of unknowns, and hands
back the same record with numbers filled in inside Found, Refuted
and Crashes. Unknowns are numbered in the order the fields are
written, which is the order find_near and check take values in.
What a search cost, in the solver's own counters. The counters depend
only on the question, never on the machine or the build, so a budget
that is enough once is enough every time. conflicts is what a budget
counts.
Prepared
Solve.Prepared(v) :: # (opaque)
A claim built once, to be asked any number of times with ask.
Building the formula and loading it into a solver is most of the cost
of a find_near, so a generator that is called again and again should
hold one of these, in a top-level constant: the compiler works it out
once, when the program is built.
One query of a prepared claim: proposals to steer towards as in
find_near, the extra claims that must hold as well, by their
positions in the list given to prepare_with, and a budget of
conflicts.