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

feat: support MessageData in #eval #1967

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

gebner
Copy link
Member

@gebner gebner commented Dec 18, 2022

This adds two features to #eval that I've wanted for a long time.

  • Interactive expressions and traces.
  • Doesn't refuse to evaluate terms that have missing Repr/etc. instances. #eval (Except.ok (42, Bar.mk) : Except Foo (Nat × Bar)) now works and prints 42 even if there are no instances for Foo and Bar.

h : p ∧ q
hAux : ¬p
⊢ q ∧ q
[Meta.debug] case inl p q : Prop h : p ∧ q hAux : p ⊢ q ∧ q ------ case inr p q : Prop h : p ∧ q hAux : ¬p ⊢ q ∧ q
Copy link
Member

Choose a reason for hiding this comment

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

Regression?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't really know why this PR changed the output here, but none of the spaces here were hard linebreaks (and everything fits on one line). ppGoal uses Format.line, and trace[foo] "bar\n" also uses Format.line (because the String→MessageData coercion replaces newlines by Format.line).

I've changed ppGoal to use hard newlines.

src/Lean/Eval.lean Outdated Show resolved Hide resolved
@@ -1 +1 @@
Except.ok Lean.Name.anonymous
Except.ok ([anonymous])
Copy link
Member

Choose a reason for hiding this comment

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

Something that's consistently confusing is whether the output of #eval should be valid code via Repr, or if it can be any string representation whatsoever via ToString too. In any case the output here is a bit weird because it is mixing the valid code Except.ok (..) representation with ToString Name. Both the previous version and ok: [anonymous] seem to make more sense. I thought the new scoped instance (priority := low - 10) [ToString α] : Repr α where was causing this but it still appears after removing the instance so I am not sure about the reason.

Copy link
Member Author

Choose a reason for hiding this comment

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

Indeed, it's not completely what the output of #eval should be. It used to take either a ToString or a Repr instance, so you already weren't guaranteed to get a "valid code" output.

The "culprit" here is the new instance:

scoped instance [ToMessageData ε] [ToMessageData α] : ToMessageData (Except ε α)

Which raises another related question: should MessageData be more like Repr or like ToString?

Copy link
Member Author

Choose a reason for hiding this comment

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

As for what it should be: for me the most important part is to get some output, and that expressions are clickable. None of the ToMessageData/Repr/ToString classes have instances for everything, so we'll need to mix them for some time.

Copy link
Member

Choose a reason for hiding this comment

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

Which raises another related question: should MessageData be more like Repr or like ToString?

I'm afraid the answer is that we want both. We use the class for many specific types like Expr and Syntax where it is like ToString, but then there are instance like for List, and Except above, that should definitely be Repr-like (which would also properly handle the parentheses).

Copy link
Member Author

Choose a reason for hiding this comment

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

that should definitely be Repr-like (which would also properly handle the parentheses).

Are you suggesting that toMessageData should have a precedence argument?

Copy link
Member

Choose a reason for hiding this comment

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

I think we would a new, separate typeclass ReprToMessageData (or whatever), which should have a precedence argument, yeah

Copy link
Member

@Kha Kha left a comment

Choose a reason for hiding this comment

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

I very much like the goals of this PR, just not sure yet about the current output

Comment on lines +318 to +325
let inst ← Meta.transform inst fun e => do
if let .app (.const ``EvalInstances.typeInfo [v]) α := e then
let ppE := (← ppExpr α).pretty
return .done (.app (.app (.const ``EvalInstances.TypeInfo.mk [v]) α) (toExpr ppE))
else
return .continue
Copy link
Member

Choose a reason for hiding this comment

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

I think this block deserves a short comment

@@ -1 +1 @@
Except.ok Lean.Name.anonymous
Except.ok ([anonymous])
Copy link
Member

Choose a reason for hiding this comment

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

Which raises another related question: should MessageData be more like Repr or like ToString?

I'm afraid the answer is that we want both. We use the class for many specific types like Expr and Syntax where it is like ToString, but then there are instance like for List, and Except above, that should definitely be Repr-like (which would also properly handle the parentheses).

The goal syntax is indentation-sensitive.  If we leave out the newlines
in goals like `h : p a b : Nat ⊢ a = b` then it's ambiguous what the
free variables are.
@gebner gebner force-pushed the evalmsgdata branch 2 times, most recently from db69327 to 53821cb Compare January 10, 2023 00:05
@gebner gebner added the dev meeting It will be discussed at the (next) dev meeting label Jan 28, 2023
@@ -42,4 +42,4 @@ def sefFn (x_1 : obj) (x_2 : obj) : obj :=
Lean.Expr.mdata._impl →
ret x_1
Lean.Expr.proj._impl →
ret x_1
ret x_1
Copy link
Member

@tydeu tydeu Apr 26, 2023

Choose a reason for hiding this comment

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

Why did this test lose an EOF newline?

@Kha Kha requested a review from leodemoura as a code owner November 20, 2023 08:15
kmill added a commit to kmill/lean4 that referenced this pull request Oct 7, 2024
This function uses soft newlines in many places where hard newlines are more appropriate. Pointed out by @gebner in leanprover#1967.
github-merge-queue bot pushed a commit that referenced this pull request Oct 8, 2024
This function uses soft newlines in many places where hard newlines are
more appropriate. Pointed out by @gebner in #1967.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dev meeting It will be discussed at the (next) dev meeting
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants