FEXPRs vs. vtable:LispE 解释器的工作原理

Lobsters Hottest 工具

摘要

LispE 是 NAVER 开发的一个紧凑的 Lisp 方言,结合了函数式和数组语言特性,提供了针对 PyTorch、GGUF、MLX 和 tiktoken 的 AI 库,并附带一个基于浏览器的测试环境。

<p><a href="https://lobste.rs/s/leh6g3/fexprs_vs_vtable_how_lispe_interpreter">评论</a></p>
查看原文
查看缓存全文

缓存时间: 2026/06/23 13:46

naver/lispe

来源: https://github.com/naver/lispe

LispE: Lisp Elémentaire

(在浏览器中测试 LispE 请点击: 此处 (https://naver.github.io/lispe))

欢迎使用 Lisp Elémentaire,这是一个既紧凑又提供大量函数式和数组语言特性的 Lisp 版本。该代码还附带了一个来自 NAVER 另一个项目 TAMGU (https://github.com/naver/tamgu) 的小型内部编辑器。

LispE 的主要目标是提供一种跨平台语言,能够利用函数式语言和数组语言的能力。 Lisp 语言(LispE 是其方言之一)的真正优势在于其非常简单但相当通用的形式主义,这有助于将所有这些编程趋势结合到一种单一语言中。

我基于以下文章完成了大部分工作:The Root of Lisp (http://www.paulgraham.com/rootsoflisp.html)。

  • 语言描述可在此处获取:Introduction to LispE (https://github.com/naver/lispe/wiki/1.-Introduction)
  • LispE 提供了大量的函数,请参阅索引:Index of functions (https://github.com/naver/lispe/wiki/Index-of-functions)
  • 关于可用函数的帮助在此:LispE Language Description (https://github.com/naver/lispe/wiki/5.-Description-of-Functions,-Operators-and-Libraries)
  • Wiki 索引在此:HOME (https://github.com/naver/lispe/wiki)
  • 人工智能库:
    • lispe_torch (https://github.com/naver/lispe/tree/master/lispetorch):封装 PyTorch 内部 libtorch 库和 SentencePiece
    • lispe_gguf (https://github.com/naver/lispe/tree/master/lispegguf):基于 llama.cpp,加载和运行 gguf 模型
    • lispe_mlx (https://github.com/naver/lispe/tree/master/lispemlx):基于 MLX 库以访问 Mac GPU
    • lispe_tiktoken (https://github.com/naver/lispe/tree/master/lispetiktoken):基于 Open AI 的 tiktoken 库进行分词

在浏览器中测试 LispE

由于 LispE 也是 一个 WASM 库:你可以在此处测试 LispE (https://naver.github.io/lispe)。

另见:浏览器中的基于谓词的智能体 (PREDIBAG) (https://github.com/naver/lispe/wiki/6.1.5-PREDIBAG-in-the-Browser)

查看二进制文件

我们在此处 (https://github.com/naver/lispe/tree/master/binaries) 存放了 Windows 和 Mac OS(包括 M1)的预编译版本…

功能齐全的 Lisp

LispE 是一个真正的 Lisp,具备人们期望从这类语言中得到的所有传统操作符:

``Lisp (cons ’a ’(b c)) ; (a b c) (cdr ’(a b c d e)) ; ’(b c d e) (car ’(a b c d e)) ; ’a

(+ 10 20 (* 3 4)) ; 42

(list ’a ’b ’c ’d ’e) ; (a b c d e)

; 它还提供了一些内置类型来处理数值

(setq l1 (integers 1 2 3)) (setq l2 (integers 4 5 6))

; 我们可以将这些列表相加 (+ l1 l2) ; 5 7 9

; 或者字符串

(setq s1 (strings “a” “b” “c”)) (setq s2 (strings “d” “e” “f”))

; 我们可以将这些列表相加 (+ s1 s2) ; (“ad” “be” “cf”)

; 列表被视为向量

(@ s1 1) ; “b” (@ l1 2) ; 3

; 你可以遍历列表 (loop i s1 (println i)) ``

你可以轻松运行线程

``Lisp ; threadspace 中的变量受保护 ; 免于值并发问题 (threadspace (seth titi 10) (seth toto (rho 2 10 ’(0))) )

; 使用关键字 dethread 声明线程 (dethread tst(x y) (threadspace (+= titi x) (set@ toto 0 y titi) ) )

; 启动 10 个线程 (loop i (range 1 10 1) (tst (+ i 10) i) )

; 等待线程终止 (wait)

; threadspace 实际上是一个特定的命名空间 (space thread (println titi) ; 145 ; 注意,值的顺序是随机的,因为线程执行 (println toto) ; ((0 21 46 34 60 75 108 92 126 145) (0 0 0 0 0 0 0 0 0 0)) )

``

现代函数式特性

LispE 提供了圆括号的替代方案:组合运算符 “.” (https://github.com/naver/lispe/wiki/5.-Description-of-Functions,-Operators-and-Libraries#composition-):

Lisp (sum (numbers 1 2 3)) 可以写作 (sum . numbers 1 2 3)

LispE 提供了一个强大的内置模式匹配 (https://github.com/naver/lispe/wiki/6.1-Pattern-Functions) 机制:

``Lisp ; 你可以在函数的模式定义中调用函数 (defun checking (x y) (eq 0 (% y x)))

; 参数应为整数,其值必须是 15 的倍数 ; 参数存储在 x 中 (defpat fizzbuzz ((integer_ (checking 15 x))) ’fizzbuzz) (defpat fizzbuzz ((integer_ (checking 3 x))) ’fizz) (defpat fizzbuzz ((integer_ (checking 5 x))) ’buzz) (defpat fizzbuzz (x) x) (mapcar ’fizzbuzz (range 1 100 1)) ``

LispE 还提供了一些有趣的特性,例如:数据结构 (https://github.com/naver/lispe/wiki/6.7-Data-Structures)

``Lisp ; 首先定义一些数据结构 ; nil 或 _ 是相同的值

(data@ (Point integer_ integer_) (Pixel _ _) (Circle (Point _ _) _) (Rectangle (Point _ _) _ nil))

; 然后是一些模式方法 (defpat Surface ((Circle (Point x y) r)) (* _pi r r)) (defpat Surface ((Rectangle _ h w)) (* h w)) (defpat Surface (true) ’wrong)

(println “Circle:” (Surface (Circle (Point 12 32) 10))) (println “Rectangle:” (Surface (Rectangle (Point 21 20) 10 20)))

(setq x 10) (setq y 20) (setq r 12)

(setq cercle (Circle (Point x y) r))

(println “Circle:” (Surface cercle)) (println “Point:” (Surface (Point 10 20))) (println “Circle:” (Surface ((atom “Circle”) (Point 20 30) 3)))

(data@ Shape (Triangle _) (Square _))

(defpat dimension ( (Shape x)) (println ’Dimension x) ) (dimension (Triangle 10)) (dimension (Square 20)) ``

LispE 还提供了一些面向对象特性,例如:类 (https://github.com/naver/lispe/wiki/6.21-Classes)

``Lisp ; 定义名为 ‘truc’ 的类,带有字段 x、y 和 z (class@ truc (x y z)

(defun appel(u) (setq x 100) (println x y z) (+ x y z u) ) )

; 定义名为 ‘machin’ 的类,带有字段 x、y 和 z(默认值为 1) (class@ machin (x y (z 1)) ; 定义接受一个参数 ‘u’ 的方法 ‘appel’ (defun appel(u) (println ’machin)(+ x y z u))

; 定义接受一个参数 'e' 的方法 'configure'
(defun configure(e)
    (setqi x e)
    (setqi v 100))) ; 添加新字段:`v`

``

同时具备数组语言能力

得益于用数组实现的内部结构 (https://github.com/naver/lispe/wiki/2.3-Lists),我们还提供了一些数组操作符。

例如,这里展示了如何用一条指令解决 Game of Life (https://github.com/naver/lispe/wiki/6.20-Conway-Game-of-Life-in-LispE):

``Lisp (defmacro ⊖(l n) (rotate l n true))

(defun lifeinit(x y) (rho x y . random_choice (* x y) (integers 0 1) 17))

(setq r (lifeinit 20 40)) (defun gol8(⍵) ((λ(⍺) (| (& (== ⍺ 4) r) (== ⍺ 3))) (⌿ ’+ (⌿ ’+ (° (λ (x ⍺) (⊖ ⍺ x)) ’(1 0 -1) (↑ (λ (x) (⌽ ⍵ x)) ’(1 0 -1)))))))

(println . prettify (gol8 r) 20) ``

参见:Array Operators (https://github.com/naver/lispe/wiki/5.3-A-la-APL)

尝试使用

最后,LispE 还可以用作 Shell:Shell (https://github.com/naver/lispe/wiki/7.-Shell)

快来发现 LispELisp Elémentaire

我们提供了一个有趣的小程序,用于发现 LispE 最引人入胜的一些方面:minizork (https://github.com/naver/lispe/blob/master/examples/patterns/minizork_en.lisp)

来看看并试试它

许可证

BSD 3-Clause License

`` LispE Copyright (c) 2020-present NAVER Corp.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

  3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ``

相似文章

将 Python 转译为 Lisp

Lobsters Hottest

LispE 是 NAVER 推出的一款开源 Lisp 方言,兼具函数式与数组编程特性,并支持 PyTorch、llama.cpp 以及 MLX 等 AI 库。该语言既可作为原生应用运行,也可打包为支持多线程与现代函数式编程特性的 WebAssembly 库。

JavaScript 精简

Lobsters Hottest

LispE 是 NAVER 开发的一个紧凑的 Lisp 方言,它结合了函数式和数组语言特性,并支持 PyTorch 和 llama.cpp 等 AI 库。

Rust类型系统中的Lisp

Hacker News Top

一个嵌入在Rust trait系统中的Lisp解释器,支持在编译时进行递归函数、闭包和延续传递风格。