Skip to content

Conversation

@joscoh
Copy link
Contributor

@joscoh joscoh commented Nov 5, 2025

Description of changes:

This PR makes 3 main changes:

  1. Adds identifiers to the type environment, providing general-purpose mechanisms (in Identifiers.lean) for checking dupliates more efficiently (with a hash table rather than a list)
  2. Splits the typing environment into 2 parts: LContext, which provides contextual information that does not change during Lambda typechecking (Factory, KnownTypes, and identifiers), and TEnv, which does (inferred types, substitutions, etc).
  3. Uses these changes to improve the typechecking for Boogie programs in ProgramType.lean. In particular, this no longer requires a separate check for nodups in the Program decl list; rather, it adds each identifier to the context as it is seen, automatically checking for uniqueness. This has the effect of performing more well-formedness checks in Lambda typechecking rather than in Boogie.

There are also two smaller changes:
4. Due to the above, proving that well-typed Boogie programs have unique names is no longer trivial, and this proof is added in ProgramWF.lean.
5. This PR does some other refactoring of TEnv, splitting it into the name-generation part and the rest. This will be needed for the bounded ints and/or function precondition PR, but is not immediately needed yet.

The bulk of the changes are in Identifiers.lean, LExprTypeEnv.lean, ProgramType.lean, and ProgramWF.lean.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@joscoh joscoh requested a review from MikaelMayer as a code owner November 5, 2025 18:18
Copy link
Contributor

@atomb atomb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a good change to me. And I've checked on some large examples that it doesn't slow down type checking.

@shigoel shigoel requested a review from aqjune-aws as a code owner November 7, 2025 03:48
Copy link
Contributor

@shigoel shigoel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @joscoh! Looks beautiful.

@shigoel shigoel added this pull request to the merge queue Nov 7, 2025
Merged via the queue into main with commit f8483b5 Nov 7, 2025
10 checks passed
@shigoel shigoel deleted the josh/refactor-env branch November 7, 2025 03:55
substitution and fresh variable generation, and a `KnownTypes` to track the
supported type constructors. It also has type information about a
factory of user-specified functions, which is used during type checking.
supported type constructors.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this comment is outdated - it does not use TState anymore, and also it isn't a stack of contexts (but this had existed even before this PR).

@aqjune-aws
Copy link
Contributor

Actually, I think that after this patch TState isn't necessary at all. I have this branch that implements it: https://github.com/strata-org/Strata/tree/jlee/tstate To reduce the number of relevant data structures, may I open a pull request that does this cleanup?

@joscoh
Copy link
Contributor Author

joscoh commented Nov 7, 2025

Actually, I think that after this patch TState isn't necessary at all. I have this branch that implements it: https://github.com/strata-org/Strata/tree/jlee/tstate To reduce the number of relevant data structures, may I open a pull request that does this cleanup?

Yes, I see what you mean. This PR was a combination of splitting up the TState which we will need for bounded ints/preconditions and then separating out the LContext, but when combining them, the end result might actually have too many data structures. This PR is merged but I think that PR would be a good idea.

@aqjune-aws
Copy link
Contributor

Thanks for confirming - I made #190. :)

github-merge-queue bot pushed a commit that referenced this pull request Nov 8, 2025
…te` to `TState` (#190)

*Description of changes:*

#184 factored out members in `structure TEnv` that don't change during
typing. This made `TState` somewhat less useful because (1) its
`TGenState` member is already included in `TGenEnv` and (2) type
substitution info is already in `TEnv`. Considering that `TEnv` is still
the top-level data structure that is passed around typing functions,
whenever one wants to use `TState`, `TEnv` can be used instead.
The diff of this PR indeed shows that the data structure was redundant
and every use case through `.state.substInfo` could be replaced with
`stateSubstInfo`.

By submitting this pull request, I confirm that you can use, modify,
copy, and redistribute this contribution, under the terms of your
choice.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants