JuliaSymbolics/Symbolics.jl

`build_function` on `ifelse` gives `if` instead of `ifelse`

Opened this issue · 0 comments

Presumably build_function on ifelse should use the ifelse function in the generated code, not an if block.

julia> @variables x;

julia> f(x) = ifelse(x - 1 > 0, 1, 0);

julia> g = build_function(f(x), x)
:(function (x,)
      #= /Users/dsanders/.julia/packages/SymbolicUtils/qyMYa/src/code.jl:373 =#
      #= /Users/dsanders/.julia/packages/SymbolicUtils/qyMYa/src/code.jl:374 =#
      #= /Users/dsanders/.julia/packages/SymbolicUtils/qyMYa/src/code.jl:375 =#
      if (>)((+)(-1, x), 0)
          1
      else
          0
      end
  end)

julia> dump(g)
Expr
  head: Symbol function
  args: Array{Any}((2,))
    1: Expr
      head: Symbol tuple
      args: Array{Any}((1,))
        1: Symbol x
    2: Expr
      head: Symbol block
      args: Array{Any}((4,))
        1: LineNumberNode
          line: Int64 373
          file: Symbol /Users/dsanders/.julia/packages/SymbolicUtils/qyMYa/src/code.jl
        2: LineNumberNode
          line: Int64 374
          file: Symbol /Users/dsanders/.julia/packages/SymbolicUtils/qyMYa/src/code.jl
        3: LineNumberNode
          line: Int64 375
          file: Symbol /Users/dsanders/.julia/packages/SymbolicUtils/qyMYa/src/code.jl
        4: Expr
          head: Symbol if
          args: Array{Any}((3,))
            1: Expr
              head: Symbol call
              args: Array{Any}((3,))
                1: > (function of type typeof(>))
                2: Expr
                3: Int64 0
            2: Int64 1
            3: Int64 0

Compare:

julia> dump(:(ifelse(x - 1 > 0, 1, 0)))
Expr
  head: Symbol call
  args: Array{Any}((4,))
    1: Symbol ifelse
    2: Expr
      head: Symbol call
      args: Array{Any}((3,))
        1: Symbol >
        2: Expr
          head: Symbol call
          args: Array{Any}((3,))
            1: Symbol -
            2: Symbol x
            3: Int64 1
        3: Int64 0
    3: Int64 1
    4: Int64 0