Datasets:
Download lean_project/FormalConjecturesForMathlib/Computability/Complexity.lean from nielsr/formal-conjectures: direct link, hf CLI and curl.
- Browser
- Download file 4.26 kB
-
https://huggingface.co/datasets/nielsr/formal-conjectures/resolve/main/lean_project/FormalConjecturesForMathlib/Computability/Complexity.lean
- Command line
-
hf download hf://datasets/nielsr/formal-conjectures/lean_project/FormalConjecturesForMathlib/Computability/Complexity.lean
-
curl -L -o Complexity.lean https://huggingface.co/datasets/nielsr/formal-conjectures/resolve/main/lean_project/FormalConjecturesForMathlib/Computability/Complexity.lean
4.26 kB
| /- | |
| Copyright 2026 The Formal Conjectures Authors. | |
| Licensed under the Apache License, Version 2.0 (the "License"); | |
| you may not use this file except in compliance with the License. | |
| You may obtain a copy of the License at | |
| https://www.apache.org/licenses/LICENSE-2.0 | |
| Unless required by applicable law or agreed to in writing, software | |
| distributed under the License is distributed on an "AS IS" BASIS, | |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| See the License for the specific language governing permissions and | |
| limitations under the License. | |
| -/ | |
| module | |
| public import FormalConjecturesForMathlib.Computability.BitstringEncoding | |
| public import Mathlib.Computability.TuringMachine.Computable | |
| /-! | |
| # Complexity Classes | |
| This file contains formal definitions of notions from complexity theory, | |
| including the complexity classes P, NP, coNP, and BPP. | |
| *References:* | |
| - Sanjeev Arora and Boaz Barak. Computational Complexity: A Modern Approach. | |
| Cambridge University Press, 2009. | |
| - Melissa Antonelli, Ugo Dal Lago, and Paolo Pistone. On Randomized Computational Models | |
| and Complexity Classes: a Historical Overview. 2024. | |
| [arXiv:2409.11999](https://arxiv.org/abs/2409.11999) | |
| - [Wikipedia: Complexity class](https://en.wikipedia.org/wiki/Complexity_class) | |
| -/ | |
| @[expose] public section | |
| open Computability Turing | |
| namespace ComplexityTheory | |
| /-- | |
| The type of decision problems. | |
| We define these as functions from lists of booleans to booleans, | |
| implicitly assuming the usual encodings. | |
| -/ | |
| abbrev DecisionProblem := List Bool → Bool | |
| /-- | |
| The type of complexity classes. We define these as sets of decision problems. | |
| -/ | |
| abbrev DecisionComplexityClass := Set DecisionProblem | |
| /-- | |
| `IsPolyTimeWithEncoding ea eb f` asserts that `f` is computable in polynomial time | |
| when its input and output are encoded via the given `Encoding`s `ea` and `eb`. | |
| -/ | |
| def IsPolyTimeWithEncoding {α β Γα Γβ : Type} (ea : Encoding α Γα) (eb : Encoding β Γβ) | |
| (f : α → β) := | |
| Nonempty (TM2ComputableInPolyTime ea.encode eb.encode f) | |
| /-- | |
| A function is polynomial-time computable when it is `IsPolyTimeWithEncoding` | |
| for the canonical `Bool`-alphabet encodings of its domain and codomain | |
| as given by the `BitstringEncoding` typeclass. | |
| -/ | |
| def IsPolyTime {α β : Type} [BitstringEncoding α] [BitstringEncoding β] (f : α → β) : Prop := | |
| IsPolyTimeWithEncoding (BitstringEncoding.toEncoding (α := α)) (BitstringEncoding.toEncoding (α := β)) f | |
| /-- The identity function is polynomial-time computable. -/ | |
| theorem isPolyTime_id {α : Type} [BitstringEncoding α] : IsPolyTime (id : α → α) := | |
| ⟨Turing.idComputableInPolyTime BitstringEncoding.bitEncode⟩ | |
| /- ## Class definitions -/ | |
| /-- | |
| The class P is the set of decision problems | |
| decidable in polynomial time by a deterministic Turing machine. | |
| -/ | |
| def P : DecisionComplexityClass := | |
| { L | IsPolyTime L } | |
| /-- | |
| The class NP is the set of decision problems $L$ | |
| such that there exists a polynomial $p$ over ℕ | |
| and a poly-time Turing machine computing a relation $R$ | |
| where for all $x$, $L(x)$ iff there exists a $w$ of length at most $p (|x|)$ | |
| such that $R(x,w)$ accepts. | |
| See Definition 2.1 in Arora-Barak (2009). | |
| -/ | |
| def NP : DecisionComplexityClass := | |
| { L | ∃ (p : Polynomial ℕ), ∃ R : (List Bool × List Bool) → Bool, | |
| IsPolyTime R ∧ | |
| ∀ x, L x ↔ ∃ w : List Bool, w.length ≤ p.eval x.length ∧ R (x, w) } | |
| /-- | |
| The class coNP is the set of decision problems | |
| whose complements are in NP. | |
| -/ | |
| def coNP : DecisionComplexityClass := | |
| { L | Lᶜ ∈ NP } | |
| /-- | |
| The class BPP is the set of decision problems $L$ | |
| such that there exists a polynomial $p$ over ℕ | |
| and a poly-time Turing machine computing a relation $R$ | |
| where for all $x$, a uniformly random string $r$ of length $p (|x|)$ satisfies | |
| $R (x, r) = L (x)$ with probability at least /3$. | |
| See Definition 7.4 in Arora-Barak (2009), or Antonelli et al. (2024). | |
| -/ | |
| def BPP : DecisionComplexityClass := | |
| { L | ∃ (p : Polynomial ℕ), ∃ R : (List Bool × List Bool) → Bool, | |
| IsPolyTime R ∧ | |
| ∀ x, (2 : ℚ) / 3 ≤ | |
| ((Finset.univ.filter fun r : Fin (p.eval x.length) → Bool => | |
| R (x, List.ofFn r) = L x).card : ℚ) / 2 ^ p.eval x.length } | |
| end ComplexityTheory | |