Build provably correct software using formal methods like Hoare Logic, Weakest Preconditions, and Design-by-Contract.
99
Quality
100%
Does it follow best practices?
Impact
99%
1.45xAverage score across 5 eval scenarios
Build software that is correct by design using formal verification.
{P} C {Q} to specify code blocks.wp(C, Q) to derive the weakest precondition from your postcondition.wp("x := E", R) = R[x -> E].wp("S1; S2", R) = wp(S1, wp(S2, R)).Define Postcondition (Q): State the exact logical goal.
Derive Precondition (P): Calculate the minimum required state using wp.
Loop Verification (Total Correctness):
(I AND NOT B) => Q.P => I), Preservation ({I AND B} Body {I}), and Termination (v decreases, I => v >= 0).assert statements within the loop body to verify the Invariant and Variant at each iteration.Annotate: Use language-native assertions (e.g., assert in Python, @requires/@ensures in Eiffel/JML) to document the contract.
def binary_search(array: list[int], target: int) -> int:
"""
Precondition: array is sorted.
Postcondition: returns mid s.t. array[mid] == target, else -1.
"""
# DbC Precondition
assert all(array[i] <= array[i+1] for i in range(len(array)-1))
low, high = 0, len(array) - 1
# Invariant I: target in array iff target in array[low...high]
# Variant v: high - low + 1
while low <= high:
# Runtime Invariant & Variant Verification
v_old = high - low + 1
assert (target not in array) or (target in array[low:high+1])
assert v_old >= 0
mid = (low + high) // 2
if array[mid] == target:
return mid # Postcondition Q reached
elif array[mid] < target:
low = mid + 1
else:
high = mid - 1
# v decreases, I is preserved
v_new = high - low + 1
assert v_new < v_old
return -1 # (I AND low > high) => target not in arrayInstall with Tessl CLI
npx tessl i jpc0/provably-correct-software