将 Python 转译为 Lisp

Lobsters Hottest 工具

摘要

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

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

缓存时间: 2026/04/21 02:58

naver/lispe

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

LispE: Lisp Elémentaire

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

欢迎使用 Lisp Elémentaire,这是一种紧凑且提供丰富函数式与数组语言特性的 Lisp 版本。该代码还集成了一款来自 NAVER 旗下另一项目 TAMGU 的内部小型编辑器:TAMGU (https://github.com/naver/tamgu)。

LispE 的主要目标是打造一款跨平台语言,能够将函数式语言与数组语言的能力相结合。作为 LispE 所属的 Lisp 语言,其真正的优势在于极其简洁却高度通用的形式化表达,有助于将各类编程范式统一于单一语言之中。

本项目的开发很大程度上参考了以下文章:《Lisp 之源》(The Root of Lisp) (http://www.paulgraham.com/rootsoflisp.html)。

  • 语言描述请参阅此处:Introduction to LispE (https://github.com/naver/lispe/wiki/1.-Introduction)
  • LispE 提供了大量内置函数,索引请见此处 (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 HERE (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 和 macOS(含 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 (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 还支持部分面向对象编程 (OOP) 特性,如类定义 (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. ``

相似文章

JavaScript 精简

Lobsters Hottest

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

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

Lobsters Hottest

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

Slisp: 简单的Lisp编译器 (Linux/amd64)

Hacker News Top

Slisp 是一个简单的编译器,它读取 Lisp 程序并生成适用于 Linux/AMD64 的独立汇编表示,支持基本原语、闭包和标准库。