元编程是反射的一个子集吗?

发布于 2024-12-08 02:06:33 字数 759 浏览 0 评论 0原文

我曾经认为元编程涉及修改程序,并且(就像

反射是程序观察和观察的过程 在运行时修改其自身的结构和行为。

反射是程序执行的过程 内省。这种内省通常涉及以下能力: 在运行时观察并修改其自身的结构和行为。从 理论视角反思与以下事实有关:程序 指令被存储为数据。程序代码的区别 数据是如何处理信息的问题。因此程序 可以将自己的代码视为数据并观察或修改它们。

[强调]

元编程的描述是

元编程是编写可以编写或操作其他内容的程序 程序作为他们的数据。

元编程很有用,因为它可以为程序员节省宝贵的时间 时间。有些语言支持对自身进行元编程,这 允许创建具有强大表达能力的代码。

(我认为“写入”并不意味着将源代码写入文件,因为那将是代码生成。)

这会使元编程仅仅是反射的一个子集吗?

或者这些术语是否不同,因为某些编程语言是由另一种语言进行元编程的,在这种情况下会发生元编程但不会发生反射? (元编程维基百科文章中有一个未引用的句子声称这一点)

或者术语“反射” ”和“元编程”的使用方式有所不同,具体取决于人们使用的编程语言?

I used to think that metaprogramming involved modifying the program, and (as do some answers to What is reflection and why is it useful? ) that reflection merely consisted of introspection of a program. However, the reflection tag wiki says

Reflection is the process by which a program can observe and
modify its own structure and behavior at runtime.

Reflection is the process by which a program can perform
introspection. This introspection usually involves the ability to
observe and modify its own structure and behavior at runtime. From
theoretical perspective reflection relates to the fact that program
instructions are stored as data. The distinction between program code
and data is a matter of how the information is treated. Hence programs
can treat their own code as data and observe or modify them.

[emphasis added]

And the description for metaprogramming is

Metaprogramming is writing programs that write or manipulate other
programs as their data.

Metaprogramming is useful because it can save programmers valuable
time. Some languages have support to metaprogram themselves and this
allows to create code with great expressive power.

(I assume that "write" doesn't mean writing source code to a file, because that'd be code generation.)

Would this make metaprogramming merely a subset of reflection?

Or are the terms distinct because some programming languages are metaprogrammed by another language, in which case metaprogramming occurs but not reflection? (There was a single uncited sentence claiming this in the metaprogramming Wikipedia article)

Or do the terms "reflection" and "metaprogramming" get used differently depending on what programming language the person is using?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寻梦旅人 2024-12-15 02:06:33

不。相反,反射提供的功能是元编程功能的一个子集。

元编程是“编写程序的程序”。这包括读取程序文本(可以说包括程序本身,但这种情况相当罕见)、分析代码并进行更改的程序。是的,它包括将源文本写入文件。代码生成是元编程的一个特例。

我理解的反射是程序查询自身结构的能力。在我见过的几乎所有可以进行反射的系统中(Lisp 及其等效变体的真正例外情况),反射机制仅提供了有限的内省手段。 Java 和 C# 可以让你找到类和方法的名称,但你不能向这些系统询问方法、语句或局部声明的内容。您也不能要求大多数此类反射语言实际更改其结构,也就是说,您不能使用反射工具添加新的类或字段。大多数语言(例如,C++)基本上没有内置的“反射”能力。虽然内置于语言中的反射实用程序可能很有用,但相对于语言设计者/编译器构建者决定在运行时保留的内容而言,它们往往是特殊的。

如果您跳出语言以及语言设计者内置的一组限制,您最终会获得更强大的“反射”能力。一个真正好的元编程系统可以访问整个程序结构,因此可以回答有关程序结构的任意问题(模图灵限制)。

例如,我们的DMS Software Reengineering Toolkit是一个程序转换工具,可以完全访问抽象语法程序树以及由各种 DMS 语言前端派生的许多其他事实。因此,DMS 可以相当任意地“反映”(检查/分析/推理)它正在处理的语言。它可以对 C、COBOL、Java、C# 和 C++ 执行此操作;对于许多这样的语言,它不仅可以提供对 AST 的访问,还可以提供对符号表信息以及各种形式的控制和数据流的访问,这是我见过的反射工具所无法提供的。

此外,像 DMS 这样的程序转换工具可以基于“反射”修改代码以生成新代码、优化、重组、检测……通过这种方式可以实现的效果种类繁多,令人惊讶。

[由于 DMS 是作为一组 DSL 实现的,因此它实际上可以并且确实对自己的代码进行推理(“反映”)。我们使用 DMS 从 DSL 合成其自身的大部分,包括具有一些非常有趣的优化的代码生成,包括工作并行化。]

No. Rather, reflection provides facilities that are a subset of what metaprogramming can do.

Metaprogramming is "programs which write programs". This includes programs that read the text of programs (arguably including themselves but that is rather rare), analyze that code, and make changes. Yes, it includes writing source text to files. Code generation is a special case of metaprogramming.

Reflection as I understand it is the ability for a program to inquire about its own structure. In virtually every system I have seen in which reflection is possible (with the really exceptional case of Lisp and equivalent variants), the reflection machinery provided only a limited means of introspection. Java and C# will let you find out the names of classes and methods, but you cannot ask these systems for the content of a method, statement or local declaration. Nor can you ask most such reflective langauges to actually change their structure, that is, you can't add new classes or fields using the reflection facilities. Most langauges (e.g., C++) have basically no built-in ability to "reflect". While the reflection utilities built into langauges can be useful, they tend to be idiosyncratic with respect to what the language designers/compiler builders decided to keep around at runtime.

You end up with a much more powerful "reflection" capability if you step outside the language and the set of restrictions the langauge designers built into it. A really good metaprogramming system has access to the entire program structure, and thus can answer arbitrary questions about the program structure (modulo Turing limitations).

As an example, our DMS Software Reengineering Toolkit is a program transformation tool that has complete access to the abstract syntax tree of the program and many other facts derived by the various DMS language front ends. So DMS can "reflect" (inspect/analyze/reason) rather arbitrarily about the language which it is processing. And it can do so for C, COBOL, Java, C# and C++; for many of these langauges, it can not only provide access to the AST, but access to symbol table information and various forms of control and data flow, which no reflection facilities I've ever seen offer you.

Additionally, a program transformation tool like DMS can modify the code based on the "reflection" to generate new code, optimize, restructure, instrument, ... The variety of effects achievable this way is surprisingly broad.

[Since DMS is implemented as set of DSLs, it in fact can and does reason ("reflect") about its own code. We use DMS to synthesize large parts of itself from its DSLs, including code generation with some pretty interesting optimizations, including working parallelization.]

终陌 2024-12-15 02:06:33

反射也可能在执行过程中发生,例如,当程序能够检查自己的调用堆栈(通过查看内部的调用帧)时,元编程甚至可以更改已被调用的函数(即调用堆栈中较低的帧) )及其调用框架。

您可以考虑阅读 Jacques Pitrat 关于人造生物的书 -有意识机器的良心详细解释了为什么这是有用的以及如何实现它。另请参阅他的博客

Reflection can also happen during execution, when for instance a program is able to inspect its own call stack (by looking into call frames inside) And metaprogramming could even change the functions which have been called (that is which have frames lower in the call stack) together with their call frames.

You might consider reading Jacques Pitrat's book on Artificial Beings - the conscience of a conscious machine which explain in details why this is useful and how it can be implemented. See also his blog.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文