检索方法内容为“Expr” [英] Retrieve method content as an `Expr`ession

查看:107
本文介绍了检索方法内容为“Expr”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  f (x,y)= 3x ^ 2 + x * y  -  2y + 1 

以下引用阻止此方法,其中包含函数内容?

  quote#REPL [0],第2行:
((3 * x ^ 2 + x * y) - 2y)+ 1
end
pre>

解决方案

正如人们在评论中提到的那样,挖掘这些方法的领域不是一个稳定的或正式的支持的API。此外,你的简单例子是欺骗。这通常不代表您为该方法编写的原始代码。它是一个简单的中间AST表示,具有单一分配变量和大大简化的控制流程。一般来说,它返回的AST是无效的顶级Julia代码。这只是为了你的简单的例子,就是这样。



那就是说,有一个记录的方法来做到这一点。您可以使用 code_lowered() 可以访问此中间表达式,而无需挖掘未记录的字段。这将贯穿于Julia版本,但我不认为中间代表的稳定性有正式的保证。这是一个稍微复杂的例子:

  julia> f(X)=对于X中的elt;的println(ELT); end 
f(具有1个方法的通用函数)

julia> code_lowered(f)[1]
在REPL [17]的f(X)的LambdaInfo模板:1
:(开始

SSAValue(0)= X
#temp#=(Base.start)(SSAValue(0))
4:
除非!((Base.done)(SSAValue(0),#temp#))goto 13 $ b $ (SSAValue(1),1)
#temp#=(Core() .getfield)(SSAValue(1),2)#行1:
(Main.println)(elt)
11:
goto 4
13:
return
end)

julia> code_lowered(f)[1] == methods(f).ms [1] .lambda_template
true

如果您真的希望看到代码完全一样,最好的方法是使用嵌入的文件和行信息,并参考原始来源。请注意,这正是Gallium.jl(Julia的调试器)在逐步执行功能时找到源显示的方式。这是没有记录的,但是您甚至可以访问REPL历史记录来交互定义功能。 查看Gallium如何在这里进行


I have a function f defined as follows.

f(x, y) = 3x^2 + x*y - 2y + 1

How can I retrieve the following quote block for this method, which includes the function contents?

quote  # REPL[0], line 2:
    ((3 * x ^ 2 + x * y) - 2y) + 1
end

解决方案

As folks have mentioned in the comments, digging through the fields of the methods like this isn't a stable or officially supported API. Further, your simple example is deceiving. This isn't, in general, representative of the original code you wrote for the method. It's a simplified intermediate AST representation with single-assignment variables and drastically simplified control flow. In general, the AST it returns isn't valid top-level Julia code. It just so happens that for your simple example, it is.

That said, there is a documented way to do this. You can use code_lowered() to get access to this intermediate representation without digging through undocumented fields. This will work across Julia versions, but I don't think there are official guarantees on the stability of the intermediate representation yet. Here's a slightly more complicated example:

julia> f(X) = for elt in X; println(elt); end
f (generic function with 1 method)

julia> code_lowered(f)[1]
LambdaInfo template for f(X) at REPL[17]:1
:(begin
        nothing
        SSAValue(0) = X
        #temp# = (Base.start)(SSAValue(0))
        4:
        unless !((Base.done)(SSAValue(0),#temp#)) goto 13
        SSAValue(1) = (Base.next)(SSAValue(0),#temp#)
        elt = (Core.getfield)(SSAValue(1),1)
        #temp# = (Core.getfield)(SSAValue(1),2) # line 1:
        (Main.println)(elt)
        11:
        goto 4
        13:
        return
    end)

julia> code_lowered(f)[1] == methods(f).ms[1].lambda_template
true

If you really want to see the code exactly as it was written, the best way is to use the embedded file and line information and refer to the original source. Note that this is precisely the manner in which Gallium.jl (Julia's debugger) finds the source to display as it steps through functions. It's undocumented, but you can even access the REPL history for functions defined interactively. See how Gallium does it through here.

这篇关于检索方法内容为“Expr”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