Reference · The Applied Higher-Order Functions catalogue
rainbow.algorithms.proofs.ahof.gas-station
AHoF Gas Station (greedy circular tour): around a circle of stations, station i provides gas[i] and it costs cost[i] to drive to the next. Determine whether a starting station exists from which you can complete the loop (tank never negative), and find one, in a single O(n) pass. The elegant fact: a valid start EXISTS exactly when total gas covers total cost; and the greedy scan (resetting the candidate start whenever the running tank goes negative) finds it. The feasibility test is: can-complete? total-gas total-cost = decide (total-cost <= total-gas) over exact-rational totals. Defined: the feasibility test. Proved native and axiom-free, as worked instances: gas 7 with cost 5 is judged completable, gas 3 with cost 5 is not, and the worked certificate 5 <= 7 (native nnrat/at-most). The classical fact that total gas covering total cost is necessary and sufficient for a valid start, and the correctness of the greedy scan, are not proved here. The running-tank scan that locates the start is executable. Requires nnrat/at-most, audit/decide-nnrat-at-most, nnrat/decide-at-most-true-sound (all in the D63 checkpoint). Scope (measured by the kernel): this module has 3 worked instances (statements about specific values). None of its theorems is a guarantee about the algorithm for all inputs; `lein verify rainbow.algorithms.proofs.ahof.gas-station` prints this classification.
gas-station/can-complete?
gas-station/can-complete? : (x₁ x₂ : ℚ≥0) → data/Bool
ℚ≥0 = nnrat/NonnegativeRational
gas-station/sufficient-gas-completable
gas-station/sufficient-gas-completable : → bo/is-safe 7 5 = data/true
= = equality/Eq
gas-station/completable-cost-covered
gas-station/completable-cost-covered : → 5 ≤ 7
≤ = nnrat/at-most
gas-station/gas-deficit-impossible
gas-station/gas-deficit-impossible : → bo/is-safe 3 5 = data/false
= = equality/Eq