Draft — mutable and not usable as a dependency; its citation marks the draft state.

Lax314295.MSOLogic

Monadic second-order logic on strings

concepts/Lax314295/MSOLogic.lean · lax-314295

definition

Loading review…

Sign in with ORCID

Community review

Flags

Each flag is tied to a public ORCID identity and explains why this concept may be incorrect.

No flags have been submitted.

    Community review

    Flag this concept

    State precisely what appears incorrect. This explanation will be public under your ORCID name.

    No source line selected.

    Concept map

    Proven claimDefinitionThis conceptRelated conceptA → B: B builds on A

    In the paper

    Definition

    A string wAw \in A^* is a structure whose universe is the set of positions of ww, with the order xyx \le y on positions and, for every letter aAa \in A, the unary predicate a(x)a(x) "position xx carries the letter aa". Monadic second-order logic (mso) has first-order variables ranging over positions and second-order variables ranging over sets of positions, the atomic formulas xyx \le y, a(x)a(x) and xXx \in X, Boolean connectives, and existential quantification over both kinds of variables (Section C.4.1 of Transducers). A sentence — a formula without free variables — defines the language of the strings that satisfy it; the first-order fragment is the set of formulas that use neither set variables nor membership. A formula with free variables is evaluated on a string together with a valuation, which the book represents by annotating the string: the string w{x1}Xw \otimes \{x_1\} \otimes \cdots \otimes X_\ell over the alphabet A×2k+A \times 2^{k + \ell} carries at every position the bits saying which of the variables point to it.

    Lean source view on GitHub

    1import Mathlib.Computability.DFA
    2import Mathlib.Data.Fintype.Basic
    3
    4/-!
    5---
    6title: Monadic second-order logic on strings
    7type: definition
    8---
    9A string wAw \in A^* is a structure whose universe is the set of positions of
    10ww, with the order xyx \le y on positions and, for every letter aAa \in A, the
    11unary predicate a(x)a(x) "position xx carries the letter aa". *Monadic
    12second-order logic* (mso) has first-order variables ranging over positions and
    13second-order variables ranging over sets of positions, the atomic formulas
    14xyx \le y, a(x)a(x) and xXx \in X, Boolean connectives, and existential
    15quantification over both kinds of variables (Section C.4.1 of *Transducers*).
    16A sentence — a formula without free variables — *defines* the language of the
    17strings that satisfy it; the *first-order fragment* is the set of formulas that
    18use neither set variables nor membership. A formula with free variables is
    19evaluated on a string together with a valuation, which the book represents by
    20*annotating* the string: the string w{x1}Xw \otimes \{x_1\} \otimes \cdots \otimes X_\ell
    21over the alphabet A×2k+A \times 2^{k + \ell} carries at every position the bits
    22saying which of the variables point to it.
    23
    24# Formalization notes
    25
    26Variables of both kinds are named by natural numbers, and a valuation is a pair
    27of functions `ℕ → ℕ` (positions of the first-order variables) and `ℕ → Set ℕ`
    28(sets of positions of the second-order variables); quantifiers range over
    29positions `p < w.length` and subsets of them. Satisfaction, the first-order
    30fragment, the quantifier rank and the free variables of a formula are defined
    31by recursion on the syntax; `MSODefinable L` asks for a formula satisfied by
    32exactly the strings of `L` under every valuation, which for a sentence is the
    33usual notion and for a formula with free variables is the same as for its
    34universal closure. `annotate k l w fo so` is the annotated string of a valuation
    35of the variables `0, …, k-1` and `0, …, l-1`, and `extFO`/`extSO` extend such a
    36valuation to all variables. Alphabets are arbitrary types; finiteness is a
    37hypothesis of the theorems.
    38-/
    39
    40namespace Lax314295.MSOLogic
    41
    42/-- Formulas of monadic second-order logic over strings with letters in `A`;
    43first-order and second-order variables are named by natural numbers. -/
    44inductive MSO (A : Type) : Type
    45 /-- The order test `x_i ≤ x_j`. -/
    46 | le : ℕ → ℕ → MSO A
    47 /-- The label test `a (x_i)`. -/
    48 | lab : A → ℕ → MSO A
    49 /-- The membership test `x_i ∈ X_j`. -/
    50 | mem : ℕ → ℕ → MSO A
    51 /-- Negation. -/
    52 | not : MSO A → MSO A
    53 /-- Conjunction. -/
    54 | and : MSO A → MSO A → MSO A
    55 /-- Disjunction. -/
    56 | or : MSO A → MSO A → MSO A
    57 /-- First-order existential quantification `∃ x_i`. -/
    58 | exFO : ℕ → MSO A → MSO A
    59 /-- Second-order existential quantification `∃ X_i`. -/
    60 | exSO : ℕ → MSO A → MSO A
    61
    62namespace MSO
    63
    64variable {A : Type}
    65
    66/-- Satisfaction of a formula in a string under a valuation of the first-order
    67variables by positions and of the second-order variables by sets of positions. -/
    68def Sat (w : List A) : (ℕ → ℕ) → (ℕ → Set ℕ) → MSO A → Prop
    69 | fo, _, le i j => fo i ≤ fo j
    70 | fo, _, lab a i => w[fo i]? = some a
    71 | fo, so, mem i j => fo i ∈ so j
    72 | fo, so, not φ => ¬ Sat w fo so φ
    73 | fo, so, and φ ψ => Sat w fo so φ ∧ Sat w fo so ψ
    74 | fo, so, or φ ψ => Sat w fo so φ ∨ Sat w fo so ψ
    75 | fo, so, exFO i φ => ∃ p < w.length, Sat w (Function.update fo i p) so φ
    76 | fo, so, exSO i φ => ∃ S ⊆ {p | p < w.length}, Sat w fo (Function.update so i S) φ
    77
    78/-- A formula is first-order if it uses neither set quantification nor membership.
    79-/
    80def IsFO : MSO A → Prop
    81 | le _ _ => True
    82 | lab _ _ => True
    83 | mem _ _ => False
    84 | not φ => IsFO φ
    85 | and φ ψ => IsFO φ ∧ IsFO ψ
    86 | or φ ψ => IsFO φ ∧ IsFO ψ
    87 | exFO _ φ => IsFO φ
    88 | exSO _ _ => False
    89
    90/-- The quantifier rank: the maximal number of nested quantifiers. -/
    91def qrank : MSO A → ℕ
    92 | le _ _ => 0
    93 | lab _ _ => 0
    94 | mem _ _ => 0
    95 | not φ => qrank φ
    96 | and φ ψ => max (qrank φ) (qrank ψ)
    97 | or φ ψ => max (qrank φ) (qrank ψ)
    98 | exFO _ φ => qrank φ + 1
    99 | exSO _ φ => qrank φ + 1
    100
    101/-- The free first-order variables of a formula. -/
    102def freeFO : MSO A → Set ℕ
    103 | le i j => {i, j}
    104 | lab _ i => {i}
    105 | mem i _ => {i}
    106 | not φ => freeFO φ
    107 | and φ ψ => freeFO φ ∪ freeFO ψ
    108 | or φ ψ => freeFO φ ∪ freeFO ψ
    109 | exFO i φ => freeFO φ \ {i}
    110 | exSO _ φ => freeFO φ
    111
    112/-- The free second-order variables of a formula. -/
    113def freeSO : MSO A → Set ℕ
    114 | le _ _ => ∅
    115 | lab _ _ => ∅
    116 | mem _ j => {j}
    117 | not φ => freeSO φ
    118 | and φ ψ => freeSO φ ∪ freeSO ψ
    119 | or φ ψ => freeSO φ ∪ freeSO ψ
    120 | exFO _ φ => freeSO φ
    121 | exSO i φ => freeSO φ \ {i}
    122
    123end MSO
    124
    125/-- A language is definable in monadic second-order logic. -/
    126def MSODefinable {A : Type} (L : Language A) : Prop :=
    127 ∃ φ : MSO A, ∀ (w : List A) (fo : ℕ → ℕ) (so : ℕ → Set ℕ), MSO.Sat w fo so φ ↔ w ∈ L
    128
    129/-- A language is definable in first-order logic. -/
    130def FODefinable {A : Type} (L : Language A) : Prop :=
    131 ∃ φ : MSO A, φ.IsFO
    132 ∀ (w : List A) (fo : ℕ → ℕ) (so : ℕ → Set ℕ), MSO.Sat w fo so φ ↔ w ∈ L
    133
    134open scoped Classical in
    135/-- The annotation `w ⊗ {x₁} ⊗ ⋯ ⊗ {x_k} ⊗ X₁ ⊗ ⋯ ⊗ X_l` of a string by the values
    136of `k` first-order and `l` second-order variables. -/
    137noncomputable def annotate {A : Type} (k l : ℕ) (w : List A)
    138 (fo : Fin k → ℕ) (so : Fin l → Set ℕ) : List (A × (Fin k → Bool) × (Fin l → Bool)) :=
    139 w.zipIdx.map (fun z => (z.1, fun i => decide (fo i = z.2), fun j => decide (z.2 ∈ so j)))
    140
    141/-- Extend a valuation of the first-order variables `0, …, k-1` to all variables. -/
    142def extFO (k : ℕ) (fo : Fin k → ℕ) : ℕ → ℕ :=
    143 fun i => if h : i < k then fo ⟨i, h⟩ else 0
    144
    145/-- Extend a valuation of the set variables `0, …, l-1` to all variables. -/
    146def extSO (l : ℕ) (so : Fin l → Set ℕ) : ℕ → Set ℕ :=
    147 fun j => if h : j < l then so ⟨j, h⟩ else
    148
    149end Lax314295.MSOLogic
    150

    Formalization notes

    Variables of both kinds are named by natural numbers, and a valuation is a pair of functions NNℕ → ℕ (positions of the first-order variables) and NSetNℕ → Set ℕ (sets of positions of the second-order variables); quantifiers range over positions p<w.lengthp < w.length and subsets of them. Satisfaction, the first-order fragment, the quantifier rank and the free variables of a formula are defined by recursion on the syntax; MSODefinableLMSODefinable L asks for a formula satisfied by exactly the strings of LL under every valuation, which for a sentence is the usual notion and for a formula with free variables is the same as for its universal closure. annotateklwfosoannotate k l w fo so is the annotated string of a valuation of the variables 0,,k10, …, k-1 and 0,,l10, …, l-1, and extFOextFO/extSOextSO extend such a valuation to all variables. Alphabets are arbitrary types; finiteness is a hypothesis of the theorems.

    Community review

    Discussion

    Ask a question or add context. Endorsements and structured flags are kept in the review panel above; your ORCID profile must share a public name.

    0 comments

    Loading discussion…