MIT License

技术解读

以下内容由 GitRead 中文社区 编辑团队基于项目 README 与官方资料整理, 结合中文开发者的常见使用场景补充技术选型建议。具体以项目官方文档为准。

深度技术分析:Visual Studio Code Extension for Coq

项目背景与痛点

Coq 是一个交互式的定理证明助手,它可以帮助用户构造数学上和编程上的证明。不过,纯 Coq 环境往往对用户不太友好。rocq-prover/vsrocq 扩展则为 Visual Studio Code 提供了一个无缝整合的环境,让开发者在熟悉的编辑器中高效地进行 Coq 开发。

VSCode 是一个非常流行且功能丰富的代码编辑器,它广泛适用于多种编程语言。而 Coq 的用户则主要集中在理论计算机科学和形式化验证领域。这个扩展就是为了应对这两者之间的差距而生的。通过该扩展,用户可以在 Visual Studio Code 中直接使用 Coq,享受包括语法高亮、智能补全等功能在内的全方位支持,总体提升开发效率和体验。

核心技术揭秘

vsrocq 扩展核心技术在于它如何高效地与 Coq 交互,并将 Coq 的复杂功能转换为 VSCode 插件中的便捷操作。从技术栈上看,该扩展主要依赖于 VSCode 提供的 API,这样可以方便地集成进编辑器的工作流,并利用 VSCode 提供的丰富的扩展生态。

我们来分析一下重难点的技术实现:

  1. 语法高亮、智能补全: 通过扫描 Coq 的数据文件,解析其语法结构,生成相应的语法高亮规则,并且基于这些规则实现更智能的补全功能。

  2. 交互式定理证明: Coq 的一大特性是它可以进行交互式定理证明。插件通过暴露 Coq 的证明流程的各种命令和步骤,让开发者能够直接在编辑器上进行步进式的证明,这对整个证明过程的效率有着极大的提升。

  3. 错误提示与修复: 通过与 Coq 服务器的通讯,插件可以实时向用户展示错误信息,并在可能的情况下提供修复建议,这归功于插件对 Coq 语言的深度解析能力。

以下是一个简化的架构图,展示了 vsrocq 在 VSCode 中与 Coq 的互动流程:

graph TD A[用户代码输入] --> B{"语法高亮"} B -->|"解析Coq语法"| C[VSCode API] B -->|"提示与错误"| D[用户反馈] C --> E[传输给Coq服务器] E --> F{"Coq执行"} F -->|"反馈执行结果"| G[VSCode] G -->|"展示状态、错误"| D

功能亮点与差异

与其他同类插件相比,vsrocq 最突出的特点是其高度集成性与灵活性。对于开发者来说,能够在熟悉的 VSCode 环境中直接运行 Coq,而不必在多个工具之间切换。并且,该插件提供了丰富的 Coq 交互功能,进一步提高了开发者编写证明代码的效率。

唯一的缺点是它对 Coq 的某些高级功能支持相对较弱,比如一些非标准的证明助手和机制。

应用场景与落地建议

此扩展适用于各种需要进行形式化验证或定理证明的场景。无论是学术研究领域的数学证明,还是工业领域对程序逻辑的验证,VSCode 中内置的 Coq 支持都能提供高效的解决方案。

开发者在使用时需要注意的是,插件的性能会受到 Coq 运行速度的影响,这主要是 Coq 本身的计算复杂度决定的。因此在大型或复杂的证明任务时,需要合理安排资源。

综合评价

vsrocq 扩展提供了一种崭新的体验方式,支持用户在 Visual Studio Code 这样的现代化编辑器中进行 Coq 的开发。对于需要使用 Coq 的开发者来说,它是不可多得的利器,极大提高了开发效率。同时,它在技术实现上的精巧设计也值得学习研究。

关于

GitHub 原始描述(英文)

Visual Studio Code extension for Coq

描述取自 GitHub 官方 API,保持仓库作者书写的原文。

简要分析

热度分
708
价值分
305
活跃状态
活跃

语言占比

Batchfile
CSS
Dune
HTML
Makefile
Nix
OCaml
Perl
Raku
Rocq Prover
Shell
TypeScript

Release

README

CI Zulip

VsRocq is an extension for Visual Studio Code (VS Code) and VSCodium which provides support for the Rocq Interactive Theorem Prover. It is built around a language server which natively speaks the LSP protocol.

Supported Rocq versions

VsRocq supports all recent Rocq/Coq versions >= 8.18. If you are running an Coq < 8.18 you should use VsCoq Legacy.

Installing VsRocq

To use VsRocq, you need to:

  1. Install the VsRocq language server, and
  2. install and configure the VsRocq extension, in either VS Code or VSCodium.

The VS Code extension and the opam package vsrocq-language-server are installed and updated separately, and they may stop working if their versions don’t match. To check which versions you have, run Rocq: Troubleshooting: Show setup from the command palette (F1). See the troubleshooting section in case of problems.

