解释器和动态类型语言

发布于 2024-10-06 13:27:17 字数 36 浏览 0 评论 0原文

为什么具有动态类型语言的程序通常是解释性的而不是编译性的?

Why are programs that have dynamically typed languages usually interpreted rather than compiled?

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

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

发布评论

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

评论(6

落花浅忆 2024-10-13 13:27:17

简而言之:它们就像豌豆和胡萝卜一样结合在一起。

编译与解释和语言输入从根本上来说是不同的问题,因为您可以拥有所有可能的排列。另一方面,为语言设计选择编译和不选择动态类型的“原因”通常是相同的:性能。选择动态类型和解释的“原因”也有些相关。

这不是一个硬性规定。你总是可以把它们混合起来。例如,您可以编译 Perl 和 Lisp 并解释 C。

In short: They go together like peas and carrots.

Compiling vs. interpreting and language typing are fundamentally separate concerns in that you can have all possible permutations. On the other hand, the "reason" for picking compiling and not picking dynamic typing for a language design are usually the same: performance. The "reason" for picking dynamic typing and interpretation are also somewhat related.

It's not a hard and fast rule. You can always mix 'em up. You can compile Perl and Lisp for example and interpret C.

陌生 2024-10-13 13:27:17

您正在观察一种非因果相关性:

  • 动态类型和解释相关,因为两者都很容易实现。
  • 静态类型和编译是相关的,因为两者都有利于获得可预测的良好性能。

编译器通常会被改造为动态类型语言,以试图提高性能(因为性能通常很差)。例如,以下是一些主要动态类型语言在编写第一个编译器之前解释的时间:Lisp (1958-1962)、Mathematica (1988-2004)、Lua (1993-2004)、Python (1991-2002) 和JavaScript(1995-2009)。相比之下,OCaml (1996) 和 F# (2001) 等语言首先作为编译器发布。

You are observing a non-causal correlation:

  • Dynamic typing and interpretation correlate because both are easy to implement.
  • Static typing and compilation correlate because both are conducive of predictably-good performance.

Compilers are usually retrofitted onto dynamically-typed languages in an attempt to improve performance (because performance is often very poor). For example, here's how long some major dynamically-typed languages were interpreted for before their first compiler was written: Lisp (1958-1962), Mathematica (1988-2004), Lua (1993-2004), Python (1991-2002) and Javascript (1995-2009). In contrast, languages like OCaml (1996) and F# (2001) were released first as compilers.

怂人 2024-10-13 13:27:17

正如其他人所指出的,语言既不是编译的,也不是解释的。它们只是需要翻译的规则,并且大多数已经解释和编译了实现。即便如此,当许多“解释器”jitting 到处都是,如果源文件发生变化,一些“编译器”很乐意按需编译。

也许最好将实现分类为完全预编译按需编译。如果我们使用这些类别,破坏完整预编译的一件事就是 eval 函数。这对实现的影响可能比动态类型更大。如果您有 eval 函数,则需要支持按需编译。

As noted by others, languages are neither compiled or interpreted. They're just rules that need translating and most have interpreted and compiled implementations. Even then, it's hard to talk about interpretation versus compilation when many "interpreters" are jitting all over the place and some "compilers" are happy to compile-on-demand if a source file changes.

Maybe it's better to categorize implementations as fully pre-compiled or compiled-on-demand. If we use these categories, the one thing that breaks full pre-compilation is an eval function. This probably has more of an effect on the implementation than dynamic types. If you have an eval function, you're required to support compile-on-demand.

究竟谁懂我的在乎 2024-10-13 13:27:17

Common Lisp 代码大部分是经过编译的。 Common Lisp 编程语言已在 ANSI 标准中描述并支持编译。 ANSI 标准描述了编译代码的函数、描述了优化的各个方面、描述了编译过程的各个方面等等。

Common Lisp 的解释器是存在的,但很少使用。

Common Lisp 实现通常可以自由地混合不同的执行模式。几乎都有编译器。少数只有编译器。

几乎所有实现中的编译都有增量模式,这样就可以交互使用。都可以编译文件。有些具有“块编译”模式来编译文件组。

Common Lisp code is mostly compiled. The Common Lisp programming language has been described in an ANSI standard with support for compilation. The ANSI standard describes functions that compile code, describes various aspects of optimizations, describes aspects of the compilation process and more.

Interpreters for Common Lisp exist, but are less often used.

Common Lisp implementations often can mix different execution modes freely. Almost all have a compiler. A few only have a compiler.

Compilation in almost all implementations has an incremental mode, so that it can be used interactively. All can compile files. Some have 'block compilation' modes to compile groups of files.

月依秋水 2024-10-13 13:27:17

根据动态类型语言的定义......

当一种编程语言的大部分类型检查是在运行时而不是在编译时执行时,就被认为是动态类型的。在动态类型中,值有类型,但变量没有;也就是说,变量可以引用任何类型的值。

也就是说,很难知道将程序编译成什么,因为它可能在运行时发生变化。维基百科的另一个摘录......

动态类型允许构造
一些静态类型检查会拒绝
作为非法的。例如,评估
函数,执行任意
数据即代码,成为可能。
此外,动态类型更好
容纳过渡代码和
原型设计,例如允许
占位符数据结构(模拟
对象)透明地用于
成熟的数据结构的地方
(通常是为了
实验和测试)。

我觉得这个答案仍然不完整,但我希望它能为您指明正确的方向。也许这里的其他人可以对此进行扩展。

Per the definition of dynamically typed languages...

A programming language is said to be dynamically typed when the majority of its type checking is performed at run-time as opposed to at compile-time. In dynamic typing, values have types but variables do not; that is, a variable can refer to a value of any type.

That said, it becomes hard to know what to compile the program down to since it could change at runtime. Another excerpt here from Wikipedia...

Dynamic typing allows constructs that
some static type checking would reject
as illegal. For example, eval
functions, which execute arbitrary
data as code, become possible.
Furthermore, dynamic typing better
accommodates transitional code and
prototyping, such as allowing a
placeholder data structure (mock
object) to be transparently used in
place of a full-fledged data structure
(usually for the purposes of
experimentation and testing).

I feel this answer is still incomplete but I hope it points you in the right direction. Perhaps others here can expand on this.

墟烟 2024-10-13 13:27:17

检查类型一次实际上就是“编译器”(〜类型检查器)的组成部分。

当类型是“动态”时,您无法“编译”(检查类型一次)。

Checking types once is actually what makes a "compiler" (~ a type checker).

You can't "compile" (check types once) when types are "dynamic".

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