Zulip Chat Archive

Stream: new members

Topic: Can't calc with expectations


Duncan Skahan (Oct 27 2024 at 20:22):

Why does

import Mathlib

noncomputable section

open MeasureTheory ProbabilityTheory

variable {Ω: Type*} [: MeasurableSpace Ω]
variable {P: Measure Ω}

theorem zero_mean {X : Ω  } (mX: Measurable X) :
  P[X] = 0 := by
  calc
    P[X] = 0 := by sorry

give
invalid 'calc' step, failed to synthesize `Trans` instance Trans Eq Eq ?m.2997 Additional diagnostic information may be available using the `set_option diagnostics true` command.?

Ruben Van de Velde (Oct 27 2024 at 20:24):

You should not assume single line calc blocks will work, but (0 : ℝ) might get you closer

Duncan Skahan (Oct 27 2024 at 20:26):

That does fix it. Do you know why it is needed?

Duncan Skahan (Oct 27 2024 at 20:31):

Also how can I make this work when both sides of the equation include expectations. For example:

import Mathlib

noncomputable section

open MeasureTheory ProbabilityTheory

variable {Ω: Type*} [: MeasurableSpace Ω]
variable {P: Measure Ω}

theorem rfl_mean {X : Ω  } :
  P[X] = P[X] + 0 := by
  calc
    P[X] = P[X] + 0 := by simp

gives the same error.

Ruben Van de Velde (Oct 27 2024 at 20:35):

Oh, I think there's something off with the P[X] notation, this works:

import Mathlib

noncomputable section

open MeasureTheory ProbabilityTheory

variable {Ω: Type*} [: MeasurableSpace Ω]
variable {P: Measure Ω}

theorem rfl_mean {X : Ω  } :
    P[X] = P[X] + 0 := by
  calc
     (x : Ω), X x P =  (x : Ω), X x P + 0 := by simp

Duncan Skahan (Oct 27 2024 at 20:38):

Thanks. Is this a bug or just a limitation of how notation can be defined in Lean?

Yaël Dillies (Oct 27 2024 at 20:38):

Looks like a bug to me. The notation seems to have the wrong precedence EDIT: Nope

Duncan Skahan (Oct 27 2024 at 20:39):

Though putting P[X] in brackets doesn't fix it.

Yaël Dillies (Oct 27 2024 at 20:52):

It looks like it's some kind of wrong elaboration order: This fixes it:

import Mathlib

open scoped ProbabilityTheory

variable {Ω : Type*} [ : MeasurableSpace Ω] {P : MeasureTheory.Measure Ω}

theorem rfl_mean {X : Ω  } :
  P[X] = P[X] + 0 := by
  calc
    P[X] = P[X] + (0 : ) := by simp

Yaël Dillies (Oct 27 2024 at 20:54):

The 0 is being interpreted as a Nat and is pushing for X to be Nat-valued. Instead we would expect that X being Real-valued should push the 0 to be interpreted as a Real.

Ruben Van de Velde (Oct 27 2024 at 21:01):

Btw, putting exact before calc gives other error messages


Last updated: May 02 2025 at 03:31 UTC