Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions LeanPool/FormalLinguistics.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/-
Copyright (c) 2026 bocchitherock888-ux. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: bocchitherock888-ux
-/

module

public import LeanPool.FormalLinguistics.BarHillel
public import LeanPool.FormalLinguistics.LambekPentus
public import LeanPool.FormalLinguistics.MGOriginal

/-!
# Categorial, Lambek, and minimalist grammar equivalences

Source: url:https://github.com/bocchitherock888-ux/lean-grammar-equivalences/releases/tag/v2.0
Authors: bocchitherock888-ux
Status: verified
Main declarations: `FormalLinguistics.bar_hillel_gaifman_shamir`,
`FormalLinguistics.chomsky_pentus`, `FormalLinguistics.michaelis_harkema_equivalence_SK`
Tags: formal-languages, categorial-grammars, lambek-calculus, minimalist-grammars
MSC: 68Q42, 03B47
-/

/-
Upstream source license for this project

MIT License

Copyright (c) 2026 bocchitherock888-ux

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-/
274 changes: 274 additions & 0 deletions LeanPool/FormalLinguistics/ABG.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,274 @@
/-
Copyright (c) 2026 bocchitherock888-ux. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: bocchitherock888-ux
-/

module

public import Init

/-!
AB Basic Categorial Grammars (Ajdukiewicz 1935; Bar-Hillel 1953; Bar-Hillel, Gaifman & Shamir 1960)
Standard directional slash-cancellation definition + equivalence with AB derivation trees (`ABTree`).
-/

@[expose] public section

namespace FormalLinguistics

/-- Categorial types over atomic categories `P`:
`rslash A B` denotes `A / B` (combines with `B` on the right to yield `A`),
`lslash B A` denotes `B \ A` (combines with `B` on the left to yield `A`). -/
inductive Cat (P : Type) where
| atom : P → Cat P
| rslash : Cat P → Cat P → Cat P
| lslash : Cat P → Cat P → Cat P
deriving DecidableEq, Repr

/-- A finite AB Categorial Grammar over atomic categories `P` and alphabet `T`. -/
structure ABG (P T : Type) where
/-- Primitive category of complete derivations. -/
distinguished : P
/-- Allowed assignments of categories to individual terminals. -/
lexicon : List (T × Cat P)

namespace ABG

variable {P T : Type}

/-- Single-step AB cancellation:
- Forward application (`>`): `(A / B) B ⊢ A`
- Backward application (`<`): `B (B \ A) ⊢ A` -/
inductive ABStep : List (Cat P) → List (Cat P) → Prop where
| fwd (u v : List (Cat P)) (A B : Cat P) :
ABStep (u ++ [Cat.rslash A B, B] ++ v) (u ++ [A] ++ v)
| bwd (u v : List (Cat P)) (A B : Cat P) :
ABStep (u ++ [B, Cat.lslash B A] ++ v) (u ++ [A] ++ v)

/-- Multi-step AB cancellation: reflexive-transitive closure of `ABStep`. -/
inductive ABDerives : List (Cat P) → List (Cat P) → Prop where
| refl (γ : List (Cat P)) : ABDerives γ γ
| tail {γ1 γ2 γ3 : List (Cat P)} : ABDerives γ1 γ2 → ABStep γ2 γ3 → ABDerives γ1 γ3

/-- Lexical assignment: word `w = [a₁, ..., aₙ]` is assigned categories `γ = [c₁, ..., cₙ]`
with `(aᵢ, cᵢ) ∈ G.lexicon`. -/
inductive LexAssign (G : ABG P T) : List T → List (Cat P) → Prop where
| nil : LexAssign G [] []
| cons {a : T} {c : Cat P} {w : List T} {γ : List (Cat P)} :
(a, c) ∈ G.lexicon → LexAssign G w γ → LexAssign G (a :: w) (c :: γ)

/-- The string language generated by an AB Categorial Grammar. -/
def Lang (G : ABG P T) (w : List T) : Prop :=
∃ γ, LexAssign G w γ ∧ ABDerives γ [Cat.atom G.distinguished]

