探索 Erlang Abstract Form--Module声明和Form

本文探讨Erlang抽象语法树(Abstract Form)的基本构成及其解析过程,通过实例讲解如何将Erlang源代码转换为抽象形式,重点介绍模块声明、属性及函数声明等元素的表示方式。

转载:http://agileprogrammer.blogspot.com/2006/08/erlang-abstract-form-moduleform.html


我们将具体考察Erlang Abstract Form的组成。方法很简单,参考Eralng文档的Abstract Form一节,用实际的例子加以验证。

Abstract Form


Abstract Form文档中,用函数Rep表示从Erlang源代码C到abstract form形式R的映射。简单地说,如果源代码C解析成为Abstract Form R,那么写成R = Rep(C)。
另外文档中LINE表示源代码的行号。
下面是module声明的描述:

A module declaration consists of a sequence of forms that are either function declarations or attributes.

  • If D is a module declaration consisting of the forms F_1, ..., F_k, then Rep(D) = [Rep(F_1), ..., Rep(F_k)].
  • If F is an attribute -module(Mod), then Rep(F) = {attribute,LINE,module,Mod}.
  • If F is an attribute -export([Fun_1/A_1, ..., Fun_k/A_k]), then Rep(F) = {attribute,LINE,export,[{Fun_1,A_1}, ..., {Fun_k,A_k}]}.
  • If F is an attribute -import(Mod,[Fun_1/A_1, ..., Fun_k/A_k]), then Rep(F) = {attribute,LINE,import,{Mod,[{Fun_1,A_1}, ..., {Fun_k,A_k}]}}.
  • If F is an attribute -compile(Options), then Rep(F) = {attribute,LINE,compile,Options}.
  • If F is an attribute -file(File,Line), then Rep(F) = {attribute,LINE,file,{File,Line}}.
  • If F is a record declaration -record(Name,{V_1, ..., V_k}), then Rep(F) = {attribute,LINE,record,{Name,[Rep(V_1), ..., Rep(V_k)]}}. For Rep(V), see below.
  • If F is a wild attribute -A(T), then Rep(F) = {attribute,LINE,A,T}.
  • If F is a function declaration Name Fc_1 ; ... ; Name Fc_k, where each Fc_i is a function clause with a pattern sequence of the same length Arity, then Rep(F) = {function,LINE,Name,Arity,[Rep(Fc_1), ...,Rep(Fc_k)]}.
模块声明由一系列Form组成,这些Form要么是函数声明,要么是属性(attribute)。

Simplest

考察我们最简单的模块 simplest。

-module(simplest).

我们对它进行编译,然后获取它的abstract_code:

Eshell V5.5  (abort with ^G)
1> c(simplest,[debug_info]).
{ok,simplest}
2> beam_lib:chunks(simplest, [abstract_code]).
{ok,{simplest,[{abstract_code,{raw_abstract_v1,
                  [{attribute,1,file,{"./simplest.erl",1}},
                   {attribute,1,module,simplest},
                   {eof,1}]}}]}}


beam_lib:chunks返回的abstract_code定义如下:
 
{ChunkName, DataT} = {abstract_code, AbstractCode} AbstractCode = {AbstVersion, Forms} | no_abstract_code AbstVersion = atom()
如果无法在beam文件中找到abstract form,那么将返回no_abstract_code。如果找到的话,则是一个tuple, tuple的第一项是版本,即我们上面例子中的raw_abstract_v1,tuple的第2项就是真正的form。因此,最简单的simplest beam文件中包含的Form如下:
  [{attribute,1,file,{"./simplest.erl",1}},
 {attribute,1,module,simplest},
 {eof,1}]
Abstract Form关于module声明Form的第一条说:
If D is a module declaration consisting of the forms F_1, ..., F_k, then Rep(D) = [Rep(F_1), ..., Rep(F_k)].
这可以解释Form为什么是一个列表。
If F is an attribute -module(Mod), then Rep(F) = {attribute,LINE,module,Mod}.

因此,例子中Form的第2行是:
 {attribute,1,module,simplest}
