File size: 5,655 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
import OAI.Computability.PerfectCompleteness.Machines.CanonicalKeyMachine
import OAI.Computability.PerfectCompleteness.Reduction.SignedCompletionSchedule

namespace OAI

section

namespace PerfectCompleteness.DescriptorKeyTemplate

open CanonicalKeys CanonicalKeyShape MixedSupport MetadataFreeSampler ClauseSupport
open scoped Classical

noncomputable section

def occurrence : Slot → Nat
  | .clause c _ _ => c
  | .bit _ => 0

def variableIDs : Slot → Fin 3 → Nat
  | .clause _ ids _ => ids
  | .bit v => fun _ => v

@[simp] theorem actualSlot_erase (slot : Slot) :
    KeyMetadataMachine.actualSlot (erase slot) (occurrence slot) (variableIDs slot) = slot := by
  cases slot <;> rfl

structure Template (width : Nat) where
  shapes : Fin width → Shape
  modes : Fin width → SupportMode
  partition : Set (Set (Fin width → ReducedValue))

instance (width : Nat) : Finite (Template width) := by
  let : Finite ReducedValue := ReducedDomains.reducedValue_finite
  apply Finite.of_injective
    (fun template : Template width => (template.shapes, template.modes, template.partition))
  intro a b h
  cases a
  cases b
  cases h
  rfl

def ofInput {width k : Nat} (data : CanonicalKeyShape.Input width k) : Template width :=
  ⟨data.1, inputModes data, inputPartition data⟩

def recover {width : Nat} (side : Side) (template : Template width)
    (ids : Fin width → Nat) (vars : Fin width → Fin 3 → Nat) : Key width :=
  CanonicalKeyMachine.recoveredKey template.shapes template.modes side template.partition ids vars

theorem recover_ofInput {width k : Nat} (side : Side) (slots : Fin width → Slot)
    (data : CanonicalKeyShape.Input width k) (sameShape : data.1 = shape slots) :
    recover side (ofInput data) (fun i => occurrence (slots i)) (fun i => variableIDs (slots i)) =
      CanonicalKeyShapeMachine.recoverKey side slots data := by
  have restored : CanonicalKeyMachine.actualSlots data.1
      (fun i => occurrence (slots i)) (fun i => variableIDs (slots i)) = slots := by
    funext i
    rw [sameShape]
    exact actualSlot_erase (slots i)
  change CanonicalKeyMachine.recoveredKey data.1 (inputModes data) side
    (inputPartition data) _ _ = _
  unfold CanonicalKeyMachine.recoveredKey
  rw [restored]
  rfl

variable {branch : Nat → Nat} {n t q : Nat} {rows repeats : Nat → Nat}
    (hq : 0 < q)
    (large : CanonicalKeyEncoding.partitionWidth (TreeCanonical.locationCount branch n t) ≤ q)

def tapeInput (descriptor : SignedCompletionSchedule.Descriptor
    (rows := rows) (repeats := repeats) hq large) :
    MetadataFreeTape.Input branch n t rows repeats := ⟨descriptor.1, descriptor.2.1⟩

def ofTape (side : Side) (data : MetadataFreeTape.Input branch n t rows repeats) :
    Template (TreeCanonical.locationCount branch n t) :=
  match side with
  | .left => ofInput (MetadataFreeTape.keyInput data)
  | .right => ofInput (FixedQueryCodec.rightKeyInput data)

def ofDescriptor (side : Side) (descriptor : SignedCompletionSchedule.Descriptor
    (rows := rows) (repeats := repeats) hq large) :
    Template (TreeCanonical.locationCount branch n t) := ofTape side (tapeInput hq large descriptor)

theorem ofDescriptor_seed_independent (side : Side) (signs : SignTuple branch n t)
    (outcome : MetadataFreeLaw.SignedOutcome signs rows repeats)
    (a b : Fin (SignedCompletionSchedule.active hq large signs outcome)) :
    ofDescriptor hq large side ⟨signs, outcome, a⟩ =
      ofDescriptor hq large side ⟨signs, outcome, b⟩ := rfl

variable {v m : Nat}

theorem recover_left (clauses : Fin m → SourceClause.NormalizedClause v)
    (e : PreliminarySampler.Raw clauses branch n t rows repeats) :
    let slots := TreeCanonical.numberedSlots (sourceSlots clauses e.1.1)
    recover .left (ofTape .left (MetadataFreeTape.encodeRaw clauses rows repeats e))
        (fun i => occurrence (slots i)) (fun i => variableIDs (slots i)) =
      CanonicalKeys.key .left slots
        (TreeCanonical.numberedFunction (sourceSlots clauses e.1.1)
          (HierarchicalArrays.fullJoint
            (WholeArraySampler.evaluate rows repeats (GeometricPath.leafPath e.1.2)
              (sourceSlots clauses e.1.1) e.2.1))) := by
  dsimp only
  rw [ofTape, recover_ofInput]
  · exact MetadataTapeMachine.recoverKey_encodeRaw .left clauses e
  · rw [MetadataFreeTape.keyInput_encodeRaw]
    rfl

theorem recover_right (clauses : Fin m → SourceClause.NormalizedClause v)
    (e : PreliminarySampler.Raw clauses branch n t rows repeats) :
    let slots := TreeCanonical.numberedSlots (sourceSlots clauses e.1.1)
    recover .right (ofTape .right (MetadataFreeTape.encodeRaw clauses rows repeats e))
        (fun i => occurrence (slots i)) (fun i => variableIDs (slots i)) =
      CanonicalKeys.key .right slots
        (FixedQueryCodec.project (FixedQueryCodec.rawTestIndex clauses e) ∘
          TreeCanonical.numberedFunction (sourceSlots clauses e.1.1)
            (HierarchicalArrays.fullJoint
              (WholeArraySampler.evaluate rows repeats (GeometricPath.leafPath e.1.2)
                (sourceSlots clauses e.1.1) e.2.1))) := by
  dsimp only
  rw [ofTape, recover_ofInput]
  · exact FiniteConstraintMachine.recover_right_key clauses e
  · rw [FixedQueryCodec.rightKeyInput_encodeRaw]
    rfl

theorem writer_payload {width : Nat} (side : Side) (template : Template width)
    (ids : Fin width → Nat) (vars : Fin width → Fin 3 → Nat) :
    CanonicalKeyMachine.payload template.shapes template.modes side template.partition ids vars =
      CanonicalKeyEncoding.bits (recover side template ids vars) ++ [true] := rfl

end
end PerfectCompleteness.DescriptorKeyTemplate

end

end OAI