Zulip Chat Archive

Stream: lean4

Topic: inferType in delaborator


Alexander Bentkamp (Oct 19 2021 at 16:13):

I am trying to use inferType in a delaborator. This causes Lean to crash when I type constants that are undefined, such as test below:

import Lean

namespace Lean.PrettyPrinter.Delaborator
open SubExpr
open Meta

@[delab app]
partial def delabMinimization : Delab := do
  let ty  inferType $  getExpr
  failure

end Lean.PrettyPrinter.Delaborator

set_option pp.rawOnError true

#check test
/-
[Error - 6:06:45 PM] Request textDocument/hover failed.
  Message: unknown constant '[Error pretty printing expression: unknown constant 'test'. Falling back to raw printer.]
test'
  Code: -32603
-/

Do I first need to test whether ← getExpr is actually a valid expression? How can I do that?

Mac (Oct 19 2021 at 16:15):

(deleted)

Sebastian Ullrich (Oct 19 2021 at 16:15):

That is definitely not expected (and the error message is hilarious).

Daniel Selsam (Oct 21 2021 at 00:25):

@Sebastian Ullrich Maybe just remove the mkConst from https://github.com/leanprover/lean4/blob/master/src/Lean/Elab/Term.lean#L1504? Since we know the constant does not exist, I don't see a point in having the message view it as an expression rather than as a name.

Leonardo de Moura (Oct 21 2021 at 01:02):

The mkConst is there to make sure the name is not treated as a regular Name object, but as an Expr.
The delaborator should tolerate type incorrect terms.

Alexander Bentkamp (Oct 21 2021 at 06:56):

So how can I check if a term is not type correct?

Daniel Selsam (Oct 21 2021 at 19:57):

Alexander Bentkamp said:

So how can I check if a term is not type correct?

Delaborators should not assume that inferType will succeed in general. In your example, you could just wrap the inferType with try/catch. It is perhaps surprising though that the delaborator outer-loop will not recover from the exception -- it might make sense to put a try/catch here as well: https://github.com/leanprover/lean4/blob/master/src/Lean/PrettyPrinter/Delaborator/Basic.lean#L226

Alexander Bentkamp (Oct 22 2021 at 10:14):

Ok, thanks!


Last updated: Dec 20 2023 at 11:08 UTC