Documentation

CslibFrontier.Computability.Program.Structured

Structured programs over an instruction set #

Flat programs are lists of instructions with numeric jump targets, which are inconvenient to write and to reason about. A structured program (Code) is built from data instructions by sequencing, conditionals and loops on tests. This file defines structured programs and their cost-exact semantics once, for every machine: a machine supplies an InstructionSet, that is its data, its data instructions and tests, and their meaning.

The big-step semantics Exec counts exactly the instructions of the flat program a structured program assembles to (Code.assemble and Exec.runs in Program.Assemble). The costs assume a machine whose test instruction jumps when the test holds:

ExecWithin bounds the count instead, and its rules compose like Hoare logic, with time bounds added along sequences. Code.eval is a structurally recursive interpreter with a budget of loop iterations, so simp and decide evaluate it, and Exec.of_eval turns its result into an execution.

Main definitions #

Main results #

References #

structure Cslib.Program.InstructionSet :
Type (max (max (u + 1) (v + 1)) (w + 1))

The data of a machine, its data instructions and its tests, with their meaning. Control flow is not part of an instruction set: structured programs (Code) supply it.

  • Data : Type u

    What a program transforms: registers and memory.

  • Op : Type v

    Data instructions.

  • Test : Type w

    Tests.

  • eval : self.Op → self.Data → Option self.Data

    The data after a data instruction, or none if it faults.

  • Holds : self.Test → self.Data → Prop

    Whether a test holds of the data.

inductive Cslib.Program.Code (Op : Type v) (Test : Type w) :
Type (max v w)

Structured programs over data instructions Op and tests Test.

  • skip {Op : Type v} {Test : Type w} : Code Op Test

    Do nothing; no instruction.

  • op {Op : Type v} {Test : Type w} (o : Op) : Code Op Test

    A data instruction.

  • seq {Op : Type v} {Test : Type w} (c₁ c₂ : Code Op Test) : Code Op Test

    Run c₁, then c₂.

  • branch {Op : Type v} {Test : Type w} (t : Test) (pos neg : Code Op Test) : Code Op Test

    Run pos if t holds, and neg otherwise.

  • loop {Op : Type v} {Test : Type w} (t : Test) (body : Code Op Test) : Code Op Test

    Run body while t holds.

  • loopUntil {Op : Type v} {Test : Type w} (t : Test) (body : Code Op Test) : Code Op Test

    Run body until t holds.

def Cslib.Program.instDecidableEqCode.decEq {Op✝ : Type u_1} {Test✝ : Type u_2} [DecidableEq Op✝] [DecidableEq Test✝] (x✝ x✝¹ : Code Op✝ Test✝) :
Decidable (x✝ = x✝¹)
@[instance_reducible]
instance Cslib.Program.instDecidableEqCode {Op✝ : Type u_1} {Test✝ : Type u_2} [DecidableEq Op✝] [DecidableEq Test✝] :
DecidableEq (Code Op✝ Test✝)
def Cslib.Program.Code.size {Op : Type v} {Test : Type w} :
Code Op Test → ℕ

The length of the flat program a structured program assembles to.

def Cslib.Program.Code.seqList {Op : Type v} {Test : Type w} :
List (Code Op Test) → Code Op Test

Run the programs of a list in order.

def Cslib.Program.Code.ops {Op : Type v} {Test : Type w} :
Code Op Test → List Op

The data instructions that occur in a structured program.

Executions #

inductive Cslib.Program.Exec (I : InstructionSet) :
Code I.Op I.Test → I.Data → I.Data → ℕ → Prop

Exec I c x y n: the structured program c leads from data x to data y executing exactly n instructions of the flat program it assembles to.

def Cslib.Program.ExecWithin (I : InstructionSet) (c : Code I.Op I.Test) (x y : I.Data) (T : ℕ) :

ExecWithin I c x y T: the structured program c leads from x to y within T instructions.

