Skip to content

fix: binop% info tree - #1903

Merged
leodemoura merged 2 commits into
leanprover:masterfrom
Kha:binop-macros
Jan 16, 2023
Merged

leodemoura merged 2 commits into
leanprover:masterfrom
Kha:binop-macros

Conversation

@Kha

@Kha Kha commented Dec 1, 2022

Copy link
Copy Markdown
Member

@rish987 noticed that the binop% elaborator discarded MacroExpansionInfo nodes, which is a problem for #1767. This change moves the macro info into its own Op.Tree constructor, cleaning up the code and also lifting the arbitrary limitation on at most one macro expansion between operators.

@Kha
Kha requested a review from leodemoura December 1, 2022 10:00
@Kha

Kha commented Dec 1, 2022

Copy link
Copy Markdown
Member Author

test output diff:

--- exp	2022-12-01 13:46:03.598509424 +0100
+++ ./binopInfoTree.lean.expected.out	2022-12-01 13:46:28.786301599 +0100
@@ -8,64 +8,71 @@
           1 + 2 +
             3
               -- should propagate through multiple macro expansions
           ===>
           binop% HAdd.hAdd✝
             (1 + 2)3
               -- should propagate through multiple macro expansions
           • 1 + 2 + 3 : Nat @ ⟨5, 7⟩†-⟨5, 16⟩† @ Lean.Elab.Term.Op.elabBinOp
             • 1 + 2 + 3 : Nat @ ⟨5, 7⟩†-⟨5, 16⟩†
               • 1 + 2 : Nat @ ⟨5, 7⟩-⟨5, 12⟩ @ «_aux_Init_Notation___macroRules_term_+__2»
-                • [.] `[email protected]._hyg.176 : none @ ⟨5, 7⟩†-⟨5, 16⟩†
-                • [.] `[email protected]._hyg.178 : none @ ⟨5, 7⟩†-⟨5, 16⟩†
-                • 1 : Nat @ ⟨5, 7⟩-⟨5, 8⟩ @ Lean.Elab.Term.elabNumLit
-                • 2 : Nat @ ⟨5, 11⟩-⟨5, 12⟩ @ Lean.Elab.Term.elabNumLit
+                • Macro expansion
+                  1 + 2
+                  ===>
+                  binop% HAdd.hAdd✝ 1 2
+                  • 1 + 2 : Nat @ ⟨5, 7⟩†-⟨5, 12⟩†
+                    • [.] `[email protected]._hyg.176 : none @ ⟨5, 7⟩†-⟨5, 16⟩†
+                    • [.] `[email protected]._hyg.178 : none @ ⟨5, 7⟩†-⟨5, 12⟩†
+                    • 1 : Nat @ ⟨5, 7⟩-⟨5, 8⟩ @ Lean.Elab.Term.elabNumLit
+                    • 2 : Nat @ ⟨5, 11⟩-⟨5, 12⟩ @ Lean.Elab.Term.elabNumLit
               • 3 : Nat @ ⟨5, 15⟩-⟨5, 16⟩ @ Lean.Elab.Term.elabNumLit
-fun n m l => Int.ofNat n + Int.ofNat (m + l) : Nat → Nat → Nat → Int
+fun n m l => Int.ofNat n + (Int.ofNat m + Int.ofNat l) : Nat → Nat → Nat → Int
 [Elab.info] • command @ ⟨7, 0⟩-⟨7, 48⟩ @ Lean.Elab.Command.elabCheck
