Skip to content

maximum/minimum breaks inference in recursive definitions where mapreduce works #44906

@danielwe

Description

@danielwe

When defining f(x) = maximum(f, x) plus an appropriate base case, julia fails to infer the return type of f. The equivalent definition f(x) = mapreduce(f, max, x) does not have this problem. minimum is the same.

julia> x = [[1, 2], [3, 4]];

julia> f(x) = maximum(f, x); f(x::Number) = x;

julia> @code_warntype f(x)
MethodInstance for f(::Vector{Vector{Int64}})
  from f(x) in Main at REPL[19]:1
Arguments
  #self#::Core.Const(f)
  x::Vector{Vector{Int64}}
Body::Any
1%1 = Main.maximum(Main.f, x)::Any
└──      return %1


julia> g(x) = mapreduce(g, max, x); g(x::Number) = x;

julia> @code_warntype g(x)
MethodInstance for g(::Vector{Vector{Int64}})
  from g(x) in Main at REPL[21]:1
Arguments
  #self#::Core.Const(g)
  x::Vector{Vector{Int64}}
Body::Int64
1%1 = Main.mapreduce(Main.g, Main.max, x)::Int64
└──      return %1

Stumbled upon this in #44893. Perhaps related to #43461, but not the same as this is still a problem after #43467, and maximum(f, x[1]) infers.

I'm using version 1.7.2. @MasonProtter confirmed this is still an issue on master.

julia> versioninfo()
Julia Version 1.7.2
Commit bf53498635 (2022-02-06 15:21 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-12.0.1 (ORCJIT, skylake)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions