如何从命令行/其他语言使用 Agda?

How to use Agda from the command line / from another language?

大多数与 Agda 的交互都是通过 EMACS 完成的,但是有没有办法以编程方式进行呢?即,是否可以从命令行或某些 API 执行所有操作?主要目标是构建一个薄包装器,以便我们可以从另一种语言调用 Agda,例如:

var Agda = require("agda");

var code = `
    data Bool: Set where
        true: Bool
        false: Bool

    not : Bool -> Bool
    not true = false
    not false = true

    val : Bool
    val = not true
`;

console.log(Agda.infer(code, "true")); // prints "Bool"
console.log(Agda.normalize(code, "val")); // prints "false"

我之前问过如何使用 Agda as a library,但那显然只涵盖了 Haskell。我已经尝试查看 Agda 的 VIM 扩展以了解它是如何做到的,它似乎正在向 Agda 发送命令,但我不确定具体是如何做到的。非常感谢指向相关文档的指针!

据我所知,目前(在 master 分支上)有两种从命令行与 Agda 交互的方式:

  1. Emacs 的原始后端 agda --interaction
  2. 基于 JSON 的新后端 agda --interaction-json

Emacs 后端

  • Emacs 会将格式为 Haskell 数据类型 的消息发送到 Agda (easy)
  • Agda 将以 Emacs Lisp 的形式回复 Emacs 消费 (hard)

如您所见,此后端专为 Emacs 设计。 需要一些逆向工程才能弄清楚他们在互相交谈什么。

我做到了some notes about the Emacs protocol when I was implementing agda-mode on Atom。但恐怕在撰写本文时已经偏离了实际的实现。

以下是 Agda 源代码的一些相关部分,如果您想与 Emacs 后端交互,您可能会发现它们很有用:


JSON 后端

不用说,使用 Emacs 协议很痛苦。
所以我设法在新后端用 JSON 替换了 Emacs Lisp。

  • 您仍然需要像在 Emacs
  • 中一样将格式为 Haskell 数据类型 的消息发送到 Agda
  • Agda 将在 JSON
  • 中回复

现在,您不必处理 Emacs Lisp 的 S 表达式。 就是这样 responses are encoded as JSON


然而,有效载荷仍然被序列化为字符串,这使得从 Agda 中提取有用的信息变得困难。所以我仍在 JSON 中 the json branch, trying to encode the payload 工作。