leanprover / lean4

Lean 4 programming language and theorem prover
https://lean-lang.org
Apache License 2.0
4.63k stars 414 forks source link

[RFC] Revise `Std.Range` #1962

Open Kha opened 1 year ago

Kha commented 1 year ago

The current Std.Range

structure Range where
  start : Nat := 0
  stop  : Nat
  step  : Nat := 1

in my experience has quite a few issues:

My proposal is to build on top of a simple .. infix syntax and include all the variations present in mathlib:

This could be implemented either as separate types, or as a single type taking two bounds that individually encode one of the three above choices.

Note that while this is partially inspired by Rust's a..b, that syntax is in fact a right-exclusive interval! But that seems like an extremely poor fit for potential openness in both directions, and it is not clear to me whether Rust would have made that choice if they had known at the time that they would introduce a..=b inclusive ranges at a much later point.

JamesGallicchio commented 1 year ago

Syntax bikeshedding: I'm a bit confused why the syntax for exclusive lower bound uses >. I expected a<..b :thinking: is there a good reason to use > there?

Otherwise I would be very happy to see this implemented! :smile:

Kha commented 1 year ago

Err... you are totally correct, my mind simply assumed it had to be mirrored!

Edit: edited

tydeu commented 1 year ago

Note that while this is partially inspired by Rust's a..b, that syntax is in fact a right-exclusive interval! But that seems like an extremely poor fit for potential openness in both direction...

If we need some more justification for the approach of this RFC over Rust's, the a..b syntax is also used for inclusive ranges in both Ruby and Perl.

sullyj3 commented 8 months ago

It is unnecessarily limited to Nat. It should work for any Type u, with operations using appropriate type classes.

As a data point, I ran into a situation today where a range of Fin would be really nice.

j-loreaux commented 2 weeks ago

Just to note: Mathlib does use the syntax a..b for something already: intervalIntegral

That's not to say it should necessarily prevent using it for this. I'm only noting the conflict.

urkud commented 2 weeks ago

In case of interalIntegral, the .. notation causes issues when I try to use it as ∫ x in 0..1, x, because it parses 0. as a Float before recognizing ... I have to use ∫ x in (0)..1, x instead. Probably, this can be fixed by changing some priorities, but this has to be done, if you're going to change notation.