-
Notifications
You must be signed in to change notification settings - Fork 95
feat: LambdaCalculus.LocallyNameless.Coc
#392
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
matthunz
wants to merge
17
commits into
leanprover:main
Choose a base branch
from
matthunz:coc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+344
β0
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
0ddd4a8
feat: Term
matthunz 448207e
feat: Term.rename
matthunz 745a7c6
feat: Term.subst
matthunz 1d265e4
feat: Term.BetaEquiv and HasBetaEquiv
matthunz 7979775
feat: Typing
matthunz e8cef24
refactor: rename lam to abs to fit styling of other AST
matthunz 29c2bd9
fix: BetaEq relationship order in eq
matthunz b3c0b23
feat: switch to locally-nameless representation
matthunz 8a20752
feat: add opening lemmas for locally-closed terms and split up code iβ¦
matthunz 13ec089
feat: WellFormed
matthunz 0d6e3b9
refactor: re-use existing Context for Env
matthunz 52d3606
refactor: rename opening to open' to match current conventions
matthunz 58caddf
refactor: rename LC-related variables for clarity
matthunz 827087b
refactor: replace use of classical with constraint on DecidableEq to β¦
matthunz d6afe92
fix: extend BetaEquiv to match the definition by Chargueraud
matthunz 12c85b0
feat: add sort level to Term.type AST
matthunz e69ac78
fix: stronger typing relations in Env.Wf and Typing
matthunz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| /- | ||
| Copyright (c) 2026 Matt Hunzinger. All rights reserved. | ||
| Released under Apache 2.0 license as described in the file LICENSE. | ||
| Authors: Matt Hunzinger | ||
| -/ | ||
|
|
||
| module | ||
|
|
||
| public import Cslib.Init | ||
|
|
||
| public section | ||
|
|
||
| namespace Cslib | ||
|
|
||
| /-- Typeclass for the Ξ²-equivalence notation `x =Ξ² y`. -/ | ||
| class HasBetaEquiv (Ξ± : Type u) where | ||
| /-- Ξ²-equivalence relation for type Ξ±. -/ | ||
| BetaEquiv : Ξ± β Ξ± β Prop | ||
|
|
||
| @[inherit_doc] | ||
| notation m:max " =Ξ² " n:max => HasBetaEquiv.BetaEquiv m n | ||
|
|
||
| end Cslib |
62 changes: 62 additions & 0 deletions
62
Cslib/Languages/LambdaCalculus/LocallyNameless/Coc/Basic.lean
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /- | ||
| Copyright (c) 2026 Matt Hunzinger. All rights reserved. | ||
| Released under Apache 2.0 license as described in the file LICENSE. | ||
| Authors: Matt Hunzinger | ||
| -/ | ||
|
|
||
| module | ||
|
|
||
| public import Cslib.Foundations.Data.HasFresh | ||
| public import Cslib.Languages.LambdaCalculus.LocallyNameless.Context | ||
|
|
||
| @[expose] public section | ||
|
|
||
| /-! # Calculus of Constructions | ||
|
|
||
| The Calculus of Constructions | ||
|
|
||
| ## References | ||
|
|
||
| * [T. Coquand, *An algorithm for type-checking dependent types*][Coquand1996] | ||
|
|
||
| * [A. Chargueraud, *The Locally Nameless Representation*][Chargueraud2012] | ||
|
|
||
| -/ | ||
|
|
||
| namespace Cslib | ||
|
|
||
| universe u | ||
|
|
||
| namespace LambdaCalculus.LocallyNameless.Coc | ||
|
|
||
| /-- Syntax of locally nameless terms, with free variables over `Var`. -/ | ||
| inductive Term (Var : Type u) : Type u | ||
| /-- Bound term variables using a de-Bruijn index. -/ | ||
| | bvar : β β Term Var | ||
| /-- Free term variables. -/ | ||
| | fvar : Var β Term Var | ||
| /-- Function application. -/ | ||
| | app : Term Var β Term Var β Term Var | ||
| /-- Lambda abstraction. -/ | ||
| | abs : Term Var β Term Var β Term Var | ||
| /-- Pi type. -/ | ||
| | pi : Term Var β Term Var β Term Var | ||
| /-- Type universe. -/ | ||
| | type : β β Term Var | ||
| deriving DecidableEq | ||
|
|
||
| /-- Free variables. -/ | ||
| @[scoped grind =] | ||
| def Term.fv [DecidableEq Var] : Term Var β Finset Var | ||
| | .bvar _ => β | ||
| | .fvar z => {z} | ||
| | .app f a => f.fv βͺ a.fv | ||
| | .abs t b => t.fv βͺ b.fv | ||
| | .pi t b => t.fv βͺ b.fv | ||
| | .type _ => β | ||
|
|
||
| abbrev Env (Var : Type u) := Context Var (Term Var) | ||
|
|
||
| end LambdaCalculus.LocallyNameless.Coc | ||
|
|
||
| end Cslib |
128 changes: 128 additions & 0 deletions
128
Cslib/Languages/LambdaCalculus/LocallyNameless/Coc/Opening.lean
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| /- | ||
| Copyright (c) 2026 Matt Hunzinger. All rights reserved. | ||
| Released under Apache 2.0 license as described in the file LICENSE. | ||
| Authors: Matt Hunzinger | ||
| -/ | ||
|
|
||
| module | ||
|
|
||
| public import Cslib.Foundations.Data.HasFresh | ||
| public import Cslib.Foundations.Syntax.HasSubstitution | ||
| public import Cslib.Languages.LambdaCalculus.LocallyNameless.Coc.Basic | ||
|
|
||
| @[expose] public section | ||
|
|
||
| /-! # Calculus of Constructions | ||
|
|
||
| The Calculus of Constructions | ||
|
|
||
| ## References | ||
|
|
||
| * [T. Coquand, *An algorithm for type-checking dependent types*][Coquand1996] | ||
|
|
||
| * [A. Chargueraud, *The Locally Nameless Representation*][Chargueraud2012] | ||
|
|
||
| -/ | ||
|
|
||
| set_option linter.unusedDecidableInType false | ||
|
|
||
| namespace Cslib | ||
|
|
||
| universe u | ||
|
|
||
| namespace LambdaCalculus.LocallyNameless.Coc | ||
|
|
||
| namespace Term | ||
|
|
||
| /-- Variable opening of the ith bound variable. -/ | ||
| @[scoped grind =] | ||
| def openRec (i : β) (s : Term Var) : Term Var β Term Var | ||
| | .bvar y => if i = y then s else (bvar y) | ||
| | .fvar x => fvar x | ||
| | .app f a => .app (openRec i s f) (openRec i s a) | ||
| | .abs Ο tβ => abs (openRec i s Ο) (openRec (i + 1) s tβ) | ||
| | .pi Ο tβ => .pi (openRec i s Ο) (openRec (i + 1) s tβ) | ||
| | .type s => .type s | ||
|
|
||
| /-- Variable opening of the closest binding. -/ | ||
| @[scoped grind =] | ||
| def open' (s : Term Var) (t : Term Var) : Term Var := openRec 0 t s | ||
|
|
||
| @[inherit_doc] | ||
| scoped infixr:80 " ^α΅ " => open' | ||
|
|
||
| @[inherit_doc] | ||
| scoped notation:68 Ξ³ "β¦" X " β " Ξ΄ "β§"=> openRec X Ξ΄ Ξ³ | ||
|
|
||
| /-- | ||
| Capture-avoiding substitution. | ||
| `m.subst x r` replaces the free occurrences of variable `x` in `m` with `r`. | ||
| -/ | ||
| @[scoped grind =] | ||
| def subst [DecidableEq Var] (m : Term Var) (x : Var) (r : Term Var) : Term Var := | ||
| match m with | ||
| | .bvar n => .bvar n | ||
| | .fvar z => if z = x then r else .fvar z | ||
| | .app f a => .app (f.subst x r) (a.subst x r) | ||
| | .abs t b => .abs (t.subst x r) (b.subst x r) | ||
| | .pi t b => .pi (t.subst x r) (b.subst x r) | ||
| | .type s => .type s | ||
|
|
||
| /-- `Term.subst` is a substitution for Ξ»-terms. Gives access to the notation `m[x := n]`. -/ | ||
| instance instHasSubstitutionTerm [DecidableEq Var] : | ||
| HasSubstitution (Term Var) Var (Term Var) where | ||
| subst := Term.subst | ||
|
|
||
| @[scoped grind _=_] | ||
| lemma subst_def [DecidableEq Var] : | ||
| Term.subst (m : Term Var) (x : Var) (r : Term Var) = m[x := r] := rfl | ||
|
|
||
| /-- Substitution of a free variable not present in a term leaves it unchanged. -/ | ||
| lemma subst_fresh [DecidableEq Var] {Ξ³ : Term Var} | ||
| (nmem : X β Ξ³.fv) (Ξ΄ : Term Var) : Ξ³ = Ξ³[X := Ξ΄] := by | ||
| induction Ξ³ <;> grind | ||
|
|
||
| /-- An opening appearing in both sides of an equality of terms can be removed. -/ | ||
| lemma openRec_neq_eq (neq : x β y) (eq : tβ¦y β sββ§ = tβ¦y β sββ§β¦x β sββ§) : | ||
| t = tβ¦x β sββ§ := by | ||
| induction t generalizing x y <;> grind | ||
|
|
||
| /-- Locally closed terms. -/ | ||
| inductive LC : Term Var β Prop | ||
| | var : LC (.fvar x) | ||
| | app : LC tβ β LC tβ β LC (app tβ tβ) | ||
| | abs (L : Finset Var) : tβ.LC β (β x β L, LC (tβ ^α΅ fvar x)) β LC (abs tβ tβ) | ||
| | pi (L : Finset Var) : tβ.LC β (β x β L, LC (tβ ^α΅ fvar x)) β LC (pi tβ tβ) | ||
| | type : LC (.type _) | ||
|
|
||
| attribute [scoped grind .] LC.var LC.app LC.type | ||
|
|
||
| /-- Predicate for being a locally closed body of an abstraction. -/ | ||
| @[scoped grind =] | ||
| def body (t : Term Var) := β L : Finset Var, β x β L, LC (t ^α΅ fvar x) | ||
|
|
||
| /-- A locally closed term is unchanged by opening. -/ | ||
| lemma openRec_lc [DecidableEq Var] [HasFresh Var] {Ο Ο : Term Var} (lc : Ο.LC) : Ο = Οβ¦X β Οβ§ := by | ||
| induction lc generalizing X with | ||
| | abs | pi => grind [fresh_exists <| free_union Var, openRec_neq_eq] | ||
| | _ => grind | ||
|
|
||
| /-- Substitution of a locally closed type distributes with opening. -/ | ||
| lemma openRec_subst [DecidableEq Var] [HasFresh Var] {Ξ΄ : Term Var} | ||
| (Y : β) (tβ tβ : Term Var) (lc : Ξ΄.LC) (X : Var) : | ||
| (tββ¦Y β tββ§)[X := Ξ΄] = tβ[X := Ξ΄]β¦Y β tβ[X := Ξ΄]β§ := by | ||
| induction tβ generalizing Y <;> grind [openRec_lc] | ||
|
|
||
| /-- A locally closed term remains locally closed after substitution. -/ | ||
| lemma subst_lc [DecidableEq Var] [HasFresh Var] {tβ tβ : Term Var} | ||
| (tβ_lc : tβ.LC) (tβ_lc : tβ.LC) (X : Var) : tβ[X := tβ].LC := by | ||
| induction tβ_lc with | ||
| | abs => grind [LC.abs (free_union Var), openRec_subst] | ||
| | pi => grind [LC.pi (free_union Var), openRec_subst] | ||
| | _ => grind [openRec_subst] | ||
|
|
||
| end Term | ||
|
|
||
| end LambdaCalculus.LocallyNameless.Coc | ||
|
|
||
| end Cslib |
117 changes: 117 additions & 0 deletions
117
Cslib/Languages/LambdaCalculus/LocallyNameless/Coc/Typing.lean
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| /- | ||
| Copyright (c) 2026 Matt Hunzinger. All rights reserved. | ||
| Released under Apache 2.0 license as described in the file LICENSE. | ||
| Authors: Matt Hunzinger | ||
| -/ | ||
|
|
||
| module | ||
|
|
||
| public import Cslib.Foundations.Data.HasFresh | ||
| public import Cslib.Foundations.Syntax.HasAlphaEquiv | ||
| public import Cslib.Foundations.Syntax.HasBetaEquiv | ||
| public import Cslib.Foundations.Syntax.HasSubstitution | ||
| public import Cslib.Languages.LambdaCalculus.LocallyNameless.Coc.Opening | ||
|
|
||
| @[expose] public section | ||
|
|
||
| /-! # Calculus of Constructions | ||
| The Calculus of Constructions | ||
| ## References | ||
| * [T. Coquand, *An algorithm for type-checking dependent types*][Coquand1996] | ||
| * [A. Chargueraud, *The Locally Nameless Representation*][Chargueraud2012] | ||
| -/ | ||
|
|
||
| namespace Cslib | ||
|
|
||
| universe u | ||
|
|
||
| namespace LambdaCalculus.LocallyNameless.Coc | ||
|
|
||
| open Term | ||
|
|
||
| /-- Ξ²-reduction. -/ | ||
| inductive BetaEquiv : Term Var β Term Var β Prop | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I should have mentioned previously. This should use the |
||
| /-- Ξ²-redex: `(Ξ» A. B) N βΆ B ^α΅ N`. -/ | ||
| | red : (abs A B).LC β N.LC β BetaEquiv (.app (.abs A B) N) (B ^α΅ N) | ||
| /-- Congruence in the function position of an application. -/ | ||
| | appβ : tβ.LC β BetaEquiv tβ tβ' β BetaEquiv (.app tβ tβ) (.app tβ' tβ) | ||
| /-- Congruence in the argument position of an application. -/ | ||
| | appβ : tβ.LC β BetaEquiv tβ tβ' β BetaEquiv (.app tβ tβ) (.app tβ tβ') | ||
| /-- Congruence in the type annotation of an abstraction. -/ | ||
| | absβ : tβ.body β BetaEquiv tβ tβ' β BetaEquiv (.abs tβ tβ) (.abs tβ' tβ) | ||
| /-- Congruence in the body of an abstraction. -/ | ||
| | absβ (Ο : Finset Var) : | ||
| tβ.LC β | ||
| (β x β Ο, BetaEquiv (tβ ^α΅ .fvar x) (tβ' ^α΅ .fvar x)) β | ||
| BetaEquiv (.abs tβ tβ) (.abs tβ tβ') | ||
| /-- Congruence in the domain of a pi type. -/ | ||
| | piβ : tβ.body β BetaEquiv tβ tβ' β BetaEquiv (.pi tβ tβ) (.pi tβ' tβ) | ||
| /-- Congruence in the codomain of a pi type. -/ | ||
| | piβ (Ο : Finset Var) : | ||
| tβ.LC β | ||
| (β x β Ο, BetaEquiv (tβ ^α΅ .fvar x) (tβ' ^α΅ .fvar x)) β | ||
| BetaEquiv (.pi tβ tβ) (.pi tβ tβ') | ||
|
|
||
| instance instHasBetaEquivTerm : HasBetaEquiv (Term Var) where | ||
| BetaEquiv := BetaEquiv | ||
|
|
||
|
|
||
| variable [DecidableEq Var] | ||
|
|
||
| namespace Term | ||
|
|
||
| /-- Well-formedness of terms. -/ | ||
| inductive Wf : Env Var β Term Var β Prop | ||
| | var : β¨x, _β© β Ξ β Wf Ξ (fvar x) | ||
| | app : Wf Ξ f β Wf Ξ a β Wf Ξ (app f a) | ||
| | abs (L : Finset Var) : | ||
| Wf Ξ Ο β | ||
| (β X β L, Wf ({β¨X,Οβ©} βͺ Ξ) (Ο ^α΅ fvar X)) β | ||
| Wf Ξ (abs Ο Ο) | ||
| | pi (L : Finset Var) : | ||
| Wf Ξ Ο β | ||
| (β X β L, Wf ({β¨X,Οβ©} βͺ Ξ) (Ο ^α΅ fvar X)) β | ||
| Wf Ξ (pi Ο Ο) | ||
| | type : Wf Ξ (type _) | ||
|
|
||
| end Term | ||
|
|
||
| mutual | ||
|
|
||
| /-- An environment is well-formed if it binds each variable exactly once to a well-formed type. -/ | ||
| inductive Env.Wf : Env Var β Prop | ||
| | nil : Env.Wf {} | ||
| | cons : Env.Wf Ξ β Typing Ξ Ο (.type i) β x β Ξ.dom β Env.Wf ({β¨x, Οβ©} βͺ Ξ) | ||
|
|
||
| /-- Typing judgement -/ | ||
| inductive Typing : Env Var β Term Var β Term Var β Prop | ||
| /-- Variable lookup in Ξ -/ | ||
| | var : Ξ.Wf β β¨x, Aβ© β Ξ β Typing Ξ (.fvar x) A | ||
| /-- Function application -/ | ||
| | app : Typing Ξ M (.pi A B) β Typing Ξ N A β Typing Ξ (.app M N) (B ^α΅ N) | ||
| /-- Lambda abstraction -/ | ||
| | abs (Ο : Finset Var) : | ||
| Typing Ξ A K β | ||
| (β x β Ο, Typing ({β¨x, Aβ©} βͺ Ξ) (N ^α΅ .fvar x) (B ^α΅ .fvar x)) β | ||
| Typing Ξ (.abs A N) (.pi A B) | ||
| /-- Pi type -/ | ||
| | pi (Ο : Finset Var) : | ||
| Typing Ξ A (.type k) β | ||
| (β x β Ο, Typing ({β¨x, Aβ©} βͺ Ξ) (B ^α΅ .fvar x) (.type i)) β | ||
| (i = k β¨ i = 0) β | ||
| Typing Ξ (.pi A B) (.type i) | ||
| /-- Type universe -/ | ||
| | type : Typing Ξ (.type s) (.type (s + 1)) | ||
| /-- Ξ²-conversion -/ | ||
| | conv : Typing Ξ M A β A =Ξ² B β Typing Ξ B (.type i) β Typing Ξ M B | ||
|
|
||
| end | ||
|
|
||
| end LambdaCalculus.LocallyNameless.Coc | ||
|
|
||
| end Cslib | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This definition looks very different from what I expected. See for instance the definition in the Rocq formalization I closely followed for other type systems.