File size: 4,258 Bytes
8ca48d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/-
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 $2/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