Structured stack programs with black-box calls
Lax218471.Programs · concepts/Lax218471/Programs.lean · lax-218471
No public endorsements yet.
Loading review…
Sign in with ORCIDNatural Language Statement
Definition
Programs use stack operations, finite control, sequencing, conditionals, while loops, and calls to a fixed library of contracts. A call replaces one stack with its result and preserves every other stack and the caller's control. The execution relation records the number of source instructions and a bound on all call argument and result lengths. In particular, a polynomial bound on the number of iterations alone does not assert polynomial time.
Concept map
Lean source view on GitHub
| 1 | import Lax218471.Subroutine |
| 2 | import Lax218471.PolynomialTime |
| 3 | |
| 4 | /-! |
| 5 | --- |
| 6 | title: Structured stack programs with black-box calls |
| 7 | type: definition |
| 8 | --- |
| 9 | Programs use stack operations, finite control, sequencing, conditionals, |
| 10 | while loops, and calls to a fixed library of contracts. A call replaces one |
| 11 | stack with its result and preserves every other stack and the caller's control. |
| 12 | The execution relation records the number of source instructions and a bound |
| 13 | on all call argument and result lengths. In particular, a polynomial bound |
| 14 | on the number of iterations alone does not assert polynomial time. |
| 15 | -/ |
| 16 | |
| 17 | namespace Lax218471.Programs |
| 18 | |
| 19 | open Subroutine |
| 20 | |
| 21 | variable {K A σ I : Type} |
| 22 | |
| 23 | /-- One local operation; the control type is finite when programs are compiled. -/ |
| 24 | inductive Op (K A σ : Type) |
| 25 | | push (k : K) (f : σ → A) |
| 26 | | pop (k : K) (f : σ → Option A → σ) |
| 27 | | peek (k : K) (f : σ → Option A → σ) |
| 28 | | load (f : σ → σ) |
| 29 | |
| 30 | /-- A finite program with named library calls. Loops retain their body as finite code. -/ |
| 31 | inductive Program (K A σ I : Type) |
| 32 | | atom (o : Op K A σ) |
| 33 | | call (i : I) (port : K) |
| 34 | | seq (p q : Program K A σ I) |
| 35 | | branch (b : σ → Bool) (p q : Program K A σ I) |
| 36 | | loop (b : σ → Bool) (p : Program K A σ I) |
| 37 | |
| 38 | def Op.apply [DecidableEq K] (o : Op K A σ) (s : Store (fun _ : K => A) σ) : |
| 39 | Store (fun _ : K => A) σ := |
| 40 | match o with |
| 41 | | .push k f => ⟨s.state, Function.update s.stk k (f s.state :: s.stk k)⟩ |
| 42 | | .pop k f => ⟨f s.state (s.stk k).head?, Function.update s.stk k (s.stk k).tail⟩ |
| 43 | | .peek k f => ⟨f s.state (s.stk k).head?, s.stk⟩ |
| 44 | | .load f => ⟨f s.state, s.stk⟩ |
| 45 | |
| 46 | /-- Source execution, with a bound on the length of every argument and reply. -/ |
| 47 | inductive Executes [DecidableEq K] (R : I → List A → List A → Prop) (size : ℕ) : |
| 48 | Program K A σ I → Store (fun _ : K => A) σ → Store (fun _ : K => A) σ → ℕ → Prop |
| 49 | | atom (o) (s) : Executes R size (.atom o) s (o.apply s) 1 |
| 50 | | call (i) (port) (s) (y) : R i (s.stk port) y → |
| 51 | (s.stk port).length ≤ size → y.length ≤ size → |
| 52 | Executes R size (.call i port) s ⟨s.state, Function.update s.stk port y⟩ 1 |
| 53 | | seq {p q s u t a b} : Executes R size p s u a → Executes R size q u t b → |
| 54 | Executes R size (.seq p q) s t (a + b) |
| 55 | | branch_true {p q s t a} {b : σ → Bool} : b s.state = true → |
| 56 | Executes R size p s t a → Executes R size (.branch b p q) s t (a + 1) |
| 57 | | branch_false {p q s t a} {b : σ → Bool} : b s.state = false → |
| 58 | Executes R size q s t a → Executes R size (.branch b p q) s t (a + 1) |
| 59 | | loop_false {p s} {b : σ → Bool} : b s.state = false → |
| 60 | Executes R size (.loop b p) s s 1 |
| 61 | | loop_true {p s u t a c} {b : σ → Bool} : b s.state = true → |
| 62 | Executes R size p s u a → Executes R size (.loop b p) u t c → |
| 63 | Executes R size (.loop b p) s t (a + c + 1) |
| 64 | |
| 65 | end Lax218471.Programs |
| 66 |
Used by
From Mathlib
none
Discussion
Ask a question or add context. Endorsements and structured flags are kept in the review panel above.
0 comments