有没有一种方法可以在Julia中深度复制一个函数?

3
基本上就是标题所说的。如果我分配 g=f 或进行 g=deepcopy(f),结果都是相同的:重新定义 f 将重新定义 g。有没有办法使 g 成为 f 的独立副本?
julia> function f(x) x end
f (generic function with 1 method)

julia> f(1)
1

julia> g = deepcopy(f)
f (generic function with 1 method)

julia> g(1)
1

julia> function f(x) x+1 end
f (generic function with 1 method)

julia> f(1)
2

julia> g(1)
2

3
这个问题本身很有趣,但我怀疑在实践中,你最好使用 f = x -> x; g = f; f = x -> x + 1 - phipsgabler
好的,这实际上不会影响 g!谢谢! - Levasco
2个回答

2

据我所知,Julia中的函数被认为是位类型:

julia> f(x) = x
f (generic function with 1 method)

julia> isbitstype(typeof(f))
true

这意味着在它们身上使用deepcopy是不起作用的。这里有一个deepcopy的定义:
function deepcopy(x)
    isbitstype(typeof(x)) && return x
    return deepcopy_internal(x, IdDict())::typeof(x)
end

这是因为每个函数都有自己的类型(但可以有许多方法)。 phipsgabler提出的解决方案有效,因为每次定义匿名函数时,它都会获得自己的新类型。请参见此处:
julia> typeof(x -> x)
var"#1#2"

julia> typeof(x -> x)
var"#3#4"

julia> typeof(x -> x)
var"#5#6"

然而,这也有一个缺点 - 每次将新的匿名函数传递给另一个函数时,它都必须被编译,例如:

julia> @time map(x -> x, 1:2);
  0.024220 seconds (49.61 k allocations: 2.734 MiB)

julia> @time map(x -> x, 1:2);
  0.023754 seconds (48.25 k allocations: 2.530 MiB)

julia> @time map(x -> x, 1:2);
  0.023336 seconds (48.25 k allocations: 2.530 MiB)

vs

julia> fun = x -> x
#7 (generic function with 1 method)

julia> @time map(fun, 1:2);
  0.023459 seconds (48.23 k allocations: 2.530 MiB)

julia> @time map(fun, 1:2);
  0.000016 seconds (4 allocations: 192 bytes)

julia> @time map(fun, 1:2);
  0.000013 seconds (4 allocations: 192 bytes)

回到您最初的问题。即使您写下以下内容:
julia> f(x) = x
f (generic function with 1 method)

julia> g = x -> f(x)
#1 (generic function with 1 method)

julia> g(1)
1

你需要的是:

julia> f(x) = 2x
f (generic function with 1 method)

julia> g(1)
2

因为f(x) = 2x替换了函数f的方法,但其类型并未改变(如上所述 - 一个函数可以有多个方法,甚至可以像上面的例子一样更新函数的方法)。这与所谓的“世界年龄”问题有关,你在Julia手册中这里已经解释过。


1
您可以使用 IRTools.jl 进行处理。对于您的情况,应该定义为:
_f = IRTools.@code_ir f(1)

old_f = IRTools.func(_f)

然后,您可以重新定义

function f(x) x+1 end

当你调用 "old_f" 时,它应该是你想要的

old_f(_f, 2) = 2

此外,如果你想在自定义包中完成所有这些操作,你应该将old_f = IRTools.func(_f)移动到一个函数体内,并用以下代码替换最后的调用函数:

Base.invokelatest(old_f, _f, 2) 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接