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

Macro for entering permutations in cycle notation #1307

Merged
merged 26 commits into from
May 14, 2022

Conversation

danielrademacher
Copy link
Contributor

@danielrademacher danielrademacher commented May 5, 2022

Resolves oscar-system/GAP.jl#420
Resolves #1189

@thofma
Copy link
Collaborator

thofma commented May 5, 2022

Would be good if there were some tests with invalid input, e.g. @perm (-1, 1) or @perm 10 [(1 11)].

@fingolfin fingolfin changed the title Dr/permutations Macro for entering permutations in cycle notation May 6, 2022
Copy link
Member

@fingolfin fingolfin left a comment

Choose a reason for hiding this comment

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

Thank you!! Great work. Still some changes would be good

experimental/Permutations/Permutations.jl Outdated Show resolved Hide resolved
experimental/Permutations/Permutations.jl Outdated Show resolved Hide resolved
experimental/Permutations/Permutations.jl Outdated Show resolved Hide resolved
experimental/Permutations/Permutations.jl Outdated Show resolved Hide resolved
experimental/Permutations/Permutations.jl Outdated Show resolved Hide resolved
experimental/Permutations/Permutations.jl Outdated Show resolved Hide resolved
experimental/Permutations/Permutations.jl Outdated Show resolved Hide resolved
experimental/Permutations/Permutations.jl Outdated Show resolved Hide resolved
test/Experimental/Permutations-test.jl Outdated Show resolved Hide resolved
test/Experimental/Permutations-test.jl Outdated Show resolved Hide resolved
test/Groups/Permutations.jl Outdated Show resolved Hide resolved
experimental/Permutations/Permutations.jl Outdated Show resolved Hide resolved
src/Groups/perm.jl Outdated Show resolved Hide resolved
src/Groups/perm.jl Outdated Show resolved Hide resolved
src/Groups/perm.jl Outdated Show resolved Hide resolved
src/Groups/perm.jl Outdated Show resolved Hide resolved
src/Groups/perm.jl Outdated Show resolved Hide resolved
test/runtests.jl Outdated Show resolved Hide resolved
@danielrademacher
Copy link
Contributor Author

danielrademacher commented May 12, 2022

I also added a check for

@perm ()

But than an error occurs for cperm

cperm([])
ERROR: MethodError: no method matching cperm(::Vector{Any})
Closest candidates are:
  cperm(::AbstractVector{T}...) where T<:Union{Integer, fmpz} at ~/.julia/dev/Oscar/src/Groups/perm.jl:214
  cperm(::PermGroup, ::AbstractVector{T}...) where T<:Union{Integer, fmpz} at ~/.julia/dev/Oscar/src/Groups/perm.jl:226

Is this intended?

src/Groups/perm.jl Outdated Show resolved Hide resolved
src/Groups/perm.jl Outdated Show resolved Hide resolved
src/Groups/perm.jl Outdated Show resolved Hide resolved
src/Groups/perm.jl Outdated Show resolved Hide resolved
src/Groups/perm.jl Outdated Show resolved Hide resolved
Comment on lines 517 to 518
ores[i] = esc(:(Oscar.cperm(symmetric_group($n),$(res...))))
i = i + 1
Copy link
Member

Choose a reason for hiding this comment

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

You could just let ores start out as an empty vector and then do push!(ores, ...), then you don't need i.

src/Groups/perm.jl Outdated Show resolved Hide resolved

pushfirst!(res, Expr(:vect,ex.args...))

ores[i] = esc(:(Oscar.cperm(symmetric_group($n),$(res...))))
Copy link
Member

Choose a reason for hiding this comment

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

This calls symmetric_group again and again, which isn't great, as we create a new group object each time. This could be improved by using let block; but I don't want to complicate things even further, so I suggest we first iron out everything else and get this merged, then we can worry about fine tuning.

Just for the record, here is what I'd do: I'd only collect the res values here:

Suggested change
ores[i] = esc(:(Oscar.cperm(symmetric_group($n),$(res...))))
ores[i] = esc(:( [$(res...)] ))

Then, the final return of this macro could look something like this:

return quote
   let g = symmetric_group($n)
       [ cperm(g, pi) for pi in [$(ores...)] ]
   end
end

Note: normally we'd have to worry that g and pi could clash with symbols defined by the programmer in the surrounding code, and referenced in the permutation expression. This is where the esc function comes into play: we applied it to the expressions we got from the user, but not to the outer expression above. This leads to the Julia macro hygiene functionality kicking in, which determines that g and pi (unlike symmetric_group and cperm) are not known variables, and replaces them by special generated names. You can check the effect of this via macroexpand:

julia> g = 7  # introduce a variable g and use it in the macro expression:
7

julia> macroexpand(Main, :( Oscar.@perm 14 [
                     (g,9)
                     (1,2)(10,11)
                    ]))
quote
    let var"#332#g" = Oscar.symmetric_group(14)
        [Oscar.cperm(var"#332#g", var"#333#pi"...) for var"#333#pi" = [[[g, 9]], [[1, 2], [10, 11]]]]
    end
end

src/Groups/perm.jl Outdated Show resolved Hide resolved
test/Groups/Permutations.jl Outdated Show resolved Hide resolved
Copy link
Member

@fingolfin fingolfin left a comment

Choose a reason for hiding this comment

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

Thanks!

@fingolfin fingolfin merged commit 7c55e6a into oscar-system:master May 14, 2022
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.

Nicer syntax for entering permutations Feature: nicer input syntax for permutations
3 participants