Prove the semver and solver kernel instead of testing it
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:
1 parent
2d815bbb75
commit
83e694c6c4
16 files changed
+874
-330
No files matched your search
@@ -16,6 +16,8 @@ permissions:
|
||||
|
||||
env:
|
||||
MOONBIT_VERSION: 0.10.20+65efd5b93
|
||||
Z3_VERSION: 5.3.3
|
||||
Z3_SHA256: d56f60b06f432f33de5810f7f1a384a339131f361e2d54d08ea83eea14326880
|
||||
ZIG_VERSION: 0.17.1
|
||||
ZIG_SHA256: 3581863e3cbcfd0fce0efc682ce7e9e93da96668a977791d356a26d2a7e9b17a
|
||||
|
||||
@@ -55,6 +57,20 @@ jobs:
|
||||
- name: Test
|
||||
run: moon test --target native
|
||||
|
||||
# Proofs replace tests of src/kernel. The override checks for overflow;
|
||||
# `just prove` runs the same command.
|
||||
- name: Prove
|
||||
run: |
|
||||
z3="z3-$Z3_VERSION-x64-glibc-2.39"
|
||||
curl -fsSL --retry 5 -o "$RUNNER_TEMP/z3.zip" \
|
||||
"https://github.com/Z3Prover/z3/releases/download/z3-$Z3_VERSION/$z3.zip"
|
||||
echo "$Z3_SHA256 $RUNNER_TEMP/z3.zip" | sha256sum -c -
|
||||
python3 -m zipfile -e "$RUNNER_TEMP/z3.zip" "$RUNNER_TEMP"
|
||||
chmod +x "$RUNNER_TEMP/$z3/bin/z3"
|
||||
export Z3PATH="$RUNNER_TEMP/$z3/bin/z3"
|
||||
export MOON_PROVE_PRELUDE_OVERRIDE="$HOME/.moon/lib/prelude_proof_machine_int"
|
||||
moon prove src/kernel
|
||||
|
||||
- name: Build static binary
|
||||
run: scripts/build-musl.sh
|
||||
|
||||
|
||||
@@ -172,7 +172,8 @@ linked-in functions instead.
|
||||
|
||||
```sh
|
||||
moon build --target native --release
|
||||
moon test --target native # semver / manifest unit tests
|
||||
moon test --target native # unit and mock-registry tests
|
||||
just prove # proofs of src/kernel (needs an SMT solver: z3, cvc5 or alt-ergo)
|
||||
# the binary is _build/native/release/build/main/main.exe
|
||||
```
|
||||
|
||||
@@ -213,6 +214,10 @@ orbit upgrade orbit
|
||||
|
||||
## Layout
|
||||
|
||||
`src/kernel/` holds the code whose behaviour is proved rather than tested: number
|
||||
parsing, version ordering and constraints, sorting, and solver literals. See its
|
||||
[README](src/kernel/README.md).
|
||||
|
||||
```
|
||||
src/main/
|
||||
stub.c C glue: time, termios echo toggle, getenv, symlink, stderr, exit
|
||||
@@ -220,7 +225,7 @@ src/main/
|
||||
sha256.mbt streaming FIPS 180-4 SHA-256 (+ tests)
|
||||
auth.mbt credentials store (~/.orbit/auth.json + Docker config fallback)
|
||||
oci.mbt OCI Distribution v2 HTTP transport, Bearer/Basic auth, redirects
|
||||
semver.mbt versions, relational/compound constraints, tag selection (+ tests)
|
||||
semver.mbt versions, relational/compound constraints, parsing over src/kernel (+ tests)
|
||||
model.mbt orbit.json / state.json / config.json (+ tests)
|
||||
registry.mbt OCI registry ops: tags, resolve, cached manifests, fetch, publish, login
|
||||
solver.mbt lazy Resolvo-style CDCL SAT solver & UNSAT explainer (+ tests)
|
||||
|
||||
@@ -14,6 +14,10 @@ build-musl:
|
||||
test:
|
||||
moon test --target native
|
||||
|
||||
# Prove src/kernel with overflow-checked integers (needs z3, cvc5 or alt-ergo).
|
||||
prove:
|
||||
MOON_PROVE_PRELUDE_OVERRIDE="${MOON_HOME:-$HOME/.moon}/lib/prelude_proof_machine_int" moon prove src/kernel
|
||||
|
||||
# Install Orbit to ~/.local/bin and set up its package PATH for Bash.
|
||||
install: build
|
||||
install -Dm755 _build/native/release/build/main/main.exe "$HOME/.local/bin/orbit"
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
# kernel
|
||||
|
||||
The proved part of Orbit. `src/main` runs this code, so the proofs replace tests
|
||||
of it. There are no axioms. Contracts are in `.mbt`, specifications in `.mbtp`.
|
||||
|
||||
| File | Proved |
|
||||
|---|---|
|
||||
| `nat` | The result is the decimal value, or -1 for an empty string, a non-digit, or 10^18 and up. Nothing overflows. |
|
||||
| `version` | `compare` is a total order, and each constraint operator accepts exactly its interval. |
|
||||
| `sort` | `sort_desc` terminates and lists every index once, highest version first and stable. |
|
||||
| `lit` | Literals round trip to their variable and sign, and a literal and its complement never agree. |
|
||||
|
||||
Run `just prove`. It selects the machine-integer prelude. Plain `moon prove`
|
||||
models integers as unbounded, so it would miss overflow.
|
||||
|
||||
Not proved, and still tested in `src/main`: splitting strings, ranking
|
||||
prereleases (`Key.pre`), and anything that does I/O. The prover treats strings
|
||||
as opaque. `src/main` is not proof-enabled, so its calls must meet the
|
||||
`proof_require` clauses by themselves.
|
||||
|
||||
**Writing specifications:** `moonc-prove` 0.10.20 drops the parentheses around an
|
||||
implication, so `(a → b) && (c → d)` is read as `((a → b) && c) → d`. Never put
|
||||
`→` or a quantifier under `&&` or `||`. Give it its own predicate, or use `==`.
|
||||
After changing a proof, break the code on purpose and check that it fails.
|
||||
@@ -0,0 +1,80 @@
|
||||
///|
|
||||
#derive(Eq)
|
||||
pub(all) enum Val {
|
||||
Unassigned
|
||||
True
|
||||
False
|
||||
}
|
||||
|
||||
///|
|
||||
pub extend Val with Eq::{equal, not_equal}
|
||||
|
||||
///|
|
||||
pub fn pos_lit(
|
||||
v : Int32,
|
||||
) -> Int32 where {
|
||||
proof_require: var_ok(v),
|
||||
proof_ensure: r => 0 <= r && r / 2 == v && r % 2 == 0,
|
||||
} {
|
||||
v * 2
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn neg_lit(
|
||||
v : Int32,
|
||||
) -> Int32 where {
|
||||
proof_require: var_ok(v),
|
||||
proof_ensure: r => 0 <= r && r / 2 == v && r % 2 == 1,
|
||||
} {
|
||||
v * 2 + 1
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn lit_var(
|
||||
l : Int32,
|
||||
) -> Int32 where {
|
||||
proof_require: 0 <= l,
|
||||
proof_ensure: r => 0 <= r && (l == r * 2 || l == r * 2 + 1),
|
||||
} {
|
||||
l / 2
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn lit_is_neg(
|
||||
l : Int32,
|
||||
) -> Bool where {
|
||||
proof_require: 0 <= l,
|
||||
proof_ensure: r => r == (l % 2 == 1),
|
||||
} {
|
||||
l % 2 != 0
|
||||
}
|
||||
|
||||
///|
|
||||
/// The complement: same variable, opposite sign.
|
||||
pub fn lit_neg(
|
||||
l : Int32,
|
||||
) -> Int32 where {
|
||||
proof_require: 0 <= l,
|
||||
proof_ensure: r => 0 <= r && r != l && r / 2 == l / 2 && r % 2 != l % 2,
|
||||
} {
|
||||
if l % 2 == 0 {
|
||||
l + 1
|
||||
} else {
|
||||
l - 1
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn lit_val(
|
||||
v : Val,
|
||||
l : Int32,
|
||||
) -> Val where {
|
||||
proof_require: 0 <= l,
|
||||
proof_ensure: r => truth_ok(v, l, r),
|
||||
} {
|
||||
match v {
|
||||
Unassigned => Unassigned
|
||||
True => if lit_is_neg(l) { False } else { True }
|
||||
False => if lit_is_neg(l) { True } else { False }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
// Specification of solver literals: variable `v` has the positive literal `2v`
|
||||
// and the negative literal `2v + 1`.
|
||||
|
||||
///|
|
||||
predicate var_ok(v : Int32) {
|
||||
0 <= v && v < 1073741823
|
||||
}
|
||||
|
||||
///|
|
||||
predicate is_true(v : Val) {
|
||||
match v {
|
||||
True => true
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
predicate is_false(v : Val) {
|
||||
match v {
|
||||
False => true
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
predicate lit_true(v : Val, l : Int32) {
|
||||
(is_true(v) && l % 2 == 0) || (is_false(v) && l % 2 == 1)
|
||||
}
|
||||
|
||||
///|
|
||||
predicate lit_false(v : Val, l : Int32) {
|
||||
(is_false(v) && l % 2 == 0) || (is_true(v) && l % 2 == 1)
|
||||
}
|
||||
|
||||
///|
|
||||
/// `r` is the truth of literal `l` when its variable holds `v`.
|
||||
predicate truth_ok(v : Val, l : Int32, r : Val) {
|
||||
is_true(r) == lit_true(v, l) && is_false(r) == lit_false(v, l)
|
||||
}
|
||||
|
||||
///|
|
||||
predicate opposite(a : Val, b : Val) {
|
||||
is_true(a) == is_false(b) && is_false(a) == is_true(b)
|
||||
}
|
||||
|
||||
///|
|
||||
/// Complementary literals never agree.
|
||||
lemma truth_complement(v : Val, l : Int32, m : Int32, a : Val, b : Val) where {
|
||||
proof_require: 0 <= l,
|
||||
proof_require: 0 <= m,
|
||||
proof_require: l % 2 != m % 2,
|
||||
proof_require: truth_ok(v, l, a),
|
||||
proof_require: truth_ok(v, m, b),
|
||||
proof_ensure: opposite(a, b),
|
||||
} {
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
proof_enabled = true
|
||||
@@ -0,0 +1,33 @@
|
||||
///|
|
||||
/// Value of the decimal code points `s`, or -1 if `s` is empty, has a
|
||||
/// non-digit, or is 10^18 or more.
|
||||
pub fn parse_nat(
|
||||
s : FixedArray[Int64],
|
||||
) -> Int64 where {
|
||||
proof_require: s.length() < 2147483647,
|
||||
proof_ensure: r => parse_post(s, r),
|
||||
proof_ensure: r => valid_or_rejected(r),
|
||||
} {
|
||||
let t = FixedArray::make(s.length() + 1, 0L)
|
||||
for i in 0..<s.length() {
|
||||
let n = t[i]
|
||||
let c = s[i]
|
||||
t[i + 1] = if n < 0L || n > 99999999999999999L || c < 48L || c > 57L {
|
||||
-1L
|
||||
} else {
|
||||
n * 10L + (c - 48L)
|
||||
}
|
||||
} where {
|
||||
proof_invariant: 0 <= i && i <= s.length(),
|
||||
proof_invariant: trace(s, t, i, t[i]),
|
||||
proof_invariant: valid_or_rejected(t[i]),
|
||||
}
|
||||
let r = t[s.length()]
|
||||
proof_assert trace(s, t, s.length(), r)
|
||||
proof_assert decimal(s, r)
|
||||
if s.length() == 0 {
|
||||
-1L
|
||||
} else {
|
||||
r
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
// Specification of `parse_nat`.
|
||||
|
||||
///|
|
||||
predicate nat_ok(n : Int64) {
|
||||
0L <= n && n <= 999999999999999999L
|
||||
}
|
||||
|
||||
///|
|
||||
/// `p` followed by the digit `c`, or -1 if `p` was already rejected, `c` is not
|
||||
/// a digit, or the result is 10^18 or more.
|
||||
fn append(p : Int64, c : Int64) -> Int64 {
|
||||
if 0L <= p && p <= 99999999999999999L && 48L <= c && c <= 57L {
|
||||
p * 10L + (c - 48L)
|
||||
} else {
|
||||
0L - 1L
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// `t[i]` is the decimal value of the first `i` code points, for all `i <= n`.
|
||||
predicate trace_ok(s : FixedArray[Int64], t : FixedArray[Int64], n : Int32) {
|
||||
∀ i : Int32, (0 <= i && i < n) → t[i + 1] == append(t[i], s[i])
|
||||
}
|
||||
|
||||
///|
|
||||
predicate trace(
|
||||
s : FixedArray[Int64],
|
||||
t : FixedArray[Int64],
|
||||
n : Int32,
|
||||
r : Int64,
|
||||
) {
|
||||
t.length() == s.length() + 1 &&
|
||||
t[0] == 0L &&
|
||||
trace_ok(s, t, n) &&
|
||||
t[n] == r
|
||||
}
|
||||
|
||||
///|
|
||||
/// `r` is the decimal value of `s`, or -1 as for `append`.
|
||||
predicate decimal(s : FixedArray[Int64], r : Int64) {
|
||||
∃ t : FixedArray[Int64], trace(s, t, s.length(), r)
|
||||
}
|
||||
|
||||
///|
|
||||
predicate parse_post(s : FixedArray[Int64], r : Int64) {
|
||||
(s.length() == 0 && r == 0L - 1L) || (s.length() > 0 && decimal(s, r))
|
||||
}
|
||||
|
||||
///|
|
||||
predicate valid_or_rejected(r : Int64) {
|
||||
r == 0L - 1L || nat_ok(r)
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
///|
|
||||
/// Indices of `keys`, highest version first; equal versions keep their order.
|
||||
pub fn sort_desc(
|
||||
keys : FixedArray[Key],
|
||||
) -> FixedArray[Int32] where {
|
||||
proof_ensure: r => sort_post(keys, r),
|
||||
} {
|
||||
let idx = FixedArray::make(keys.length(), 0)
|
||||
for i in 0..<keys.length() {
|
||||
idx[i] = i
|
||||
sink(keys, idx, i, i)
|
||||
} where {
|
||||
proof_invariant: 0 <= i && i <= keys.length(),
|
||||
proof_invariant: sorted_prefix(keys, idx, i),
|
||||
}
|
||||
idx
|
||||
}
|
||||
|
||||
///|
|
||||
/// Move the entry at `j` left until the first `n + 1` entries are ordered.
|
||||
fn sink(
|
||||
keys : FixedArray[Key],
|
||||
idx : FixedArray[Int32],
|
||||
j : Int32,
|
||||
n : Int32,
|
||||
) -> Unit where {
|
||||
proof_decrease: j,
|
||||
proof_require: 0 <= j && j <= n && n < keys.length(),
|
||||
proof_require: sinking(keys, idx, j, n),
|
||||
proof_ensure: sorted_prefix(keys, idx, n + 1),
|
||||
} {
|
||||
if j > 0 && compare(keys[idx[j]], keys[idx[j - 1]]) > 0 {
|
||||
let t = idx[j]
|
||||
idx[j] = idx[j - 1]
|
||||
idx[j - 1] = t
|
||||
sink(keys, idx, j - 1, n)
|
||||
} else {
|
||||
()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
// Specification of `sort_desc`: the result lists every index of `keys` once,
|
||||
// highest version first, ties in their original order.
|
||||
|
||||
///|
|
||||
predicate same_length(keys : FixedArray[Key], idx : FixedArray[Int32]) {
|
||||
idx.length() == keys.length()
|
||||
}
|
||||
|
||||
///|
|
||||
/// The first `n` entries are all below `n`.
|
||||
predicate below(idx : FixedArray[Int32], n : Int32) {
|
||||
∀ p : Int32, (0 <= p && p < n) → (0 <= idx[p] && idx[p] < n)
|
||||
}
|
||||
|
||||
///|
|
||||
predicate distinct(idx : FixedArray[Int32], n : Int32) {
|
||||
∀ p : Int32, ∀ q : Int32, (0 <= p && p < q && q < n) → idx[p] != idx[q]
|
||||
}
|
||||
|
||||
///|
|
||||
/// Index `i` comes before index `j` in the result.
|
||||
predicate before(keys : FixedArray[Key], i : Int32, j : Int32) {
|
||||
key_lt(keys[j], keys[i]) || (key_eq(keys[j], keys[i]) && i < j)
|
||||
}
|
||||
|
||||
///|
|
||||
predicate ordered(keys : FixedArray[Key], idx : FixedArray[Int32], n : Int32) {
|
||||
∀ p : Int32, ∀ q : Int32, (0 <= p && p < q && q < n) →
|
||||
before(keys, idx[p], idx[q])
|
||||
}
|
||||
|
||||
///|
|
||||
/// Everything in `(j, n]` comes after the entry being inserted at `j`.
|
||||
predicate after_hole(
|
||||
keys : FixedArray[Key],
|
||||
idx : FixedArray[Int32],
|
||||
j : Int32,
|
||||
n : Int32,
|
||||
) {
|
||||
∀ q : Int32, (j < q && q <= n) → before(keys, idx[j], idx[q])
|
||||
}
|
||||
|
||||
///|
|
||||
/// The first `n + 1` entries are ordered, ignoring the one at `j`.
|
||||
predicate ordered_except(
|
||||
keys : FixedArray[Key],
|
||||
idx : FixedArray[Int32],
|
||||
j : Int32,
|
||||
n : Int32,
|
||||
) {
|
||||
∀ p : Int32, ∀ q : Int32, (0 <= p && p < q && q <= n && p != j && q != j) →
|
||||
before(keys, idx[p], idx[q])
|
||||
}
|
||||
|
||||
///|
|
||||
predicate sort_post(keys : FixedArray[Key], idx : FixedArray[Int32]) {
|
||||
sorted_prefix(keys, idx, keys.length())
|
||||
}
|
||||
|
||||
///|
|
||||
/// The first `n` entries are `0..<n`, reordered.
|
||||
predicate sorted_prefix(
|
||||
keys : FixedArray[Key],
|
||||
idx : FixedArray[Int32],
|
||||
n : Int32,
|
||||
) {
|
||||
same_length(keys, idx) &&
|
||||
below(idx, n) &&
|
||||
distinct(idx, n) &&
|
||||
ordered(keys, idx, n)
|
||||
}
|
||||
|
||||
///|
|
||||
/// Entry `n` is being inserted and has reached position `j`.
|
||||
predicate sinking(
|
||||
keys : FixedArray[Key],
|
||||
idx : FixedArray[Int32],
|
||||
j : Int32,
|
||||
n : Int32,
|
||||
) {
|
||||
same_length(keys, idx) &&
|
||||
below(idx, n + 1) &&
|
||||
distinct(idx, n + 1) &&
|
||||
idx[j] == n &&
|
||||
after_hole(keys, idx, j, n) &&
|
||||
ordered_except(keys, idx, j, n)
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
///|
|
||||
/// A version reduced to integers. `pre` ranks the prerelease: callers must give
|
||||
/// equal prereleases equal ranks and order the others as they compare.
|
||||
pub(all) struct Key {
|
||||
major : Int64
|
||||
minor : Int64
|
||||
patch : Int64
|
||||
pre : Int32
|
||||
}
|
||||
|
||||
///|
|
||||
#derive(Eq, Debug)
|
||||
pub(all) enum Op {
|
||||
Any
|
||||
Ge
|
||||
Le
|
||||
Gt
|
||||
Lt
|
||||
Eq
|
||||
Ne
|
||||
Caret
|
||||
Tilde
|
||||
Prefix
|
||||
NotPrefix
|
||||
}
|
||||
|
||||
///|
|
||||
pub extend Op with Eq::{equal, not_equal}
|
||||
|
||||
///|
|
||||
pub extend Op with Debug::{to_repr}
|
||||
|
||||
///|
|
||||
/// -1, 0 or 1 as `a` is below, equal to or above `b`.
|
||||
pub fn compare(
|
||||
a : Key,
|
||||
b : Key,
|
||||
) -> Int32 where {
|
||||
proof_ensure: r => compare_post(a, b, r),
|
||||
} {
|
||||
if a.major < b.major {
|
||||
-1
|
||||
} else if a.major > b.major {
|
||||
1
|
||||
} else if a.minor < b.minor {
|
||||
-1
|
||||
} else if a.minor > b.minor {
|
||||
1
|
||||
} else if a.patch < b.patch {
|
||||
-1
|
||||
} else if a.patch > b.patch {
|
||||
1
|
||||
} else if a.pre < b.pre {
|
||||
-1
|
||||
} else if a.pre > b.pre {
|
||||
1
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// Whether `v` satisfies the clause `op b`, where `b` was written with `parts`
|
||||
/// components.
|
||||
pub fn matches(
|
||||
op : Op,
|
||||
v : Key,
|
||||
b : Key,
|
||||
parts : Int32,
|
||||
) -> Bool where {
|
||||
proof_require: key_inv(v),
|
||||
proof_require: key_inv(b),
|
||||
proof_require: 1 <= parts && parts <= 3,
|
||||
proof_ensure: r => r == allowed(op, v, b, parts),
|
||||
} {
|
||||
match op {
|
||||
Any => true
|
||||
Ge => compare(v, b) >= 0
|
||||
Le => compare(v, b) <= 0
|
||||
Gt => compare(v, b) > 0
|
||||
Lt => compare(v, b) < 0
|
||||
Eq => compare(v, b) == 0
|
||||
Ne => compare(v, b) != 0
|
||||
Caret =>
|
||||
compare(v, b) >= 0 &&
|
||||
v.major == b.major &&
|
||||
(b.major != 0L || v.minor == b.minor)
|
||||
Tilde =>
|
||||
compare(v, b) >= 0 &&
|
||||
v.major == b.major &&
|
||||
(parts == 1 || v.minor == b.minor)
|
||||
Prefix => same_prefix(v, b, parts)
|
||||
NotPrefix => !same_prefix(v, b, parts)
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn same_prefix(
|
||||
v : Key,
|
||||
b : Key,
|
||||
parts : Int32,
|
||||
) -> Bool where {
|
||||
proof_require: key_inv(v),
|
||||
proof_require: key_inv(b),
|
||||
proof_require: 1 <= parts && parts <= 3,
|
||||
proof_ensure: r => r == in_prefix(v, b, parts),
|
||||
} {
|
||||
if parts == 1 {
|
||||
v.major == b.major
|
||||
} else if parts == 2 {
|
||||
v.major == b.major && v.minor == b.minor
|
||||
} else {
|
||||
true
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
// Specification of versions: a key is ordered lexicographically, and every
|
||||
// constraint operator is an interval of that order.
|
||||
|
||||
///|
|
||||
predicate key_inv(k : Key) {
|
||||
nat_ok(k.major) && nat_ok(k.minor) && nat_ok(k.patch)
|
||||
}
|
||||
|
||||
///|
|
||||
predicate core_lt(a : Key, b : Key) {
|
||||
a.major < b.major ||
|
||||
(a.major == b.major && a.minor < b.minor) ||
|
||||
(a.major == b.major && a.minor == b.minor && a.patch < b.patch)
|
||||
}
|
||||
|
||||
///|
|
||||
predicate core_eq(a : Key, b : Key) {
|
||||
a.major == b.major && a.minor == b.minor && a.patch == b.patch
|
||||
}
|
||||
|
||||
///|
|
||||
predicate key_lt(a : Key, b : Key) {
|
||||
core_lt(a, b) || (core_eq(a, b) && a.pre < b.pre)
|
||||
}
|
||||
|
||||
///|
|
||||
predicate key_eq(a : Key, b : Key) {
|
||||
core_eq(a, b) && a.pre == b.pre
|
||||
}
|
||||
|
||||
///|
|
||||
predicate key_le(a : Key, b : Key) {
|
||||
key_lt(a, b) || key_eq(a, b)
|
||||
}
|
||||
|
||||
///|
|
||||
predicate not_lt(a : Key, b : Key) {
|
||||
!key_lt(a, b)
|
||||
}
|
||||
|
||||
///|
|
||||
predicate compare_post(a : Key, b : Key, r : Int32) {
|
||||
(r == 0 || r == 1 || r == -1) &&
|
||||
((r == -1) == key_lt(a, b)) &&
|
||||
((r == 0) == key_eq(a, b)) &&
|
||||
((r == 1) == key_lt(b, a))
|
||||
}
|
||||
|
||||
// Laws of the order: these say `compare` is a total order.
|
||||
|
||||
///|
|
||||
lemma key_lt_irreflexive(a : Key) where {
|
||||
proof_ensure: not_lt(a, a),
|
||||
} {
|
||||
}
|
||||
|
||||
///|
|
||||
lemma key_lt_asymmetric(a : Key, b : Key) where {
|
||||
proof_require: key_lt(a, b),
|
||||
proof_ensure: not_lt(b, a),
|
||||
} {
|
||||
}
|
||||
|
||||
///|
|
||||
lemma key_lt_transitive(a : Key, b : Key, c : Key) where {
|
||||
proof_require: key_lt(a, b),
|
||||
proof_require: key_lt(b, c),
|
||||
proof_ensure: key_lt(a, c),
|
||||
} {
|
||||
}
|
||||
|
||||
///|
|
||||
predicate comparable(a : Key, b : Key) {
|
||||
key_lt(a, b) || key_eq(a, b) || key_lt(b, a)
|
||||
}
|
||||
|
||||
///|
|
||||
lemma key_lt_total(a : Key, b : Key) where {
|
||||
proof_ensure: comparable(a, b),
|
||||
} {
|
||||
}
|
||||
|
||||
///|
|
||||
/// `v`'s core is below the first version of the next major.
|
||||
predicate below_next_major(v : Key, b : Key) {
|
||||
v.major < b.major + 1L
|
||||
}
|
||||
|
||||
///|
|
||||
/// `v`'s core is below the first version of the next minor.
|
||||
predicate below_next_minor(v : Key, b : Key) {
|
||||
v.major < b.major || (v.major == b.major && v.minor < b.minor + 1L)
|
||||
}
|
||||
|
||||
///|
|
||||
predicate from_major(v : Key, b : Key) {
|
||||
b.major <= v.major
|
||||
}
|
||||
|
||||
///|
|
||||
predicate from_minor(v : Key, b : Key) {
|
||||
b.major < v.major || (b.major == v.major && b.minor <= v.minor)
|
||||
}
|
||||
|
||||
///|
|
||||
/// Half-open core interval covered by a one- or two-part prefix.
|
||||
predicate in_prefix(v : Key, b : Key, parts : Int32) {
|
||||
(parts == 1 && from_major(v, b) && below_next_major(v, b)) ||
|
||||
(parts == 2 && from_minor(v, b) && below_next_minor(v, b)) ||
|
||||
parts == 3
|
||||
}
|
||||
|
||||
///|
|
||||
predicate in_caret(v : Key, b : Key) {
|
||||
(b.major != 0L && key_le(b, v) && below_next_major(v, b)) ||
|
||||
(b.major == 0L && key_le(b, v) && below_next_minor(v, b))
|
||||
}
|
||||
|
||||
///|
|
||||
predicate in_tilde(v : Key, b : Key, parts : Int32) {
|
||||
(parts == 1 && key_le(b, v) && below_next_major(v, b)) ||
|
||||
(parts != 1 && key_le(b, v) && below_next_minor(v, b))
|
||||
}
|
||||
|
||||
///|
|
||||
predicate allowed(op : Op, v : Key, b : Key, parts : Int32) {
|
||||
match op {
|
||||
Any => true
|
||||
Ge => key_le(b, v)
|
||||
Le => key_le(v, b)
|
||||
Gt => key_lt(b, v)
|
||||
Lt => key_lt(v, b)
|
||||
Eq => key_eq(v, b)
|
||||
Ne => !key_eq(v, b)
|
||||
Caret => in_caret(v, b)
|
||||
Tilde => in_tilde(v, b, parts)
|
||||
Prefix => in_prefix(v, b, parts)
|
||||
NotPrefix => !in_prefix(v, b, parts)
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import {
|
||||
"latha/orbit/kernel",
|
||||
"moonbitlang/core/json",
|
||||
"moonbitlang/core/encoding/base64",
|
||||
"moonbitlang/core/encoding/hex",
|
||||
|
||||
+185
-253
@@ -12,24 +12,20 @@
|
||||
|
||||
///|
|
||||
struct Ver {
|
||||
nums : Array[Int32]
|
||||
nums : Array[Int64]
|
||||
parts : Int32
|
||||
pre : String
|
||||
}
|
||||
|
||||
///|
|
||||
fn parse_nat(s : String) -> Int32? {
|
||||
if s == "" {
|
||||
return None
|
||||
fn parse_nat(s : String) -> Int64? {
|
||||
let codes = FixedArray::from_array(chars(s).map(c => c.to_int().to_int64()))
|
||||
let n = @kernel.parse_nat(codes)
|
||||
if n < 0L {
|
||||
None
|
||||
} else {
|
||||
Some(n)
|
||||
}
|
||||
let mut n = 0
|
||||
for c in s {
|
||||
if c < '0' || c > '9' {
|
||||
return None
|
||||
}
|
||||
n = n * 10 + (c.to_int() - '0'.to_int())
|
||||
}
|
||||
Some(n)
|
||||
}
|
||||
|
||||
///|
|
||||
@@ -41,7 +37,7 @@ fn parse_ver(s0 : String) -> Ver? {
|
||||
if parts.length() == 0 || parts.length() > 3 {
|
||||
return None
|
||||
}
|
||||
let nums = [0, 0, 0]
|
||||
let nums = [0L, 0L, 0L]
|
||||
for i = 0; i < parts.length(); i = i + 1 {
|
||||
match parse_nat(parts[i]) {
|
||||
Some(n) => nums[i] = n
|
||||
@@ -60,23 +56,29 @@ fn is_ver(s : String) -> Bool {
|
||||
}
|
||||
|
||||
///|
|
||||
fn cmp_ver(a : Ver, b : Ver) -> Int32 {
|
||||
for i = 0; i < 3; i = i + 1 {
|
||||
if a.nums[i] != b.nums[i] {
|
||||
return if a.nums[i] < b.nums[i] { -1 } else { 1 }
|
||||
}
|
||||
}
|
||||
if a.pre == b.pre {
|
||||
fn ver_key(v : Ver, pre : Int32) -> @kernel.Key {
|
||||
{ major: v.nums[0], minor: v.nums[1], patch: v.nums[2], pre, }
|
||||
}
|
||||
|
||||
///|
|
||||
/// Order of prereleases; a release ("") comes after all of them.
|
||||
fn cmp_pre(a : String, b : String) -> Int32 {
|
||||
if a == b {
|
||||
0
|
||||
} else if a.pre == "" {
|
||||
} else if a == "" {
|
||||
1
|
||||
} else if b.pre == "" {
|
||||
} else if b == "" {
|
||||
-1
|
||||
} else {
|
||||
a.pre.compare(b.pre)
|
||||
a.compare(b)
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn cmp_ver(a : Ver, b : Ver) -> Int32 {
|
||||
@kernel.compare(ver_key(a, cmp_pre(a.pre, b.pre)), ver_key(b, 0))
|
||||
}
|
||||
|
||||
///|
|
||||
fn strip_dot_star(s : String) -> (String, Bool) {
|
||||
let a = chars(s)
|
||||
@@ -89,98 +91,72 @@ fn strip_dot_star(s : String) -> (String, Bool) {
|
||||
}
|
||||
|
||||
///|
|
||||
fn matches_prefix(v : Ver, b : Ver) -> Bool {
|
||||
for i = 0; i < b.parts; i = i + 1 {
|
||||
if v.nums[i] != b.nums[i] {
|
||||
return false
|
||||
/// Split a clause into its operator and the version it applies to; `None` if it
|
||||
/// is malformed.
|
||||
fn parse_clause(cl : String) -> (@kernel.Op, Ver)? {
|
||||
let c = trim(cl)
|
||||
if c == "*" || c == "latest" {
|
||||
return Some((Any, Ver::{ nums: [0L, 0L, 0L], parts: 3, pre: "", }))
|
||||
}
|
||||
let plain = fn(op : @kernel.Op, rest : String) -> (@kernel.Op, Ver)? {
|
||||
match parse_ver(trim(rest)) {
|
||||
Some(b) => Some((op, b))
|
||||
None => None
|
||||
}
|
||||
}
|
||||
true
|
||||
// An exact version, or a prefix when parts are missing or end in ".*".
|
||||
let exact = fn(
|
||||
whole : @kernel.Op,
|
||||
prefix : @kernel.Op,
|
||||
rest : String,
|
||||
) -> (@kernel.Op, Ver)? {
|
||||
let (ver_str, had_wild) = strip_dot_star(trim(rest))
|
||||
match parse_ver(ver_str) {
|
||||
Some(b) =>
|
||||
if b.parts < 3 {
|
||||
Some((prefix, b))
|
||||
} else if had_wild {
|
||||
None
|
||||
} else {
|
||||
Some((whole, b))
|
||||
}
|
||||
None => None
|
||||
}
|
||||
}
|
||||
if has_prefix(c, ">=") {
|
||||
plain(Ge, drop_chars(c, 2))
|
||||
} else if has_prefix(c, "<=") {
|
||||
plain(Le, drop_chars(c, 2))
|
||||
} else if has_prefix(c, "!=") {
|
||||
exact(Ne, NotPrefix, drop_chars(c, 2))
|
||||
} else if has_prefix(c, "==") {
|
||||
exact(Eq, Prefix, drop_chars(c, 2))
|
||||
} else if has_prefix(c, ">") {
|
||||
plain(Gt, drop_chars(c, 1))
|
||||
} else if has_prefix(c, "<") {
|
||||
plain(Lt, drop_chars(c, 1))
|
||||
} else if has_prefix(c, "^") {
|
||||
plain(Caret, drop_chars(c, 1))
|
||||
} else if has_prefix(c, "~") {
|
||||
plain(Tilde, drop_chars(c, 1))
|
||||
} else if has_prefix(c, "=") {
|
||||
exact(Eq, Prefix, drop_chars(c, 1))
|
||||
} else {
|
||||
exact(Eq, Prefix, c)
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn satisfies_clause(v : Ver, cl : String) -> Bool {
|
||||
let c = trim(cl)
|
||||
if c == "*" || c == "latest" {
|
||||
return true
|
||||
}
|
||||
let with_base = fn(rest : String, rule : (Ver) -> Bool) -> Bool {
|
||||
match parse_ver(trim(rest)) {
|
||||
Some(b) => cmp_ver(v, b) >= 0 && rule(b)
|
||||
None => false
|
||||
}
|
||||
}
|
||||
if has_prefix(c, ">=") {
|
||||
with_base(drop_chars(c, 2), fn(_b) { true })
|
||||
} else if has_prefix(c, "<=") {
|
||||
match parse_ver(trim(drop_chars(c, 2))) {
|
||||
Some(b) => cmp_ver(v, b) <= 0
|
||||
None => false
|
||||
}
|
||||
} else if has_prefix(c, "!=") {
|
||||
let (ver_str, had_wild) = strip_dot_star(trim(drop_chars(c, 2)))
|
||||
match parse_ver(ver_str) {
|
||||
Some(b) =>
|
||||
if b.parts < 3 || had_wild {
|
||||
if had_wild && b.parts == 3 {
|
||||
false
|
||||
} else {
|
||||
!matches_prefix(v, b)
|
||||
}
|
||||
} else {
|
||||
cmp_ver(v, b) != 0
|
||||
}
|
||||
None => false
|
||||
}
|
||||
} else if has_prefix(c, "==") {
|
||||
let (ver_str, had_wild) = strip_dot_star(trim(drop_chars(c, 2)))
|
||||
match parse_ver(ver_str) {
|
||||
Some(b) =>
|
||||
if had_wild && b.parts == 3 {
|
||||
false
|
||||
} else if b.parts == 3 && !had_wild {
|
||||
cmp_ver(v, b) == 0
|
||||
} else {
|
||||
matches_prefix(v, b)
|
||||
}
|
||||
None => false
|
||||
}
|
||||
} else if has_prefix(c, ">") {
|
||||
match parse_ver(trim(drop_chars(c, 1))) {
|
||||
Some(b) => cmp_ver(v, b) > 0
|
||||
None => false
|
||||
}
|
||||
} else if has_prefix(c, "<") {
|
||||
match parse_ver(trim(drop_chars(c, 1))) {
|
||||
Some(b) => cmp_ver(v, b) < 0
|
||||
None => false
|
||||
}
|
||||
} else if has_prefix(c, "^") {
|
||||
with_base(drop_chars(c, 1), fn(b) {
|
||||
v.nums[0] == b.nums[0] && (b.nums[0] != 0 || v.nums[1] == b.nums[1])
|
||||
})
|
||||
} else if has_prefix(c, "~") {
|
||||
with_base(drop_chars(c, 1), fn(b) {
|
||||
v.nums[0] == b.nums[0] && (b.parts == 1 || v.nums[1] == b.nums[1])
|
||||
})
|
||||
} else {
|
||||
let exact = if has_prefix(c, "=") {
|
||||
trim(drop_chars(c, 1))
|
||||
} else {
|
||||
c
|
||||
}
|
||||
let (ver_str, had_wild) = strip_dot_star(exact)
|
||||
match parse_ver(ver_str) {
|
||||
Some(b) =>
|
||||
if had_wild && b.parts == 3 {
|
||||
false
|
||||
} else if b.parts == 3 && !had_wild {
|
||||
cmp_ver(v, b) == 0
|
||||
} else {
|
||||
matches_prefix(v, b)
|
||||
}
|
||||
None => false
|
||||
}
|
||||
match parse_clause(cl) {
|
||||
Some((op, b)) =>
|
||||
@kernel.matches(
|
||||
op,
|
||||
ver_key(v, cmp_pre(v.pre, b.pre)),
|
||||
ver_key(b, 0),
|
||||
b.parts,
|
||||
)
|
||||
None => false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -190,7 +166,7 @@ fn satisfies(v : Ver, c0 : String) -> Bool {
|
||||
if c == "" || c == "*" || c == "latest" {
|
||||
return v.pre == ""
|
||||
}
|
||||
if v.pre != "" && !(has_char(c, '-')) {
|
||||
if v.pre != "" && !has_char(c, '-') {
|
||||
return false
|
||||
}
|
||||
let pieces = split_on(c, ',')
|
||||
@@ -205,34 +181,7 @@ fn satisfies(v : Ver, c0 : String) -> Bool {
|
||||
|
||||
///|
|
||||
fn valid_semver_clause(cl : String) -> Bool {
|
||||
let c = trim(cl)
|
||||
if c == "" {
|
||||
return false
|
||||
}
|
||||
if c == "*" || c == "latest" {
|
||||
return true
|
||||
}
|
||||
let check_exact_or_wild = fn(raw : String) -> Bool {
|
||||
let (ver_str, had_wild) = strip_dot_star(trim(raw))
|
||||
match parse_ver(ver_str) {
|
||||
Some(b) => !(had_wild && b.parts == 3)
|
||||
None => false
|
||||
}
|
||||
}
|
||||
if has_prefix(c, ">=") || has_prefix(c, "<=") {
|
||||
parse_ver(trim(drop_chars(c, 2))) is Some(_)
|
||||
} else if has_prefix(c, "!=") || has_prefix(c, "==") {
|
||||
check_exact_or_wild(drop_chars(c, 2))
|
||||
} else if has_prefix(c, ">") ||
|
||||
has_prefix(c, "<") ||
|
||||
has_prefix(c, "^") ||
|
||||
has_prefix(c, "~") {
|
||||
parse_ver(trim(drop_chars(c, 1))) is Some(_)
|
||||
} else if has_prefix(c, "=") {
|
||||
check_exact_or_wild(drop_chars(c, 1))
|
||||
} else {
|
||||
check_exact_or_wild(c)
|
||||
}
|
||||
parse_clause(cl) is Some(_)
|
||||
}
|
||||
|
||||
///|
|
||||
@@ -272,7 +221,7 @@ fn satisfies_all(version : String, cons : Array[String]) -> Bool {
|
||||
match parse_ver(version) {
|
||||
Some(v) => {
|
||||
for c in cons {
|
||||
if !(satisfies(v, c)) {
|
||||
if !satisfies(v, c) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
@@ -295,143 +244,126 @@ fn satisfies_all(version : String, cons : Array[String]) -> Bool {
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// Pick the highest tag that satisfies every constraint. Falls back to a
|
||||
/// literal non-semver tag when a constraint names one exactly.
|
||||
fn pick_tag(tags : Array[String], cons : Array[String]) -> String? {
|
||||
let mut best : (String, Ver)? = None
|
||||
for t in tags {
|
||||
match parse_ver(t) {
|
||||
Some(v) =>
|
||||
if satisfies_all(t, cons) {
|
||||
match best {
|
||||
None => best = Some((t, v))
|
||||
Some((_, bv)) => if cmp_ver(v, bv) > 0 { best = Some((t, v)) }
|
||||
}
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
match best {
|
||||
Some((t, _)) => Some(t)
|
||||
None => {
|
||||
for c in cons {
|
||||
let t = trim(c)
|
||||
if t != "" && t != "*" && !(is_ver(t)) && tags.contains(t) {
|
||||
return Some(t)
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// Semver tags, highest first.
|
||||
fn sorted_versions(tags : Array[String]) -> Array[String] {
|
||||
let out : Array[(String, Ver)] = []
|
||||
let names : Array[String] = []
|
||||
let vers : Array[Ver] = []
|
||||
for t in tags {
|
||||
match parse_ver(t) {
|
||||
Some(v) => out.push((t, v))
|
||||
Some(v) => {
|
||||
names.push(t)
|
||||
vers.push(v)
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
// insertion sort, descending
|
||||
for i = 1; i < out.length(); i = i + 1 {
|
||||
let mut j = i
|
||||
while j > 0 && cmp_ver(out[j].1, out[j - 1].1) > 0 {
|
||||
let tmp = out[j]
|
||||
out[j] = out[j - 1]
|
||||
out[j - 1] = tmp
|
||||
j = j - 1
|
||||
let order = @kernel.sort_desc(FixedArray::from_array(ver_keys(vers)))
|
||||
Array::makei(order.length(), fn(i) { names[order[i]] })
|
||||
}
|
||||
|
||||
///|
|
||||
/// Keys that order `vers`: the prerelease rank is how many prereleases among
|
||||
/// `vers` come before it.
|
||||
fn ver_keys(vers : Array[Ver]) -> Array[@kernel.Key] {
|
||||
vers.map(fn(v) {
|
||||
let mut rank = 0
|
||||
for other in vers {
|
||||
if cmp_pre(other.pre, v.pre) < 0 {
|
||||
rank = rank + 1
|
||||
}
|
||||
}
|
||||
ver_key(v, rank)
|
||||
})
|
||||
}
|
||||
|
||||
// Number parsing, ordering, interval and sorting laws are proved in
|
||||
// src/kernel. The tests below cover what the prover cannot see: splitting
|
||||
// strings into versions and clauses, and ranking prereleases.
|
||||
|
||||
///|
|
||||
test "versions parse into numbers and a prerelease" {
|
||||
let a = parse_ver("v1.10-rc1+build").unwrap()
|
||||
assert_eq(a.nums, [1L, 10L, 0L])
|
||||
assert_eq(a.parts, 2)
|
||||
assert_eq(a.pre, "rc1")
|
||||
assert_eq(parse_ver("1.0.20261130123000").unwrap().nums, [
|
||||
1L, 0L, 20261130123000L,
|
||||
])
|
||||
for bad in ["", "1.x", "1.", "1.2.3.4", "1.-2", "1.0.1000000000000000000"] {
|
||||
assert_true(parse_ver(bad) is None)
|
||||
}
|
||||
let r : Array[String] = []
|
||||
for p in out {
|
||||
r.push(p.0)
|
||||
}
|
||||
r
|
||||
}
|
||||
|
||||
///|
|
||||
test "parse and compare" {
|
||||
let a = parse_ver("1.2.3").unwrap()
|
||||
let b = parse_ver("v1.10.0").unwrap()
|
||||
assert_true(cmp_ver(a, b) < 0)
|
||||
assert_true(
|
||||
cmp_ver(parse_ver("1.0.0-rc1").unwrap(), parse_ver("1.0.0").unwrap()) < 0,
|
||||
test "prereleases rank below their release and by name" {
|
||||
assert_eq(
|
||||
sorted_versions([
|
||||
"1.0.0-rc1", "edge", "2.0.0", "1.0.0", "1.0.0-rc2", "2.1.0-rc1",
|
||||
]),
|
||||
["2.1.0-rc1", "2.0.0", "1.0.0", "1.0.0-rc2", "1.0.0-rc1"],
|
||||
)
|
||||
assert_true(parse_ver("1.x") is None)
|
||||
let rc1 = parse_ver("1.0.0-rc1").unwrap()
|
||||
assert_true(cmp_ver(rc1, parse_ver("1.0.0").unwrap()) < 0)
|
||||
assert_true(cmp_ver(rc1, parse_ver("1.0.0-rc2").unwrap()) < 0)
|
||||
assert_eq(cmp_ver(rc1, rc1), 0)
|
||||
}
|
||||
|
||||
///|
|
||||
test "constraints" {
|
||||
test "clauses parse into an operator and a version" {
|
||||
let table : Array[(String, (@kernel.Op, Array[Int64], Int32)?)] = [
|
||||
("*", Some((Any, [0L, 0L, 0L], 3))),
|
||||
("latest", Some((Any, [0L, 0L, 0L], 3))),
|
||||
(">=1.2", Some((Ge, [1L, 2L, 0L], 2))),
|
||||
("<= 1.2.3", Some((Le, [1L, 2L, 3L], 3))),
|
||||
(">1", Some((Gt, [1L, 0L, 0L], 1))),
|
||||
("<2.0.0", Some((Lt, [2L, 0L, 0L], 3))),
|
||||
("^1.2.0", Some((Caret, [1L, 2L, 0L], 3))),
|
||||
("~1", Some((Tilde, [1L, 0L, 0L], 1))),
|
||||
("1.4.2", Some((Eq, [1L, 4L, 2L], 3))),
|
||||
("=1.4.2", Some((Eq, [1L, 4L, 2L], 3))),
|
||||
("==1.4.2", Some((Eq, [1L, 4L, 2L], 3))),
|
||||
("1.4", Some((Prefix, [1L, 4L, 0L], 2))),
|
||||
("==1.4.*", Some((Prefix, [1L, 4L, 0L], 2))),
|
||||
("1.*", Some((Prefix, [1L, 0L, 0L], 1))),
|
||||
("!=1.4.2", Some((Ne, [1L, 4L, 2L], 3))),
|
||||
("!=1.2.*", Some((NotPrefix, [1L, 2L, 0L], 2))),
|
||||
("", None),
|
||||
("<=", None),
|
||||
(">=abc", None),
|
||||
("^^1.0", None),
|
||||
("~>1.0", None),
|
||||
("1.2.3.*", None),
|
||||
("1.2.3.4", None),
|
||||
]
|
||||
for row in table {
|
||||
let (text, want) = row
|
||||
let got = match parse_clause(text) {
|
||||
Some((op, b)) => Some((op, b.nums, b.parts))
|
||||
None => None
|
||||
}
|
||||
assert_eq(got, want, msg=text)
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
test "constraints join clauses and gate prereleases" {
|
||||
let v = parse_ver("1.4.2").unwrap()
|
||||
assert_true(satisfies(v, "^1.2.0"))
|
||||
assert_true(!(satisfies(v, "^2.0.0")))
|
||||
assert_true(satisfies(v, "~1.4.0"))
|
||||
assert_true(!(satisfies(v, "~1.3.0")))
|
||||
assert_true(satisfies(v, ">=1.0"))
|
||||
assert_true(satisfies(v, "1"))
|
||||
assert_true(satisfies(v, "1.4"))
|
||||
assert_true(!(satisfies(v, "1.5")))
|
||||
let rc = parse_ver("1.2.0-rc1").unwrap()
|
||||
assert_true(satisfies(v, ">=1.0, !=1.4.1, <2.0"))
|
||||
assert_false(satisfies(v, ">=1.0, !=1.4.2, <2.0"))
|
||||
assert_true(satisfies(v, "*"))
|
||||
assert_true(!(satisfies(parse_ver("0.3.1").unwrap(), "^0.2.0")))
|
||||
}
|
||||
assert_false(satisfies(rc, "*"))
|
||||
assert_true(satisfies(rc, ">=1.0.0-rc1, <2.0.0"))
|
||||
assert_false(satisfies(rc, ">=1.0.0, <2.0.0"))
|
||||
|
||||
///|
|
||||
test "pick" {
|
||||
let tags = ["1.0.0", "1.2.0", "2.0.0", "2.1.0-rc1", "latest"]
|
||||
assert_eq(pick_tag(tags, ["*"]), Some("2.0.0"))
|
||||
assert_eq(pick_tag(tags, ["^1.0.0"]), Some("1.2.0"))
|
||||
assert_eq(pick_tag(tags, ["^1.0.0", "~1.0.0"]), Some("1.0.0"))
|
||||
assert_eq(pick_tag(tags, ["latest"]), Some("2.0.0"))
|
||||
assert_eq(pick_tag(tags, ["^3"]), None)
|
||||
assert_eq(sorted_versions(tags), ["2.1.0-rc1", "2.0.0", "1.2.0", "1.0.0"])
|
||||
}
|
||||
|
||||
///|
|
||||
test "compound and relational constraints" {
|
||||
let v142 = parse_ver("1.4.2").unwrap()
|
||||
let v120 = parse_ver("1.2.0").unwrap()
|
||||
let v200 = parse_ver("2.0.0").unwrap()
|
||||
let v150 = parse_ver("1.5.0").unwrap()
|
||||
let v120rc = parse_ver("1.2.0-rc1").unwrap()
|
||||
|
||||
assert_true(satisfies(v142, "<2.0.0"))
|
||||
assert_true(!satisfies(v200, "<2.0.0"))
|
||||
assert_true(satisfies(v200, "<=2.0.0"))
|
||||
assert_true(satisfies(v142, ">1.4.0"))
|
||||
assert_true(!satisfies(v120, ">1.2.0"))
|
||||
assert_true(satisfies(v142, "==1.4.2"))
|
||||
assert_true(!satisfies(v142, "!=1.4.2"))
|
||||
assert_true(satisfies(v150, "!=1.4.2"))
|
||||
assert_true(satisfies(v142, "1.*"))
|
||||
assert_true(satisfies(v142, "1.4.*"))
|
||||
assert_true(!satisfies(v150, "1.4.*"))
|
||||
assert_true(satisfies(v142, "!=1.2.*"))
|
||||
assert_true(!satisfies(v120, "!=1.2.*"))
|
||||
assert_true(satisfies(v142, ">=1.2.0, <2.0.0"))
|
||||
assert_true(!satisfies(v200, ">=1.2.0, <2.0.0"))
|
||||
assert_true(satisfies(v150, ">=1.0, !=1.4.2, <2.0"))
|
||||
assert_true(!satisfies(v142, ">=1.0, !=1.4.2, <2.0"))
|
||||
assert_true(satisfies(v120rc, ">=1.0.0-rc1, <2.0.0"))
|
||||
assert_true(!satisfies(v120rc, ">=1.0.0, <2.0.0"))
|
||||
assert_true(satisfies(v142, "~1"))
|
||||
|
||||
assert_true(satisfies_all("edge", ["edge"]))
|
||||
assert_true(satisfies_all("edge", ["=edge"]))
|
||||
assert_true(satisfies_all("edge", ["==edge"]))
|
||||
assert_true(!satisfies_all("edge", [">=1.0"]))
|
||||
assert_true(satisfies_all("1.4.2", ["^1.0.0", "~1.4.0"]))
|
||||
assert_false(satisfies_all("1.4.2", ["^1.0.0", "~1.3.0"]))
|
||||
assert_true(satisfies_all("edge", ["edge", "=edge", "==edge", "*"]))
|
||||
assert_false(satisfies_all("edge", [">=1.0"]))
|
||||
|
||||
assert_true(valid_constraint(">=1.2.0, <2.0.0"))
|
||||
assert_true(valid_constraint("1.4.*"))
|
||||
assert_true(valid_constraint("edge"))
|
||||
assert_false(valid_constraint(">=abc"))
|
||||
assert_false(valid_constraint(">=1,"))
|
||||
assert_false(valid_constraint("<="))
|
||||
assert_false(valid_constraint("^^1.0"))
|
||||
assert_false(valid_constraint("1.2.3.4"))
|
||||
assert_false(valid_constraint("1.2.3.*"))
|
||||
assert_false(valid_constraint("~>1.0"))
|
||||
}
|
||||
+33
-75
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user