File size: 4,973 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
import Mathlib
import OAI.Computability.QuantumFactoring.BitStackDivision

namespace OAI



section

namespace ExactQuantumFactoring.BitStackProgram

def intData (z : ℤ) : Bool×ℕ := (decide (z<0),z.natAbs)
def intCode (z : ℤ) : List Bool := prodCode Procedure.boolCode Nat.bits (intData z)
def intOfSign (x : Bool×ℕ) : ℤ := if x.1 then -(x.2 : ℤ) else (x.2 : ℤ)

lemma intOfSign_data (z : ℤ) : intOfSign (intData z)=z := by
  by_cases h : z<0
  · simp only [intData,intOfSign,h,decide_true,ite_true]
    exact (Int.eq_neg_natAbs_of_nonpos (le_of_lt h)).symm
  · simp only [intData,intOfSign,h,decide_false,Bool.false_eq_true,ite_false]
    exact Int.natAbs_of_nonneg (le_of_not_gt h)

lemma intData_ofSign (x : Bool×ℕ) :
    intData (intOfSign x)=(x.1 && decide (x.2≠0),x.2) := by
  rcases x with ⟨s,n⟩
  cases s <;> simp [intData,intOfSign,Nat.pos_iff_ne_zero]

def signedAdd (x : (Bool×ℕ)×(Bool×ℕ)) : Bool×ℕ :=
  if x.1.1=x.2.1 then (x.1.1,x.1.2+x.2.2)
  else if x.1.2≤x.2.2 then (x.2.1,x.2.2-x.1.2) else (x.1.1,x.1.2-x.2.2)

lemma signedAdd_value (x : (Bool×ℕ)×(Bool×ℕ)) :
    intOfSign (signedAdd x)=intOfSign x.1+intOfSign x.2 := by
  rcases x with ⟨⟨s,a⟩,⟨t,b⟩⟩
  cases s <;> cases t <;> simp only [signedAdd,intOfSign,Bool.false_eq_true,
    Bool.true_eq_false,ite_true,ite_false,Int.natCast_add]
  all_goals first | omega | (split <;> simp only [ite_true,Bool.false_eq_true,ite_false] <;> omega)

lemma signedMul_value (x : (Bool×ℕ)×(Bool×ℕ)) :
    intOfSign (xor x.1.1 x.2.1,x.1.2*x.2.2)=intOfSign x.1*intOfSign x.2 := by
  rcases x with ⟨⟨s,a⟩,⟨t,b⟩⟩
  cases s <;> cases t <;> simp [intOfSign]

lemma intOfSign_not (x : Bool×ℕ) : intOfSign (!x.1,x.2) = -intOfSign x := by
  rcases x with ⟨s,n⟩
  cases s <;> simp [intOfSign]

namespace Procedure
noncomputable def intSign : Procedure intCode boolCode (fun z=>decide (z<0)) :=
  (first boolCode Nat.bits).precompose intData
noncomputable def intAbs : Procedure intCode Nat.bits Int.natAbs :=
  (second boolCode Nat.bits).precompose intData

noncomputable def signedInt : Procedure (prodCode boolCode Nat.bits) intCode intOfSign := by
  let s:=first boolCode Nat.bits
  let a:=second boolCode Nat.bits
  let nz:=boolNot.comp (binaryZero.comp a)
  exact ((boolAnd.comp (s.pair nz)).pair a).result (by
    intro x
    change prodCode boolCode Nat.bits (x.1 && !decide (x.2=0),x.2)=intCode (intOfSign x)
    rw [intCode,intData_ofSign]
    simp only [decide_not])

noncomputable def natToInt : Procedure Nat.bits intCode (fun n=>(n : ℤ)) :=
  (signedInt.comp ((constant Nat.bits boolCode false).pair (identity Nat.bits))).congrFun (by intro n;rfl)

noncomputable def intNeg : Procedure intCode intCode Neg.neg :=
  (signedInt.comp ((boolNot.comp intSign).pair intAbs)).congrFun (by
    intro z
    change intOfSign (!(intData z).1,(intData z).2) = -z
    rw [intOfSign_not,intOfSign_data])

noncomputable def signedAddP : Procedure
    (prodCode (prodCode boolCode Nat.bits) (prodCode boolCode Nat.bits))
    (prodCode boolCode Nat.bits) signedAdd := by
  let a:=first (prodCode boolCode Nat.bits) (prodCode boolCode Nat.bits)
  let b:=second (prodCode boolCode Nat.bits) (prodCode boolCode Nat.bits)
  let s:=(first boolCode Nat.bits).comp a
  let t:=(first boolCode Nat.bits).comp b
  let m:=(second boolCode Nat.bits).comp a
  let n:=(second boolCode Nat.bits).comp b
  let eqs:=boolNot.comp (boolXor.comp (s.pair t))
  let same:=s.pair (binaryAdd.comp (m.pair n))
  let le:=binaryLe.comp (m.pair n)
  let small:=t.pair (binarySub.comp (n.pair m))
  let large:=s.pair (binarySub.comp (m.pair n))
  exact (conditional eqs same (conditional le small large)).congrFun (by
    rintro ⟨⟨s,m⟩,⟨t,n⟩⟩
    cases s <;> cases t <;> simp [signedAdd,Function.comp_apply])

noncomputable def intAdd : Procedure (prodCode intCode intCode) intCode (fun x=>x.1+x.2) :=
  ((signedInt.comp signedAddP).precompose (fun x : ℤ×ℤ=>(intData x.1,intData x.2))).congrFun
    (by intro x;change intOfSign (signedAdd (intData x.1,intData x.2))=x.1+x.2
        rw [signedAdd_value,intOfSign_data,intOfSign_data])

noncomputable def intSub : Procedure (prodCode intCode intCode) intCode (fun x=>x.1-x.2) :=
  (intAdd.comp ((first intCode intCode).pair (intNeg.comp (second intCode intCode)))).congrFun
    (by intro x;rfl)

noncomputable def intMul : Procedure (prodCode intCode intCode) intCode (fun x=>x.1*x.2) := by
  let a:=first intCode intCode
  let b:=second intCode intCode
  let sign:=boolXor.comp ((intSign.comp a).pair (intSign.comp b))
  let mag:=binaryMul.comp ((intAbs.comp a).pair (intAbs.comp b))
  exact (signedInt.comp (sign.pair mag)).congrFun (by
    intro x
    change intOfSign (xor (intData x.1).1 (intData x.2).1,
      (intData x.1).2*(intData x.2).2)=x.1*x.2
    rw [signedMul_value (intData x.1,intData x.2),intOfSign_data,intOfSign_data])
end Procedure
end ExactQuantumFactoring.BitStackProgram

end



end OAI