theorem ABStep.length_eq {γ1 γ2 : List (Cat P)} (h : ABStep γ1 γ2) :
γ1.length = γ2.length + 1 := by
cases h <;> simp <;> omega

theorem ABDerives.length_ge {γ1 γ2 : List (Cat P)} (h : ABDerives γ1 γ2) :
γ2.length ≤ γ1.length := by
induction h with
| refl => exact Nat.le_refl _
| tail _ hstep ih =>
have hl := hstep.length_eq
omega

theorem LexAssign.length_eq {G : ABG P T} {w : List T} {γ : List (Cat P)}
(h : LexAssign G w γ) : w.length = γ.length := by
induction h with
| nil => rfl
| cons _ _ ih => simp [ih]

/-- Every AB categorial language is ε-free (contains no empty string). -/
theorem lang_nonempty {G : ABG P T} {w : List T} (h : G.Lang w) : w ≠ [] := by
obtain ⟨γ, hlex, hder⟩ := h
have hlen1 := hlex.length_eq
have hlen2 := hder.length_ge
intro hw
subst hw
simp at hlen1 hlen2
omega

theorem ABDerives.single {γ1 γ2 : List (Cat P)} (h : ABStep γ1 γ2) : ABDerives γ1 γ2 :=
ABDerives.tail (ABDerives.refl γ1) h

theorem ABDerives.trans {γ1 γ2 γ3 : List (Cat P)}
(h1 : ABDerives γ1 γ2) (h2 : ABDerives γ2 γ3) : ABDerives γ1 γ3 := by
induction h2 with
| refl => exact h1
| tail _ hstep ih => exact ABDerives.tail ih hstep

theorem ABStep.append_left (p : List (Cat P)) {γ1 γ2 : List (Cat P)}
(h : ABStep γ1 γ2) : ABStep (p ++ γ1) (p ++ γ2) := by
cases h with
| fwd l r A B =>
have h1 : p ++ (l ++ [Cat.rslash A B, B] ++ r) = (p ++ l) ++ [Cat.rslash A B, B] ++ r := by
simp [List.append_assoc]
have h2 : p ++ (l ++ [A] ++ r) = (p ++ l) ++ [A] ++ r := by
simp [List.append_assoc]
rw [h1, h2]
exact ABStep.fwd (p ++ l) r A B
| bwd l r A B =>
have h1 : p ++ (l ++ [B, Cat.lslash B A] ++ r) = (p ++ l) ++ [B, Cat.lslash B A] ++ r := by
simp [List.append_assoc]
have h2 : p ++ (l ++ [A] ++ r) = (p ++ l) ++ [A] ++ r := by
simp [List.append_assoc]
rw [h1, h2]
exact ABStep.bwd (p ++ l) r A B

theorem ABStep.append_right (s : List (Cat P)) {γ1 γ2 : List (Cat P)}
(h : ABStep γ1 γ2) : ABStep (γ1 ++ s) (γ2 ++ s) := by
cases h with
| fwd l r A B =>
have h1 : (l ++ [Cat.rslash A B, B] ++ r) ++ s = l ++ [Cat.rslash A B, B] ++ (r ++ s) := by
simp [List.append_assoc]
have h2 : (l ++ [A] ++ r) ++ s = l ++ [A] ++ (r ++ s) := by
simp [List.append_assoc]
rw [h1, h2]
exact ABStep.fwd l (r ++ s) A B
| bwd l r A B =>
have h1 : (l ++ [B, Cat.lslash B A] ++ r) ++ s = l ++ [B, Cat.lslash B A] ++ (r ++ s) := by
simp [List.append_assoc]
have h2 : (l ++ [A] ++ r) ++ s = l ++ [A] ++ (r ++ s) := by
simp [List.append_assoc]
rw [h1, h2]
exact ABStep.bwd l (r ++ s) A B

