File size: 22,188 Bytes
b224933
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
import Mathlib

namespace OAI

noncomputable section

universe uA uB uC uF uR

namespace CPTSeparation

abbrev Scalar := ZMod 3

inductive Symbol
  | Ed | Cf | EB | VB | I | Z (δ : Scalar)
  deriving DecidableEq, Fintype

structure Input (A : Type) where
  rel : Symbol → A → A → Bool

namespace Input

variable {A B : Type} [Fintype A] [Fintype B]

abbrev Edge (S : Input A) := {y : A // S.rel .Ed y y = true}

abbrev Config (S : Input A) := {a : A // S.rel .Cf a a = true}

instance (S : Input A) : Fintype S.Edge := inferInstanceAs (Fintype (Subtype _))

instance (S : Input A) : Fintype S.Config := inferInstanceAs (Fintype (Subtype _))

def coefficient (S : Input A) (y : S.Edge) (a : S.Config) : Scalar :=
  ∑ x : S.Edge, if S.rel .I a x then
    ∑ δ : Scalar, if S.rel (.Z δ) y x then δ else 0
    else 0

def incident (S : Input A) (t : S.Config) (y : S.Edge) : Prop :=
  ∃ a : S.Config, S.rel .VB t a = true ∧
    ∃ x : S.Edge, S.rel .I a x = true ∧ S.rel .EB y x = true

def normalized (S : Input A) (lambda : S.Config → Scalar) : Prop :=
  ∀ t : S.Config, (∑ a : S.Config, if S.rel .VB t a then lambda a else 0) = 1

def consistent (S : Input A) (lambda : S.Config → Scalar) (mu : S.Edge → Scalar) : Prop :=
  ∀ (t : S.Config) (y : S.Edge), S.incident t y →
    mu y = ∑ a : S.Config, if S.rel .VB t a then lambda a * S.coefficient y a else 0

def query (S : Input A) : Prop :=
  ∃ (lambda : S.Config → Scalar) (mu : S.Edge → Scalar), S.normalized lambda ∧ S.consistent lambda mu

structure Iso (S : Input A) (T : Input B) extends A ≃ B where
  rel_eq : ∀ r x y, T.rel r (toEquiv x) (toEquiv y) = S.rel r x y

end Input

end CPTSeparation

namespace CPTSeparation.Hereditary

section

abbrev HF (A : Type uA) := Quotient (@Lists.instSetoidLists A)

variable {A : Type uA} {B : Type uB}

local instance instDecidableEqHF {A : Type uA} : DecidableEq (HF A) := Classical.decEq _

def mk (l : Lists A) : HF A := Quotient.mk Lists.instSetoidLists l

def atom (a : A) : HF A := mk (Lists.atom a)

@[simp] theorem mk_eq_mk {l r : Lists A} : mk l = mk r ↔ Lists.Equiv l r :=
  Quotient.eq

def isSet (x : HF A) : Bool := Quotient.liftOn x (fun l => l.1)
  (by intro l r h; cases h <;> rfl)

def rawElements (l : Lists A) : Finset (HF A) := by
  classical
  exact l.toList.toFinset.image mk

theorem mem_rawElements (a l : Lists A) : mk a ∈ rawElements l ↔ a ∈ l := by
  classical
  rcases l with ⟨(_ | _), l⟩
  · cases l
    change mk a ∈ (∅ : Finset (HF A)) ↔ False
    simp
  · change mk a ∈ (Lists'.toList l).toFinset.image mk ↔ a ∈ l
    simp only [Finset.mem_image, List.mem_toFinset, Lists'.mem_def, mk_eq_mk]
    constructor
    · rintro ⟨a',ha',heq⟩
      exact ⟨a',ha',heq.symm⟩
    · rintro ⟨a',ha',heq⟩
      exact ⟨a',ha',heq.symm⟩

theorem raw_mem_congr_right {l r : Lists A} (h : Lists.Equiv l r) (a : Lists A) :
    a ∈ l ↔ a ∈ r := by
  cases h with
  | refl => rfl
  | antisymm hl hr =>
    exact ⟨Lists'.mem_of_subset hl, Lists'.mem_of_subset hr⟩

theorem rawElements_congr {l r : Lists A} (h : Lists.Equiv l r) :
    rawElements l = rawElements r := by
  ext x
  induction x using Quotient.inductionOn with
  | _ a =>
    change mk a ∈ rawElements l ↔ mk a ∈ rawElements r
    rw [mem_rawElements, mem_rawElements]
    exact raw_mem_congr_right h a

def elements (x : HF A) : Finset (HF A) := Quotient.liftOn x rawElements (by
  have mem_rawElements (a l : Lists A) : mk a ∈ rawElements l ↔ a ∈ l := by
    classical
    rcases l with ⟨(_ | _), l⟩
    · cases l
      change mk a ∈ (∅ : Finset (HF A)) ↔ False
      simp
    · change mk a ∈ (Lists'.toList l).toFinset.image mk ↔ a ∈ l
      simp only [Finset.mem_image, List.mem_toFinset, Lists'.mem_def, mk, Quotient.eq]
      constructor
      · rintro ⟨a',ha',heq⟩
        exact ⟨a',ha',heq.symm⟩
      · rintro ⟨a',ha',heq⟩
        exact ⟨a',ha',heq.symm⟩
  have raw_mem_congr_right {l r : Lists A} (h : Lists.Equiv l r) (a : Lists A) :
      a ∈ l ↔ a ∈ r := by
    cases h with
    | refl => rfl
    | antisymm hl hr =>
      exact ⟨Lists'.mem_of_subset hl, Lists'.mem_of_subset hr⟩
  intro l r h
  ext z
  induction z using Quotient.inductionOn with
  | _ a =>
    change mk a ∈ rawElements l ↔ mk a ∈ rawElements r
    rw [mem_rawElements, mem_rawElements]
    exact raw_mem_congr_right h a)

instance : Membership (HF A) (HF A) := ⟨fun y x => x ∈ elements y⟩

@[simp] theorem mem_mk (a l : Lists A) : mk a ∈ mk l ↔ a ∈ l := mem_rawElements a l

def ofFinset (s : Finset (HF A)) : HF A :=
  mk (Lists.ofList (s.toList.map Quotient.out))

def rawRank' : {b : Bool} → Lists' A b → ℕ
  | _, .atom _ => 0
  | _, .nil => 0
  | _, .cons' a l => max (rawRank' a + 1) (rawRank' l)

def rawRank (l : Lists A) : ℕ := rawRank' l.2

theorem rawRank_lt_of_mem {b : Bool} (l : Lists' A b) (a : Lists A)
    (ha : a ∈ l.toList) : rawRank a < rawRank' l := by
  induction l with
  | atom => simp at ha
  | nil => simp at ha
  | cons' c l ihc ihl =>
    rcases List.mem_cons.mp ha with rfl | ha
    · exact lt_of_lt_of_le (Nat.lt_succ_self _) (le_max_left _ _)
    · exact lt_of_lt_of_le (ihl ha) (le_max_right _ _)

theorem rawRank_congr {l r : Lists A} (h : Lists.Equiv l r) : rawRank l = rawRank r := by
  induction h using Lists.Equiv.rec
      (motive_2 := fun l r _ => rawRank' l ≤ rawRank' r) with
  | refl => rfl
  | antisymm hl hr ihl ihr => exact Nat.le_antisymm ihl ihr
  | nil => exact Nat.zero_le _
  | @cons a a' l r heq hmem hsub iheq ihsub =>
    change max (rawRank a + 1) (rawRank' l) ≤ rawRank' r
    refine max_le ?_ ihsub
    rw [iheq]
    exact rawRank_lt_of_mem r a' hmem

def rank (x : HF A) : ℕ := Quotient.liftOn x rawRank (by
  have rawRank_lt_of_mem {b : Bool} (l : Lists' A b) (a : Lists A)
      (ha : a ∈ l.toList) : rawRank a < rawRank' l := by
    induction l with
    | atom => simp at ha
    | nil => simp at ha
    | cons' c l ihc ihl =>
      rcases List.mem_cons.mp ha with rfl | ha
      · exact lt_of_lt_of_le (Nat.lt_succ_self _) (le_max_left _ _)
      · exact lt_of_lt_of_le (ihl ha) (le_max_right _ _)
  have rawRank_congr {l r : Lists A} (h : Lists.Equiv l r) : rawRank l = rawRank r := by
    induction h using Lists.Equiv.rec
        (motive_2 := fun l r _ => rawRank' l ≤ rawRank' r) with
    | refl => rfl
    | antisymm hl hr ihl ihr => exact Nat.le_antisymm ihl ihr
    | nil => exact Nat.zero_le _
    | @cons a a' l r heq hmem hsub iheq ihsub =>
      change max (rawRank a + 1) (rawRank' l) ≤ rawRank' r
      refine max_le ?_ ihsub
      rw [iheq]
      exact rawRank_lt_of_mem r a' hmem
  exact fun _ _ h => rawRank_congr h)

theorem rank_lt_of_mem {x y : HF A} (h : x ∈ y) : rank x < rank y := by
  induction y using Quotient.inductionOn with
  | _ l =>
    induction x using Quotient.inductionOn with
    | _ a =>
      have h' := (mem_mk a l).mp h
      rcases l with ⟨(_ | _), l⟩
      · exact False.elim h'
      · obtain ⟨a',ha',heq⟩ := h'
        change rawRank a < rawRank' l
        rw [rawRank_congr heq]
        exact rawRank_lt_of_mem l a' ha'

end

open Classical Finset

variable {A : Type uA} {B : Type uB}

def double (a b : HF A) : HF A := ofFinset {a,b}

def ordinal : ℕ → HF A
  | 0 => ofFinset ∅
  | i+1 => ofFinset (insert (ordinal i) (elements (ordinal i)))

end CPTSeparation.Hereditary

namespace CPTSeparation.Operational

open Classical Hereditary Finset

variable {A : Type uA} {B : Type uB} {R : Type uR} {F : Type uF} {arity : F → ℕ}

local instance instDecidableEqHF {C : Type uC} : DecidableEq (HF C) := Classical.decEq _

abbrev empty : HF A := ordinal 0

abbrev truth : HF A := ordinal 1

def boolean (p : Prop) : HF A := if p then truth else empty

def inputRelation (rel : R → A → A → Bool) (r : R) (x y : HF A) : Prop :=
  ∃ a b, x = atom a ∧ y = atom b ∧ rel r a b = true

def allAtoms [Fintype A] : HF A := ofFinset (univ.image atom)

def unionHF (x : HF A) : HF A := ofFinset ((elements x).biUnion elements)

def uniqueHF (x : HF A) : HF A :=
  if h : ∃ a, elements x = {a} then h.choose else empty

def cardinalityHF (x : HF A) : HF A := ordinal (elements x).card

abbrev Location (F : Type uF) (arity : F → ℕ) (A : Type uA) := Σ f : F, Fin (arity f) → HF A

structure State (F : Type uF) (arity : F → ℕ) (A : Type uA) where
  value : Location F arity A → HF A
  finite : Set.Finite {l | value l ≠ empty}

namespace State

def initial : State F arity A := ⟨fun _ => empty, by simp⟩

def support (s : State F arity A) : Finset (Location F arity A) := s.finite.toFinset

end State

abbrev Update (F : Type uF) (arity : F → ℕ) (A : Type uA) := Location F arity A × HF A

namespace State

def Consistent (u : Finset (Update F arity A)) : Prop :=
  ∀ l x y, (l,x) ∈ u → (l,y) ∈ u → x = y

def updatedValue (s : State F arity A) (u : Finset (Update F arity A))
    (l : Location F arity A) : HF A :=
  if h : ∃ x, (l,x) ∈ u then h.choose else s.value l

def applyUpdates (s : State F arity A) (u : Finset (Update F arity A)) : State F arity A where
  value := s.updatedValue u
  finite := by
    apply (s.finite.union (u.image Prod.fst).finite_toSet).subset
    intro l hl
    by_cases h : ∃ x, (l,x) ∈ u
    · obtain ⟨x,hx⟩ := h
      exact Or.inr (mem_image.mpr ⟨(l,x),hx,rfl⟩)
    · exact Or.inl (by simpa only [Set.mem_ofPred_eq,updatedValue,dite_eq_right h] using hl)

end State

inductive Control | halt | accept deriving DecidableEq

end CPTSeparation.Operational

namespace CPTSeparation.Hereditary

open Classical Finset

variable {A : Type uA} {B : Type uB}

local instance instDecidableEqHF_1 {C : Type uC} : DecidableEq (HF C) := Classical.decEq _

def closure (x : HF A) : Finset (HF A) :=
  insert x ((elements x).attach.biUnion (fun y => closure y.val))
termination_by rank x
decreasing_by
  have mem_rawElements (a l : Lists A) : mk a ∈ rawElements l ↔ a ∈ l := by
    classical
    rcases l with ⟨(_ | _), l⟩
    · cases l
      change mk a ∈ (∅ : Finset (HF A)) ↔ False
      simp
    · change mk a ∈ (Lists'.toList l).toFinset.image mk ↔ a ∈ l
      simp only [Finset.mem_image, List.mem_toFinset, Lists'.mem_def, mk, Quotient.eq]
      constructor
      · rintro ⟨a',ha',heq⟩
        exact ⟨a',ha',heq.symm⟩
      · rintro ⟨a',ha',heq⟩
        exact ⟨a',ha',heq.symm⟩
  have rawRank_lt_of_mem {b : Bool} (l : Lists' A b) (a : Lists A)
      (ha : a ∈ l.toList) : rawRank a < rawRank' l := by
    induction l with
    | atom => simp at ha
    | nil => simp at ha
    | cons' c l ihc ihl =>
      rcases List.mem_cons.mp ha with rfl | ha
      · exact lt_of_lt_of_le (Nat.lt_succ_self _) (le_max_left _ _)
      · exact lt_of_lt_of_le (ihl ha) (le_max_right _ _)
  have rawRank_congr {l r : Lists A} (h : Lists.Equiv l r) : rawRank l = rawRank r := by
    induction h using Lists.Equiv.rec
        (motive_2 := fun l r _ => rawRank' l ≤ rawRank' r) with
    | refl => rfl
    | antisymm _ _ ihl ihr => exact Nat.le_antisymm ihl ihr
    | nil => exact Nat.zero_le _
    | @cons a a' l r _ hmem _ iheq ihsub =>
      change max (rawRank a + 1) (rawRank' l) ≤ rawRank' r
      refine max_le ?_ ihsub
      rw [iheq]
      exact rawRank_lt_of_mem r a' hmem
  have rank_lt_of_mem {x y : HF A} (h : x ∈ y) : rank x < rank y := by
    induction y using Quotient.inductionOn with
    | _ l =>
      induction x using Quotient.inductionOn with
      | _ a =>
        have h' := (mem_rawElements a l).mp h
        rcases l with ⟨(_ | _), l⟩
        · exact False.elim h'
        · obtain ⟨a',ha',heq⟩ := h'
          change rawRank a < rawRank' l
          rw [rawRank_congr heq]
          exact rawRank_lt_of_mem l a' ha'
  exact rank_lt_of_mem y.property

def familyClosure (f : Finset (HF A)) : Finset (HF A) := f.biUnion closure

end CPTSeparation.Hereditary

namespace CPTSeparation.Operational

open Classical Hereditary Finset

variable {A : Type uA} {R : Type uR} {F : Type uF} {arity : F → ℕ}

local instance instDecidableEqHF_1 {C : Type uC} : DecidableEq (HF C) := Classical.decEq _

namespace State

def critical (s : State F arity A) : Finset (HF A) :=
  s.support.biUnion (fun l => insert (s.value l) (univ.image l.2))

def active [Fintype A] (s : State F arity A) : Finset (HF A) :=
  familyClosure (s.critical ∪ {allAtoms,empty,truth} ∪ univ.image atom)

end State

end CPTSeparation.Operational

namespace CPTSeparation.FullCPT

section

open Classical Hereditary Finset

open Operational (State Control Location Update)

local instance instDecidableEqHF {C : Type} : DecidableEq (HF C) := Classical.decEq _

inductive Term (R F : Type) (arity : F → ℕ) : Type
  | var (name : ℕ)
  | ordinal (n : ℕ)
  | atoms
  | app (f : F) (args : Fin (arity f) → Term R F arity)
  | pair (a b : Term R F arity)
  | union (a : Term R F arity)
  | unique (a : Term R F arity)
  | card (a : Term R F arity)
  | equal (a b : Term R F arity)
  | member (a b : Term R F arity)
  | isAtom (a : Term R F arity)
  | input (r : R) (a b : Term R F arity)
  | not (a : Term R F arity)
  | and (a b : Term R F arity)
  | or (a b : Term R F arity)
  | conditional (c a b : Term R F arity)
  | comprehend (name : ℕ) (bound guard body : Term R F arity)

variable {A R F : Type} {arity : F → ℕ}

namespace Term

variable [Fintype A]

def eval (rel : R → A → A → Bool) (s : State F arity A) :
    Term R F arity → (ℕ → HF A) → HF A
  | .var n, v => v n
  | .ordinal n, _ => Hereditary.ordinal n
  | .atoms, _ => Operational.allAtoms
  | .app f args, v => s.value ⟨f, fun i => eval rel s (args i) v⟩
  | .pair a b, v => double (eval rel s a v) (eval rel s b v)
  | .union a, v => Operational.unionHF (eval rel s a v)
  | .unique a, v => Operational.uniqueHF (eval rel s a v)
  | .card a, v => Operational.cardinalityHF (eval rel s a v)
  | .equal a b, v => Operational.boolean (eval rel s a v = eval rel s b v)
  | .member a b, v => Operational.boolean (eval rel s a v ∈ eval rel s b v)
  | .isAtom a, v => Operational.boolean (isSet (eval rel s a v) = false)
  | .input r a b, v => Operational.boolean (Operational.inputRelation rel r (eval rel s a v) (eval rel s b v))
  | .not a, v => Operational.boolean (eval rel s a v ≠ Operational.truth)
  | .and a b, v => Operational.boolean (eval rel s a v = Operational.truth ∧ eval rel s b v = Operational.truth)
  | .or a b, v => Operational.boolean (eval rel s a v = Operational.truth ∨ eval rel s b v = Operational.truth)
  | .conditional c a b, v => if eval rel s c v = Operational.truth then eval rel s a v else eval rel s b v
  | .comprehend n bound guard body, v =>
      ofFinset (((elements (eval rel s bound v)).filter
        (fun x => eval rel s guard (Function.update v n x) = Operational.truth)).image
        (fun x => eval rel s body (Function.update v n x)))

end Term

inductive Rule (R F : Type) (arity : F → ℕ) : Type
  | skip
  | update (f : F) (args : Fin (arity f) → Term R F arity) (value : Term R F arity)
  | parallel (a b : Rule R F arity)
  | conditional (condition : Term R F arity) (a b : Rule R F arity)
  | forall (name : ℕ) (bound : Term R F arity) (body : Rule R F arity)
  | letValue (name : ℕ) (value : Term R F arity) (body : Rule R F arity)

namespace Rule

def updates [Fintype A] (rel : R → A → A → Bool) (s : State F arity A) :
    Rule R F arity → (ℕ → HF A) → Finset (Update F arity A)
  | .skip, _ => ∅
  | .update f args value, v => {(⟨f, fun i => (args i).eval rel s v⟩, value.eval rel s v)}
  | .parallel a b, v => updates rel s a v ∪ updates rel s b v
  | .conditional c a b, v => if c.eval rel s v = Operational.truth then updates rel s a v else updates rel s b v
  | .forall n bound body, v => (elements (bound.eval rel s v)).biUnion
      (fun x => updates rel s body (Function.update v n x))
  | .letValue n value body, v => updates rel s body (Function.update v n (value.eval rel s v))

end Rule

structure Program (R : Type) where
  Functions : Type
  finiteFunctions : Fintype Functions
  arity : Functions → ℕ
  body : Rule R (Control ⊕ Functions) (Sum.elim (fun _ => 0) arity)

namespace Program

variable (P : Program R) [Fintype A]

abbrev functionArity : Control ⊕ P.Functions → ℕ := Sum.elim (fun _ => 0) P.arity

abbrev Store := State (Control ⊕ P.Functions) P.functionArity A

def flag (s : P.Store (A := A)) (c : Control) : Prop := s.value ⟨.inl c, Fin.elim0⟩ = Operational.truth

def step (rel : R → A → A → Bool) (s : P.Store (A := A)) : Option (P.Store (A := A)) :=
  if P.flag s .halt then some s else
    let u := P.body.updates rel s (fun _ => Operational.empty)
    if State.Consistent u then some (s.applyUpdates u) else none

def run (rel : R → A → A → Bool) : ℕ → Option (P.Store (A := A))
  | 0 => some State.initial
  | j+1 => (run rel j).bind (P.step rel)

def active (rel : R → A → A → Bool) (h : ℕ) : Finset (HF A) :=
  (Finset.range (h+1)).biUnion (fun j => ((P.run rel j).toFinset).biUnion State.active)

end Program

end

open Classical Hereditary Finset

open Operational (State Control)

local instance instDecidableEqHF_1 {C : Type} : DecidableEq (HF C) := Classical.decEq _

variable {A R F : Type} {arity : F → ℕ}

namespace Term

variable [Fintype A]

def roots (rel : R → A → A → Bool) (s : State F arity A) :
    Term R F arity → (ℕ → HF A) → Finset (HF A)
  | t@(.var _), v | t@(.ordinal _), v | t@(.atoms), v => {t.eval rel s v}
  | t@(.app _ args), v => insert (t.eval rel s v)
      ((List.ofFn (fun i => roots rel s (args i) v)).foldr (· ∪ ·) ∅)
  | t@(.pair a b), v | t@(.equal a b), v | t@(.member a b), v
  | t@(.input _ a b), v | t@(.and a b), v | t@(.or a b), v =>
      insert (t.eval rel s v) (roots rel s a v ∪ roots rel s b v)
  | t@(.union a), v | t@(.unique a), v | t@(.card a), v
  | t@(.isAtom a), v | t@(.not a), v => insert (t.eval rel s v) (roots rel s a v)
  | .conditional c a b, v => roots rel s c v ∪
      if c.eval rel s v = Operational.truth then roots rel s a v else roots rel s b v
  | t@(.comprehend n bound guard body), v => insert (t.eval rel s v)
      (roots rel s bound v ∪ (elements (bound.eval rel s v)).biUnion (fun x =>
        roots rel s guard (Function.update v n x) ∪
        if guard.eval rel s (Function.update v n x) = Operational.truth then
          roots rel s body (Function.update v n x) else ∅))

end Term

namespace Rule

variable [Fintype A]

def roots (rel : R → A → A → Bool) (s : State F arity A) :
 Rule R F arity → (ℕ → HF A) → Finset (HF A)
 | .skip, _ => ∅
 | .update _ args value, v => value.roots rel s v ∪
     (List.ofFn (fun i => (args i).roots rel s v)).foldr (· ∪ ·) ∅
 | .parallel a b, v => roots rel s a v ∪ roots rel s b v
 | .conditional c a b, v => c.roots rel s v ∪
     if c.eval rel s v = Operational.truth then roots rel s a v else roots rel s b v
 | .forall n bound body, v => bound.roots rel s v ∪
     (elements (bound.eval rel s v)).biUnion (fun x => roots rel s body (Function.update v n x))
 | .letValue n value body, v => value.roots rel s v ∪
     roots rel s body (Function.update v n (value.eval rel s v))

end Rule

namespace Program

variable (P : Program R) [Fintype A]

def stepRoots (rel : R → A → A → Bool) (s : P.Store (A := A)) : Finset (HF A) :=
 if P.flag s .halt then ∅ else P.body.roots rel s (fun _ => Operational.empty)

def occurring (rel : R → A → A → Bool) (h : ℕ) : Finset (HF A) :=
 P.active rel h ∪ (range h).biUnion (fun j => match P.run rel j with
   | none => ∅
   | some s => familyClosure (P.stepRoots rel s))

def evaluationAccepts (time space : Polynomial ℕ) (rel : R → A → A → Bool) : Prop :=
 ∃ h st, h ≤ time.eval (Fintype.card A) ∧ P.run rel h = some st ∧ P.flag st .halt ∧ P.flag st .accept ∧
   (P.occurring rel h).card ≤ space.eval (Fintype.card A)

end Program

def EvaluationDefinable (Q : ∀ {A : Type}, [Fintype A] → Input A → Prop) : Prop :=
 ∃ P : Program Symbol, ∃ time space : Polynomial ℕ,
   ∀ (A : Type) [Fintype A] (I : Input A), P.evaluationAccepts time space I.rel ↔ Q I

end CPTSeparation.FullCPT

namespace CPTSeparation.OrderedQuery

section

open Finset

variable {N : ℕ}

abbrev Data (N : ℕ) := Input (Fin N)

end

def indexSymbol (i : Fin 8) : Symbol :=
  if i.val = 0 then .Ed else
  if i.val = 1 then .Cf else
  if i.val = 2 then .EB else
  if i.val = 3 then .VB else
  if i.val = 4 then .I else .Z ((i.val-5 : ℕ) : Scalar)

variable {N : ℕ}

def cellValue (S : Data N) (k : Fin (8*(N*N))) : Bool :=
  let ij := finProdFinEquiv.symm k
  let ab := finProdFinEquiv.symm ij.2
  S.rel (indexSymbol ij.1) ab.1 ab.2

def tableBits (S : Data N) : List Bool := List.ofFn (cellValue S)

abbrev OrderedInput := Σ N, Data N

end CPTSeparation.OrderedQuery

namespace CPTSeparation.GaussMachine

abbrev Letter := Option Scalar

def boolCode (b : Bool) : List Letter := [some (if b then 1 else 0)]

end CPTSeparation.GaussMachine

namespace CPTSeparation.CounterLang

abbrev Letter := Option Scalar

end CPTSeparation.CounterLang

namespace CPTSeparation.TableMachine

open CounterLang

def bitLetter (b : Bool) : Letter := some (if b then 1 else 0)

def tableInput {N} (S : OrderedQuery.Data N) : List Letter :=
 (OrderedQuery.tableBits S).map bitLetter

def code (S : OrderedQuery.OrderedInput) : List Letter :=
 List.replicate S.1 none ++ some 0 :: tableInput S.2

end CPTSeparation.TableMachine

namespace CPTSeparation

def Input.ordered {A : Type} {N : ℕ} (S : Input A) (e : A ≃ Fin N) : OrderedQuery.Data N :=
  ⟨fun r i j => S.rel r (e.symm i) (e.symm j)⟩

def OrdinaryPolynomialTime : Prop :=
  ∃ answer : OrderedQuery.OrderedInput → Bool,
    ∃ machine : Turing.TM2ComputableInPolyTime TableMachine.code GaussMachine.boolCode answer,
      (∀ k, Finite (machine.tm.Γ k)) ∧
      ∀ (A : Type) [Fintype A] (N : ℕ) (e : A ≃ Fin N) (S : Input A),
        answer ⟨N,S.ordered e⟩ = true ↔ S.query

end CPTSeparation

end

end OAI