Prove the semver and solver kernel instead of testing it
Rolling Release / build (push) Successful in 1m39s
Rolling Release / publish (push) Successful in 8s

Move number parsing, version ordering and constraints, version sorting and
solver literals into src/kernel, with contracts checked by moon prove under
overflow-checked integers. Replace the semver behaviour tests with those
proofs, keep tests for string parsing, and run the proofs in CI.

Version numbers are now 64-bit: the old parser silently wrapped at 2^31.
Drop pick_tag, which only tests called.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
nandiandClaude Opus 5.5 committed 2026-11-30 16:56:21 -08:00
1 parent 2d815bbb75
commit 83e694c6c4
16 files changed
+874 -330

No files matched your search

+33 -75
View File
@@ -46,14 +46,6 @@ struct SolverProvider {
locked_bins : Map[String, String]
}
///|
#derive(Eq)
enum Val {
Unassigned
True
False
}
///|
enum ClauseOrigin {
Root(String, String, String?, String)
@@ -88,7 +80,7 @@ struct SatSolver {
pkg_index : Map[String, Int32]
clauses : Array[Clause]
watches : Array[Array[Int32]]
assign : Array[Val]
assign : Array[@kernel.Val]
level : Array[Int32]
reason : Array[Int32]
trail : Array[Int32]
@@ -102,35 +94,6 @@ struct SatSolver {
// ---------------------------------------------------------------- helpers
///|
fn pos_lit(v : Int32) -> Int32 {
v * 2
}
///|
fn neg_lit(v : Int32) -> Int32 {
v * 2 + 1
}
///|
fn lit_var(l : Int32) -> Int32 {
l / 2
}
///|
fn lit_is_neg(l : Int32) -> Bool {
(l % 2) != 0
}
///|
fn lit_neg(l : Int32) -> Int32 {
if (l % 2) == 0 {
l + 1
} else {
l - 1
}
}
///|
fn swap_idx(a : Array[Int32], i : Int32, j : Int32) -> Unit {
if i != j {
@@ -289,19 +252,14 @@ fn current_level(s : SatSolver) -> Int32 {
}
///|
fn lit_val(s : SatSolver, l : Int32) -> Val {
let v = lit_var(l)
match s.assign[v] {
Unassigned => Unassigned
True => if lit_is_neg(l) { False } else { True }
False => if lit_is_neg(l) { True } else { False }
}
fn lit_val(s : SatSolver, l : Int32) -> @kernel.Val {
@kernel.lit_val(s.assign[@kernel.lit_var(l)], l)
}
///|
fn enqueue(s : SatSolver, l : Int32, reason_cid : Int32) -> Unit {
let v = lit_var(l)
s.assign[v] = if lit_is_neg(l) { False } else { True }
let v = @kernel.lit_var(l)
s.assign[v] = if @kernel.lit_is_neg(l) { False } else { True }
s.level[v] = current_level(s)
s.reason[v] = reason_cid
s.trail.push(l)
@@ -315,7 +273,7 @@ fn backtrack(s : SatSolver, target_lvl : Int32) -> Unit {
let stop = s.trail_lim[target_lvl]
while s.trail.length() > stop {
let p = s.trail.pop().unwrap()
let v = lit_var(p)
let v = @kernel.lit_var(p)
s.assign[v] = Unassigned
s.level[v] = -1
s.reason[v] = -1
@@ -332,8 +290,8 @@ fn backtrack(s : SatSolver, target_lvl : Int32) -> Unit {
fn attach_watches(s : SatSolver, cid : Int32) -> Unit {
let lits = s.clauses[cid].lits
if lits.length() >= 2 {
s.watches[lit_neg(lits[0])].push(cid)
s.watches[lit_neg(lits[1])].push(cid)
s.watches[@kernel.lit_neg(lits[0])].push(cid)
s.watches[@kernel.lit_neg(lits[1])].push(cid)
}
}
@@ -345,7 +303,7 @@ fn add_clause_dynamic(
) -> Int32 {
let lits : Array[Int32] = []
for l in raw_lits {
if lits.contains(lit_neg(l)) {
if lits.contains(@kernel.lit_neg(l)) {
return -1 // tautology
}
if !lits.contains(l) {
@@ -368,12 +326,12 @@ fn add_clause_dynamic(
for i = 0; i < n; i = i + 1 {
match lit_val(s, lits[i]) {
True =>
if true_idx < 0 || s.level[lit_var(lits[i])] < s.level[lit_var(lits[true_idx])] {
if true_idx < 0 || s.level[@kernel.lit_var(lits[i])] < s.level[@kernel.lit_var(lits[true_idx])] {
true_idx = i
}
Unassigned => unassigned.push(i)
False => {
let lvl = s.level[lit_var(lits[i])]
let lvl = s.level[@kernel.lit_var(lits[i])]
if max_false_idx < 0 || lvl > max_false_lvl {
second_false_idx = max_false_idx
second_false_lvl = max_false_lvl
@@ -561,7 +519,7 @@ async fn ensure_pkg_channel(
ignore(
add_clause_dynamic(
s,
[neg_lit(w), neg_lit(u)],
[@kernel.neg_lit(w), @kernel.neg_lit(u)],
AtMostOne(pkg.name),
),
)
@@ -582,21 +540,21 @@ fn propagate(s : SatSolver) -> Int32 {
let p = s.trail[s.qhead]
s.qhead = s.qhead + 1
// Lazy At-Most-One when candidate `v` is assigned True.
if !lit_is_neg(p) {
let v = lit_var(p)
if !@kernel.lit_is_neg(p) {
let v = @kernel.lit_var(p)
let pkg = s.pkgs[s.var_pkg[v]]
for other in pkg.all_vars {
if other != v && s.assign[other] != False {
let cid = s.clauses.length()
s.clauses.push(Clause::{
lits: [neg_lit(other), neg_lit(v)],
lits: [@kernel.neg_lit(other), @kernel.neg_lit(v)],
origin: AtMostOne(pkg.name),
})
attach_watches(s, cid)
if s.assign[other] == True {
return cid
}
enqueue(s, neg_lit(other), cid)
enqueue(s, @kernel.neg_lit(other), cid)
}
}
}
@@ -604,7 +562,7 @@ fn propagate(s : SatSolver) -> Int32 {
// Watch lists store clauses keyed by `lit_neg(watched_lit)`, so when `p`
// becomes True, `lit_neg(p)` became False and watches[p] must be updated.
let ws = s.watches[p]
let false_lit = lit_neg(p)
let false_lit = @kernel.lit_neg(p)
let mut i : Int32 = 0
let mut j : Int32 = 0
let n = ws.length()
@@ -627,7 +585,7 @@ fn propagate(s : SatSolver) -> Int32 {
if !(lit_val(s, lits[k]) is False) {
lits[1] = lits[k]
lits[k] = false_lit
s.watches[lit_neg(lits[1])].push(cid)
s.watches[@kernel.lit_neg(lits[1])].push(cid)
found = true
break
}
@@ -674,7 +632,7 @@ fn analyze_conflict(
let process_clause = fn(cid : Int32, skip_var : Int32) {
for q in s.clauses[cid].lits {
let qv = lit_var(q)
let qv = @kernel.lit_var(q)
if qv != skip_var && !seen[qv] {
seen[qv] = true
if s.level[qv] == cur_lvl {
@@ -693,7 +651,7 @@ fn analyze_conflict(
let mut p : Int32 = -1
while idx >= 0 {
let t = s.trail[idx]
let v = lit_var(t)
let v = @kernel.lit_var(t)
if seen[v] {
seen[v] = false
counter = counter - 1
@@ -709,13 +667,13 @@ fn analyze_conflict(
}
idx = idx - 1
}
learned[0] = lit_neg(p)
learned[0] = @kernel.lit_neg(p)
let mut bt_level : Int32 = 0
if learned.length() > 1 {
let mut best : Int32 = 1
let mut best_lvl = s.level[lit_var(learned[1])]
let mut best_lvl = s.level[@kernel.lit_var(learned[1])]
for k = 2; k < learned.length(); k = k + 1 {
let lvl = s.level[lit_var(learned[k])]
let lvl = s.level[@kernel.lit_var(learned[k])]
if lvl > best_lvl {
best_lvl = lvl
best = k
@@ -733,7 +691,7 @@ async fn add_req_clause(s : SatSolver, req : SolveReq) -> Int32 raise OrbitError
let matches = matching_vars(s, var_ids, req.constraint)
let lits : Array[Int32] = []
for vid in matches {
lits.push(pos_lit(vid))
lits.push(@kernel.pos_lit(vid))
}
add_clause_dynamic(
s,
@@ -745,8 +703,8 @@ async fn add_req_clause(s : SatSolver, req : SolveReq) -> Int32 raise OrbitError
///|
fn first_unexpanded_true(s : SatSolver) -> Int32? {
for t in s.trail {
if !lit_is_neg(t) {
let v = lit_var(t)
if !@kernel.lit_is_neg(t) {
let v = @kernel.lit_var(t)
if !s.expanded[v] {
return Some(v)
}
@@ -781,9 +739,9 @@ async fn expand_next(s : SatSolver) -> (Bool, Int32) raise OrbitError {
let (dep_pin, dep_name) = parse_dep_key(dep_key)
let (_, var_ids) = ensure_pkg_channel(s, dep_name, dep_pin)
let matches = matching_vars(s, var_ids, dep_con)
let lits : Array[Int32] = [neg_lit(v)]
let lits : Array[Int32] = [@kernel.neg_lit(v)]
for mvid in matches {
lits.push(pos_lit(mvid))
lits.push(@kernel.pos_lit(mvid))
}
let c = add_clause_dynamic(
s,
@@ -802,7 +760,7 @@ async fn expand_next(s : SatSolver) -> (Bool, Int32) raise OrbitError {
if owner != cand.name && !s.pkg_index.contains(owner) {
let c = add_clause_dynamic(
s,
[neg_lit(v)],
[@kernel.neg_lit(v)],
LockedBinConflict(bn, v, owner),
)
record_confl(c)
@@ -821,7 +779,7 @@ async fn expand_next(s : SatSolver) -> (Bool, Int32) raise OrbitError {
if s.var_pkg[u] != s.var_pkg[v] {
let c = add_clause_dynamic(
s,
[neg_lit(v), neg_lit(u)],
[@kernel.neg_lit(v), @kernel.neg_lit(u)],
BinConflict(bn, v, u),
)
record_confl(c)
@@ -862,7 +820,7 @@ fn pick_decision(s : SatSolver) -> Int32? {
break
}
if val == Unassigned {
if lit_is_neg(l) {
if @kernel.lit_is_neg(l) {
blocked_by_guard = true
break
}
@@ -944,7 +902,7 @@ fn explain_unsat(s : SatSolver, confl : Int32) -> String {
}
}
for l in cl.lits {
let v = lit_var(l)
let v = @kernel.lit_var(l)
if s.assign[v] != Unassigned && s.level[v] == 0 {
let r = s.reason[v]
if r >= 0 && r < visited.length() && !visited[r] {
@@ -1025,7 +983,7 @@ async fn solve(
}
let lits : Array[Int32] = []
for vid in matches {
lits.push(pos_lit(vid))
lits.push(@kernel.pos_lit(vid))
}
let c = add_clause_dynamic(
s,