AbstractAlgebra.jl icon indicating copy to clipboard operation
AbstractAlgebra.jl copied to clipboard

`@varnames_interface`: handle `polynomial_ring(QQ, :x => 3)` better

Open fingolfin opened this issue 6 months ago • 0 comments

Right now polynomial_ring(QQ, :x => 3) leads to an error that is not very helpful:

julia> polynomial_ring(QQ, :x => 3)
ERROR: MethodError: no method matching iterate(::Symbol)

Closest candidates are:
  iterate(::Core.Compiler.InstructionStream, ::Int64)
   @ Base show.jl:2778
  iterate(::Core.Compiler.InstructionStream)
   @ Base show.jl:2778
  iterate(::Oscar.EdgeIterator)
   @ Oscar ~/Projekte/OSCAR/Oscar.spielwiese/src/Combinatorics/Graphs/functions.jl:352
  ...

Stacktrace:
  [1] iterate
    @ ./generator.jl:44 [inlined]
  [2] iterate
    @ ./iterators.jl:1202 [inlined]
  [3] iterate
    @ ./iterators.jl:1196 [inlined]
  [4] _collect(::Type{Symbol}, itr::Base.Iterators.Flatten{Base.Generator{…}}, isz::Base.SizeUnknown)
    @ Base ./array.jl:700
  [5] collect(::Type{Symbol}, itr::Base.Iterators.Flatten{Base.Generator{Tuple{…}, AbstractAlgebra.var"#392#393"{…}}})
    @ Base ./array.jl:694
  [6] variable_names(as::Tuple{Pair{Symbol, Int64}}, brackets::Val{true})
    @ AbstractAlgebra ~/.julia/packages/AbstractAlgebra/tABFQ/src/misc/VarNames.jl:73
  [7] variable_names(as::Pair{Symbol, Int64})
    @ AbstractAlgebra ~/.julia/packages/AbstractAlgebra/tABFQ/src/misc/VarNames.jl:71
  [8] polynomial_ring(R::QQField, s::Tuple{Pair{Symbol, Int64}}; kv::@Kwargs{})
    @ AbstractAlgebra ~/.julia/packages/AbstractAlgebra/tABFQ/src/misc/VarNames.jl:305
  [9] polynomial_ring(R::QQField, s::Tuple{Pair{Symbol, Int64}})
    @ AbstractAlgebra ~/.julia/packages/AbstractAlgebra/tABFQ/src/misc/VarNames.jl:304
 [10] polynomial_ring(::QQField, ::Pair{Symbol, Int64}; kv::@Kwargs{})
    @ AbstractAlgebra ~/.julia/packages/AbstractAlgebra/tABFQ/src/misc/VarNames.jl:302
 [11] top-level scope
    @ REPL[63]:1
Some type information was truncated. Use `show(err)` to see complete types.

I'd like to improve this. The two major options are:

  1. catch the problem earlier and give a more helpful error,
  2. pick an interpretation and make it work
    1. perhaps it should be the same as polynomial_ring(QQ, :x => 1:3)
    2. perhaps it should be the same as polynomial_ring(QQ, :x => 3:3)

The fact that there are at least two interpretations might suggest we should prefer the error. Then again I am not sure how natural each is? For me I comparatively often make this mistake because I think "I want three x-variables", so I use :x => 3 when I really mean :x => 1:3. The only reason I am considering the other option is that it is based on Julia semantics: one can "iterate" over an integer, and the integer is treated as if it was a 0-dimensional container with a single entry (itself). Honestly I don't like this, but it is what it is, so one might just lean into it...

Still the safe route would be 1. An error can always be turned into working code later on.

Any thoughts on this?

fingolfin avatar Jul 26 '24 14:07 fingolfin