gpt4 book ai didi

lisp - Lisp 语法对 AST 建模的目的

转载 作者:太空宇宙 更新时间:2023-11-03 18:38:11 30 4
gpt4 key购买 nike

据我所知,Lisp 语法表示 AST,但采用高级格式以允许人类轻松阅读和修改,同时也便于机器处理源代码。

因此,在 Lisp 中,代码是数据,数据是代码,因为代码 (s-epxression) 本质上只是 AST。我们可以将更多 AST(这是我们的数据,只是 lisp 代码)插入其他 AST(lisp 代码)或独立地扩展其功能并在运行时(运行时)对其进行操作,而无需重新编译整个操作系统以集成新的代码。在其他语言中,我们必须重新编译才能将人类语言源代码转换为有效的 AST,然后再编译成代码。

这是否是 Lisp 语法最初设计的原因(代表 AST 但人类可读,以满足人类和机器)?为了在人机之间实现更强大(即时 - 运行时)以及更简单(无需重新编译,更快)的通信?

我听说 Lisp 机器只有一个地址空间来保存所有数据。在Linux这样的操作系统中,程序员只有虚拟地址空间,并假装是真实的物理地址空间,可以为所欲为。 Linux 中的数据和代码是分开的区域,因为实际上,数据就是数据,数据就是代码。在用 C(或类 C 语言)编写的普通操作系统中,如果我们只为整个系统操作一个地址空间,并且将数据与代码混合在一起,那将是非常困惑的。

在 Lisp Machine 中,既然代码就是数据,数据就是代码,这就是它只有一个地址空间(没有虚拟层)的原因吗?既然我们有 GC 而没有指针,那么在不破坏物理内存的情况下操裁剪理内存是否安全(因为只有 1 个空间要简单得多)?

编辑: 我问这个是因为据说 one of the advantage Lisp 的是单地址空间:

A safe language means a reliable environment without the need to separate tasks out into their own separate memory spaces.

The "clearly separated process" model characteristic of Unix has potent merits when dealing with software that might be unreliable to the point of being unsafe, as is the case with code written in C or C++ , where an invalid pointer access can "take down the system." MS-DOS and its heirs are very unreliable in that sense, where just about any program bug can take the whole system down; "Blue Screen of Death" and the likes.

If the whole system is constructed and coded in Lisp, the system is as reliable as the Lisp environment. Typically this is quite safe, as once you get to the standards-compliant layers, they are quite reliable, and don't offer direct pointer access that would allow the system to self-destruct.

Third Law of Sane Personal Computing

Volatile storage devices (i.e. RAM) shall serve exclusively as read/write cache for non-volatile storage devices. From the perspective of all software except for the operating system, the machine must present a single address space which can be considered non-volatile. No computer system obeys this law which takes longer to fully recover its state from a disruption of its power source than an electric lamp would.

如前所述,单个地址空间将所有正在运行的进程保存在同一内存空间中。我很好奇为什么人们坚持认为单一地址空间更好。我将它与 Lisp 的类 AST 语法联系起来,试图解释它如何适合单空间模型。

最佳答案

您的问题并没有非常准确地反射(reflect)现实,尤其是在 Linux 和其他操作系统中有关代码/数据分离的部分。实际上,这种分离不是在操作系统级别强制执行的,而是由编译器/程序加载器强制执行的。在操作系统级别,只有内存页面可以设置不同的保护位(如可执行、只读等),在这个级别之上存在不同的可执行格式(如 Linux 中的 ELF),它们指定对程序内存不同部分的限制。

回到 Lisp,据我所知,历史上,Lisp 的创建者使用 S-expression 格式,因为他们想专注于语言的语义,将语法搁置一段时间。有一个计划最终为 Lisp 创建一些语法(参见 M 表达式),并且有一些基于 Lisp 的语言有更多的语法,比如 Dylan。但是,总的来说,Lisp 社区已经达成共识,即 S 表达式的好处超过了它们的缺点,因此他们坚持了下来。

关于代码作为数据,这并不严格绑定(bind)到S表达式,因为其他代码也可以被视为数据。整个方法称为元编程,许多语言在不同级别和不同机制上支持它。每一种支持 eval 的语言(Perl、JavaScript、Python)都允许将代码视为数据,只是表示几乎总是一个字符串,而在 Lisp 中它是一棵树,这要方便得多并促进高级的东西,比如宏。

关于lisp - Lisp 语法对 AST 建模的目的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11322922/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com