File size: 4,454 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
import OAI.MathematicalPhysics.RapidForcing.RatPrimrec

namespace OAI

section
open Encodable Denumerable
namespace RapidForcing.EffectiveArithmetic

lemma computable_of_graph {A B : Type} [Primcodable A] [Primcodable B]
    (f : A β†’ B) (R : A β†’ B β†’ Prop) [DecidableRel R]
    (hR : PrimrecPred (fun p : A Γ— B => R p.1 p.2))
    (h : βˆ€ a b, R a b ↔ b = f a) : Computable f := by
  let test : A β†’ β„• β†’ Option B := fun a n =>
    (decode n).bind fun b => if R a b then some b else none
  have ht : Primrecβ‚‚ test :=
    Primrec.option_bind (Primrec.decode.comp Primrec.snd)
      (Primrec.ite (hR.comp ((Primrec.fst.comp Primrec.fst).pair Primrec.snd))
        (Primrec.option_some.comp Primrec.snd) (Primrec.const none)).toβ‚‚
  apply (Partrec.rfindOpt ht.to_comp).of_eq_tot
  intro a
  have hd : (Nat.rfindOpt (test a)).Dom := Nat.rfindOpt_dom.mpr
    ⟨encode (f a), f a, by simp [test, Encodable.encodek, (h a (f a)).mpr rfl]⟩
  have hm : (Nat.rfindOpt (test a)).get hd ∈ Nat.rfindOpt (test a) := ⟨hd, rfl⟩
  obtain ⟨n, hn⟩ := Nat.rfindOpt_spec hm
  have he : (Nat.rfindOpt (test a)).get hd = f a := by
    simp only [test, Option.mem_bind_iff] at hn
    obtain ⟨b, _, hb⟩ := hn
    split_ifs at hb with hab
    Β· obtain rfl := Option.mem_some_iff.mp hb
      exact (h a _).mp hab
    Β· contradiction
  exact he β–Έ hm

@[fun_prop] lemma primrec_rat_le : PrimrecPred (fun p : β„š Γ— β„š => p.1 ≀ p.2) := by
  exact (show PrimrecPred (fun p : β„š Γ— β„š => p.1.num * (p.2.den : β„€) ≀
      p.2.num * (p.1.den : β„€)) by fun_prop).of_eq (fun p => (Rat.le_iff _ _).symm)

@[fun_prop] lemma primrec_rat_lt : PrimrecPred (fun p : β„š Γ— β„š => p.1 < p.2) := by
  exact (PrimrecPred.not (primrec_rat_le.comp (Primrec.snd.pair Primrec.fst))).of_eq
    (fun p => not_le)

lemma rat_div_graph (n : β„€) (d : β„•) (q : β„š) (hd : d β‰  0) :
    q.num * (d : β„€) = n * (q.den : β„€) ↔ q = (n : β„š) / d := by
  have hq : (q.den : β„š) β‰  0 := by exact_mod_cast q.den_nz
  have hd' : (d : β„š) β‰  0 := by exact_mod_cast hd
  conv_rhs => rw [← Rat.num_div_den q, div_eq_div_iff hq hd']
  exact_mod_cast (Iff.rfl : q.num * (d : β„€) = n * (q.den : β„€) ↔ _)

@[fun_prop] lemma computable_rat_divNat : Computable (fun p : β„€ Γ— β„• => (p.1 : β„š) / p.2) := by
  apply computable_of_graph _ (fun p q =>
      if p.2 = 0 then q = 0 else q.num * (p.2 : β„€) = p.1 * (q.den : β„€))
  Β· have hi : PrimrecPred (fun p : (β„€ Γ— β„•) Γ— β„š =>
        (p.1.2 = 0 ∧ p.2 = 0) ∨ (p.1.2 β‰  0 ∧ p.2.num * (p.1.2 : β„€) = p.1.1 * (p.2.den : β„€))) := by fun_prop
    exact hi.of_eq (fun p => by split_ifs <;> simp_all)
  Β· intro p q
    split_ifs with hd
    Β· simp [hd]
    Β· exact rat_div_graph _ _ _ hd

@[fun_prop] lemma computable_rat_castInt : Computable (fun z : β„€ => (z : β„š)) := by
  simpa using computable_rat_divNat.comp (Computable.id.pair (Computable.const 1))

@[fun_prop] lemma computable_rat_castNat : Computable (fun n : β„• => (n : β„š)) := by
  simpa only [Int.cast_natCast] using computable_rat_castInt.comp primrec_int_cast.to_comp

@[fun_prop] lemma computable_rat_add : Computable (fun p : β„š Γ— β„š => p.1 + p.2) := by
  have hh : Primrec (fun p : β„š Γ— β„š =>
      (p.1.num * (p.2.den : β„€) + p.2.num * (p.1.den : β„€), p.1.den * p.2.den)) := by fun_prop
  apply (computable_rat_divNat.comp hh.to_comp).of_eq
  intro p
  dsimp; push_cast
  conv_rhs => rw [← Rat.num_div_den p.1, ← Rat.num_div_den p.2]
  have h1 : (p.1.den : β„š) β‰  0 := by exact_mod_cast p.1.den_nz
  have h2 : (p.2.den : β„š) β‰  0 := by exact_mod_cast p.2.den_nz
  field_simp

@[fun_prop] lemma computable_rat_mul : Computable (fun p : β„š Γ— β„š => p.1 * p.2) := by
  have hh : Primrec (fun p : β„š Γ— β„š => (p.1.num * p.2.num, p.1.den * p.2.den)) := by fun_prop
  apply (computable_rat_divNat.comp hh.to_comp).of_eq
  intro p
  dsimp; push_cast
  rw [mul_div_mul_comm, Rat.num_div_den, Rat.num_div_den]

@[fun_prop] lemma computable_rat_neg : Computable (fun q : β„š => -q) := by
  have hh : Primrec (fun q : β„š => (-q.num, q.den)) := by fun_prop
  apply (computable_rat_divNat.comp hh.to_comp).of_eq
  intro q
  simp only [Int.cast_neg, neg_div, Rat.num_div_den]

@[fun_prop] lemma computable_rat_sub : Computable (fun p : β„š Γ— β„š => p.1 - p.2) := by
  simpa only [sub_eq_add_neg] using (show Computable (fun p : β„š Γ— β„š => p.1 + -p.2) by fun_prop)

end RapidForcing.EffectiveArithmetic

end

end OAI