Skip to content

Preserve class when subsetting a list#4242

Merged
d-torrance merged 3 commits into
Macaulay2:developmentfrom
d-torrance:splice
May 8, 2026
Merged

Preserve class when subsetting a list#4242
d-torrance merged 3 commits into
Macaulay2:developmentfrom
d-torrance:splice

Conversation

@d-torrance
Copy link
Copy Markdown
Member

@d-torrance d-torrance commented May 1, 2026

Closes: #3812

Before

i1 : [a,b,c,d]_{1,3}

o1 = {b, d}

o1 : List

After

i1 : [a,b,c,d]_{1,3}

o1 = [b, d]

o1 : Array

@d-torrance
Copy link
Copy Markdown
Member Author

It's so weird that the cmake-ubuntu builds keep failing fetching the PPA information but the autotools-ubuntu ones are fine...

The only reason we need the PPA for the builds is msolve -- its version in Ubuntu 24.04 is too old. The next LTS, Ubuntu 26.04, was just released a few days ago and I imagine GitHub will add runners soon. Then we can drop the PPA from the builds.

Comment thread M2/Macaulay2/m2/lists.m2 Outdated
Comment on lines +36 to +38
VisibleList _ List := VisibleList => (x,y) -> (
L := class x;
new L from apply(splice y, i -> x#i))
Copy link
Copy Markdown
Member

@mahrud mahrud May 1, 2026

Choose a reason for hiding this comment

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

Why not this?

Suggested change
VisibleList _ List := VisibleList => (x,y) -> (
L := class x;
new L from apply(splice y, i -> x#i))
VisibleList _ List := VisibleList => (L, inds) -> new class L from apply(splice inds, i -> L#i)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Also, often I've wished L_(1..3) would also work, so I don't have to do L_(toList seq). I don't think this conflicts with any other notation ....
It might even be useful if X_Y produced an object with the type of Y instead.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I split it up over a few lines so it wouldn't be too wide.

Defining it as VisibleList_VisibleList would make sense so we can use sequences. I feel like it should have the same class as the first argument.

Copy link
Copy Markdown
Member

@mahrud mahrud May 1, 2026

Choose a reason for hiding this comment

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

Even with splitting, new class L from apply(splice inds, i -> L#i)) is short, but I'm being too nitpicky :) (part of why I mentioned is I don't like just using "x" or "y" as variables.

@mahrud
Copy link
Copy Markdown
Member

mahrud commented May 1, 2026

Very unrelated to this PR, but would be nice to have toArray also. new Array from blah is too long XD

@d-torrance d-torrance force-pushed the splice branch 2 times, most recently from a5343ac to f7df29d Compare May 2, 2026 12:57
}
document {
Key => (symbol _, VisibleList, List),
Key => (symbol _, VisibleList, VisibleList),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Since the builds failed again and needs fixing still, I'll point out that I think I prefer limiting to just List and Sequence for indices, not arbitrary visible lists. e.g. perhaps L_[...] or L_<|...|> could have other uses in the future.

@d-torrance
Copy link
Copy Markdown
Member Author

d-torrance commented May 7, 2026

Discussed with Mike and Anton. Will remove the == change but keep the subsetting change.

Later: == w/ different types on each side should be an error, even w/ two d-level List objects of different classes (or something else - open to discussion)

@mahrud
Copy link
Copy Markdown
Member

mahrud commented May 7, 2026

Oh I thought you were just fixing the error to be more sensible. You should still do this I think, no?

@d-torrance
Copy link
Copy Markdown
Member Author

Yeah, definitely. Right now we have:

i1 : {1, 2, 3} == (1, 2, 3)
stdio:1:10:(3):[1]: error: expected argument 2 to be a visible list

i2 : {1, 2, 3} == [1, 2, 3]

o2 = false

Either they should both be errors or both false. The current state of this PR is for them to be both false, but after discussion both errors would be better. But that will be a future PR

Comment thread M2/Macaulay2/m2/lists.m2 Outdated

VisibleList _ List := VisibleList => (x,y) -> apply(splice y, i -> x#i)
VisibleList _ List :=
VisibleList _ Sequence := VisibleList => (L, ind) -> (
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why should we have VisibleList_Sequence? it's not obvious to me that this is a sensible syntax, and we may want to reserve it for a different purpose in the future.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This was a request from @mahrud (#4242 (comment))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't think this makes sense -- in the example, L_{1..3} works just fine and is not longer than L_(1..3) (in particular, doesn't require toList). Overall I think this is a mistake and should be removed.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ah, yeah -- it calls splice on the list, so nested sequences get flattened out. Neat.

Since you're opposed, I'll go ahead and remove it for now and maybe we can debate the merits of subsetting with sequences at some point in M2internals or something

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I explained why it's useful above, namely avoiding the need to do X_(toList Y). @pzinn do you have a particular purpose in mind?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Oh you're saying L_{seq} also works. Fair enough.

@d-torrance d-torrance merged commit 1d68387 into Macaulay2:development May 8, 2026
5 checks passed
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.

3 participants