|
| 1 | +# Getting Started |
| 2 | + |
| 3 | +## Installation |
| 4 | + |
| 5 | +```bash |
| 6 | +uv add gds-proof |
| 7 | +``` |
| 8 | + |
| 9 | +For development (monorepo): |
| 10 | + |
| 11 | +```bash |
| 12 | +git clone https://github.com/BlockScience/gds-core.git |
| 13 | +cd gds-core |
| 14 | +uv sync --all-packages |
| 15 | +``` |
| 16 | + |
| 17 | +## Your First Invariant Proof |
| 18 | + |
| 19 | +The typical workflow: define a GDS spec, enrich blocks with symbolic expressions, declare invariants, and run the proof engine. |
| 20 | + |
| 21 | +### 1. Define the structural spec |
| 22 | + |
| 23 | +```python |
| 24 | +import sympy |
| 25 | +from gds import GDSSpec, Mechanism, Entity, interface, state_var, typedef |
| 26 | + |
| 27 | +# Types and state |
| 28 | +Balance = typedef("Balance", float) |
| 29 | +account = Entity( |
| 30 | + name="Account", |
| 31 | + variables={"balance": state_var(Balance, symbol="x_prev")}, |
| 32 | +) |
| 33 | + |
| 34 | +# Mechanism block |
| 35 | +withdrawal = Mechanism( |
| 36 | + name="withdrawal", |
| 37 | + interface=interface(forward_in=["Balance Command"]), |
| 38 | + updates=[("Account", "balance")], |
| 39 | +) |
| 40 | + |
| 41 | +# Register in a spec |
| 42 | +spec = GDSSpec(name="bank_account") |
| 43 | +spec.collect(Balance, account, withdrawal) |
| 44 | +``` |
| 45 | + |
| 46 | +### 2. Enrich with symbolic expressions |
| 47 | + |
| 48 | +GDS blocks are structural -- they don't carry SymPy math. Use `GDSSymbolicBlock` to pair a block with its symbolic state transition: |
| 49 | + |
| 50 | +```python |
| 51 | +from gds_proof import ( |
| 52 | + GDSSymbolicBlock, GDSSymbolicModel, |
| 53 | + Invariant, predicate_from_post_check, |
| 54 | +) |
| 55 | + |
| 56 | +X = sympy.Symbol("x_prev") # pre-state (plain, no assumptions!) |
| 57 | +U = sympy.Symbol("u") # input |
| 58 | + |
| 59 | +# Build a predicate: balance must remain positive after withdrawal |
| 60 | +pred = predicate_from_post_check( |
| 61 | + name="no_overdraft", |
| 62 | + post_state_check=sympy.Symbol("x") > 0, # desired post-state property |
| 63 | + state_transition={"x": X - U}, # x = x_prev - u |
| 64 | +) |
| 65 | + |
| 66 | +# Enrich the mechanism with symbolic expressions |
| 67 | +symbolic_block = GDSSymbolicBlock( |
| 68 | + block=withdrawal, |
| 69 | + spec=spec, |
| 70 | + state_transition={"x_prev": X - U}, # f(x_prev, u) = x_prev - u |
| 71 | + output_expressions={"balance": X - U}, # observable output |
| 72 | + predicates_list=[pred.expr], # admissibility guard |
| 73 | + inputs=frozenset({U}), |
| 74 | +) |
| 75 | +``` |
| 76 | + |
| 77 | +### 3. Declare invariants and run analysis |
| 78 | + |
| 79 | +```python |
| 80 | +from gds_proof import analyze_invariants, analyze_inductive_safety |
| 81 | + |
| 82 | +# Build the proof-ready model |
| 83 | +model = GDSSymbolicModel( |
| 84 | + spec=spec, |
| 85 | + enrichments={"withdrawal": symbolic_block}, |
| 86 | + invariants_dict={ |
| 87 | + "balance_nonneg": Invariant( |
| 88 | + name="balance_nonneg", |
| 89 | + expr=sympy.Ge(X, 0), # x_prev >= 0 |
| 90 | + ), |
| 91 | + }, |
| 92 | + assumptions={ |
| 93 | + X: {"nonnegative": True, "real": True}, |
| 94 | + U: {"nonnegative": True, "real": True}, |
| 95 | + }, |
| 96 | +) |
| 97 | + |
| 98 | +# Run symbolic analysis |
| 99 | +result = analyze_invariants(model) |
| 100 | +for r in result.results: |
| 101 | + print(f"{r.invariant_name} x {r.mechanism_name}: " |
| 102 | + f"{r.status} ({r.proof_method})") |
| 103 | + |
| 104 | +# Run inductive safety analysis |
| 105 | +safety = analyze_inductive_safety(model) |
| 106 | +print(f"Multi-step verdict: {safety.multi_step.verdict}") |
| 107 | +``` |
| 108 | + |
| 109 | +### 4. Convert to VerificationReport |
| 110 | + |
| 111 | +Integrate proof results with the existing verification pipeline: |
| 112 | + |
| 113 | +```python |
| 114 | +from gds_proof.findings import ( |
| 115 | + symbolic_analysis_to_findings, |
| 116 | + proof_findings_to_report, |
| 117 | +) |
| 118 | + |
| 119 | +findings = symbolic_analysis_to_findings(result) |
| 120 | +report = proof_findings_to_report("bank_account", findings) |
| 121 | +print(f"Checks: {report.checks_total}, Errors: {report.errors}") |
| 122 | +``` |
| 123 | + |
| 124 | +## Auxiliary Proofs for INCONCLUSIVE Results |
| 125 | + |
| 126 | +When the automatic prover can't resolve a pair, build a manual proof script: |
| 127 | + |
| 128 | +```python |
| 129 | +from gds_proof import ( |
| 130 | + hash_model, ProofBuilder, LemmaKind, |
| 131 | + verify_proof, attach_proof, |
| 132 | +) |
| 133 | + |
| 134 | +model_hash = hash_model(model) |
| 135 | + |
| 136 | +# Build a proof script with a geometric series lemma |
| 137 | +k = sympy.Symbol("k", integer=True, nonneg=True) |
| 138 | +script = ( |
| 139 | + ProofBuilder( |
| 140 | + model_hash, "balance_nonneg", |
| 141 | + "convergence_proof", "Balance converges to steady state", |
| 142 | + ) |
| 143 | + .lemma( |
| 144 | + "series_limit", |
| 145 | + LemmaKind.EQUALITY, |
| 146 | + expr=sympy.Sum(sympy.Rational(1, 2) ** k, (k, 0, sympy.oo)), |
| 147 | + expected=sympy.Integer(2), |
| 148 | + ) |
| 149 | + .build() |
| 150 | +) |
| 151 | + |
| 152 | +# Verify independently (no trust in original analyst) |
| 153 | +proof_result = verify_proof(script, model_hash) |
| 154 | +print(f"Proof status: {proof_result.status}") |
| 155 | + |
| 156 | +# Attach to invariant if VERIFIED |
| 157 | +if proof_result.status == "VERIFIED": |
| 158 | + inv = model.invariants()["balance_nonneg"] |
| 159 | + inv = attach_proof(inv, script, model_hash) |
| 160 | + print(f"Proof hash: {inv.proof_hash}") |
| 161 | +``` |
| 162 | + |
| 163 | +## Next Steps |
| 164 | + |
| 165 | +- [Proof Overview](index.md) -- architecture, key concepts, and verification integration |
| 166 | +- [Framework](../framework/index.md) -- GDS specification and structural types |
| 167 | +- [Analysis](../analysis/index.md) -- simulation-based reachability and PSUU |
0 commit comments