Zulip Chat Archive

Stream: new members

Topic: coercion from nnreal to ennreal


Jason KY. (Aug 10 2021 at 08:41):

Hi! I'm trying to coerce a nonnegative real to a ennreal using a coercion

import data.real.ennreal

open_locale nnreal ennreal

variables (a : 0) (b : ) (hb : 0  b)

#check (a : 0) -- works

#check (⟨b, hb : 0) -- works

#check ((⟨b, hb : 0) : 0)
/- invalid type ascription, term has type
  {r // 0 ≤ r}
but is expected to have type
  ℝ≥0∞
-/

I have no clue why the third #check doesn't work

Alex J. Best (Aug 10 2021 at 09:47):

If you put two type ascriptions in a row with nothing else lean sometimes just ignores the first one, so its trying to interpret ⟨b, hb⟩ as a ℝ≥0∞, you can force lean to do what you intend by adding a docs#id as in

#check ((id b, hb : 0) : 0)

but what the actual fix is depends on what you are trying to do.

Jason KY. (Aug 10 2021 at 09:58):

I would like to address the comment from here: https://github.com/leanprover-community/mathlib/pull/8597#discussion_r685779359 though I don't know if its worth using id just to force a coercion instead of using some since they are all defeq

Alex J. Best (Aug 10 2021 at 10:31):

Looks a bit funny but making the coe explicitly works, and looks like it gives the right definition

import data.real.ennreal

open_locale nnreal ennreal

variables (a : 0) (b : ) (hb : 0  b)

#check (a : 0) -- works

#check (⟨b, hb : 0) -- works

def t : ennreal := @coe nnreal ennreal _ b, hb
#print t
set_option pp.all true
#print t

Yury G. Kudryashov (Aug 11 2021 at 18:20):

When you write ((⟨b, hb⟩ : ℝ≥0) : ℝ≥0∞), Lean verifies that ⟨b, hb⟩ has type defeq to nnreal but continues to treat it as {r // 0 ≤ r}. There are a few workarounds: (1) add id; (2) use @coe nnreal ennreal so that Lean expects an nnreal when it parses ⟨b, hb⟩; (3) use show nnreal, from ⟨b, hb⟩.

Kevin Buzzard (Aug 11 2021 at 22:54):

(3) and (1) are the same IIRC

Eric Wieser (Aug 11 2021 at 22:55):

2 is best in lemma statements


Last updated: Dec 20 2023 at 11:08 UTC