Installing the language server

Installation (Opam)

After creating an opam switch, pin Rocq, and install the vsrocq-language-server package:

$ opam pin add rocq-core 9.1.0 # replace with correct version
# For Coq 8.x: use coq / coq-core packages instead of rocq-core
$ opam install vsrocq-language-server
# For a specific version, e.g. v2.3.4: opam install vsrocq-language-server.2.3.4

Installation (nixos)

nix profile install nixpkgs#coq_8_18 nixpkgs#coqPackages_8_18.vscoq-language-server

Verify installation

After the previous steps, check that you have vsrocqtop in your shell:

$ which vsrocqtop
Pre-release versions

We often roll out pre-release versions. To get the correct language server version please pin the git repository. For example, for pre-release v2.3.1:

$ opam pin add vsrocq-language-server.2.3.1  https://github.com/rocq-prover/vsrocq/releases/download/v2.3.1/vsrocq-language-server-2.3.1.tar.gz

When pinning a pre-release language server, ensure you also select the same VsRocq extension version in VS Code.

Using a Local Version of Rocq

See the developers documentation.

Installing and configuring the extension

Install VsRocq from the VS Code Marketplace or, for VSCodium, from Open VSX. In the Extensions view, search for “vsrocq” and pick the one published by rocq-prover. If you haven’t installed an extension before, see the VS Code guide on installing extensions.

By default, VsRocq uses the vsrocqtop found in your PATH, so no further configuration is needed. If VsRocq can’t find it, see Language server not found.

Pre-release versions

To install a pre-release version or a specific version of the extension, see the VS Code guide. Make sure you also install the same language server version.

Features

  • Syntax highlighting
  • Asynchronous proof checking
  • Continuous and incremental checking of Rocq documents

Vsrocq allows users to opt for continuous checking, see the goal panel update as you scroll or edit your document.

By default, vsrocq is configured to use classic step by step checking mode. To switch between the two, change the “Proof: Mode” setting (vsrocq.proof.mode).

  • Customisable goal panel

Users can choose their preferred display mode, see goals in accordion lists…

… Or organized in tabs.

  • Dedicated panel for queries and their history

We now support a dedicated panel for queries. We currently support Search, Check, About, Locate and Print with plans to add more in the future.

  • Messages in the goal panel

We also support inline queries which then trigger messages in the goal panel.

  • Supports _RocqProject/_CoqProject

Since version 2.1.7

  • Outline

We now support a document outline, which displays theorems and definitions in the document.

  • Ellipsis for the goal panel

Goals can now be ellided. First through the "vsrocq.goals.maxDepth" setting which ellides a goal if the display becomes to large. Finally by clicking on the goal view as showcased here. The following modifiers can be used:

  • Alt + Click: open/close an ellipsis (only opens partially).
  • Shift + Alt + Click: fully open an ellipsis (all children are also opened).

  • Quickfixes (only for Rocq/Coq >= 8.21)

We have added support for quickfixes. However, quickfixes rely on some Rocq API which will only make it in the 8.21 release. Developpers are encouraged to list and add their own quickfixes in the Rocq source code.

  • Block on first error

We support the classic block on error mode, in which the execution of a document is halted upon reaching an error. This affects both checking modes (Continuous and Manual). A user can opt out of this execution mode by setting it to false in the user settings.

Settings

After installation and activation of the extension:

(Press F1 and start typing “settings” to open either workspace/project or user settings.)

Rocq configuration
  • "vsrocq.path": "" – specify the path to vsrocqtop (e.g. path/to/vsrocq/bin/vsrocqtop)
  • "vsrocq.args": [] – an array of strings specifying additional command line arguments for vsrocqtop (typically accepts the same flags as rocqtop)
  • "vsrocq-language-server.trace.server": off | messages | compact | verbose – Toggles the tracing of communications between the server and client (defaults to off)
Memory management (since >= 2.1.7)
  • "vsrocq.memory.limit": int – specifies how much memory (in Gb) the server can use before it discards the execution state of closed documents. The check runs each time a document is closed. Defaults to 4Gb.
Interrupts
  • "vsrocq.interrupt.preempt": bool – Lets hovering and other queries preempt checking tasks (defaults to false; recommended for Rocq >= 9.3)
Goal and info view panel
  • "vsrocq.goals.auto": bool – A toggle to automatically display the goal view when navigating a Rocq document (defaults to true)
  • "vsrocq.goals.display": Tabs | List – Decide whether to display goals in separate tabs or as a list of collapsibles (defaults to List)
  • "vsrocq.goals.messages.full": bool – A toggle to include warnings and errors in the proof view (defaults to true)
  • "vsrocq.goals.maxDepth": int – A setting to determine at which point the goal display starts elliding. Defaults to 17. (since version >= 2.1.7)
