While this submission is a draft, it cannot be used by other submissions.

Structured programs and exact execution costs

Lax350013.StructuredPrograms · concepts/Lax350013/StructuredPrograms.lean · lax-350013

definition

Loading review…

Sign in with ORCID

Community review

Flags

Each flag is tied to a public ORCID identity and explains why this concept may be incorrect.

No flags have been submitted.

    Community review

    Flag this concept

    State precisely what appears incorrect. This explanation will be public under your ORCID name.

    No source line selected.

    Natural Language Statement

    Definition

    The imperative language used to construct the algorithms: finite programs, procedure calls, memory, word and stack limits, and an exact step-counted execution relation. The upstream compiler proves that these programs run on the word RAM with constant-factor overhead.

    Concept map
    1 concept; 8 descendants hidden
    100%
    Proven claimDefinitionThis conceptRelated conceptA → B: B builds on A

    Lean source view on GitHub

    1/-
    2Copyright (c) 2026 Anthropic, PBC. All rights reserved.
    3Released under Apache 2.0 license as described in the file LICENSE.
    4SPDX-License-Identifier: Apache-2.0
    5-/
    6/-
    7Modified for the independent Lax packaging by Édouard Bonnet, 2026.
    8Derived from 3sum-apsp/ThreeSumApsp/Lang/Syntax.lean at upstream commit e1a4e6508154ea59f030480661590a9fe3018011.
    9Changes: Lax module/namespace layout, separated concepts and proofs, archive
    10annotations, and compatibility with the archive Lean/mathlib environment.
    11See NOTICE and README.md in the submission root for provenance and scope.
    12-/
    13
    14import Mathlib.Algebra.MvPolynomial.Basic
    15import Mathlib.Analysis.SpecialFunctions.Log.Base
    16import Mathlib.Analysis.SpecialFunctions.Log.Basic
    17import Mathlib.Analysis.SpecialFunctions.Pow.Real
    18import Mathlib.Combinatorics.SimpleGraph.Basic
    19import Mathlib.Data.Finset.Sort
    20import Mathlib.LinearAlgebra.Matrix.Notation
    21import Mathlib.MeasureTheory.Integral.Bochner.Basic
    22import Mathlib.NumberTheory.PrimeCounting
    23import Mathlib.Probability.Independence.Basic
    24import Mathlib.Tactic.DeriveFintype
    25
    26/-!
    27---
    28title: Structured programs and exact execution costs
    29type: definition
    30---
    31The imperative language used to construct the algorithms: finite programs, procedure calls, memory, word and stack limits, and an exact step-counted execution relation. The upstream compiler proves that these programs run on the word RAM with constant-factor overhead.
    32-/
    33
    34namespace Lax350013.StructuredPrograms
    35
    36open Finset
    37
    38/-- The operations on two words. -/
    39inductive Op : Type
    40 | add | sub | mul
    41 deriving DecidableEq, Repr
    42
    43/-- The result of an operation. -/
    44def Op.eval : Op → ℤ → ℤ → ℤ
    45 | .add, a, b => a + b
    46 | .sub, a, b => a - b
    47 | .mul, a, b => a * b
    48
    49/-- Expressions: constants, local variables, operations, and the content of the memory cell at an
    50address. -/
    51inductive Expr : Type
    52 | const (n : ℕ)
    53 | var (x : ℕ)
    54 | op (o : Op) (a b : Expr)
    55 | load (a : Expr)
    56 deriving Repr
    57
    58/-- Tests. -/
    59inductive Cond : Type
    60 | lt (a b : Expr)
    61 | eq (a b : Expr)
    62 deriving Repr
    63
    64/-- Statements. `call p args x` runs procedure number p on the values of args and puts its result
    65into x. -/
    66inductive Stmt : Type
    67 | skip
    68 | set (x : ℕ) (e : Expr)
    69 | store (a e : Expr)
    70 | seq (s₁ s₂ : Stmt)
    71 | ite (c : Cond) (s₁ s₂ : Stmt)
    72 | while (c : Cond) (s : Stmt)
    73 | call (p : ℕ) (args : List Expr) (x : ℕ)
    74 deriving Repr
    75
    76/-- A program: the bodies of its procedures, numbered from 0. -/
    77abbrev Program : Type := List Stmt
    78
    79/-- What a running procedure sees: its local variables and the memory. -/
    80structure State : Type where
    81 loc : ℕ → ℤ
    82 mem : ℕ → ℤ
    83
    84/-- The limits on a run: largest absolute value of a word, number of memory cells, nesting depth of
    85calls. -/
    86structure Limits : Type where
    87 word : ℤ
    88 space : ℕ
    89 depth : ℕ
    90
    91/-- The value of an expression. -/
    92def Expr.val (s : State) : Expr → ℤ
    93 | .const n => n
    94 | .var x => s.loc x
    95 | .op o a b => o.eval (a.val s) (b.val s)
    96 | .load a => s.mem (a.val s).toNat
    97
    98/-- The number of steps that evaluating an expression takes: one for each of its constants,
    99variables, operations, loads. -/
    100def Expr.cost : Expr → ℕ
    101 | .const _ => 1
    102 | .var _ => 1
    103 | .op _ a b => a.cost + b.cost + 1
    104 | .load a => a.cost + 1
    105
    106/-- An address within the limits. -/
    107def Limits.Addr (lim : Limits) (a : ℤ) : Prop := 0 ≤ a ∧ a < lim.space
    108
    109/-- The evaluation of an expression stays within the limits: every value that is formed fits in a
    110word, and every address that is read is within the memory. -/
    111def Expr.Safe (lim : Limits) (s : State) : Expr → Prop
    112 | .const n => (n : ℤ) ≤ lim.word
    113 | .var _ => True
    114 | .op o a b => a.Safe lim s ∧ b.Safe lim s ∧ |o.eval (a.val s) (b.val s)| ≤ lim.word
    115 | .load a => a.Safe lim s ∧ lim.Addr (a.val s)
    116
    117/-- Whether a test holds. -/
    118def Cond.Holds (s : State) : Cond → Prop
    119 | .lt a b => a.val s < b.val s
    120 | .eq a b => a.val s = b.val s
    121
    122/-- The number of steps of a test: its two sides and the comparison. -/
    123def Cond.cost : Cond → ℕ
    124 | .lt a b => a.cost + b.cost + 1
    125 | .eq a b => a.cost + b.cost + 1
    126
    127/-- The evaluation of a test stays within the limits. -/
    128def Cond.Safe (lim : Limits) (s : State) : Cond → Prop
    129 | .lt a b => a.Safe lim s ∧ b.Safe lim s
    130 | .eq a b => a.Safe lim s ∧ b.Safe lim s
    131
    132/-- The local variables of a procedure that has just been called: the arguments in 0, 1, …, and 0 in
    133all others. -/
    134def frame (args : List ℤ) : ℕ → ℤ := fun i => args.getD i 0
    135
    136/-- The memory that holds the list ws in the cells 0, 1, 2, … and 0 elsewhere. -/
    137def memOf (ws : List ℤ) : ℕ → ℤ := fun a => ws.getD a 0
    138
    139/-- `Exec lim P d s σ σ' c`: within the limits lim, and at nesting depth d of calls, the statement
    140s of the program P, started in the state σ, ends in the state σ' after exactly c steps. -/
    141inductive Exec (lim : Limits) (P : Program) : ℕ → Stmt → State → State → ℕ → Prop
    142 | skip {d σ} : Exec lim P d .skip σ σ 0
    143 | set {d σ x e} : e.Safe lim σ →
    144 Exec lim P d (.set x e) σ { σ with loc := Function.update σ.loc x (e.val σ) } (e.cost + 1)
    145 | store {d σ a e} : a.Safe lim σ → e.Safe lim σ → lim.Addr (a.val σ) →
    146 Exec lim P d (.store a e) σ { σ with mem := Function.update σ.mem (a.val σ).toNat (e.val σ) }
    147 (a.cost + e.cost + 1)
    148 | seq {d σ σ' σ'' s₁ s₂ c₁ c₂} : Exec lim P d s₁ σ σ' c₁ → Exec lim P d s₂ σ' σ'' c₂ →
    149 Exec lim P d (.seq s₁ s₂) σ σ'' (c₁ + c₂)
    150 | iteTrue {d σ σ' c s₁ s₂ k} : c.Safe lim σ → c.Holds σ → Exec lim P d s₁ σ σ' k →
    151 Exec lim P d (.ite c s₁ s₂) σ σ' (c.cost + 1 + k)
    152 | iteFalse {d σ σ' c s₁ s₂ k} : c.Safe lim σ → ¬ c.Holds σ → Exec lim P d s₂ σ σ' k →
    153 Exec lim P d (.ite c s₁ s₂) σ σ' (c.cost + 1 + k)
    154 | whileFalse {d σ c s} : c.Safe lim σ → ¬ c.Holds σ → Exec lim P d (.while c s) σ σ (c.cost + 1)
    155 | whileTrue {d σ σ' σ'' c s k₁ k₂} : c.Safe lim σ → c.Holds σ → Exec lim P d s σ σ' k₁ →
    156 Exec lim P d (.while c s) σ' σ'' k₂ → Exec lim P d (.while c s) σ σ'' (c.cost + 1 + k₁ + k₂)
    157 | call {d σ σ' p args x body k} : (∀ e ∈ args, e.Safe lim σ) → P[p]? = some body → d < lim.depth →
    158 Exec lim P (d + 1) body ⟨frame (args.map (·.val σ)), σ.mem⟩ σ' k →
    159 Exec lim P d (.call p args x) σ ⟨Function.update σ.loc x (σ'.loc 0), σ'.mem⟩
    160 ((args.map Expr.cost).sum + 2 + k)
    161
    162/-- Everything held in a variable or in a cell fits in a word. -/
    163def State.Bounded (lim : Limits) (σ : State) : Prop :=
    164 (∀ x, |σ.loc x| ≤ lim.word) ∧ ∀ a, |σ.mem a| ≤ lim.word
    165
    166end Lax350013.StructuredPrograms
    167

    Discussion

    Ask a question or add context. Endorsements and structured flags are kept in the review panel above.

    Loading discussion…