Zulip Chat Archive

Stream: general

Topic: Simply Typed Lambda Calculus in mathlib


zbatt (Nov 09 2022 at 02:23):

I noticed recently there's no formalization for simply typed lambda calculus in the mathlib. I'm a fairly inexperienced Lean user, but I do have a formalization that I did for a project here. I would be really interested in trying to get this to a place where it could live in the mathlib. There are a bunch of auxillary lemmas, but the main "theorem-worthy" results I have right now are proofs of Unicity of typing, Progress, and Preservation. (There are also a couple of results I proved only because they were asked as a homework question in one of my classes - lines 300 to 565 are probably not worth putting in the mathlib) Besides concerns of style which obviously needs to be improved, I have a couple of questions:

  1. Is this even a reasonable foundation for STLC in Lean? If not, why not and how can I improve it?

  2. Is this a thing that belongs in the mathlib?

  3. If the answers to the previous two questions were yes, how would I decide the "canonical" version of STLC to work with. What types should I include? How should substituion be defined? etc.

Thank you so much and I would be eager for any feedback about my code or otherwise!

Johan Commelin (Nov 09 2022 at 06:50):

I'm not an expert on lambda calculus at all. But I think such things could definitely go in mathlib.

Kevin Buzzard (Nov 09 2022 at 06:55):

Does this development need anything at all of the current theory in mathlib?

zbatt (Nov 09 2022 at 07:13):

Kevin Buzzard said:

Does this development need anything at all of the current theory in mathlib?

Sorry, what do you mean by this?

Johan Commelin (Nov 09 2022 at 07:16):

Are you depending on parts of mathlib?

Johan Commelin (Nov 09 2022 at 07:17):

It looks like you depend on some tactics, and maybe a few things about option?

zbatt (Nov 09 2022 at 07:18):

Ah ok, yes that's exactly right. I use some of the mathlib tactics such as induction' and split_ifs. I'm realizing now that the option import was from an earlier version and I no longer need it so mathlib dependencies are limited to just tactics.

Yuyang Zhao (Nov 09 2022 at 08:52):

I've heard another definition from others, which translates into lean is something like the following (there are some missed things), but I'm not sure it's better.

Johan Commelin (Nov 09 2022 at 09:49):

@zbatt I should maybe walk back my statement a bit. As I said I'm not an expert on lambda calculus at all. It is my opinion that some type theoretic stuff should be welcome in mathlib. But for the particular details, I would rather defer to someone more knowledgeable.

Floris van Doorn (Nov 09 2022 at 11:18):

I think a formalization of lambda calculus / type theory suffers from a similar problem as that of graph theory: there are many different (non-equivalent) formulations, and it depends on your goals which formulation you want to use.
A difference with graph theory is that eventually quite some mathematical results will use graphs, but I think the usage of a formalization of (simply) typed lambda calculus will be quite limited.

Some choices you have to make when formalizing type theory. Of course there is the question of simply typed lambda calculus vs dependent type theory, but even in simply typed lambda calculus, there are many questions:

  • What are your base types?
  • How do you encode variables (named, de Bruijn variables, locally nameless, ...)?
  • Do lambda abstractions come with their type?
  • Do you want to define well-typed terms in terms of raw terms, or do you want to directly define the "well-typed terms in context Γ with type A" directly as an inductive definition?

My preference is to wait with including type theory/lambda calculus in mathlib, until someone does a relatively big formalization and shows that their formalization is actually usable, or maybe until multiple (groups of) people want to work on it independently...

Btw, @zbatt: one thing in your formalization that is not very standard is that you allow for infinite contexts.

Alex J. Best (Nov 09 2022 at 14:31):

@Floris van Doorn what about putting it in achive instead?

Floris van Doorn (Nov 09 2022 at 14:45):

If the code quality is good, I'm fine with that.

Floris van Doorn (Nov 09 2022 at 14:47):

Although that might send the wrong message that lambda calculus does not belong to mathlib, which is not the message I want to send...

zbatt (Nov 09 2022 at 16:58):

Floris van Doorn said:

I think a formalization of lambda calculus / type theory suffers from a similar problem as that of graph theory: there are many different (non-equivalent) formulations, and it depends on your goals which formulation you want to use.
A difference with graph theory is that eventually quite some mathematical results will use graphs, but I think the usage of a formalization of (simply) typed lambda calculus will be quite limited.

You have to make many choices when formalizing type theory. Of course there is the question of simply typed lambda calculus vs dependent type theory, but even in simply typed lambda calculus, there are many questions:

  • What are your base types?
  • How do you encode variables (named, de Bruijn variables, locally nameless, ...)?
  • Do lambda abstractions come with their type?
  • Do you want to define well-typed terms in terms of raw terms, or do you want to directly define the "well-typed terms in context Γ with type A" directly as an inductive definition?

My preference is to wait with including type theory/lambda calculus in mathlib, until someone does a relatively big formalization and shows that their formalization is actually usable, or maybe until multiple (groups of) people want to work on it independently...

Btw, zbatt: one thing in your formalization that is not very standard is that you allow for infinite contexts.

That makes a lot of sense. I definitely am not the person that should be making those decisions about the canonical formalization of this. I was hoping that maybe there was some standard I was unaware of which is part of the reason I posted this, but if not, those decisions should absolutely be made by someone with more experience.

And thanks for the note about infinite contexts! I'll be sure to get around to updating that.

Thorsten Altenkirch (Nov 10 2022 at 11:24):

In general I think it is a bad idea to define untyped terms first. I would always do it intrinsically, see attached. stl.lean

Thorsten Altenkirch (Nov 10 2022 at 11:29):

This certainly works very well in agda using dependently typed pattern matching, I don't know how well this fits into Lean.
However, I would argue that this is conceptually the right definition, since there is no reason to define untyped terms if you are interested in typed ones. We have also done this for dependent types, e.g. see https://dl.acm.org/doi/abs/10.1145/2837614.2837638


Last updated: Dec 20 2023 at 11:08 UTC