Skip to content

Commit d7296d1

Browse files
author
Arthur Freitas Ramos
committed
docs: update README and add ARCHITECTURE.md
1 parent ab29259 commit d7296d1

2 files changed

Lines changed: 303 additions & 4 deletions

File tree

README.md

Lines changed: 115 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
# Metatheory
22

33
[![Lean 4](https://img.shields.io/badge/Lean-4.24.0-blue.svg)](https://lean-lang.org/)
4-
[![Mathlib](https://img.shields.io/badge/Mathlib-v4.24.0-green.svg)](https://github.com/leanprover-community/mathlib4)
54
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
65

7-
A comprehensive **programming language metatheory library for Lean 4**, providing formally verified proofs of fundamental results in rewriting theory and type systems.
6+
A comprehensive **programming language metatheory library for Lean 4**, now spanning **130+ Lean files and 58K+ lines** of mechanized proofs and metatheoretic case studies.
87

98
## Overview
109

@@ -20,6 +19,15 @@ Metatheory formalizes core results from programming language theory:
2019
- **System F** (Polymorphic Lambda Calculus): Subject reduction with type substitution
2120
- **Term/String Rewriting**: Confluence via Newman's lemma, critical pair analysis, and first-order TRS completion (KBO/LPO)
2221
- **TRS Proof Comparison**: Diamond vs Newman confluence for a tiny deterministic TRS
22+
- **Extended Formalizations**: 78 standalone modules covering type systems, semantics, compilation, effects, logic, macros, and automation
23+
24+
### Project Scale
25+
26+
- **Tracked Lean files**: 155 (`git ls-files '*.lean'`)
27+
- **Tracked Lean LOC**: 58,126
28+
- **Lean files under `Metatheory/`**: 153
29+
- **Standalone modules under `Metatheory/*.lean`**: 78
30+
- **Top-level dependency model**: no Mathlib requirement in `lakefile.toml`; many standalone modules are self-contained
2331

2432

2533
### Why Metatheory?
@@ -29,7 +37,7 @@ Metatheory formalizes core results from programming language theory:
2937
| **Multiple proof techniques** | Learn different approaches to confluence (Diamond, Newman, Hindley-Rosen) |
3038
| **Layered architecture** | Generic framework instantiated by specific systems |
3139
| **De Bruijn indices** | Capture-avoiding substitution without alpha-equivalence |
32-
| **Mathlib integration** | Uses Mathlib for standard lemmas; core theorems axiom-free |
40+
| **Dependency-light design** | Top-level build has no Mathlib requirement; many modules are self-contained |
3341
| **Axiom/placeholder free** | No `axiom`/`constant` declarations and no `sorry`/`admit` |
3442
| **Extensively documented** | Docstrings, references, and proof explanations |
3543

@@ -39,7 +47,6 @@ Metatheory formalizes core results from programming language theory:
3947

4048
- [Lean 4](https://lean-lang.org/lean4/doc/setup.html) (version 4.24.0 or compatible)
4149
- [Lake](https://github.com/leanprover/lake) (included with Lean)
42-
- [Mathlib](https://github.com/leanprover-community/mathlib4) (automatically fetched by Lake)
4350

4451
### Building
4552

@@ -191,6 +198,109 @@ example {M : Term} {τ : Ty} (h : ⊢ M : τ) : M.IsValue ∨ ∃ N, M.Step N :=
191198
progress h
192199
```
193200

201+
## Extended Formalizations (Standalone Modules)
202+
203+
In addition to the layered core directories (`Rewriting/`, `Lambda/`, `CL/`, `TRS/`, `StringRewriting/`, `STLC/`, `STLCext/`, `STLCextBool/`, `SystemF/`), the project includes **78 standalone modules** at `Metatheory/*.lean`.
204+
205+
### Type systems and typing disciplines
206+
207+
- `AbstractionSafety.lean` — Safety-oriented typing relation with abstraction-preservation lemmas.
208+
- `AffineTypes.lean` — Affine ownership typing inspired by Rust-style single-use resources.
209+
- `Bidirectional.lean` — Bidirectional typing (synthesis/checking) for concise typing derivations.
210+
- `ContractTypes.lean` — Contract-annotated typing with interface-level guarantees.
211+
- `DependentPattern.lean` — Dependently typed pattern matching and indexed elimination structure.
212+
- `EffectSystems.lean` — Type-and-effect judgments with explicit effect labels.
213+
- `Gradual.lean` — Gradual typing with dynamic type and consistency relations.
214+
- `GradedTypeTheory.lean` — Graded modality/type usage tracking (0/1/ω-style discipline).
215+
- `InformationFlow.lean` — Security typing over a lattice of confidentiality levels.
216+
- `Intersection.lean` — Intersection type operators and associated metatheoretic lemmas.
217+
- `LinearTypes.lean` — Linear usage-sensitive typing and structural control.
218+
- `PolymorphismVariants.lean` — Comparative formalization of polymorphism design variants.
219+
- `QuantitativeTypes.lean` — Quantitative type usage accounting for resource-aware typing.
220+
- `RecursiveTypes.lean` — Recursive type constructors with unfold/fold metatheory.
221+
- `Refinement.lean` — Refinement-annotated base types with predicate-indexed judgments.
222+
- `RefinementTypes.lean` — Predicate subtyping and refinement typing rules.
223+
- `RowPoly.lean` — Row polymorphism for extensible typing contexts/signatures.
224+
- `SecrecyTypes.lean` — Secrecy/security labels integrated into typing judgments.
225+
- `SessionTypes.lean` — Binary session type protocols for typed communication.
226+
- `SizedTypes.lean` — Size indices for termination/productivity-aware typing.
227+
- `TemporalTypes.lean` — Temporal type structure over trace-indexed behavior.
228+
- `TypeClasses.lean` — Type-class declarations and resolution-oriented typing scaffolding.
229+
- `TypeInference.lean` — Constraint-based type inference for monomorphic fragments.
230+
- `TypeInhabitance.lean` — Constructive inhabitation results (type-to-term witness style).
231+
- `TypePreservation.lean` — Standalone substitution and preservation theorems.
232+
- `UniversePolymorphism.lean` — Universe-level expressions and polymorphic universe handling.
233+
234+
### Semantics, normalization, and equivalence
235+
236+
- `AbstractInterpretation.lean` — Abstract interpretation skeletons with soundness-oriented structure.
237+
- `AbstractMachines.lean` — CEK/Krivine/SECD-style machine encodings and transitions.
238+
- `CallByPushValue.lean` — Call-by-push-value syntax, translation, and metatheory lemmas.
239+
- `CategorySemantics.lean` — Categorical semantics building blocks for typed languages.
240+
- `Coinduction.lean` — Coinductive streams/processes and bisimulation-style reasoning.
241+
- `DomainTheory.lean` — Domain/path-style order-theoretic lemmas for denotational settings.
242+
- `Erasure.lean` — Erasure translation preserving key typing/evaluation structure.
243+
- `GameSemantics.lean` — Game-semantic traces/plays with compositional properties.
244+
- `LogicalRelations.lean` — Logical-relations framework for normalization/equivalence proofs.
245+
- `ModalTypeTheory.lean` — Modal type-theoretic syntax and proof obligations.
246+
- `NormByEval.lean` — Normalization by evaluation (NbE) core constructions.
247+
- `Normalization.lean` — Generic normalization-oriented lemmas and structures.
248+
- `ObservationalEquality.lean` — Observational/contextual equality formulations.
249+
- `ParallelReduction.lean` — Parallel reduction relation and confluence-support infrastructure.
250+
- `Parametricity.lean` — Relational parametricity setup for polymorphic calculi.
251+
- `ProgramEquivalence.lean` — Program equivalence relations and congruence toolkit.
252+
- `Realizability.lean` — Realizability semantics over combinatory-style structures.
253+
- `Termination.lean` — Well-founded termination analysis and accessibility lemmas.
254+
255+
### Compilation and transformation pipeline
256+
257+
- `ANFRegAlloc.lean` — ANF conversion plus register-allocation metatheory.
258+
- `CPS.lean` — CPS-focused typed syntax and proof support.
259+
- `CPSTransformation.lean` — Source-to-CPS transformation and simulation lemmas.
260+
- `CompilerOptimizations.lean` — Verified optimization rewrites for a small compiler core.
261+
- `ElaborationAlgorithm.lean` — Surface-to-core elaboration algorithm components.
262+
- `StackMachineCompilation.lean` — Compilation to stack machine code with correctness statements.
263+
- `StackMachines.lean` — Stack machine semantics and execution lemmas.
264+
- `SyntaxTransformers.lean` — Generic syntax transformer combinators and properties.
265+
266+
### Effects, concurrency, and resource management
267+
268+
- `AlgebraicEffects.lean` — Algebraic effects with handlers and effect-row-style structure.
269+
- `EffectHandlers.lean` — Typed handler interfaces and operational effect handling.
270+
- `ConcurrencyTypes.lean` — Typing-oriented concurrency process/rewrite structure.
271+
- `MemoryManagement.lean` — Memory-aware terms/types and management invariants.
272+
- `SessionProc.lean` — Session-typed process calculus fragments and safety scaffolding.
273+
274+
### Logic, proof theory, and structural systems
275+
276+
- `Focusing.lean` — Focused proof search structure with polarized connectives.
277+
- `LinearLogic.lean` — Linear logic formulae and proof-theoretic infrastructure.
278+
- `ModalLogic.lean` — Modal proof system lemmas and formula structure.
279+
- `OTT.lean` — OTT-style syntax/typing support definitions.
280+
- `ProofIrrelevance.lean` — Proof irrelevance principles and consequences.
281+
- `ProofNets.lean` — Proof-net representation for linear logic fragments.
282+
- `ResourceLogic.lean` — Resource-sensitive logic connectives and judgments.
283+
- `SeparationLogic.lean` — Separation logic structures and composition lemmas.
284+
- `Sequent.lean` — Sequent calculus style rewriting/judgment relations.
285+
- `Substructural.lean` — Substructural logic framework controlling structural rules.
286+
287+
### Macros, metaprogramming, and proof automation
288+
289+
- `HigherOrderUnification.lean` — Higher-order unification syntax and solving scaffolding.
290+
- `HygienicMacros.lean` — Hygienic macro expansion and scope-safety metatheory.
291+
- `InductionRecursion.lean` — Induction-recursion universes and code interpretations.
292+
- `MacroSystems.lean` — Macro system core with scoped marks and expansion relations.
293+
- `MacroTyping.lean` — Macro typing/staging rules for typed expansion.
294+
- `TacticFrameworks.lean` — Lightweight tactic framework formalization hooks.
295+
296+
### Project scaffolding and integration modules
297+
298+
- `Basic.lean` — Minimal sandbox module used for local sanity checks.
299+
- `Confluence.lean` — Standalone confluence/closure helper development.
300+
- `DeBruijn.lean` — De Bruijn-indexed substitution/renaming infrastructure.
301+
- `Metrics.lean` — Project metrics and theorem inventory checks.
302+
- `MetatheoryGrandTour.lean` — Grand tour module connecting major components.
303+
194304
## Key Theorems
195305

196306
### Generic Rewriting Framework
@@ -292,6 +402,7 @@ example {M : Term} {τ : Ty} (h : ⊢ M : τ) : M.IsValue ∨ ∃ N, M.Step N :=
292402
Metatheory/
293403
├── Metatheory.lean # Main entry point
294404
├── Metrics.lean # Project statistics and theorem summary
405+
├── *.lean (78 standalone modules) # Extended formalizations; see section above
295406
296407
├── Rewriting/ # Layer 0: Generic ARS Framework
297408
│ ├── Basic.lean # Star, Plus, Joinable, Diamond, Confluent

docs/ARCHITECTURE.md

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
# Metatheory Architecture
2+
3+
This document summarizes the architecture of the `Metatheory` Lean 4 project after reviewing all tracked `.lean` files in the repository.
4+
5+
## 1) Two-tier architecture
6+
7+
Metatheory has two distinct layers:
8+
9+
### A. Core formalized modules (tightly coupled)
10+
11+
Core directories:
12+
13+
- `Metatheory/Rewriting`
14+
- `Metatheory/Lambda`
15+
- `Metatheory/CL`
16+
- `Metatheory/TRS`
17+
- `Metatheory/StringRewriting`
18+
- `Metatheory/STLC`
19+
- `Metatheory/STLCext`
20+
- `Metatheory/STLCextBool`
21+
- `Metatheory/SystemF`
22+
23+
Characteristics:
24+
25+
- Deep interdependencies across modules (especially through `Rewriting` and between typed/untyped calculi).
26+
- Shared proof infrastructure (`Star`, `Confluent`, `Diamond`, termination, normal forms).
27+
- Generic-to-specific proof reuse (e.g., proving a local property once, then lifting to confluence through generic theorems).
28+
- Mathlib-facing interoperability via `Rewriting/Compat.lean` (mathlib-style naming), plus targeted Mathlib imports in advanced TRS components.
29+
30+
### B. Extended standalone modules (broad, mostly self-contained)
31+
32+
At `Metatheory/*.lean`, there are about ~70+ standalone modules (currently 78 files) covering topics like effects, macro systems, abstract interpretation, domain/game/category semantics, compilation, and advanced type disciplines.
33+
34+
Characteristics:
35+
36+
- Usually self-contained, with light or no imports.
37+
- Repeated use of a lightweight **computational paths** style (`Step`/`Path`) rather than depending on the full generic rewriting stack.
38+
- Local proof algebra built from path composition and symmetry (`trans`, `symm`) and context lifting (`congrArg`-style transport).
39+
40+
---
41+
42+
## 2) Core module dependency graph
43+
44+
Directory-level dependencies (cross-module):
45+
46+
```text
47+
Rewriting -> (foundation; no core dependency)
48+
Lambda -> Rewriting
49+
CL -> Rewriting
50+
TRS -> Rewriting
51+
StringRewriting-> Rewriting
52+
STLC -> Lambda, Rewriting
53+
STLCext -> Rewriting
54+
STLCextBool -> STLCext, Rewriting
55+
SystemF -> Rewriting
56+
```
57+
58+
Mermaid view:
59+
60+
```mermaid
61+
graph TD
62+
Rewriting --> Lambda
63+
Rewriting --> CL
64+
Rewriting --> TRS
65+
Rewriting --> StringRewriting
66+
Rewriting --> STLC
67+
Rewriting --> STLCext
68+
Rewriting --> STLCextBool
69+
Rewriting --> SystemF
70+
Lambda --> STLC
71+
STLCext --> STLCextBool
72+
```
73+
74+
Representative internal pipelines:
75+
76+
- **Lambda:** `Term → Beta → MultiStep → Parallel → Complete → Diamond → Confluence/Generic`
77+
- **STLCext:** `Types/Terms/Reduction/Parallel/Complete → Confluence`
78+
- **SystemF:** `Types/Terms/Typing → SubjectReduction + StrongReduction + StrongNormalization → Parallel/Complete/Diamond → Confluence`
79+
- **TRS/StringRewriting:** `Syntax/Rules → local confluence + termination → Newman-based confluence`
80+
81+
---
82+
83+
## 3) Core types and relations
84+
85+
| Name | Location | Role |
86+
|---|---|---|
87+
| `Term` | `Lambda/Term.lean`, `CL/Syntax.lean`, `SystemF/Terms.lean`, `STLCext*/Terms.lean`, `TRS/FirstOrder/Syntax.lean` | Object language terms (de Bruijn in lambda/system F; first-order terms over signatures in TRS). |
88+
| `Ty` | `STLC/Types.lean`, `STLCext/Types.lean`, `STLCextBool/Types.lean`, `SystemF/Types.lean` | Type grammars for each calculus. |
89+
| `Subst` | `TRS/FirstOrder/Syntax.lean` (`Nat → Term sig`), `SystemF/StrongNormalization.lean` | Substitution models for first-order and higher-order developments. |
90+
| `Signature` | `TRS/FirstOrder/Syntax.lean` | Function symbols + arity for first-order TRS. |
91+
| `Rule` / `RuleSet` | `TRS/FirstOrder/Rules.lean` | Rewrite rules and rule predicates. |
92+
| `HasType` | `STLC/Typing.lean`, `STLCext/Typing.lean`, `STLCextBool/Typing.lean`, `SystemF/Typing.lean` | Typing judgments. |
93+
| `Step` | multiple modules | One-step reduction relation. |
94+
| `ParRed` | `Lambda/Parallel.lean`, `CL/Parallel.lean`, `STLCext/Parallel.lean`, `STLCextBool/Parallel.lean`, `SystemF/Parallel.lean` | Parallel reduction for diamond-style confluence proofs. |
95+
| `Star`, `Plus`, `Joinable`, `Diamond`, `Confluent`, `Terminating` | `Rewriting/Basic.lean` | Generic ARS closure/confluence/termination framework. |
96+
97+
---
98+
99+
## 4) Key theorems across the project
100+
101+
### Generic rewriting framework
102+
103+
- `Rewriting.confluent_of_diamond` (`Rewriting/Diamond.lean`)
104+
- `Rewriting.confluent_of_terminating_localConfluent` (`Rewriting/Newman.lean`)
105+
- `Rewriting.confluent_union` (Hindley-Rosen, `Rewriting/HindleyRosen.lean`)
106+
- Normal-form uniqueness/existence results in `Rewriting/Basic.lean`
107+
108+
### Lambda calculus / CL
109+
110+
- `Lambda.confluence` (Church-Rosser, `Lambda/Confluence.lean`)
111+
- `Lambda.parRed_diamond` (`Lambda/Generic.lean`)
112+
- `CL.parRed_diamond`, `CL.confluent`, `CL.church_rosser` (`CL/Confluence.lean`)
113+
114+
### TRS / String rewriting
115+
116+
- `TRS.local_confluent`, `TRS.step_terminating`, TRS confluence via Newman (`TRS/Confluence.lean`)
117+
- `StringRewriting.local_confluent`, `StringRewriting.step_terminating`, and confluence (`StringRewriting/Confluence.lean`)
118+
119+
### STLC / extended STLC / booleans / System F
120+
121+
- `STLC.subject_reduction`, `STLC.strong_normalization`
122+
- `STLCext.subject_reduction`, `STLCext.strong_normalization`, `STLCext.confluence`
123+
- `STLCextBool.subject_reduction`, `STLCextBool.strong_normalization`, `STLCextBool.confluence`
124+
- `SystemF.subject_reduction`, `SystemF.strong_normalization`, `SystemF.confluence`
125+
126+
---
127+
128+
## 5) Computational paths pattern in standalone modules
129+
130+
Many standalone modules use a recurring lightweight proof kernel:
131+
132+
```lean
133+
inductive Step (α : Type) : α → α → Type
134+
inductive Path (α : Type) : α → α → Type
135+
def Path.trans : Path α a b → Path α b c → Path α a c
136+
def Path.symm : Path α a b → Path α b a
137+
def Path.congrArg ...
138+
```
139+
140+
Interpretation:
141+
142+
- `Step`: named atomic rewrite/equational moves.
143+
- `Path`: explicit proof object for multi-step chains.
144+
- `trans`: composition of chains (path concatenation).
145+
- `symm`: reversible reasoning.
146+
- `congrArg` (and variants): lift paths through context/formers.
147+
148+
This functions as a local, domain-specific rewriting/equality framework without requiring the full generic ARS stack in each standalone file.
149+
150+
---
151+
152+
## 6) Design decisions
153+
154+
### De Bruijn indices
155+
156+
- Used in `Lambda`, `STLC`-adjacent term layers, and `SystemF`.
157+
- Avoids alpha-conversion bureaucracy and gives direct structural recursion for substitution/shift lemmas.
158+
159+
### Parallel reduction for confluence
160+
161+
- Core confluence strategy in `Lambda`, `CL`, `STLCext`, `STLCextBool`, and `SystemF`.
162+
- Standard pipeline: define `ParRed` + `complete` development, prove diamond, then apply generic `confluent_of_diamond`.
163+
164+
### Tait/logical-relations method for strong normalization
165+
166+
- Used in `STLC/Normalization.lean`, `STLCext/Normalization.lean`, and `SystemF/StrongNormalization.lean`.
167+
- Main shape: reducibility candidates / reducibility predicate / fundamental lemma / SN corollary.
168+
169+
---
170+
171+
## 7) Future work and currently parked pieces
172+
173+
### TRS FirstOrder case-study modules are intentionally parked
174+
175+
The following files contain TODO comment blocks around substantial proof/code regions tied to missing dependency alignment (e.g., `Fin.cons`, `List.Shortlex`, related Mathlib APIs):
176+
177+
- `TRS/FirstOrder/GroupTheory.lean`
178+
- `TRS/FirstOrder/BooleanCaseStudy.lean`
179+
- `TRS/FirstOrder/DependencyPairs.lean`
180+
181+
Related TODO marker:
182+
183+
- `TRS/FirstOrder/Examples.lean` includes a restoration note for `Ordering/DependencyPairs`.
184+
185+
### STLCext confluence is already present
186+
187+
`Metatheory/STLCext/Confluence.lean` exists and proves confluence (Church-Rosser form) using the same parallel-reduction + diamond architecture as other core calculi.
188+

0 commit comments

Comments
 (0)