-      • fun n m l => Int.ofNat n + Int.ofNat (m + l) : Nat → Nat → Nat → Int @ ⟨7, 7⟩-⟨7, 48⟩ @ Lean.Elab.Term.elabFun
+      • fun n m l =>
+          Int.ofNat n + (Int.ofNat m + Int.ofNat l) : Nat → Nat → Nat → Int @ ⟨7, 7⟩-⟨7, 48⟩ @ Lean.Elab.Term.elabFun
         • Nat : Type @ ⟨7, 20⟩-⟨7, 23⟩ @ Lean.Elab.Term.elabIdent
           • [.] `Nat : some Sort.{?_uniq} @ ⟨7, 20⟩-⟨7, 23⟩
           • Nat : Type @ ⟨7, 20⟩-⟨7, 23⟩
         • n (isBinder := true) : Nat @ ⟨7, 12⟩-⟨7, 13⟩
         • Nat : Type @ ⟨7, 20⟩-⟨7, 23⟩ @ Lean.Elab.Term.elabIdent
           • [.] `Nat : some Sort.{?_uniq} @ ⟨7, 20⟩-⟨7, 23⟩
           • Nat : Type @ ⟨7, 20⟩-⟨7, 23⟩
         • m (isBinder := true) : Nat @ ⟨7, 14⟩-⟨7, 15⟩
         • Nat : Type @ ⟨7, 20⟩-⟨7, 23⟩ @ Lean.Elab.Term.elabIdent
           • [.] `Nat : some Sort.{?_uniq} @ ⟨7, 20⟩-⟨7, 23⟩
           • Nat : Type @ ⟨7, 20⟩-⟨7, 23⟩
         • l (isBinder := true) : Nat @ ⟨7, 16⟩-⟨7, 17⟩
-        • Int.ofNat n + Int.ofNat (m + l) : Int @ ⟨7, 28⟩-⟨7, 48⟩ @ Lean.Elab.Term.elabTypeAscription
+        • Int.ofNat n + (Int.ofNat m + Int.ofNat l) : Int @ ⟨7, 28⟩-⟨7, 48⟩ @ Lean.Elab.Term.elabTypeAscription
           • Int : Type @ ⟨7, 44⟩-⟨7, 47⟩ @ Lean.Elab.Term.elabIdent
             • [.] `Int : some Sort.{?_uniq} @ ⟨7, 44⟩-⟨7, 47⟩
             • Int : Type @ ⟨7, 44⟩-⟨7, 47⟩