Proof checking
  • "vsrocq.proof.mode": 0 | 1 – Decide whether documents should be checked using the classic navigation commands (0, Manual) or continuously (1, Continuous). Defaults to 0.
  • "vsrocq.proof.pointInterpretationMode": 0 | 1 – Determines the point to which the proof should be checked when using the ‘Interpret to point’ command: up to the last command before the cursor (0, Cursor) or through the command after the cursor (1, NextCommand). Defaults to 0.
  • "vsrocq.proof.cursor.sticky": bool – a toggle to specify whether the cursor should move as Rocq interactively navigates a document (step forward, backward, etc…). Defaults to true.
  • "vsrocq.proof.delegation": None | Skip | Delegate – Decides which delegation strategy should be used by the server (defaults to None). Skip allows to skip proofs which are out of focus. Delegate allocates a settable amount of workers to delegate proofs.
  • "vsrocq.proof.workers": int – Determines how many workers should be used for proof checking in Delegate mode (defaults to 1)
  • "vsrocq.proof.block": bool – Determines if the execution of a document should halt on the first error. Defaults to true (since version >= 2.1.7).
  • "vsrocq.proof.display-buttons": bool – A toggle to control whether buttons related to Rocq (step forward/back, reset, etc.) are displayed in the editor actions menu (defaults to true)
Code completion (experimental)
  • "vsrocq.completion.enable": bool – Toggle code completion (defaults to false)
  • "vsrocq.completion.algorithm": 0 | 1 – Which completion ranking algorithm to use: 0 (SplitTypeIntersection) or 1 (StructuredSplitUnification). Defaults to 1.
  • "vsrocq.completion.unificationLimit": int – Sets the limit for how many theorems unification is attempted (defaults to 100)
Diagnostics
  • "vsrocq.diagnostics.full": bool – Toggles the printing of Info level diagnostics (defaults to false)

Troubleshooting

This section covers the most common setup problems. For other questions, see the FAQ.

Diagnosing problems

VsRocq includes two commands to help you find out what is wrong. Run them from the command palette (F1):

  • Rocq: Troubleshooting: Show setup shows the versions and paths of your VsRocq extension, language server (vsrocqtop), Rocq, OS, and VS Code, with a button to copy them to the clipboard.
  • Rocq: Troubleshooting: Show Log Output opens the VsRocq output channel, where the extension reports problems with finding and starting the language server.

Both commands work even if the language server didn’t start. In that case, Show setup shows the language server version as “not available”.

Server and extension versions mismatch

The VS Code extension and the opam package vsrocq-language-server are installed and updated separately, and they may stop working if their versions don’t match. VS Code updates extensions automatically, so a mismatch can appear without you changing anything.

Typical symptoms:

  • An error saying “This version of VsRocq requires version X of vsrocq-language-server”.
  • Commands such as “Interpret to point” seem to do nothing.

To check your versions, run Rocq: Troubleshooting: Show setup (see Diagnosing problems). It shows the extension, language server, and Rocq versions together. If the language server version shows as “not available”, check the versions installed in your current opam switch:

$ opam list vsrocq-language-server rocq-core coq-core

If they differ, install the same version of both: either upgrade the language server (opam update && opam upgrade vsrocq-language-server), or install the extension version that matches your language server.

Language server not found

If VsRocq shows a “No language server found” error, it couldn’t find vsrocqtop. The VsRocq output channel shows [Toolchain] Did not find vsrocqtop path.

First, run which vsrocqtop in a terminal:

  • It prints nothing: the language server isn’t installed in your current opam switch, or the opam environment isn’t loaded in that terminal. Run eval $(opam env) and try again; if it still prints nothing, see Installing the language server.
  • It prints a path: VS Code isn’t seeing the same PATH as your terminal. This happens, for example, when VS Code is started from the desktop instead of a terminal where the opam environment is loaded. To fix it, either:
    • start VS Code from that terminal (code .), or
    • set the “Vsrocq: Path” setting (vsrocq.path) to the path printed by which vsrocqtop.

After changing vsrocq.path, run Developer: Reload Window from the command palette (F1). VsRocq only looks for vsrocqtop when it starts. If you change your opam switch instead, fully quit VS Code and start it again, so that it picks up the new PATH.

Getting help

If you can’t set up VsRocq, feel free to ask on the VsRocq Devs and Users channel in Zulip. To report a bug, open an issue.

In both cases, include the output of Rocq: Troubleshooting: Show setup (see Diagnosing problems).

For extension developers

See Dev docs

Maintainers

This extension is currently developed and maintained by Enrico Tassi, Romain Tetley.

License

Unless mentioned otherwise, files in this repository are distributed under the MIT License.

The following files are also distributed under the MIT License, Copyright © Christian J. Bell and contributors:

  • client/syntax/rocq.tmLanguage.json
  • client/syntax/rocq.language-configuration.json

评论

暂无评论