Compare commits
10
Commits
3bb5d062ff
...
e1395f20ef
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e1395f20ef | ||
|
|
a0b68d148b | ||
|
|
7a41d1f647 | ||
|
|
24f28dd9c8 | ||
|
|
c8e49aead8 | ||
|
|
bb32cca6f9 | ||
|
|
cb160a6881 | ||
|
|
4e26f3710b | ||
|
|
273ad72f0c | ||
|
|
73e371fb40 |
No files matched your search
@@ -0,0 +1,74 @@
|
||||
name: CI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: ["main"]
|
||||
pull_request:
|
||||
branches: ["main"]
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
build-and-check:
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 120
|
||||
steps:
|
||||
- name: Check out
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Install Lean toolchain
|
||||
run: |
|
||||
curl -sSfL https://raw.githubusercontent.com/leanprover/elan/master/elan-init.sh \
|
||||
| sh -s -- -y --default-toolchain none
|
||||
echo "$HOME/.elan/bin" >> "$GITHUB_PATH"
|
||||
|
||||
# Mathlib is pinned in `lake-manifest.json`; the cache supplies its
|
||||
# prebuilt `.olean` files so the job only compiles this project.
|
||||
- name: Cache Lake packages and build artifacts
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
path: |
|
||||
.lake
|
||||
~/.elan
|
||||
key: lake-${{ runner.os }}-${{ hashFiles('lean-toolchain', 'lake-manifest.json', 'lakefile.toml') }}
|
||||
restore-keys: |
|
||||
lake-${{ runner.os }}-
|
||||
|
||||
- name: Fetch Mathlib cache
|
||||
run: lake exe cache get
|
||||
|
||||
- name: Build Lean
|
||||
run: lake build
|
||||
|
||||
# bwrap-free replacement for the comparator's axiom check; catches
|
||||
# `sorryAx` and any newly introduced axiom in the headline theorems.
|
||||
- name: Check axioms
|
||||
run: ./scripts/check-axioms.sh
|
||||
|
||||
- name: Install C++ toolchain
|
||||
run: |
|
||||
if command -v sudo >/dev/null 2>&1; then SUDO=sudo; else SUDO=; fi
|
||||
$SUDO apt-get update
|
||||
$SUDO apt-get install -y clang cmake ninja-build
|
||||
clang_major="$(clang++ -dumpversion | cut -d. -f1)"
|
||||
$SUDO apt-get install -y "llvm-${clang_major}"
|
||||
|
||||
- name: Configure and build C++
|
||||
run: |
|
||||
cmake -S cpp -B cpp/build -G Ninja \
|
||||
-DCMAKE_CXX_COMPILER=clang++ \
|
||||
-DCR_BUILD_FUZZERS=OFF \
|
||||
-DCR_BUILD_BENCH=OFF
|
||||
cmake --build cpp/build
|
||||
|
||||
# `golden_test` and `diff_corpus` read vectors generated by the actual
|
||||
# Lean `Statement.closedForm`; the file is gitignored.
|
||||
- name: Generate golden vectors
|
||||
run: lake env lean --run cpp/oracle/gen.lean > cpp/oracle/golden.txt
|
||||
|
||||
- name: Run C++ tests
|
||||
run: ctest --test-dir cpp/build --output-on-failure
|
||||
|
||||
- name: Check C++ coverage
|
||||
run: |
|
||||
CXX="clang++-$(clang++ -dumpversion | cut -d. -f1)" \
|
||||
./cpp/run-coverage.sh
|
||||
@@ -1,3 +1,6 @@
|
||||
/-
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
-/
|
||||
import Statement
|
||||
import FormalConflictRange.SimpleClosedForm
|
||||
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
/-
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
-/
|
||||
import Statement
|
||||
|
||||
/-!
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
/-
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
-/
|
||||
|
||||
import Proof
|
||||
|
||||
/-!
|
||||
# Axiom audit for the headline theorems
|
||||
|
||||
`#print axioms` lists every axiom a declaration depends on. The expected
|
||||
output for both headlines is exactly `[propext, Classical.choice, Quot.sound]`;
|
||||
a `sorry` would show up as `sorryAx` and any new axiom would appear by name.
|
||||
`scripts/check-axioms.sh` (used by CI) enforces that output.
|
||||
-/
|
||||
|
||||
#print axioms Statement.closedFormMinimal
|
||||
#print axioms Statement.minimalUnique
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
import FormalConflictRange.Empty
|
||||
import FormalConflictRange.Nonpos
|
||||
@@ -16,18 +14,25 @@ The conflict-range inference has two layers.
|
||||
(unconditional) empty-result theory of `FormalConflictRange.Empty` and
|
||||
`FormalConflictRange.Nonpos`: a result with no keys is answered by the empty-result
|
||||
value `emptyValue`, and the remaining certified offset cases are handled as in
|
||||
`FormalConflictRange.Sound`. **This is the object of record**: the correctness
|
||||
statement is `conflictRangePartial_spec`, and the open frontier is exactly the
|
||||
set of inputs on which it returns `none`. When that set is empty the algorithm is
|
||||
total and is promoted to the `ConflictRange`-valued algorithm.
|
||||
`FormalConflictRange.Sound`. The correctness statement is
|
||||
`conflictRangePartial_spec`: wherever the function returns `some cr`, that range
|
||||
is minimal. The inputs on which it returns `none` are the cases this case-analysis
|
||||
route did not certify. The headline theorem is proved independently in
|
||||
`Proof.lean` through the closed-form development
|
||||
(`FormalConflictRange.ClosedFormAll`). This module also supplies that
|
||||
development's empty-result theory — `emptyValue`, `EmptyInferenceWitness`,
|
||||
and `minimal_emptyValue_of_emptyInference` — so the partial-algorithm
|
||||
sections below are a record of the case analysis, while the empty-result
|
||||
declarations are shared with the final proof.
|
||||
* The provisional totalization `conflictRangeVal`, assembled by *reduction to the
|
||||
empty reads* rather than case analysis. It is the every-input wrapper used to
|
||||
*state* the general reduction: it dispatches on emptiness, truncation, and
|
||||
traversal and returns a `ConflictRange` on the certified regions. Off them its
|
||||
value is a placeholder whose hull fallback is not sound in general (see
|
||||
`FormalConflictRange.Counterexample`), and its `_spec` theorems are relative to
|
||||
the open `EmptyTheoryComplete`. It is kept only until `conflictRangePartial` is
|
||||
total, at which point it is replaced. It dispatches as follows:
|
||||
value is a placeholder whose hull fallback is not sound in general (a complete
|
||||
read whose reported keys are not the endpoint keys of the hull gives a
|
||||
counterexample), and its `_spec` theorems are relative to the conditional
|
||||
`EmptyTheoryComplete`. It is kept only as the intermediate reduction interface.
|
||||
It dispatches as follows:
|
||||
|
||||
- an empty result is answered by the empty theory's value `emptyValue`;
|
||||
- a forward truncated result is `[w, keyAfter kₙ)`: the begin selector's empty
|
||||
@@ -54,15 +59,15 @@ The conflict-range inference has two layers.
|
||||
stretched to the complete value's endpoints (`BoundaryNecessity`), the content the
|
||||
empty inference supplies as `ψ` at the two boundary reads. Hull soundness is derived
|
||||
from `EmptyInference` for all bridge factors except the bracket (`BracketFactor`),
|
||||
which is the notes' observability residue. The complete branch is then
|
||||
which is the bracket-observability residue. The complete branch is then
|
||||
`minimal_completeRange_of_factors`/`_rev`, with the interior derived rather than
|
||||
assumed; `GeneralObligation` adds only the two truncated dispatch values. Its
|
||||
conditional correctness statement is `conflictRangeVal_spec` (relative to
|
||||
`EmptyTheoryComplete`); the algorithm of record remains `conflictRangePartial`. In the
|
||||
canonical region (`begin.offset = 1`, `end.offset ≤ 1`, canonical endpoints) the complete
|
||||
branch is unconditional
|
||||
`EmptyTheoryComplete`); the certified algorithm in this module remains
|
||||
`conflictRangePartial`. In the canonical region (`begin.offset = 1`,
|
||||
`end.offset ≤ 1`, canonical endpoints) the complete branch is unconditional
|
||||
(`minimal_completeRange_canonical`), needing neither boundary hypothesis. There are
|
||||
no `sorry`s: the open content appears only as those explicit hypotheses.
|
||||
no `sorry`s: the uncertified content appears only as those explicit hypotheses.
|
||||
|
||||
The reduction identity itself is `necessaryRange_completeRange_of_reported`: the
|
||||
complete value's necessary range is the lower boundary, the reported interval, and the
|
||||
@@ -77,11 +82,10 @@ The conflict-range inference has two layers.
|
||||
`EmptyInferenceWitness` records the empty theory in the witness-exposing form the graft
|
||||
consumes and implies `EmptyInference` (`emptyInference_of_witness`). The one genuine
|
||||
extraction — turning an abstract `Minimal` for a boundary read into a full-compatible
|
||||
witness database — remains the length-sensitive joint-base graft; it is the reason the
|
||||
boundary content is a hypothesis and not a consequence of `EmptyInference` alone. (It is
|
||||
in fact *false*, not merely unproved:
|
||||
`FormalConflictRange.Counterexample.cw_notBoundaryJointBases` refutes `BoundaryJointBases`
|
||||
on the `cwRead`/`cwRes` witness.)
|
||||
witness database — is the length-sensitive joint-base graft; it is the reason the
|
||||
boundary content is a hypothesis and not a consequence of `EmptyInference` alone. It is
|
||||
in fact *false*, not merely unproved: a two-key witness makes the hull endpoints differ
|
||||
from the reported interval's, so `BoundaryJointBases` fails for it.
|
||||
|
||||
The pushed-down interface makes the confinement of offsets explicit. The complete
|
||||
branch's nonempty content is `CompleteObligation`, whose fields are the generic graft
|
||||
@@ -94,17 +98,17 @@ The conflict-range inference has two layers.
|
||||
the witness-exposing empty oracle, the generic graft, the bracket gap split, and the
|
||||
truncated one-sided graft. From it, `conflictRangeVal_spec_complete` proves the general
|
||||
algorithm correct with `Readable` the only other hypothesis, and
|
||||
`exists_minimal_conflict_range_of_complete` gives the existence conjecture. The graft
|
||||
fields (`joint`, `truncated`) are guarded by `Readable`: a bridge database exists only
|
||||
when the reported keys are the complete keys of some database, so for an unrealizable
|
||||
pair `BoundaryJointBases`/`GeneralObligation` are outright false (a duplicate reported
|
||||
key can never satisfy `midEq`). The unconditional headline claim is *not* proved here: it
|
||||
is stated with explicit `sorry`s in `FormalConflictRange.Open` (which is built but
|
||||
deliberately not imported by this module). All three non-witness fields of
|
||||
`exists_minimal_conflict_range_of_complete` gives existence from the same hypotheses.
|
||||
The graft fields (`joint`, `truncated`) are guarded by `Readable`: a bridge database
|
||||
exists only when the reported keys are the complete keys of some database, so for an
|
||||
unrealizable pair `BoundaryJointBases`/`GeneralObligation` are outright false (a
|
||||
duplicate reported key can never satisfy `midEq`). This conditional route is not the
|
||||
headline proof: the unconditional statement is proved in `Proof.lean`, where the closed
|
||||
form is established directly for the reference development. All three non-witness
|
||||
fields of
|
||||
`EmptyTheoryComplete` are in fact *refuted* — `bracket` and `truncated` as before, and
|
||||
`joint` (`BoundaryJointBases`) by the same `cwRead`/`cwRes` witness that refutes the hull
|
||||
(`FormalConflictRange.Counterexample.cw_notBoundaryJointBases`), so the reduction is
|
||||
uninhabited rather than merely unproved.
|
||||
`joint` (`BoundaryJointBases`) by the two-key witness that also refutes the hull — so
|
||||
the reduction is uninhabited rather than merely unproved.
|
||||
|
||||
On the empty theory itself, the witness-exposing oracle is proved at
|
||||
`begin.offset = 1` (`emptyWitness_beginOne`), throughout the whole `end.offset ≤ 1`
|
||||
@@ -137,14 +141,16 @@ The conflict-range inference has two layers.
|
||||
`EmptyInferenceWitnessResidual` is therefore vacuous: the whole oracle is
|
||||
`emptyInferenceWitness`, unconditionally, and `emptyInference : EmptyInference` follows.
|
||||
What remains of the empty theory is only the graft and gap-split fields of
|
||||
`EmptyTheoryComplete`, packaged by `emptyTheoryComplete_of_witness`; those fields are the
|
||||
open `sorry`s of `FormalConflictRange.Open`. The gap-split field `BracketFactor` is in
|
||||
fact *false* as stated on the old hull, by the kernel-checked witness in
|
||||
`FormalConflictRange.Counterexample`; `completeValue` routes the canonical region
|
||||
`1 ≤ begin.offset ∧ end.offset ≤ 1` around the hull precisely so that this pair is
|
||||
answered correctly without it.
|
||||
`EmptyTheoryComplete`, packaged by `emptyTheoryComplete_of_witness`. The gap-split field
|
||||
`BracketFactor` is in fact *false* as stated on the hull: a compatible database can
|
||||
satisfy the boundary conditions while the bracket fails. `completeValue` routes the
|
||||
canonical region `1 ≤ begin.offset ∧ end.offset ≤ 1` around the hull precisely so that
|
||||
this pair is answered correctly without it.
|
||||
|
||||
The reduction is the subject of section 7 of `notes/main.tex`.
|
||||
The reduction described here was the working route to the closed form; the final
|
||||
argument in `Proof.lean` uses the closed-form theorems instead. The empty-result
|
||||
part of this module (`emptyValue`, `EmptyInference`, and
|
||||
`minimal_emptyValue_of_emptyInference`) is shared with that development.
|
||||
-/
|
||||
|
||||
namespace FormalConflictRange
|
||||
@@ -152,8 +158,8 @@ namespace FormalConflictRange
|
||||
/-!
|
||||
## A partial algorithm: the certified cases
|
||||
|
||||
The general existence conjecture is open, but the theorems above certify a region
|
||||
of reads. Package them into one computable `Option`-valued function,
|
||||
The theorems above certify a region of reads. Package them into one computable
|
||||
`Option`-valued function,
|
||||
`conflictRangePartial`, returning `some` range exactly where a certificate exists
|
||||
and `none` otherwise. This is the honest encoding of "proved only for certain
|
||||
cases": it is total in type but makes absence explicit, and `none` carries no
|
||||
@@ -393,7 +399,7 @@ abbrev zeroCrossCompleteRev (r : RangeRead) (res : Result) : Prop :=
|
||||
## The empty-result value and its inference
|
||||
|
||||
The empty-result value `emptyValue` and the standing hypothesis `EmptyInference` are
|
||||
declared here, before `conflictRangePartial`, because the algorithm of record consults
|
||||
declared here, before `conflictRangePartial`, because the certified algorithm consults
|
||||
them directly: an empty result is answered by `emptyValue r`, and the certified branch
|
||||
of the partial algorithm is exactly the empty-result inference. The witness oracle and
|
||||
the unconditional proof of `EmptyInference` live later in this file, so the inference is
|
||||
@@ -523,9 +529,10 @@ def conflictRangePartial (r : RangeRead) (res : Result) : Option ConflictRange :
|
||||
-- (`minimal_canonicalRange_of_end_offset_le_one_general`). A truncated
|
||||
-- (`more = true`) forward result now has it too
|
||||
-- (`minimal_canonicalRange_of_truncated_end_offset_nonpos`): FoundationDB's bare
|
||||
-- successor `[begin.key, keyAfter (last reported))` is *unsound* there
|
||||
-- (`FormalConflictRange.Counterexample.tr_notSound`) because the resolved end
|
||||
-- clamps below `end_.key`, but the canonical upper end `end_.key` is correct. A
|
||||
-- successor `[begin.key, keyAfter (last reported))` is *unsound* there: a
|
||||
-- compatible database with a nonpositive end offset realises the failure,
|
||||
-- because the resolved end clamps below `end_.key`, but the canonical upper
|
||||
-- end `end_.key` is correct. A
|
||||
-- truncated *reversed* result has the reversed truncated range
|
||||
-- `[last reported, end_.key)` (`minimal_truncatedRangeRev_of_end_offset_nonpos`):
|
||||
-- the top `(1 - end_.offset)` canonical keys are dropped, but filtering at or
|
||||
@@ -544,10 +551,11 @@ def conflictRangePartial (r : RangeRead) (res : Result) : Option ConflictRange :
|
||||
-- (`getRangeFinished`, `NativeAPI.actor.cpp:4770-4785`). Left uncertified:
|
||||
-- an `end_.offset ≥ 2` can run past the data even when a reported key lies at
|
||||
-- or above `end_.key`, so the end anchor is not always observable and the
|
||||
-- candidate need not be sound. The cell has no single shape: the `cwMin` value
|
||||
-- (bare successor of the greatest reported key) is minimal at `end.offset = 2`
|
||||
-- but fails for larger slack, where the window runs past the reported last
|
||||
-- (`FormalConflictRange.Counterexample.ns_cand_notSound`, true value the top).
|
||||
-- candidate need not be sound. The cell has no single shape: the
|
||||
-- bare-successor value (of the greatest reported key) is minimal at
|
||||
-- `end.offset = 2`
|
||||
-- but fails for larger slack, where the window runs past the reported last (a
|
||||
-- compatible witness realises this; the true value is the top).
|
||||
none /- end offset at least two but not overshooting: no certificate -/
|
||||
else if _h : 1 ≤ r.begin.offset then
|
||||
-- A nonempty result at `begin.offset ≥ 1` (here `begin.offset ≠ 1`, so at least two)
|
||||
@@ -592,9 +600,10 @@ def conflictRangePartial (r : RangeRead) (res : Result) : Option ConflictRange :
|
||||
some (truncatedRange r (res.keys.getLastD ([] : Key)))
|
||||
else
|
||||
-- Truncated *reversed* at `begin.offset ≠ 1`, `end_.offset ≥ 2`, overshooting: the
|
||||
-- reversed value `topOfLast` is unsound here (`Counterexample.ev_topOfLast_notSound`),
|
||||
-- because the begin selector skips keys below the reported last that the candidate
|
||||
-- cannot see. The minimal value is instead the unbounded top from `begin.key`,
|
||||
-- reversed value `topOfLast` is unsound here (a compatible witness realises
|
||||
-- this), because the begin selector skips keys below the reported last that
|
||||
-- the candidate cannot see. The minimal value is instead the unbounded top
|
||||
-- from `begin.key`,
|
||||
-- `[begin.key, ⊤)` (`minimal_topRange_of_end_offset_ge_two_rev_more`): the begin
|
||||
-- key is necessary, and convexity through the reported keys with
|
||||
-- `necessary_of_end_offset_ge_two_rev` above the head forces the whole interval.
|
||||
@@ -706,7 +715,7 @@ least. The two `rangeBegin`/`rangeEnd` clamp steps mirror the
|
||||
`NativeAPI.actor.cpp:4739-4794`), parameterized by the `readToBegin` and
|
||||
`readThroughEnd` flags. A `true` flag replaces the corresponding endpoint by
|
||||
`⊥`/`⊤`. This records FoundationDB's choice; the formal development does not
|
||||
assert it minimal on the open branches (see the section note above), except for
|
||||
assert it minimal on the uncertified branches (see the module docstring), except for
|
||||
`minimal_canonicalRange_of_underEnd_empty_conj` on the empty `underEnd` case. -/
|
||||
def fdbRange (r : RangeRead) (res : Result) (readToBegin readThroughEnd : Bool) :
|
||||
ConflictRange :=
|
||||
@@ -1129,8 +1138,8 @@ def BoundaryNecessity (r : RangeRead) (res : Result) : Prop :=
|
||||
|
||||
/-- **The bracket-observability residue.** Agreement on the complete value fixes the
|
||||
bracketing predicate, i.e. whether the resolved endpoints straddle the reported span.
|
||||
This is the notes' equation~(obs): the one bridge factor that the emptiness and interval
|
||||
equality factors do not imply. It is discharged by the empty theory's gap split at
|
||||
This is the bracket-observability equation: the one bridge factor that the emptiness
|
||||
and interval equality factors do not imply. It is discharged by the empty theory's gap split at
|
||||
positive offsets (the stored counts in `[begin.key, k₀)` and `[end.key, kₙ)`), which is
|
||||
why the pins `resolve begin = k₀` / `resolve end_ = keyAfter kₙ` do not by themselves
|
||||
remove it. -/
|
||||
@@ -1168,11 +1177,11 @@ merely open:
|
||||
nonempty key): there the hull need not be minimal;
|
||||
* the `begin.offset ≠ 1` cell at an overshooting-free end, where the resolved begin may
|
||||
clamp to `⊤` on one side of an agreement and to a stored key on the other, so the hull
|
||||
is *unsound* (`FormalConflictRange.Counterexample.cw_notSound`). That cell has no
|
||||
single minimal shape either: the `cwMin` value (the bare successor of the greatest
|
||||
is *unsound* (a compatible witness refutes it). That cell has no
|
||||
single minimal shape either: the bare-successor value (the successor of the greatest
|
||||
reported key) at `end.offset = 2` fails once the end selector's slack runs the window
|
||||
past the reported last, as `Counterexample.ns_cand_notSound` records, with every key
|
||||
necessary (`ns_top_minimal`) and the true value the unbounded top (`ns_top_sound`).
|
||||
past the reported last, as a compatible witness shows, with every key necessary and
|
||||
the true value the unbounded top.
|
||||
|
||||
`conflictRangePartial` returns `none` on both, so `completeValue` is a totalization
|
||||
whose hull fallback is not correct off the canonical and overshoot regions. -/
|
||||
@@ -1196,17 +1205,15 @@ result is dispatched on emptiness, truncation, and traversal:
|
||||
The low and high contributions are the empty boundary reads' minimal ranges, each the
|
||||
empty range when its read is trivially empty; the definition is computable.
|
||||
|
||||
**This is a provisional totalization, not the object of record.** The
|
||||
**This is a provisional totalization, not the headline algorithm.** The
|
||||
certified algorithm is `conflictRangePartial`; `conflictRangeVal` is the
|
||||
every-input `ConflictRange`-valued wrapper, and its branches off the certified
|
||||
region (the truncated dispatch and the `completeValue` hull fallback) are *not*
|
||||
sound in general — the kernel-checked `FormalConflictRange.Counterexample`
|
||||
(`cw_notSound`, and the truncated-branch violations) record this. Its correctness
|
||||
sound in general — explicit counterexamples in the complete hull and in the
|
||||
truncated branches record this. Its correctness
|
||||
theorems (`conflictRangeVal_spec`, `_spec_of_witness`, `_spec_complete`) are stated
|
||||
relative to the open `EmptyTheoryComplete` and are consequently vacuous, not the
|
||||
headline claim. Once `conflictRangePartial` is proved total it becomes the
|
||||
`ConflictRange`-valued algorithm and this definition is subsumed by it; the naming
|
||||
change is deferred until then. -/
|
||||
relative to the conditional `EmptyTheoryComplete` and are consequently vacuous, not
|
||||
the headline claim. The unconditional statement is proved in `Proof.lean`. -/
|
||||
def conflictRangeVal (r : RangeRead) (res : Result) : ConflictRange :=
|
||||
if res.keys = [] then
|
||||
emptyValue r
|
||||
@@ -1219,7 +1226,7 @@ def conflictRangeVal (r : RangeRead) (res : Result) : ConflictRange :=
|
||||
/-!
|
||||
## The complete branch: the assembled hull
|
||||
|
||||
The complete branch is the section-7 reduction: the hull of the two boundary reads'
|
||||
The complete branch is the reduction: the hull of the two boundary reads'
|
||||
minimal ranges and the reported interval, necessary on the two boundary ranges
|
||||
(`BoundaryNecessity`) and the derived interior, and sound from `EmptyInference` once the
|
||||
bracket residue is supplied. -/
|
||||
@@ -1711,9 +1718,9 @@ theorem minimal_completeRange_of_jointBasesEmptyRev {r : RangeRead} {res : Resul
|
||||
ranges of `BoundaryNecessity`, obtained from the empty theory's flip witnesses, in
|
||||
whichever traversal order the read uses. This is the exact input the complete branch
|
||||
extracts from the empty theory beyond `EmptyInference`; the extraction itself is the
|
||||
length-sensitive graft recorded in the notes. It is *false* — the endpoints are those of the
|
||||
length-sensitive graft described above. It is *false* — the endpoints are those of the
|
||||
(unsound) hull, and `BoundaryNecessity` on them can demand keys that no sound range need
|
||||
contain (`FormalConflictRange.Counterexample.cw_notBoundaryJointBases`). -/
|
||||
contain (a two-key witness refutes it). -/
|
||||
def BoundaryJointBases (r : RangeRead) (res : Result) : Prop :=
|
||||
if r.reverse then
|
||||
LowerJointBaseRangeRev r res.keys (completeRange r res).begin
|
||||
@@ -1760,7 +1767,7 @@ theorem minimal_completeRange_of_factors_joint_rev {r : RangeRead} {res : Result
|
||||
/-- **The complete branch, from `EmptyInferenceWitness` and the joint bases.** The
|
||||
witness-exposing oracle supplies soundness (`EmptyInference` via
|
||||
`emptyInference_of_witness`); the joint bases remain the one extraction hypothesis (false in
|
||||
general: `FormalConflictRange.Counterexample.cw_notBoundaryJointBases`). This
|
||||
general: a two-key witness refutes it). This
|
||||
is the sharpest form of the reduction available: the reported keys and their convex hull
|
||||
are derived, and the boundary necessity is stated at the witness level rather than as the
|
||||
finished `BoundaryNecessity`. -/
|
||||
@@ -2055,16 +2062,17 @@ The forward/reverse truncated values are the lower/upper boundary contribution's
|
||||
begin/end glued to the reported interval. The offset-one certificates above are the
|
||||
same statement via `emptyValue`; the general statement is the same reduction as the
|
||||
complete branch, with the far boundary read replaced by the reported interval. Those
|
||||
reductions are the remaining obligations of the truncated branches and are stated in
|
||||
the notes.
|
||||
reductions are the remaining obligations of the truncated branches and are stated
|
||||
separately.
|
||||
-/
|
||||
|
||||
/-!
|
||||
## The empty branch
|
||||
|
||||
The empty result is answered by `emptyValue`, reading off the empty theory's cases.
|
||||
The offset-one underEnd case is certified below; the remaining offsets are the open
|
||||
empty theory.
|
||||
The offset-one underEnd case is certified below; the remaining offsets are handled
|
||||
by the separate empty-theory development (`FormalConflictRange.Empty`,
|
||||
`FormalConflictRange.Nonpos`).
|
||||
-/
|
||||
|
||||
/-- **The empty branch, `begin.offset = 1`, `end.offset ≤ 1`.** A non-trivially-empty
|
||||
@@ -2103,7 +2111,7 @@ theorem minimal_emptyValue_underEnd (r : RangeRead) (hbegin : r.begin.offset = 1
|
||||
Traversal is invisible to the empty inference, and `emptyValue` never mentions
|
||||
`reverse`, so `EmptyInference` reduces to its non-reversed instance; and the
|
||||
`begin.offset = 1` instance splits by the end offset into the three empty cases already
|
||||
in the library. This pins the open empty-theory content to `begin.offset ≠ 1`.
|
||||
in the library. This pins the uncertified empty-theory content to `begin.offset ≠ 1`.
|
||||
-/
|
||||
|
||||
/-- `AlwaysEmpty` is invariant under flipping the traversal. -/
|
||||
@@ -2473,7 +2481,7 @@ theorem conflictRangeVal_spec_of_witness {r : RangeRead} {res : Result}
|
||||
exact minimal_completeRange_of_witness_joint_rev hempty ⟨db, hcomp⟩ hr hm hkeys hp hlim
|
||||
hc.joint hbr
|
||||
|
||||
/-- The existence conjecture follows from the witness-exposing empty oracle and the two
|
||||
/-- Existence follows from the witness-exposing empty oracle and the two
|
||||
nonempty obligations: the value witnesses a minimal sound range, with all offset handling
|
||||
confined to the empty theory. -/
|
||||
theorem conjecture_exists_minimal_conflict_range_of_witness {r : RangeRead} {res : Result}
|
||||
@@ -2524,7 +2532,7 @@ theorem conflictRangeVal_spec {r : RangeRead} {res : Result}
|
||||
exact minimal_completeRange_of_factors hempty hread (by simpa using hr) hm hkeys
|
||||
hψ hbr
|
||||
|
||||
/-- The existence conjecture follows from the empty inference, the boundary `ψ`, the
|
||||
/-- Existence follows from the empty inference, the boundary `ψ`, the
|
||||
bracket factor, and the truncated obligation: the value witnesses a minimal sound
|
||||
range. -/
|
||||
theorem conjecture_exists_minimal_conflict_range {r : RangeRead} {res : Result}
|
||||
@@ -2548,12 +2556,11 @@ empty theory's analysis at the boundary reads; none is an offset analysis of the
|
||||
read. With them the general case is the offset-free assembly.
|
||||
|
||||
`EmptyTheoryComplete` implies the abstract `EmptyInference` (`EmptyTheoryComplete.emptyInference`)
|
||||
and the conjecture (`exists_minimal_conflict_range_of_complete`). It is the exact form of
|
||||
"the empty theory is finished": there is nothing else to prove. In fact it is *refuted*:
|
||||
all three of `joint`, `bracket`, and `truncated` are false, independently
|
||||
(`FormalConflictRange.Counterexample.cw_emptyTheoryComplete_false`,
|
||||
`ce_emptyTheoryComplete_false`, `tr_emptyTheoryComplete_false`), so the structure is
|
||||
uninhabited and the reduction is not a route to the headline. -/
|
||||
and the existence statement (`exists_minimal_conflict_range_of_complete`). It is the exact
|
||||
form of "the empty theory is finished": there is nothing else to prove. In fact it is
|
||||
*refuted*: each of `joint`, `bracket`, and `truncated` has an explicit compatible
|
||||
counterexample, so the structure is uninhabited and the reduction is not a route to the
|
||||
headline. -/
|
||||
|
||||
/-- **A complete empty theory, in reduction form.** The empty-result theory together with
|
||||
the generic graft, the bracket gap split, and the truncated one-sided graft. Every field
|
||||
@@ -2616,7 +2623,7 @@ theorem conflictRangeVal_spec_complete (H : EmptyTheoryComplete) {r : RangeRead}
|
||||
conflictRangeVal_spec_of_witness H.witness (H.truncated r res hread)
|
||||
(H.completeObligation r res hread) hread
|
||||
|
||||
/-- **The existence conjecture from a complete empty theory.** -/
|
||||
/-- **Existence from a complete empty theory.** -/
|
||||
theorem exists_minimal_conflict_range_of_complete (H : EmptyTheoryComplete) {r : RangeRead}
|
||||
{res : Result} (hread : Readable r res) :
|
||||
∃ cr : ConflictRange, Minimal r res cr :=
|
||||
@@ -3102,8 +3109,7 @@ With `begin.offset ≤ 0`, `end.offset = 1` and `begin.key ≤ end.key`, `emptyV
|
||||
bottom range `[⊥, end.key)`, and every key below `end.key` has the singleton flip witness:
|
||||
the empty database is compatible (`compatible_emptyDB_emptyRes`) and toggling `k` gives
|
||||
`{k}`, whose complete keys are nonempty (`not_compatible_singleton_self`). This is the
|
||||
witness-exposing companion of `minimal_bottomRange_of_nonpos_end_one`, and it is the part
|
||||
of issue #19's item 1 that the single-key witness still separates. -/
|
||||
witness-exposing companion of `minimal_bottomRange_of_nonpos_end_one`. -/
|
||||
theorem emptyWitness_nonpos_end_one_crossFree (r : RangeRead) (hbegin : r.begin.offset ≤ 0)
|
||||
(hend : r.end_.offset = 1) (hle : r.begin.key ≤ r.end_.key)
|
||||
(_hread : Readable r { keys := [], more := false }) :
|
||||
@@ -4133,11 +4139,11 @@ range on every realizable read/result pair. This section makes the objective pre
|
||||
locates it relative to the existence headline. As a *function*, the objective is not
|
||||
stronger than the headline: given the headline, `Classical.choice` turns the pointwise
|
||||
existentials into a total `ConflictRange`-valued function, and `conflictRangeTotal` below
|
||||
refines the algorithm of record `conflictRangePartial` — it agrees with it on every
|
||||
refines the certified algorithm `conflictRangePartial` — it agrees with it on every
|
||||
certified `some` value, and on the uncertified `none` branches returns the chosen minimal
|
||||
range. What would be strictly stronger is a *computable* total algorithm, i.e. branch
|
||||
completeness of `conflictRangePartial`, which is blocked on exactly the regions where no
|
||||
offset-formula candidate exists (`cwMin` versus the unbounded top).
|
||||
offset-formula candidate exists (the bare successor versus the unbounded top).
|
||||
-/
|
||||
|
||||
/-- The desired outcome, as a proposition: a total `ConflictRange`-valued algorithm that
|
||||
@@ -4169,7 +4175,7 @@ theorem totalMinimalAlgorithm_iff :
|
||||
rw [dite_eq_left hh]
|
||||
exact Classical.choose_spec hh
|
||||
|
||||
/-- **A totalization of the algorithm of record.** Given the headline, `conflictRangePartial`
|
||||
/-- **A totalization of the certified algorithm.** Given the headline, `conflictRangePartial`
|
||||
extends to a total algorithm that is minimal on every realizable pair: it keeps the certified
|
||||
value where `conflictRangePartial` returns `some`, and on the uncertified `none` branches
|
||||
returns the (classically chosen) minimal range. Making it *computable* — filling the `none`
|
||||
@@ -4200,7 +4206,7 @@ theorem conflictRangeTotal_spec
|
||||
rw [dite_eq_left hh]
|
||||
exact Classical.choose_spec hh
|
||||
|
||||
/-- `conflictRangeTotal` agrees with the algorithm of record on every certified `some`
|
||||
/-- `conflictRangeTotal` agrees with the certified algorithm on every certified `some`
|
||||
value. -/
|
||||
theorem conflictRangeTotal_eq_partial
|
||||
(h : ∀ r res, Readable r res → ∃ cr : ConflictRange, Minimal r res cr)
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
import Mathlib
|
||||
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
import FormalConflictRange.General
|
||||
import FormalConflictRange.Sound
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
import FormalConflictRange.CrossedTruncatedGeneral
|
||||
import FormalConflictRange.CrossedRevGeneral
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
|
||||
import FormalConflictRange.CrossedRev
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
import FormalConflictRange.ComputableHull
|
||||
import FormalConflictRange.ComputableSearch
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
import FormalConflictRange.General
|
||||
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
|
||||
import FormalConflictRange.FiniteCountDecider
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
import FormalConflictRange.RangeRead
|
||||
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
import FormalConflictRange.General
|
||||
import FormalConflictRange.Prototype
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
|
||||
import FormalConflictRange.Residual
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
import FormalConflictRange.Empty
|
||||
import FormalConflictRange.ClosedForm
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
import FormalConflictRange.CrossedTruncated
|
||||
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
|
||||
import FormalConflictRange.CrossedRevGeneral
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
|
||||
import FormalConflictRange.CrossedResidual
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
|
||||
import FormalConflictRange.CrossedTruncated
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
|
||||
import FormalConflictRange.CrossedRev
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
import FormalConflictRange.CrossedClosedForm
|
||||
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
/-
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
-/
|
||||
import FormalConflictRange.CrossedGeneral
|
||||
|
||||
/-!
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
|
||||
import Mathlib
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
import FormalConflictRange.Sound
|
||||
|
||||
@@ -2297,8 +2295,8 @@ disjunct is unsatisfiable and `ψ` reduces to "no stored key `≥ B`".
|
||||
/-- **The emptiness predicate.** At `begin.offset = 1` and `end_.offset ≥ 1`, an
|
||||
empty complete result is compatible with `db` exactly when either no stored key
|
||||
reaches `begin.key` (`c_B = n`) or the count difference is at least
|
||||
`end_.offset - 1`. This is the formal counterpart of the predicate `ψ` derived in
|
||||
the notes, and it holds regardless of the relative order of the selector keys. -/
|
||||
`end_.offset - 1`. This is the formal counterpart of the predicate `ψ` derived from the
|
||||
slice characterization, and it holds regardless of the relative order of the selector keys. -/
|
||||
theorem completeKeys_eq_nil_iff_counts {db : DB} {r : RangeRead}
|
||||
(hbegin : r.begin.offset = 1) (hoE : 1 ≤ r.end_.offset) :
|
||||
RangeRead.completeKeys db r = [] ↔
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
|
||||
import FormalConflictRange.CountBridge
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
import FormalConflictRange.Prototype
|
||||
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
/-
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
-/
|
||||
import FormalConflictRange.NegCrossComplete
|
||||
|
||||
/-!
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
import FormalConflictRange.NegCrossComplete
|
||||
import FormalConflictRange.NegCrossLongNonpos
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
import FormalConflictRange.NegCrossBottom
|
||||
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
import FormalConflictRange.CrossedGeneral
|
||||
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
import FormalConflictRange.NonposClosedForm
|
||||
import FormalConflictRange.NegCrossClosedForm
|
||||
@@ -537,7 +535,7 @@ abbrev negCrossEndOne (r : RangeRead) (res : Result) : Prop :=
|
||||
((res.keys.headD ([] : Key) : Key) : WithTop Key) < r.begin.key ∧
|
||||
beginSlack r < reportedBelowBegin r res ∧ r.begin.key ≤ r.end_.key
|
||||
|
||||
/-- The closed form for the crossed negative-begin complete cell at `end.offset = 1`:
|
||||
/-- The closed form for the crossed negative-begin complete cell at `end.offset = 1`:
|
||||
`[head, end_.key)`. -/
|
||||
noncomputable def closedFormNegCrossEndOne (r : RangeRead) (res : Result) : ConflictRange :=
|
||||
⟨((res.keys.headD ([] : Key) : Key) : WithTop Key), r.end_.key⟩
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
import FormalConflictRange.NegCrossCompleteResidual
|
||||
|
||||
@@ -20,8 +18,8 @@ last reported key, `q = beginSlack` and `r = reportedBelowBegin`, put `C = q + 1
|
||||
(so `C ≥ 1` in the clamping cell). The upper endpoint is `begin.key` when the interval
|
||||
`[keyAfter L, begin.key)` holds `C` keys (or `C + 1` keys when `end.key = ⊥`), and
|
||||
`keyAfter L` otherwise. The witness `res = [[5], [6]]`, `end.key = [1]`,
|
||||
`begin.key = [6,0,0]`, `begin.offset = -3`, `end.offset = 3` is kernel-checked in
|
||||
`Counterexample.lean` (`nch_M_not_minimal`): every compatible database then has
|
||||
`begin.key = [6,0,0]`, `begin.offset = -3`, `end.offset = 3` refutes the rule:
|
||||
every compatible database then has
|
||||
`begin.key = [6,0,0]` strictly above `keyAfter last = [6,0]`, but the extra key `[6,0]`
|
||||
lies in no sound range, so the `max` rule over-collects it.
|
||||
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
import FormalConflictRange.NegCrossComplete
|
||||
import FormalConflictRange.NegCrossClosedForm
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
import FormalConflictRange.NegCrossCompleteHigh
|
||||
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
|
||||
import FormalConflictRange.NegCrossResidual
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
import FormalConflictRange.NegCrossClampNonpos
|
||||
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
|
||||
import FormalConflictRange.NegCrossLongNonpos
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
|
||||
import FormalConflictRange.NegCrossTruncClampLastGe
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
|
||||
import FormalConflictRange.NegCrossTruncNonclamp
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
|
||||
import FormalConflictRange.NegCrossTruncClamp
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
|
||||
import FormalConflictRange.NegCrossClampNonpos
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
import FormalConflictRange.NegCrossClampLongNonpos
|
||||
import FormalConflictRange.NonposRevClosedForm
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
|
||||
import FormalConflictRange.NegCrossComplete
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
|
||||
import FormalConflictRange.NegCrossTruncNonclamp
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
|
||||
import FormalConflictRange.NegCrossTruncBottomGap
|
||||
@@ -30,10 +28,12 @@ The soundness half is complete.
|
||||
transports the resolution across agreement on that gap; this yields soundness of
|
||||
`[head, U)` (`sound_headLong_of_nonbottom`).
|
||||
|
||||
The necessity half is still open; see `notes/main.tex`. It splits into toggle witnesses
|
||||
The necessity half is proved in the companion modules
|
||||
`NegCrossTruncNonposGapA2a`, `NegCrossTruncNonposGapA2aBottom`, and
|
||||
`NegCrossTruncNonposGapCrossFree`. It splits into toggle witnesses
|
||||
for keys in `(last, end_.key)` (erase) and in `[end_.key, U)` (insert), with the filler
|
||||
budget determined by `clampNonposBottom`. The closed form `closedFormNegCrossTruncNonposGap`
|
||||
is recorded here so the dispatch can be wired once the necessity lemmas land.
|
||||
is defined here; the companion modules prove it minimal on the three subcells.
|
||||
-/
|
||||
|
||||
namespace FormalConflictRange
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
|
||||
import FormalConflictRange.NegCrossTruncNonposGap
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
|
||||
import FormalConflictRange.NegCrossTruncNonposGapA2a
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
import FormalConflictRange.NegCrossTruncNonposGapA2aBottom
|
||||
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
|
||||
import FormalConflictRange.NegCrossTruncRevFinite
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
|
||||
import FormalConflictRange.NegCrossTruncRevBeginTop
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
|
||||
import FormalConflictRange.NegCrossTruncRevBeginTop
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
|
||||
import FormalConflictRange.NegCrossTruncRevCrossedSmall
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
|
||||
import FormalConflictRange.NegCrossTruncRevCrossedLarge
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
|
||||
import FormalConflictRange.NegCrossTruncRevNonpos
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
|
||||
import FormalConflictRange.NegCrossTruncRevTop
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
|
||||
import FormalConflictRange.NegCrossTruncRevBeginTop
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
|
||||
import FormalConflictRange.NegCrossTruncRevCrossedNonpos
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
|
||||
import FormalConflictRange.NegCrossTruncRevResidualLargeSentinel
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
|
||||
import FormalConflictRange.NegCrossTruncRevResidualFinite
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
|
||||
import FormalConflictRange.NegCrossTruncRevResidualLargeTop
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
|
||||
import FormalConflictRange.NegCrossTruncRevResidual
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
|
||||
import FormalConflictRange.NegCrossTruncClamp
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
|
||||
import FormalConflictRange.NegCrossTruncClampLastGe
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
|
||||
import FormalConflictRange.NegCrossTruncStrictTop
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
import FormalConflictRange.Empty
|
||||
import FormalConflictRange.Reduction
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
|
||||
import FormalConflictRange.Residual
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
|
||||
import FormalConflictRange.NonposClosedForm
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
|
||||
import FormalConflictRange.Algorithm
|
||||
@@ -79,7 +77,7 @@ conjoins its argument.
|
||||
* `hullRanges`, `collectReadOff`, `sound_collectReadOff` — the concrete computable
|
||||
read-off (hull of the required boundary `emptyValue`s and the bracket/core supports)
|
||||
and its **unconditional** soundness, for any realizable read/result with nonempty
|
||||
ascending reported keys. It validates on the old counterexample `ceRead`, whose
|
||||
ascending reported keys. It validates on an earlier counterexample whose
|
||||
`completeRange` was unsound.
|
||||
* `sound_congr_toSet`, `minimal_congr_toSet`, `minimal_collectReadOff_of_subrange` —
|
||||
soundness/minimality depend on a range only through its key set, so the read-off is
|
||||
@@ -99,14 +97,15 @@ conjoins its argument.
|
||||
it contained in the minimal range; `minimal_inter_collectReadOff_of_minimal` is the
|
||||
wrapper for the read-off, and `minimal_inter_collectReadOff_truncated_fwd`/`_rev` (+ the
|
||||
overshoot variant) apply it to the truncated branch, where the read-off alone overshoots
|
||||
(`Counterexample.ct_collectReadOff_not_minimal`).
|
||||
(an explicit truncated witness).
|
||||
* `reported_keys_bounds`, `subrange_collectReadOff_canonical`,
|
||||
`collectReadOff_toSet_eq_canonical`, `minimal_collectReadOff_canonical` — the
|
||||
canonical-region containment, proved. For a complete nonempty realizable pair in
|
||||
`1 ≤ begin.offset ∧ end_.offset ≤ 1`, every reported key lies in `[begin.key, end.key)`,
|
||||
so each boundary `emptyValue` and bracket/core support is contained in the canonical
|
||||
range; the read-off is contained in it and, being sound, shares its key set. Thus
|
||||
`collectReadOff` is minimal there, extending end-to-end minimality beyond `ceRead`.
|
||||
`collectReadOff` is minimal there, extending end-to-end minimality beyond the earlier
|
||||
counterexamples.
|
||||
* `subrange_collectReadOff_topRange`, `minimal_collectReadOff_topRange` — the
|
||||
overshoot-region containment, proved. For a complete nonempty realizable pair in
|
||||
`1 ≤ begin.offset ∧ 2 ≤ end.offset` with every reported key below `end.key`, the
|
||||
@@ -483,7 +482,7 @@ theorem shortGap_not_le (db : DB) {g : ℕ} {lo hi : WithTop Key} (hfin : lo ≤
|
||||
/-- The constraints a complete nonempty result exposes, in difference form. `B`
|
||||
and `E` are the selector keys, `oB`/`oE` their offsets, `L` the reported keys.
|
||||
|
||||
The binding directions are chosen from the bridge analysis of the main notes,
|
||||
The binding directions are chosen from the bridge analysis of the main development,
|
||||
displayed in the single normal form `ρ hi − ρ lo ≤ n` (`ge` is `le` with the
|
||||
endpoints swapped and the bound negated):
|
||||
|
||||
@@ -2723,7 +2722,7 @@ theorem collectReadOff_toSet_eq_canonical {r : RangeRead} {res : Result}
|
||||
/-- **Minimality of the concrete read-off in the canonical region.** For a complete
|
||||
nonempty realizable read/result pair with `1 ≤ begin.offset` and `end_.offset ≤ 1`, the
|
||||
computable hull `collectReadOff` is a minimal sound conflict range. This extends
|
||||
end-to-end minimality beyond the counterexample `ceRead`. -/
|
||||
end-to-end minimality beyond the earlier counterexamples. -/
|
||||
theorem minimal_collectReadOff_canonical {r : RangeRead} {res : Result}
|
||||
(hread : Readable r res) (hmore : res.more = false) (hne : res.keys ≠ [])
|
||||
(hbegin : 1 ≤ r.begin.offset) (hend : r.end_.offset ≤ 1) :
|
||||
@@ -2922,7 +2921,7 @@ theorem minimal_collectReadOff_complete_general {r : RangeRead} {res : Result}
|
||||
|
||||
/-! ### The truncated branch through the meet
|
||||
|
||||
The read-off over-collects on a truncated result (`Counterexample.ct_collectReadOff_not_minimal`),
|
||||
The read-off over-collects on a truncated result (an explicit truncated witness),
|
||||
but its meet with the algorithm's proven-minimal truncated value is still minimal
|
||||
(`minimal_inter_of_sound_of_minimal`): the meet is sound, so it sits inside the minimal
|
||||
range, and being a subrange of it (the minimal range is itself a subrange of the
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
import FormalConflictRange.Selector
|
||||
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
import FormalConflictRange.Empty
|
||||
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
import FormalConflictRange.Empty
|
||||
|
||||
@@ -743,7 +741,7 @@ theorem compatible_iff_bridge_general {db : DB} {r : RangeRead} {A : List Key} {
|
||||
|
||||
The necessity half of the reduction never cites an end theorem directly: it needs
|
||||
only one full-compatible witness `db₀` and a single-key perturbation that breaks
|
||||
the targeted factor. The generic statement below is the notes' Lemma 7.1: a sound
|
||||
the targeted factor. The generic statement below is the factor-toggle lemma: a sound
|
||||
range can omit a key `k` only if flipping `k`'s membership in a compatible
|
||||
database preserves compatibility. Since the flip is outside the range, soundness
|
||||
forces compatibility on the flipped database; if that fails, `k` is necessary.
|
||||
@@ -766,7 +764,7 @@ theorem agreeOn_toggle_of_not_mem (cr : ConflictRange) (db : DB) {k : Key} (hk :
|
||||
AgreeOn cr db (toggle db k) :=
|
||||
(keys_toggle_of_not_mem cr db hk).symm
|
||||
|
||||
/-- **Factor-toggle (Lemma 7.1).** If some database is compatible with `(r, res)` but
|
||||
/-- **Factor-toggle.** If some database is compatible with `(r, res)` but
|
||||
flipping the membership of `k` there breaks compatibility, then `k` is necessary:
|
||||
any sound range omitting `k` would see both databases as agreeing, so soundness
|
||||
would transfer the compatibility, contradicting the break. -/
|
||||
@@ -782,9 +780,9 @@ theorem necessary_of_toggle {r : RangeRead} {res : Result} {k : Key}
|
||||
/-- **The factor-toggle lemma.** Suppose compatibility factors as `φ₁ ∧ φ₂` on
|
||||
databases, some database `db₀` satisfies both factors, and flipping the membership
|
||||
of `k` in `db₀` breaks `φ₁`. Then no range omitting `k` is sound: such a range sees
|
||||
`db₀` and its flip as agreeing, so soundness would carry `φ₁`. This is the notes'
|
||||
Lemma 7.1, and it is the form the necessity constructions use — only the one
|
||||
targeted factor must break. -/
|
||||
`db₀` and its flip as agreeing, so soundness would carry `φ₁`. This is the
|
||||
factor-toggle lemma in factored form, and it is the form the necessity
|
||||
constructions use — only the one targeted factor must break. -/
|
||||
theorem not_sound_of_factor_toggle {r : RangeRead} {res : Result} {cr : ConflictRange}
|
||||
{φ₁ φ₂ : DB → Prop} {db₀ : DB} {k : Key}
|
||||
(hcomp : ∀ db, RangeRead.Compatible db r res ↔ (φ₁ db ∧ φ₂ db))
|
||||
@@ -796,7 +794,7 @@ theorem not_sound_of_factor_toggle {r : RangeRead} {res : Result} {cr : Conflict
|
||||
(hs db₀ (toggle db₀ k) (agreeOn_toggle_of_not_mem cr db₀ hk)).mp hc₀
|
||||
exact hnot ((hcomp (toggle db₀ k)).mp hc₁).1
|
||||
|
||||
/-- **Factor-toggle necessity.** Under the factorization of Lemma 7.1, a single
|
||||
/-- **Factor-toggle necessity.** Under the factorization above, a single
|
||||
database whose flip breaks one factor makes `k` necessary. -/
|
||||
theorem necessary_of_factor_toggle {r : RangeRead} {res : Result} {k : Key}
|
||||
{φ₁ φ₂ : DB → Prop} {db₀ : DB}
|
||||
@@ -810,7 +808,7 @@ theorem necessary_of_factor_toggle {r : RangeRead} {res : Result} {k : Key}
|
||||
/-!
|
||||
## The joint base construction
|
||||
|
||||
Section 7's necessity argument does not transfer soundness from the end pair; it
|
||||
The necessity argument does not transfer soundness from the end pair; it
|
||||
exhibits a single database `db₀` that is compatible with the *full* result and whose
|
||||
flip at `k` breaks the targeted end factor. That object is named here: a *lower joint
|
||||
base* for `k` is a full-compatible database whose flip makes the lower boundary read
|
||||
@@ -821,7 +819,7 @@ factorization.
|
||||
|
||||
/-- A **lower joint base** for `k`: a database compatible with the full complete
|
||||
result `⟨L, false⟩` (via the bridge) whose flip at `k` makes the lower boundary read
|
||||
nonempty, i.e. breaks the lower factor. This is the section-7 witness that turns the
|
||||
nonempty, i.e. breaks the lower factor. This is the joint-base witness that turns the
|
||||
empty lower certificate into `Necessary r res k`. -/
|
||||
def LowerJointBase (r : RangeRead) (L : List Key) (k : Key) : Prop :=
|
||||
∃ db₀ : DB, bridge r L db₀ ∧
|
||||
@@ -1132,7 +1130,7 @@ theorem NecessaryRange.mono {r : RangeRead} {res : Result} {a b a' b' : WithTop
|
||||
middle interval `[cr₁.end_, cr₂.begin)` is necessary, the upper end range `cr₂` is
|
||||
necessary, and the intervals are ordered (`cr₁.begin ≤ cr₂.begin` and
|
||||
`cr₁.end_ ≤ cr₂.end_`), then the hull `[cr₁.begin, cr₂.end_)` is necessary. This is
|
||||
exactly section 7's assembly: everything below the reported keys is the lower end,
|
||||
exactly the hull assembly: everything below the reported keys is the lower end,
|
||||
the interior is convexity, and everything above is the upper end. -/
|
||||
theorem necessaryRange_hull_of_chain {r : RangeRead} {res : Result}
|
||||
{cr₁ cr₂ : ConflictRange}
|
||||
@@ -1185,7 +1183,7 @@ theorem minimal_hull_of_minimal_ends {r : RangeRead} {res : Result}
|
||||
|
||||
/-- **Necessity of the hull from the joint bases.** With the lower joint bases below
|
||||
the reported keys, the interior necessary by convexity, and the upper joint bases
|
||||
above, the hull's necessary range is assembled. This is section 7's necessity
|
||||
above, the hull's necessary range is assembled. This is the hull necessity
|
||||
assembly: the ends via factor-toggle joint witnesses, the middle free. -/
|
||||
theorem necessaryRange_hull_of_jointBases {r : RangeRead} {res : Result} {L : List Key}
|
||||
{cr₁ cr₂ : ConflictRange} (hrev : r.reverse = false) (hL : L ≠ [])
|
||||
@@ -1201,7 +1199,7 @@ theorem necessaryRange_hull_of_jointBases {r : RangeRead} {res : Result} {L : Li
|
||||
hmid (necessaryRange_of_upperJointBaseRange hrev hL hpair hlim hres hhi) hmin hmax
|
||||
|
||||
/-- **Minimality of the hull from the joint bases and hull soundness.** This is the
|
||||
end-to-end shape of the section-7 necessity argument: the end ranges are established
|
||||
end-to-end shape of the necessity argument: the end ranges are established
|
||||
by the joint bases (factor-toggle), the interior by convexity over the reported keys
|
||||
(`necessaryRange_between_reported`), and the only remaining hypothesis is soundness
|
||||
of the hull — the soundness half of the reduction. -/
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
|
||||
import FormalConflictRange.Empty
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
import FormalConflictRange.Basic
|
||||
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
import FormalConflictRange.ClosedFormAll
|
||||
import FormalConflictRange.NegCrossTruncClampAny
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
import FormalConflictRange.ConflictRange
|
||||
|
||||
@@ -2132,7 +2130,7 @@ theorem minimal_iff_sound_and_necessaryRange {r : RangeRead} {res : Result}
|
||||
/-- A minimal sound range is contained in every sound range: intersecting it with
|
||||
another sound range stays sound and is a subrange, so minimality forbids the
|
||||
intersection from being proper. This is the least-element half of the
|
||||
least-sound-range conjecture (issue #14), and it is unconditional — no
|
||||
least-sound-range statement, and it is unconditional — no
|
||||
nonemptiness or realizability hypothesis is needed. -/
|
||||
theorem Minimal.subrange_of_sound {r : RangeRead} {res : Result} {cr : ConflictRange}
|
||||
(h : Minimal r res cr) {cr' : ConflictRange} (hs : Sound r res cr') :
|
||||
@@ -2233,7 +2231,7 @@ theorem exists_unique_minimal_of_minimal {r : RangeRead} {res : Result}
|
||||
/-!
|
||||
## The canonical range at offset one
|
||||
|
||||
The first concrete instance of the minimal-range conjecture: when both selectors
|
||||
The first concrete instance of the minimal-range theorem: when both selectors
|
||||
have `offset = 1` and the result is complete (`more = false`), the minimal sound
|
||||
conflict range is the canonical range `[begin.key, end.key)` named directly by
|
||||
the read. At offset one the resolved window `[resolve begin, resolve end)` cuts a
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
import FormalConflictRange.Sound
|
||||
import FormalConflictRange.Empty
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 FormalConflictRange contributors. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: FormalConflictRange contributors
|
||||
-/
|
||||
|
||||
import FormalConflictRange.ZeroCrossLong
|
||||
|
||||
+4
-1
@@ -1,7 +1,10 @@
|
||||
/-
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
-/
|
||||
import Bridge
|
||||
|
||||
/-!
|
||||
# Transporting the reference theorem to this repository's statement
|
||||
# Transporting the reference theorem to the public statement
|
||||
|
||||
`Bridge` proves, for each definition of `Statement`, that its reference
|
||||
counterpart agrees with it under the translation `toRef*`. This file uses those
|
||||
|
||||
@@ -1,12 +1,19 @@
|
||||
# conflict-range
|
||||
|
||||
A formalised **closed form** for the minimal conflict range of a read/result
|
||||
pair, together with a C++20 translation and its test harness.
|
||||
A closed-form algorithm that computes the minimal conflict range for an observed
|
||||
range read and result, based on the semantics used by FoundationDB.
|
||||
|
||||
Given a database, a range read (two selectors plus offsets, either traversal
|
||||
direction), and the keys a transaction observed, `Statement.closedForm` computes
|
||||
a minimal sound conflict range: a set of keys that is sufficient to detect every
|
||||
conflicting write, and none of whose proper subranges is sufficient.
|
||||
See `Statement.lean` for definitions and the headline claim that the algorithm's
|
||||
output is minimal. Intuitively, the minimal range is the smallest range that
|
||||
preserves serializability (i.e. pinning this range pins the validity of any
|
||||
inference you can make from the observed range read and result), and the key set
|
||||
is unique: any two minimal ranges denote the same set of keys, though their
|
||||
endpoints may differ.
|
||||
|
||||
See `cpp/src/fast.cpp` for the C++20 implementation.
|
||||
|
||||
The rest of this README documents the Lean proof and the C++/test support for
|
||||
these claims.
|
||||
|
||||
## Layout
|
||||
|
||||
@@ -24,14 +31,30 @@ conflicting write, and none of whose proper subranges is sufficient.
|
||||
## Build and check
|
||||
|
||||
```sh
|
||||
lake exe cache get # clean checkout: download Mathlib's prebuilt oleans
|
||||
lake build
|
||||
./scripts/check-axioms.sh
|
||||
lake comparator
|
||||
```
|
||||
|
||||
This repository pins Lean `v4.35.0` and Mathlib `v4.35.0`
|
||||
(`lean-toolchain`, `lakefile.toml`). `lake comparator` checks that the solution
|
||||
proves the challenge's statements and that the proofs use only `propext`,
|
||||
`Quot.sound`, and `Classical.choice`.
|
||||
(`lean-toolchain`, `lakefile.toml`). `lake exe cache get` downloads Mathlib's
|
||||
prebuilt `.olean` files; without it a fresh clone compiles Mathlib from source.
|
||||
`lake build` then compiles the default targets (`Statement` and `Proof`, which
|
||||
pull in the reference development); run it before the checks so that a manifest
|
||||
is present and dependencies are resolved.
|
||||
|
||||
`./scripts/check-axioms.sh` prints the axiom dependencies of
|
||||
`Statement.closedFormMinimal` and `Statement.minimalUnique` and fails unless
|
||||
both are exactly `[propext, Classical.choice, Quot.sound]`. This catches `sorry`
|
||||
(`sorryAx`) and newly introduced axioms without needing the comparator sandbox.
|
||||
|
||||
`lake comparator` then builds `Challenge` and `Solution` independently and
|
||||
checks that the solution proves the challenge's statements and uses only
|
||||
`propext`, `Quot.sound`, and `Classical.choice`. It requires `bubblewrap` and
|
||||
either unprivileged user namespaces or a setuid-root `bwrap`; on systems where
|
||||
user namespaces are restricted, `lake comparator` will fail before checking the
|
||||
proof.
|
||||
|
||||
## Status and scope
|
||||
|
||||
@@ -39,18 +62,27 @@ proves the challenge's statements and that the proofs use only `propext`,
|
||||
proof in `Proof.lean`, `Bridge.lean`, or `FormalConflictRange/` uses `sorry`.
|
||||
* `Challenge.lean` deliberately contains `sorry`: it is the statement side of
|
||||
the comparator check, not part of the proof.
|
||||
* Some docstrings in `FormalConflictRange/` are a research log and refer to
|
||||
files that are **not** part of this repository:
|
||||
`FormalConflictRange/Counterexample`, `FormalConflictRange/Open`, and
|
||||
`notes/main.tex`. Those references appear only in comments and docstrings, so
|
||||
they do not affect the build or the headline proofs, but their specific claims
|
||||
cannot be checked here. `FormalConflictRange/Prototype.lean` is likewise an
|
||||
experimental prototype, not part of the headline proof path.
|
||||
* The headline argument is the closed-form development under `FormalConflictRange/`.
|
||||
`FormalConflictRange/Algorithm.lean` and `FormalConflictRange/Prototype.lean`
|
||||
also supply shared ingredients used by that development: the empty-result
|
||||
theory (`emptyValue`, `EmptyInferenceWitness`,
|
||||
`minimal_emptyValue_of_emptyInference`) and the ascending-key list and
|
||||
constraint collectors (`ascendingKeys`, `emptyConstraints`), respectively.
|
||||
Their partial-algorithm and prototype sections are earlier case-analysis and
|
||||
search material; those sections are not the route taken by the final
|
||||
closed-form proof.
|
||||
* The C++ tree is validated against Lean-generated golden vectors, by
|
||||
differential fuzzing, and by a deterministic exhaustive sweep of small
|
||||
differential fuzzing, and by a deterministic finite sweep of small
|
||||
readable instances; `cpp/README.md` describes the model, the tests, the
|
||||
coverage check, and the benchmark. The summarised implementation has 100%
|
||||
line coverage via `cpp/run-coverage.sh`.
|
||||
line and branch coverage via `cpp/run-coverage.sh`.
|
||||
|
||||
## Continuous integration
|
||||
|
||||
`.gitea/workflows/ci.yml` runs the Lean build, the axiom audit, and the C++ test
|
||||
suite (including the coverage gate) on pushes to `main` and on pull requests. It
|
||||
is written for Gitea Actions; a GitHub mirror would need an equivalent workflow
|
||||
under `.github/workflows/`.
|
||||
|
||||
## License
|
||||
|
||||
|
||||
@@ -1 +1,4 @@
|
||||
/-
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
-/
|
||||
import Proof
|
||||
+2
-2
@@ -1,5 +1,5 @@
|
||||
/-
|
||||
Copyright (c) 2026 Andrew Noyes. All rights reserved.
|
||||
Copyright (c) 2026 Andrew Noyes.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
|
||||
# Statement of minimality for a conflict range inference algorithm
|
||||
@@ -85,7 +85,7 @@ structure Selector where
|
||||
below `selector.key` (none for the empty key, all of them for `⊤`), minus one,
|
||||
shifted by `selector.offset`. Offset `0` names the last stored key strictly
|
||||
below `selector.key`, which is index `-1` when there is none; offset `1` names
|
||||
the first stored key at or above it; offset `-1` names the key before that. -/
|
||||
the first stored key at or above it, or index `List.length db.keys` when there is none. -/
|
||||
def Selector.anchor (db : DB) (s : Selector) : Int :=
|
||||
(db.keys.countP (fun (k : Key) => decide ((↑k : WithTop Key) < s.key)) : Int)
|
||||
+ s.offset - 1
|
||||
|
||||
+16
-5
@@ -12,6 +12,8 @@ endif()
|
||||
option(CR_BUILD_FUZZERS "Build libFuzzer differential/safety harnesses" ON)
|
||||
option(CR_BUILD_BENCH "Build the nanobench comparison" ON)
|
||||
|
||||
enable_testing()
|
||||
|
||||
add_library(cr_literal STATIC src/literal.cpp)
|
||||
target_include_directories(cr_literal PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src)
|
||||
|
||||
@@ -34,12 +36,21 @@ target_include_directories(extreme_test PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/tests)
|
||||
target_link_libraries(extreme_test PRIVATE cr_literal cr_fast)
|
||||
|
||||
# Deterministic exhaustive sweep of small readable instances. This is the
|
||||
# driver for the line-coverage check in `run-coverage.sh`.
|
||||
add_executable(exhaustive_test tests/exhaustive.cpp)
|
||||
target_include_directories(exhaustive_test PRIVATE
|
||||
# Deterministic sweep over a finite class of small readable instances. This is
|
||||
# the driver for the line-coverage check in `run-coverage.sh`.
|
||||
add_executable(sweep_test tests/sweep.cpp)
|
||||
target_include_directories(sweep_test PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/tests)
|
||||
target_link_libraries(exhaustive_test PRIVATE cr_literal cr_fast)
|
||||
target_link_libraries(sweep_test PRIVATE cr_literal cr_fast)
|
||||
|
||||
set(CR_GOLDEN_VECTORS "${CMAKE_CURRENT_SOURCE_DIR}/oracle/golden.txt")
|
||||
add_test(NAME golden COMMAND golden_test "${CR_GOLDEN_VECTORS}")
|
||||
add_test(NAME diff_corpus COMMAND diff_corpus "${CR_GOLDEN_VECTORS}")
|
||||
# Curated readable cases found by SMT-assisted mutation analysis.
|
||||
add_test(NAME smt_cases COMMAND diff_corpus
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/tests/smt_cases.txt")
|
||||
add_test(NAME extreme COMMAND extreme_test)
|
||||
add_test(NAME sweep COMMAND sweep_test)
|
||||
|
||||
if(CR_BUILD_BENCH)
|
||||
add_executable(cr_bench tests/bench.cpp)
|
||||
|
||||
+55
-44
@@ -16,14 +16,16 @@ The Lean build is untouched.
|
||||
| `oracle/gen.lean` | Deterministic generator of golden vectors by evaluating the real Lean `closedForm`. |
|
||||
| `tests/golden.cpp` | Checks the literal translation against the Lean golden vectors. |
|
||||
| `tests/diff_corpus.cpp` | Compares literal and fast on the sorted subset of the golden corpus. |
|
||||
| `tests/smt_cases.txt` | Curated readable cases found by SMT-assisted mutation analysis; run by the `smt_cases` CTest. |
|
||||
| `tests/readable_gen.h` | Readable-by-construction instance generator (owns the selector keys and reported keys, with borrowed views into them). |
|
||||
| `tests/extreme.cpp` | Regression test for offsets at the ends of the 32-bit input range. |
|
||||
| `tests/exhaustive.cpp` | Deterministic exhaustive sweep of small readable instances (also the coverage driver). |
|
||||
| `tests/sweep.cpp` | Deterministic sweep over a finite class of small readable instances (also the coverage driver). |
|
||||
| `tests/fuzz_readable.cpp` | libFuzzer differential test: literal vs. fast on readable instances. |
|
||||
| `tests/corpus/readable/` | Committed read-only seeds for `fuzz_readable`. |
|
||||
| `tests/fuzz_raw.cpp` | libFuzzer safety test on arbitrary instances under ASan/UBSan. |
|
||||
| `tests/bench.cpp` | nanobench literal-vs-fast benchmark. |
|
||||
| `run-fuzz.sh` | Rebuilds and runs both fuzzers for a fixed time. |
|
||||
| `run-coverage.sh` | Builds with Clang source-based coverage and requires 100% line coverage of `src/fast.cpp`. |
|
||||
| `run-coverage.sh` | Builds with Clang source-based coverage and requires 100% line and branch coverage of `src/fast.cpp`. |
|
||||
| `third_party/nanobench.h` | Vendored nanobench (commit `7252da9d96bd0d71f508e9b2813ab1f57fdfaec1`). |
|
||||
|
||||
## Data model
|
||||
@@ -35,15 +37,14 @@ them in place. `ConflictRange` returns owned `std::string`s because the decision
|
||||
procedure can synthesise endpoints (`keyAfter`, clamp bounds) that are not
|
||||
present in the input.
|
||||
|
||||
Caller-supplied selector offsets must fit in `std::int32_t`. Internally they are
|
||||
stored as `Int = std::int64_t`, because the closed form also builds derived
|
||||
selectors whose offsets (for example `end.offset - residualT`) need not fit in
|
||||
32 bits. All intermediate `Int` arithmetic assumes 64-bit signed integers do not
|
||||
overflow; with 32-bit input offsets and in-memory key counts that is far from
|
||||
the limit. The corpus parsers reject offsets outside the 32-bit input range, and
|
||||
both public `closedForm` entry points assert the same contract in debug builds.
|
||||
The assertion lives at the API boundary rather than in `Selector`, because
|
||||
derived selectors legitimately exceed the input range.
|
||||
Selector offsets are `std::int32_t`, matching the caller contract. The closed
|
||||
form converts the public `RangeRead` to `detail::RangeRead`, whose offsets are
|
||||
`Int = std::int64_t`, before deriving selectors (for example
|
||||
`end.offset - residualT`). All intermediate `Int` arithmetic assumes 64-bit
|
||||
signed integers do not overflow; with 32-bit input offsets and in-memory key
|
||||
counts that is far from the limit. The corpus parsers read offsets into a wider
|
||||
integer and reject values outside the 32-bit range before narrowing to
|
||||
`Selector`.
|
||||
|
||||
## Scope of the two implementations
|
||||
|
||||
@@ -51,7 +52,7 @@ derived selectors legitimately exceed the input range.
|
||||
and `golden_test` compares it with Lean over the whole generated corpus, most of
|
||||
which is not readable. `fast.cpp` is the readable-only implementation:
|
||||
`diff_corpus` filters the corpus to readable cases, and the fuzzers and the
|
||||
exhaustive sweep generate readable instances. The Lean anchor therefore covers
|
||||
finite sweep generate readable instances. The Lean anchor therefore covers
|
||||
`literal` on all inputs, while `fast` is anchored to `literal` on the readable
|
||||
subset.
|
||||
|
||||
@@ -63,6 +64,16 @@ cmake -S . -B build -G Ninja -DCMAKE_CXX_COMPILER=clang++-21
|
||||
cmake --build build
|
||||
```
|
||||
|
||||
Run the registered tests with CTest:
|
||||
|
||||
```sh
|
||||
ctest --test-dir build --output-on-failure
|
||||
```
|
||||
|
||||
`golden` and `diff_corpus` read `oracle/golden.txt`; regenerate it first if it
|
||||
is missing (see below). `smt_cases` reads the committed
|
||||
`tests/smt_cases.txt` and needs no generated input.
|
||||
|
||||
## Golden vectors from Lean
|
||||
|
||||
```sh
|
||||
@@ -90,18 +101,24 @@ cases that satisfy the readable sorting invariant:
|
||||
# extreme: 7574 cases, 0 mismatches
|
||||
```
|
||||
|
||||
`exhaustive_test` enumerates small readable instances instead of sampling them:
|
||||
every sorted database of up to four keys over a fixed universe of boundary
|
||||
keys, every selector key (including `⊥` and `⊤`) with offsets in `[-3, 4]`, and
|
||||
every readable result (the complete traversal, each nonempty truncated prefix,
|
||||
or the empty result when the traversal is empty) in both directions:
|
||||
`sweep_test` is exhaustive over a specific bounded class rather than sampling
|
||||
it: every sorted database of at most four stored keys drawn from a fixed universe of
|
||||
boundary keys, every selector key (including `⊥` and `⊤`) with offsets in
|
||||
`[-3, 5]`, both traversal directions, and every readable result shape (the
|
||||
complete traversal, each nonempty truncated prefix, or the empty result when the
|
||||
traversal is empty):
|
||||
|
||||
```sh
|
||||
./cpp/build/exhaustive_test
|
||||
# exhaustive: 10562068 cases, 0 mismatches
|
||||
./cpp/build/sweep_test
|
||||
# sweep: 13565772 cases, 0 mismatches
|
||||
```
|
||||
|
||||
It runs in about a second and is also the coverage driver.
|
||||
It runs in about a second and is also the coverage driver. It is exhaustive
|
||||
only within those bounds; the class is not all readable inputs, and the
|
||||
guarantee is that every instance in this family agrees. In
|
||||
particular, branch coverage cannot exercise a predicate operand that the
|
||||
implementation no longer contains: the coverage result below describes the code
|
||||
as written, it is not a proof of equivalence with `literal.cpp`.
|
||||
|
||||
## Coverage
|
||||
|
||||
@@ -110,45 +127,39 @@ It runs in about a second and is also the coverage driver.
|
||||
```
|
||||
|
||||
The script configures a Clang source-based-coverage build, runs
|
||||
`exhaustive_test`, and fails unless `src/fast.cpp` has 100% line coverage.
|
||||
`exhaustive_test` alone covers every line of the summarised implementation:
|
||||
`sweep_test`, and fails unless `src/fast.cpp` has 100% line and branch
|
||||
coverage. `sweep_test` alone covers every region, function, line, and
|
||||
branch side of the summarised implementation:
|
||||
|
||||
```text
|
||||
src/fast.cpp 836 regions (12 missed) 131 functions (0 missed) 773 lines (0 missed) 670 branches (125 missed)
|
||||
src/fast.cpp 560 regions (0 missed) 121 functions (0 missed) 675 lines (0 missed) 374 branches (0 missed)
|
||||
```
|
||||
|
||||
That is 100.00% line and function coverage, 98.56% region coverage, and 81.34%
|
||||
branch coverage. The missed branches are short-circuit operands: line coverage
|
||||
counts a condition as covered once any operand decides it, but branch coverage
|
||||
requires every operand to be the deciding one. Reaching 100% branch coverage
|
||||
would need inputs that force each redundant operand to disagree with the rest of
|
||||
its condition, which is not practical here.
|
||||
That is 100.00% line, function, region, and branch coverage. Offset 5 is part of
|
||||
the sweep specifically to exercise the last three short-circuit branch sides,
|
||||
which the previous `[-3, 4]` window missed.
|
||||
|
||||
Because equivalence is only promised on readable inputs, `src/fast.cpp` deletes
|
||||
*control-flow branches* that no readable input can take: the complete-closed
|
||||
family, the offset-one total/any wrappers and their `end.offset <= 1` shortcut,
|
||||
the `end.offset <= 1` shortcut in `completeForwardCrossedGeneral`, the
|
||||
`crossedCompleteEndLeOne` guard, and one `lastK < end.key` test that readability
|
||||
always satisfies.
|
||||
|
||||
The deletion criterion is deliberately limited to unreachable arms. An
|
||||
individual predicate operand that a dispatch invariant makes always true at one
|
||||
call site (for example `!c.r->reverse` inside a condition only reached after the
|
||||
forward branch was selected) is kept, because it is part of the Lean condition
|
||||
that the translation mirrors; re-proving the invariant at every site would make
|
||||
the tree harder to audit. Those operands are why every line is covered while branch coverage stays
|
||||
around 81%.
|
||||
both unreachable control-flow branches and predicate operands that an enclosing
|
||||
dispatch invariant fixes. Examples include the complete-closed family, the
|
||||
offset-one total/any wrappers, the `end.offset <= 1` shortcuts, the
|
||||
`crossedCompleteEndLeOne` guard, and the repeated
|
||||
`!c.r->reverse`/`!o.more`/offset tests inside subtrees that are only entered
|
||||
after those tests succeeded. `literal.cpp` keeps the full Lean conditions so the
|
||||
translation can still be audited line by line.
|
||||
|
||||
## Fuzzing
|
||||
|
||||
```sh
|
||||
cd cpp
|
||||
mkdir -p /tmp/cr-corpus/readable /tmp/cr-corpus/raw
|
||||
./build/fuzz_readable -max_total_time=300 /tmp/cr-corpus/readable
|
||||
./build/fuzz_readable -max_total_time=300 /tmp/cr-corpus/readable tests/corpus/readable
|
||||
./build/fuzz_raw -max_total_time=300 /tmp/cr-corpus/raw
|
||||
```
|
||||
|
||||
or simply `./cpp/run-fuzz.sh` (optionally `./cpp/run-fuzz.sh build 600`).
|
||||
`tests/corpus/readable` holds committed seeds; the first directory above is the
|
||||
writable corpus where libFuzzer stores newly discovered units.
|
||||
The CMake fuzz targets compile the implementation sources directly so that the
|
||||
decoder code, not just the harness, is covered by ASan/UBSan.
|
||||
|
||||
@@ -168,7 +179,7 @@ One representative validation run on this machine:
|
||||
| literal vs. Lean golden vectors | 40,000 | 0 mismatches (exact endpoints) |
|
||||
| fast vs. literal on sorted corpus | 18,971 | 0 mismatches |
|
||||
| extreme-offset regression (`extreme_test`) | 7,574 | 0 mismatches |
|
||||
| exhaustive readable sweep (`exhaustive_test`) | 10,562,068 | 0 mismatches |
|
||||
| finite readable sweep (`sweep_test`) | 13,565,772 | 0 mismatches |
|
||||
| readable differential fuzzing (60 s) | 1,182,424 | 0 mismatches |
|
||||
| raw safety fuzzing (60 s) | 1,905,270 | no ASan/UBSan findings |
|
||||
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
/-
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
-/
|
||||
|
||||
import Statement
|
||||
|
||||
/-!
|
||||
|
||||
+66
-9
@@ -1,20 +1,72 @@
|
||||
#!/usr/bin/env bash
|
||||
# Build with Clang source-based coverage and require 100% line coverage of the
|
||||
# summarised implementation (`src/fast.cpp`). `exhaustive_test` enumerates
|
||||
# small readable instances deterministically and is sufficient on its own.
|
||||
# Build with Clang source-based coverage and require 100% line and branch
|
||||
# coverage of the summarised implementation (`src/fast.cpp`). `sweep_test`
|
||||
# deterministically enumerates a finite class of small readable instances and is
|
||||
# sufficient on its own.
|
||||
set -euo pipefail
|
||||
|
||||
here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
build="${1:-$here/build-coverage}"
|
||||
cxx="${CXX:-clang++-21}"
|
||||
profdata="${LLVM_PROFDATA:-$(command -v llvm-profdata-21 || command -v llvm-profdata || true)}"
|
||||
cov="${LLVM_COV:-$(command -v llvm-cov-21 || command -v llvm-cov || true)}"
|
||||
# Prefer LLVM tools whose version matches the compiler (for example
|
||||
# `clang++-21` with `llvm-profdata-21`/`llvm-cov-21`). Do not guess among
|
||||
# versioned tools: mixing profile formats fails in confusing ways.
|
||||
if [[ "$(basename "$cxx")" =~ -([0-9]+)$ ]]; then
|
||||
cxx_version="${BASH_REMATCH[1]}"
|
||||
else
|
||||
cxx_version=""
|
||||
fi
|
||||
cxx_major=""
|
||||
if [[ -z "$cxx_version" ]]; then
|
||||
cxx_major="$("$cxx" -dumpversion 2>/dev/null | cut -d. -f1 || true)"
|
||||
fi
|
||||
|
||||
pick_tool() {
|
||||
local tool="$1" override="$2"
|
||||
if [[ -n "$override" ]]; then
|
||||
printf '%s\n' "$override"
|
||||
return 0
|
||||
fi
|
||||
if [[ -n "$cxx_version" ]]; then
|
||||
if command -v "${tool}-${cxx_version}" >/dev/null 2>&1; then
|
||||
command -v "${tool}-${cxx_version}"
|
||||
return 0
|
||||
fi
|
||||
return 1
|
||||
fi
|
||||
if command -v "$tool" >/dev/null 2>&1; then
|
||||
command -v "$tool"
|
||||
return 0
|
||||
fi
|
||||
if [[ -n "$cxx_major" ]] && command -v "${tool}-${cxx_major}" >/dev/null 2>&1; then
|
||||
command -v "${tool}-${cxx_major}"
|
||||
return 0
|
||||
fi
|
||||
return 1
|
||||
}
|
||||
|
||||
profdata="$(pick_tool llvm-profdata "${LLVM_PROFDATA:-}")" || true
|
||||
cov="$(pick_tool llvm-cov "${LLVM_COV:-}")" || true
|
||||
|
||||
if [[ -z "$profdata" || -z "$cov" ]]; then
|
||||
echo "llvm-profdata and llvm-cov are required" >&2
|
||||
echo "install the LLVM tools matching $cxx, or set LLVM_PROFDATA and LLVM_COV" >&2
|
||||
exit 2
|
||||
fi
|
||||
|
||||
# Reusing a coverage build directory after changing the compiler makes CMake
|
||||
# discard its cache, which also drops the instrumentation flags. Recreate the
|
||||
# directory instead of silently producing an uninstrumented binary.
|
||||
cxx_path="$(command -v "$cxx" 2>/dev/null || printf '%s' "$cxx")"
|
||||
cache="$build/CMakeCache.txt"
|
||||
if [[ -f "$cache" ]]; then
|
||||
cached_cxx="$(sed -n 's/^CMAKE_CXX_COMPILER:.*=//p' "$cache" | head -n 1)"
|
||||
if [[ -n "$cached_cxx" && "$cached_cxx" != "$cxx" && "$cached_cxx" != "$cxx_path" ]]; then
|
||||
echo "coverage build used $cached_cxx; recreating it for $cxx" >&2
|
||||
rm -rf "$build"
|
||||
fi
|
||||
fi
|
||||
|
||||
cmake -S "$here" -B "$build" -G Ninja \
|
||||
-DCMAKE_CXX_COMPILER="$cxx" \
|
||||
-DCMAKE_BUILD_TYPE=Debug \
|
||||
@@ -22,25 +74,30 @@ cmake -S "$here" -B "$build" -G Ninja \
|
||||
-DCR_BUILD_BENCH=OFF \
|
||||
-DCMAKE_CXX_FLAGS="-fprofile-instr-generate -fcoverage-mapping" \
|
||||
-DCMAKE_EXE_LINKER_FLAGS="-fprofile-instr-generate"
|
||||
cmake --build "$build" --target exhaustive_test
|
||||
cmake --build "$build" --target sweep_test
|
||||
|
||||
profdir="$build/prof"
|
||||
rm -rf "$profdir"
|
||||
mkdir -p "$profdir"
|
||||
|
||||
cd "$here"
|
||||
LLVM_PROFILE_FILE="$profdir/exhaustive-%p.profraw" "$build/exhaustive_test"
|
||||
LLVM_PROFILE_FILE="$profdir/sweep-%p.profraw" "$build/sweep_test"
|
||||
"$profdata" merge -sparse "$profdir"/*.profraw -o "$profdir/cov.profdata"
|
||||
|
||||
report() {
|
||||
"$cov" report "$build/exhaustive_test" \
|
||||
"$cov" report "$build/sweep_test" \
|
||||
-instr-profile="$profdir/cov.profdata" src/fast.cpp
|
||||
}
|
||||
output="$(report)"
|
||||
printf '%s\n' "$output"
|
||||
line_cover="$(printf '%s\n' "$output" | awk '/fast\.cpp/ { print $10 }')"
|
||||
branch_cover="$(printf '%s\n' "$output" | awk '/fast\.cpp/ { print $13 }')"
|
||||
if [[ "$line_cover" != "100.00%" ]]; then
|
||||
echo "fast.cpp line coverage is $line_cover, expected 100.00%" >&2
|
||||
exit 1
|
||||
fi
|
||||
echo "fast.cpp: 100.00% line coverage"
|
||||
if [[ "$branch_cover" != "100.00%" ]]; then
|
||||
echo "fast.cpp branch coverage is $branch_cover, expected 100.00%" >&2
|
||||
exit 1
|
||||
fi
|
||||
echo "fast.cpp: 100.00% line and branch coverage"
|
||||
+5
-1
@@ -15,9 +15,13 @@ cmake --build "$build" --target fuzz_readable fuzz_raw
|
||||
|
||||
corpus="${TMPDIR:-/tmp}/cr-fuzz-corpus"
|
||||
mkdir -p "$corpus/readable" "$corpus/raw"
|
||||
# Committed read-only seeds (cases found by SMT/mutation analysis) are
|
||||
# passed after the writable corpus, which is where libFuzzer stores new units.
|
||||
seeds="$here/tests/corpus/readable"
|
||||
|
||||
echo "== differential fuzzing (readable instances) =="
|
||||
"$build/fuzz_readable" -max_total_time="$seconds" -max_len=1024 "$corpus/readable"
|
||||
"$build/fuzz_readable" -max_total_time="$seconds" -max_len=1024 \
|
||||
"$corpus/readable" "$seeds"
|
||||
|
||||
echo "== safety fuzzing (arbitrary instances) =="
|
||||
"$build/fuzz_raw" -max_total_time="$seconds" -max_len=1024 "$corpus/raw"
|
||||
+99
-169
@@ -1,22 +1,27 @@
|
||||
// Copyright (c) 2026 Andrew Noyes.
|
||||
// Released under Apache 2.0 license as described in the file LICENSE.
|
||||
|
||||
// A summarised implementation of the same decision procedure as
|
||||
// `Statement.closedForm`.
|
||||
//
|
||||
// `literal.cpp` mirrors the Lean definition line by line and therefore rescans
|
||||
// `res.keys` (and re-derives small arithmetic facts) on almost every branch.
|
||||
// This version summarises the reported keys once (two binary searches on the
|
||||
// sorted traversal order) and caches the handful of interval cardinalities the
|
||||
// decision tree needs, then replays the same tree for readable inputs. Branches
|
||||
// that the `Readable` invariant makes unreachable are omitted. Reversed results
|
||||
// are handled through a `reversed` view instead of copying the key list.
|
||||
// sorted traversal order), then replays the same tree for readable inputs.
|
||||
// Branches and predicate operands that the `Readable` invariant or a dispatch
|
||||
// invariant makes unreachable/constant are omitted; `literal.cpp` keeps the
|
||||
// full Lean conditions for audit. Reversed results are handled through a
|
||||
// `reversed` view instead of copying the key list.
|
||||
#include "fast.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <utility>
|
||||
|
||||
namespace cr::fast {
|
||||
namespace {
|
||||
|
||||
using WideRead = detail::RangeRead;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Observational summary
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -34,44 +39,28 @@ struct Obs {
|
||||
bool ascending = true;
|
||||
std::size_t n = 0;
|
||||
|
||||
const KeyView& firstK() const {
|
||||
assert(!keys.empty());
|
||||
return reversed ? keys.back() : keys.front();
|
||||
}
|
||||
const KeyView& lastK() const {
|
||||
assert(!keys.empty());
|
||||
return reversed ? keys.front() : keys.back();
|
||||
}
|
||||
// `closedForm` handles an empty result before building the tree, so `keys` is
|
||||
// non-empty everywhere these are called.
|
||||
const KeyView& firstK() const { return reversed ? keys.back() : keys.front(); }
|
||||
const KeyView& lastK() const { return reversed ? keys.front() : keys.back(); }
|
||||
};
|
||||
|
||||
// Facts that only depend on the read and on the *multiset* of reported keys.
|
||||
// They are therefore invariant under reversing the view.
|
||||
struct Ctx {
|
||||
const RangeRead* r = nullptr;
|
||||
const WideRead* r = nullptr;
|
||||
std::size_t n = 0;
|
||||
std::size_t below = 0; // number of reported keys < begin.key
|
||||
std::size_t t = 0; // number of reported keys >= end.key
|
||||
// Lazily computed interval cardinalities.
|
||||
mutable bool haveBE = false;
|
||||
mutable bool haveEB = false;
|
||||
mutable NatInf be;
|
||||
mutable NatInf eb;
|
||||
};
|
||||
|
||||
NatInf cardBE(const Ctx& c) {
|
||||
if (!c.haveBE) {
|
||||
c.be = intervalCard(c.r->begin.key, c.r->end_.key);
|
||||
c.haveBE = true;
|
||||
}
|
||||
return c.be;
|
||||
return intervalCard(c.r->begin.key, c.r->end_.key);
|
||||
}
|
||||
|
||||
// Each path consults this at most once, so it needs no cache.
|
||||
NatInf cardEB(const Ctx& c) {
|
||||
if (!c.haveEB) {
|
||||
c.eb = intervalCard(c.r->end_.key, c.r->begin.key);
|
||||
c.haveEB = true;
|
||||
}
|
||||
return c.eb;
|
||||
return intervalCard(c.r->end_.key, c.r->begin.key);
|
||||
}
|
||||
|
||||
NatInf cardKO(KeyView b, OptKeyView e) {
|
||||
@@ -118,7 +107,7 @@ std::size_t countLess(std::span<const KeyView> ks, OptKeyView x,
|
||||
return n - lo;
|
||||
}
|
||||
|
||||
Ctx makeCtx(const RangeRead& r, const Obs& o) {
|
||||
Ctx makeCtx(const WideRead& r, const Obs& o) {
|
||||
Ctx c;
|
||||
c.r = &r;
|
||||
c.n = o.n;
|
||||
@@ -132,10 +121,6 @@ Ctx makeCtx(const RangeRead& r, const Obs& o) {
|
||||
// Comparisons that avoid allocating a one-element `OptKey`
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
// ↑k < b
|
||||
bool kLtO(KeyView k, OptKeyView b) { return !b || keyLt(k, *b); }
|
||||
// ↑k <= b
|
||||
bool kLeO(KeyView k, OptKeyView b) { return !b || !keyLt(*b, k); }
|
||||
// a < ↑k
|
||||
bool oLtK(OptKeyView a, KeyView k) { return a.has_value() && keyLt(*a, k); }
|
||||
// a <= ↑k
|
||||
@@ -149,7 +134,6 @@ std::uint64_t rB(const Ctx& c) { return residualB(*c.r); }
|
||||
std::uint64_t npd(const Ctx& c) { return clampNonposD(*c.r); }
|
||||
|
||||
bool allBelowEnd(const Ctx& c) { return c.t == 0; }
|
||||
bool anyAtOrAboveEnd(const Ctx& c) { return c.t != 0; }
|
||||
|
||||
const KeyView& leastReportedK(const Ctx& c, const Obs& o) {
|
||||
return c.r->reverse ? o.lastK() : o.firstK();
|
||||
@@ -205,13 +189,16 @@ bool clampNonposBottom(const Ctx& c) {
|
||||
return niLe(lhs, NatInf::of(bSlack(c)));
|
||||
}
|
||||
|
||||
// The caller reaches this only after `begin.offset >= 1` failed, so the offset
|
||||
// half of the condition is already known.
|
||||
bool clampedNonpos(const Ctx& c, const Obs& o) {
|
||||
return c.r->begin.offset <= 0 && oLeK(c.r->begin.key, leastReportedK(c, o));
|
||||
return oLeK(c.r->begin.key, leastReportedK(c, o));
|
||||
}
|
||||
|
||||
bool clampsToBottom(const Ctx& c, const Obs& o) {
|
||||
return c.r->begin.offset <= 0 && oLeK(c.r->begin.key, o.firstK());
|
||||
}
|
||||
// Called from the positive branch: if `begin.offset <= 0` then the entry
|
||||
// dispatch already established `clampedNonpos`, i.e. the selector key is at or
|
||||
// before the first reported key, so only the offset decides here.
|
||||
bool clampsToBottom(const Ctx& c) { return c.r->begin.offset <= 0; }
|
||||
|
||||
std::uint64_t cELo(Int oB, Int oE) {
|
||||
return std::max(toNat(2 - oE), toNat(oB - oE + 1));
|
||||
@@ -253,43 +240,34 @@ OptKeyView crossedBegin(const Ctx& c) {
|
||||
struct Impl {
|
||||
// ----- crossed complete --------------------------------------------------
|
||||
|
||||
// The complete-crossed subtree is entered only with `!reverse` and `!more`
|
||||
// (`closedFormCrossed` normalises a reversed complete read first). Within it,
|
||||
// `crossedCompleteBeginZero` has already claimed every `begin.offset == 0`
|
||||
// case before these clamp nodes are tried, so `begin.offset < 0` holds here.
|
||||
bool negCrossClampEndOne(const Ctx& c, const Obs& o) const {
|
||||
return !c.r->reverse && c.r->begin.offset < 0 && !o.more &&
|
||||
c.r->end_.offset == 1 && kLtO(o.firstK(), c.r->begin.key) &&
|
||||
c.below <= bSlack(c);
|
||||
// `crossedCompleteEndLeOne` already failed, and for `end.offset == 1` it
|
||||
// fails exactly when `bSlack >= below`.
|
||||
return c.r->end_.offset == 1;
|
||||
}
|
||||
|
||||
bool negCrossHeadBottom(const Ctx& c, const Obs& o) const {
|
||||
return !c.r->reverse && c.r->begin.offset < 0 && !o.more &&
|
||||
c.r->end_.offset <= 0 && optLe(c.r->begin.key, c.r->end_.key) &&
|
||||
return c.r->end_.offset <= 0 && optLe(c.r->begin.key, c.r->end_.key) &&
|
||||
o.firstK().empty();
|
||||
}
|
||||
|
||||
bool negCrossClampNonposLastGe(const Ctx& c, const Obs& o) const {
|
||||
return !c.r->reverse && c.r->begin.offset < 0 && !o.more &&
|
||||
c.r->end_.offset <= 0 && kLtO(o.firstK(), c.r->begin.key) &&
|
||||
oLeK(c.r->begin.key, o.lastK()) && c.below <= bSlack(c);
|
||||
}
|
||||
|
||||
bool negCrossClampNonposBottom(const Ctx& c, const Obs& o) const {
|
||||
return !c.r->reverse && c.r->begin.offset < 0 && !o.more &&
|
||||
c.r->end_.offset <= 0 && optLe(c.r->begin.key, c.r->end_.key) &&
|
||||
kLtO(o.firstK(), c.r->begin.key) && clampNonposBottom(c);
|
||||
return c.r->end_.offset <= 0 && optLe(c.r->begin.key, c.r->end_.key);
|
||||
}
|
||||
|
||||
bool negCrossClampLongNonpos(const Ctx& c, const Obs& o) const {
|
||||
return !c.r->reverse && c.r->begin.offset < 0 && !o.more &&
|
||||
c.r->end_.offset <= 0 && kLtO(o.firstK(), c.r->begin.key) &&
|
||||
c.below <= bSlack(c) && clampNonposBottom(c) &&
|
||||
optLt(c.r->end_.key, c.r->begin.key);
|
||||
return c.r->end_.offset <= 0 && optLt(c.r->end_.key, c.r->begin.key);
|
||||
}
|
||||
|
||||
ConflictRange closedFormNegCrossClampUnified(const Ctx& c, const Obs& o) const {
|
||||
OptKey end;
|
||||
if (negCrossClampEndOne(c, o))
|
||||
end = clampEndOneUpper(c, o);
|
||||
else if (negCrossClampLongNonpos(c, o) &&
|
||||
!negCrossClampNonposLastGe(c, o))
|
||||
else if (negCrossClampLongNonpos(c, o))
|
||||
end = clampLongUpper(c, o);
|
||||
else
|
||||
end = c.r->end_.key;
|
||||
@@ -316,9 +294,7 @@ struct Impl {
|
||||
}
|
||||
|
||||
bool negCrossCompleteHigh(const Ctx& c, const Obs& o) const {
|
||||
return !c.r->reverse && c.r->begin.offset < 0 && !o.more &&
|
||||
c.r->end_.offset >= 2 && kLtO(o.firstK(), c.r->begin.key) &&
|
||||
oLtK(c.r->end_.key, o.firstK());
|
||||
return oLtK(c.r->end_.key, o.firstK());
|
||||
}
|
||||
|
||||
ConflictRange closedFormNegCrossCompleteHigh(const Ctx& c, const Obs& o) const {
|
||||
@@ -348,7 +324,7 @@ struct Impl {
|
||||
ConflictRange closedFormNegCrossCompleteResidual(const Ctx& c, const Obs& o) const {
|
||||
// Keep the synthesised end key alive while `rp` borrows it.
|
||||
OptKey reducedEnd = after(o.lastK());
|
||||
RangeRead rp = *c.r;
|
||||
WideRead rp = *c.r;
|
||||
rp.end_.key = optView(reducedEnd);
|
||||
rp.end_.offset = rp.end_.offset - static_cast<Int>(c.t);
|
||||
Ctx cp = makeCtx(rp, o);
|
||||
@@ -379,28 +355,17 @@ struct Impl {
|
||||
|
||||
// ----- crossed truncated -------------------------------------------------
|
||||
|
||||
bool negCrossTruncNonposGap(const Ctx& c, const Obs& o) const {
|
||||
return o.more && !c.r->reverse && c.r->begin.offset < 0 &&
|
||||
kLtO(leastReportedK(c, o), c.r->begin.key) &&
|
||||
c.r->end_.offset <= 0 && c.below <= bSlack(c) &&
|
||||
kLtO(o.lastK(), c.r->begin.key) &&
|
||||
kLtO(o.lastK(), c.r->end_.key) &&
|
||||
!isTop(c.r->end_.key) &&
|
||||
(optLt(c.r->end_.key, c.r->begin.key) ||
|
||||
niLt(cardBE(c), NatInf::of(npd(c) + 1)));
|
||||
}
|
||||
|
||||
bool negCrossTruncNonposGapA2aBottom(const Ctx& c, const Obs& o) const {
|
||||
return negCrossTruncNonposGap(c, o) &&
|
||||
optLt(c.r->end_.key, c.r->begin.key) && clampNonposBottom(c);
|
||||
}
|
||||
|
||||
// `closedFormForwardTruncatedNonpos` calls this only inside
|
||||
// `below <= bSlack`, `end.offset <= 0`, `end < begin` and
|
||||
// `clampNonposBottom`; the remaining Lean conditions of
|
||||
// `negCrossTruncNonposGap`/`...A2aBottom` are invariants of the readable
|
||||
// forward-crossed regime.
|
||||
bool negCrossTruncNonposGapA2aBottomHigh(const Ctx& c, const Obs& o) const {
|
||||
std::uint64_t extra = o.firstK().empty() ? 1 : 0;
|
||||
NatInf lhs = NatInf::of(bSlack(c) + 1 + extra);
|
||||
NatInf rhs = niAdd(NatInf::of(static_cast<std::uint64_t>(c.n)),
|
||||
cardKO(keyAfter(o.lastK()), c.r->begin.key));
|
||||
return negCrossTruncNonposGapA2aBottom(c, o) && niLe(lhs, rhs);
|
||||
return niLe(lhs, rhs);
|
||||
}
|
||||
|
||||
ConflictRange closedFormNegCrossTruncNonposGapA2aBottom(const Ctx& c,
|
||||
@@ -416,10 +381,10 @@ struct Impl {
|
||||
return niLe(lhs, NatInf::of(bSlack(c)));
|
||||
}
|
||||
|
||||
// Reached only in the reversed truncation branch (`more` set, `reverse` set,
|
||||
// `begin.offset < 0`, `end.offset >= 2`).
|
||||
bool negCrossTruncRevResidualFiniteOver(const Ctx& c, const Obs& o) const {
|
||||
return o.more && c.r->reverse && c.r->begin.offset < 0 &&
|
||||
c.r->end_.offset >= 2 && oLtK(c.r->end_.key, o.lastK()) &&
|
||||
kLtO(o.lastK(), c.r->begin.key) && c.t == rE(c);
|
||||
return oLtK(c.r->end_.key, o.lastK());
|
||||
}
|
||||
|
||||
ConflictRange closedFormNegCrossTruncRevResidualFiniteUnified(const Ctx& c,
|
||||
@@ -440,10 +405,7 @@ struct Impl {
|
||||
}
|
||||
|
||||
bool negCrossTruncRevResidualTopLarge(const Ctx& c, const Obs& o) const {
|
||||
if (!(o.more && c.r->reverse && c.r->begin.offset < 0 &&
|
||||
c.r->end_.offset >= 2 && oLtK(c.r->end_.key, o.lastK()) &&
|
||||
c.t < rE(c)))
|
||||
return false;
|
||||
if (!(oLtK(c.r->end_.key, o.lastK()) && c.t < rE(c))) return false;
|
||||
NatInf lhs = NatInf::of(rE(c));
|
||||
NatInf rhs = niAdd(cardOK(c.r->end_.key, o.lastK()),
|
||||
NatInf::of(static_cast<std::uint64_t>(c.n)));
|
||||
@@ -503,15 +465,15 @@ struct Impl {
|
||||
return mk(crossedBegin(c), topKey());
|
||||
}
|
||||
|
||||
RangeRead zeroBeginAnchor(const Ctx& c, const Obs& o) const {
|
||||
RangeRead out = *c.r;
|
||||
WideRead zeroBeginAnchor(const Ctx& c, const Obs& o) const {
|
||||
WideRead out = *c.r;
|
||||
out.begin.key = asKey(o.firstK());
|
||||
out.begin.offset = 1;
|
||||
return out;
|
||||
}
|
||||
|
||||
ConflictRange zeroCrossLongAnchor(const Ctx& c, const Obs& o) const {
|
||||
RangeRead a = zeroBeginAnchor(c, o);
|
||||
WideRead a = zeroBeginAnchor(c, o);
|
||||
Ctx ca = makeCtx(a, o);
|
||||
if (oLtK(c.r->end_.key, o.firstK()))
|
||||
return completeForwardCrossedGeneral(ca, o);
|
||||
@@ -533,7 +495,7 @@ struct Impl {
|
||||
}
|
||||
|
||||
ConflictRange closedFormZeroCrossUnderEnd(const Ctx& c, const Obs& o) const {
|
||||
RangeRead a = zeroBeginAnchor(c, o);
|
||||
WideRead a = zeroBeginAnchor(c, o);
|
||||
Ctx ca = makeCtx(a, o);
|
||||
return closedFormOffsetOne(ca, o);
|
||||
}
|
||||
@@ -551,7 +513,7 @@ struct Impl {
|
||||
}
|
||||
|
||||
ConflictRange closedFormZeroCrossCompleteRev(const Ctx& c, const Obs& o) const {
|
||||
RangeRead r2 = *c.r;
|
||||
WideRead r2 = *c.r;
|
||||
r2.reverse = false;
|
||||
Obs o2 = o;
|
||||
o2.reversed = true;
|
||||
@@ -561,10 +523,8 @@ struct Impl {
|
||||
}
|
||||
|
||||
bool zeroCrossUnderEnd(const Ctx& c, const Obs& o) const {
|
||||
return c.r->begin.offset == 0 && !o.more && !c.r->reverse &&
|
||||
c.r->end_.offset >= 2 &&
|
||||
kLtO(leastReportedK(c, o), c.r->begin.key) &&
|
||||
optLe(c.r->begin.key, c.r->end_.key);
|
||||
// The only caller tests `crossedCompleteBeginZero`, i.e. `begin.offset == 0`.
|
||||
return optLe(c.r->begin.key, c.r->end_.key);
|
||||
}
|
||||
|
||||
ConflictRange closedFormZeroCrossResidual(const Ctx& c, const Obs& o) const {
|
||||
@@ -580,7 +540,7 @@ struct Impl {
|
||||
|
||||
// ----- empty result ------------------------------------------------------
|
||||
|
||||
bool AlwaysEmpty(const RangeRead& r) const {
|
||||
bool AlwaysEmpty(const WideRead& r) const {
|
||||
NatInf gap = intervalCard(r.begin.key, r.end_.key);
|
||||
std::uint64_t b = noPinB(gap, r.begin.offset, r.end_.offset);
|
||||
std::uint64_t e = cELo(r.begin.offset, r.end_.offset);
|
||||
@@ -598,32 +558,30 @@ struct Impl {
|
||||
} else if (isTop(r.end_.key)) {
|
||||
inner = true;
|
||||
} else {
|
||||
bool firstFour =
|
||||
niLe(NatInf::of(b), underBegin) &&
|
||||
niLe(NatInf::of(e), underEnd) &&
|
||||
niLe(NatInf::of(natSub(b, e)),
|
||||
intervalCard(r.end_.key, r.begin.key)) &&
|
||||
niLe(NatInf::of(natSub(e, b)),
|
||||
intervalCard(r.begin.key, r.end_.key));
|
||||
// Readability makes the two cardinality bounds and `ePlusEnd > 0`
|
||||
// automatic once the first two bounds hold, so only the endpoint
|
||||
// ordering remains to decide.
|
||||
bool firstTwo = niLe(NatInf::of(b), underBegin) &&
|
||||
niLe(NatInf::of(e), underEnd);
|
||||
Int ePlusEnd = static_cast<Int>(e) + r.end_.offset - 1;
|
||||
Int bPlusBegin = static_cast<Int>(b) + r.begin.offset - 1;
|
||||
inner = firstFour && ePlusEnd > 0 && bPlusBegin < ePlusEnd;
|
||||
inner = firstTwo && bPlusBegin < ePlusEnd;
|
||||
}
|
||||
return !inner;
|
||||
}
|
||||
|
||||
ConflictRange emptyRange(const RangeRead& r) const {
|
||||
ConflictRange emptyRange(const WideRead& r) const {
|
||||
return mk(r.begin.key, r.begin.key);
|
||||
}
|
||||
|
||||
ConflictRange crossedRangeGeneral(const RangeRead& r) const {
|
||||
ConflictRange crossedRangeGeneral(const WideRead& r) const {
|
||||
NatInf diff = niSub(NatInf::of(residualE(r)), NatInf::of(residualB(r)));
|
||||
if (niLe(diff, intervalCard(r.end_.key, r.begin.key)))
|
||||
return mk(r.end_.key, topKey());
|
||||
return mk(r.begin.key, topKey());
|
||||
}
|
||||
|
||||
ConflictRange emptyValue(const RangeRead& r) const {
|
||||
ConflictRange emptyValue(const WideRead& r) const {
|
||||
if (AlwaysEmpty(r)) return emptyRange(r);
|
||||
if (r.begin.offset <= 0) {
|
||||
if (r.end_.offset >= 2) return mk(bottomKey(), topKey());
|
||||
@@ -637,14 +595,14 @@ struct Impl {
|
||||
return crossedRangeGeneral(r);
|
||||
}
|
||||
|
||||
ConflictRange topRange(const RangeRead& r) const {
|
||||
ConflictRange topRange(const WideRead& r) const {
|
||||
return mk(r.begin.key, topKey());
|
||||
}
|
||||
|
||||
// ----- complete and forward truncated results ----------------------------
|
||||
|
||||
OptKeyView closedFormBegin(const Ctx& c, const Obs& o) const {
|
||||
return clampsToBottom(c, o) ? bottomKeyView() : c.r->begin.key;
|
||||
return clampsToBottom(c) ? bottomKeyView() : c.r->begin.key;
|
||||
}
|
||||
|
||||
bool closedFormFinite(const Ctx& c) const {
|
||||
@@ -655,7 +613,7 @@ struct Impl {
|
||||
OptKeyView begin = closedFormBegin(c, o);
|
||||
if (c.r->end_.offset <= 1)
|
||||
return mk(std::move(begin), c.r->end_.key);
|
||||
if (clampsToBottom(c, o) || optLe(c.r->begin.key, c.r->end_.key)) {
|
||||
if (clampsToBottom(c) || optLe(c.r->begin.key, c.r->end_.key)) {
|
||||
if (c.t == rE(c)) return mk(std::move(begin), after(o.lastK()));
|
||||
if (allBelowEnd(c)) return mk(std::move(begin), topKey());
|
||||
if (closedFormFinite(c)) return mk(std::move(begin), after(o.lastK()));
|
||||
@@ -668,7 +626,7 @@ struct Impl {
|
||||
|
||||
ConflictRange closedFormComplete(const Ctx& c, const Obs& o) const {
|
||||
if (!c.r->reverse) return closedFormCompleteForward(c, o);
|
||||
RangeRead r2 = *c.r;
|
||||
WideRead r2 = *c.r;
|
||||
r2.reverse = false;
|
||||
Obs o2 = o;
|
||||
o2.reversed = true;
|
||||
@@ -678,7 +636,7 @@ struct Impl {
|
||||
}
|
||||
|
||||
NatInf closedFormForwardCapacity(const Ctx& c, const Obs& o) const {
|
||||
if (clampsToBottom(c, o)) return NatInf::of(0);
|
||||
if (clampsToBottom(c)) return NatInf::of(0);
|
||||
return cardEB(c);
|
||||
}
|
||||
|
||||
@@ -745,19 +703,11 @@ struct Impl {
|
||||
return c.r->end_.offset >= 2 && allBelowEnd(c);
|
||||
}
|
||||
|
||||
bool zeroCrossOvershootComplete(const Ctx& c, const Obs& o) const {
|
||||
return c.r->begin.offset == 0 && !o.more && !c.r->reverse &&
|
||||
overshoot(c) && kLtO(leastReportedK(c, o), c.r->begin.key);
|
||||
}
|
||||
|
||||
bool negCrossOvershoot(const Ctx& c, const Obs& o) const {
|
||||
return !c.r->reverse && c.r->begin.offset < 0 && !o.more && overshoot(c) &&
|
||||
kLtO(o.firstK(), c.r->begin.key);
|
||||
}
|
||||
|
||||
bool crossedCompleteOvershoot(const Ctx& c, const Obs& o) const {
|
||||
return zeroCrossOvershootComplete(c, o) || negCrossOvershoot(c, o);
|
||||
}
|
||||
// `closedFormCrossedComplete` is reached only from `closedFormCrossed`,
|
||||
// which the entry point enters only when `begin.offset <= 0`; the
|
||||
// `begin.offset >= 1` cases go to the positive subtree. So the mode does
|
||||
// not need to be retested here.
|
||||
bool crossedCompleteOvershoot(const Ctx& c) const { return overshoot(c); }
|
||||
|
||||
bool crossedCompleteEndLeOne(const Ctx& c) const {
|
||||
if (c.r->end_.offset == 1 && bSlack(c) < c.below)
|
||||
@@ -766,21 +716,10 @@ struct Impl {
|
||||
!(c.below <= bSlack(c) && clampNonposBottom(c));
|
||||
}
|
||||
|
||||
bool zeroCrossLongTop(const Ctx& c, const Obs& o) const {
|
||||
return c.r->begin.offset == 0 && !o.more && !c.r->reverse &&
|
||||
c.r->end_.offset >= 2 && isTop(c.r->begin.key);
|
||||
}
|
||||
|
||||
bool zeroCrossLongFinite(const Ctx& c, const Obs& o) const {
|
||||
return c.r->begin.offset == 0 && !o.more && !c.r->reverse &&
|
||||
c.r->end_.offset >= 2 && !isTop(c.r->begin.key) &&
|
||||
optLt(c.r->end_.key, c.r->begin.key) &&
|
||||
kLtO(leastReportedK(c, o), c.r->begin.key);
|
||||
}
|
||||
|
||||
// The overshoot branch above already covered the offset-zero overshoot
|
||||
// cases, so the mode alone decides here.
|
||||
bool crossedCompleteBeginZero(const Ctx& c, const Obs& o) const {
|
||||
return zeroCrossUnderEnd(c, o) || zeroCrossLongTop(c, o) ||
|
||||
zeroCrossLongFinite(c, o);
|
||||
return c.r->begin.offset == 0;
|
||||
}
|
||||
|
||||
bool crossedCompleteClamp(const Ctx& c, const Obs& o) const {
|
||||
@@ -790,7 +729,7 @@ struct Impl {
|
||||
}
|
||||
|
||||
ConflictRange closedFormCrossedComplete(const Ctx& c, const Obs& o) const {
|
||||
if (crossedCompleteOvershoot(c, o))
|
||||
if (crossedCompleteOvershoot(c))
|
||||
return closedFormNegCrossTopUnified(c, o);
|
||||
if (crossedCompleteEndLeOne(c))
|
||||
return closedFormNegCrossNonclampUnified(c, o);
|
||||
@@ -803,21 +742,19 @@ struct Impl {
|
||||
|
||||
// ----- crossed truncated dispatch ---------------------------------------
|
||||
|
||||
// Every node below is reached only from the reversed truncation branch, where
|
||||
// `more` is set, `reverse` is set, `begin.offset < 0` and `end.offset >= 2`
|
||||
// (the `end.offset <= 1` case is handled by the dispatch).
|
||||
bool zeroCrossTruncRevCrossed(const Ctx& c, const Obs& o) const {
|
||||
return c.r->begin.offset == 0 && c.r->reverse && o.more &&
|
||||
kLtO(leastReportedK(c, o), c.r->begin.key);
|
||||
return c.r->begin.offset == 0;
|
||||
}
|
||||
|
||||
bool negCrossTruncRevUnder(const Ctx& c, const Obs& o) const {
|
||||
return o.more && c.r->reverse && c.r->begin.offset < 0 &&
|
||||
kLtO(o.lastK(), c.r->begin.key) &&
|
||||
optLe(c.r->begin.key, c.r->end_.key) && c.r->end_.offset <= 1;
|
||||
return optLe(c.r->begin.key, c.r->end_.key) && c.r->end_.offset <= 1;
|
||||
}
|
||||
|
||||
bool negCrossTruncRevCrossedLow(const Ctx& c, const Obs& o) const {
|
||||
return o.more && c.r->reverse && c.r->begin.offset < 0 &&
|
||||
kLtO(o.lastK(), c.r->begin.key) &&
|
||||
optLt(c.r->end_.key, c.r->begin.key) &&
|
||||
return optLt(c.r->end_.key, c.r->begin.key) &&
|
||||
c.r->end_.offset <= 1 &&
|
||||
niLe(niAdd(niAdd(cardEB(c),
|
||||
NatInf::of(static_cast<std::uint64_t>(c.n))),
|
||||
@@ -831,31 +768,24 @@ struct Impl {
|
||||
}
|
||||
|
||||
bool negCrossTruncRevBelowEnd(const Ctx& c, const Obs& o) const {
|
||||
return o.more && c.r->reverse && c.r->begin.offset < 0 &&
|
||||
kLtO(o.lastK(), c.r->begin.key) && overshoot(c);
|
||||
return overshoot(c);
|
||||
}
|
||||
|
||||
bool negCrossTruncRevResidualRoom(const Ctx& c, const Obs& o) const {
|
||||
return o.more && c.r->reverse && c.r->begin.offset < 0 &&
|
||||
c.r->end_.offset >= 2 && anyAtOrAboveEnd(c) &&
|
||||
niLe(niAdd(cardOK(c.r->end_.key, o.firstK()), NatInf::of(2)),
|
||||
return niLe(niAdd(cardOK(c.r->end_.key, o.firstK()), NatInf::of(2)),
|
||||
NatInf::of(rE(c)));
|
||||
}
|
||||
|
||||
bool negCrossTruncRevResidualTopCrossedSmallGap(const Ctx& c,
|
||||
const Obs& o) const {
|
||||
if (!(o.more && c.r->reverse && c.r->begin.offset < 0 &&
|
||||
c.r->end_.offset >= 2 && oLtK(c.r->end_.key, o.lastK())))
|
||||
return false;
|
||||
if (!oLtK(c.r->end_.key, o.lastK())) return false;
|
||||
NatInf lhs = niAdd(cardOK(c.r->end_.key, o.lastK()),
|
||||
NatInf::of(static_cast<std::uint64_t>(c.n)));
|
||||
return niLt(lhs, NatInf::of(rE(c)));
|
||||
}
|
||||
|
||||
bool negCrossTruncRevResidualTopCrossed(const Ctx& c, const Obs& o) const {
|
||||
return o.more && c.r->reverse && c.r->begin.offset < 0 &&
|
||||
c.r->end_.offset >= 2 && kLeO(o.lastK(), c.r->end_.key) &&
|
||||
c.t < rE(c);
|
||||
return !oLtK(c.r->end_.key, o.lastK()) && c.t < rE(c);
|
||||
}
|
||||
|
||||
bool revTruncGeTwoTop(const Ctx& c, const Obs& o) const {
|
||||
@@ -867,7 +797,7 @@ struct Impl {
|
||||
|
||||
bool negCrossTruncRevResidualTopLargeCrossed(const Ctx& c, const Obs& o) const {
|
||||
return negCrossTruncRevResidualTopLarge(c, o) &&
|
||||
kLtO(o.lastK(), c.r->begin.key) && !isTop(c.r->begin.key);
|
||||
!isTop(c.r->begin.key);
|
||||
}
|
||||
|
||||
bool revTruncGeTwoLarge(const Ctx& c, const Obs& o) const {
|
||||
@@ -876,9 +806,7 @@ struct Impl {
|
||||
}
|
||||
|
||||
bool negCrossTruncTopTop(const Ctx& c, const Obs& o) const {
|
||||
return o.more && !c.r->reverse && c.r->begin.offset < 0 &&
|
||||
isTop(c.r->begin.key) && isTop(c.r->end_.key) &&
|
||||
c.r->end_.offset <= 0;
|
||||
return isTop(c.r->begin.key);
|
||||
}
|
||||
|
||||
bool negCrossTruncTopTopEq(const Ctx& c, const Obs& o) const {
|
||||
@@ -917,7 +845,10 @@ struct Impl {
|
||||
}
|
||||
|
||||
bool closedFormForwardSaturated(const Ctx& c, const Obs& o) const {
|
||||
if (!(rE(c) > 0 && oLtK(c.r->end_.key, o.firstK()))) return false;
|
||||
// The caller has `end.offset > 0`. If `end.offset == 1` then `rE == 0`,
|
||||
// and readability makes `end.key < firstKey` false, so the `rE > 0`
|
||||
// guard adds nothing.
|
||||
if (!oLtK(c.r->end_.key, o.firstK())) return false;
|
||||
NatInf rhs = niAdd(cardOK(c.r->end_.key, o.firstK()),
|
||||
NatInf::of(static_cast<std::uint64_t>(c.n)));
|
||||
return niLt(NatInf::of(rE(c)), rhs);
|
||||
@@ -954,7 +885,7 @@ struct Impl {
|
||||
ConflictRange closedFormCrossed(const Ctx& c, const Obs& o) const {
|
||||
if (!o.more) {
|
||||
if (!c.r->reverse) return closedFormCrossedComplete(c, o);
|
||||
RangeRead r2 = *c.r;
|
||||
WideRead r2 = *c.r;
|
||||
r2.reverse = false;
|
||||
Obs o2 = o;
|
||||
o2.reversed = true;
|
||||
@@ -980,7 +911,7 @@ struct Impl {
|
||||
|
||||
// ----- entry point ------------------------------------------------------
|
||||
|
||||
ConflictRange closedForm(const RangeRead& r, const Result& res) const {
|
||||
ConflictRange closedForm(const WideRead& r, const Result& res) const {
|
||||
if (res.keys.empty()) return emptyValue(r);
|
||||
Obs o;
|
||||
o.keys = res.keys;
|
||||
@@ -998,8 +929,7 @@ struct Impl {
|
||||
} // namespace
|
||||
|
||||
ConflictRange closedForm(const RangeRead& r, const Result& res) {
|
||||
assertInputOffsetsFitInt32(r);
|
||||
return Impl{}.closedForm(r, res);
|
||||
return Impl{}.closedForm(detail::widen(r), res);
|
||||
}
|
||||
|
||||
} // namespace cr::fast
|
||||
@@ -1,3 +1,5 @@
|
||||
// Released under Apache 2.0 license as described in the file LICENSE.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "model.h"
|
||||
|
||||
+134
-131
@@ -1,3 +1,5 @@
|
||||
// Released under Apache 2.0 license as described in the file LICENSE.
|
||||
|
||||
// A deliberately literal transliteration of `Statement.closedForm` from
|
||||
// Statement.lean. Every helper mirrors the Lean definition of the same name,
|
||||
// including repeated scans of `res.keys`; correctness of the translation, not
|
||||
@@ -10,10 +12,11 @@
|
||||
namespace cr::literal {
|
||||
namespace {
|
||||
|
||||
using WideRead = cr::detail::RangeRead;
|
||||
|
||||
using cr::afterFirst;
|
||||
using cr::afterLast;
|
||||
using cr::asKey;
|
||||
using cr::assertInputOffsetsFitInt32;
|
||||
using cr::beginSlack;
|
||||
using cr::bottomKey;
|
||||
using cr::clampNonposD;
|
||||
@@ -57,13 +60,13 @@ ConflictRange mkRange(B&& b, E&& e) {
|
||||
// Arithmetic helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
NatInf clampCap(const RangeRead& r) { return intervalCard(r.end_.key, r.begin.key); }
|
||||
NatInf clampCap(const WideRead& r) { return intervalCard(r.end_.key, r.begin.key); }
|
||||
|
||||
std::uint64_t clampSlack(const RangeRead& r, const Result& res) {
|
||||
std::uint64_t clampSlack(const WideRead& r, const Result& res) {
|
||||
return natSub(beginSlack(r) + 1, reportedBelowBegin(r, res));
|
||||
}
|
||||
|
||||
OptKey clampEndOneUpper(const RangeRead& r, const Result& res) {
|
||||
OptKey clampEndOneUpper(const WideRead& r, const Result& res) {
|
||||
NatInf slack = NatInf::of(clampSlack(r, res));
|
||||
NatInf cap = clampCap(r);
|
||||
if (niLe(slack, cap) && (niLt(slack, cap) || first(res) != bottomKeyView()))
|
||||
@@ -71,25 +74,25 @@ OptKey clampEndOneUpper(const RangeRead& r, const Result& res) {
|
||||
return ownKey(r.end_.key);
|
||||
}
|
||||
|
||||
NatInf clampHighCap(const RangeRead& r, const Result& res) {
|
||||
NatInf clampHighCap(const WideRead& r, const Result& res) {
|
||||
return intervalCard(afterLast(res), r.begin.key);
|
||||
}
|
||||
|
||||
std::uint64_t clampHighThresh(const RangeRead& r, const Result& res) {
|
||||
std::uint64_t clampHighThresh(const WideRead& r, const Result& res) {
|
||||
return clampSlack(r, res) + (isBottom(r.end_.key) ? 1 : 0);
|
||||
}
|
||||
|
||||
OptKey clampHighU(const RangeRead& r, const Result& res) {
|
||||
OptKey clampHighU(const WideRead& r, const Result& res) {
|
||||
if (niLe(NatInf::of(clampHighThresh(r, res)), clampHighCap(r, res)))
|
||||
return ownKey(r.begin.key);
|
||||
return afterLast(res);
|
||||
}
|
||||
|
||||
std::uint64_t clampLongC(const RangeRead& r, const Result& res) {
|
||||
std::uint64_t clampLongC(const WideRead& r, const Result& res) {
|
||||
return natSub(natSub(beginSlack(r), reportedBelowBegin(r, res)), clampNonposD(r));
|
||||
}
|
||||
|
||||
OptKey clampLongUpper(const RangeRead& r, const Result& res) {
|
||||
OptKey clampLongUpper(const WideRead& r, const Result& res) {
|
||||
NatInf cap = clampCap(r);
|
||||
NatInf c = NatInf::of(clampLongC(r, res));
|
||||
if (niLt(cap, c) || (niEq(cap, c) && first(res) == bottomKeyView()))
|
||||
@@ -97,19 +100,19 @@ OptKey clampLongUpper(const RangeRead& r, const Result& res) {
|
||||
return ownKey(r.begin.key);
|
||||
}
|
||||
|
||||
bool clampNonposBottom(const RangeRead& r, const Result& res) {
|
||||
bool clampNonposBottom(const WideRead& r, const Result& res) {
|
||||
NatInf lhs = niAdd(NatInf::of(reportedBelowBegin(r, res)),
|
||||
niSub(NatInf::of(clampNonposD(r) + 1),
|
||||
intervalCard(r.begin.key, r.end_.key)));
|
||||
return niLe(lhs, NatInf::of(beginSlack(r)));
|
||||
}
|
||||
|
||||
bool clampedNonpos(const RangeRead& r, const Result& res) {
|
||||
bool clampedNonpos(const WideRead& r, const Result& res) {
|
||||
return r.begin.offset <= 0 &&
|
||||
optLe(r.begin.key, leastReportedOpt(r, res));
|
||||
}
|
||||
|
||||
bool clampsToBottom(const RangeRead& r, const Result& res) {
|
||||
bool clampsToBottom(const WideRead& r, const Result& res) {
|
||||
return r.begin.offset <= 0 && optLe(r.begin.key, first(res));
|
||||
}
|
||||
|
||||
@@ -127,36 +130,36 @@ std::uint64_t noPinB(NatInf KBE, Int oB, Int oE) {
|
||||
return lo > KBE.n ? lo - KBE.n : 0;
|
||||
}
|
||||
|
||||
std::uint64_t residualD(const RangeRead& r, NatInf g) {
|
||||
std::uint64_t residualD(const WideRead& r, NatInf g) {
|
||||
if (g.inf) return 0;
|
||||
return natSub(residualB(r), g.n);
|
||||
}
|
||||
|
||||
std::uint64_t closedFormDeficit(const RangeRead& r) {
|
||||
std::uint64_t closedFormDeficit(const WideRead& r) {
|
||||
return residualD(r, intervalCard(r.begin.key, r.end_.key));
|
||||
}
|
||||
|
||||
bool allBelowEnd(const RangeRead& r, const Result& res) {
|
||||
bool allBelowEnd(const WideRead& r, const Result& res) {
|
||||
for (KeyView k : res.keys)
|
||||
if (!optLt(asKey(k), r.end_.key)) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool anyAtOrAboveEnd(const RangeRead& r, const Result& res) {
|
||||
bool anyAtOrAboveEnd(const WideRead& r, const Result& res) {
|
||||
for (KeyView k : res.keys)
|
||||
if (optLe(r.end_.key, asKey(k))) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::uint64_t gapOvershoot(const RangeRead& r, const Result& res) {
|
||||
std::uint64_t gapOvershoot(const WideRead& r, const Result& res) {
|
||||
return natSub(residualE(r), residualT(r, res));
|
||||
}
|
||||
|
||||
std::uint64_t crossedOvershootGeneral(const RangeRead& r, const Result& res) {
|
||||
std::uint64_t crossedOvershootGeneral(const WideRead& r, const Result& res) {
|
||||
return natSub(natSub(residualE(r), residualT(r, res)), residualB(r));
|
||||
}
|
||||
|
||||
OptKeyView crossedBegin(const RangeRead& r, const Result& res) {
|
||||
OptKeyView crossedBegin(const WideRead& r, const Result& res) {
|
||||
if (niLe(NatInf::of(crossedOvershootGeneral(r, res)),
|
||||
intervalCard(r.end_.key, r.begin.key)))
|
||||
return r.end_.key;
|
||||
@@ -167,39 +170,39 @@ OptKeyView crossedBegin(const RangeRead& r, const Result& res) {
|
||||
// Crossed complete cases
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
bool negCrossClampEndOne(const RangeRead& r, const Result& res) {
|
||||
bool negCrossClampEndOne(const WideRead& r, const Result& res) {
|
||||
return !r.reverse && r.begin.offset < 0 && !res.more &&
|
||||
r.end_.offset == 1 && optLt(first(res), r.begin.key) &&
|
||||
reportedBelowBegin(r, res) <= beginSlack(r);
|
||||
}
|
||||
|
||||
bool negCrossHeadBottom(const RangeRead& r, const Result& res) {
|
||||
bool negCrossHeadBottom(const WideRead& r, const Result& res) {
|
||||
return !r.reverse && r.begin.offset < 0 && !res.more &&
|
||||
r.end_.offset <= 0 && optLe(r.begin.key, r.end_.key) &&
|
||||
first(res) == bottomKeyView();
|
||||
}
|
||||
|
||||
bool negCrossClampNonposLastGe(const RangeRead& r, const Result& res) {
|
||||
bool negCrossClampNonposLastGe(const WideRead& r, const Result& res) {
|
||||
return !r.reverse && r.begin.offset < 0 && !res.more &&
|
||||
r.end_.offset <= 0 && optLt(first(res), r.begin.key) &&
|
||||
optLe(r.begin.key, last(res)) &&
|
||||
reportedBelowBegin(r, res) <= beginSlack(r);
|
||||
}
|
||||
|
||||
bool negCrossClampNonposBottom(const RangeRead& r, const Result& res) {
|
||||
bool negCrossClampNonposBottom(const WideRead& r, const Result& res) {
|
||||
return !r.reverse && r.begin.offset < 0 && !res.more &&
|
||||
r.end_.offset <= 0 && optLe(r.begin.key, r.end_.key) &&
|
||||
optLt(first(res), r.begin.key) && clampNonposBottom(r, res);
|
||||
}
|
||||
|
||||
bool negCrossClampLongNonpos(const RangeRead& r, const Result& res) {
|
||||
bool negCrossClampLongNonpos(const WideRead& r, const Result& res) {
|
||||
return !r.reverse && r.begin.offset < 0 && !res.more &&
|
||||
r.end_.offset <= 0 && optLt(first(res), r.begin.key) &&
|
||||
reportedBelowBegin(r, res) <= beginSlack(r) &&
|
||||
clampNonposBottom(r, res) && optLt(r.end_.key, r.begin.key);
|
||||
}
|
||||
|
||||
ConflictRange closedFormNegCrossClampUnified(const RangeRead& r, const Result& res) {
|
||||
ConflictRange closedFormNegCrossClampUnified(const WideRead& r, const Result& res) {
|
||||
OptKey end;
|
||||
if (negCrossClampEndOne(r, res))
|
||||
end = clampEndOneUpper(r, res);
|
||||
@@ -211,31 +214,31 @@ ConflictRange closedFormNegCrossClampUnified(const RangeRead& r, const Result& r
|
||||
return mkRange(bottomKey(), std::move(end));
|
||||
}
|
||||
|
||||
OptKey negCrossCompleteHighM(const RangeRead& r, const Result& res) {
|
||||
OptKey negCrossCompleteHighM(const WideRead& r, const Result& res) {
|
||||
return optMax(r.begin.key, afterLast(res));
|
||||
}
|
||||
|
||||
std::uint64_t negCrossHighD(const RangeRead& r, const Result& res) {
|
||||
std::uint64_t negCrossHighD(const WideRead& r, const Result& res) {
|
||||
return natSub(residualE(r), res.keys.size());
|
||||
}
|
||||
|
||||
NatInf negCrossHighGapCap(const RangeRead& r, const Result& res) {
|
||||
NatInf negCrossHighGapCap(const WideRead& r, const Result& res) {
|
||||
return intervalCard(r.end_.key, first(res));
|
||||
}
|
||||
|
||||
bool negCrossHighUsesEnd(const RangeRead& r, const Result& res) {
|
||||
bool negCrossHighUsesEnd(const WideRead& r, const Result& res) {
|
||||
NatInf d = NatInf::of(negCrossHighD(r, res));
|
||||
NatInf gap = negCrossHighGapCap(r, res);
|
||||
return niLt(d, gap) || (niLe(d, gap) && optLt(r.begin.key, topKey()));
|
||||
}
|
||||
|
||||
bool negCrossCompleteHigh(const RangeRead& r, const Result& res) {
|
||||
bool negCrossCompleteHigh(const WideRead& r, const Result& res) {
|
||||
return !r.reverse && r.begin.offset < 0 && !res.more &&
|
||||
r.end_.offset >= 2 && optLt(first(res), r.begin.key) &&
|
||||
optLt(r.end_.key, first(res));
|
||||
}
|
||||
|
||||
ConflictRange closedFormNegCrossCompleteHigh(const RangeRead& r, const Result& res) {
|
||||
ConflictRange closedFormNegCrossCompleteHigh(const WideRead& r, const Result& res) {
|
||||
if (reportedBelowBegin(r, res) <= beginSlack(r)) {
|
||||
if (res.keys.size() == residualE(r)) return mkRange(bottomKey(), clampHighU(r, res));
|
||||
return mkRange(bottomKey(), topKey());
|
||||
@@ -248,23 +251,23 @@ ConflictRange closedFormNegCrossCompleteHigh(const RangeRead& r, const Result& r
|
||||
|
||||
ConflictRange topOfLast(KeyView k) { return mkRange(asKey(k), topKey()); }
|
||||
|
||||
ConflictRange closedFormNegCrossOvershoot(const RangeRead& r, const Result& res) {
|
||||
ConflictRange closedFormNegCrossOvershoot(const WideRead& r, const Result& res) {
|
||||
if (reportedBelowBegin(r, res) <= beginSlack(r))
|
||||
return mkRange(bottomKey(), topKey());
|
||||
return topOfLast(firstKey(res));
|
||||
}
|
||||
|
||||
ConflictRange closedFormNegCrossEndOne(const RangeRead& r, const Result& res) {
|
||||
ConflictRange closedFormNegCrossEndOne(const WideRead& r, const Result& res) {
|
||||
return mkRange(first(res), r.end_.key);
|
||||
}
|
||||
|
||||
ConflictRange closedFormNegCrossEndOneLong(const RangeRead& r, const Result& res) {
|
||||
ConflictRange closedFormNegCrossEndOneLong(const WideRead& r, const Result& res) {
|
||||
return mkRange(first(res), r.begin.key);
|
||||
}
|
||||
|
||||
ConflictRange closedFormNegCrossCompleteResidual(const RangeRead& r, const Result& res) {
|
||||
ConflictRange closedFormNegCrossCompleteResidual(const WideRead& r, const Result& res) {
|
||||
OptKey reducedEnd = afterLast(res);
|
||||
RangeRead rp = r;
|
||||
WideRead rp = r;
|
||||
rp.end_.key = optView(reducedEnd);
|
||||
rp.end_.offset = r.end_.offset - static_cast<Int>(residualT(r, res));
|
||||
if (rp.end_.offset >= 2)
|
||||
@@ -276,17 +279,17 @@ ConflictRange closedFormNegCrossCompleteResidual(const RangeRead& r, const Resul
|
||||
return closedFormNegCrossEndOneLong(rp, res);
|
||||
}
|
||||
|
||||
ConflictRange closedFormNegCrossCompleteHighResidual(const RangeRead& r, const Result& res) {
|
||||
ConflictRange closedFormNegCrossCompleteHighResidual(const WideRead& r, const Result& res) {
|
||||
if (negCrossCompleteHigh(r, res))
|
||||
return closedFormNegCrossCompleteHigh(r, res);
|
||||
return closedFormNegCrossCompleteResidual(r, res);
|
||||
}
|
||||
|
||||
ConflictRange closedFormNegCrossNonclampUnified(const RangeRead& r, const Result& res) {
|
||||
ConflictRange closedFormNegCrossNonclampUnified(const WideRead& r, const Result& res) {
|
||||
return mkRange(first(res), optMax(r.end_.key, r.begin.key));
|
||||
}
|
||||
|
||||
ConflictRange closedFormNegCrossTopUnified(const RangeRead& r, const Result& res) {
|
||||
ConflictRange closedFormNegCrossTopUnified(const WideRead& r, const Result& res) {
|
||||
OptKeyView begin = reportedBelowBegin(r, res) <= beginSlack(r)
|
||||
? bottomKeyView()
|
||||
: leastReportedOpt(r, res);
|
||||
@@ -297,7 +300,7 @@ ConflictRange closedFormNegCrossTopUnified(const RangeRead& r, const Result& res
|
||||
// Crossed truncated cases
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
bool negCrossTruncNonposGap(const RangeRead& r, const Result& res) {
|
||||
bool negCrossTruncNonposGap(const WideRead& r, const Result& res) {
|
||||
return res.more && !r.reverse && r.begin.offset < 0 &&
|
||||
optLt(leastReportedOpt(r, res), r.begin.key) &&
|
||||
r.end_.offset <= 0 &&
|
||||
@@ -310,12 +313,12 @@ bool negCrossTruncNonposGap(const RangeRead& r, const Result& res) {
|
||||
NatInf::of(clampNonposD(r) + 1)));
|
||||
}
|
||||
|
||||
bool negCrossTruncNonposGapA2aBottom(const RangeRead& r, const Result& res) {
|
||||
bool negCrossTruncNonposGapA2aBottom(const WideRead& r, const Result& res) {
|
||||
return negCrossTruncNonposGap(r, res) && optLt(r.end_.key, r.begin.key) &&
|
||||
clampNonposBottom(r, res);
|
||||
}
|
||||
|
||||
bool negCrossTruncNonposGapA2aBottomHigh(const RangeRead& r, const Result& res) {
|
||||
bool negCrossTruncNonposGapA2aBottomHigh(const WideRead& r, const Result& res) {
|
||||
std::uint64_t extra = isBottom(first(res)) ? 1 : 0;
|
||||
NatInf lhs = NatInf::of(beginSlack(r) + 1 + extra);
|
||||
NatInf rhs = niAdd(NatInf::of(res.keys.size()),
|
||||
@@ -323,26 +326,26 @@ bool negCrossTruncNonposGapA2aBottomHigh(const RangeRead& r, const Result& res)
|
||||
return negCrossTruncNonposGapA2aBottom(r, res) && niLe(lhs, rhs);
|
||||
}
|
||||
|
||||
ConflictRange closedFormNegCrossTruncNonposGapA2aBottom(const RangeRead& r,
|
||||
ConflictRange closedFormNegCrossTruncNonposGapA2aBottom(const WideRead& r,
|
||||
const Result& res) {
|
||||
if (negCrossTruncNonposGapA2aBottomHigh(r, res))
|
||||
return mkRange(bottomKey(), r.begin.key);
|
||||
return mkRange(bottomKey(), r.end_.key);
|
||||
}
|
||||
|
||||
bool finiteUnderCond(const RangeRead& r, const Result& res) {
|
||||
bool finiteUnderCond(const WideRead& r, const Result& res) {
|
||||
NatInf lhs = niAdd(NatInf::of(natSub(reportedBelowBegin(r, res), 1)),
|
||||
intervalCard(afterFirst(res), r.begin.key));
|
||||
return niLe(lhs, NatInf::of(beginSlack(r)));
|
||||
}
|
||||
|
||||
bool negCrossTruncRevResidualFiniteOver(const RangeRead& r, const Result& res) {
|
||||
bool negCrossTruncRevResidualFiniteOver(const WideRead& r, const Result& res) {
|
||||
return res.more && r.reverse && r.begin.offset < 0 && r.end_.offset >= 2 &&
|
||||
optLt(r.end_.key, last(res)) && optLt(last(res), r.begin.key) &&
|
||||
residualT(r, res) == residualE(r);
|
||||
}
|
||||
|
||||
ConflictRange closedFormNegCrossTruncRevResidualFiniteUnified(const RangeRead& r,
|
||||
ConflictRange closedFormNegCrossTruncRevResidualFiniteUnified(const WideRead& r,
|
||||
const Result& res) {
|
||||
OptKeyView L = last(res);
|
||||
OptKey U = finiteUnderCond(r, res) ? afterFirst(res) : ownKey(r.begin.key);
|
||||
@@ -351,7 +354,7 @@ ConflictRange closedFormNegCrossTruncRevResidualFiniteUnified(const RangeRead& r
|
||||
return mkRange(std::move(L), std::move(U));
|
||||
}
|
||||
|
||||
bool sentinelLargeTight(const RangeRead& r, const Result& res) {
|
||||
bool sentinelLargeTight(const WideRead& r, const Result& res) {
|
||||
NatInf lhs = NatInf::of(residualE(r));
|
||||
NatInf rhs = niAdd(intervalCard(r.end_.key, last(res)),
|
||||
NatInf::of(res.keys.size()));
|
||||
@@ -359,7 +362,7 @@ bool sentinelLargeTight(const RangeRead& r, const Result& res) {
|
||||
return niEq(lhs, rhs) && nPlusBegin == 1;
|
||||
}
|
||||
|
||||
bool negCrossTruncRevResidualTopLarge(const RangeRead& r, const Result& res) {
|
||||
bool negCrossTruncRevResidualTopLarge(const WideRead& r, const Result& res) {
|
||||
if (!(res.more && r.reverse && r.begin.offset < 0 && r.end_.offset >= 2 &&
|
||||
optLt(r.end_.key, last(res)) && residualT(r, res) < residualE(r)))
|
||||
return false;
|
||||
@@ -369,11 +372,11 @@ bool negCrossTruncRevResidualTopLarge(const RangeRead& r, const Result& res) {
|
||||
return niLe(lhs, rhs);
|
||||
}
|
||||
|
||||
bool negCrossTruncRevResidualTopLargeSentinel(const RangeRead& r, const Result& res) {
|
||||
bool negCrossTruncRevResidualTopLargeSentinel(const WideRead& r, const Result& res) {
|
||||
return negCrossTruncRevResidualTopLarge(r, res) && isTop(r.begin.key);
|
||||
}
|
||||
|
||||
ConflictRange closedFormNegCrossTruncRevResidualTopLargeUnified(const RangeRead& r,
|
||||
ConflictRange closedFormNegCrossTruncRevResidualTopLargeUnified(const WideRead& r,
|
||||
const Result& res) {
|
||||
OptKeyView L = last(res);
|
||||
if (negCrossTruncRevResidualTopLargeSentinel(r, res) &&
|
||||
@@ -382,7 +385,7 @@ ConflictRange closedFormNegCrossTruncRevResidualTopLargeUnified(const RangeRead&
|
||||
return mkRange(r.end_.key, topKey());
|
||||
}
|
||||
|
||||
ConflictRange truncatedRangeRev(const RangeRead& r, KeyView lastReported) {
|
||||
ConflictRange truncatedRangeRev(const WideRead& r, KeyView lastReported) {
|
||||
return mkRange(asKey(lastReported), r.end_.key);
|
||||
}
|
||||
|
||||
@@ -391,7 +394,7 @@ ConflictRange truncatedRangeRev(const RangeRead& r, KeyView lastReported) {
|
||||
// Complete and offset-one cases
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
ConflictRange closedFormReverseTruncatedZero(const RangeRead& r, const Result& res) {
|
||||
ConflictRange closedFormReverseTruncatedZero(const WideRead& r, const Result& res) {
|
||||
if (r.end_.offset <= 1) return truncatedRangeRev(r, lastKey(res));
|
||||
OptKeyView L = last(res);
|
||||
if (residualT(r, res) == residualE(r)) {
|
||||
@@ -407,18 +410,18 @@ ConflictRange closedFormReverseTruncatedZero(const RangeRead& r, const Result& r
|
||||
return mkRange(std::move(L), topKey());
|
||||
}
|
||||
|
||||
ConflictRange truncatedRange(const RangeRead& r, KeyView lastReported) {
|
||||
ConflictRange truncatedRange(const WideRead& r, KeyView lastReported) {
|
||||
return mkRange(r.begin.key, asKey(keyAfter(lastReported)));
|
||||
}
|
||||
|
||||
ConflictRange truncatedForwardClosed(const RangeRead& r, const Result& res) {
|
||||
ConflictRange truncatedForwardClosed(const WideRead& r, const Result& res) {
|
||||
NatInf cap = intervalCard(r.end_.key, r.begin.key);
|
||||
NatInf room = NatInf::of(natSub(residualE(r), res.keys.size()));
|
||||
if (niLe(cap, room)) return truncatedRange(r, lastKey(res));
|
||||
return mkRange(r.end_.key, afterLast(res));
|
||||
}
|
||||
|
||||
ConflictRange completeClosedWith(const OptKey& endKey, const RangeRead& r,
|
||||
ConflictRange completeClosedWith(const OptKey& endKey, const WideRead& r,
|
||||
const Result& res) {
|
||||
OptKeyView begin = r.begin.key;
|
||||
if (optLt(r.end_.key, r.begin.key) &&
|
||||
@@ -432,48 +435,48 @@ ConflictRange completeClosedWith(const OptKey& endKey, const RangeRead& r,
|
||||
return mkRange(std::move(begin), topKey());
|
||||
}
|
||||
|
||||
ConflictRange completeReversedClosed(const RangeRead& r, const Result& res) {
|
||||
ConflictRange completeReversedClosed(const WideRead& r, const Result& res) {
|
||||
return completeClosedWith(afterFirst(res), r, res);
|
||||
}
|
||||
ConflictRange completeForwardClosed(const RangeRead& r, const Result& res) {
|
||||
ConflictRange completeForwardClosed(const WideRead& r, const Result& res) {
|
||||
return completeClosedWith(afterLast(res), r, res);
|
||||
}
|
||||
ConflictRange completeClosed(const RangeRead& r, const Result& res) {
|
||||
ConflictRange completeClosed(const WideRead& r, const Result& res) {
|
||||
return r.reverse ? completeReversedClosed(r, res) : completeForwardClosed(r, res);
|
||||
}
|
||||
|
||||
ConflictRange closedFormOffsetOneTruncated(const RangeRead& r, const Result& res) {
|
||||
ConflictRange closedFormOffsetOneTruncated(const WideRead& r, const Result& res) {
|
||||
if (r.end_.offset <= 0) return mkRange(r.begin.key, r.end_.key);
|
||||
return truncatedRange(r, lastKey(res));
|
||||
}
|
||||
|
||||
ConflictRange topRange(const RangeRead& r) { return mkRange(r.begin.key, topKey()); }
|
||||
ConflictRange topRange(const WideRead& r) { return mkRange(r.begin.key, topKey()); }
|
||||
|
||||
ConflictRange closedFormOffsetOneWith(KeyView endKey, const RangeRead& r,
|
||||
ConflictRange closedFormOffsetOneWith(KeyView endKey, const WideRead& r,
|
||||
const Result& res) {
|
||||
if (r.end_.offset <= 1) return mkRange(r.begin.key, r.end_.key);
|
||||
if (residualT(r, res) == residualE(r)) return truncatedRange(r, endKey);
|
||||
return topRange(r);
|
||||
}
|
||||
|
||||
ConflictRange closedFormOffsetOneRev(const RangeRead& r, const Result& res) {
|
||||
ConflictRange closedFormOffsetOneRev(const WideRead& r, const Result& res) {
|
||||
return closedFormOffsetOneWith(firstKey(res), r, res);
|
||||
}
|
||||
ConflictRange closedFormOffsetOne(const RangeRead& r, const Result& res) {
|
||||
ConflictRange closedFormOffsetOne(const WideRead& r, const Result& res) {
|
||||
return closedFormOffsetOneWith(lastKey(res), r, res);
|
||||
}
|
||||
ConflictRange closedFormOffsetOneAny(const RangeRead& r, const Result& res) {
|
||||
ConflictRange closedFormOffsetOneAny(const WideRead& r, const Result& res) {
|
||||
return r.reverse ? closedFormOffsetOneRev(r, res) : closedFormOffsetOne(r, res);
|
||||
}
|
||||
|
||||
ConflictRange closedFormOffsetOneUniversal(const RangeRead& r, const Result& res) {
|
||||
ConflictRange closedFormOffsetOneUniversal(const WideRead& r, const Result& res) {
|
||||
if (res.more)
|
||||
return r.reverse ? closedFormReverseTruncatedZero(r, res)
|
||||
: closedFormOffsetOneTruncated(r, res);
|
||||
return closedFormOffsetOneAny(r, res);
|
||||
}
|
||||
|
||||
ConflictRange closedFormOffsetOneTotal(const RangeRead& r, const Result& res) {
|
||||
ConflictRange closedFormOffsetOneTotal(const WideRead& r, const Result& res) {
|
||||
if (optLt(r.end_.key, r.begin.key)) {
|
||||
if (res.more)
|
||||
return r.reverse ? closedFormReverseTruncatedZero(r, res)
|
||||
@@ -483,7 +486,7 @@ ConflictRange closedFormOffsetOneTotal(const RangeRead& r, const Result& res) {
|
||||
return closedFormOffsetOneUniversal(r, res);
|
||||
}
|
||||
|
||||
ConflictRange closedFormZeroCrossEndOneLong(const RangeRead& r, const Result& res) {
|
||||
ConflictRange closedFormZeroCrossEndOneLong(const WideRead& r, const Result& res) {
|
||||
return mkRange(leastReportedOpt(r, res), r.begin.key);
|
||||
}
|
||||
|
||||
@@ -491,31 +494,31 @@ ConflictRange closedFormZeroCrossEndOneLong(const RangeRead& r, const Result& re
|
||||
// Zero-crossed cases
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
ConflictRange closedFormZeroCrossEnd(const RangeRead& r, const Result& res) {
|
||||
ConflictRange closedFormZeroCrossEnd(const WideRead& r, const Result& res) {
|
||||
return mkRange(leastReportedOpt(r, res), r.end_.key);
|
||||
}
|
||||
|
||||
ConflictRange completeForwardCrossedGeneral(const RangeRead& r, const Result& res) {
|
||||
ConflictRange completeForwardCrossedGeneral(const WideRead& r, const Result& res) {
|
||||
if (r.end_.offset <= 1) return mkRange(r.begin.key, r.end_.key);
|
||||
if (residualT(r, res) + residualB(r) == residualE(r))
|
||||
return mkRange(r.end_.key, afterLast(res));
|
||||
return mkRange(crossedBegin(r, res), topKey());
|
||||
}
|
||||
|
||||
RangeRead zeroBeginAnchor(const RangeRead& r, const Result& res) {
|
||||
RangeRead out = r;
|
||||
WideRead zeroBeginAnchor(const WideRead& r, const Result& res) {
|
||||
WideRead out = r;
|
||||
out.begin.key = first(res);
|
||||
out.begin.offset = 1;
|
||||
return out;
|
||||
}
|
||||
|
||||
ConflictRange zeroCrossLongAnchor(const RangeRead& r, const Result& res) {
|
||||
ConflictRange zeroCrossLongAnchor(const WideRead& r, const Result& res) {
|
||||
if (optLt(r.end_.key, first(res)))
|
||||
return completeForwardCrossedGeneral(zeroBeginAnchor(r, res), res);
|
||||
return closedFormOffsetOne(zeroBeginAnchor(r, res), res);
|
||||
}
|
||||
|
||||
ConflictRange closedFormZeroCrossLong(const RangeRead& r, const Result& res) {
|
||||
ConflictRange closedFormZeroCrossLong(const WideRead& r, const Result& res) {
|
||||
if (isTop(r.begin.key)) {
|
||||
OptKeyView H = first(res);
|
||||
std::uint64_t e = residualE(r);
|
||||
@@ -529,11 +532,11 @@ ConflictRange closedFormZeroCrossLong(const RangeRead& r, const Result& res) {
|
||||
return mkRange(a.begin, optMax(a.end_, r.begin.key));
|
||||
}
|
||||
|
||||
ConflictRange closedFormZeroCrossUnderEnd(const RangeRead& r, const Result& res) {
|
||||
ConflictRange closedFormZeroCrossUnderEnd(const WideRead& r, const Result& res) {
|
||||
return closedFormOffsetOneTotal(zeroBeginAnchor(r, res), res);
|
||||
}
|
||||
|
||||
ConflictRange closedFormZeroCrossComplete(const RangeRead& r, const Result& res) {
|
||||
ConflictRange closedFormZeroCrossComplete(const WideRead& r, const Result& res) {
|
||||
if (r.end_.offset <= 0) return closedFormZeroCrossEnd(r, res);
|
||||
if (r.end_.offset == 1) {
|
||||
if (optLt(r.end_.key, r.begin.key))
|
||||
@@ -545,23 +548,23 @@ ConflictRange closedFormZeroCrossComplete(const RangeRead& r, const Result& res)
|
||||
return closedFormZeroCrossUnderEnd(r, res);
|
||||
}
|
||||
|
||||
ConflictRange closedFormZeroCrossCompleteRev(const RangeRead& r, const Result& res) {
|
||||
ConflictRange closedFormZeroCrossCompleteRev(const WideRead& r, const Result& res) {
|
||||
std::vector<KeyView> rev = reversedKeys(res);
|
||||
return closedFormZeroCrossComplete(forwardRead(r), Result{rev, false});
|
||||
}
|
||||
|
||||
bool zeroCrossUnderEnd(const RangeRead& r, const Result& res) {
|
||||
bool zeroCrossUnderEnd(const WideRead& r, const Result& res) {
|
||||
return r.begin.offset == 0 && !res.more && !r.reverse &&
|
||||
r.end_.offset >= 2 && optLt(leastReportedOpt(r, res), r.begin.key) &&
|
||||
optLe(r.begin.key, r.end_.key);
|
||||
}
|
||||
|
||||
ConflictRange closedFormZeroCrossResidual(const RangeRead& r, const Result& res) {
|
||||
ConflictRange closedFormZeroCrossResidual(const WideRead& r, const Result& res) {
|
||||
if (zeroCrossUnderEnd(r, res)) return closedFormZeroCrossUnderEnd(r, res);
|
||||
return closedFormZeroCrossLong(r, res);
|
||||
}
|
||||
|
||||
ConflictRange closedFormZeroCrossTruncRevCrossed(const RangeRead& r, const Result& res) {
|
||||
ConflictRange closedFormZeroCrossTruncRevCrossed(const WideRead& r, const Result& res) {
|
||||
Result complete = res;
|
||||
complete.more = false;
|
||||
return closedFormZeroCrossCompleteRev(r, complete);
|
||||
@@ -571,7 +574,7 @@ ConflictRange closedFormZeroCrossTruncRevCrossed(const RangeRead& r, const Resul
|
||||
// Empty result
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
bool AlwaysEmpty(const RangeRead& r) {
|
||||
bool AlwaysEmpty(const WideRead& r) {
|
||||
NatInf gap = intervalCard(r.begin.key, r.end_.key);
|
||||
std::uint64_t b = noPinB(gap, r.begin.offset, r.end_.offset);
|
||||
std::uint64_t e = cELo(r.begin.offset, r.end_.offset);
|
||||
@@ -601,18 +604,18 @@ bool AlwaysEmpty(const RangeRead& r) {
|
||||
return !inner;
|
||||
}
|
||||
|
||||
ConflictRange emptyRange(const RangeRead& r) {
|
||||
ConflictRange emptyRange(const WideRead& r) {
|
||||
return mkRange(r.begin.key, r.begin.key);
|
||||
}
|
||||
|
||||
ConflictRange crossedRangeGeneral(const RangeRead& r) {
|
||||
ConflictRange crossedRangeGeneral(const WideRead& r) {
|
||||
NatInf diff = niSub(NatInf::of(residualE(r)), NatInf::of(residualB(r)));
|
||||
if (niLe(diff, intervalCard(r.end_.key, r.begin.key)))
|
||||
return mkRange(r.end_.key, topKey());
|
||||
return mkRange(r.begin.key, topKey());
|
||||
}
|
||||
|
||||
ConflictRange emptyValue(const RangeRead& r) {
|
||||
ConflictRange emptyValue(const WideRead& r) {
|
||||
if (AlwaysEmpty(r)) return emptyRange(r);
|
||||
if (r.begin.offset <= 0) {
|
||||
if (r.end_.offset >= 2) return mkRange(bottomKey(), topKey());
|
||||
@@ -630,15 +633,15 @@ ConflictRange emptyValue(const RangeRead& r) {
|
||||
// Complete and forward-truncated results
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
OptKeyView closedFormBegin(const RangeRead& r, const Result& res) {
|
||||
OptKeyView closedFormBegin(const WideRead& r, const Result& res) {
|
||||
return clampsToBottom(r, res) ? bottomKeyView() : r.begin.key;
|
||||
}
|
||||
|
||||
bool closedFormFinite(const RangeRead& r, const Result& res) {
|
||||
bool closedFormFinite(const WideRead& r, const Result& res) {
|
||||
return closedFormDeficit(r) + residualT(r, res) == residualE(r);
|
||||
}
|
||||
|
||||
ConflictRange closedFormCompleteForward(const RangeRead& r, const Result& res) {
|
||||
ConflictRange closedFormCompleteForward(const WideRead& r, const Result& res) {
|
||||
OptKeyView begin = closedFormBegin(r, res);
|
||||
if (r.end_.offset <= 1) return mkRange(begin, r.end_.key);
|
||||
if (clampsToBottom(r, res) || optLe(r.begin.key, r.end_.key)) {
|
||||
@@ -652,18 +655,18 @@ ConflictRange closedFormCompleteForward(const RangeRead& r, const Result& res) {
|
||||
return mkRange(crossedBegin(r, res), topKey());
|
||||
}
|
||||
|
||||
ConflictRange closedFormComplete(const RangeRead& r, const Result& res) {
|
||||
ConflictRange closedFormComplete(const WideRead& r, const Result& res) {
|
||||
if (!r.reverse) return closedFormCompleteForward(forwardRead(r), res);
|
||||
std::vector<KeyView> rev = reversedKeys(res);
|
||||
return closedFormCompleteForward(forwardRead(r), Result{rev, false});
|
||||
}
|
||||
|
||||
NatInf closedFormForwardCapacity(const RangeRead& r, const Result& res) {
|
||||
NatInf closedFormForwardCapacity(const WideRead& r, const Result& res) {
|
||||
if (clampsToBottom(r, res)) return NatInf::of(0);
|
||||
return intervalCard(r.end_.key, r.begin.key);
|
||||
}
|
||||
|
||||
ConflictRange closedFormForwardTruncated(const RangeRead& r, const Result& res) {
|
||||
ConflictRange closedFormForwardTruncated(const WideRead& r, const Result& res) {
|
||||
OptKeyView begin = closedFormBegin(r, res);
|
||||
if (r.end_.offset <= 0) return mkRange(begin, r.end_.key);
|
||||
std::uint64_t room = natSub(natSub(residualE(r), res.keys.size()), residualB(r));
|
||||
@@ -676,22 +679,22 @@ ConflictRange closedFormForwardTruncated(const RangeRead& r, const Result& res)
|
||||
// Reversed truncation
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
std::uint64_t closedFormReverseCount(const RangeRead& r, const Result& res) {
|
||||
std::uint64_t closedFormReverseCount(const WideRead& r, const Result& res) {
|
||||
if (optLt(r.end_.key, r.begin.key))
|
||||
return static_cast<std::uint64_t>(res.keys.size());
|
||||
return residualT(r, res);
|
||||
}
|
||||
|
||||
bool closedFormReverseFinite(const RangeRead& r, const Result& res) {
|
||||
bool closedFormReverseFinite(const WideRead& r, const Result& res) {
|
||||
return closedFormDeficit(r) + closedFormReverseCount(r, res) == residualE(r);
|
||||
}
|
||||
|
||||
bool closedFormCrossedRoom(const RangeRead& r, const Result& res) {
|
||||
bool closedFormCrossedRoom(const WideRead& r, const Result& res) {
|
||||
NatInf lhs = NatInf::of(natSub(residualE(r), res.keys.size()));
|
||||
return niLe(lhs, intervalCard(r.end_.key, last(res)));
|
||||
}
|
||||
|
||||
ConflictRange closedFormReverseTruncatedSkip(const RangeRead& r, const Result& res) {
|
||||
ConflictRange closedFormReverseTruncatedSkip(const WideRead& r, const Result& res) {
|
||||
if (r.end_.offset <= 1) return mkRange(r.begin.key, r.end_.key);
|
||||
if (allBelowEnd(r, res)) return topRange(r);
|
||||
if (closedFormReverseFinite(r, res)) {
|
||||
@@ -704,12 +707,12 @@ ConflictRange closedFormReverseTruncatedSkip(const RangeRead& r, const Result& r
|
||||
return mkRange(begin, topKey());
|
||||
}
|
||||
|
||||
ConflictRange closedFormReverseTruncated(const RangeRead& r, const Result& res) {
|
||||
ConflictRange closedFormReverseTruncated(const WideRead& r, const Result& res) {
|
||||
if (residualB(r) == 0) return closedFormReverseTruncatedZero(r, res);
|
||||
return closedFormReverseTruncatedSkip(r, res);
|
||||
}
|
||||
|
||||
ConflictRange closedFormPositive(const RangeRead& r, const Result& res) {
|
||||
ConflictRange closedFormPositive(const WideRead& r, const Result& res) {
|
||||
if (!res.more) return closedFormComplete(r, res);
|
||||
if (!r.reverse) return closedFormForwardTruncated(r, res);
|
||||
return closedFormReverseTruncated(r, res);
|
||||
@@ -719,25 +722,25 @@ ConflictRange closedFormPositive(const RangeRead& r, const Result& res) {
|
||||
// Crossed table
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
bool overshoot(const RangeRead& r, const Result& res) {
|
||||
bool overshoot(const WideRead& r, const Result& res) {
|
||||
return r.end_.offset >= 2 && allBelowEnd(r, res);
|
||||
}
|
||||
|
||||
bool zeroCrossOvershootComplete(const RangeRead& r, const Result& res) {
|
||||
bool zeroCrossOvershootComplete(const WideRead& r, const Result& res) {
|
||||
return r.begin.offset == 0 && !res.more && !r.reverse && overshoot(r, res) &&
|
||||
optLt(leastReportedOpt(r, res), r.begin.key);
|
||||
}
|
||||
|
||||
bool negCrossOvershoot(const RangeRead& r, const Result& res) {
|
||||
bool negCrossOvershoot(const WideRead& r, const Result& res) {
|
||||
return !r.reverse && r.begin.offset < 0 && !res.more && overshoot(r, res) &&
|
||||
optLt(first(res), r.begin.key);
|
||||
}
|
||||
|
||||
bool crossedCompleteOvershoot(const RangeRead& r, const Result& res) {
|
||||
bool crossedCompleteOvershoot(const WideRead& r, const Result& res) {
|
||||
return zeroCrossOvershootComplete(r, res) || negCrossOvershoot(r, res);
|
||||
}
|
||||
|
||||
bool crossedCompleteEndLeOne(const RangeRead& r, const Result& res) {
|
||||
bool crossedCompleteEndLeOne(const WideRead& r, const Result& res) {
|
||||
if (!(!r.reverse && !res.more && r.begin.offset <= 0 &&
|
||||
optLt(first(res), r.begin.key)))
|
||||
return false;
|
||||
@@ -748,30 +751,30 @@ bool crossedCompleteEndLeOne(const RangeRead& r, const Result& res) {
|
||||
clampNonposBottom(r, res));
|
||||
}
|
||||
|
||||
bool zeroCrossLongTop(const RangeRead& r, const Result& res) {
|
||||
bool zeroCrossLongTop(const WideRead& r, const Result& res) {
|
||||
return r.begin.offset == 0 && !res.more && !r.reverse &&
|
||||
r.end_.offset >= 2 && isTop(r.begin.key);
|
||||
}
|
||||
|
||||
bool zeroCrossLongFinite(const RangeRead& r, const Result& res) {
|
||||
bool zeroCrossLongFinite(const WideRead& r, const Result& res) {
|
||||
return r.begin.offset == 0 && !res.more && !r.reverse &&
|
||||
r.end_.offset >= 2 && !isTop(r.begin.key) &&
|
||||
optLt(r.end_.key, r.begin.key) &&
|
||||
optLt(leastReportedOpt(r, res), r.begin.key);
|
||||
}
|
||||
|
||||
bool crossedCompleteBeginZero(const RangeRead& r, const Result& res) {
|
||||
bool crossedCompleteBeginZero(const WideRead& r, const Result& res) {
|
||||
return zeroCrossUnderEnd(r, res) || zeroCrossLongTop(r, res) ||
|
||||
zeroCrossLongFinite(r, res);
|
||||
}
|
||||
|
||||
bool crossedCompleteClamp(const RangeRead& r, const Result& res) {
|
||||
bool crossedCompleteClamp(const WideRead& r, const Result& res) {
|
||||
return negCrossClampEndOne(r, res) || negCrossHeadBottom(r, res) ||
|
||||
negCrossClampNonposBottom(r, res) ||
|
||||
negCrossClampLongNonpos(r, res);
|
||||
}
|
||||
|
||||
ConflictRange closedFormCrossedComplete(const RangeRead& r, const Result& res) {
|
||||
ConflictRange closedFormCrossedComplete(const WideRead& r, const Result& res) {
|
||||
if (crossedCompleteOvershoot(r, res))
|
||||
return closedFormNegCrossTopUnified(r, res);
|
||||
if (crossedCompleteEndLeOne(r, res))
|
||||
@@ -783,18 +786,18 @@ ConflictRange closedFormCrossedComplete(const RangeRead& r, const Result& res) {
|
||||
return closedFormNegCrossCompleteHighResidual(r, res);
|
||||
}
|
||||
|
||||
bool zeroCrossTruncRevCrossed(const RangeRead& r, const Result& res) {
|
||||
bool zeroCrossTruncRevCrossed(const WideRead& r, const Result& res) {
|
||||
return r.begin.offset == 0 && r.reverse && res.more &&
|
||||
optLt(leastReportedOpt(r, res), r.begin.key);
|
||||
}
|
||||
|
||||
bool negCrossTruncRevUnder(const RangeRead& r, const Result& res) {
|
||||
bool negCrossTruncRevUnder(const WideRead& r, const Result& res) {
|
||||
return res.more && r.reverse && r.begin.offset < 0 &&
|
||||
optLt(last(res), r.begin.key) && optLe(r.begin.key, r.end_.key) &&
|
||||
r.end_.offset <= 1;
|
||||
}
|
||||
|
||||
bool negCrossTruncRevCrossedLow(const RangeRead& r, const Result& res) {
|
||||
bool negCrossTruncRevCrossedLow(const WideRead& r, const Result& res) {
|
||||
return res.more && r.reverse && r.begin.offset < 0 &&
|
||||
optLt(last(res), r.begin.key) && optLt(r.end_.key, r.begin.key) &&
|
||||
r.end_.offset <= 1 &&
|
||||
@@ -804,24 +807,24 @@ bool negCrossTruncRevCrossedLow(const RangeRead& r, const Result& res) {
|
||||
NatInf::of(beginSlack(r) + 1));
|
||||
}
|
||||
|
||||
bool revTruncLeOneLow(const RangeRead& r, const Result& res) {
|
||||
bool revTruncLeOneLow(const WideRead& r, const Result& res) {
|
||||
return negCrossTruncRevUnder(r, res) ||
|
||||
negCrossTruncRevCrossedLow(r, res);
|
||||
}
|
||||
|
||||
bool negCrossTruncRevBelowEnd(const RangeRead& r, const Result& res) {
|
||||
bool negCrossTruncRevBelowEnd(const WideRead& r, const Result& res) {
|
||||
return res.more && r.reverse && r.begin.offset < 0 &&
|
||||
optLt(last(res), r.begin.key) && overshoot(r, res);
|
||||
}
|
||||
|
||||
bool negCrossTruncRevResidualRoom(const RangeRead& r, const Result& res) {
|
||||
bool negCrossTruncRevResidualRoom(const WideRead& r, const Result& res) {
|
||||
return res.more && r.reverse && r.begin.offset < 0 &&
|
||||
r.end_.offset >= 2 && anyAtOrAboveEnd(r, res) &&
|
||||
niLe(niAdd(intervalCard(r.end_.key, first(res)), NatInf::of(2)),
|
||||
NatInf::of(residualE(r)));
|
||||
}
|
||||
|
||||
bool negCrossTruncRevResidualTopCrossedSmallGap(const RangeRead& r,
|
||||
bool negCrossTruncRevResidualTopCrossedSmallGap(const WideRead& r,
|
||||
const Result& res) {
|
||||
if (!(res.more && r.reverse && r.begin.offset < 0 &&
|
||||
r.end_.offset >= 2 && optLt(r.end_.key, last(res))))
|
||||
@@ -831,52 +834,52 @@ bool negCrossTruncRevResidualTopCrossedSmallGap(const RangeRead& r,
|
||||
return niLt(lhs, NatInf::of(residualE(r)));
|
||||
}
|
||||
|
||||
bool negCrossTruncRevResidualTopCrossed(const RangeRead& r, const Result& res) {
|
||||
bool negCrossTruncRevResidualTopCrossed(const WideRead& r, const Result& res) {
|
||||
return res.more && r.reverse && r.begin.offset < 0 &&
|
||||
r.end_.offset >= 2 && optLe(last(res), r.end_.key) &&
|
||||
residualT(r, res) < residualE(r);
|
||||
}
|
||||
|
||||
bool revTruncGeTwoTop(const RangeRead& r, const Result& res) {
|
||||
bool revTruncGeTwoTop(const WideRead& r, const Result& res) {
|
||||
return negCrossTruncRevBelowEnd(r, res) ||
|
||||
negCrossTruncRevResidualRoom(r, res) ||
|
||||
negCrossTruncRevResidualTopCrossedSmallGap(r, res) ||
|
||||
negCrossTruncRevResidualTopCrossed(r, res);
|
||||
}
|
||||
|
||||
bool negCrossTruncRevResidualTopLargeCrossed(const RangeRead& r,
|
||||
bool negCrossTruncRevResidualTopLargeCrossed(const WideRead& r,
|
||||
const Result& res) {
|
||||
return negCrossTruncRevResidualTopLarge(r, res) &&
|
||||
optLt(last(res), r.begin.key) && !isTop(r.begin.key);
|
||||
}
|
||||
|
||||
bool revTruncGeTwoLarge(const RangeRead& r, const Result& res) {
|
||||
bool revTruncGeTwoLarge(const WideRead& r, const Result& res) {
|
||||
return negCrossTruncRevResidualTopLargeCrossed(r, res) ||
|
||||
negCrossTruncRevResidualTopLargeSentinel(r, res);
|
||||
}
|
||||
|
||||
bool negCrossTruncTopTop(const RangeRead& r, const Result& res) {
|
||||
bool negCrossTruncTopTop(const WideRead& r, const Result& res) {
|
||||
return res.more && !r.reverse && r.begin.offset < 0 &&
|
||||
isTop(r.begin.key) && isTop(r.end_.key) && r.end_.offset <= 0;
|
||||
}
|
||||
|
||||
bool negCrossTruncTopTopEq(const RangeRead& r, const Result& res) {
|
||||
bool negCrossTruncTopTopEq(const WideRead& r, const Result& res) {
|
||||
if (!negCrossTruncTopTop(r, res)) return false;
|
||||
std::uint64_t extra = toNat(1 - r.end_.offset);
|
||||
return res.keys.size() + extra == beginSlack(r) + 1;
|
||||
}
|
||||
|
||||
std::uint64_t closedFormClampSlack(const RangeRead& r, const Result& res) {
|
||||
std::uint64_t closedFormClampSlack(const WideRead& r, const Result& res) {
|
||||
return natSub(beginSlack(r) + 1, res.keys.size()) +
|
||||
(isBottom(first(res)) ? 1 : 0);
|
||||
}
|
||||
|
||||
bool closedFormClampGapSmall(const RangeRead& r, const Result& res) {
|
||||
bool closedFormClampGapSmall(const WideRead& r, const Result& res) {
|
||||
return niLe(intervalCard(asKey(lastKey(res)), r.begin.key),
|
||||
NatInf::of(closedFormClampSlack(r, res)));
|
||||
}
|
||||
|
||||
bool closedFormClampGapExactSaturated(const RangeRead& r, const Result& res) {
|
||||
bool closedFormClampGapExactSaturated(const WideRead& r, const Result& res) {
|
||||
if (!(first(res) != bottomKeyView() && isBottom(r.end_.key) &&
|
||||
residualE(r) == res.keys.size()))
|
||||
return false;
|
||||
@@ -884,24 +887,24 @@ bool closedFormClampGapExactSaturated(const RangeRead& r, const Result& res) {
|
||||
NatInf::of(natSub(beginSlack(r) + 1, res.keys.size())));
|
||||
}
|
||||
|
||||
bool closedFormClampGapLow(const RangeRead& r, const Result& res) {
|
||||
bool closedFormClampGapLow(const WideRead& r, const Result& res) {
|
||||
return closedFormClampGapSmall(r, res) ||
|
||||
closedFormClampGapExactSaturated(r, res);
|
||||
}
|
||||
|
||||
OptKey closedFormClampUpper(const RangeRead& r, const Result& res) {
|
||||
OptKey closedFormClampUpper(const WideRead& r, const Result& res) {
|
||||
if (closedFormClampGapLow(r, res)) return afterLast(res);
|
||||
return ownKey(r.begin.key);
|
||||
}
|
||||
|
||||
bool closedFormForwardSaturated(const RangeRead& r, const Result& res) {
|
||||
bool closedFormForwardSaturated(const WideRead& r, const Result& res) {
|
||||
if (!(residualE(r) > 0 && optLt(r.end_.key, first(res)))) return false;
|
||||
NatInf rhs = niAdd(intervalCard(r.end_.key, first(res)),
|
||||
NatInf::of(res.keys.size()));
|
||||
return niLt(NatInf::of(residualE(r)), rhs);
|
||||
}
|
||||
|
||||
ConflictRange closedFormForwardTruncatedNonpos(const RangeRead& r,
|
||||
ConflictRange closedFormForwardTruncatedNonpos(const WideRead& r,
|
||||
const Result& res) {
|
||||
if (reportedBelowBegin(r, res) <= beginSlack(r)) {
|
||||
if (r.end_.offset <= 0) {
|
||||
@@ -927,7 +930,7 @@ ConflictRange closedFormForwardTruncatedNonpos(const RangeRead& r,
|
||||
return mkRange(begin, optMax(r.begin.key, afterLast(res)));
|
||||
}
|
||||
|
||||
ConflictRange closedFormCrossed(const RangeRead& r, const Result& res) {
|
||||
ConflictRange closedFormCrossed(const WideRead& r, const Result& res) {
|
||||
if (!res.more) {
|
||||
if (!r.reverse) return closedFormCrossedComplete(r, res);
|
||||
std::vector<KeyView> rev = reversedKeys(res);
|
||||
@@ -952,11 +955,11 @@ ConflictRange closedFormCrossed(const RangeRead& r, const Result& res) {
|
||||
} // namespace
|
||||
|
||||
ConflictRange closedForm(const RangeRead& r, const Result& res) {
|
||||
assertInputOffsetsFitInt32(r);
|
||||
if (res.keys.empty()) return emptyValue(r);
|
||||
if (r.begin.offset >= 1 || clampedNonpos(r, res))
|
||||
return closedFormPositive(r, res);
|
||||
return closedFormCrossed(r, res);
|
||||
const WideRead wr = cr::detail::widen(r);
|
||||
if (res.keys.empty()) return emptyValue(wr);
|
||||
if (wr.begin.offset >= 1 || clampedNonpos(wr, res))
|
||||
return closedFormPositive(wr, res);
|
||||
return closedFormCrossed(wr, res);
|
||||
}
|
||||
|
||||
} // namespace cr::literal
|
||||
@@ -1,3 +1,5 @@
|
||||
// Released under Apache 2.0 license as described in the file LICENSE.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "model.h"
|
||||
|
||||
+54
-34
@@ -1,3 +1,5 @@
|
||||
// Released under Apache 2.0 license as described in the file LICENSE.
|
||||
|
||||
// Shared data model for the C++ translation of `Statement.closedForm`.
|
||||
//
|
||||
// Keys are byte strings ordered lexicographically. The model separates
|
||||
@@ -16,7 +18,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <limits>
|
||||
@@ -38,16 +39,28 @@ using Key = std::string;
|
||||
using KeyView = std::string_view;
|
||||
using OptKey = std::optional<Key>;
|
||||
using OptKeyView = std::optional<KeyView>;
|
||||
// Lean's unbounded `Int`. Caller-supplied offsets are required to fit in
|
||||
// `std::int32_t` (see `Selector`), so all intermediate arithmetic in `Int`
|
||||
// stays far from overflow; the code below relies on that.
|
||||
// Lean's unbounded `Int`. Public selector offsets are 32-bit; the internal
|
||||
// wide-offset read used by the decision procedure stores derived offsets here.
|
||||
using Int = std::int64_t;
|
||||
|
||||
struct Selector {
|
||||
OptKeyView key;
|
||||
// Offsets supplied by callers must lie in `[INT32_MIN, INT32_MAX]`. They are
|
||||
// stored as `Int` because the closed form also builds derived selectors whose
|
||||
// offsets (for example `end.offset - residualT`) need not fit in 32 bits.
|
||||
// Caller-supplied offsets are `std::int32_t`, matching FoundationDB.
|
||||
std::int32_t offset = 0;
|
||||
};
|
||||
|
||||
struct RangeRead {
|
||||
Selector begin;
|
||||
Selector end_;
|
||||
bool reverse = false;
|
||||
};
|
||||
|
||||
namespace detail {
|
||||
|
||||
// Internal mirror of `RangeRead` whose offsets are wide enough for derived
|
||||
// selectors such as `end.offset - residualT`.
|
||||
struct Selector {
|
||||
OptKeyView key;
|
||||
Int offset = 0;
|
||||
};
|
||||
|
||||
@@ -57,11 +70,17 @@ struct RangeRead {
|
||||
bool reverse = false;
|
||||
};
|
||||
|
||||
// The public offset contract: caller-supplied offsets must fit in
|
||||
// `std::int32_t`. This is checked at the public `closedForm` entry points
|
||||
// (debug builds only) and by the corpus parsers. It is deliberately not
|
||||
// checked in `Selector`, because the decision procedure derives selectors
|
||||
// whose offsets may exceed this range.
|
||||
inline RangeRead widen(const ::cr::RangeRead& r) {
|
||||
return {{r.begin.key, r.begin.offset},
|
||||
{r.end_.key, r.end_.offset},
|
||||
r.reverse};
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
|
||||
// The offset contract shared with the corpus parsers. `Selector::offset` is
|
||||
// already `std::int32_t`, so parsers validate wider parsed values with these
|
||||
// bounds before narrowing.
|
||||
inline constexpr Int kMinInputOffset =
|
||||
std::numeric_limits<std::int32_t>::min();
|
||||
inline constexpr Int kMaxInputOffset =
|
||||
@@ -72,14 +91,6 @@ inline bool offsetsFitInt32(Int begin, Int end) {
|
||||
end >= kMinInputOffset && end <= kMaxInputOffset;
|
||||
}
|
||||
|
||||
inline bool inputOffsetsFitInt32(const RangeRead& r) {
|
||||
return offsetsFitInt32(r.begin.offset, r.end_.offset);
|
||||
}
|
||||
|
||||
inline void assertInputOffsetsFitInt32(const RangeRead& r) {
|
||||
assert(inputOffsetsFitInt32(r));
|
||||
}
|
||||
|
||||
struct Result {
|
||||
// A non-owning view of the reported keys. The caller keeps the underlying
|
||||
// keys alive for the duration of the call.
|
||||
@@ -270,15 +281,18 @@ inline OptKey afterLast(const Result& res) {
|
||||
return asKey(keyAfter(lastKey(res)));
|
||||
}
|
||||
|
||||
inline KeyView leastReported(const RangeRead& r, const Result& res) {
|
||||
template <class R>
|
||||
inline KeyView leastReported(const R& r, const Result& res) {
|
||||
return r.reverse ? lastKey(res) : firstKey(res);
|
||||
}
|
||||
inline OptKeyView leastReportedOpt(const RangeRead& r, const Result& res) {
|
||||
template <class R>
|
||||
inline OptKeyView leastReportedOpt(const R& r, const Result& res) {
|
||||
return asKey(leastReported(r, res));
|
||||
}
|
||||
|
||||
inline RangeRead forwardRead(const RangeRead& r) {
|
||||
RangeRead out = r;
|
||||
template <class R>
|
||||
inline R forwardRead(const R& r) {
|
||||
R out = r;
|
||||
out.reverse = false;
|
||||
return out;
|
||||
}
|
||||
@@ -308,7 +322,8 @@ inline NatInf intervalCard(OptKeyView b, OptKeyView e) {
|
||||
// Result-based counters, in the exact form used by the statement
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
inline std::uint64_t residualT(const RangeRead& r, const Result& res) {
|
||||
template <class R>
|
||||
inline std::uint64_t residualT(const R& r, const Result& res) {
|
||||
if (isTop(r.end_.key)) return 0;
|
||||
std::uint64_t count = 0;
|
||||
for (KeyView k : res.keys)
|
||||
@@ -316,7 +331,8 @@ inline std::uint64_t residualT(const RangeRead& r, const Result& res) {
|
||||
return count;
|
||||
}
|
||||
|
||||
inline std::uint64_t reportedBelowBegin(const RangeRead& r, const Result& res) {
|
||||
template <class R>
|
||||
inline std::uint64_t reportedBelowBegin(const R& r, const Result& res) {
|
||||
if (isTop(r.begin.key)) return static_cast<std::uint64_t>(res.keys.size());
|
||||
std::uint64_t count = 0;
|
||||
for (KeyView k : res.keys)
|
||||
@@ -324,17 +340,21 @@ inline std::uint64_t reportedBelowBegin(const RangeRead& r, const Result& res) {
|
||||
return count;
|
||||
}
|
||||
|
||||
inline std::uint64_t residualB(const RangeRead& r) {
|
||||
return toNat(r.begin.offset - 1);
|
||||
template <class R>
|
||||
inline std::uint64_t residualB(const R& r) {
|
||||
return toNat(static_cast<Int>(r.begin.offset) - 1);
|
||||
}
|
||||
inline std::uint64_t residualE(const RangeRead& r) {
|
||||
return toNat(r.end_.offset - 1);
|
||||
template <class R>
|
||||
inline std::uint64_t residualE(const R& r) {
|
||||
return toNat(static_cast<Int>(r.end_.offset) - 1);
|
||||
}
|
||||
inline std::uint64_t beginSlack(const RangeRead& r) {
|
||||
return toNat(-r.begin.offset);
|
||||
template <class R>
|
||||
inline std::uint64_t beginSlack(const R& r) {
|
||||
return toNat(-static_cast<Int>(r.begin.offset));
|
||||
}
|
||||
inline std::uint64_t clampNonposD(const RangeRead& r) {
|
||||
return toNat(-r.end_.offset);
|
||||
template <class R>
|
||||
inline std::uint64_t clampNonposD(const R& r) {
|
||||
return toNat(-static_cast<Int>(r.end_.offset));
|
||||
}
|
||||
|
||||
} // namespace cr
|
||||
+4
-2
@@ -1,3 +1,5 @@
|
||||
// Released under Apache 2.0 license as described in the file LICENSE.
|
||||
|
||||
// nanobench comparison of the literal translation and the summarised
|
||||
// implementation on representative readable instances.
|
||||
#define ANKERL_NANOBENCH_IMPLEMENT
|
||||
@@ -47,8 +49,8 @@ std::vector<Key> makeDb(std::size_t n, std::size_t len) {
|
||||
return db;
|
||||
}
|
||||
|
||||
Case makeCase(const std::vector<Key>& db, OptKey beginKey, Int beginOff,
|
||||
OptKey endKey, Int endOff, bool reverse, bool truncate,
|
||||
Case makeCase(const std::vector<Key>& db, OptKey beginKey, std::int32_t beginOff,
|
||||
OptKey endKey, std::int32_t endOff, bool reverse, bool truncate,
|
||||
const char* name) {
|
||||
gen::Instance inst;
|
||||
inst.beginKey = std::move(beginKey);
|
||||
|
||||
Binary file not shown.
@@ -1,3 +1,5 @@
|
||||
// Released under Apache 2.0 license as described in the file LICENSE.
|
||||
|
||||
// Compare the literal translation with the summarised implementation on the
|
||||
// Lean-generated corpus (readable or not).
|
||||
#include <cstdint>
|
||||
@@ -102,8 +104,8 @@ int main(int argc, char** argv) {
|
||||
std::vector<KeyView> keyViews;
|
||||
keyViews.reserve(keys.size());
|
||||
for (const Key& k : keys) keyViews.push_back(k);
|
||||
RangeRead r{Selector{optView(beginKey), static_cast<Int>(bo)},
|
||||
Selector{optView(endKey), static_cast<Int>(eo)}, reverse};
|
||||
RangeRead r{Selector{optView(beginKey), static_cast<std::int32_t>(bo)},
|
||||
Selector{optView(endKey), static_cast<std::int32_t>(eo)}, reverse};
|
||||
Result res{keyViews, more != 0};
|
||||
++cases;
|
||||
ConflictRange a = literal::closedForm(r, res);
|
||||
|
||||
+23
-20
@@ -1,3 +1,5 @@
|
||||
// Released under Apache 2.0 license as described in the file LICENSE.
|
||||
|
||||
// Regression test for offsets at the extremes of the input domain. Offsets
|
||||
// supplied by callers are 32-bit; a positive `end.offset` near `INT32_MAX`
|
||||
// would otherwise materialise about 2^31 zero bytes for Lean's
|
||||
@@ -7,6 +9,7 @@
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <limits>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
@@ -31,33 +34,33 @@ int main() {
|
||||
owned.push_back(OptKey{Key("a")});
|
||||
owned.push_back(OptKey{Key("\xff\xff", 2)});
|
||||
|
||||
// The public offset contract accepts exactly the 32-bit boundaries; the
|
||||
// entry-point assertions and corpus parsers share this predicate.
|
||||
const RangeRead boundary{Selector{std::nullopt, kMinInputOffset},
|
||||
Selector{std::nullopt, kMaxInputOffset}, false};
|
||||
const RangeRead outside{Selector{std::nullopt, kMinInputOffset - 1},
|
||||
Selector{std::nullopt, 0}, false};
|
||||
if (!inputOffsetsFitInt32(boundary) || inputOffsetsFitInt32(outside)) {
|
||||
// `Selector` stores offsets as `std::int32_t`, so `RangeRead` itself cannot
|
||||
// violate the input contract. The corpus parsers still validate wider
|
||||
// parsed values with `offsetsFitInt32` before narrowing.
|
||||
static_assert(std::is_same_v<decltype(Selector::offset), std::int32_t>,
|
||||
"Selector::offset must match the 32-bit input contract");
|
||||
if (!offsetsFitInt32(kMinInputOffset, kMaxInputOffset) ||
|
||||
offsetsFitInt32(kMinInputOffset - 1, kMaxInputOffset + 1)) {
|
||||
std::fprintf(stderr, "offset contract check failed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
const Int offs[] = {std::numeric_limits<std::int32_t>::min(),
|
||||
std::numeric_limits<std::int32_t>::min() + 1,
|
||||
-1000000000,
|
||||
-3,
|
||||
-1,
|
||||
0,
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
1000000000,
|
||||
std::numeric_limits<std::int32_t>::max() - 1,
|
||||
std::numeric_limits<std::int32_t>::max()};
|
||||
const std::int32_t offs[] = {std::numeric_limits<std::int32_t>::min(),
|
||||
std::numeric_limits<std::int32_t>::min() + 1,
|
||||
-1000000000,
|
||||
-3,
|
||||
-1,
|
||||
0,
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
1000000000,
|
||||
std::numeric_limits<std::int32_t>::max() - 1,
|
||||
std::numeric_limits<std::int32_t>::max()};
|
||||
std::vector<Selector> sels;
|
||||
sels.reserve(owned.size() * (sizeof(offs) / sizeof(offs[0])));
|
||||
for (const OptKey& k : owned)
|
||||
for (Int off : offs) sels.push_back(Selector{optView(k), off});
|
||||
for (std::int32_t off : offs) sels.push_back(Selector{optView(k), off});
|
||||
|
||||
long long cases = 0, bad = 0;
|
||||
for (const Selector& bs : sels)
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
// Released under Apache 2.0 license as described in the file LICENSE.
|
||||
|
||||
// libFuzzer safety harness: arbitrary (not necessarily readable) instances.
|
||||
// Equivalence is only promised on readable inputs, so this harness merely runs
|
||||
// both implementations under ASan/UBSan to catch memory-safety or UB issues in
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
// Released under Apache 2.0 license as described in the file LICENSE.
|
||||
|
||||
// libFuzzer differential harness: the literal translation versus the
|
||||
// summarised implementation, on readable-by-construction instances.
|
||||
#include <cstddef>
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
// Released under Apache 2.0 license as described in the file LICENSE.
|
||||
|
||||
// Validates the C++ literal translation against golden vectors produced by
|
||||
// `cpp/oracle/gen.lean` (which calls the real `Statement.closedForm`).
|
||||
#include <cstdint>
|
||||
@@ -107,8 +109,8 @@ int main(int argc, char** argv) {
|
||||
std::vector<KeyView> keyViews;
|
||||
keyViews.reserve(keys.size());
|
||||
for (const Key& k : keys) keyViews.push_back(k);
|
||||
RangeRead r{Selector{optView(beginKey), static_cast<Int>(bo)},
|
||||
Selector{optView(endKey), static_cast<Int>(eo)}, rev != 0};
|
||||
RangeRead r{Selector{optView(beginKey), static_cast<std::int32_t>(bo)},
|
||||
Selector{optView(endKey), static_cast<std::int32_t>(eo)}, rev != 0};
|
||||
Result res{keyViews, more != 0};
|
||||
OptKey wantBegin, wantEnd;
|
||||
if (!parseKey(ob, wantBegin) || !parseKey(oe, wantEnd)) {
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
// Released under Apache 2.0 license as described in the file LICENSE.
|
||||
|
||||
// Deterministic, readable-by-construction instance generation shared by the
|
||||
// differential fuzzer and the benchmarks.
|
||||
//
|
||||
@@ -59,15 +61,15 @@ inline OptKey randomOptKey(Bytes& b, std::size_t maxLen) {
|
||||
return OptKey{randomKey(b, maxLen)};
|
||||
}
|
||||
|
||||
inline Int randomOffset(Bytes& b) {
|
||||
inline std::int32_t randomOffset(Bytes& b) {
|
||||
// Bias towards the offsets that decide the interesting boundary cases, but
|
||||
// still allow the full [-256, 255] window.
|
||||
std::uint8_t pick = b.u8();
|
||||
if (pick < 192) {
|
||||
static const Int small[] = {-3, -2, -1, 0, 1, 2, 3, 4, 5};
|
||||
static const std::int32_t small[] = {-3, -2, -1, 0, 1, 2, 3, 4, 5};
|
||||
return small[pick % 9];
|
||||
}
|
||||
return static_cast<Int>(b.u16() % 512) - 256;
|
||||
return static_cast<std::int32_t>(b.u16() % 512) - 256;
|
||||
}
|
||||
|
||||
inline OptKey resolve(const std::vector<Key>& db, const Selector& s) {
|
||||
@@ -75,7 +77,7 @@ inline OptKey resolve(const std::vector<Key>& db, const Selector& s) {
|
||||
if (!s.key) {
|
||||
anchor = static_cast<Int>(db.size()) + s.offset - 1;
|
||||
} else if (s.key->empty()) {
|
||||
anchor = s.offset - 1;
|
||||
anchor = static_cast<Int>(s.offset) - 1;
|
||||
} else {
|
||||
std::size_t below = 0;
|
||||
for (const Key& k : db)
|
||||
@@ -108,8 +110,8 @@ struct Instance {
|
||||
// Owned selector keys; `read()` borrows them.
|
||||
OptKey beginKey;
|
||||
OptKey endKey;
|
||||
Int beginOffset = 0;
|
||||
Int endOffset = 0;
|
||||
std::int32_t beginOffset = 0;
|
||||
std::int32_t endOffset = 0;
|
||||
bool reverse = false;
|
||||
bool more = false;
|
||||
|
||||
|
||||
Loaded 100 of 103 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user