-          • Int.ofNat n + Int.ofNat (m + l) : Int @ ⟨7, 29⟩-⟨7, 41⟩ @ «_aux_Init_Notation___macroRules_term_+__2»
+          • Int.ofNat n +
+              (Int.ofNat m + Int.ofNat l) : Int @ ⟨7, 29⟩-⟨7, 41⟩ @ «_aux_Init_Notation___macroRules_term_+__2»
             • Macro expansion
               n + (m +' l)
               ===>
               binop% HAdd.hAdd✝ n(m +' l)
-              • Int.ofNat n + Int.ofNat (m + l) : Int @ ⟨7, 29⟩†-⟨7, 41⟩† @ Lean.Elab.Term.Op.elabBinOp
-                • Int.ofNat n + Int.ofNat (m + l) : Int @ ⟨7, 29⟩†-⟨7, 41⟩†
+              • Int.ofNat n + (Int.ofNat m + Int.ofNat l) : Int @ ⟨7, 29⟩†-⟨7, 41⟩† @ Lean.Elab.Term.Op.elabBinOp
+                • Int.ofNat n + (Int.ofNat m + Int.ofNat l) : Int @ ⟨7, 29⟩†-⟨7, 41⟩†
                   • [.] `[email protected]._hyg.192 : none @ ⟨7, 29⟩†-⟨7, 41⟩†
                   • n : Nat @ ⟨7, 29⟩-⟨7, 30⟩ @ Lean.Elab.Term.elabIdent
                     • [.] `n : none @ ⟨7, 29⟩-⟨7, 30⟩
                     • n : Nat @ ⟨7, 29⟩-⟨7, 30⟩
-                  • m + l : Nat @ ⟨7, 34⟩-⟨7, 40⟩ @ «_aux_binopInfoTree___macroRules_term_+'__1»
+                  • Int.ofNat m + Int.ofNat l : Int @ ⟨7, 34⟩-⟨7, 40⟩ @ «_aux_binopInfoTree___macroRules_term_+'__1»
                     • Macro expansion
                       m +' l
                       ===>
                       m + l
-                      • m + l : Nat @ ⟨7, 34⟩†-⟨7, 40⟩† @ «_aux_Init_Notation___macroRules_term_+__2»
+                      • Int.ofNat m +
+                          Int.ofNat l : Int @ ⟨7, 34⟩†-⟨7, 40⟩† @ «_aux_Init_Notation___macroRules_term_+__2»
                         • Macro expansion
                           m + l
                           ===>
                           binop% HAdd.hAdd✝ m l
-                          • m + l : Nat @ ⟨7, 34⟩†-⟨7, 40⟩† @ Lean.Elab.Term.Op.elabBinOp
-                            • m + l : Nat @ ⟨7, 34⟩†-⟨7, 40⟩†
-                              • [.] `[email protected]._hyg.199 : none @ ⟨7, 34⟩†-⟨7, 40⟩†
-                              • m : Nat @ ⟨7, 34⟩-⟨7, 35⟩ @ Lean.Elab.Term.elabIdent
-                                • [.] `m : none @ ⟨7, 34⟩-⟨7, 35⟩
-                                • m : Nat @ ⟨7, 34⟩-⟨7, 35⟩
-                              • l : Nat @ ⟨7, 39⟩-⟨7, 40⟩ @ Lean.Elab.Term.elabIdent
-                                • [.] `l : none @ ⟨7, 39⟩-⟨7, 40⟩
-                                • l : Nat @ ⟨7, 39⟩-⟨7, 40⟩
+                          • Int.ofNat m + Int.ofNat l : Int @ ⟨7, 34⟩†-⟨7, 40⟩†
+                            • [.] `[email protected]._hyg.196 : none @ ⟨7, 34⟩†-⟨7, 40⟩†
+                            • m : Nat @ ⟨7, 34⟩-⟨7, 35⟩ @ Lean.Elab.Term.elabIdent
+                              • [.] `m : none @ ⟨7, 34⟩-⟨7, 35⟩
+                              • m : Nat @ ⟨7, 34⟩-⟨7, 35⟩
+                            • l : Nat @ ⟨7, 39⟩-⟨7, 40⟩ @ Lean.Elab.Term.elabIdent
+                              • [.] `l : none @ ⟨7, 39⟩-⟨7, 40⟩
+                              • l : Nat @ ⟨7, 39⟩-⟨7, 40⟩

@Kha
Kha force-pushed the binop-macros branch 2 times, most recently from 9b21b56 to 695c431 Compare December 1, 2022 12:55
Comment on lines +23 to +24
• [.] `HAdd.hAdd[email protected]._hyg.176 : none @ ⟨5, 7⟩†-⟨5, 16⟩†
• [.] `HAdd.hAdd[email protected]._hyg.178 : none @ ⟨5, 7⟩†-⟨5, 12⟩†
• [.] HAdd.hAdd✝ : none @ ⟨5, 7⟩†-⟨5, 16⟩†
• [.] HAdd.hAdd✝ : none @ ⟨5, 7⟩†-⟨5, 12⟩†

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@leodemoura This should make sure committed Elab.info output does not break from unrelated changes

@leodemoura leodemoura added the awaiting-review Waiting for someone to review the PR label Dec 1, 2022
Comment thread src/Lean/Elab/Extra.lean
match s' with
| `(binop% $f $lhs $rhs) => processBinOp (lazy := false) s macroName f lhs rhs
| `(binop_lazy% $f $lhs $rhs) => processBinOp (lazy := true) s macroName f lhs rhs
| `(unop% $f $arg) => processUnOp s .anonymous f arg

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Just wanted to make sure, this should have been macroName instead of .anonymous right (origin e6caee9)? Not sure if there was a particular reason that we wanted to erase elaborator information in the case of a macro expanding to an unop%.

@rish987 rish987 Dec 4, 2022 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Oh, yeah this was probably just a typo considering the line 168 above, NVM!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah, I guess so too. I didn't even notice.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants