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

Recurse rewrite into if blocks

Open odow opened this issue 1 year ago • 0 comments

Because people do stuff like this: https://github.com/GenXProject/GenX.jl/blob/150fe7af80410a11bd17adf544c186f898f0ddcd/src/model/core/co2.jl#L76-L83

julia> using JuMP

julia> flag = true
true

julia> model = Model()
A JuMP Model
├ solver: none
├ objective_sense: FEASIBILITY_SENSE
├ num_variables: 0
├ num_constraints: 0
└ Names registered in the model: none

julia> @variable(model, x[1:3])
3-element Vector{VariableRef}:
 x[1]
 x[2]
 x[3]

julia> @expression(model, 
           if flag
               sum(x[i] for i in 1:3)
           end
       )
x[1] + x[2] + x[3]

julia> @macroexpand @expression(model, 
           if flag
               sum(x[i] for i in 1:3)
           end
       )
quote
    #= REPL[13]:1 =#
    JuMP._valid_model(model, :model)
    begin
        #= /Users/oscar/.julia/packages/JuMP/6RAQ9/src/macros.jl:392 =#
        let model = model
            #= /Users/oscar/.julia/packages/JuMP/6RAQ9/src/macros.jl:393 =#
            begin
                #= /Users/oscar/.julia/packages/JuMP/6RAQ9/src/macros/@expression.jl:86 =#
                begin
                    #= /Users/oscar/.julia/packages/JuMP/6RAQ9/src/macros.jl:257 =#
                    var"#11###299" = begin
                            #= /Users/oscar/.julia/packages/MutableArithmetics/OfRUD/src/rewrite.jl:344 =#
                            let
                                #= /Users/oscar/.julia/packages/MutableArithmetics/OfRUD/src/rewrite.jl:345 =#
                                begin
                                    #= /Users/oscar/.julia/packages/MutableArithmetics/OfRUD/src/rewrite.jl:340 =#
                                end
                                #= /Users/oscar/.julia/packages/MutableArithmetics/OfRUD/src/rewrite.jl:346 =#
                                if flag
                                    #= REPL[13]:3 =#
                                    sum((x[i] for i = 1:3))
                                end
                            end
                        end
                    #= /Users/oscar/.julia/packages/JuMP/6RAQ9/src/macros.jl:258 =#
                    var"#12###300" = (JuMP.flatten!)(var"#11###299")
                end
                #= /Users/oscar/.julia/packages/JuMP/6RAQ9/src/macros/@expression.jl:89 =#
                JuMP._replace_zero(model, var"#12###300")
            end
        end
    end
end

cc @lbonaldo

odow avatar Sep 23 '24 03:09 odow