File size: 4,397 Bytes
4a56cd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import OAI.MathematicalPhysics.RapidForcing.RatOperations
import Mathlib.Computability.PartrecCode

namespace OAI

namespace FixedClauseThreshold.Computability

open Filter
open scoped Topology
open RapidForcing.EffectiveArithmetic

def tolerance (r : β„•) : β„š := (2 ^ r : β„š)⁻¹

@[fun_prop]
theorem tolerance_computable : Computable tolerance := by
  unfold tolerance
  fun_prop

theorem tolerance_pos (r : β„•) : 0 < tolerance r := by
  unfold tolerance
  positivity

def certificateTest (lower upper : β„• β†’ β„š) (valid : β„• β†’ Bool)
    (r n : β„•) : Option β„š :=
  let i := n.unpair.1
  let j := n.unpair.2
  if valid j = true ∧ upper j - lower i ≀ 2 * tolerance r then
    some ((lower i + upper j) / 2)
  else none

theorem certificateTest_computable {lower upper : β„• β†’ β„š} {valid : β„• β†’ Bool}
    (hl : Computable lower) (hu : Computable upper) (hv : Computable valid) :
    Computableβ‚‚ (certificateTest lower upper valid) := by
  unfold Computableβ‚‚ certificateTest
  fun_prop

def certificateProgram (lower upper : β„• β†’ β„š) (valid : β„• β†’ Bool)
    (r : β„•) : Part β„š :=
  Nat.rfindOpt (certificateTest lower upper valid r)

theorem certificateProgram_partrec {lower upper : β„• β†’ β„š} {valid : β„• β†’ Bool}
    (hl : Computable lower) (hu : Computable upper) (hv : Computable valid) :
    Partrec (certificateProgram lower upper valid) :=
  Partrec.rfindOpt (certificateTest_computable hl hu hv)

theorem certificateTest_sound {Ξ± : ℝ} {lower upper : β„• β†’ β„š} {valid : β„• β†’ Bool}
    (hl : βˆ€ i, (lower i : ℝ) ≀ Ξ±)
    (hu : βˆ€ j, valid j = true β†’ Ξ± ≀ (upper j : ℝ))
    {r n : β„•} {q : β„š} (hq : q ∈ certificateTest lower upper valid r n) :
    |(q : ℝ) - Ξ±| ≀ (tolerance r : ℝ) := by
  dsimp only [certificateTest] at hq
  split_ifs at hq with h
  Β· obtain rfl := Option.mem_some_iff.mp hq
    have hlo := hl n.unpair.1
    have hup := hu n.unpair.2 h.1
    have hwidth : (upper n.unpair.2 : ℝ) - lower n.unpair.1 ≀
        2 * (tolerance r : ℝ) := by exact_mod_cast h.2
    push_cast
    apply abs_le.mpr
    constructor <;> linarith
  Β· contradiction

theorem certificateProgram_total {Ξ± : ℝ} {lower upper : β„• β†’ β„š} {valid : β„• β†’ Bool}
    (hl : βˆ€ Ξ΅ : ℝ, 0 < Ξ΅ β†’ βˆƒ i, Ξ± - Ξ΅ < (lower i : ℝ))
    (hu : βˆ€ Ξ΅ : ℝ, 0 < Ξ΅ β†’ βˆƒ j, valid j = true ∧ (upper j : ℝ) < Ξ± + Ξ΅)
    (r : β„•) : (certificateProgram lower upper valid r).Dom := by
  have hΞ΅ : (0 : ℝ) < tolerance r := by exact_mod_cast tolerance_pos r
  obtain ⟨i, hi⟩ := hl (tolerance r) hΡ
  obtain ⟨j, hj, hj'⟩ := hu (tolerance r) hΡ
  apply Nat.rfindOpt_dom.mpr
  refine ⟨Nat.pair i j, (lower i + upper j) / 2, ?_⟩
  have hwidth : upper j - lower i ≀ 2 * tolerance r := by
    have : (upper j : ℝ) - lower i ≀ 2 * (tolerance r : ℝ) := by linarith
    exact_mod_cast this
  simp [certificateTest, hj, hwidth]

theorem finite_certificate_search {Ξ± : ℝ}
    {lower upper : β„• β†’ β„š} {valid : β„• β†’ Bool}
    (hlc : Computable lower) (huc : Computable upper) (hvc : Computable valid)
    (hls : βˆ€ i, (lower i : ℝ) ≀ Ξ±)
    (hus : βˆ€ j, valid j = true β†’ Ξ± ≀ (upper j : ℝ))
    (hld : βˆ€ Ξ΅ : ℝ, 0 < Ξ΅ β†’ βˆƒ i, Ξ± - Ξ΅ < (lower i : ℝ))
    (hud : βˆ€ Ξ΅ : ℝ, 0 < Ξ΅ β†’ βˆƒ j, valid j = true ∧ (upper j : ℝ) < Ξ± + Ξ΅) :
    βˆƒ q : β„• β†’ β„š, Computable q ∧
      (βˆ€ r, q r ∈ certificateProgram lower upper valid r) ∧
      βˆ€ r, |(q r : ℝ) - Ξ±| ≀ (2 ^ r : ℝ)⁻¹ := by
  let total := certificateProgram_total hld hud
  let q := fun r => (certificateProgram lower upper valid r).get (total r)
  have hq (r : β„•) : q r ∈ certificateProgram lower upper valid r := Part.get_mem _
  refine ⟨q, (certificateProgram_partrec hlc huc hvc).of_eq_tot hq, hq, ?_⟩
  intro r
  obtain ⟨n, hn⟩ := Nat.rfindOpt_spec (hq r)
  simpa only [tolerance, Rat.cast_inv, Rat.cast_pow, Rat.cast_ofNat] using
    certificateTest_sound hls hus hn

theorem rational_sequence_code {q : β„• β†’ β„š} (hq : Computable q) :
    βˆƒ c : Nat.Partrec.Code, βˆ€ r,
      Nat.Partrec.Code.eval c r = Part.some (Encodable.encode (q r)) := by
  have hencode := Computable.encode.comp hq
  obtain ⟨c, hc⟩ := Nat.Partrec.Code.exists_code.mp (Partrec.nat_iff.mp hencode)
  exact ⟨c, fun r => congrFun hc r⟩

end FixedClauseThreshold.Computability

end OAI