还有:
If F is an attribute -file(File,Line), then Rep(F) = {attribute,LINE,file,{File,Line}}.
这也说明了为什么会出现:
 {attribute,1,file,{"./simplest.erl",1}}
尽管我们没有在源代码中编写-file属性,但是编译器还是在abstract code中加入了这个属性。

最后,由于文件在第一行结束,因此还包含
 {eof,1}


这是在Abstract Form文档的其中一节提到的:
4.1.2 Representation of parse errors and end of file
In addition to the representations of forms, the list that represents a module declaration (as returned by functions in erl_parse and epp) may contain tuples {error,E}, denoting syntactically incorrect forms, and {eof,LINE}, denoting an end of stream encountered before a complete form had been parsed.

加入一个方法


接下去,我们在simplest.erl加入一个新的函数test,并export:

-module(simplest). %1
-export([test/0]). %2
test() ->  %3
ok.   %4


重新编译simplest,并获取abstract code 如下:

5> c(simplest,[debug_info]).       
{ok,simplest}
6> beam_lib:chunks(simplest, [abstract_code]).
{ok,{simplest,[{abstract_code,{raw_abstract_v1,
                       [{attribute,1,file,{"./simplest.erl",1}},
                        {attribute,1,module,simplest},
                        {attribute,2,export,[{test,0}]},
                        {function,
                            3,
                            test,
                            0,
                            [{clause,3,[],[],[{atom,4|...}]}]},
                        {eof,5}]}}]}}


首先,我们看到新增加了export属性,出现在代码的第2行,其中包括0个参数的test这个tuple。最主要的变化是一个新的function Form:

If F is a function declaration Name Fc_1 ; ... ; Name Fc_k, where each Fc_i is a function clause with a pattern sequence of the same length Arity, then Rep(F) = {function,LINE,Name,Arity,[Rep(Fc_1), ...,Rep(Fc_k)]}.
function的Form共有5项,第一项是function这个atom,第二项是行号,第3项是函数的名字,第4项是函数参数的个数。最后一项是一个列表,包含每个子句的Rep。

我们可以深入到function的每一个子句中去,但是探索Erlang Abstract Form的目的是为了能够理解metaprogramming的原理。而在实际编程时,很少有人会用Form来动态生成一个新的函数。通常使用的方法是提供一个函数的源代码,或者直接使用函数参数,关于直接对Form更详细的操纵,我们放到后面再说。

我将在下一篇中描述如何把源代码解析为Form,编译Form并动态加载成功的二进制数据,从而实现运行时刻的metaprogramming能力。

(Kriging_NSGA2)克里金模型结合多目标遗传算法求最优因变量及对应的最佳自变量组合研究(Matlab代码实现)内容概要:本文介绍了克里金模型(Kriging)与多目标遗传算法NSGA-II相结合的方法,用于求解最优因变量及其对应的最佳自变量组合,并提供了完整的Matlab代码实现。该方法首先利用克里金模型构建高精度的代理模型,逼近复杂的非线性系统响应,减少计算成本;随后结合NSGA-II算法进行多目标优化,搜索帕累托前沿解集,从而获得多个最优折衷方案。文中详细阐述了代理模型构建、算法集成流程及参数设置,适用于工程设计、参数反演等复杂优化问题。此外,文档还展示了该方法在SCI一区论文中的复现应用,体现了其科学性与实用性。; 适合人群:具备一定Matlab编程基础,熟悉优化算法数值建模的研究生、科研人员及工程技术人员,尤其适合从事仿真优化、实验设计、代理模型研究的相关领域工作者。; 使用场景及目标:①解决高计算成本的多目标优化问题,通过代理模型降低仿真次数;②在无法解析求导或函数高度非线性的情况下寻找最优变量组合;③复现SCI高水平论文中的优化方法,提升科研可信度与效率;④应用于工程设计、能源系统调度、智能制造等需参数优化的实际场景。; 阅读建议:建议读者结合提供的Matlab代码逐段理解算法实现过程,重点关注克里金模型的构建步骤与NSGA-II的集成方式,建议自行调整测试函数或实际案例验证算法性能,并配合YALMIP等工具包扩展优化求解能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值