-
Notifications
You must be signed in to change notification settings - Fork 10
Lef/ternary hoisting #589
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
elefthei
wants to merge
5
commits into
main
Choose a base branch
from
lef/ternary-hoisting
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.
Open
Lef/ternary hoisting #589
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6fdbcdf
Aspirational test.
gebner c1445e4
Implement defer construct for resource cleanup
gebner 7ef4749
Backtrack in the prover.
gebner 4f5a589
Merge branch 'main' into lef/ternary-hoisting
4a0ae21
Remove --cmi flag, fix auto-paren lexer for compound bracket tokens
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,75 @@ | ||
| # Stateful If/Match Conditions | ||
|
|
||
| ## Problem | ||
|
|
||
| `if` and `match` conditions were `term` (pure F\* expressions). Hoisting only | ||
| descends into `Tv_App` arguments, so stateful operations nested inside F\*-level | ||
| `if`/`match` within a condition were invisible: | ||
|
|
||
| ``` | ||
| if (if true then !r = 0 else false) { ... } // fails: !r buried in Tv_Match | ||
| ``` | ||
|
|
||
| ## Solution | ||
|
|
||
| Change `Tm_If.b` and `Tm_Match.sc` from `term` to `st_term`, following the | ||
| existing `Tm_While.condition` pattern. A token-stream preprocessor makes | ||
| parentheses optional for backward compatibility. | ||
|
|
||
| ## Files Changed | ||
|
|
||
| ### Parser & Sugar | ||
|
|
||
| | File | Change | | ||
| |------|--------| | ||
| | `pulseparser.mly` — `ifStmt`, `matchStmt` | `IF LPAREN pulseStmt RPAREN`, `MATCH LPAREN pulseStmt RPAREN` | | ||
| | `PulseSyntaxExtension_Parser.ml` — `make_auto_paren_lexer` | Token preprocessor: auto-inserts `LPAREN`/`RPAREN` when omitted. Detects `LBRACE` at depth 0 as end-of-condition. Passes through F\*-level `if/then` and `match/with` unmodified. | | ||
|
Contributor
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. Please just add two productions (one for pulseStmt, one for terms) to the grammar instead. |
||
| | `PulseSyntaxExtension.Sugar.fst` — `If.head`, `Match.head` | `A.term` → `stmt` | | ||
| | `PulseSyntaxExtension.Desugar.fst` — `If`/`Match` cases | `desugar_term` → `desugar_stmt` | | ||
| | `PulseSyntaxExtension.SyntaxWrapper.fsti` — `tm_if`, `tm_match` | Parameter type `term` → `st_term` | | ||
| | `PulseSyntaxExtension_SyntaxWrapper.ml` — `tm_if`, `tm_match` | Wrap with `Tm_STApp`/return as needed | | ||
|
|
||
| ### AST & Naming | ||
|
|
||
| | File | Change | | ||
| |------|--------| | ||
| | `Pulse.Syntax.Base.fsti` — `Tm_If.b`, `Tm_Match.sc` | `term` → `st_term` | | ||
| | `Pulse.Syntax.Base.fst` — `eq_st_term'` | `eq_tm` → `eq_st_term` for `b`/`sc` | | ||
| | `Pulse.Syntax.Naming.fsti` — `freevars_st'`, `ln_st'`, `subst_st_term'` | `*_term` → `*_st_term` for `b`/`sc` (follows `Tm_While.condition`) | | ||
| | `Pulse.Syntax.Naming.fst` — `close_open_inverse_st'` | Same pattern | | ||
| | `Pulse.Typing.FV.fst` — `freevars_close_st_term'` | Same pattern | | ||
| | `Pulse.Syntax.Printer.fst` — `print_st_head` | `term_to_string b` → `st_term_to_string b` | | ||
| | `Pulse.ElimGoto.fst` — `Tm_If`/`Tm_Match` cases | `elab_term` → `elab_st_term` for `b`/`sc` | | ||
|
|
||
| ### Checker | ||
|
|
||
| | File | Function | Change | | ||
| |------|----------|--------| | ||
| | `Pulse.Checker.If.fst` | `check_if_term` (new) | Pure path: extracts `term` from `Tm_Return`, uses original `check_equiv_emp` logic | | ||
| | | `check` | Dispatches: `Tm_Return` → `check_if_term`, other → checks `b` via `check g b ...`, composes with `compose_checker_result_t` | | ||
| | `Pulse.Checker.Match.fst` | `check_match_term` (new) | Pure path: `compute_tot_term_type_and_u` on extracted term | | ||
| | | `check` | Dispatches: `Tm_Return` → `check_match_term`, other → checks `sc` via `check g sc ...`, composes | | ||
| | `Pulse.Checker.fst` | `maybe_elaborate_stateful_head` | Restored `Tm_If`/`Tm_Match` cases: extracts F\* term from `Tm_Return`, runs `hoist_stateful_apps`, rebuilds as `st_term` | | ||
|
|
||
| ### Extraction | ||
|
|
||
| | File | Change | | ||
| |------|--------| | ||
| | `Pulse.Extract.Main.fst` — `Tm_If`, `Tm_Match` | Uniform `extract_dv g b` / `extract_dv g sc` — no `Tm_Return` special-case | | ||
| | `Pulse_Extract_CompilerLib.ml` — `mk_if` | Binds monadic condition to fresh variable via `mk_let`, then branches on it | | ||
|
|
||
| ### Test | ||
|
|
||
| | File | Purpose | | ||
| |------|---------| | ||
| | `test/StatefulIfCondition.fst` | Regression: `if !r = 0 { }`, `if (if true { !r = 0 } else { false }) { }`, `match Some 1 { }` | | ||
|
|
||
| ## Token Preprocessor Logic | ||
|
|
||
| `make_auto_paren_lexer` wraps the base lexer. On `IF`/`MATCH`: | ||
|
|
||
| 1. If next token is `LPAREN` → pass through (already parenthesized) | ||
| 2. Otherwise, buffer tokens tracking `()`/`[]` nesting depth: | ||
| - `LBRACE` at depth 0 → insert `LPAREN` before buffered tokens, `RPAREN` after | ||
| - `RETURNS`/`ENSURES` at depth 0 → same (annotation before body) | ||
| - `THEN`/`WITH` at depth 0 → F\*-level syntax, no modification | ||
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
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
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
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
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 |
|---|---|---|
|
|
@@ -11,13 +11,13 @@ OTHERFLAGS += --include ../_cache | |
| # Note: ^ a bit of a hack. This directory can work independently of the | ||
| # DPE directory above, but in a test we first run verify on DPE which | ||
| # involves verifying everything here already, so this saves some time. | ||
| OTHERFLAGS += --warn_error -342 --cmi | ||
| OTHERFLAGS += --warn_error -342 | ||
|
Contributor
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. This requires updating CI to use the fstar2 branch. |
||
| OUTPUT_DIR=_output | ||
| CODEGEN=krml | ||
| TAG=cbor | ||
| ROOTS=$(shell find $(SRC) -type f -name '*.fst' -o -name '*.fsti') | ||
| DEPFLAGS=--extract '* -FStar -Pulse -PulseCore' | ||
| OTHERFLAGS += --cmi --already_cached '*,-CBOR.Pulse.Type,-CDDLExtractionTest' | ||
| OTHERFLAGS += --already_cached '*,-CBOR.Pulse.Type,-CDDLExtractionTest' | ||
| include $(PULSE_ROOT)/mk/boot.mk | ||
|
|
||
| .DEFAULT_GOAL := myall | ||
|
|
||
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
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
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
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
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
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
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
FWIW, this is kind of the easy case. We could support this very easily be desugaring it during checking to:
(And indeed, after reading the PR that's exactly what you're doing, which is good.)
Maybe this wasn't clear from the issue description, but the actually hard issue is the following where a stateful expression needs to be hoisted out of an if in a pure expression.