{
  "id": "7141",
  "ident": "NS.07141",
  "slug": "constants-14--c14-upper-bound-improved",
  "title": "Tao's Optimization Constant 14 / Smallest $n$ for which the value of $BB(n)$ is undecidable · c14_upper_bound_improved",
  "status": "open",
  "statement_status": "reviewed",
  "statement_md": "Can the current best upper bound be improved?",
  "subjects": [
    "math.LO",
    "math.CO",
    "msc:03",
    "msc:68"
  ],
  "tags": [
    "question"
  ],
  "collection": "constants",
  "source_url": null,
  "solved_on_platform": false,
  "needs_answer": false,
  "version": 1,
  "decl_name": "Constant14.c14_upper_bound_improved",
  "statement_lean": "theorem c14_upper_bound_improved : C14 < 230",
  "challenge_source": "/-\nCopyright 2026 The Formal Conjectures Authors.\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n    https://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n-/\nmodule\n\npublic import FormalConjecturesUtil\n\n/-!\n# Tao's Optimization Constant 14 / Smallest $n$ for which the value of $BB(n)$ is undecidable\n\n*References:*\n- [Tao's Optimization Constant 14](https://teorth.github.io/optimizationproblems/constants/14a.html)\n- [LS1965] Lin, S. and Radó, T., [*Computer studies of Turing machine\n  problems*](https://doi.org/10.1145/321264.321270). J. ACM **12** (1965), 196–212.\n- [B1983] Brady, A. H., [*The determination of Radó's noncomputable function $\\Sigma(k)$ for\n  four-state Turing machines*](https://docs.bbchallenge.org/papers/Brady1983.pdf).\n  Math. Comp. 40 (1983), 647–665.\n- [BB2025] Blanchard, J. et al., *Determination of the fifth Busy Beaver value* (2025).\n  [arXiv:2509.12337](https://arxiv.org/abs/2509.12337)\n- [YS2016] Yedidia, A. and Aaronson, S., *A relatively small Turing machine whose behavior is\n  independent of set theory* (2016). [arXiv:1605.04343](https://arxiv.org/abs/1605.04343)\n- [O2016] O'Rear, S.,\n  [*metamath-turing-machines*](https://github.com/sorear/metamath-turing-machines) (2016).\n- [W2025] Wade, A. J.,\n  [*turing_machine_explorer*](https://codeberg.org/ajwade/turing_machine_explorer) (2025).\n- [G2026] Glazer, E., [*K230: a 230-state Turing machine for the consistency of\n  ZFC*](https://github.com/elliotglazer/k230-rocq) (2026).\n  [Announcement](https://x.com/ElliotGlazer/status/2112179703687856497)\n- [A2020] Aaronson, S., [*The Busy Beaver\n  Frontier*](https://doi.org/10.1145/3427361.3427369). ACM SIGACT News **51** (2020), 32–54.\n\nThis file defines the smallest $n$ for which the value of $BB(n)$ is undecidable. More precisely,\nit formalizes the least number of states for which ZFC can neither prove nor disprove that a\nspecific Turing machine halts. Its existence and known upper bounds follow from Gödel's second\nincompleteness theorem.\n\nWe do this by modeling a version of first order logic, ZFC and Turing machines within Lean.\nThe implementation of FOL and ZFC is taken from the\n[FormalizedLogicLean](https://github.com/FormalizedFormalLogic/Foundation) project.\nThe implementation is copied and minimized from that project. It is not necessary to read it to\nunderstand the definition of `C14`. The copied definitions span the section\n`FormalizedFormalLogic`. Confidence in that implementation partly comes from the large body of\nproofs developed with it, including formalizations of independence theorems.\n\nThis is followed by a model of Turing machines within the first-order logic framework, developed\nfor Formal Conjectures in the section `BackgroundDefs`. The file then defines `C14` and states its\nknown bounds and related conjectures. The known bounds use `sorry`; formalizing one would be a\nsubstantial achievement.\n\n-/\n@[expose] public section\n\nuniverse u v₁ v₂ v\n\nsection FormalizedFormalLogic\n\nsection\n\nnamespace Multiset\n\nvariable {α : Type*}\n\n/-- Function to avoid reducing `{a} + s` to `a ::ₘ s` -/\ndef atom (a : α) : Multiset α := {a}\n\n/-- `⦃x, y, z, ...⦄` notation for `kpair` -/\nsyntax \"⦃\" term,* \"⦄\" : term\n\nmacro_rules\n  | `(⦃$terms:term,*, $term:term⦄) => `(⦃$terms,*⦄ + atom $term)\n  | `(⦃$term:term⦄) => `(atom $term)\n  | `(⦃⦄) => `(0)\n\nend Multiset\n\nend\n\nsection\n\nnamespace Nat\n\nvariable {α : ℕ → Sort u}\n\ndef cases (hzero : α 0) (hsucc : ∀ n, α (n + 1)) : ∀ n, α n\n  | 0     => hzero\n  | n + 1 => hsucc n\n\ninfixr:70 \" :>ₙ \" => cases\n\nend Nat\n\nend\n\nsection\n\nnamespace Fin\n\nvariable {n : ℕ} (m : ℕ)\n\n@[inline] def addCast (m) : Fin n → Fin (m + n) := castLE <| Nat.le_add_left n m\n\nend Fin\n\nend\n\nsection\n\nnamespace Matrix\n\nsection\n\nvariable {n : ℕ} {α : Type u}\n\ninfixr:70 \" :> \" => vecCons\n\ndef vecConsLast {n : ℕ} (t : Fin n → α) (h : α) : Fin n.succ → α :=\n  Fin.lastCases h t\n\ninfixl:70 \" <: \" => vecConsLast\n\nend\n\nend Matrix\n\nend\n\nsection\n\nnamespace FFL\n\nclass HTilde (α : Type*) (β : outParam Type*) where\n  hTilde : α → β\n\nprefix:75 \"∼\" => HTilde.hTilde\n\nmacro_rules | `(∼$x) => `(unop% HTilde.hTilde $x)\n\nclass HArrow (α β : Type*) (γ : outParam Type*) where\n  hArrow : α → β → γ\n\ninfixr:60 \" 🡒 \" => HArrow.hArrow\n\nmacro_rules | `($x 🡒 $y) => `(binop% HArrow.hArrow $x $y)\n\nclass HWedge (α β : Type*) (γ : outParam Type*) where\n  hWedge : α → β → γ\n\ninfixr:69 \" ⋏ \" => HWedge.hWedge\n\nmacro_rules | `($x ⋏ $y) => `(binop% HWedge.hWedge $x $y)\n\nclass HVee (α β : Type*) (γ : outParam Type*) where\n  hVee : α → β → γ\n\ninfixr:68 \" ⋎ \" => HVee.hVee\n\nmacro_rules | `($x ⋎ $y) => `(binop% HVee.hVee $x $y)\n\nattribute [match_pattern] HTilde.hTilde HArrow.hArrow HWedge.hWedge HVee.hVee\n\nclass Tilde (α : Type*) where\n  tilde : α → α\n\nclass Arrow (α : Type*) where\n  arrow : α → α → α\n\nclass Wedge (α : Type*) where\n  wedge : α → α → α\n\nclass Vee (α : Type*) where\n  vee : α → α → α\n\nvariable {α : Type*}\n\n@[default_instance]\ninstance Tilde.instHTilde [Tilde α] : HTilde α α := ⟨Tilde.tilde⟩\n\n@[default_instance]\ninstance Arrow.instHArrow [Arrow α] : HArrow α α α := ⟨Arrow.arrow⟩\n\n@[default_instance]\ninstance Wedge.instHWedge [Wedge α] : HWedge α α α := ⟨Wedge.wedge⟩\n\n@[default_instance]\ninstance Vee.instHVee [Vee α] : HVee α α α := ⟨Vee.vee⟩\n\nend FFL\n\nend\n\nsection\n\nnamespace FFL\n\n/--\nA class for types with logical connectives $\\top, \\bot, \\land, \\lor, \\to, \\lnot$.\n-/\nclass LogicalConnective (α : Type*) extends Tilde α, Arrow α, Wedge α, Vee α\n\nclass LogicalNeutral (α : Type*) extends Top α, Bot α\n\nnamespace LogicalConnective\n\nsection\n\nvariable {α : Type*} [LogicalConnective α]\n\n@[match_pattern] def iff (a b : α) := (a 🡒 b) ⋏ (b 🡒 a)\n\n/--\nA defined logical connective for \"iff\", defined from the logical connectives `🡒` and `⋏`.\n-/\ninfix:61 \" 🡘 \" => LogicalConnective.iff\n\nend\n\nvariable (α β γ : Type*)\n  [LogicalConnective α] [LogicalConnective β] [LogicalConnective γ]\n  [LogicalNeutral α] [LogicalNeutral β] [LogicalNeutral γ]\n\nstructure Hom where\n  toTr : α → β\n  map_top' : toTr ⊤ = ⊤\n  map_bot' : toTr ⊥ = ⊥\n  map_neg' : ∀ φ, toTr (∼φ) = ∼toTr φ\n  map_imply' : ∀ φ ψ, toTr (φ 🡒 ψ) = toTr φ 🡒 toTr ψ\n  map_and' : ∀ φ ψ, toTr (φ ⋏ ψ) = toTr φ ⋏ toTr ψ\n  map_or'  : ∀ φ ψ, toTr (φ ⋎ ψ) = toTr φ ⋎ toTr ψ\n\n/--\nA structure for homomorphisms (for logical connectives) from `α` to `β`.\n-/\ninfix:25 \" →ˡᶜ \" => Hom\n\nnamespace Hom\n\nvariable {α β γ}\n\ninstance : FunLike (α →ˡᶜ β) α β where\n  coe := toTr\n  coe_injective := by\n    intro f g h; rcases f; rcases g; simpa using h\n\nend Hom\n\nend LogicalConnective\n\nend FFL\n\nopen FFL\n\nnamespace Matrix\n\nsection conjunction\n\nvariable {α : Type*} [Top α] [Wedge α]\n\n/-- The conjunction of a vector of elements of type `α`, where `α` is a type with `Wedge α`. -/\ndef conj : {n : ℕ} → (Fin n → α) → α\n  |     0, _ => ⊤\n  | _ + 1, v => v 0 ⋏ conj (vecTail v)\n\nend conjunction\n\nend Matrix\n\nnamespace Multiset\n\nvariable {α : Type*} [Tilde α]\n\ninstance : Tilde (Multiset α) := ⟨fun Γ ↦ Γ.map (∼·)⟩\n\nend Multiset\n\nend\n\nsection\n\nnamespace FFL\n\n/-- Entailment relation on proof system `S` and formula `F` -/\nclass Entailment (S : Type*) (F : outParam Type*) where\n  Prf : S → F → Type*\n\ninfix:45 \" ⊢! \" => Entailment.Prf\n\nnamespace Entailment\n\nvariable {F : Type*} {S T U : Type*} [Entailment S F] [Entailment T F] [Entailment U F]\n\nsection\n\nvariable (𝓢 : S)\n\n/-- Proposition that states `φ` is provable. -/\ndef Provable (φ : F) : Prop := Nonempty (𝓢 ⊢! φ)\n\n/-- Abbreviation for unprovability. -/\nabbrev Unprovable (φ : F) : Prop := ¬Provable 𝓢 φ\n\ninfix:45 \" ⊬ \" => Unprovable\n\nend\n\nend Entailment\n\nend FFL\n\nend\n\nsection\n\nnamespace FFL\n\nnamespace FirstOrder\n\nstructure Language where\n  Func : Nat → Type u\n  Rel  : Nat → Type u\n\nnamespace Language\n\nprotected class Eq (L : Language) where\n  eq : L.Rel 2\n\nprotected class Mem (L : Language) where\n  mem : L.Rel 2\n\nend Language\n\nend FirstOrder\n\nend FFL\n\nend\n\nsection\n\nnamespace FFL\n\nnamespace FirstOrder\n\n/--\nA semiterm of language `L`, with bound variables indexed by `Fin n` and free variables indexed by `ξ`. In `FFL.FirstOrder.Semiformula`, bound variables are de Bruijn indices with a separate type from free variables.\n-/\ninductive Semiterm (L : Language) (ξ : Type*) (n : ℕ)\n  | bvar : Fin n → Semiterm L ξ n\n  | fvar : ξ → Semiterm L ξ n\n  | func : ∀ {arity : ℕ}, L.Func arity → (Fin arity → Semiterm L ξ n) → Semiterm L ξ n\n\n/-- `&x` is the free variable indexed by the element `x : ξ`. -/\nscoped prefix:max \"&\" => Semiterm.fvar\n\n/-- `#x` is the bound variable with de Bruijn index `x`. -/\nscoped prefix:max \"#\" => Semiterm.bvar\n\nabbrev ClosedSemiterm (L : Language) (n : ℕ) := Semiterm L Empty n\n\nnamespace Semiterm\n\nvariable {L L' L₁ L₂ L₃ : Language} {ξ ξ' ξ₁ ξ₂ ξ₃ : Type*} {n n₁ n₂ n₃ : ℕ}\n\nsection freeVariables\n\nvariable [DecidableEq ξ]\n\n/--\nThe set of free variables occuring in a semiterm.\n-/\ndef freeVariables : Semiterm L ξ n → Finset ξ\n  |       #_ => ∅\n  |       &x => {x}\n  | func _ v => .biUnion .univ fun i ↦ freeVariables (v i)\n\nend freeVariables\n\nend Semiterm\n\nend FirstOrder\n\nend FFL\n\nend\n\nsection\n\nnamespace FFL\n\nnamespace FirstOrder\n\nclass UnivQuantifier (α : ℕ → Type*) where\n  all {n : ℕ} : α (n + 1) → α n\n\nprefix:64 \"∀¹ \" => UnivQuantifier.all\n\nclass ExsQuantifier (α : ℕ → Type*) where\n  exs {n : ℕ} : α (n + 1) → α n\n\nprefix:64 \"∃¹ \" => ExsQuantifier.exs\n\nattribute [match_pattern] UnivQuantifier.all ExsQuantifier.exs\n\nclass Quantifier (α : ℕ → Type*) extends UnivQuantifier α, ExsQuantifier α\n\n/-- Logical Connectives with Quantifiers. -/\nclass LCWQ (α : ℕ → Type*) extends Quantifier α where\n  connectives : (n : ℕ) → LogicalConnective (α n)\n  neutrals : (n : ℕ) → LogicalNeutral (α n)\n\ninstance (α : ℕ → Type*) [LCWQ α] (n : ℕ) : LogicalConnective (α n) := LCWQ.connectives n\n\ninstance (α : ℕ → Type*) [LCWQ α] (n : ℕ) : LogicalNeutral (α n) := LCWQ.neutrals n\n\ninstance (α : ℕ → Type*) [Quantifier α] [(n : ℕ) → LogicalConnective (α n)]\n    [(n : ℕ) → LogicalNeutral (α n)] : LCWQ α where\n  connectives := inferInstance\n  neutrals := inferInstance\n\nsection UnivQuantifier\n\nvariable {α : ℕ → Type*} [UnivQuantifier α]\nvariable {n : ℕ}\n\ndef allClosure : {n : ℕ} → α n → α 0\n  |     0, a => a\n  | _ + 1, a => allClosure (∀¹ a)\n\n/--\nThe universal closure of a formula.\n-/\nprefix:64 \"∀¹* \" => allClosure\n\ndef allItr : (k : ℕ) → α (n + k) → α n\n  |     0, a => a\n  | k + 1, a => allItr k (∀¹ a)\n\nnotation \"∀¹^[\" k \"] \" φ:64 => allItr k φ\n\nend UnivQuantifier\n\nsection quantifier\n\nvariable {α : ℕ → Type*} {n : ℕ}\n\ndef ball [UnivQuantifier α] [Arrow (α (n + 1))] (φ : α (n + 1)) (ψ : α (n + 1)) : α n := ∀¹ (φ 🡒 ψ)\n\ndef bexs [ExsQuantifier α] [Wedge (α (n + 1))] (φ : α (n + 1)) (ψ : α (n + 1)) : α n := ∃¹ (φ ⋏ ψ)\n\n/-- A bounded universal quantifier. `∀¹[φ] ψ` is defined as `∀¹ (φ 🡒 ψ)`. -/\nnotation:64 \"∀¹[\" φ \"] \" ψ => ball φ ψ\n\n/-- A bounded existential quantifier. `∃¹[φ] ψ` is defined as `∃¹ (φ ⋏ ψ)`. -/\nnotation:64 \"∃¹[\" φ \"] \" ψ => bexs φ ψ\n\nend quantifier\n\nend FirstOrder\n\nend FFL\n\nend\n\nsection\n\nnamespace FFL.FirstOrder\n\n/--\nA semiformula of language `L`. Free variables are of type `ξ`, and bound variables are implemented as de Bruijn indices, of a type `Fin n` separate from free variables.\n-/\ninductive Semiformula (L : Language) (ξ : Type*) : ℕ → Type _ where\n  |  verum {n : ℕ} : Semiformula L ξ n\n  | falsum {n : ℕ} : Semiformula L ξ n\n  |    rel : {n arity : ℕ} → L.Rel arity → (Fin arity → Semiterm L ξ n) → Semiformula L ξ n\n  |   nrel : {n arity : ℕ} → L.Rel arity → (Fin arity → Semiterm L ξ n) → Semiformula L ξ n\n  |    and {n : ℕ} : Semiformula L ξ n → Semiformula L ξ n → Semiformula L ξ n\n  |     or {n : ℕ} : Semiformula L ξ n → Semiformula L ξ n → Semiformula L ξ n\n  |    all {n : ℕ} : Semiformula L ξ (n + 1) → Semiformula L ξ n\n  |    exs {n : ℕ} : Semiformula L ξ (n + 1) → Semiformula L ξ n\n\nabbrev Formula (L : Language) (ξ : Type*) := Semiformula L ξ 0\n\nabbrev Sentence (L : Language) := Formula L Empty\n\nabbrev Semisentence (L : Language) (n : ℕ) := Semiformula L Empty n\n\nabbrev Semiproposition (L : Language) (n : ℕ) := Semiformula L ℕ n\n\nabbrev Proposition (L : Language) := Semiproposition L 0\n\nnamespace Semiformula\n\nvariable\n  {L : Language} {L₁ : Language} {L₂ : Language} {L₃ : Language}\n  {ξ ξ₁ ξ₂ ξ₃ : Type*}\n  {n n₁ n₂ n₂ m m₁ m₂ m₃ : ℕ}\n\ndef neg {n} : Semiformula L ξ n → Semiformula L ξ n\n  |    verum => falsum\n  |   falsum => verum\n  |  rel r v => nrel r v\n  | nrel r v => rel r v\n  |  and φ ψ => or (neg φ) (neg ψ)\n  |   or φ ψ => and (neg φ) (neg ψ)\n  |    all φ => exs (neg φ)\n  |    exs φ => all (neg φ)\n\ninstance : LogicalConnective (Semiformula L ξ n) where\n  arrow := fun φ ψ => or (neg φ) ψ\n  wedge := and\n  vee := or\n  tilde := neg\n\ninstance : LogicalNeutral (Semiformula L ξ n) where\n  top := verum\n  bot := falsum\n\ninstance : Quantifier (Semiformula L ξ) where\n  all := all\n  exs := exs\n\nsection FreeVariables\n\nvariable [DecidableEq ξ]\n\ndef freeVariables {n} : Semiformula L ξ n → Finset ξ\n  |  rel _ v => .biUnion .univ fun i ↦ (v i).freeVariables\n  | nrel _ v => .biUnion .univ fun i ↦ (v i).freeVariables\n  |        ⊤ => ∅\n  |        ⊥ => ∅\n  |    φ ⋏ ψ => freeVariables φ ∪ freeVariables ψ\n  |    φ ⋎ ψ => freeVariables φ ∪ freeVariables ψ\n  |     ∀¹ φ => freeVariables φ\n  |     ∃¹ φ => freeVariables φ\n\ndef fvSup (φ : Semiproposition L n) : ℕ := (φ.freeVariables.max).recBotCoe 0 .succ\n\nend FreeVariables\n\nend Semiformula\n\nabbrev Theory (L : Language) := Set (Sentence L)\n\nend FFL.FirstOrder\n\nend\n\nsection\n\nnamespace FFL\n\nnamespace FirstOrder\n\n/--\nA structure for maps which rewrite the semiterms occurring in a term.\n\ntoFun - A function from `Semiterm L ξ₁ n₁` to `Semiterm L ξ₂ n₂`.\n\nfunc'' - A proof that `toFun` respects the function symbols of `L`.\n-/\nstructure Rew (L : Language) (ξ₁ : Type*) (n₁ : ℕ) (ξ₂ : Type*) (n₂ : ℕ) where\n  toFun : Semiterm L ξ₁ n₁ → Semiterm L ξ₂ n₂\n  func'' {k : ℕ} (f : L.Func k) (v : Fin k → Semiterm L ξ₁ n₁) :\n    toFun (Semiterm.func f v) = Semiterm.func f fun i ↦ toFun (v i)\n\nabbrev SyntacticRew (L : Language) (n₁ n₂ : ℕ) := Rew L ℕ n₁ ℕ n₂\n\nnamespace Rew\n\nopen Semiterm\n\nvariable {L L' L₁ L₂ L₃ : Language} {ξ ξ' ξ₁ ξ₂ ξ₃ : Type*} {n n₁ n₂ n₃ : ℕ}\n\ninstance : FunLike (Rew L ξ₁ n₁ ξ₂ n₂) (Semiterm L ξ₁ n₁) (Semiterm L ξ₂ n₂) where\n  coe := fun f => f.toFun\n  coe_injective := fun f g h => by rcases f; rcases g; simpa using h\n\nprotected def id : Rew L ξ n ξ n where\n  toFun := id\n  func'' := fun _ _ => rfl\n\nprotected def comp (ω₂ : Rew L ξ₂ n₂ ξ₃ n₃) (ω₁ : Rew L ξ₁ n₁ ξ₂ n₂) : Rew L ξ₁ n₁ ξ₃ n₃ where\n  toFun := fun t => ω₂ (ω₁ t)\n  func'' := fun f v ↦\n    (congrArg ω₂ (ω₁.func'' f v)).trans (ω₂.func'' f (fun i ↦ ω₁ (v i)))\n\ndef bindAux (b : Fin n₁ → Semiterm L ξ₂ n₂) (e : ξ₁ → Semiterm L ξ₂ n₂) : Semiterm L ξ₁ n₁ → Semiterm L ξ₂ n₂\n  |       #x => b x\n  |       &x => e x\n  | func f v => func f (fun i => bindAux b e (v i))\n\n/-- `FFL.FirstOrder.Rew.bind f` is a substitution of the bound variables occurring in a term by `b : Fin n₁ → Semiterm L ξ₂ n₂`, and the free variables occurring in a term by `e : ξ₁ → Semiterm L ξ₂ n₂`. -/\ndef bind (b : Fin n₁ → Semiterm L ξ₂ n₂) (e : ξ₁ → Semiterm L ξ₂ n₂) : Rew L ξ₁ n₁ ξ₂ n₂ where\n  toFun := bindAux b e\n  func'' := fun _ _ => rfl\n\ndef map (b : Fin n₁ → Fin n₂) (e : ξ₁ → ξ₂) : Rew L ξ₁ n₁ ξ₂ n₂ :=\n  bind (fun n => #(b n)) (fun m => &(e m))\n\n/-- `FFL.FirstOrder.Rew.subst v` is a substitution of the bound variables occurring in a term by `v : Fin n → Semiterm L ξ n'`. -/\ndef subst {n' : ℕ} (v : Fin n → Semiterm L ξ n') : Rew L ξ n ξ n' :=\n  bind v fvar\n\n/-- `FFL.FirstOrder.Rew.emb` is a embedding of a term with no free variables. It can be thought of as a cast from `Semiterm L Empty n` to `Semiterm L ξ n` for any type `ξ`. -/\ndef emb {o : Type v₁} [h : IsEmpty o] {ξ : Type v₂} {n} : Rew L o n ξ n := map id h.elim\n\n/-- `FFL.FirstOrder.Rew.bShift` is a transformation of the bounded variables occurring in a term by `#x ↦ #(Fin.succ x)`. -/\ndef bShift : Rew L ξ n ξ (n + 1) :=\n  map Fin.succ id\n\nprotected def q (ω : Rew L ξ₁ n₁ ξ₂ n₂) : Rew L ξ₁ (n₁ + 1) ξ₂ (n₂ + 1) :=\n  bind (#0 :> bShift ∘ ω ∘ bvar) (bShift ∘ ω ∘ fvar)\n\nsection Syntactic\n\n/-- `FFL.FirstOrder.Rew.shift` is a transformation of the free variables occurring in the term by `&x ↦ &(x + 1)`. -/\ndef shift : SyntacticRew L n n := map id Nat.succ\n\ndef free : SyntacticRew L (n + 1) n := bind (bvar <: &0) (fun m => &(Nat.succ m))\n\ndef fix : SyntacticRew L n (n + 1) := bind (fun x => #(Fin.castSucc x)) (#(Fin.last n) :>ₙ fvar)\n\ndef fixitr (n : ℕ) : (m : ℕ) → SyntacticRew L n (n + m)\n  |     0 => Rew.id\n  | m + 1 => Rew.fix.comp (fixitr n m)\n\nend Syntactic\n\nend Rew\n\nnamespace Semiterm\n\nvariable {L L' L₁ L₂ L₃ : Language} {ξ ξ' ξ₁ ξ₂ ξ₃ : Type*} {n n₁ n₂ n₃ : ℕ}\n\ndef toEmpty [DecidableEq ξ] {n : ℕ} : (t : Semiterm L ξ n) → t.freeVariables = ∅ → ClosedSemiterm L n\n  |       #x, _ => #x\n  |       &x, h => False.elim (by simp [freeVariables] at h)\n  | func f v, h =>\n    have : ∀ i, (v i).freeVariables = ∅ := fun i ↦ Finset.subset_empty.mp\n        (h ▸ Finset.subset_biUnion_of_mem (fun j ↦ (v j).freeVariables) (Finset.mem_univ i))\n    func f fun i ↦ toEmpty (v i) (this i)\n\nend Semiterm\n\n/--\nA typeclass for `Rew`s which additionally respect quantifiers.\n\n`app` - A notion of application of `Rew`s to formulas.\n\n`app_all` - Application preserves universal quantification.\n\n`app_exs` - Application preserves existential quantification.\n-/\nclass Rewriting (L : outParam Language) (ξ : outParam Type*) (F : ℕ → Type*) (ζ : Type*) (G : outParam (ℕ → Type*))\n    [LCWQ F] [LCWQ G] where\n  app {n₁ n₂ : ℕ} : Rew L ξ n₁ ζ n₂ → F n₁ →ˡᶜ G n₂\n  app_all {n₁ n₂ : ℕ} (ω₁₂ : Rew L ξ n₁ ζ n₂) (φ : F (n₁ + 1)) :\n    app ω₁₂ (∀¹ φ) = ∀¹ (app ω₁₂.q φ)\n  app_exs {n₁ n₂ : ℕ} (ω₁₂ : Rew L ξ n₁ ζ n₂) (φ : F (n₁ + 1)) :\n    app ω₁₂ (∃¹ φ) = ∃¹ (app ω₁₂.q φ)\n\nnamespace Rewriting\n\nvariable {F : ℕ → Type _} {G : ℕ → Type _}\n  {L : Language} {ξ : Type _} {ζ : Type _}\nvariable [LCWQ F] [LCWQ G] [Rewriting L ξ F ζ G]\nvariable {n₁ n₂ n : ℕ}\n\n/-- Application of a `Rewriting` to a formula. -/\ninfixr:73 \" ▹ \" => app\n\nabbrev subst [Rewriting L ξ F ξ F] (φ : F n₁) (w : Fin n₁ → Semiterm L ξ n₂) : F n₂ := Rew.subst w ▹ φ\n\n/-- Applies the substitution `FFL.FirstOrder.Rew.subst w` to a formula. This substitutes the bound variables occurring in the formula by `w : Fin n₁ → Semiterm L ξ n₂`. -/\ninfix:90 \" ⇜ \" => FFL.FirstOrder.Rewriting.subst\n\n/-- Applies the substitution `FFL.FirstOrder.Rew.shift` to a formula. This substitutes each free variable `&x` with `&(x + 1)`. -/\nabbrev shift [Rewriting L ℕ F ℕ F] : F n →ˡᶜ F n := app Rew.shift\n\nabbrev free [Rewriting L ℕ F ℕ F] : F (n + 1) →ˡᶜ F n := app Rew.free\n\ndef shifts [Rewriting L ℕ F ℕ F] (Γ : Multiset (F n)) : Multiset (F n) := Γ.map Rewriting.shift\n\nscoped[FFL.FirstOrder] postfix:max \"⁺\" => FirstOrder.Rewriting.shifts\n\nabbrev emb {ο : Type _} {ξ : Type _} [IsEmpty ο] {O F : ℕ → Type*}\n    [LCWQ O] [LCWQ F] [Rewriting L ο O ξ F] : O n →ˡᶜ F n := app (Rew.emb (ξ := ξ))\n\nend Rewriting\n\nsection Notation\n\nsyntax (name := substNotation) term:max \"/[\" term,* \"]\" : term\n\n/-- Slash notation for rewriting bound variables of a formula.\n\nThe notation `φ/w` is equivalent to `φ ⇜ w`, which for a formula `φ` with bound variables from `Fin n₁`, substitutes the bound variables occurring in `φ` by `w : Fin n₁ → Semiterm L ξ n₂`. -/\nmacro_rules (kind := substNotation)\n  | `($φ:term /[$terms:term,*]) => `($φ ⇜ ![$terms,*])\n\nend Notation\n\nend FirstOrder\n\nend FFL\n\nend\n\nsection\n\nnamespace FFL\n\nnamespace FirstOrder\n\nnamespace Semiformula\n\nvariable {L : Language} {ξ₁ : Type _} {n₁ : ℕ} {ξ₂ : Type _} {n₂ : ℕ}\n  {ξ : Type _} {ζ : Type _} {n : ℕ}\n\ndef rewAux ⦃n₁ n₂ : ℕ⦄ (ω : Rew L ξ₁ n₁ ξ₂ n₂) : Semiformula L ξ₁ n₁ → Semiformula L ξ₂ n₂\n  |        ⊤ => ⊤\n  |        ⊥ => ⊥\n  |  rel r v => rel r (ω ∘ v)\n  | nrel r v => nrel r (ω ∘ v)\n  |    φ ⋏ ψ => rewAux ω φ ⋏ rewAux ω ψ\n  |    φ ⋎ ψ => rewAux ω φ ⋎ rewAux ω ψ\n  |     ∀¹ φ => ∀¹ rewAux ω.q φ\n  |     ∃¹ φ => ∃¹ rewAux ω.q φ\n\ndef rew (ω : Rew L ξ₁ n₁ ξ₂ n₂) : Semiformula L ξ₁ n₁ →ˡᶜ Semiformula L ξ₂ n₂ where\n  toTr := rewAux ω\n  map_top' := by rfl\n  map_bot' := by rfl\n  map_neg' := fun φ ↦ by\n    change rewAux ω (neg φ) = neg (rewAux ω φ)\n    induction φ generalizing n₂ <;> simp_all [rewAux, neg] <;> rfl\n  map_and' := fun φ ψ ↦ rfl\n  map_or' := fun φ ψ ↦ rfl\n  map_imply' := fun φ ψ ↦ by\n    change or (rewAux ω (neg φ)) (rewAux ω ψ) =\n      or (neg (rewAux ω φ)) (rewAux ω ψ)\n    congr 1\n    induction φ generalizing n₂ <;> simp_all [rewAux, neg] <;> rfl\n\ninstance : Rewriting L ξ (Semiformula L ξ) ζ (Semiformula L ζ) where\n  app := rew\n  app_all (_ _) := rfl\n  app_exs (_ _) := rfl\n\nabbrev free (φ : Semiproposition L (n + 1)) : Semiproposition L n := Rewriting.free φ\n\nsection univCl\n\ndef univCl' (φ : Proposition L) : Proposition L := ∀¹* (@Rew.fixitr L 0 φ.fvSup ▹ φ)\n\nsection\n\nvariable {ξ : Type _} {L : Language}\n\ndef toEmpty [DecidableEq ξ] {n : ℕ} : (φ : Semiformula L ξ n) → φ.freeVariables = ∅ → Semisentence L n\n  |  rel R v, h => rel R fun i ↦ (v i).toEmpty <| Finset.subset_empty.mp\n      (h ▸ Finset.subset_biUnion_of_mem (fun j ↦ (v j).freeVariables) (Finset.mem_univ i))\n  | nrel R v, h => nrel R fun i ↦ (v i).toEmpty <| Finset.subset_empty.mp\n      (h ▸ Finset.subset_biUnion_of_mem (fun j ↦ (v j).freeVariables) (Finset.mem_univ i))\n  |        ⊤, _ => ⊤\n  |        ⊥, _ => ⊥\n  |    φ ⋏ ψ, h =>\n    φ.toEmpty ((Finset.union_eq_empty.mp h).1) ⋏\n    ψ.toEmpty ((Finset.union_eq_empty.mp h).2)\n  |    φ ⋎ ψ, h =>\n    φ.toEmpty ((Finset.union_eq_empty.mp h).1) ⋎\n    ψ.toEmpty ((Finset.union_eq_empty.mp h).2)\n  |     ∀¹ φ, h => ∀¹ φ.toEmpty (h)\n  |     ∃¹ φ, h => ∃¹ φ.toEmpty (h)\n\nend\n\n/-- An universal closure of formula -/\ndef univCl (φ : Proposition L) : Sentence L := φ.univCl'.toEmpty (by\n  have term_closed {n m : ℕ} (ω : SyntacticRew L n m) (t : Semiterm L ℕ n)\n      (hb : ∀ x, (ω #x).freeVariables = ∅)\n      (hf : ∀ x ∈ t.freeVariables, (ω &x).freeVariables = ∅) :\n      (ω t).freeVariables = ∅ := by\n    induction t with\n    | bvar x => exact hb x\n    | fvar x => exact hf x (by simp [Semiterm.freeVariables])\n    | func f v ih =>\n      change (ω.toFun (Semiterm.func f v)).freeVariables = ∅\n      rw [ω.func'']\n      apply Finset.eq_empty_iff_forall_notMem.mpr\n      intro x hx\n      obtain ⟨i, _, hi⟩ := Finset.mem_biUnion.mp hx\n      have hi' := ih i (fun y hy ↦ hf y\n        (Finset.mem_biUnion.mpr ⟨i, Finset.mem_univ i, hy⟩))\n      exact Finset.notMem_empty x (hi' ▸ hi)\n\n  have shift_closed {n : ℕ} (t : Semiterm L ℕ n) (h : t.freeVariables = ∅) :\n      (Rew.bShift t).freeVariables = ∅ :=\n    term_closed Rew.bShift t (fun _ ↦ rfl)\n      (fun x hx ↦ False.elim (Finset.notMem_empty x (h ▸ hx)))\n\n  have q_closed {n m : ℕ} (ω : SyntacticRew L n m)\n      (hb : ∀ x, (ω #x).freeVariables = ∅) :\n      ∀ x, (ω.q #x).freeVariables = ∅ := by\n    intro x\n    cases x using Fin.cases with\n    | zero => rfl\n    | succ x => exact shift_closed (ω #x) (hb x)\n\n  have formula_closed {n m : ℕ} (φ : Semiproposition L n) (ω : SyntacticRew L n m)\n      (hb : ∀ x, (ω #x).freeVariables = ∅)\n      (hf : ∀ x ∈ φ.freeVariables, (ω &x).freeVariables = ∅) :\n      (Semiformula.rewAux ω φ).freeVariables = ∅ := by\n    induction φ generalizing m with\n    | verum => rfl\n    | falsum => rfl\n    | rel R v | nrel R v =>\n      apply Finset.eq_empty_iff_forall_notMem.mpr\n      intro x hx\n      obtain ⟨i, _, hi⟩ := Finset.mem_biUnion.mp hx\n      have hi' := term_closed ω (v i) hb (fun y hy ↦ hf y\n        (Finset.mem_biUnion.mpr ⟨i, Finset.mem_univ i, hy⟩))\n      exact Finset.notMem_empty x (hi' ▸ hi)\n    | and φ ψ ihφ ihψ | or φ ψ ihφ ihψ =>\n      exact Finset.union_eq_empty.mpr\n        ⟨ihφ ω hb (fun x hx ↦ hf x (Finset.mem_union_left _ hx)),\n         ihψ ω hb (fun x hx ↦ hf x (Finset.mem_union_right _ hx))⟩\n    | all φ ih | exs φ ih =>\n      exact ih ω.q (q_closed ω hb) (fun x hx ↦ shift_closed (ω &x) (hf x hx))\n\n  have lt_fvSup_of_mem {n : ℕ} {φ : Semiproposition L n} {x : ℕ}\n      (hx : x ∈ φ.freeVariables) : x < φ.fvSup := by\n    obtain ⟨m, hm⟩ := Finset.max_of_mem hx\n    simpa only [Semiformula.fvSup, hm, WithBot.recBotCoe_coe] using\n      Nat.lt_succ_of_le (Finset.le_max_of_eq hx hm)\n\n  have freeVariables_allClosure {n : ℕ} (φ : Semiproposition L n) :\n      (∀¹* φ).freeVariables = φ.freeVariables := by\n    induction n with\n    | zero => rfl\n    | succ n ih => exact ih (∀¹ φ)\n\n  have fixitr_fvar (n m x : ℕ) :\n      Rew.fixitr n m (&x : Semiterm L ℕ n) =\n        if h : x < m then #(Fin.natAdd n ⟨x, h⟩) else &(x - m) := by\n    induction m with\n    | zero => rfl\n    | succ m ih =>\n      change Rew.fix (Rew.fixitr n m &x) = _\n      rw [ih]\n      by_cases hx : x < m\n      · have hx' : x < m + 1 := by omega\n        simp only [dif_pos hx, dif_pos hx']\n        rfl\n      · by_cases hx' : x < m + 1\n        · have he : x = m := by omega\n          subst x\n          simp only [dif_neg (Nat.lt_irrefl m), dif_pos (Nat.lt_succ_self m), Nat.sub_self]\n          rfl\n        · have he : x - m = x - (m + 1) + 1 := by omega\n          simp only [dif_neg hx, dif_neg hx', he]\n          rfl\n  change (∀¹* (Semiformula.rewAux (Rew.fixitr 0 φ.fvSup) φ)).freeVariables = ∅\n  rw [freeVariables_allClosure]\n  exact formula_closed φ (Rew.fixitr 0 φ.fvSup) (fun x ↦ Fin.elim0 x) (fun x hx ↦ by\n    simp [fixitr_fvar, lt_fvSup_of_mem hx, Semiterm.freeVariables]))\n\nend univCl\n\nend Semiformula\n\nend FirstOrder\n\nend FFL\n\nend\n\nsection\n\nnamespace FFL\n\nnamespace FirstOrder\n\nvariable {L : Language} {ξ : Type _} {n : ℕ}\n\nnamespace Semiformula\n\nstructure Operator (L : Language.{u}) (n : ℕ) where\n  sentence : Semisentence L n\n\nnamespace Operator\n\ndef operator {arity : ℕ} (o : Operator L arity) (v : Fin arity → Semiterm L ξ n) : Semiformula L ξ n :=\n  Rewriting.emb o.sentence ⇜ v\n\nprotected class Eq (L : Language) where\n  eq : Semiformula.Operator L 2\n\nprotected class Mem (L : Language) where\n  mem : Semiformula.Operator L 2\n\nnotation \"op(=)\" => Operator.Eq.eq\n\ninstance [Language.Eq L] : Operator.Eq L := ⟨⟨Semiformula.rel Language.Eq.eq Semiterm.bvar⟩⟩\n\ninstance [L.Mem] : Operator.Mem L := ⟨⟨Semiformula.rel Language.Mem.mem Semiterm.bvar⟩⟩\n\nend Operator\n\ndef ballMem [Operator.Mem L] (t : Semiterm L ξ n) (φ : Semiformula L ξ (n + 1)) : Semiformula L ξ n := ∀¹[Operator.Mem.mem.operator ![#0, Rew.bShift t]] φ\n\ndef bexsMem [Operator.Mem L] (t : Semiterm L ξ n) (φ : Semiformula L ξ (n + 1)) : Semiformula L ξ n := ∃¹[Operator.Mem.mem.operator ![#0, Rew.bShift t]] φ\n\nend Semiformula\n\nend FirstOrder\n\nend FFL\n\nend\n\nsection\n\nopen Lean Elab PrettyPrinter Delaborator SubExpr\n\nnamespace Lean.TSyntax\n\nvariable {m : Type → Type}\n\nmeta def freshIdent [Monad m] [MonadQuotation m] : m (TSyntax `ident) := do\n  let name ← Term.mkFreshBinderName\n  return ⟨mkIdent name⟩\n\nend Lean.TSyntax\n\nnamespace FFL.FirstOrder\n\nnamespace Semiformula\n\nvariable {L : Language} {ξ : Type*} {n m : ℕ}\n\n/-- `nestFormulae φ(x₁,…,xₙ) ![Ψ₁(x₁,y₁,…,yₘ), …, Ψₙ(xₙ,y₁,…,yₘ)]` is the formula `∀ x₁, …, xₙ ((Ψ₁(x₁,y₁,…,yₘ) ∧ … ∧ Ψₙ(xₙ,y₁,…,yₘ)) → φ(x₁,…,xₙ))`.\n\nHere, each formula `Ψᵢ` has `m + 1` bound variables, one for expressing a predicate of an `xᵢ`, and `m` remaining ones. In the resulting formula, the bound variables are the `m` remaining bound variables, while all of the original `n` bound variables of `φ` get bounded to a `∀` quantifier.\n\nIntuitively, nestFormulae gives `R(f₁(y₁,…,yₘ), …, fₙ(y₁,…,yₘ))`, the result of substituting functions `f₁, … fₙ` into a relation `R`, using the defining formulae of their graphs (`xᵢ = fᵢ(y₁,…,yₘ)` iff `Ψᵢ(xᵢ,y₁,…,yₘ)`, and `R(x₁,…,xₙ)` iff `φ(x₁,…,xₙ)`). -/\ndef nestFormulae (φ : Semiformula L ξ n) (Ψ : Fin n → Semiformula L ξ (m + 1)) : Semiformula L ξ m :=\n  let σ : Semiformula L ξ (m + n) :=\n    (Matrix.conj fun i : Fin n ↦ Rewriting.subst (Ψ i) (#(i.addCast m) :> fun j ↦ #(j.addNat n))) 🡒\n      Rewriting.subst φ fun i ↦ #(i.addCast m)\n  ∀¹^[n] σ\n\n/-- `nestFormulaeFunc φ(x,x₁,…,xₙ) ![Ψ₁(x₁,y₁,…,yₘ), …, Ψₙ(xₙ,y₁,…,yₘ)]` is the formula `∀ x₁, …, xₙ ((Ψ₁(x₁,y₁,…,yₘ) ∧ … ∧ Ψₙ(xₙ,y₁,…,yₘ)) → φ(x,x₁,…,xₙ))`.\n\nHere, each formula `Ψᵢ` has `m + 1` bound variables, one for expressing a predicate of an `xᵢ`, and `m` remaining ones. In the resulting formula, the bound variables are the `m` remaining bound variables plus `x`, while all of the original bound variables `x₁,…,xₙ` get bounded to a `∀` quantifier.\n\nIntuitively, nestFormulaeFunc gives `F(f₁(y₁,…,yₘ), …, fₙ(y₁,…,yₘ))`, the result of substituting functions `f₁, … fₙ` into a function `F`, using the defining formulae of their graphs (`xᵢ = fᵢ(y₁,…,yₘ)` iff `Ψᵢ(xᵢ,y₁,…,yₘ)`, and `x = F(x₁,…,xₙ)` iff `φ(x,x₁,…,xₙ)`). -/\ndef nestFormulaeFunc (φ : Semiformula L ξ (n + 1)) (Ψ : Fin n → Semiformula L ξ (m + 1)) : Semiformula L ξ (m + 1) :=\n  let σ : Semiformula L ξ ((m + 1) + n) :=\n    (Matrix.conj fun i : Fin n ↦ Rewriting.subst (Ψ i) (#(i.addCast m.succ) :> fun j ↦ #(j.succ.addNat n))) 🡒\n      Rewriting.subst φ (#((0 : Fin (m + 1)).addNat n) :> fun i ↦ #(i.addCast m.succ))\n  ∀¹^[n] σ\n\nend Semiformula\n\nnamespace BinderNotation\n\n@[simp] abbrev finSuccItr {n} (i : Fin n) : (m : ℕ) → Fin (n + m)\n  | 0     => i\n  | m + 1 => (finSuccItr i m).succ\n\ndeclare_syntax_cat first_order_term\n\ndeclare_syntax_cat first_order.quote_type\n\nsyntax:max \"lit\" : first_order.quote_type\n\nsyntax:max \"faf\" : first_order.quote_type\n\nsyntax \"⤫term(\" first_order.quote_type \")[\" ident* \" | \" ident* \" | \" first_order_term:0 \"]\" : term\n\nsyntax \"(\" first_order_term \")\" : first_order_term\n\nsyntax:max ident : first_order_term\n\nsyntax:max \"#\" term:max : first_order_term\n\nsyntax:max \"&\" term:max : first_order_term\n\nsyntax:80 \"!\" term:max first_order_term:81* (\" ⋯\")? : first_order_term\n\nmacro_rules\n  | `(⤫term($type)[ $binders* | $fbinders* | ($e) ]) => `(⤫term($type)[ $binders* | $fbinders* | $e ])\n\nmacro_rules\n  | `(⤫term(lit)[ $binders* | $fbinders* | $x:ident]) => do\n    match binders.idxOf? x with\n    | none =>\n      match fbinders.idxOf? x with\n      | none => Macro.throwErrorAt x \"error: variable did not found.\"\n      | some x =>\n        let i := Syntax.mkNumLit (toString x)\n        `(&$i)\n    | some x =>\n      let i := Syntax.mkNumLit (toString x)\n      `(#$i)\n  | `(⤫term(lit)[ $_*       | $_*        | #$x:term   ]) => `(#$x)\n  | `(⤫term(lit)[ $_*       | $_*        | &$x:term   ]) => `(&$x)\n\nopen Semiformula\n\ndeclare_syntax_cat first_order_formula\n\nsyntax \"⤫formula(\" first_order.quote_type \")[\" ident* \" | \" ident* \" | \" first_order_formula:0 \"]\" : term\n\nsyntax \"(\" first_order_formula \")\" : first_order_formula\n\nsyntax:60 \"!\" term:max first_order_term:61* (\"⋯\")? : first_order_formula\n\nsyntax:32 first_order_formula:33 \" ∧ \" first_order_formula:32 : first_order_formula\n\nsyntax:30 first_order_formula:31 \" ∨ \" first_order_formula:30 : first_order_formula\n\nsyntax:max \"¬\" first_order_formula:35 : first_order_formula\n\nsyntax:10 first_order_formula:9 \" → \" first_order_formula:10 : first_order_formula\n\nsyntax:5 first_order_formula \" ↔ \" first_order_formula : first_order_formula\n\nsyntax:max \"∀ \" ident+ \", \" first_order_formula:0 : first_order_formula\n\nsyntax:max \"∃ \" ident+ \", \" first_order_formula:0 : first_order_formula\n\nmacro_rules\n  | `(⤫formula($type)[ $binders* | $fbinders* | ($e)          ]) => `(⤫formula($type)[ $binders* | $fbinders* | $e ])\n  | `(⤫formula($type)[ $binders* | $fbinders* | $φ ∧ $ψ       ]) => `(@HWedge.hWedge _ _ _ Wedge.instHWedge ⤫formula($type)[ $binders* | $fbinders* | $φ ] ⤫formula($type)[ $binders* | $fbinders* | $ψ ])\n  | `(⤫formula($type)[ $binders* | $fbinders* | $φ ∨ $ψ       ]) => `(@HVee.hVee _ _ _ Vee.instHVee ⤫formula($type)[ $binders* | $fbinders* | $φ ] ⤫formula($type)[ $binders* | $fbinders* | $ψ ])\n  | `(⤫formula($type)[ $binders* | $fbinders* | ¬$φ           ]) => `(@HTilde.hTilde _ _ Tilde.instHTilde ⤫formula($type)[ $binders* | $fbinders* | $φ ])\n  | `(⤫formula($type)[ $binders* | $fbinders* | $φ → $ψ       ]) => `(@HArrow.hArrow _ _ _ Arrow.instHArrow ⤫formula($type)[ $binders* | $fbinders* | $φ ] ⤫formula($type)[ $binders* | $fbinders* | $ψ ])\n  | `(⤫formula($type)[ $binders* | $fbinders* | $φ ↔ $ψ       ]) => `(⤫formula($type)[ $binders* | $fbinders* | $φ ] 🡘 ⤫formula($type)[ $binders* | $fbinders* | $ψ ])\n  | `(⤫formula($type)[ $binders* | $fbinders* | ∀ $xs*, $φ    ]) => do\n    let xs := xs.reverse\n    let binders' : TSyntaxArray `ident ← xs.foldrM\n      (fun z binders' ↦ do\n        if binders.elem z then Macro.throwErrorAt z \"error: variable is duplicated.\" else\n        return binders'.insertIdx 0 z)\n      binders\n    let s : TSyntax `term ← xs.size.rec `(⤫formula($type)[ $binders'* | $fbinders* | $φ ]) (fun _ ψ ↦ ψ >>= fun ψ ↦ `(∀¹ $ψ))\n    return s\n  | `(⤫formula($type)[ $binders* | $fbinders* | ∃ $xs*, $φ    ]) => do\n    let xs := xs.reverse\n    let binders' : TSyntaxArray `ident ← xs.foldrM\n      (fun z binders' ↦ do\n        if binders.elem z then Macro.throwErrorAt z \"error: variable is duplicated.\" else\n        return binders'.insertIdx 0 z)\n      binders\n    let s : TSyntax `term ← xs.size.rec `(⤫formula($type)[ $binders'* | $fbinders* | $φ ]) (fun _ ψ ↦ ψ >>= fun ψ ↦ `(∃¹ $ψ))\n    return s\n\n/--\nA formula in literal notation. For a formula `φ`, write `!φ` to include `φ` in the formula. Identifiers may be written after `!φ` as its bound variables.\n\n`⋯` adds enough unnamed bound variables to fill up the arity of `φ`, with indices starting after the last named identifier. For example, assume `φ` is a `Semiformula L k`, and consider the formula `“x y z. !φ x y ⋯”`. Here `x`, `y`, and `z` are the bound variables `#0`, `#1`, and `#2` respectively. Then `!φ x y ⋯` will add `k - 2` new bound variables, and expand to `!φ #0 #1 #3 #4 ... #(k + 1)`.\n-/\nmacro_rules\n  | `(⤫formula(lit)[ $binders* | $fbinders* | !$φ:term $vs:first_order_term*   ]) => do\n    let v ← vs.foldrM (β := Lean.TSyntax _) (init := ← `(![])) (fun a s ↦ `(⤫term(lit)[ $binders* | $fbinders* | $a ] :> $s))\n    `($φ ⇜ $v)\n  | `(⤫formula(lit)[ $binders* | $fbinders* | !$φ:term $vs:first_order_term* ⋯ ]) =>\n    do\n    let length := Syntax.mkNumLit (toString binders.size)\n    let v ← vs.foldrM (β := Lean.TSyntax _) (init := ← `(fun x ↦ #(finSuccItr x $length)))\n      (fun a s ↦ `(⤫term(lit)[ $binders* | $fbinders* | $a] :> $s))\n    `($φ ⇜ $v)\n\nsyntax \"“\" ident* \"| \"  first_order_formula:0 \"”\" : term\n\nsyntax \"“\" ident* \". \"  first_order_formula:0 \"”\" : term\n\nsyntax \"“\" first_order_formula:0 \"”\" : term\n\nmacro_rules\n  | `(“ $e:first_order_formula ”)              => `(⤫formula(lit)[           |            | $e ])\n  | `(“ $binders*. $e:first_order_formula ”)   => `(⤫formula(lit)[ $binders* |            | $e ])\n  | `(“ $fbinders* | $e:first_order_formula ”) => `(⤫formula(lit)[           | $fbinders* | $e ])\n\nsyntax:45 first_order_term:45 \" = \" first_order_term:0 : first_order_formula\n\nsyntax:45 first_order_term:45 \" ∈ \" first_order_term:0 : first_order_formula\n\nsyntax:45 first_order_term:45 \" ≠ \" first_order_term:0 : first_order_formula\n\nsyntax:45 first_order_term:45 \" ∉ \" first_order_term:0 : first_order_formula\n\nsyntax:max \"∀ \" ident \" ∈ \" first_order_term \", \" first_order_formula:0 : first_order_formula\n\nsyntax:max \"∃ \" ident \" ∈ \" first_order_term \", \" first_order_formula:0 : first_order_formula\n\nmacro_rules\n  | `(⤫formula(lit)[ $binders* | $fbinders* | $t:first_order_term = $u:first_order_term ]) => `(Semiformula.Operator.operator Operator.Eq.eq ![⤫term(lit)[ $binders* | $fbinders* | $t ], ⤫term(lit)[ $binders* | $fbinders* | $u ]])\n  | `(⤫formula(lit)[ $binders* | $fbinders* | $t:first_order_term ∈ $u:first_order_term ]) => `(Semiformula.Operator.operator Operator.Mem.mem ![⤫term(lit)[ $binders* | $fbinders* | $t ], ⤫term(lit)[ $binders* | $fbinders* | $u ]])\n  | `(⤫formula(lit)[ $binders* | $fbinders* | $t:first_order_term ≠ $u:first_order_term ]) => `(@HTilde.hTilde _ _ Tilde.instHTilde (Semiformula.Operator.operator Operator.Eq.eq ![⤫term(lit)[ $binders* | $fbinders* | $t ], ⤫term(lit)[ $binders* | $fbinders* | $u ]]))\n  | `(⤫formula(lit)[ $binders* | $fbinders* | $t:first_order_term ∉ $u:first_order_term ]) => `(@HTilde.hTilde _ _ Tilde.instHTilde (Semiformula.Operator.operator Operator.Mem.mem ![⤫term(lit)[ $binders* | $fbinders* | $t ], ⤫term(lit)[ $binders* | $fbinders* | $u ]]))\n\nmacro_rules\n  | `(⤫formula(lit)[ $binders* | $fbinders* | ∀ $x ∈ $t, $φ ]) => do\n    if binders.elem x then Macro.throwErrorAt x \"error: variable is duplicated.\" else\n    `(Semiformula.ballMem ⤫term(lit)[ $binders* | $fbinders* | $t ] ⤫formula(lit)[ $x $binders* | $fbinders* | $φ ])\n  | `(⤫formula(lit)[ $binders* | $fbinders* | ∃ $x ∈ $t, $φ ]) => do\n    if binders.elem x then Macro.throwErrorAt x \"error: variable is duplicated.\" else\n    `(Semiformula.bexsMem ⤫term(lit)[ $binders* | $fbinders* | $t ] ⤫formula(lit)[ $x $binders* | $fbinders* | $φ ])\n\nmacro_rules\n  | `(⤫formula(faf)[ $binders* | $fbinders* | !$φ:term $vs:first_order_term*   ]) => do\n    let Ψ ← vs.foldrM (β := Lean.TSyntax _) (init := ← `(![])) fun a s ↦ do\n      let x : TSyntax `ident ← TSyntax.freshIdent\n      `(⤫term(faf)[ $x $binders* | $fbinders* | $a ] :> $s)\n    `(($φ).nestFormulae $Ψ)\n  | `(⤫formula(faf)[ $binders* | $fbinders* | !$φ:term $vs:first_order_term* ⋯ ]) => do\n    let length := Syntax.mkNumLit (toString binders.size)\n    let Ψ ← vs.foldrM (β := Lean.TSyntax _) (init := ← `(fun x ↦ #(finSuccItr x $length))) fun a s ↦ do\n      let x : TSyntax `ident ← TSyntax.freshIdent\n      `(⤫term(faf)[ $x $binders* | $fbinders* | $a] :> $s)\n    `(($φ).nestFormulae $Ψ)\n\nmacro_rules\n  | `(⤫term(faf)[ $binders* | $fbinders* | $x:ident                         ]) => do\n    match binders.idxOf? x with\n    | none =>\n      match fbinders.idxOf? x with\n      | none => Macro.throwErrorAt x \"error: variable does not appeared.\"\n      | some x =>\n        let i := Syntax.mkNumLit (toString x)\n        `(“#0 = &$i”)\n    | some x =>\n      let i := Syntax.mkNumLit (toString x)\n      `(“#0 = #$i”)\n  | `(⤫term(faf)[ $binders* | $fbinders* | !$f:term $vs:first_order_term*   ]) => do\n    let Ψ ← vs.foldrM (β := Lean.TSyntax _) (init := ← `(![])) fun a s ↦ do\n      `(⤫term(faf)[ $binders* | $fbinders* | $a ] :> $s)\n    `(($f).nestFormulaeFunc $Ψ)\n  | `(⤫term(faf)[ $binders* | $fbinders* | !$f:term $vs:first_order_term* ⋯ ]) => do\n    let length := Syntax.mkNumLit (toString binders.size)\n    let Ψ ← vs.foldrM (β := Lean.TSyntax _) (init := ← `(fun x ↦ “#0 = #(finSuccItr x $length)”)) fun a s ↦ do\n      `(⤫term(faf)[ $binders* | $fbinders* | $a] :> $s)\n    `(($f).nestFormulaeFunc $Ψ)\n\nmacro_rules\n  | `(⤫formula(faf)[ $binders* | $fbinders* | $t:first_order_term = $u:first_order_term ]) => do\n    let x₁ : TSyntax `ident ← TSyntax.freshIdent\n    let x₂ : TSyntax `ident ← TSyntax.freshIdent\n    `(∀¹ (⤫term(faf)[ $x₁ $binders* | $fbinders* | $t ] 🡒 ∀¹ (⤫term(faf)[ $x₁ $x₂ $binders* | $fbinders* | $u ] 🡒 “#1 = #0”)))\n  | `(⤫formula(faf)[ $binders* | $fbinders* | $t:first_order_term ≠ $u:first_order_term ]) => do\n    let x₁ : TSyntax `ident ← TSyntax.freshIdent\n    let x₂ : TSyntax `ident ← TSyntax.freshIdent\n    `(∀¹ (⤫term(faf)[ $x₁ $binders* | $fbinders* | $t ] 🡒 ∀¹ (⤫term(faf)[ $x₁ $x₂ $binders* | $fbinders* | $u ] 🡒 “#1 ≠ #0”)))\n  | `(⤫formula(faf)[ $binders* | $fbinders* | $t:first_order_term ∈ $u:first_order_term ]) => do\n    let x₁ : TSyntax `ident ← TSyntax.freshIdent\n    let x₂ : TSyntax `ident ← TSyntax.freshIdent\n    `(∀¹ (⤫term(faf)[ $x₁ $binders* | $fbinders* | $t ] 🡒 ∀¹ (⤫term(faf)[ $x₁ $x₂ $binders* | $fbinders* | $u ] 🡒 “#1 ∈ #0”)))\n  | `(⤫formula(faf)[ $binders* | $fbinders* | $t:first_order_term ∉ $u:first_order_term ]) => do\n    let x₁ : TSyntax `ident ← TSyntax.freshIdent\n    let x₂ : TSyntax `ident ← TSyntax.freshIdent\n    `(∀¹ (⤫term(faf)[ $x₁ $binders* | $fbinders* | $t ] 🡒 ∀¹ (⤫term(faf)[ $x₁ $x₂ $binders* | $fbinders* | $u ] 🡒 “#1 ∉ #0”)))\n\nmacro_rules\n  | `(⤫formula(faf)[ $binders* | $fbinders* | ∀ $x ∈ $t, $φ ]) => do\n    if binders.elem x then Macro.throwErrorAt x \"error: variable is duplicated.\" else\n      let vt : TSyntax `ident ← TSyntax.freshIdent\n      `(∀¹ (⤫term(faf)[ $vt $binders* | $fbinders* | $t ] 🡒 Semiformula.ballMem #0 ⤫formula(faf)[ $x $vt $binders* | $fbinders* | $φ ]))\n  | `(⤫formula(faf)[ $binders* | $fbinders* | ∃ $x ∈ $t, $φ ]) => do\n    if binders.elem x then Macro.throwErrorAt x \"error: variable is duplicated.\" else\n      let vt : TSyntax `ident ← TSyntax.freshIdent\n      `(∀¹ (⤫term(faf)[ $vt $binders* | $fbinders* | $t ] 🡒 Semiformula.bexsMem #0 ⤫formula(faf)[ $x $vt $binders* | $fbinders* | $φ ]))\n\nsyntax \"f“\" ident* \"| \"  first_order_formula:0 \"”\" : term\n\nsyntax \"f“\" ident* \". \"  first_order_formula:0 \"”\" : term\n\nsyntax \"f“\" first_order_formula:0 \"”\" : term\n\n/-- A formula in formula-as-function notation. Use `f“⋯. ⋯”` for bound variables, and `f“⋯ | ⋯”` for free variables. -/\nmacro_rules\n  | `(f“ $e:first_order_formula ”)              => `(⤫formula(faf)[           |            | $e ])\n  | `(f“ $fbinders* | $e:first_order_formula ”) => `(⤫formula(faf)[           | $fbinders* | $e ])\n  | `(f“ $binders*. $e:first_order_formula ”)   => `(⤫formula(faf)[ $binders* |            | $e ])\n\nend BinderNotation\n\nend FFL.FirstOrder\n\nend\n\nsection\n\nnamespace FFL\n\nnamespace OneSidedLK\n\nvariable {F : Type*} [LogicalConnective F] [LogicalNeutral F]\n\nabbrev Pullback (𝔇 : Multiset F → Type*) {G : Type*} [LogicalConnective G]\n    [LogicalNeutral G] (f : G →ˡᶜ F) : Multiset G → Type _ := fun Γ ↦ 𝔇 (Γ.map f)\n\nend OneSidedLK\n\nend FFL\n\nend\n\nsection\n\nnamespace FFL\n\nnamespace FirstOrder\n\nvariable {L : Language}\n\nabbrev Sequent (L : Language) := Multiset (Proposition L)\n\n/-- Derivation for $\\mathbf{LK}$ -/\ninductive Derivation : Sequent L → Type _\n| identity {k : ℕ} (r : L.Rel k) (v : Fin k → Semiterm L ℕ 0) : Derivation ⦃.rel r v, .nrel r v⦄\n| cut {Γ : Sequent L} {φ : Proposition L} {Δ : Sequent L} :\n    Derivation (Γ + ⦃φ⦄) → Derivation (Δ + ⦃∼φ⦄) → Derivation (Γ + Δ)\n| contraction {Δ Γ : Sequent L} : Derivation Δ → Δ ⊆ Γ → Derivation Γ\n| verum : Derivation ⦃⊤⦄\n| or {Γ : Multiset (Proposition L)} {φ ψ : Proposition L} :\n    Derivation (HAdd.hAdd (γ := Sequent L) Γ ⦃φ, ψ⦄) →\n    Derivation (HAdd.hAdd (γ := Sequent L) Γ ⦃φ ⋎ ψ⦄)\n| and {Γ : Multiset (Proposition L)} {φ ψ : Proposition L} :\n    Derivation (HAdd.hAdd (γ := Sequent L) Γ ⦃φ⦄) →\n    Derivation (HAdd.hAdd (γ := Sequent L) Γ ⦃ψ⦄) →\n    Derivation (HAdd.hAdd (γ := Sequent L) Γ ⦃φ ⋏ ψ⦄)\n| all {Γ : Multiset (Semiproposition L 0)} {φ : Semiproposition L (0 + 1)} :\n    Derivation (HAdd.hAdd (γ := Sequent L) Γ⁺ ⦃φ.free⦄) →\n    Derivation (HAdd.hAdd (γ := Sequent L) Γ ⦃∀¹ φ⦄)\n| exs {Γ : Multiset (Semiproposition L 0)} {φ : Semiproposition L (Nat.succ 0)}\n    {t : Semiterm L ℕ 0} :\n    Derivation (HAdd.hAdd (γ := Sequent L) Γ ⦃φ/[t]⦄) →\n    Derivation (HAdd.hAdd (γ := Sequent L) Γ ⦃∃¹ φ⦄)\n\nstructure Theory.Proof (T : Theory L) (σ : Sentence L) where\n  axioms : Multiset (Sentence L)\n  axioms_mem : ∀ ψ ∈ axioms, ψ ∈ T\n  derivation : OneSidedLK.Pullback Derivation Rewriting.emb (⦃σ⦄ + ∼axioms)\n\nnamespace Theory.Proof\n\ninstance : Entailment (Theory L) (Sentence L) where\n  Prf := Theory.Proof\n\nend Theory.Proof\n\nend FirstOrder\n\nend FFL\n\nend\n\nsection\n\nnamespace FFL\n\nnamespace FirstOrder\n\nvariable {L : Language} {ξ : Type*} [Semiformula.Operator.Eq L]\nvariable {n : ℕ}\n\nnamespace Theory\n\nsection Eq\n\nvariable (L)\n\nabbrev Eq.refl : Sentence L := “∀ x, x = x”\n\nabbrev Eq.symm : Sentence L := “∀ x y, x = y → y = x”\n\nabbrev Eq.trans : Sentence L := “∀ x y z, x = y → y = z → x = z”\n\nvariable {L}\n\nabbrev Eq.funcExt {k : ℕ} (f : L.Func k) : Sentence L :=\n  let σ : Semisentence L (k + k) :=\n    (Matrix.conj fun i : Fin k ↦ “#(i.addCast k) = #(i.addNat k)”) 🡒\n      op(=).operator ![Semiterm.func f (fun i ↦ #(i.addCast k)), Semiterm.func f (fun i ↦ #(i.addNat k))]\n  ∀¹* σ\n\nabbrev Eq.relExt {k : ℕ} (r : L.Rel k) : Sentence L :=\n  let σ : Semisentence L (k + k) :=\n    (Matrix.conj fun i : Fin k ↦ “#(i.addCast k) = #(i.addNat k)”) 🡒\n      Semiformula.rel r (fun i ↦ #(i.addCast k)) 🡒 Semiformula.rel r (fun i ↦ #(i.addNat k))\n  ∀¹* σ\n\nvariable (L)\n\ninductive eqAxiom : Theory L\n  | refl : eqAxiom (Eq.refl L)\n  | symm : eqAxiom (Eq.symm L)\n  | trans : eqAxiom (Eq.trans L)\n  | funcExt {k : ℕ} (f : L.Func k) : eqAxiom (Eq.funcExt f)\n  | relExt {k : ℕ} (r : L.Rel k) : eqAxiom (Eq.relExt r)\n\nnotation \"𝗘𝗤\" => eqAxiom\n\nend Eq\n\nend Theory\n\nnamespace Semiformula\n\ndef existsUnique {ξ} (φ : Semiformula L ξ (n + 1)) : Semiformula L ξ n :=\n  “∃ y, !φ y ⋯ ∧ ∀ z, !φ z ⋯ → z = y”\n\nprefix:64 \"∃¹! \" => existsUnique\n\nend Semiformula\n\nnamespace BinderNotation\n\nopen Lean PrettyPrinter Delaborator SubExpr\n\nsyntax:max \"∃! \" first_order_formula:0 : first_order_formula\n\nsyntax:max \"∃! \" ident \", \" first_order_formula:0 : first_order_formula\n\nmacro_rules\n  | `(⤫formula($type)[ $binders* | $fbinders* | ∃! $φ:first_order_formula ]) => do\n    let v := mkIdent (Name.mkSimple (\"var\" ++ toString binders.size))\n    let binders' := binders.insertIdx 0 v\n    `(∃¹! ⤫formula($type)[ $binders'* | $fbinders* | $φ])\n  | `(⤫formula($type)[ $binders* | $fbinders* | ∃! $x, $φ ])                 => do\n    if binders.elem x then Macro.throwErrorAt x \"error: variable is duplicated.\" else\n    let binders' := binders.insertIdx 0 x\n    `(∃¹! ⤫formula($type)[ $binders'* | $fbinders* | $φ ])\n\nend BinderNotation\n\nend FirstOrder\n\nend FFL\n\nend\n\nsection\n\nnamespace FFL.FirstOrder\n\nnamespace Language\n\nnamespace Set\n\nabbrev Func : ℕ → Type := fun _ ↦ Empty\n\ninductive Rel : ℕ → Type\n  | eq : Rel 2\n  | mem : Rel 2\n\nend Set\n\n/-- Language of set theory -/\n@[reducible]\ndef set : Language where\n  Func := Set.Func\n  Rel := Set.Rel\n\nnotation \"ℒₛₑₜ\" => set\n\nnamespace Set\n\ninstance : (ℒₛₑₜ).Eq := ⟨Rel.eq⟩\n\ninstance : (ℒₛₑₜ).Mem := ⟨Rel.mem⟩\n\nend Set\n\nend Language\n\nabbrev SetTheory := Theory ℒₛₑₜ\n\nabbrev SetTheorySemisentence (n : ℕ) := Semisentence ℒₛₑₜ n\n\nabbrev SetTheorySentence := Sentence ℒₛₑₜ\n\nabbrev SetTheorySemiproposition (n : ℕ) := Semiproposition ℒₛₑₜ n\n\nend FFL.FirstOrder\n\nend\n\nsection\n\nnamespace FFL.FirstOrder.SetTheory\n\ndef isSubsetOf : SetTheorySemisentence 2 := “x y. ∀ z ∈ x, z ∈ y”\n\nsyntax:45 first_order_term:45 \" ⊆ \" first_order_term:0 : first_order_formula\n\nopen Lean Elab PrettyPrinter Delaborator SubExpr in\nmacro_rules\n  | `(⤫formula($type)[ $binders* | $fbinders* | $t:first_order_term ⊆ $u:first_order_term ]) =>\n    `(⤫formula($type)[ $binders* | $fbinders* | !isSubsetOf $t:first_order_term $u:first_order_term ])\n\ndef isEmpty : SetTheorySemisentence 1 := “x. ∀ y, y ∉ x”\n\ndef isNonempty : SetTheorySemisentence 1 := “x. ∃ y, y ∈ x”\n\ndef isSucc : SetTheorySemisentence 2 := “y x. ∀ z, z ∈ y ↔ z = x ∨ z ∈ x”\n\nnamespace Axiom\n\n/-- Axiom of empty set. -/\ndef empty : SetTheorySentence := “∃ e, ∀ y, y ∉ e”\n\n/-- Axiom of extentionality. -/\ndef extentionality : SetTheorySentence := “∀ x y, x = y ↔ ∀ z, z ∈ x ↔ z ∈ y”\n\n/-- Axiom of pairing. -/\ndef pairing : SetTheorySentence := “∀ x y, ∃ z, ∀ w, w ∈ z ↔ w = x ∨ w = y”\n\n/-- Axiom of union. -/\ndef union : SetTheorySentence := “∀ x, ∃ y, ∀ z, z ∈ y ↔ ∃ w ∈ x, z ∈ w”\n\n/-- Axiom of power set. -/\ndef power : SetTheorySentence := “∀ x, ∃ y, ∀ z, z ∈ y ↔ z ⊆ x”\n\n/-- Axiom of infinity. -/\ndef infinity : SetTheorySentence := “∃ I, (∀ e, !isEmpty e → e ∈ I) ∧ (∀ x ∈ I, ∀ x', !isSucc x' x → x' ∈ I)”\n\n/-- Axiom of foundation. -/\ndef foundation : SetTheorySentence := “∀ x, !isNonempty x → ∃ y ∈ x, ∀ z ∈ x, z ∉ y”\n\n/-- Axiom schema of separation (Aussonderungsaxiom). -/\ndef separationSchema (φ : SetTheorySemiproposition 1) : SetTheorySentence :=\n  .univCl “∀ x, ∃ y, ∀ z, z ∈ y ↔ z ∈ x ∧ !φ z”\n\n/-- Axiom schema of replacement. -/\ndef replacementSchema (φ : SetTheorySemiproposition 2) : SetTheorySentence :=\n  .univCl “(∀ x, ∃! y, !φ x y) → ∀ X, ∃ Y, ∀ y, y ∈ Y ↔ ∃ x ∈ X, !φ x y”\n\n/-- Axiom of choice. -/\ndef choice : SetTheorySentence :=\n  “∀ 𝓧, (∀ X ∈ 𝓧, !isNonempty X) ∧ (∀ X ∈ 𝓧, ∀ Y ∈ 𝓧, (∃ z, z ∈ X ∧ z ∈ Y) → X = Y) → ∃ C, ∀ X ∈ 𝓧, ∃! x, x ∈ C ∧ x ∈ X”\n\nend Axiom\n\n/-- Zermelo-Fraenkel set theory. -/\ninductive ZermeloFraenkel : SetTheory\n  /-- Axiom of equality. -/\n  | axiom_of_equality : ∀ φ ∈ 𝗘𝗤 ℒₛₑₜ, ZermeloFraenkel φ\n  /-- Axiom of empty set. -/\n  | axiom_of_empty_set : ZermeloFraenkel Axiom.empty\n  /-- Axiom of extentionality. -/\n  | axiom_of_extentionality : ZermeloFraenkel Axiom.extentionality\n  /-- Axiom of pairing. -/\n  | axiom_of_pairing : ZermeloFraenkel Axiom.pairing\n  /-- Axiom of union. -/\n  | axiom_of_union : ZermeloFraenkel Axiom.union\n  /-- Axiom of power set. -/\n  | axiom_of_power_set : ZermeloFraenkel Axiom.power\n  /-- Axiom of infinity. -/\n  | axiom_of_infinity : ZermeloFraenkel Axiom.infinity\n  /-- Axiom of foundation. -/\n  | axiom_of_foundation : ZermeloFraenkel Axiom.foundation\n  /-- Axiom schema of separation. -/\n  | axiom_of_separation (φ : SetTheorySemiproposition 1) : ZermeloFraenkel (Axiom.separationSchema φ)\n  /-- Axiom schema of replacement. -/\n  | axiom_of_replacement (φ : SetTheorySemiproposition 2) : ZermeloFraenkel (Axiom.replacementSchema φ)\n\nnotation \"𝗭𝗙\" => ZermeloFraenkel\n\n/-- AC: Axiom of choice. -/\ndef AxiomOfChoice : SetTheory := {Axiom.choice}\n\nnotation \"𝗔𝗖\" => AxiomOfChoice\n\n/-- Zermelo-Fraenkel set theory with axiom of choice. -/\nabbrev ZermeloFraenkelChoice : SetTheory := 𝗭𝗙 ∪ 𝗔𝗖\n\nnotation \"𝗭𝗙𝗖\" => ZermeloFraenkelChoice\n\nend FFL.FirstOrder.SetTheory\n\nend\n\nsection\n\nnamespace FFL.FirstOrder.SetTheory\n\ndef doubleton.dfn : SetTheorySemisentence 3 := “p x y. ∀ z, z ∈ p ↔ z = x ∨ z = y”\n\ndef singleton.dfn : SetTheorySemisentence 2 := “p x. !doubleton.dfn p x x”\n\ndef sUnion.dfn : SetTheorySemisentence 2 := “u x. ∀ z, z ∈ u ↔ ∃ w ∈ x, z ∈ w”\n\ndef union.dfn : SetTheorySemisentence 3 := “u x y. ∀ d, !doubleton.dfn d x y → !sUnion.dfn u d”\n\ndef insert.dfn : SetTheorySemisentence 3 := “u x y. ∀ s, !singleton.dfn s x → !union.dfn u s y”\n\ndef sInter.dfn : SetTheorySemisentence 2 := “u x. ∀ z, z ∈ u ↔ !isNonempty x ∧ ∀ y ∈ x, z ∈ y”\n\ndef kpair.dfn : SetTheorySemisentence 3 :=\n  “k x y. ∀ x', !singleton.dfn x' x → ∀ z, !doubleton.dfn z x y → !doubleton.dfn k x' z”\n\ndef kpair.π₁.dfn : SetTheorySemisentence 2 := “p₁ x. ∀ i, !sInter.dfn i x → !sUnion.dfn p₁ i”\n\ndef kpair.π₂.dfn : SetTheorySemisentence 2 :=\n  “p₂ x. ∀ u, !sUnion.dfn u x → ∀ i, !sInter.dfn i x → ∀ s, (∀ z, z ∈ s ↔ (z ∈ u ∧ (z ∈ i → u = i))) → !sUnion.dfn p₂ s”\n\ndef prod.dfn : SetTheorySemisentence 3 := “p X Y. ∀ z, z ∈ p ↔ ∃ x ∈ X, ∃ y ∈ Y, !kpair.dfn z x y”\n\nabbrev succ.dfn := isSucc\n\ndef IsInductive.dfn : SetTheorySemisentence 1 :=\n  “x. (∀ e, !isEmpty e → e ∈ x) ∧ (∀ y ∈ x, ∀ y', !succ.dfn y' y → y' ∈ x)”\n\ndef isω : SetTheorySemisentence 1 := “ω. ∀ x, x ∈ ω ↔ ∀ I, !IsInductive.dfn I → x ∈ I”\n\nend FFL.FirstOrder.SetTheory\n\nend\n\nsection\n\nnamespace FFL.FirstOrder.SetTheory\n\nsection range\n\ndef range.dfn : SetTheorySemisentence 2 := f“r R. ∀ y, y ∈ r ↔ ∃ x, !kpair.dfn x y ∈ R”\n\nend range\n\ndef function.dfn : SetTheorySemisentence 3 := f“F Y X. ∀ f, f ∈ F ↔ f ⊆ !prod.dfn X Y ∧ ∀ x ∈ X, ∃! y, !kpair.dfn x y ∈ f”\n\ndef IsFunction.dfn : SetTheorySemisentence 1 := f“f. ∃ X Y, f ∈ !function.dfn Y X”\n\ndef value.dfn : SetTheorySemisentence 3 := f“v f x. ∀ z, z ∈ v ↔ z ∈ !sUnion.dfn (!range.dfn f) ∧ ∃ y, z ∈ y ∧ !kpair.dfn x y ∈ f”\n\nend FFL.FirstOrder.SetTheory\n\nend\n\nend FormalizedFormalLogic\n\nnamespace Constant14\n\nopen FFL FFL.FirstOrder Turing BusyBeaver FFL.FirstOrder.SetTheory\n\nsection BackgroundDefs\n\n/-- `O` is the least set containing `x` and closed under `f`. -/\ndef orbit : SetTheorySemisentence 3 :=\n  f“O f x. !IsFunction.dfn f ∧\n    ∀ z, z ∈ O ↔ ∀ A, (x ∈ A ∧ ∀ y ∈ A, !value.dfn f y ∈ A) → z ∈ A”\n\n/-- The von Neumann ordinal `0 = ∅`. -/\ndef zero : SetTheorySemisentence 1 :=\n  “zero. !isEmpty zero”\n\n/-- The graph formula for the von Neumann numeral associated with a Lean `n : ℕ`. -/\ndef numeral : ℕ → SetTheorySemisentence 1\n  | 0     => “zero. !isEmpty zero”\n  | n + 1 => succ.dfn.nestFormulaeFunc ![numeral n]\n\n/--\n`⟨0,n⟩` represents `n`, while `⟨1,n⟩` represents `-(n+1)`.\n-/\ndef integers : SetTheorySemisentence 1 :=\n  f“Z. Z = !prod.dfn (!(numeral 2)) (!isω)”\n\n/-- Zero in the integer encoding: `⟨0,0⟩`. -/\ndef integerZero : SetTheorySemisentence 1 :=\n  f“z. z = !kpair.dfn (!(numeral 0)) (!(numeral 0))”\n\n/-- Configurations with `n` state codes, an integer head position, and a binary tape. -/\ndef configSet (n : ℕ) : SetTheorySemisentence 1 :=\n  f“Z. Z =\n    !prod.dfn (!(numeral n)) (!prod.dfn (!integers) (!function.dfn (!(numeral 2)) (!integers)))”\n\n/-- Integer successor for the encoding `⟨0,n⟩ = n`, `⟨1,n⟩ = -(n+1)`. -/\ndef integerSucc.dfn : SetTheorySemisentence 2 :=\n  f“z' z.\n    (z ∈ (!integers) ∧\n      ((!kpair.π₁.dfn z = (!(numeral 0)) ∧\n        z' = !kpair.dfn (!(numeral 0)) (!succ.dfn (!kpair.π₂.dfn z))) ∨\n       (!kpair.π₁.dfn z = (!(numeral 1)) ∧\n        ((!kpair.π₂.dfn z = (!(numeral 0)) ∧ z' = (!integerZero)) ∨\n         ∃ n ∈ (!isω),\n          !kpair.π₂.dfn z = !succ.dfn n ∧\n          z' = !kpair.dfn (!(numeral 1)) n)))) ∨\n    (z ∉ (!integers) ∧ z' = (!integerZero))”\n\n/-- Integer predecessor for the encoding `⟨0,n⟩ = n`, `⟨1,n⟩ = -(n+1)`. -/\ndef integerPred.dfn : SetTheorySemisentence 2 :=\n  f“z' z.\n    (z ∈ (!integers) ∧ z' ∈ (!integers) ∧ !integerSucc.dfn z z') ∨\n    (z ∉ (!integers) ∧ z' = (!integerZero))”\n\ndef setPair (x y : SetTheorySemisentence 1) : SetTheorySemisentence 1 :=\n  kpair.dfn.nestFormulaeFunc ![x, y]\n\ndef setTriple (x y z : SetTheorySemisentence 1) : SetTheorySemisentence 1 :=\n  setPair x (setPair y z)\n\ndef finiteSet : List (SetTheorySemisentence 1) → SetTheorySemisentence 1\n  | []      => zero\n  | x :: xs => insert.dfn.nestFormulaeFunc ![x, finiteSet xs]\n\ndef directionCode : Option Dir → ℕ\n  | some .left  => 0\n  | none        => 1\n  | some .right => 2\n\ndef stateCode {n : ℕ} : Option (Fin n) → ℕ\n  | some q => q.val\n  | none   => n\n\ndef transitionCode {n : ℕ} [NeZero n] (M : Machine (Fin 2) (Fin n))\n    (q : Fin n) (symbol : Fin 2) : ℕ × ℕ × ℕ :=\n  match M q symbol with\n  | some (q', stmt) => (stateCode q', stmt.symbol.val, directionCode (some stmt.dir))\n  | none            => (n, symbol.val, directionCode none)\n\ndef transitionEntry (q symbol q' written direction : ℕ) :\n    SetTheorySemisentence 1 :=\n  setPair (setPair (numeral q) (numeral symbol))\n    (setTriple (numeral q') (numeral written) (numeral direction))\n\ndef transitionEntries {n : ℕ} [NeZero n] (M : Machine (Fin 2) (Fin n)) :\n    List (SetTheorySemisentence 1) :=\n  (List.ofFn fun q : Fin n ↦\n    List.ofFn fun symbol : Fin 2 ↦\n      letI output := transitionCode M q symbol\n      transitionEntry q.val symbol.val output.1 output.2.1 output.2.2).flatten ++\n  List.ofFn fun symbol : Fin 2 ↦\n    transitionEntry n symbol.val n symbol.val (directionCode none)\n\ndef transitionDomain (n : ℕ) : SetTheorySemisentence 1 :=\n  prod.dfn.nestFormulaeFunc ![numeral (n + 1), numeral 2]\n\ndef transitionCodomain (n : ℕ) : SetTheorySemisentence 1 :=\n  prod.dfn.nestFormulaeFunc ![\n    numeral (n + 1),\n    prod.dfn.nestFormulaeFunc ![numeral 2, numeral 3]]\n\n/-- The set-theoretic graph of a two-symbol Turing machine's totalized transition function. -/\ndef instructionTable {n : ℕ} [NeZero n] (M : Machine (Fin 2) (Fin n)) : SetTheorySemisentence 1 :=\n  finiteSet (transitionEntries M)\n\n/-- Given some configuration, the next configuartion of a Turing machine (abstractified). -/\ndef nextConfig (n : ℕ) (instructions : SetTheorySemisentence 1) : SetTheorySemisentence 2 :=\n  f“z' z.\n    -- both z and z' are configurations\n    z ∈ !(configSet n) ∧\n    z' ∈ !(configSet n) ∧\n    ∃ t, t =\n      -- the new configuration has the state as instructed\n      !value.dfn (!instructions)\n        (!kpair.dfn (!kpair.π₁.dfn z)\n          (!value.dfn (!kpair.π₂.dfn (!kpair.π₂.dfn z))\n            (!kpair.π₁.dfn (!kpair.π₂.dfn z)))) ∧\n      !kpair.π₁.dfn z' = !kpair.π₁.dfn t ∧\n      -- shift the cursor of the machine, depending on the direction\n      ((!kpair.π₂.dfn (!kpair.π₂.dfn t) = (!(numeral 0)) ∧\n          !integerPred.dfn (!kpair.π₁.dfn (!kpair.π₂.dfn z'))\n            (!kpair.π₁.dfn (!kpair.π₂.dfn z))) ∨\n        (!kpair.π₂.dfn (!kpair.π₂.dfn t) = (!(numeral 1)) ∧\n          !kpair.π₁.dfn (!kpair.π₂.dfn z') = !kpair.π₁.dfn (!kpair.π₂.dfn z)) ∨\n        (!kpair.π₂.dfn (!kpair.π₂.dfn t) = (!(numeral 2)) ∧\n          !integerSucc.dfn (!kpair.π₁.dfn (!kpair.π₂.dfn z'))\n            (!kpair.π₁.dfn (!kpair.π₂.dfn z)))) ∧\n      -- the new value of the tape at the cursor\n      !value.dfn (!kpair.π₂.dfn (!kpair.π₂.dfn z'))\n          (!kpair.π₁.dfn (!kpair.π₂.dfn z)) =\n        !kpair.π₁.dfn (!kpair.π₂.dfn t) ∧\n      -- the new tape agrees with the old in all but at most one place\n      ∀ k ∈ !integers,\n        k ≠ !kpair.π₁.dfn (!kpair.π₂.dfn z) →\n          !value.dfn (!kpair.π₂.dfn (!kpair.π₂.dfn z')) k =\n            !value.dfn (!kpair.π₂.dfn (!kpair.π₂.dfn z)) k”\n\n/-- The graph of the one-step configuration function determined by the transition table `instructions`. -/\ndef nextStep (n : ℕ) (instructions : SetTheorySemisentence 1) : SetTheorySemisentence 1 :=\n  f“p. ∀ e, e ∈ p ↔ ∃ z ∈ !(configSet n), ∃ z' ∈ !(configSet n),\n    e = !kpair.dfn z z' ∧ !(nextConfig n instructions) z' z”\n\n/-- The binary tape whose value is the blank symbol `0` at every integer position. -/\ndef emptyTape : SetTheorySemisentence 1 :=\n  f“f. f ∈ !function.dfn (!(numeral 2)) (!integers) ∧\n    ∀ i ∈ !integers, !value.dfn f i = !(numeral 0)”\n\n/-- The initial configuration: state `0`, head position `0`, and the blank tape. -/\ndef initialConfig : SetTheorySemisentence 1 :=\n  f“z. z = !kpair.dfn (!(numeral 0)) (!kpair.dfn (!integerZero) (!emptyTape))”\n\n/-- The set-theoretic sentence asserting that `M` reaches its encoded halting state. -/\ndef halts {n : ℕ} [NeZero n] (M : Machine (Fin 2) (Fin n)) : SetTheorySentence :=\n  f“∃ m ∈ !integers, ∃ g ∈ !function.dfn (!(numeral 2)) (!integers),\n    !kpair.dfn (!(numeral n)) (!kpair.dfn m g) ∈\n      !orbit (!(nextStep (n + 1) (instructionTable M))) (!initialConfig)”\n\nend BackgroundDefs\n\n/-- The smallest natural number `n` for which the value of BB(n) can not be proven. More precisely:\nthe smallest `n` for which there is a Turing machine with `n` states such that ZFC can neither prove\nnor disprove that the machine halts (assuming ZFC is sound).\n\nUsing the Shoenfield's absoluteness theorem, one can show that one can replace \"ZFC\" with \"ZF\"\nwithout the value of this constant changing. -/\n@[optimization_constant \"14a\"]\nnoncomputable def C14 : ℕ :=\n  sInf {n | ∃ _ : NeZero n, ∃ M : Machine (Fin 2) (Fin n),\n    𝗭𝗙𝗖 ⊬ halts M ∧ 𝗭𝗙𝗖 ⊬ ∼halts M}\n\n/-- Proven by Lin and Radó in [LS1965] by computing $BB(3) = 21$. -/\n@[category research solved, AMS 3 68]\ntheorem c14_ge_4 : 4 ≤ C14 := by sorry\n\n/-- An intermediate lower bound, proved by Brady in [B1983] by explicitly computing\n$BB(4) = 107$. -/\n@[category research solved, AMS 3 68]\ntheorem c14_ge_5 : 5 ≤ C14 := by sorry\n\n/-- The current best known lower bound, proved by Blanchard et al. in [BB2025] by explcitily computing\n$BB(5) = 47176870$.\n\nNote $BB(5) = 47176870$ (as well as the values below) has been formally proven in Rocq as part of\n[BB2025], a formal proof of the declaration below would likely be much more difficult on a technical\nlevel than the proof in Rocq, which is based on dependent type theory. -/\n@[category research solved, AMS 3 68]\ntheorem c14_lower_bound : 6 ≤ C14 := by sorry\n\n/-- Can the current best lower bound be improved? -/\n@[category research open, question, AMS 3 68]\ntheorem c14_lower_bound_improved : 6 < C14 := by sorry\n\n/-- The first known upper bound, proved by Yedidia and Aaronson in [YS2016]. Their construction\nreduces the independence problem to a graph-theoretic setting. -/\n@[category research solved, AMS 3 68]\ntheorem c14_le_7910 : C14 ≤ 7910 := by sorry\n\n/-- An intermediate upper bound, proved by O'Rear in [O2016]. It uses a Turing machine that\nenumerates proofs in ZF. -/\n@[category research solved, AMS 3 68]\ntheorem c14_le_748 : C14 ≤ 748 := by sorry\n\n/-- An intermediate upper bound, proved by Wade in [W2025]. It refines the proof-enumerating\nmachine construction from [O2016]. -/\n@[category research solved, AMS 3 68]\ntheorem c14_le_432 : C14 ≤ 432 := by sorry\n\n/-- The current best known upper bound, from [G2026]. Glazer proved in Rocq, without axioms, that\na certain 230-state Turing machine halts if and only if ZFC is inconsistent. -/\n@[category research solved, AMS 3 68]\ntheorem c14_upper_bound : C14 ≤ 230 := by sorry\n\n/-- Can the current best upper bound be improved? -/\n@[category research open, question, AMS 3 68]\ntheorem c14_upper_bound_improved : C14 < 230 := by sorry\n\n/-- Conjecture 11 in [A2020] by Scott Aaronson -/\n@[category research open, AMS 3 68]\ntheorem c14_le_20 : C14 ≤ 20 := by sorry\n\nend Constant14\n",
  "statement_hash": "23c2c4e433d329e9626a800228c8355d970ecbe88c8c0d1320b49d0f23a18fe3",
  "toolchain": "leanprover/lean4:v4.33.1",
  "upstream_commit": "f9b3868d5ca994a7bf95039bcec05338c0c21e2f",
  "workspace": "formal-conjectures",
  "field": "math",
  "formalization": "formal",
  "verification": "lean-kernel",
  "license": "Apache-2.0",
  "attribution": "google-deepmind/formal-conjectures contributors",
  "metric_name": null,
  "metric_direction": null,
  "critiques": [],
  "public_partial_proofs": [],
  "research_threads": [],
  "instructions": "Read research_threads first: other people's public repositories and what they already tried. Write Solution.lean starting from challenge_source. Keep every definition identical, prove decl_name with exactly the same type, no sorry in its dependency closure, no new axioms, no native_decide, no metaprogramming. Submit with POST /api/v1/submissions."
}