theorem Cslib.Program.Exec.unique {I : InstructionSet} {c : Code I.Op I.Test} {x y y' : I.Data} {n n' : ℕ} (h : Exec I c x y n) (h' : Exec I c x y' n') :
y = y' ∧ n = n'

Executions are deterministic.

theorem Cslib.Program.Exec.invariant {I : InstructionSet} {c : Code I.Op I.Test} {x y : I.Data} {n : ℕ} (P : I.Data → Prop) (hop : ∀ (o : I.Op) (x y : I.Data), I.eval o x = some y → P x → P y) (h : Exec I c x y n) (hx : P x) :
P y

A property that every data instruction preserves is preserved by executions.

theorem Cslib.Program.Exec.invariant_ops {I : InstructionSet} {c : Code I.Op I.Test} {x y : I.Data} {n : ℕ} (P : I.Data → Prop) (h : Exec I c x y n) (hop : ∀ o ∈ c.ops, ∀ (x y : I.Data), I.eval o x = some y → P x → P y) (hx : P x) :
P y

A property that every data instruction occurring in the program preserves is preserved by its executions.

theorem Cslib.Program.Exec.within {I : InstructionSet} {c : Code I.Op I.Test} {x y : I.Data} {n : ℕ} (h : Exec I c x y n) :
ExecWithin I c x y n

An execution is within its instruction count.

Time bounds #

theorem Cslib.Program.ExecWithin.of_exec {I : InstructionSet} {c : Code I.Op I.Test} {x y : I.Data} {T n : ℕ} (h : Exec I c x y n) (hn : n ≤ T) :
ExecWithin I c x y T

An execution whose instruction count is within the budget.

theorem Cslib.Program.ExecWithin.mono {I : InstructionSet} {c : Code I.Op I.Test} {x y : I.Data} {T T₂ : ℕ} (h : ExecWithin I c x y T) (hT : T ≤ T₂) :
ExecWithin I c x y T₂

Monotonicity in the instruction budget.

theorem Cslib.Program.ExecWithin.invariant {I : InstructionSet} {c : Code I.Op I.Test} {x y : I.Data} {T : ℕ} (P : I.Data → Prop) (hop : ∀ (o : I.Op) (x y : I.Data), I.eval o x = some y → P x → P y) (h : ExecWithin I c x y T) (hx : P x) :
P y

A property that every data instruction preserves is preserved by executions.

skip executes within any bound.

theorem Cslib.Program.ExecWithin.op {I : InstructionSet} {x y : I.Data} {o : I.Op} (h : I.eval o x = some y) :
ExecWithin I (Code.op o) x y 1

A data instruction executes within one instruction.

theorem Cslib.Program.ExecWithin.seq {I : InstructionSet} {c₁ c₂ : Code I.Op I.Test} {x y z : I.Data} {T₁ T₂ : ℕ} (h₁ : ExecWithin I c₁ x y T₁) (h₂ : ExecWithin I c₂ y z T₂) :
ExecWithin I (c₁.seq c₂) x z (T₁ + T₂)

Sequencing adds the instruction bounds.

theorem Cslib.Program.ExecWithin.branch_pos {I : InstructionSet} {pos neg : Code I.Op I.Test} {t : I.Test} {x y : I.Data} {T : ℕ} (ht : I.Holds t x) (h : ExecWithin I pos x y T) :
ExecWithin I (Code.branch t pos neg) x y (T + 1)

The first alternative of a conditional adds one instruction to the bound.

theorem Cslib.Program.ExecWithin.branch_neg {I : InstructionSet} {pos neg : Code I.Op I.Test} {t : I.Test} {x y : I.Data} {T : ℕ} (ht : ¬I.Holds t x) (h : ExecWithin I neg x y T) :
ExecWithin I (Code.branch t pos neg) x y (T + 2)

The second alternative of a conditional adds two instructions to the bound.

theorem Cslib.Program.ExecWithin.branch {I : InstructionSet} {pos neg : Code I.Op I.Test} {t : I.Test} {x y : I.Data} {T : ℕ} (hpos : I.Holds t x → ExecWithin I pos x y T) (hneg : ¬I.Holds t x → ExecWithin I neg x y T) :
ExecWithin I (Code.branch t pos neg) x y (T + 2)

A conditional, with one bound for both alternatives.

theorem Cslib.Program.ExecWithin.loop_exit {I : InstructionSet} {body : Code I.Op I.Test} {t : I.Test} {x : I.Data} (ht : ¬I.Holds t x) :
ExecWithin I (Code.loop t body) x x 2

Leaving a loop takes two instructions.

theorem Cslib.Program.ExecWithin.loop_iter {I : InstructionSet} {body : Code I.Op I.Test} {t : I.Test} {x y z : I.Data} {T₁ T₂ : ℕ} (ht : I.Holds t x) (h₁ : ExecWithin I body x y T₁) (h₂ : ExecWithin I (Code.loop t body) y z T₂) :
ExecWithin I (Code.loop t body) x z (T₁ + T₂ + 2)

An iteration of a loop followed by the rest of it.

theorem Cslib.Program.ExecWithin.loopUntil_exit {I : InstructionSet} {body : Code I.Op I.Test} {t : I.Test} {x : I.Data} (ht : I.Holds t x) :
ExecWithin I (Code.loopUntil t body) x x 1

Leaving a loopUntil takes one instruction.

theorem Cslib.Program.ExecWithin.loopUntil_iter {I : InstructionSet} {body : Code I.Op I.Test} {t : I.Test} {x y z : I.Data} {T₁ T₂ : ℕ} (ht : ¬I.Holds t x) (h₁ : ExecWithin I body x y T₁) (h₂ : ExecWithin I (Code.loopUntil t body) y z T₂) :
ExecWithin I (Code.loopUntil t body) x z (T₁ + T₂ + 2)

An iteration of a loopUntil followed by the rest of it.

theorem Cslib.Program.ExecWithin.loop_potential {I : InstructionSet} {body : Code I.Op I.Test} {t : I.Test} {x : I.Data} (P : I.Data → Prop) (Φ : I.Data → ℕ) (hbody : ∀ (x : I.Data), P x → I.Holds t x → ∃ (y : I.Data) (T : ℕ), ExecWithin I body x y T ∧ P y ∧ T + 2 + Φ y ≤ Φ x) (hx : P x) :
∃ (y : I.Data), ExecWithin I (Code.loop t body) x y (Φ x + 2) ∧ P y ∧ ¬I.Holds t y

Loop rule, invariant and potential. If the body leads every data x satisfying the invariant P and the test to data y satisfying P, within T instructions with T + 2 + Φ y ≤ Φ x, then from data satisfying P the loop ends within Φ x + 2 instructions, in data satisfying P where the test fails. The two extra instructions of an iteration are the test and the jump back; the time of the body may differ between iterations, as long as the potential Φ pays for it.

theorem Cslib.Program.ExecWithin.loop_invariant {I : InstructionSet} {body : Code I.Op I.Test} {t : I.Test} {x : I.Data} (P : I.Data → Prop) (μ : I.Data → ℕ) {T : ℕ} (hbody : ∀ (x : I.Data), P x → I.Holds t x → ∃ (y : I.Data), ExecWithin I body x y T ∧ P y ∧ μ y < μ x) (hx : P x) :
∃ (y : I.Data), ExecWithin I (Code.loop t body) x y (μ x * (T + 2) + 2) ∧ P y ∧ ¬I.Holds t y

Loop rule, invariant and measure. If the body leads every data satisfying the invariant P and the test, within T instructions, to data satisfying P with a smaller measure μ, then from data satisfying P the loop ends within μ x * (T + 2) + 2 instructions, in data satisfying P where the test fails.

theorem Cslib.Program.ExecWithin.loopUntil_potential {I : InstructionSet} {body : Code I.Op I.Test} {t : I.Test} {x : I.Data} (P : I.Data → Prop) (Φ : I.Data → ℕ) (hbody : ∀ (x : I.Data), P x → ¬I.Holds t x → ∃ (y : I.Data) (T : ℕ), ExecWithin I body x y T ∧ P y ∧ T + 2 + Φ y ≤ Φ x) (hx : P x) :
∃ (y : I.Data), ExecWithin I (Code.loopUntil t body) x y (Φ x + 1) ∧ P y ∧ I.Holds t y

Until rule, invariant and potential. If the body leads every data x satisfying the invariant P where the test fails to data y satisfying P, within T instructions with T + 2 + Φ y ≤ Φ x, then from data satisfying P the loop ends within Φ x + 1 instructions, in data satisfying P and the test.

theorem Cslib.Program.ExecWithin.loopUntil_invariant {I : InstructionSet} {body : Code I.Op I.Test} {t : I.Test} {x : I.Data} (P : I.Data → Prop) (μ : I.Data → ℕ) {T : ℕ} (hbody : ∀ (x : I.Data), P x → ¬I.Holds t x → ∃ (y : I.Data), ExecWithin I body x y T ∧ P y ∧ μ y < μ x) (hx : P x) :
∃ (y : I.Data), ExecWithin I (Code.loopUntil t body) x y (μ x * (T + 2) + 1) ∧ P y ∧ I.Holds t y

Until rule, invariant and measure. If the body leads every data satisfying the invariant P where the test fails, within T instructions, to data satisfying P with a smaller measure μ, then from data satisfying P the loop ends within μ x * (T + 2) + 1 instructions, in data satisfying P and the test.

Interpreter #

def Cslib.Program.Code.evalLoop (I : InstructionSet) (continues : I.Data → Bool) (exit : ℕ) (body : I.Data → Option (I.Data × ℕ)) :
ℕ → I.Data → Option (I.Data × ℕ)

Run a loop that continues while continues and whose body is body, for at most fuel iterations. Leaving it costs exit instructions.

def Cslib.Program.Code.eval (I : InstructionSet) [(t : I.Test) → (x : I.Data) → Decidable (I.Holds t x)] (fuel : ℕ) :
Code I.Op I.Test → I.Data → Option (I.Data × ℕ)

Run a structured program, allowing each loop at most fuel iterations per entry: some (y, n) if it leads to y executing n instructions, and none if it faults or runs out of iterations. The definition is structurally recursive, so simp and decide evaluate it.

theorem Cslib.Program.Exec.of_eval {I : InstructionSet} [(t : I.Test) → (x : I.Data) → Decidable (I.Holds t x)] {fuel : ℕ} {c : Code I.Op I.Test} {x y : I.Data} {n : ℕ} (h : Code.eval I fuel c x = some (y, n)) :
Exec I c x y n

The interpreter only finds executions.

theorem Cslib.Program.ExecWithin.of_eval {I : InstructionSet} [(t : I.Test) → (x : I.Data) → Decidable (I.Holds t x)] {fuel : ℕ} {c : Code I.Op I.Test} {x y : I.Data} {n T : ℕ} (h : Code.eval I fuel c x = some (y, n)) (hn : n ≤ T) :
ExecWithin I c x y T

An interpreter result within the budget is an execution within it.