lisp - SBCL 中 values-list 的实现在哪里?

标签 lisp common-lisp sbcl

我正在尝试找出 SBCL 中 values-list 函数的实现。 src/code/eval.lisp 的源代码看起来很简单,但为什么不陷入无限递归呢?

(defun values-list (list)
  #!+sb-doc
  "Return all of the elements of LIST, in order, as values."
  (values-list list))

如果我这样定义一个函数foo,它会陷入无限递归:

(defun foo (list)
   (foo list))

最佳答案

简而言之,因为其他东西已经教会了编译器如何编译对(values-list)的调用,所以在函数体不引用正在定义的函数。

函数定义存在的原因有点棘手。在直接调用该函数的编译代码中,编译器可以仅遵从任何其他代码来处理对该函数的编译调用。然而,仅仅因为编译器知道如何编译对 (values-list) 的直接调用并不意味着它是一个函数——实际上,它可能只是一个特殊的形式或宏。这个看似递归的定义确保了一个实际的函数对象存在,所以像 #'values-list 和在 repl 调用函数(如果 repl 被设置为使用解释器)的东西按预期工作。

在这种情况下,#'values-list 是在每个 SBCL 的目标平台上使用虚拟操作(VOP,编译器可以直接编译为机器指令的东西)实现的,如您所见用search through the source .

看看here如果您想了解更多相关信息。在那篇文章中,Paul Khoung 添加了对 SBCL 的支持,以使用 intel x86-64 popcnt 指令编译对新函数 #'popcnt 的调用,作为最后一步,他定义了这样一个函数

(in-package "POPCNT")

(defun popcnt (x) (popcnt x))

他注意到

isn’t strictly necessary, but helps people who like their REPL. The compiler knows how to compile calls to popcnt, so we can define popcnt… as a call to popcnt. Our new function is now a first-class value that can be called from interpreted code and passed to higher-order functions, like the compiler’s constant-folding pass.

关于lisp - SBCL 中 values-list 的实现在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43056782/

相关文章:

lisp - 使用 SETF 函数扩展 SETF 是如何工作的?

c# - 将一些 LISP 转换为 C#

list - 我定义的 cond 函数无法正常工作 (LISP)

macros - 试图重写一个丑陋的宏

windows - "Your Kitten of Death awaits"从哪里来?

lisp - 整数值不是 Common Lisp 中的数字?

lisp - lisp 中 "#'"的行为

generics - 专门针对关键字的泛型函数

lisp - 劣质壳实时打印输出