theorem ABDerives.append_left (p : List (Cat P)) {γ1 γ2 : List (Cat P)}
(h : ABDerives γ1 γ2) : ABDerives (p ++ γ1) (p ++ γ2) := by
induction h with
| refl => exact ABDerives.refl _
| tail _ hstep ih => exact ABDerives.tail ih (ABStep.append_left p hstep)

theorem ABDerives.append_right (s : List (Cat P)) {γ1 γ2 : List (Cat P)}
(h : ABDerives γ1 γ2) : ABDerives (γ1 ++ s) (γ2 ++ s) := by
induction h with
| refl => exact ABDerives.refl _
| tail _ hstep ih => exact ABDerives.tail ih (ABStep.append_right s hstep)

theorem ABDerives.append {γ1 δ1 γ2 δ2 : List (Cat P)}
(h1 : ABDerives γ1 δ1) (h2 : ABDerives γ2 δ2) : ABDerives (γ1 ++ γ2) (δ1 ++ δ2) :=
ABDerives.trans (ABDerives.append_right γ2 h1) (ABDerives.append_left δ1 h2)

theorem LexAssign.append {G : ABG P T} {w1 w2 : List T} {γ1 γ2 : List (Cat P)}
(h1 : LexAssign G w1 γ1) (h2 : LexAssign G w2 γ2) : LexAssign G (w1 ++ w2) (γ1 ++ γ2) := by
induction h1 with
| nil => simpa using h2
| cons ha _ ih => exact LexAssign.cons ha ih

/-- Binary derivation trees for AB Categorial Grammars. -/
inductive ABTree (G : ABG P T) : Cat P → List T → Prop where
| lex {a : T} {c : Cat P} : (a, c) ∈ G.lexicon → ABTree G c [a]
| fwd {A B : Cat P} {w1 w2 : List T} :
ABTree G (Cat.rslash A B) w1 → ABTree G B w2 → ABTree G A (w1 ++ w2)
| bwd {A B : Cat P} {w1 w2 : List T} :
ABTree G B w1 → ABTree G (Cat.lslash B A) w2 → ABTree G A (w1 ++ w2)

/-- List of AB derivation trees matching a category sequence. -/
inductive ABTreeList (G : ABG P T) : List (Cat P) → List T → Prop where
| nil : ABTreeList G [] []
| cons {c : Cat P} {γ : List (Cat P)} {w1 w2 : List T} :
ABTree G c w1 → ABTreeList G γ w2 → ABTreeList G (c :: γ) (w1 ++ w2)

theorem ABTreeList.append {G : ABG P T} {γ1 γ2 : List (Cat P)} {w1 w2 : List T}
(h1 : ABTreeList G γ1 w1) (h2 : ABTreeList G γ2 w2) :
ABTreeList G (γ1 ++ γ2) (w1 ++ w2) := by
induction h1 with
| nil => simpa using h2
| cons ht _ ih =>
simpa [List.append_assoc] using ABTreeList.cons ht ih

theorem ABTreeList.split_append {G : ABG P T} {γ1 γ2 : List (Cat P)} {w : List T}
(h : ABTreeList G (γ1 ++ γ2) w) :
∃ w1 w2, w = w1 ++ w2 ∧ ABTreeList G γ1 w1 ∧ ABTreeList G γ2 w2 := by
induction γ1 generalizing w with
| nil => exact ⟨[], w, rfl, ABTreeList.nil, h⟩
| cons c t ih =>
cases h with
| cons ht hrest =>
obtain ⟨w1, w2, hw, h1, h2⟩ := ih hrest
refine ⟨_ ++ w1, w2, ?_, ABTreeList.cons ht h1, h2⟩
simp [hw, List.append_assoc]

theorem ABTreeList.of_lexAssign {G : ABG P T} {w : List T} {γ : List (Cat P)}
(h : LexAssign G w γ) : ABTreeList G γ w := by
induction h with
| nil => exact ABTreeList.nil
| cons ha _ ih =>
simpa using ABTreeList.cons (ABTree.lex ha) ih

