File size: 5,124 Bytes
2f887db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import Mathlib
import OAI.Computability.UniqueGames.Model

namespace OAI

namespace PerfectCompleteness.BinaryFormula

structure Literal where
  name : Nat
  positive : Bool
  deriving DecidableEq

def Literal.eval (literal : Literal) (assignment : Nat → Bool) : Bool :=
  if literal.positive then assignment literal.name else !(assignment literal.name)

abbrev Clause := Vector Literal 3

def Clause.eval (clause : Clause) (assignment : Nat → Bool) : Bool :=
  (clause[0].eval assignment || clause[1].eval assignment) || clause[2].eval assignment

structure Formula where
  clauses : List Clause

def Formula.Satisfiable (F : Formula) : Prop :=
  ∃ assignment : Nat → Bool, ∀ clause ∈ F.clauses, clause.eval assignment = true

end PerfectCompleteness.BinaryFormula

namespace PerfectCompleteness.BinaryEncoding

open PerfectCompleteness.BinaryFormula

def bitsValue : List Bool → Nat
  | [] => 0
  | b :: bits => Nat.bit b (bitsValue bits)

def parseFrame : List Bool → Option (List Bool × List Bool)
  | false :: rest => some ([], rest)
  | true :: b :: rest => do
      let (bits, trailing) ← parseFrame rest
      return (b :: bits, trailing)
  | _ => none

def parseName (input : List Bool) : Option (Nat × List Bool) := do
  let (digits, rest) ← parseFrame input
  let name := bitsValue digits
  if digits = name.bits then some (name, rest) else none

def parseLiteral : List Bool → Option (Literal × List Bool)
  | sign :: input => do
      let (name, rest) ← parseName input
      return (⟨name, sign⟩, rest)
  | [] => none

def parseClause (input : List Bool) : Option (Clause × List Bool) := do
  let (a, input) ← parseLiteral input
  let (b, input) ← parseLiteral input
  let (c, rest) ← parseLiteral input
  return (#v[a, b, c], rest)

def parseClauses : Nat → List Bool → Option (List Clause × List Bool)
  | 0, _ => none
  | _ + 1, false :: rest => some ([], rest)
  | fuel + 1, true :: input => do
      let (clause, input) ← parseClause input
      let (clauses, rest) ← parseClauses fuel input
      return (clause :: clauses, rest)
  | _ + 1, [] => none

def decodeFormula (input : List Bool) : Option Formula := do
  let (clauses, rest) ← parseClauses (input.length + 1) input
  if rest = [] then some ⟨clauses⟩ else none

end PerfectCompleteness.BinaryEncoding

namespace PerfectCompleteness.BinaryLanguage

def language (input : List Bool) : Prop :=
  ∃ formula, BinaryEncoding.decodeFormula input = some formula ∧ formula.Satisfiable

end PerfectCompleteness.BinaryLanguage

namespace PerfectCompleteness

structure ProjectionTable (q : Nat) where
  images : Vector (Fin q) (2 * q)
  exactlyTwo : ∀ b : Fin q,
    ((List.finRange (2 * q)).filter (fun a => decide (images[a] = b))).length = 2

structure Edge (leftVertices rightVertices q : Nat) where
  left : Fin leftVertices
  right : Fin rightVertices
  projection : ProjectionTable q

structure Instance (q : Nat) where
  leftVertices : Nat
  rightVertices : Nat
  edges : List (Edge leftVertices rightVertices q)
  nonempty : edges ≠ []

abbrev Labeling {q : Nat} (G : Instance q) :=
  (Fin G.leftVertices → Fin (2 * q)) × (Fin G.rightVertices → Fin q)

def Edge.satisfied {l r q : Nat} (e : Edge l r q)
    (a : Fin l → Fin (2 * q)) (b : Fin r → Fin q) : Bool :=
  decide (e.projection.images[a e.left] = b e.right)

def countSatisfied {l r q : Nat} (a : Fin l → Fin (2 * q))
    (b : Fin r → Fin q) : List (Edge l r q) → Nat
  | [] => 0
  | e :: es => (if e.satisfied a b then 1 else 0) + countSatisfied a b es

end PerfectCompleteness

namespace PerfectCompleteness.Instance

def maxSatisfied {q : Nat} (G : Instance q) : Nat :=
  Finset.univ.sup (fun s : Labeling G => countSatisfied s.1 s.2 G.edges)

noncomputable def value {q : Nat} (G : Instance q) : ℝ :=
  (G.maxSatisfied : ℝ) / G.edges.length

end PerfectCompleteness.Instance

namespace PerfectCompleteness.Encoding

open UniqueGamesTheorem.Foundations.Complexity

def tableWords {q : Nat} (table : ProjectionTable q) : List Nat :=
  table.images.toList.map Fin.val

def edgeWords {l r q : Nat} (edge : Edge l r q) : List Nat :=
  [edge.left.val, edge.right.val] ++ tableWords edge.projection

def gameWords {q : Nat} (game : Instance q) : List Nat :=
  [game.leftVertices, game.rightVertices, q, game.edges.length] ++
    game.edges.flatMap edgeWords

def gameBits {q : Nat} (game : Instance q) : List Bool :=
  encodeWords (gameWords game)

end PerfectCompleteness.Encoding

namespace PerfectCompleteness

open Turing UniqueGamesTheorem.Foundations.Complexity

structure BinaryGapReduction (δ : ℚ) where
  alphabet : Nat
  alphabet_ge_two : 2 ≤ alphabet
  construct : List Bool → Instance alphabet
  computation : TM2ComputableInPolyTime (id : List Bool → List Bool)
    Encoding.gameBits construct
  finiteAlphabet : MachineFiniteAlphabet.FiniteAlphabet computation.tm
  completeness : ∀ input, BinaryLanguage.language input → (construct input).value = 1
  soundness : ∀ input, ¬BinaryLanguage.language input → (construct input).value ≤ (δ : ℝ)

end PerfectCompleteness

end OAI