One command
Point the CLI at a file, a Go package, a Rust crate or a Swift package. Nothing to install first:
npx fetches gdp-wired-cli, which brings gdp-wired and the TypeScript
compiler. The language is the file’s; the reader it needs is your language’s own toolchain, already on your
machine if you build that code.
$ npx gdp-wired-cli check src/limiter.py
Or a .rb .ex .java .kt .go .rs .swift .c .cpp .ts file, a Go package, a Rust crate,
a Swift package, a directory of files, or several at once.
- What it prints. The rule in your code’s names, how it was checked against your code, every
statement it inferred about other code, each law you stated (proven, broken with the run, unknown, or not read
with why), and laws nobody wrote, each with the comment that states it (
--no-proposeturns those off). - Exit codes. 0 when no stated law is broken, 1 when one is, 2 when one couldn’t be checked
(the rule refused, or the law not read). So it gates a merge:
uses: monotykamary/gdp-wired@mainwithpaths:runs it in CI. - Flags.
--entry X,--methods a,b,--failures kept,--on-error discard,--from any,--clock s|ms,--ms 10000(the prover’s time per law),--cert <dir>(each proven law’s certificate),--json. Each one but the last three can live ingdp.tomlinstead.
Writing a law
A law is one comment over the code’s names: gdp: law <name>: <expression>. On the line
above a method it is a law of that method’s step; above a class, an Effect service’s make effect or an
XState machine, of every step. Where the language has annotations, the law can be one.
| Language | As a comment | As an annotation |
|---|---|---|
| Python | # gdp: law … | @gdp_law("…"), a decorator of that name in your code |
| Ruby, Elixir | # gdp: law … | Elixir: @gdp_law "…", a module attribute |
| Java, Kotlin | // gdp: law … | @GdpLaw("…"), the annotation declared in your code |
| Rust | // gdp: law … or /// gdp: law … | #[doc = "gdp: law …"] |
| Go, Swift, C, C++, TypeScript, Effect, XState | // gdp: law … | none |
The expression: comparisons, + - * by a constant, and or not implies; the state after
the step by its fields (self., this., @, l-> or bare;
after.x), old(x) or before.x before it; the request’s arguments by name;
result and failed; a call of other code answered by the request as f(...);
x is None; maps as the code reads them (counts[user], user in counts,
sum(balances)); forall k in m: ...; a choice by its value (mode == "closed").
Anything else is refused with why, never dropped, and no stated law goes unaccounted: one no rule takes is
reported, exit 2.
Beyond one step: eventually laws; laws about two runs (<secret> never reaches
<fields>, isolated by <argument>, agrees with <Class>); a
duration over the request’s time (now - received < 30 days); and gdp: cite <law name>:
<clause>, which ties a law to the clause it answers, kept in the certificate’s meta and never in what
is checked.
What each language reads
Each language is read by its own parser, lowered into one shared form, run symbolically into a rule, and checked against your code by a harness in your language that runs both on thousands of requests. Nothing is approximated: what the reader can’t say exactly is refused at its line, with why.
| Language | Reader, and what runs the check | Inferred, with nothing written | Needs a tag | Injected stores |
|---|---|---|---|---|
Python.py | Python’s ast; python3 | the class whose public methods write its state; calls of other code left out, or answered over their annotations (read through your modules) | a call whose result declares no type | yes |
TypeScript.ts, .js | the TypeScript compiler | the exported class or function that reads as a rule; an injected function answered over the checker’s type | nothing: a compound answer is refused | no |
Go.go, a package | Go’s own parser; go | the struct its exported methods write; answers over their declared types | nothing: the types are declared | no |
Rust.rs, a crate | syn; cargo | the struct its public methods write; calls left out | each answered call (// gdp: answer u32) | no |
Java.java | javac’s tree API; a JDK 17+ ($JAVA_HOME) | the class; answers over declared types; a field’s @Autowired | a generic result | yes: a proxy of the interface |
Kotlin.kt | Kotlin’s PSI; kotlinc 2.4 ($KOTLIN_HOME) | as Java | a generic result | yes: a proxy of the interface |
Swift.swift, a package | swift-syntax; Swift 5.9+ ($GDP_SWIFT) | the class; answers over declared types; init parameters typed by a protocol | a value with no declared type | yes: a conforming class per protocol |
Ruby.rb | Prism; ruby | the class; field and argument types from how the code uses them | each answered call (# gdp: answer int) | yes |
Elixir.ex | Code.string_to_quoted; elixir | the module, its GenServer or Agent | each answered call | no: the state is a value |
C.c, .h | clang | the struct the file’s functions write; answers over declared types | nothing: the types are declared | no |
C++.cpp | clang++ | each class of the file tried, its constructor the start | nothing: the types are declared | no |
Effecta service, a Schedule in .ts | the TypeScript compiler; your project’s effect (3.x or 4.x) | a make effect: its Refs the state, its methods the requests, a yielded error the step’s refusal, a transaction one atomic step and a wait a guard; a retry or repeat policy one state machine | nothing | the state is its Refs |
XStatea machine in .ts | the TypeScript compiler; your project’s xstate (5.x) | the file’s one machine: its context, value and event | nothing | the state is its context |
An injected store: a dependency your class is given (a cache client, a repository) and calls by a key, held as a map of the rule’s state; the check gives your code a store that keeps what is set. From research/langs/DESIGN.md.
Language by language
The same limiter in each language where it fits: a law that holds, then a one-line bug that breaks it, then the edge
of what is read, with the refusal the CLI prints and, where one exists, the line or gdp.toml entry that
settles it. Effect gets a retry policy and a transaction. Every output below is what
gdp-wired-cli@0.43.0 printed for the file shown.
A law that holds
Two laws over hit, one as a comment and one as a decorator (any decorator named gdp_law that returns the function). old(used) is the value before the step, result what it returned.
limiter.py
def gdp_law(text):
return lambda f: f
class Limiter:
def __init__(self, limit: int):
self.limit, self.used = limit, 0
# gdp: law a counted hit stays within the limit: result implies used <= limit
@gdp_law("a counted hit adds its cost: result implies used == old(used) + cost")
def hit(self, cost: int) -> bool:
if cost <= 0 or self.used + cost > self.limit:
return False
self.used += cost
return True
$ npx gdp-wired-cli check limiter.py
limiter.py: Limiter (Python)
checked against the code: 2154 requests from the start, 400 from drawn states, 2 from the prover's witnesses, 3 of 3 branch arms
the entry inferred: Limiter, the class whose public methods write its state (hit; the others read it)
names (the code's, then the rule's):
limit state limit_
used state used
limit made with limit
hit(cost) request cost
hit() result result hit_result
laws stated in the code:
proven a counted hit stays within the limit (limiter.py:9)
proven a counted hit adds its cost (limiter.py:10)
laws proposed (nothing written; state one with the comment under it):
proven limit never changes
# gdp: law limit never changes: limit == old(limit)
proven used is never negative
# gdp: law used is never negative: used >= 0
proven used never goes down
# gdp: law used never goes down: used >= old(used)
(5 more proposed and broken by a run: not shown)
2 of 2 stated laws proven, 0 broken, 0 unknown, 0 not read
exit 0
A one-line bug
The guard checks the spend before the hit instead of after it. “A counted hit stays within the limit” breaks, with the run that breaks it, as calls of your code.
limiter.py
def hit(self, cost: int) -> bool:
- if cost <= 0 or self.used + cost > self.limit:
+ if cost <= 0 or self.used > self.limit:
return False
$ npx gdp-wired-cli check limiter.py
limiter.py: Limiter (Python)
checked against the code: 2351 requests from the start, 400 from drawn states, 2 from the prover's witnesses, 3 of 3 branch arms
the entry inferred: Limiter, the class whose public methods write its state (hit; the others read it)
names (the code's, then the rule's):
limit state limit_
used state used
limit made with limit
hit(cost) request cost
hit() result result hit_result
laws stated in the code:
broken a counted hit stays within the limit (limiter.py:9)
result implies used <= limit
run: made with limit = 1
hit(cost=-1)
hit(cost=0)
hit(cost=3)
proven a counted hit adds its cost (limiter.py:10)
laws proposed (nothing written; state one with the comment under it):
proven limit never changes
# gdp: law limit never changes: limit == old(limit)
proven used is never negative
# gdp: law used is never negative: used >= 0
proven used never goes down
# gdp: law used never goes down: used >= old(used)
(5 more proposed and broken by a run: not shown)
1 of 2 stated laws proven, 1 broken, 0 unknown, 0 not read
exit 1
The boundary
A call into your own modules is read through them: its value becomes an input of the request, over the callee’s annotation. Here cost_of has none, so the rule is refused at the line that reads it, and the refusal says how to state it.
limiter.py
from pricing import cost_of
class Limiter:
def __init__(self, limit: int):
self.limit, self.used = limit, 0
# gdp: law a counted hit stays within the limit: result implies used <= limit
def hit(self, model: str) -> bool:
cost = cost_of(model)
if cost <= 0 or self.used + cost > self.limit:
return False
self.used += cost
return True
pricing.py
PRICES = {"small": 1, "large": 5}
def cost_of(model):
return PRICES.get(model, 10)
$ npx gdp-wired-cli check limiter.py --no-propose
limiter.py: Limiter (Python)
refused at limiter.py:11:12: cost <= 0: the truth of cost_of(model): the value of cost_of, other code left out; cost_of(model): pricing.cost_of, other code (limiter.py:10:16), whose value is read, and it declares no type: state what it returns (answers: {"cost_of": 'int'}, or a `gdp: answer int` comment on its line)
laws stated in the code:
not read a counted hit stays within the limit (limiter.py:8)
result implies used <= limit
the rule was refused
0 of 1 stated laws proven, 0 broken, 0 unknown, 1 not read
exit 2
One line of gdp.toml states what it returns. The output now says the answer is stated, not inferred:
gdp.toml
[rules.Limiter]
answers = {cost_of = "int"}
$ npx gdp-wired-cli check limiter.py --no-propose
limiter.py: Limiter (Python)
checked against the code: 2154 requests from the start, 400 from drawn states, 2 from the prover's witnesses, 3 of 3 branch arms
cost_of (limiter.py:10:16): answered by the request (stated), its arguments not read
names (the code's, then the rule's):
limit state limit_
used state used
limit made with limit
hit(model) request model
cost_of(...) answered cost_of
hit() result result hit_result
laws stated in the code:
proven a counted hit stays within the limit (limiter.py:8)
1 of 1 stated laws proven, 0 broken, 0 unknown, 0 not read
exit 0
Or annotate the callee, and nothing needs stating: the answer is inferred from -> int, and the output says what it assumes.
pricing.py
-def cost_of(model):
+def cost_of(model: str) -> int:
return PRICES.get(model, 10)
$ npx gdp-wired-cli check limiter.py --no-propose
limiter.py: Limiter (Python)
checked against the code: 2154 requests from the start, 400 from drawn states, 2 from the prover's witnesses, 3 of 3 branch arms
the entry inferred: Limiter, the class whose public methods write its state (hit; the others read it)
cost_of (limiter.py:10:16): answered by the request (inferred: its value is read; assumes cost_of may return any int), its arguments not read
names (the code's, then the rule's):
limit state limit_
used state used
limit made with limit
hit(model) request model
cost_of(...) answered cost_of
hit() result result hit_result
laws stated in the code:
proven a counted hit stays within the limit (limiter.py:8)
1 of 1 stated laws proven, 0 broken, 0 unknown, 0 not read
exit 0
A law that holds
The laws as comments over the exported class; the fields by their bare names.
limiter.ts
export class Limiter {
private used = 0
constructor(private readonly limit: number) {}
// gdp: law a counted hit adds its cost: result implies used == old(used) + cost
// gdp: law a counted hit stays within the limit: result implies used <= limit
hit(cost: number): boolean {
if (cost <= 0 || this.used + cost > this.limit) return false
this.used += cost
return true
}
}
$ npx gdp-wired-cli check limiter.ts
limiter.ts: Limiter (TypeScript)
checked against the code: 2197 requests from the start, 400 from drawn states, 2 from the prover's witnesses, 3 of 3 branch arms
names (the code's, then the rule's):
used state used
limit made with limit
hit(cost) request hit_cost
hit() result result hit_result
method method method
laws stated in the code:
proven a counted hit adds its cost (limiter.ts:5)
proven a counted hit stays within the limit (limiter.ts:6)
laws proposed (nothing written; state one with the comment under it):
proven used is never negative
// gdp: law used is never negative: used >= 0
proven used never goes down
// gdp: law used never goes down: used >= old(used)
(3 more proposed and broken by a run: not shown)
2 of 2 stated laws proven, 0 broken, 0 unknown, 0 not read
exit 0
A one-line bug
The guard checks the spend before the hit instead of after it. “A counted hit stays within the limit” breaks, with the run that breaks it, as calls of your code.
limiter.ts
hit(cost: number): boolean {
- if (cost <= 0 || this.used + cost > this.limit) return false
+ if (cost <= 0 || this.used > this.limit) return false
this.used += cost
$ npx gdp-wired-cli check limiter.ts
limiter.ts: Limiter (TypeScript)
checked against the code: 2382 requests from the start, 400 from drawn states, 2 from the prover's witnesses, 3 of 3 branch arms
names (the code's, then the rule's):
used state used
limit made with limit
hit(cost) request hit_cost
hit() result result hit_result
method method method
laws stated in the code:
proven a counted hit adds its cost (limiter.ts:5)
broken a counted hit stays within the limit (limiter.ts:6)
result implies used <= limit
run: made with limit = 1
hit(cost=0)
hit(cost=-2)
hit(cost=3)
laws proposed (nothing written; state one with the comment under it):
proven used is never negative
// gdp: law used is never negative: used >= 0
proven used never goes down
// gdp: law used never goes down: used >= old(used)
(3 more proposed and broken by a run: not shown)
1 of 2 stated laws proven, 1 broken, 0 unknown, 0 not read
exit 1
The boundary
What runs differently each time is refused at its line: a rule is a function of its state and the request.
limiter.ts
this.used += cost
+ if (Math.random() < 0.01) this.used = 0
return true
$ npx gdp-wired-cli check limiter.ts --no-propose
limiter.ts: Limiter (TypeScript)
refused at limiter.ts:10:9: Math.random (a step the code takes differently each time)
laws stated in the code:
not read a counted hit adds its cost (limiter.ts:5)
result implies used == old(used) + cost
the rule was refused
not read a counted hit stays within the limit (limiter.ts:6)
result implies used <= limit
the rule was refused
0 of 2 stated laws proven, 0 broken, 0 unknown, 2 not read
exit 2
A law that holds
A struct its exported methods write. The laws over the receiver’s fields, l.used.
limiter/limiter.go
package limiter
type Limiter struct{ used, limit int }
// SetLimit sets the limit; a negative one is ignored.
func (l *Limiter) SetLimit(n int) {
if n >= 0 {
l.limit = n
}
}
// gdp: law a counted hit adds its cost: result implies l.used == old(l.used) + cost
// gdp: law a counted hit stays within the limit: result implies l.used <= l.limit
func (l *Limiter) Hit(cost int) bool {
if cost <= 0 || l.used+cost > l.limit {
return false
}
l.used += cost
return true
}
limiter/go.mod
module example.com/limiter
go 1.22
$ npx gdp-wired-cli check limiter
limiter: Limiter (Go)
checked against the code: 2391 requests from the start, 400 from drawn states, 6 from the prover's witnesses, 5 of 5 branch arms
the entry inferred: Limiter, the struct whose exported methods write it
names (the code's, then the rule's):
used state used
limit state limit
SetLimit(n) request SetLimit_n
Hit(cost) request Hit_cost
Hit() result result Hit_result
method method method
laws stated in the code:
proven a counted hit adds its cost (limiter.go:12)
proven a counted hit stays within the limit (limiter.go:13)
laws proposed (nothing written; state one with the comment under it):
proven used is never negative
// gdp: law used is never negative: used >= 0
proven used never goes down
// gdp: law used never goes down: used >= old(used)
proven limit is never negative
// gdp: law limit is never negative: limit >= 0
(7 more proposed and broken by a run: not shown)
2 of 2 stated laws proven, 0 broken, 0 unknown, 0 not read
exit 0
A one-line bug
The guard checks the spend before the hit instead of after it. “A counted hit stays within the limit” breaks, with the run that breaks it, as calls of your code.
limiter/limiter.go
func (l *Limiter) Hit(cost int) bool {
- if cost <= 0 || l.used+cost > l.limit {
+ if cost <= 0 || l.used > l.limit {
return false
$ npx gdp-wired-cli check limiter
limiter: Limiter (Go)
checked against the code: 2367 requests from the start, 400 from drawn states, 6 from the prover's witnesses, 5 of 5 branch arms
the entry inferred: Limiter, the struct whose exported methods write it
names (the code's, then the rule's):
used state used
limit state limit
SetLimit(n) request SetLimit_n
Hit(cost) request Hit_cost
Hit() result result Hit_result
method method method
laws stated in the code:
proven a counted hit adds its cost (limiter.go:12)
broken a counted hit stays within the limit (limiter.go:13)
result implies l.used <= l.limit
run: Hit(cost=0)
Hit(cost=-3)
SetLimit(n=0)
Hit(cost=-3)
SetLimit(n=-2)
Hit(cost=2)
laws proposed (nothing written; state one with the comment under it):
proven used is never negative
// gdp: law used is never negative: used >= 0
proven used never goes down
// gdp: law used never goes down: used >= old(used)
proven limit is never negative
// gdp: law limit is never negative: limit >= 0
(7 more proposed and broken by a run: not shown)
1 of 2 stated laws proven, 1 broken, 0 unknown, 0 not read
exit 1
The boundary
A goroutine is refused: its steps interleave with the rule’s, and a step is one atomic transition.
limiter/limiter.go
l.used += cost
+ go l.report()
return true
}
+
+func (l *Limiter) report() {}
$ npx gdp-wired-cli check limiter --no-propose
limiter: Limiter (Go)
refused at limiter.go:19:2: a goroutine (its steps interleave with others)
laws stated in the code:
not read a counted hit adds its cost (limiter.go:12)
result implies l.used == old(l.used) + cost
the rule was refused
not read a counted hit stays within the limit (limiter.go:13)
result implies l.used <= l.limit
the rule was refused
0 of 2 stated laws proven, 0 broken, 0 unknown, 2 not read
exit 2
A law that holds
A doc comment and a #[doc] attribute, the two forms a Rust law takes.
limiter/src/lib.rs
pub struct Limiter { used: u32, limit: u32 }
impl Limiter {
pub fn new(limit: u32) -> Self { Self { used: 0, limit } }
/// gdp: law a counted hit stays within the limit: result implies self.used <= self.limit
#[doc = "gdp: law a counted hit adds its cost: result implies used == old(used) + cost"]
pub fn hit(&mut self, cost: u32) -> bool {
if cost == 0 || self.used + cost > self.limit { return false; }
self.used += cost;
true
}
}
limiter/Cargo.toml
[package]
name = "limiter"
version = "0.1.0"
edition = "2021"
publish = false
$ npx gdp-wired-cli check limiter
limiter: Limiter (Rust)
checked against the code: 1704 requests from the start, 400 from drawn states, 6 from the prover's witnesses
the entry inferred: Limiter, the class whose public methods write its state (hit; the others read it)
names (the code's, then the rule's):
used state used
limit state limit
limit made with limit0
hit(cost) request cost
hit() result result hit_result
laws stated in the code:
proven a counted hit stays within the limit (lib.rs:6)
proven a counted hit adds its cost (lib.rs:7)
laws proposed (nothing written; state one with the comment under it):
proven used stays at most limit
// gdp: law used stays at most limit: used <= limit
proven used never goes down
// gdp: law used never goes down: used >= old(used)
proven limit never changes
// gdp: law limit never changes: limit == old(limit)
(3 more proposed and broken by a run: not shown)
2 of 2 stated laws proven, 0 broken, 0 unknown, 0 not read
exit 0
A one-line bug
The guard checks the spend before the hit instead of after it. “A counted hit stays within the limit” breaks, with the run that breaks it, as calls of your code.
limiter/src/lib.rs
pub fn hit(&mut self, cost: u32) -> bool {
- if cost == 0 || self.used + cost > self.limit { return false; }
+ if cost == 0 || self.used > self.limit { return false; }
self.used += cost;
$ npx gdp-wired-cli check limiter
limiter: Limiter (Rust)
checked against the code: 1704 requests from the start, 400 from drawn states, 5 from the prover's witnesses
the entry inferred: Limiter, the class whose public methods write its state (hit; the others read it)
names (the code's, then the rule's):
used state used
limit state limit
limit made with limit0
hit(cost) request cost
hit() result result hit_result
laws stated in the code:
broken a counted hit stays within the limit (lib.rs:6)
result implies self.used <= self.limit
run: made with limit = 2
hit(cost=1)
hit(cost=1)
hit(cost=3)
proven a counted hit adds its cost (lib.rs:7)
laws proposed (nothing written; state one with the comment under it):
proven used never goes down
// gdp: law used never goes down: used >= old(used)
proven limit never changes
// gdp: law limit never changes: limit == old(limit)
(4 more proposed and broken by a run: not shown)
1 of 2 stated laws proven, 1 broken, 0 unknown, 0 not read
exit 1
The boundary
Rust’s reader states no answer type for a call into other code, so an answered call is tagged on its line. Without the tag, the refusal names the call and the line that states it:
limiter/src/lib.rs
mod pricing;
pub struct Limiter { used: u32, limit: u32 }
impl Limiter {
pub fn new(limit: u32) -> Self { Self { used: 0, limit } }
/// gdp: law a counted hit stays within the limit: result implies self.used <= self.limit
pub fn hit(&mut self, model: &str) -> bool {
let cost = pricing::cost(model);
if cost == 0 || self.used + cost > self.limit { return false; }
self.used += cost;
true
}
}
limiter/src/pricing.rs
pub fn cost(model: &str) -> u32 {
if model == "large" { 5 } else { 1 }
}
limiter/Cargo.toml
[package]
name = "limiter"
version = "0.1.0"
edition = "2021"
publish = false
$ npx gdp-wired-cli check limiter --no-propose
limiter: Limiter (Rust)
refused at src/lib.rs:11:12: cost == 0: the truth of pricing :: cost (model): the value of pricing::cost, other code left out; pricing :: cost (model): pricing::cost, other code (src/lib.rs:10:20), whose value is read, and it declares no type: state what it returns (answers: {"pricing::cost": 'int'}, or a `gdp: answer int` comment on its line)
laws stated in the code:
not read a counted hit stays within the limit (lib.rs:8)
result implies self.used <= self.limit
the rule was refused
0 of 1 stated laws proven, 0 broken, 0 unknown, 1 not read
exit 2
With the tag (and the binding’s type written, let cost: u32, which the check’s harness needs to build):
limiter/src/lib.rs
pub fn hit(&mut self, model: &str) -> bool {
- let cost = pricing::cost(model);
+ let cost: u32 = pricing::cost(model); // gdp: answer u32
if cost == 0 || self.used + cost > self.limit { return false; }
$ npx gdp-wired-cli check limiter --no-propose
limiter: Limiter (Rust)
checked against the code: 599 requests from the start, 400 from drawn states, 6 from the prover's witnesses
the entry inferred: Limiter, the class whose public methods write its state (hit; the others read it)
pricing::cost (src/lib.rs:10:25): answered by the request (tagged: its value is read; assumes pricing::cost may return any u32), its arguments not read
names (the code's, then the rule's):
used state used
limit state limit
limit made with limit0
hit(model) request model
pricing::cost(...) answered cost
hit() result result hit_result
laws stated in the code:
proven a counted hit stays within the limit (lib.rs:8)
1 of 1 stated laws proven, 0 broken, 0 unknown, 0 not read
exit 0
A law that holds
One law as a comment and one as an annotation (@GdpLaw, declared in your code). The guard adds in long, because Java’s int wraps.
demo/Limiter.java
package demo;
@interface GdpLaw { String value(); }
public class Limiter {
private final int limit;
private int used;
public Limiter(int limit) { this.limit = limit; }
// gdp: law a counted hit stays within the limit: result implies used <= limit
@GdpLaw("a counted hit adds its cost: result implies used == old(used) + cost")
public boolean hit(int cost) {
if (cost <= 0 || (long) used + cost > limit) return false;
used += cost;
return true;
}
}
$ npx gdp-wired-cli check demo/Limiter.java
demo/Limiter.java: Limiter (Java)
checked against the code: 1610 requests from the start, 400 from drawn states, 6 from the prover's witnesses
the entry inferred: Limiter, the class whose public methods write its state (hit; the others read it)
names (the code's, then the rule's):
limit state limit
used state used
limit made with limit0
hit(cost) request cost
hit() result result hit_result
laws stated in the code:
proven a counted hit stays within the limit (Limiter.java:11)
proven a counted hit adds its cost (Limiter.java:12)
laws proposed (nothing written; state one with the comment under it):
proven limit never changes
// gdp: law limit never changes: limit == old(limit)
proven used is never negative
// gdp: law used is never negative: used >= 0
proven used never goes down
// gdp: law used never goes down: used >= old(used)
(5 more proposed and broken by a run: not shown)
2 of 2 stated laws proven, 0 broken, 0 unknown, 0 not read
exit 0
The bug Java has and Python doesn’t
The same guard in int, as Python writes it. Python proves it; in Java, used + cost wraps past 231, so a huge cost passes the guard and used wraps with it: the hit counts, but the spend is no longer what it was plus the cost. The run is long, but every step is a real call of the class.
demo/Limiter.java
public boolean hit(int cost) {
- if (cost <= 0 || (long) used + cost > limit) return false;
+ if (cost <= 0 || used + cost > limit) return false;
used += cost;
$ npx gdp-wired-cli check demo/Limiter.java
demo/Limiter.java: Limiter (Java)
checked against the code: 1610 requests from the start, 400 from drawn states, 6 from the prover's witnesses
the entry inferred: Limiter, the class whose public methods write its state (hit; the others read it)
names (the code's, then the rule's):
limit state limit
used state used
limit made with limit0
hit(cost) request cost
hit() result result hit_result
laws stated in the code:
proven a counted hit stays within the limit (Limiter.java:11)
broken a counted hit adds its cost (Limiter.java:12)
result implies used == old(used) + cost
run: made with limit = 1
hit(cost=0)
hit(cost=2147483647)
hit(cost=2147483646)
hit(cost=2147483647)
hit(cost=2147483647)
hit(cost=2147483647)
hit(cost=2147483647)
hit(cost=2147483647)
hit(cost=0)
hit(cost=0)
hit(cost=0)
hit(cost=2147483647)
hit(cost=-1)
hit(cost=0)
hit(cost=1)
hit(cost=2147483647)
laws proposed (nothing written; state one with the comment under it):
proven limit never changes
// gdp: law limit never changes: limit == old(limit)
(7 more proposed and broken by a run: not shown)
1 of 2 stated laws proven, 1 broken, 0 unknown, 0 not read
exit 1
The boundary
A constructor argument typed by an interface is an injected dependency: the check gives your class one that answers its calls, and each answer is an input of the request over the declared return type.
demo/Limiter.java
package demo;
interface Pricing { int cost(String model); }
public class Limiter {
private final int limit;
private final Pricing pricing;
private int used;
public Limiter(int limit, Pricing pricing) { this.limit = limit; this.pricing = pricing; }
// gdp: law a counted hit stays within the limit: result implies used <= limit
public boolean hit(String model) {
int cost = pricing.cost(model);
if (cost <= 0 || used + cost > limit) return false;
used += cost;
return true;
}
}
$ npx gdp-wired-cli check demo/Limiter.java --no-propose
demo/Limiter.java: Limiter (Java)
checked against the code: 666 requests from the start, 400 from drawn states, 6 from the prover's witnesses
the entry inferred: Limiter, the class whose public methods write its state (hit; the others read it)
Limiter(pricing): an injected dependency (inferred: of Pricing, its methods called), the check giving one that answers its calls
pricing.cost (Limiter.java:14:16): answered by the request (inferred: its value is read; assumes pricing.cost may return any int), its arguments not read
names (the code's, then the rule's):
limit state limit
used state used
limit made with limit0
hit(model) request model
cost(...) answered cost
hit() result result hit_result
laws stated in the code:
proven a counted hit stays within the limit (Limiter.java:12)
1 of 1 stated laws proven, 0 broken, 0 unknown, 0 not read
exit 0
A generic result has no type to answer over, and a loop in a step is outside the subset. Each is refused at its line:
demo/Limiter.java
-interface Pricing { int cost(String model); }
+interface Pricing { <T> T cost(String model); }
public boolean hit(String model) {
- int cost = pricing.cost(model);
+ int cost = pricing.<Integer>cost(model);
if (cost <= 0 || used + cost > limit) return false;
$ npx gdp-wired-cli check demo/Limiter.java --no-propose
demo/Limiter.java: Limiter (Java)
refused at Limiter.java:15:9: cost <= 0: the truth of pricing.<Integer>cost(model): the value of pricing.cost, other code left out; pricing.<Integer>cost(model): cost, a method of the store not stated as its get, set or delete (Limiter.java:14:16), whose value is read, and it returns a typevar:T (a type of other code): state what it returns (answers: {"cost": 'int'}, or a `gdp: answer int` comment on its line)
laws stated in the code:
not read a counted hit stays within the limit (Limiter.java:12)
result implies used <= limit
the rule was refused
0 of 1 stated laws proven, 0 broken, 0 unknown, 1 not read
exit 2
demo/Limiter.java
if (cost <= 0 || used + cost > limit) return false;
- used += cost;
+ for (int i = 0; i < cost; i++) used++;
return true;
$ npx gdp-wired-cli check demo/Limiter.java --no-propose
demo/Limiter.java: Limiter (Java)
refused at Limiter.java:15:5: a loop
laws stated in the code:
not read a counted hit stays within the limit (Limiter.java:11)
result implies used <= limit
the rule was refused
not read a counted hit adds its cost (Limiter.java:12)
result implies used == old(used) + cost
the rule was refused
0 of 2 stated laws proven, 0 broken, 0 unknown, 2 not read
exit 2
A law that holds
As in Java: a comment and an annotation, the guard in Long.
Limiter.kt
package demo
annotation class GdpLaw(val value: String)
class Limiter(private val limit: Int) {
private var used = 0
// gdp: law a counted hit stays within the limit: result implies used <= limit
@GdpLaw("a counted hit adds its cost: result implies used == old(used) + cost")
fun hit(cost: Int): Boolean {
if (cost <= 0 || used.toLong() + cost > limit) return false
used += cost
return true
}
}
$ npx gdp-wired-cli check Limiter.kt
Limiter.kt: Limiter (Kotlin)
checked against the code: 1610 requests from the start, 400 from drawn states, 6 from the prover's witnesses
the entry inferred: Limiter, the class whose public methods write its state (hit; the others read it)
names (the code's, then the rule's):
limit state limit
used state used
limit made with limit0
hit(cost) request cost
hit() result result hit_result
laws stated in the code:
proven a counted hit stays within the limit (Limiter.kt:8)
proven a counted hit adds its cost (Limiter.kt:9)
laws proposed (nothing written; state one with the comment under it):
proven limit never changes
// gdp: law limit never changes: limit == old(limit)
proven used is never negative
// gdp: law used is never negative: used >= 0
proven used never goes down
// gdp: law used never goes down: used >= old(used)
(5 more proposed and broken by a run: not shown)
2 of 2 stated laws proven, 0 broken, 0 unknown, 0 not read
exit 0
The bug Kotlin has and Python doesn’t
Int wraps past 231, so the guard written as in Python lets a huge cost through, and the spend wraps:
Limiter.kt
fun hit(cost: Int): Boolean {
- if (cost <= 0 || used.toLong() + cost > limit) return false
+ if (cost <= 0 || used + cost > limit) return false
used += cost
$ npx gdp-wired-cli check Limiter.kt
Limiter.kt: Limiter (Kotlin)
checked against the code: 1610 requests from the start, 400 from drawn states, 6 from the prover's witnesses
the entry inferred: Limiter, the class whose public methods write its state (hit; the others read it)
names (the code's, then the rule's):
limit state limit
used state used
limit made with limit0
hit(cost) request cost
hit() result result hit_result
laws stated in the code:
proven a counted hit stays within the limit (Limiter.kt:8)
broken a counted hit adds its cost (Limiter.kt:9)
result implies used == old(used) + cost
run: made with limit = 1
hit(cost=0)
hit(cost=2147483647)
hit(cost=2147483646)
hit(cost=2147483647)
hit(cost=2147483647)
hit(cost=2147483647)
hit(cost=2147483647)
hit(cost=2147483647)
hit(cost=0)
hit(cost=0)
hit(cost=0)
hit(cost=2147483647)
hit(cost=-1)
hit(cost=0)
hit(cost=1)
hit(cost=2147483647)
laws proposed (nothing written; state one with the comment under it):
proven limit never changes
// gdp: law limit never changes: limit == old(limit)
(7 more proposed and broken by a run: not shown)
1 of 2 stated laws proven, 1 broken, 0 unknown, 0 not read
exit 1
The boundary
An interface in the constructor is injected, its calls answered over their declared types:
Limiter.kt
package demo
interface Pricing { fun cost(model: String): Int }
class Limiter(private val limit: Int, private val pricing: Pricing) {
private var used = 0
// gdp: law a counted hit stays within the limit: result implies used <= limit
fun hit(model: String): Boolean {
val cost = pricing.cost(model)
if (cost <= 0 || used + cost > limit) return false
used += cost
return true
}
}
$ npx gdp-wired-cli check Limiter.kt --no-propose
Limiter.kt: Limiter (Kotlin)
checked against the code: 666 requests from the start, 400 from drawn states, 6 from the prover's witnesses
the entry inferred: Limiter, the class whose public methods write its state (hit; the others read it)
Limiter(pricing): an injected dependency (inferred: of Pricing, its methods called), the check giving one that answers its calls
pricing.cost (Limiter.kt:10:20): answered by the request (inferred: its value is read; assumes pricing.cost may return any int), its arguments not read
names (the code's, then the rule's):
limit state limit
used state used
limit made with limit0
hit(model) request model
cost(...) answered cost
hit() result result hit_result
laws stated in the code:
proven a counted hit stays within the limit (Limiter.kt:8)
1 of 1 stated laws proven, 0 broken, 0 unknown, 0 not read
exit 0
A loop in a step is refused at its line:
Limiter.kt
if (cost <= 0 || used + cost > limit) return false
- used += cost
+ for (i in 0 until cost) used += 1
return true
$ npx gdp-wired-cli check Limiter.kt --no-propose
Limiter.kt: Limiter (Kotlin)
refused at Limiter.kt:12:9: a loop over a collection
laws stated in the code:
not read a counted hit stays within the limit (Limiter.kt:8)
result implies used <= limit
the rule was refused
not read a counted hit adds its cost (Limiter.kt:9)
result implies used == old(used) + cost
the rule was refused
0 of 2 stated laws proven, 0 broken, 0 unknown, 2 not read
exit 2
A law that holds
Swift traps on overflow instead of wrapping, so the guard as Python writes it is proven here.
limiter.swift
final class Limiter {
private var used = 0
let limit: Int
init(limit: Int) { self.limit = limit }
// gdp: law a counted hit adds its cost: result implies used == old(used) + cost
// gdp: law a counted hit stays within the limit: result implies used <= limit
func hit(cost: Int) -> Bool {
if cost <= 0 || used + cost > limit { return false }
used += cost
return true
}
}
$ npx gdp-wired-cli check limiter.swift
limiter.swift: Limiter (Swift)
checked against the code: 2195 requests from the start, 400 from drawn states, 2 from the prover's witnesses
the entry inferred: Limiter, the class whose public methods write its state (hit; the others read it)
names (the code's, then the rule's):
used state used
limit state limit
limit made with limit0
hit(cost) request cost
hit() result result hit_result
laws stated in the code:
proven a counted hit adds its cost (limiter.swift:6)
proven a counted hit stays within the limit (limiter.swift:7)
laws proposed (nothing written; state one with the comment under it):
proven used is never negative
// gdp: law used is never negative: used >= 0
proven used never goes down
// gdp: law used never goes down: used >= old(used)
proven limit never changes
// gdp: law limit never changes: limit == old(limit)
(5 more proposed and broken by a run: not shown)
2 of 2 stated laws proven, 0 broken, 0 unknown, 0 not read
exit 0
A one-line bug
The guard checks the spend before the hit instead of after it. “A counted hit stays within the limit” breaks, with the run that breaks it, as calls of your code.
limiter.swift
func hit(cost: Int) -> Bool {
- if cost <= 0 || used + cost > limit { return false }
+ if cost <= 0 || used > limit { return false }
used += cost
$ npx gdp-wired-cli check limiter.swift
limiter.swift: Limiter (Swift)
checked against the code: 2370 requests from the start, 400 from drawn states, 2 from the prover's witnesses
the entry inferred: Limiter, the class whose public methods write its state (hit; the others read it)
names (the code's, then the rule's):
used state used
limit state limit
limit made with limit0
hit(cost) request cost
hit() result result hit_result
laws stated in the code:
proven a counted hit adds its cost (limiter.swift:6)
broken a counted hit stays within the limit (limiter.swift:7)
result implies used <= limit
run: made with limit = 1
hit(cost=-1)
hit(cost=0)
hit(cost=3)
laws proposed (nothing written; state one with the comment under it):
proven used is never negative
// gdp: law used is never negative: used >= 0
proven used never goes down
// gdp: law used never goes down: used >= old(used)
proven limit never changes
// gdp: law limit never changes: limit == old(limit)
(5 more proposed and broken by a run: not shown)
1 of 2 stated laws proven, 1 broken, 0 unknown, 0 not read
exit 1
The boundary
An init parameter typed by a protocol is injected: the check gives a conforming class that answers its calls.
limiter.swift
protocol Pricing { func cost(_ model: String) -> Int }
final class Limiter {
private var used = 0
let limit: Int
let pricing: Pricing
init(limit: Int, pricing: Pricing) { self.limit = limit; self.pricing = pricing }
// gdp: law a counted hit stays within the limit: result implies used <= limit
func hit(model: String) -> Bool {
let cost = pricing.cost(model)
if cost <= 0 || used + cost > limit { return false }
used += cost
return true
}
}
$ npx gdp-wired-cli check limiter.swift --no-propose
limiter.swift: Limiter (Swift)
checked against the code: 2195 requests from the start, 400 from drawn states, 2 from the prover's witnesses
the entry inferred: Limiter, the class whose public methods write its state (hit; the others read it)
Limiter(pricing): an injected dependency (inferred: of Pricing, its methods called), the check giving one that answers its calls
pricing.cost (limiter.swift:11:16): answered by the request (inferred: its value is read; assumes pricing.cost may return any int), its arguments not read
names (the code's, then the rule's):
used state used
limit state limit
limit made with limit0
hit(model) request model
cost(...) answered cost
hit() result result hit_result
laws stated in the code:
proven a counted hit stays within the limit (limiter.swift:9)
1 of 1 stated laws proven, 0 broken, 0 unknown, 0 not read
exit 0
A loop in a step is refused at its line:
limiter.swift
if cost <= 0 || used + cost > limit { return false }
- used += cost
+ for _ in 0..<cost { used += 1 }
return true
$ npx gdp-wired-cli check limiter.swift --no-propose
limiter.swift: Limiter (Swift)
refused at limiter.swift:10:5: a loop
laws stated in the code:
not read a counted hit adds its cost (limiter.swift:6)
result implies used == old(used) + cost
the rule was refused
not read a counted hit stays within the limit (limiter.swift:7)
result implies used <= limit
the rule was refused
0 of 2 stated laws proven, 0 broken, 0 unknown, 2 not read
exit 2
A law that holds
The fields by their own names, @used.
limiter.rb
class Limiter
def initialize(limit)
@limit = limit
@used = 0
end
# gdp: law a counted hit adds its cost: result implies @used == old(@used) + cost
# gdp: law a counted hit stays within the limit: result implies @used <= @limit
def hit(cost)
return false if cost <= 0 || @used + cost > @limit
@used += cost
true
end
end
$ npx gdp-wired-cli check limiter.rb
limiter.rb: Limiter (Ruby)
checked against the code: 2154 requests from the start, 400 from drawn states, 2 from the prover's witnesses, 2 of 2 branch arms
the entry inferred: Limiter, the class whose public methods write its state (hit; the others read it)
names (the code's, then the rule's):
@limit state limit_
@used state used
limit made with limit
hit(cost) request cost
hit() result result hit_result
laws stated in the code:
proven a counted hit adds its cost (limiter.rb:7)
proven a counted hit stays within the limit (limiter.rb:8)
laws proposed (nothing written; state one with the comment under it):
proven @limit never changes
# gdp: law @limit never changes: @limit == old(@limit)
proven @used is never negative
# gdp: law @used is never negative: @used >= 0
proven @used never goes down
# gdp: law @used never goes down: @used >= old(@used)
(5 more proposed and broken by a run: not shown)
2 of 2 stated laws proven, 0 broken, 0 unknown, 0 not read
exit 0
A one-line bug
The guard checks the spend before the hit instead of after it. “A counted hit stays within the limit” breaks, with the run that breaks it, as calls of your code.
limiter.rb
def hit(cost)
- return false if cost <= 0 || @used + cost > @limit
+ return false if cost <= 0 || @used > @limit
@used += cost
$ npx gdp-wired-cli check limiter.rb
limiter.rb: Limiter (Ruby)
checked against the code: 2351 requests from the start, 400 from drawn states, 2 from the prover's witnesses, 2 of 2 branch arms
the entry inferred: Limiter, the class whose public methods write its state (hit; the others read it)
names (the code's, then the rule's):
@limit state limit_
@used state used
limit made with limit
hit(cost) request cost
hit() result result hit_result
laws stated in the code:
proven a counted hit adds its cost (limiter.rb:7)
broken a counted hit stays within the limit (limiter.rb:8)
result implies @used <= @limit
run: made with limit = 1
hit(cost=-1)
hit(cost=0)
hit(cost=3)
laws proposed (nothing written; state one with the comment under it):
proven @limit never changes
# gdp: law @limit never changes: @limit == old(@limit)
proven @used is never negative
# gdp: law @used is never negative: @used >= 0
proven @used never goes down
# gdp: law @used never goes down: @used >= old(@used)
(5 more proposed and broken by a run: not shown)
1 of 2 stated laws proven, 1 broken, 0 unknown, 0 not read
exit 1
The boundary
Ruby declares no return types, so a call whose value the rule reads is answered only once you say what it returns:
limiter.rb
class Limiter
def initialize(limit, pricing)
@limit = limit
@pricing = pricing
@used = 0
end
# gdp: law a counted hit stays within the limit: result implies @used <= @limit
def hit(model)
cost = @pricing.cost(model)
return false if cost <= 0 || @used + cost > @limit
@used += cost
true
end
end
$ npx gdp-wired-cli check limiter.rb --no-propose
limiter.rb: Limiter (Ruby)
refused at limiter.rb:11:21: cost <= 0: the truth of @pricing.cost(model): the value of pricing.cost, other code left out; @pricing.cost(model): cost, a method of the store not stated as its get, set or delete (limiter.rb:10:12), whose value is read, and it declares no type: state what it returns (answers: {"cost": 'int'}, or a `gdp: answer int` comment on its line)
laws stated in the code:
not read a counted hit stays within the limit (limiter.rb:8)
result implies @used <= @limit
the rule was refused
0 of 1 stated laws proven, 0 broken, 0 unknown, 1 not read
exit 2
One comment on the call’s line. The injected pricing is inferred from the calls made on it:
limiter.rb
def hit(model)
- cost = @pricing.cost(model)
+ cost = @pricing.cost(model) # gdp: answer int
return false if cost <= 0 || @used + cost > @limit
$ npx gdp-wired-cli check limiter.rb --no-propose
limiter.rb: Limiter (Ruby)
checked against the code: 2154 requests from the start, 400 from drawn states, 2 from the prover's witnesses, 3 of 3 branch arms
the entry inferred: Limiter, the class whose public methods write its state (hit; the others read it)
Limiter(pricing): an injected dependency (inferred: of no stated type, its methods called), the check giving one that answers its calls
pricing.cost (limiter.rb:10:12): answered by the request (tagged: its value is read; assumes pricing.cost may return any int), its arguments not read
names (the code's, then the rule's):
@limit state limit_
@used state used
limit made with limit
cost(...) answered cost
hit() result result hit_result
laws stated in the code:
proven a counted hit stays within the limit (limiter.rb:8)
1 of 1 stated laws proven, 0 broken, 0 unknown, 0 not read
exit 0
A law that holds
A GenServer: its state the map init returns, each handle_call clause a case of the request. One law as a comment, one as a module attribute; result is the reply.
limiter.ex
defmodule Limiter do
use GenServer
@type t :: %{used: non_neg_integer, limit: non_neg_integer}
def init({limit}), do: {:ok, %{used: 0, limit: limit}}
# gdp: law a counted hit stays within the limit: result == :ok implies used <= limit
@gdp_law "a counted hit adds its cost: result == :ok implies used == old(used) + cost"
def handle_call({:hit, cost}, _from, %{used: u, limit: l} = s) when cost <= 0 or u + cost > l,
do: {:reply, :full, s}
def handle_call({:hit, cost}, _from, s), do: {:reply, :ok, %{s | used: s.used + cost}}
end
$ npx gdp-wired-cli check limiter.ex
limiter.ex: Limiter (Elixir)
checked against the code: 2195 requests from the start, 400 from drawn states, 2 from the prover's witnesses, 0 of 4 branch arms
the entry inferred: Limiter, the class whose public methods write its state (hit; the others read it)
names (the code's, then the rule's):
used state used
limit state limit
limit made with limit0
hit(cost) request cost
hit() result result hit_result
laws stated in the code:
proven a counted hit stays within the limit (limiter.ex:7)
proven a counted hit adds its cost (limiter.ex:8)
laws proposed (nothing written; state one with the comment under it):
proven used is never negative
# gdp: law used is never negative: used >= 0
proven used never goes down
# gdp: law used never goes down: used >= old(used)
proven limit never changes
# gdp: law limit never changes: limit == old(limit)
(5 more proposed and broken by a run: not shown)
2 of 2 stated laws proven, 0 broken, 0 unknown, 0 not read
exit 0
A one-line bug
The first clause’s guard checks the spend before the hit:
limiter.ex
@gdp_law "a counted hit adds its cost: result == :ok implies used == old(used) + cost"
- def handle_call({:hit, cost}, _from, %{used: u, limit: l} = s) when cost <= 0 or u + cost > l,
+ def handle_call({:hit, cost}, _from, %{used: u, limit: l} = s) when cost <= 0 or u > l,
do: {:reply, :full, s}
$ npx gdp-wired-cli check limiter.ex
limiter.ex: Limiter (Elixir)
checked against the code: 2370 requests from the start, 400 from drawn states, 2 from the prover's witnesses, 0 of 4 branch arms
the entry inferred: Limiter, the class whose public methods write its state (hit; the others read it)
names (the code's, then the rule's):
used state used
limit state limit
limit made with limit0
hit(cost) request cost
hit() result result hit_result
laws stated in the code:
broken a counted hit stays within the limit (limiter.ex:7)
result == :ok implies used <= limit
run: made with limit = 1
hit(cost=-1)
hit(cost=0)
hit(cost=3)
proven a counted hit adds its cost (limiter.ex:8)
laws proposed (nothing written; state one with the comment under it):
proven used is never negative
# gdp: law used is never negative: used >= 0
proven used never goes down
# gdp: law used never goes down: used >= old(used)
proven limit never changes
# gdp: law limit never changes: limit == old(limit)
(5 more proposed and broken by a run: not shown)
1 of 2 stated laws proven, 1 broken, 0 unknown, 0 not read
exit 1
The boundary
A call into another module whose value the rule reads has no declared return type in Elixir, so the rule is refused at the line that reads it:
limiter.ex
defmodule Limiter do
use GenServer
@type t :: %{used: non_neg_integer, limit: non_neg_integer}
def init({limit}), do: {:ok, %{used: 0, limit: limit}}
# gdp: law a counted hit stays within the limit: result == :ok implies used <= limit
def handle_call({:hit, model}, _from, %{used: u, limit: l} = s) do
cost = Pricing.cost(model)
if cost <= 0 or u + cost > l do
{:reply, :full, s}
else
{:reply, :ok, %{s | used: u + cost}}
end
end
end
$ npx gdp-wired-cli check limiter.ex --no-propose
limiter.ex: Limiter (Elixir)
refused at limiter.ex:10:13: cost <= 0: the truth of Pricing.cost: the value of Pricing.cost, other code left out; Pricing.cost(model): another module's code (limiter.ex:9:19), whose value is read, and it declares no type: state what it returns (answers: {"cost": 'int'}, or a `gdp: answer int` comment on its line)
laws stated in the code:
not read a counted hit stays within the limit (limiter.ex:7)
result == :ok implies used <= limit
the rule was refused
0 of 1 stated laws proven, 0 broken, 0 unknown, 1 not read
exit 2
A logger call is other code whose value nothing reads: left out, and run as it is by the check.
limiter.ex
use GenServer
+ require Logger
@type t :: %{used: non_neg_integer, limit: non_neg_integer}
- def handle_call({:hit, cost}, _from, s), do: {:reply, :ok, %{s | used: s.used + cost}}
+ def handle_call({:hit, cost}, _from, s) do
+ Logger.info("hit #{cost}")
+ {:reply, :ok, %{s | used: s.used + cost}}
+ end
end
$ npx gdp-wired-cli check limiter.ex --no-propose
limiter.ex: Limiter (Elixir)
checked against the code: 2195 requests from the start, 400 from drawn states, 2 from the prover's witnesses, 0 of 4 branch arms
the entry inferred: Limiter, the class whose public methods write its state (hit; the others read it)
Logger.info (limiter.ex:14:11): left out (inferred: nothing the rule keeps reads its value), run as it is by the check
names (the code's, then the rule's):
used state used
limit state limit
limit made with limit0
hit(cost) request cost
hit() result result hit_result
laws stated in the code:
proven a counted hit stays within the limit (limiter.ex:8)
proven a counted hit adds its cost (limiter.ex:9)
2 of 2 stated laws proven, 0 broken, 0 unknown, 0 not read
exit 0
A law that holds
Two functions that write struct limiter; the laws over l->used.
limiter.c
#include <stdbool.h>
struct limiter { int used; int limit; };
void set_limit(struct limiter *l, int n) { if (n >= 0 && n <= 1000000) l->limit = n; }
// gdp: law a counted hit adds its cost: result implies l->used == old(l->used) + cost
// gdp: law a counted hit stays within the limit: result implies l->used <= l->limit
bool hit(struct limiter *l, int cost) {
if (cost <= 0 || l->used + cost > l->limit) return false;
l->used += cost;
return true;
}
$ npx gdp-wired-cli check limiter.c
limiter.c: limiter (C)
checked against the code: 2353 requests from the start, 400 from drawn states, 12 from the prover's witnesses
the entry inferred: limiter, the struct the file's functions write
names (the code's, then the rule's):
used state used
limit state limit
set_limit(n) request set_limit_n
hit(cost) request hit_cost
hit() result result hit_result
method method method
laws stated in the code:
proven a counted hit adds its cost (limiter.c:7)
proven a counted hit stays within the limit (limiter.c:8)
laws proposed (nothing written; state one with the comment under it):
proven used is never negative
// gdp: law used is never negative: used >= 0
proven used never goes down
// gdp: law used never goes down: used >= old(used)
proven limit is never negative
// gdp: law limit is never negative: limit >= 0
(7 more proposed and broken by a run: not shown)
2 of 2 stated laws proven, 0 broken, 0 unknown, 0 not read
exit 0
A one-line bug
The guard checks the spend before the hit instead of after it. “A counted hit stays within the limit” breaks, with the run that breaks it, as calls of your code.
limiter.c
bool hit(struct limiter *l, int cost) {
- if (cost <= 0 || l->used + cost > l->limit) return false;
+ if (cost <= 0 || l->used > l->limit) return false;
l->used += cost;
$ npx gdp-wired-cli check limiter.c
limiter.c: limiter (C)
checked against the code: 2353 requests from the start, 400 from drawn states, 11 from the prover's witnesses
the entry inferred: limiter, the struct the file's functions write
names (the code's, then the rule's):
used state used
limit state limit
set_limit(n) request set_limit_n
hit(cost) request hit_cost
hit() result result hit_result
method method method
laws stated in the code:
proven a counted hit adds its cost (limiter.c:7)
broken a counted hit stays within the limit (limiter.c:8)
result implies l->used <= l->limit
run: hit(cost=0)
hit(cost=-3)
set_limit(n=0)
hit(cost=-3)
set_limit(n=-2)
hit(cost=2)
laws proposed (nothing written; state one with the comment under it):
proven used is never negative
// gdp: law used is never negative: used >= 0
proven used never goes down
// gdp: law used never goes down: used >= old(used)
proven limit is never negative
// gdp: law limit is never negative: limit >= 0
(7 more proposed and broken by a run: not shown)
1 of 2 stated laws proven, 1 broken, 0 unknown, 0 not read
exit 1
The boundary
Other code handed the rule’s state could change it, so a call of a function the file doesn’t define, given l, is refused:
limiter.c
#include <stdbool.h>
struct limiter { int used; int limit; };
void audit(const struct limiter *l);
void set_limit(struct limiter *l, int n) { if (n >= 0 && n <= 1000000) l->limit = n; }
// gdp: law a counted hit adds its cost: result implies l->used == old(l->used) + cost
// gdp: law a counted hit stays within the limit: result implies l->used <= l->limit
bool hit(struct limiter *l, int cost) {
if (cost <= 0 || l->used + cost > l->limit) return false;
l->used += cost;
audit(l);
return true;
}
$ npx gdp-wired-cli check limiter.c --no-propose
limiter.c: limiter (C)
refused at limiter.c:14:3: audit: a function of another file or library (other code); it is given the rule's state, which other code may change (if it doesn't, state it: skip: ["audit"], or a `gdp: skip` comment on its line)
laws stated in the code:
not read a counted hit adds its cost (limiter.c:9)
result implies l->used == old(l->used) + cost
the rule was refused
not read a counted hit stays within the limit (limiter.c:10)
result implies l->used <= l->limit
the rule was refused
0 of 2 stated laws proven, 0 broken, 0 unknown, 2 not read
exit 2
With the function’s body in the file, it is read: it writes nothing the rule keeps, and the fprintf inside it is left out.
limiter.c
#include <stdbool.h>
+#include <stdio.h>
-void audit(const struct limiter *l);
+void audit(const struct limiter *l) { fprintf(stderr, "used %d of %d\n", l->used, l->limit); }
$ npx gdp-wired-cli check limiter.c --no-propose
limiter.c: limiter (C)
checked against the code: 2400 requests from the start, 400 from drawn states, 14 from the prover's witnesses
the entry inferred: limiter, the struct the file's functions write
fprintf (limiter.c:6:39): left out (inferred: nothing the rule keeps reads its value), run as it is by the check
names (the code's, then the rule's):
used state used
limit state limit
set_limit(n) request set_limit_n
hit(cost) request hit_cost
hit() result result hit_result
method method method
laws stated in the code:
proven a counted hit adds its cost (limiter.c:10)
proven a counted hit stays within the limit (limiter.c:11)
2 of 2 stated laws proven, 0 broken, 0 unknown, 0 not read
exit 0
A law that holds
The laws over the members, used_.
limiter.cpp
class Limiter {
public:
explicit Limiter(int limit) : limit_(limit) {}
// gdp: law a counted hit adds its cost: result implies used_ == old(used_) + cost
// gdp: law a counted hit stays within the limit: result implies used_ <= limit_
bool hit(int cost) {
if (cost <= 0 || used_ + cost > limit_) return false;
used_ += cost;
return true;
}
private:
int limit_;
int used_ = 0;
};
$ npx gdp-wired-cli check limiter.cpp
limiter.cpp: Limiter (C++)
checked against the code: 2074 requests from the start, 400 from drawn states, 6 from the prover's witnesses
names (the code's, then the rule's):
limit_ state limit_
used_ state used_
limit made with limit
hit(cost) request hit_cost
hit() result result hit_result
method method method
laws stated in the code:
proven a counted hit adds its cost (limiter.cpp:5)
proven a counted hit stays within the limit (limiter.cpp:6)
laws proposed (nothing written; state one with the comment under it):
proven limit_ never changes
// gdp: law limit_ never changes: limit_ == old(limit_)
proven used_ is never negative
// gdp: law used_ is never negative: used_ >= 0
proven used_ never goes down
// gdp: law used_ never goes down: used_ >= old(used_)
(5 more proposed and broken by a run: not shown)
2 of 2 stated laws proven, 0 broken, 0 unknown, 0 not read
exit 0
A one-line bug
The guard checks the spend before the hit instead of after it. “A counted hit stays within the limit” breaks, with the run that breaks it, as calls of your code.
limiter.cpp
bool hit(int cost) {
- if (cost <= 0 || used_ + cost > limit_) return false;
+ if (cost <= 0 || used_ > limit_) return false;
used_ += cost;
$ npx gdp-wired-cli check limiter.cpp
limiter.cpp: Limiter (C++)
checked against the code: 2074 requests from the start, 400 from drawn states, 5 from the prover's witnesses
names (the code's, then the rule's):
limit_ state limit_
used_ state used_
limit made with limit
hit(cost) request hit_cost
hit() result result hit_result
method method method
laws stated in the code:
proven a counted hit adds its cost (limiter.cpp:5)
broken a counted hit stays within the limit (limiter.cpp:6)
result implies used_ <= limit_
run: made with limit = 1
hit(cost=0)
hit(cost=-2)
hit(cost=3)
laws proposed (nothing written; state one with the comment under it):
proven limit_ never changes
// gdp: law limit_ never changes: limit_ == old(limit_)
proven used_ is never negative
// gdp: law used_ is never negative: used_ >= 0
proven used_ never goes down
// gdp: law used_ never goes down: used_ >= old(used_)
(5 more proposed and broken by a run: not shown)
1 of 2 stated laws proven, 1 broken, 0 unknown, 0 not read
exit 1
The boundary
A loop in a step is refused at its line:
limiter.cpp
if (cost <= 0 || used_ + cost > limit_) return false;
- used_ += cost;
+ for (int i = 0; i < cost; i++) used_++;
return true;
$ npx gdp-wired-cli check limiter.cpp --no-propose
limiter.cpp: Limiter (C++)
refused at limiter.cpp:9:5: a loop (for)
laws stated in the code:
not read a counted hit adds its cost (limiter.cpp:5)
result implies used_ == old(used_) + cost
the rule was refused
not read a counted hit stays within the limit (limiter.cpp:6)
result implies used_ <= limit_
the rule was refused
0 of 2 stated laws proven, 0 broken, 0 unknown, 2 not read
exit 2
A retry policy
A Schedule is read into one state machine (the attempt, the retries, the delay, the time elapsed) and checked by stepping Effect’s own schedule beside it. The laws sit above the value.
backoff.ts
import {Schedule} from 'effect'
// Doubling from 100 ms, never more than 2 s apart, at most 6 retries, none decided after 10 s.
// gdp: law the delay never exceeds the cap: delay <= 2000
// gdp: law never more than 6 retries: retries <= 6
// gdp: law it stops after the deadline: elapsed > 10000 implies done
export const backoff = Schedule.min([Schedule.exponential('100 millis'), Schedule.spaced('2 seconds')]).pipe(
Schedule.upTo({times: 6, duration: '10 seconds'}),
)
$ npx gdp-wired-cli check backoff.ts
backoff.ts: backoff (TypeScript)
checked against the code: 420 requests from the start, 0 from drawn states, 0 from the prover's witnesses
names (the code's, then the rule's):
attempt state attempt
retries state retries
delay state delay
elapsed state elapsed
done state done
took request took
laws stated in the code:
proven the delay never exceeds the cap (backoff.ts:4)
proven never more than 6 retries (backoff.ts:5)
proven with what the search found: done is no → retries <= attempt
proven it stops after the deadline (backoff.ts:6)
3 of 3 stated laws proven, 0 broken, 0 unknown, 0 not read
exit 0
Without its cap, the backoff waits 3,200 ms at its sixth decision: the run of the rule, then Effect’s schedule taking it, decision by decision.
backoff.ts
// gdp: law it stops after the deadline: elapsed > 10000 implies done
-export const backoff = Schedule.min([Schedule.exponential('100 millis'), Schedule.spaced('2 seconds')]).pipe(
+export const backoff = Schedule.exponential('100 millis').pipe(
Schedule.upTo({times: 6, duration: '10 seconds'}),
$ npx gdp-wired-cli check backoff.ts
backoff.ts: backoff (TypeScript)
checked against the code: 420 requests from the start, 0 from drawn states, 0 from the prover's witnesses
names (the code's, then the rule's):
attempt state attempt
retries state retries
delay state delay
elapsed state elapsed
done state done
took request took
laws stated in the code:
broken the delay never exceeds the cap (backoff.ts:4)
delay <= 2000
run: step()
step()
step()
step()
step()
step()
Effect's schedule, decision by decision: goes on after 100 ms, goes on after 200 ms, goes on after 400 ms, goes on after 800 ms, goes on after 1600 ms, goes on after 3200 ms
proven never more than 6 retries (backoff.ts:5)
proven with what the search found: done is no → retries <= attempt
proven it stops after the deadline (backoff.ts:6)
2 of 3 stated laws proven, 1 broken, 0 unknown, 0 not read
exit 1
A transaction
A service: its TxRefs the state, its methods the requests. Each Effect.tx is one atomic step and a wait (Effect.txRetry) a guard, so calls from any number of fibers are any order of those steps.
bank.ts
import {Effect, Schema, TxRef} from 'effect'
export class Insufficient extends Schema.TaggedError<Insufficient>()('Insufficient', {amount: Schema.Number}) {}
// 100 between the two accounts: save moves some from checking to savings, spend moves it back.
// gdp: law the money is conserved: checking + savings == 100
// gdp: law checking is never overdrawn: checking >= 0
export const make = Effect.gen(function* () {
const checking = yield* TxRef.make(100)
const savings = yield* TxRef.make(0)
const save = Effect.fn('Bank.save')(function* (amount: number) {
if (amount < 0) return yield* new Insufficient({amount})
yield* Effect.tx(Effect.gen(function* () {
const balance = yield* TxRef.get(checking)
if (balance < amount) return yield* new Insufficient({amount})
yield* TxRef.set(checking, balance - amount)
yield* TxRef.update(savings, s => s + amount)
}))
})
// Waits (the transaction retries when savings change) until savings cover the amount.
const spend = Effect.fn('Bank.spend')(function* (amount: number) {
if (amount < 0) return yield* new Insufficient({amount})
yield* Effect.tx(Effect.gen(function* () {
const s = yield* TxRef.get(savings)
if (s < amount) return yield* Effect.txRetry
yield* TxRef.set(savings, s - amount)
yield* TxRef.update(checking, c => c + amount)
}))
})
return {save, spend}
})
$ npx gdp-wired-cli check bank.ts
bank.ts: make (TypeScript)
checked against the code: 2400 requests from the start, 400 from drawn states, 4 from the prover's witnesses, 8 of 8 branch arms
names (the code's, then the rule's):
checking state checking
savings state savings
save(amount) request save_amount
spend(amount) request spend_amount
method method method
laws stated in the code:
proven the money is conserved (bank.ts:6)
proven checking is never overdrawn (bank.ts:7)
laws proposed (nothing written; state one with the comment under it):
proven savings is never negative
// gdp: law savings is never negative: savings >= 0
(6 more proposed and broken by a run: not shown)
2 of 2 stated laws proven, 0 broken, 0 unknown, 0 not read
exit 0
Read the balance in one transaction and write it in another, and two fibers interleaving break the conservation law:
bank.ts
if (amount < 0) return yield* new Insufficient({amount})
+ const balance = yield* TxRef.get(checking)
+ if (balance < amount) return yield* new Insufficient({amount})
+ yield* Effect.sleep('1 millis')
yield* Effect.tx(Effect.gen(function* () {
- const balance = yield* TxRef.get(checking)
- if (balance < amount) return yield* new Insufficient({amount})
yield* TxRef.set(checking, balance - amount)
$ npx gdp-wired-cli check bank.ts
bank.ts: make (TypeScript)
checked against the code: 2400 requests from the start, 400 from drawn states, 10 from the prover's witnesses, 12 of 12 branch arms
names (the code's, then the rule's):
checking state checking
savings state savings
save(amount) request save_amount
spend(amount) request spend_amount
method method method
fiber request fiber
laws stated in the code:
broken the money is conserved (bank.ts:6)
checking + savings == 100
run: save(amount=0)
save(amount=2)
save(amount=1)
save(amount=3)
save(amount=1)
save(amount=3)
proven checking is never overdrawn (bank.ts:7)
laws proposed (nothing written; state one with the comment under it):
proven savings is never negative
// gdp: law savings is never negative: savings >= 0
(17 more proposed and broken by a run: not shown)
1 of 2 stated laws proven, 1 broken, 0 unknown, 0 not read
exit 1
The boundary
Effect’s constructors that the reader doesn’t model are refused by name:
bank.ts
})
+ yield* Effect.forkDaemon(spend(1))
return {save, spend}
$ npx gdp-wired-cli check bank.ts --no-propose
bank.ts: Insufficient (TypeScript)
refused at bank.ts:3:1: Insufficient: no make effect (an Effect.gen, a function returning one, Effect.fn, or Layer.effect of one)
bank.ts: make (TypeScript)
refused at bank.ts:30:10: Effect.forkDaemon (Effect.gen, Effect.fn bodies, succeed, sync, fail, die, void and log are read; the effect of forkDaemon isn't)
laws stated in the code:
not read the money is conserved (bank.ts:6)
checking + savings == 100
the rule was refused
not read checking is never overdrawn (bank.ts:7)
checking >= 0
the rule was refused
0 of 2 stated laws proven, 0 broken, 0 unknown, 2 not read
exit 2
A law that holds
A machine: its context the state, value its state node, event the event. The laws above the machine hold after every event.
limiter.ts
import {assign, setup} from 'xstate'
// gdp: law used never passes the limit: used <= limit
// gdp: law it stays open: value == "open"
export const limiter = setup({
types: {} as {context: {used: number, limit: number}, events: {type: 'hit', cost: number}},
guards: {fits: ({context, event}) => event.cost > 0 && context.used + event.cost <= context.limit},
}).createMachine({
context: {used: 0, limit: 10},
initial: 'open',
states: {
open: {on: {hit: {guard: 'fits', actions: assign({used: ({context, event}) => context.used + event.cost})}}},
},
})
$ npx gdp-wired-cli check limiter.ts
limiter.ts: limiter (TypeScript)
checked against XState's own actor: 3200 events, 1 of 1 branch arms
names (the code's, then the rule's):
value state state
used state used
limit state limit
event.cost request cost
event method event
laws stated in the code:
proven used never passes the limit (limiter.ts:3)
proven it stays open (limiter.ts:4)
2 of 2 stated laws proven, 0 broken, 0 unknown, 0 not read
exit 0
A one-line bug
The guard checks the spend before the hit:
limiter.ts
types: {} as {context: {used: number, limit: number}, events: {type: 'hit', cost: number}},
- guards: {fits: ({context, event}) => event.cost > 0 && context.used + event.cost <= context.limit},
+ guards: {fits: ({context, event}) => event.cost > 0 && context.used <= context.limit},
}).createMachine({
$ npx gdp-wired-cli check limiter.ts
limiter.ts: limiter (TypeScript)
checked against XState's own actor: 3200 events, 1 of 1 branch arms
names (the code's, then the rule's):
value state state
used state used
limit state limit
event.cost request cost
event method event
laws stated in the code:
broken used never passes the limit (limiter.ts:3)
used <= limit
run: hit()
hit()
hit()
hit()
hit()
hit()
proven it stays open (limiter.ts:4)
1 of 2 stated laws proven, 1 broken, 0 unknown, 0 not read
exit 1
The boundary
A guard that answers differently each time is refused:
limiter.ts
types: {} as {context: {used: number, limit: number}, events: {type: 'hit', cost: number}},
- guards: {fits: ({context, event}) => event.cost > 0 && context.used + event.cost <= context.limit},
+ guards: {fits: ({context, event}) => event.cost > 0 && Math.random() < 0.9 && context.used + event.cost <= context.limit},
}).createMachine({
$ npx gdp-wired-cli check limiter.ts --no-propose
limiter.ts: limiter (TypeScript)
refused at limiter.ts:7:58: Math.random (a step the code takes differently each time)
laws stated in the code:
not read used never passes the limit (limiter.ts:3)
used <= limit
the rule was refused
not read it stays open (limiter.ts:4)
value == "open"
the rule was refused
0 of 2 stated laws proven, 0 broken, 0 unknown, 2 not read
exit 2
And a timer that doesn’t leave its state:
limiter.ts
states: {
- open: {on: {hit: {guard: 'fits', actions: assign({used: ({context, event}) => context.used + event.cost})}}},
+ open: {after: {60000: {actions: assign({used: 0})}}, on: {hit: {guard: 'fits', actions: assign({used: ({context, event}) => context.used + event.cost})}}},
},
$ npx gdp-wired-cli check limiter.ts --no-propose
limiter.ts: limiter (TypeScript)
refused at limiter.ts: fromXState: machine: open's after: a timer that doesn't leave the state (the answer comes once a visit; a chart would take it again)
laws stated in the code:
not read used never passes the limit (limiter.ts:3)
used <= limit
the rule was refused
not read it stays open (limiter.ts:4)
value == "open"
the rule was refused
0 of 2 stated laws proven, 0 broken, 0 unknown, 2 not read
exit 2
gdp.toml
The few modelling choices a rule needs are written once, beside the code: the nearest gdp.toml from
the file up (or [tool.gdp] in pyproject.toml, "gdp" in package.json). Top-level keys hold
for every rule; [rules.<name>] for one. A flag on the command line wins over both. TOML has no
null: "None" (or "null", "nil") is one.
| Key | What it states |
|---|---|
entry, methods | The class or functions to read, and which methods are the requests (by default the public ones). Read with none named: the one class whose public methods write its state. |
answers | What a call of other code returns where the code declares no type: answers = {cost = "int"}, {get_allowed_fails_from_policy = "int | None"}. The same as a gdp: answer int comment on the call’s line. |
skip | Calls stated to change nothing the rule keeps, left out (the same as gdp: skip on the line). |
stores.<field> | An injected store’s methods (get, set, delete, skip), its value type, the values per key form, and attrs: its attributes held at a value ({"dual_cache.redis_cache" = "None"}: Redis off). |
failures | "refuse" (the default): an exception or a panic is the step’s refusal. "kept": the step is taken, the state as it was, failed true, for laws about when the code refuses. |
onError | Go and C: "discard" when the caller discards what a failed call wrote (else a write before the failure is refused). |
from | "any": prove every law from every state the laws allow, for code that steps the rule from states it is handed (loaded from a database, say); the largest inductive set of the stated laws is found. |
strict | true: nothing inferred; the boundary only as stated (answers, skip, stores), any other call of other code refused. |
clock | "s" or "ms": the unit of the language’s clock, for durations in a law, where the language doesn’t say. |
defaults, fields, share | Constructor arguments taken at their defaults; fields held at a value after construction (the claim is for objects so configured); a method’s scalar arguments one input per type and place across methods, so a law over a map they key reads fewer keys. |
root, python | Python: the import root your modules are read from, and the interpreter. |
gdp.toml
# every rule: laws about when the code raises
failures = "kept"
[rules.SessionLimiter]
methods = ["hit"]
stores.cache.attrs = {"redis" = "None"}
answers = {lookup_plan = "int | None"}
From a program
The same reading as a library: one import, the file (a path or a URL; a Go package or a Rust crate by any file
of it), a law over the rule’s names. The entry is inferred ({entry} names one), and Effect and
XState run on the packages your project has. Here, the Python limiter of the Python tab, read from TypeScript:
prove.ts
import {extractPython, proveRule} from 'gdp-wired/extract-py'
const x = extractPython('./limiter.py')
console.log(proveRule(x.rule!, [x.rule!.law('used is never negative', ({used}) => used.atLeast(0))]))
$ bun prove.ts
{
proven: true,
checked: true,
}
| Language | Function | Module |
|---|---|---|
| Python | extractPython | gdp-wired/extract-py |
| TypeScript | extract | gdp-wired/extract |
| Go | extractGo | gdp-wired/extract-go |
| Rust | extractRust | gdp-wired/extract-rs |
| Java | extractJava | gdp-wired/extract-java |
| Kotlin | extractKotlin | gdp-wired/extract-kotlin |
| Swift | extractSwift | gdp-wired/extract-swift |
| Ruby | extractRuby | gdp-wired/extract-rb |
| Elixir | extractElixir | gdp-wired/extract-ex |
| C | extractC | gdp-wired/extract-c |
| C++ | extractCpp | gdp-wired/extract-cpp |
| Effect | extractEffect | gdp-wired/effect |
| XState | extractXState | gdp-wired/extract |
Each module also exports proveRule and proveSteps. The output above is what
prove.ts printed with gdp-wired@0.43.0 from npm, on boat. In TypeScript the rule can also be
written directly, its laws as code: the guide starts there.
What the boundary claims
A proof holds of your code with every answered call free to return anything its declared type allows, so it
holds whatever the callee does. A refutation may rest on a value the callee never returns, so it names what it
assumes (assumes … may return -1 (inferred: any int)). Read it, and decide.
Every inferred statement is printed (left out, answered, an injected store, your own code taken as other code), and the check runs both sides on it: a skipped call is run as it is, an answered one is stubbed. A skipped call that raises, writes the state or changes the result is a disagreement, and the rule is refused.
Anything handed the rule’s state is refused: other code, or an injected dependency’s method, given
self or this, a nested object, a map, a list, a store the rule holds, or an alias of one,
unless you mark the call gdp: skip, stating that it changes nothing the rule keeps. An independent
adversarial review found three false proofs there before 0.32, an injected listener or a store’s get
handed this; all are refused now and kept as tests, with the other 22 cases
(research/langs/adversarial).
Tags go on a call’s line, or on a function’s line or the line above it for every call of it:
gdp: skip, gdp: answer [type], and gdp: strict to turn the inference off
and keep the boundary only as stated.