Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Range module (#7128) #7305

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

LionelMeli
Copy link
Contributor

No description provided.

@magnus-madsen
Copy link
Member

Hi @LionelMeli

Thanks for your contribution. We are very busy with some big merges, but I promise we will get around to reviewing it.

@magnus-madsen
Copy link
Member

Perhaps @stephentetley could take a look, since he has experience with Haskell? Otherwise perhaps @mlutze or @JonathanStarup

@stephentetley
Copy link
Contributor

I can take a look as I'm on holiday today.

One thing I noticed first I'd change order of the BoundType constructors so Exclusive < Inclusive holds as Inclusive will make a larger range.

@LionelMeli
Copy link
Contributor Author

Hi all
No urgency here!
Feel free to review after the high level priority issues.

@stephentetley
Copy link
Contributor

stephentetley commented Feb 28, 2024

Hi Lionel - here are my thoughts...

There are (at least) two range libraries on Hackage - I looked at the other one first - so I'd change the comment to acknowledge the particular one [1] and probably its author Robert Massaioli.

I'm not so keen on the operators - initially I thought they were versions of the vector-space operators until I looked closely to see what they were doing. Once associated types are production ready in Flix it would be nice to have the vector-space / affine-space operators [2] in the standard library as they are very general, so I think we should wait for them to pick operator symbols.

I'd propose the following named functions as replacements:


+=+ => inclFromTo
+=* => inclFromExclTo
*=+ => exclFromInclTo
*=* => exclFromTo

I didn't see the infinity shorthand constructors in Flix that are in the Haskell library so these could be added if you want to (I'm not sure about the last two):


lbi => inclToInf
lbe => exclToInf

ubi => infToIncl ??
ube => infToExcl ??

It would be nice if zeroTo etc. were polymorphic in the number type but that could be done later as an improvement.

[1] https://hackage.haskell.org/package/range
[2] https://hackage.haskell.org/package/vector-space

@LionelMeli
Copy link
Contributor Author

LionelMeli commented Feb 28, 2024

Hi Stephen,

I'm not implemented the lbi, ube etc operators because it's my next step... (Sorry I didn't explain in the PR).
For the moment I just delivered (and focused) about this set of features. I take care about your comments.
By the way I plan to extend this module with other operators you mentioned and also a set of functions like aboveRange etc. (next step)

I don't know the library https://hackage.haskell.org/package/vector-space (At a glance maybe a little bit difficult for me as I'm not fuent in Haskell and Functional Programming!). No problem, It'll be just an another next step!

PS: What do you think about this kind of naming (from: https://github.com/google/guava/wiki/RangesExplained) ?

Range type Method
(a..b) open(C, C)
[a..b] closed(C, C)
[a..b) closedOpen(C, C)
(a..b] openClosed(C, C)
(a..+∞) greaterThan(C)
[a..+∞) atLeast(C)
(-∞..b) lessThan(C)
(-∞..b] atMost(C)
(-∞..+∞) all()

@stephentetley
Copy link
Contributor

Thanks Lionel

Vector Space is quite different to Range - but it adds complementary "maths" operators to the common ones that we already have in the standard library, it would be nice if there are operators available for it (if we want the classes from it in the standard library of course).

I think your proposed function names are very good - definitely better than mine. The Flix stdlib doesn't generally have abbreviated names so avoiding then is a winner, I only chose to use abbreviations to avoid long names as I was suggesting them as alternatives to operators.

@LionelMeli
Copy link
Contributor Author

Thanks for your answers.

@magnus-madsen
Copy link
Member

@LionelMeli Why did you close this? Are we not iterating towards some implementation? :)

@LionelMeli
Copy link
Contributor Author

Hello Magnus,

the only reason is: I did wrong git commands... I reopen this one once I push the code again! Sorry for this.

@magnus-madsen
Copy link
Member

Okay, no problem!

@magnus-madsen
Copy link
Member

@LionelMeli @stephentetley Hi guys. Where are we with this?

I would like to have a Data.Range which can be used to iterate through arrays and vector indices.

In particular, without knowing the specifics, it would be nice if we can have a function:

mod Array {
  pub def indices(a: Array[t, r]): Range[Int32]
}

or something like this where Range is then iterable so we can use it in a foreach loop.

Copy link
Member

@magnus-madsen magnus-madsen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the code (briefly)

  • The definitions of BoundType and Bound looks sensible, but they should probably be in their own files and modules.
  • The definition of Range looks reasonable, if perhaps a bit over-engineered?
  • What is this IteratorFactory? (I once promised myself that Flix would not have "factories" :))
  • All public functions, e.g. open, closed, etc. should have docs.
  • I would want to implement Iterable directly on Range, but it is unclear if I can do so?
  • Perhaps we should leave out to fancy functor and map stuff. (It can come later in a subsequent PR).
  • Enumerable looks like its own abstraction. Perhaps we should start by adding that in its own PR?

@magnus-madsen
Copy link
Member

Addition: It would best if iteration through the array indices does not require allocation.

@magnus-madsen
Copy link
Member

I was looking at:

https://typelevel.org/cats-collections/range.html

and

https://doc.rust-lang.org/std/ops/struct.Range.html

which seems to have a simpler data type.

What's the upshot of the more complex Haskell one? That is clearer?

@LionelMeli
Copy link
Contributor Author

Hi @magnus-madsen
Thanks for your review.
Let's see in details your remarks in next weeks.

@LionelMeli
Copy link
Contributor Author

LionelMeli commented May 9, 2024

Hi @magnus-madsen

Looking at the code (briefly)

  • The definitions of BoundType and Bound looks sensible, but they should probably be in their own files and modules.
    Ok (however if we choose basic Range type we don't have these types anymore)
  • The definition of Range looks reasonable, if perhaps a bit over-engineered?
    You are right. I heavily follow the Data.Range from Haskell as it little bit more complicated compare to https://typelevel.org/cats-collections/range.html. I've a question: Do you only prefer inclusive Range (i.e. [a,b]) or a mix inclusive and exclusive bounds (e.g. [a,b[ or ]a,b[ ) ?
  • What is this IteratorFactory? (I once promised myself that Flix would not have "factories" :))
    The IteratorFactory is due to in Flix 0.45.0 we don't have "Associated Type" in Traits. As I know @stephentetley added (cf. refactor: add assoc type and effect to Iterable (part of issue #7433) #7615 ) this feature. So I can remove this one (I'll work with the night build as the Flix 0.46.0 not include yet this Stephen's refactoring)
  • All public functions, e.g. open, closed, etc. should have docs.
    Ok
  • I would want to implement Iterable directly on Range, but it is unclear if I can do so?
    Yes, I think we can now thanks to "Associated Type"
  • Perhaps we should leave out to fancy functor and map stuff. (It can come later in a subsequent PR).
    Sure
  • Enumerable looks like its own abstraction. Perhaps we should start by adding that in its own PR?
    Sure (but as I refactor the code - and simplified this one - Enumerable should be vanished...)

@LionelMeli
Copy link
Contributor Author

LionelMeli commented May 18, 2024

Reopened with "Right" commits!

@LionelMeli LionelMeli reopened this May 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants