Zulip Chat Archive

Stream: mathlib4

Topic: Square root typeclass


Violeta Hernández (Aug 21 2025 at 19:59):

There's a TODO comment above the docs#Real.sqrt notation, suggesting turning it into a typeclass. Has there been any progress on this?

Yakov Pechersky (Aug 21 2025 at 20:05):

src#Real.sqrt

Kevin Buzzard (Aug 21 2025 at 20:31):

What does this comment even mean? Edit: oh, the issue seems to be that the notation should be HasSqrt.sqrt so it works for NNReal etc.

Violeta Hernández (Aug 21 2025 at 20:32):

I imagine it means one of two things

  • Make a typeclass for the notation
  • Make a typeclass for types with a "well-behaved" square root operation

Violeta Hernández (Aug 21 2025 at 20:33):

I'm not sure what the second one would entail (and I don't really find it feasible), but the first one seems very uncontroversial

Niels Voss (Aug 21 2025 at 21:03):

Do we want to turn Nat.sqrt into a typeclass instance? I personally don't think that's a good idea, but I want to hear what other people's perspectives are

Violeta Hernández (Aug 21 2025 at 21:06):

I don't really see why not. It might look a bit weird to write stuff like √5 = 2 but I'd argue this is no worse than what Nat substraction and division already allow.

Niels Voss (Aug 21 2025 at 21:11):

I guess that's fair. I'm just used to having sqrt automatically coerce Nat to Real

Violeta Hernández (Aug 21 2025 at 21:12):

Hmmm yeah, that's actually a really good point against this idea

Violeta Hernández (Feb 02 2026 at 17:30):

I recently defined a square root on a new type (CGT#335) and came back to this. Surely it wouldn't be controversial to add this code somewhere?

/--
The notation typeclass for square roots.
This enables the notation `√a : α` where `a : α`.
-/
class Sqrt (α : Type u) where
  /-- `√a` computes the square root of `a`.
  The meaning of this notation is type-dependent. -/
  sqrt : α  α

@[inherit_doc] prefix:max "√" => Sqrt.sqrt

macro_rules | `( $x) => `(unop% Sqrt.sqrt $x)

Violeta Hernández (Feb 02 2026 at 17:30):

(The question of course is, does this go in core, or in some basic file in Mathlib?)

Weiyi Wang (Feb 02 2026 at 17:33):

It feels more Mathlib to me

Violeta Hernández (Feb 02 2026 at 17:38):

What would be a good file for it?

Violeta Hernández (Feb 02 2026 at 17:38):

I don't want to put it in the same file as docs#Real.sqrt because that would dramatically raise the imports to use this notation

Yury G. Kudryashov (Feb 02 2026 at 17:43):

I think that I wrote this comment some time ago, but some of use cases I thought about (e.g., Nat.sqrt) were later dismissed, because it isn't clear whether the floor one or the ceiling one should be given by the notation.

Yury G. Kudryashov (Feb 02 2026 at 17:44):

Also, note that a notation class makes all expressions heavier. If we aren't going to have any generic theory, then I'm not sure that we should have a notation class.

Yury G. Kudryashov (Feb 02 2026 at 17:44):

Another option is to have an overloaded notation.

Violeta Hernández (Feb 02 2026 at 17:46):

Truthfully I just wanted to get the elaborator for free. If there's an actual cost to introducing this notation typeclass then we probably shouldn't.

Yury G. Kudryashov (Feb 02 2026 at 17:48):

It turns every Real.sqrt into @Sqrt.sqrt Real instRealSqrt, same for other use cases.

Jireh Loreaux (Feb 02 2026 at 17:49):

If it were a notation typeclass we could use it for docs#CFC.sqrt

Violeta Hernández (Feb 02 2026 at 17:49):

We could also use it for docs#Ideal.radical, though I'm not sure we would want to.

Violeta Hernández (Feb 02 2026 at 17:49):

And of course there's my motivating example CGT#Nimber.sqrt (not yet merged)

Yury G. Kudryashov (Feb 02 2026 at 18:49):

Will an overloaded notation work for all these cases?

Violeta Hernández (Feb 02 2026 at 18:50):

I think it makes sense that we'd like to talk about CFC.sqrt and Real.sqrt simultaneously. For the other examples I don't believe there's any possible confusion.

Yury G. Kudryashov (Feb 02 2026 at 18:51):

Ah yes, is there an instance of whatever is needed for CFC.sqrt for Real?

Yury G. Kudryashov (Feb 02 2026 at 18:52):

If yes, then adding a notation typeclass will lead to a non-defeq diamond for Real, and we don't want that.

Jireh Loreaux (Feb 03 2026 at 01:43):

Yury, the answer is currently no, but it will be yes when we eventually get real C*-algebras. I'll note though that we already have a non-defeq diamond somewhere for real powers.

Yury G. Kudryashov (Feb 03 2026 at 02:18):

Where?

Yury G. Kudryashov (Feb 03 2026 at 02:20):

Do you mean docs#CFC.instPowReal ?

Yury G. Kudryashov (Feb 03 2026 at 02:21):

(assuming that Real satisfies assumptions on A)

Jireh Loreaux (Feb 03 2026 at 04:09):

Ah, maybe we don't have a diamond yet. I thought Mathlib had a Pow Complex Real instance, but apparently not. But yes, the point is that when we have real C*-algebras, Real will be one.

Yury G. Kudryashov (Feb 03 2026 at 05:04):

Is there a reasonable typeclass that can use forgetful inheritance here?


Last updated: Feb 28 2026 at 14:05 UTC