Skip to content
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

refactor: rewrite TerminationHint elaborators #2958

Merged
merged 3 commits into from
Dec 2, 2023
Merged

Conversation

nomeata
Copy link
Collaborator

@nomeata nomeata commented Nov 24, 2023

In order to familiarize myself with this code, and so that the next
person has an easier time, I

  • added docstrings explaining what I found out these things to
  • rewrote the syntax expansion functions using syntax pattern matches,
    to the extend possible

In order to familiarize myself with this code, and so that the next
person has an easier time, I

* added docstrings explaining what I found out these things to
* rewrote the syntax expansion functions using syntax pattern matches,
  to the extend possible
Comment on lines +34 to +54
match terminationHint with
| `(terminationByCore|termination_by' $hint1:terminationHint1)
| `(decreasingBy|decreasing_by $hint1:terminationHint1) =>
return TerminationHint.one { ref, value := hint1.raw[0] }
| `(terminationByCore|termination_by' $hints:terminationHintMany)
| `(decreasingBy|decreasing_by $hints:terminationHintMany) => do
let m ← hints.raw[0].getArgs.foldlM (init := {}) fun m arg =>
let declName? := cliques.findSome? fun clique => clique.findSome? fun declName =>
if arg[0].getId.isSuffixOf declName then some declName else none
match declName? with
| none => Macro.throwErrorAt arg[0] s!"function '{arg[0].getId}' not found in current declaration"
| some declName => return m.insert declName { ref := arg, value := arg[2] }
for clique in cliques do
let mut found? := Option.none
for declName in clique do
if let some { ref, .. } := m.find? declName then
if let some found := found? then
Macro.throwErrorAt ref s!"invalid termination hint element, '{declName}' and '{found}' are in the same clique"
found? := some declName
return TerminationHint.many m
| _ => Macro.throwUnsupported
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Here I wasn’t able to use more syntax matches. Is that because terminationHint1 and terminationHintMany are parametric parsers, so the syntax match elaboration doesn’t work? Or was I just not trying the right things?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, parametric parsers cannot be used as quotation parsers but you can still match through them:

`(terminationByCore| termination_by' $e:term)

Now, for the Many case something like

`(terminationByCore| termination_by' $[$ids:ident => $es $[;]?]*)

should work but I believe the lookahead is preventing that as it's not happy to see the $ instead of an actual identifier :/

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I never figured out complex syntax like that, with nested parsers. Thanks for the example!

Wrapping patternIgnore around the lookahead won’t help, I assume.

Copy link
Member

Choose a reason for hiding this comment

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

It won't, no. I suppose the lookahead can be called a workaround for tactics not currently being (contextual) keywords, which we want to fix. But also your "per-declaration termination_by" would fix it as well I assume as there would be no more need for Many?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, was about to say that; no need to dwell on this too much.

Happy with the rest of this PR?

@nomeata nomeata added the awaiting-review Waiting for someone to review the PR label Nov 24, 2023
@nomeata nomeata added the needs-update-stage0 stage 0 should be updated after merging this PR label Nov 24, 2023
@github-actions github-actions bot added the toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN label Nov 24, 2023
leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4 that referenced this pull request Nov 24, 2023
@leanprover-community-mathlib4-bot leanprover-community-mathlib4-bot added the builds-mathlib CI has verified that Mathlib builds against this PR label Nov 24, 2023
@leanprover-community-mathlib4-bot
Copy link
Collaborator

leanprover-community-mathlib4-bot commented Nov 24, 2023

  • ✅ Mathlib branch lean-pr-testing-2958 has successfully built against this PR. (2023-11-24 14:20:16) View Log
  • ❗ Mathlib CI will not be attempted unless you rebase your PR onto the 'nightly' branch. (2023-11-29 13:57:15)

@nomeata nomeata added this pull request to the merge queue Dec 2, 2023
Merged via the queue into master with commit 6d23450 Dec 2, 2023
@nomeata nomeata deleted the joachim/wf-hint-elab branch December 2, 2023 11:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting-review Waiting for someone to review the PR builds-mathlib CI has verified that Mathlib builds against this PR needs-update-stage0 stage 0 should be updated after merging this PR toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants