Zulip Chat Archive

Stream: lean4

Topic: The expected type of elabTerm


Joachim Breitner (Sep 28 2023 at 21:05):

I am surprised that this works:

import Mathlib.Tactic.RunCmd
open Lean Elab Term Command Meta

run_cmd liftTermElabM do
    let t  elabTerm ( `(term|true)) (some (.sort ( mkFreshLevelMVar)))
    IO.println t

I was expecting by saying that I expect something of type Sort _, it will reject to elaborate true (as true : Bool). Is that now how the expectedType parameter works? The docstring didn’t say.

Joachim Breitner (Sep 28 2023 at 21:07):

Ah, there exists elabTermEnsuringType

Kyle Miller (Sep 28 2023 at 21:08):

Yeah, the expected type is a hint for what type you want from the elaborated term, but you need something like docs#Lean.Elab.Term.elabTermEnsuringType to elaborate with that hint and then try to insert a coercion if there's a type mismatch.

Kyle Miller (Sep 28 2023 at 21:11):

This function does elabTerm followed by docs#Lean.Elab.Term.ensureHasType

Joachim Breitner (Sep 28 2023 at 21:14):

Thanks! Reading through the code gave me a glimpse. Maybe I’ll PR a slight docstring addition, pointing to elabTermEnsuringType.

Kyle Miller (Sep 28 2023 at 21:22):

Watch out that there's also docs#Lean.Elab.Tactic.elabTermEnsuringType, which doesn't insert a coercion. (It also does Term.synthesizeSyntheticMVars before unifying the type of the term with the expected type, which seems like it might be a bug -- I think this accounts for many problems with refine and its failure to synthesize instances.)

Joachim Breitner (Sep 28 2023 at 21:24):

Hmm, not sure if I at this point can PR a better docstring than “It’s complicated, read the code” :-D


Last updated: Dec 20 2023 at 11:08 UTC