theorem ABTreeList.step {G : ABG P T} {γ1 γ2 : List (Cat P)} {w : List T}
(h : ABTreeList G γ1 w) (hs : ABStep γ1 γ2) : ABTreeList G γ2 w := by
cases hs with
| fwd l r A B =>
rw [List.append_assoc] at h
obtain ⟨w1, w23, hw1, hl, h23⟩ := ABTreeList.split_append h
obtain ⟨w2, w3, hw2, hmid, hr⟩ := ABTreeList.split_append h23
cases hmid with
| @cons _ _ wa _ hAB hrest =>
cases hrest with
| @cons _ _ wb _ hB hnil =>
cases hnil
have hA : ABTree G A (wa ++ wb) := ABTree.fwd hAB hB
have hAs : ABTreeList G [A] (wa ++ wb) := by
simpa using ABTreeList.cons hA ABTreeList.nil
have hres := ABTreeList.append (ABTreeList.append hl hAs) hr
simpa [hw1, hw2, List.append_assoc] using hres
| bwd l r A B =>
rw [List.append_assoc] at h
obtain ⟨w1, w23, hw1, hl, h23⟩ := ABTreeList.split_append h
obtain ⟨w2, w3, hw2, hmid, hr⟩ := ABTreeList.split_append h23
cases hmid with
| @cons _ _ wa _ hB hrest =>
cases hrest with
| @cons _ _ wb _ hBA hnil =>
cases hnil
have hA : ABTree G A (wa ++ wb) := ABTree.bwd hB hBA
have hAs : ABTreeList G [A] (wa ++ wb) := by
simpa using ABTreeList.cons hA ABTreeList.nil
have hres := ABTreeList.append (ABTreeList.append hl hAs) hr
simpa [hw1, hw2, List.append_assoc] using hres

theorem ABTreeList.of_derives {G : ABG P T} {γ1 γ2 : List (Cat P)} {w : List T}
(h : ABTreeList G γ1 w) (hd : ABDerives γ1 γ2) : ABTreeList G γ2 w := by
induction hd with
| refl => exact h
| tail _ hstep ih => exact ABTreeList.step ih hstep

theorem abtree_iff_derives {G : ABG P T} {C : Cat P} {w : List T} :
ABTree G C w ↔ ∃ γ, LexAssign G w γ ∧ ABDerives γ [C] := by
constructor
· intro ht
induction ht with
| lex ha =>
exact ⟨[_], LexAssign.cons ha LexAssign.nil, ABDerives.refl _⟩
| @fwd A B w1 w2 _ _ ih1 ih2 =>
obtain ⟨γ1, hl1, hd1⟩ := ih1
obtain ⟨γ2, hl2, hd2⟩ := ih2
refine ⟨γ1 ++ γ2, LexAssign.append hl1 hl2, ?_⟩
have h1 := ABDerives.append hd1 hd2
have h2 : ABStep [Cat.rslash A B, B] [A] := by
simpa using ABStep.fwd [] [] A B
exact ABDerives.trans h1 (ABDerives.single h2)
| @bwd A B w1 w2 _ _ ih1 ih2 =>
obtain ⟨γ1, hl1, hd1⟩ := ih1
obtain ⟨γ2, hl2, hd2⟩ := ih2
refine ⟨γ1 ++ γ2, LexAssign.append hl1 hl2, ?_⟩
have h1 := ABDerives.append hd1 hd2
have h2 : ABStep [B, Cat.lslash B A] [A] := by
simpa using ABStep.bwd [] [] A B
exact ABDerives.trans h1 (ABDerives.single h2)
· rintro ⟨γ, hlex, hder⟩
have hlist := ABTreeList.of_derives (ABTreeList.of_lexAssign hlex) hder
cases hlist with
| cons ht hnil =>
cases hnil
simpa using ht

theorem lang_iff_abtree {G : ABG P T} {w : List T} :
G.Lang w ↔ ABTree G (Cat.atom G.distinguished) w :=
abtree_iff_derives.symm

end ABG
end FormalLinguistics
Loading
Loading