• Stars
    star
    157
  • Rank 238,399 (Top 5 %)
  • Language
    TypeScript
  • License
    Apache License 2.0
  • Created almost 4 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Visual Studio Code extension for the Lean 4 proof assistant

Lean 4 VSCode Extension

This extension provides VSCode editor support for the Lean 4 programming language. It uses the all new Language Server implemented in Lean.

See Quick Start Demo Video.

Installing the extension and Lean 4

  1. Install the extension from the marketplace.

  2. Create a new folder called foo and add a file named hello.lean containing the following:

    import Lake
    #eval Lake.versionString
  3. Open this folder in VS Code using File/Open Folder.

  4. Open your file hello.lean.

  5. If Lean is not yet installed on your system you will see a prompt like this:

    prompt

  6. Click the Install Lean option and enter any default options that appear in the terminal window.

  7. After this succeeds the correct version of Lean will be installed by elan and you should see something like this in the Lean: Editor output panel:

    info: downloading component 'lean'
    info: installing component 'lean'
    Lean (version 4.0.0, commit ec941735c80d, Release)
    

See Complete Setup for more information on how the lean version is determined for your VS Code workspace.

This version of the VS Code extension only works on Lean 4 source files and not Lean 3. There is a separate VS Code extension for Lean 3. You can have both extensions installed at the same time, they can live side by side.

Lake integration

Note that once the Lean toolchain is installed you can also turn your folder into a Lake project. Lake is a build system for Lean projects. The VS code extension will honor the Lean version specified in your lean-toolchain file.

Open a VS Code Terminal window in your foo folder and type the following:

lake init foo
lake build

This will add some package definition files to your project along with a Main.lean entry point and build an executable program. You can run the program ./build/bin/foo and you will see the expected output:

Hello, world!

Features

The extension supports the following features. For basic VS Code editor features, see the VS Code User Interface docs.

The extension provides:

  • A set of handy Lean4: commands available with Ctrl+Shift+P
  • Side-by-side compatibility with the Lean 3 VSCode extension
  • Nice integration with the Lean Language Server as shown below.
  • An Infoview panel showing interactive information about your Lean programs.

Lean language server features

  • Automatic installation of Lean using elan.

  • Incremental compilation and checking via the Lean server (*)

  • Type information & documentation on hover

  • Error messages and diagnostics

  • Syntax highlighting with basic Lean 4 syntax rules

  • Semantic highlighting

  • Hover shows documentation, types:

    hover_example

  • Auto-completion drop downs based on context and type via the Lean Server. For example, if you type "." after Array you will get:

    completion-example

  • An Infoview displaying useful information about your current Lean program.

  • Goto definition (F12) will work if the Lean source code is available. A standard elan install of the Lean toolchain provides Lean source code for all the Lean standard library, so you can go to the definition of any standard library symbol which is very useful when you want to learn more about how the library works. Note also that currently goto definition does not work if the cursor is at the very end of a symbol like Array.append|. Move the cursor one character to the left Array.appen|d and it will work. See open issue #767.

  • Breadcrumbs

(*) Incremental updates do not yet work automatically across files, so after changing and rebuilding the dependency of a Lean 4 file, the language server needs to be manually informed that it should re-elaborate the full file, including the imports. This can be done using the Lean 4: Refresh File Dependencies command, which can be activated via Ctrl+Shift+X.

Lean editing features

  • Support for completing abbreviations starting with a backslash (\). For example you type \alpha and the editor automatically replaces that with the nice Unicode character α. You can disable this feature using the lean4.input.enabled setting, see below.
  • When you hover the mouse over a letter that has one or more abbreviations you will see a tooltip like this: abbreviation tip
  • Auto-completing of brackets like (), {}, [], ⟦ ⟧, ⦃ ⦄, ⟮ ⟯, ⦃ ⦄ and block comments /- ... -/.

Infoview panel

The Infoview panel is essential to working interactively with Lean. It shows:

  • Tactic state widgets, with context information (hypotheses, goals) at each point in a proof / definition,
  • Expected type widgets display the context for subterms.
  • Types of sub-terms in the context can be inspected interactively using mouse hover.
  • All Messages widget, which shows all info, warning, and error messages from the Lean server for the current file.

Suppose you have the following theorem:

theorem test (p q : Prop) (hp : p) (hq : q) : p ∧ q ∧ p :=
  by apply And.intro
     exact hp
     apply And.intro
     exact hq
     exact hp

and you place the cursor at the end of the line by apply And.intro the Infoview will display the following information:

completion-example

(1). The Infoview will activate automatically when a Lean file is opened, but you can also reopen it any time using the icon in the top right of the text editor window or the Ctrl+Shift+P Lean 4: Infoview: Display Goal command or the key that is bound to the command, which is Ctrl+Shift+Enter by default. You can also disable auto-opening behavior using the setting lean4.infoViewAutoOpen described below.

(2) through (6):

Symbol Description
quotes Copy the contents of the widget to a comment in the active text editor.
pin Split off a "pinned" tactic state widget, which tracks the tactic state at a fixed position, even if you move your cursor away. When pinned you will see the unpin and reveal file location icons appear.
unpin Remove a pinned widget from the Infoview.
reveal Move the cursor in the editor to the pinned location in the file.
pause Prevent the tactic state widget from updating when the file is edited. When paused you will see the following addition icons show up.
continue Once paused you can then click this icon to resume updates.
update Refresh the tactic state of the pinned widget.
sort Reverse the order of the lists under Tactic State.

(7). Types in the context can be examined in the tactic state widget using mouse hover:

inspect-term-example

(8). The "All Messages" widget can be expanded by clicking on it (or hitting the keybind for lean4.displayList, which is Ctrl+Shift+Alt by default)

Extension Settings

This extension contributes the following settings (for a complete list, open the VS Code Settings and type Lean4 in the search box):

Server settings

  • lean4.toolchainPath: specifies the location of the Lean toolchain to be used when starting the Lean language server. Most users (i.e. those using elan) should not ever need to change this. If you are bundling Lean and vscode-lean with Portable mode VS Code, you might find it useful to specify a relative path to Lean. This can be done by starting this setting string with %extensionPath%; the extension will replace this with the absolute path of the extension folder. For example, with the default directory setup in Portable mode, %extensionPath%/../../../lean will point to lean in the same folder as the VS Code executable / application.

  • lean4.lakePath: specifies the location of the Lake executable to be used when starting the Lean language server (when possible). If left unspecified, the extension defaults to the Lake executable bundled with the Lean toolchain. Most users thus do not need to use this setting. It is only really helpful if you are building a Lake executable from the source and wish to use it with this extension.

  • lean4.serverEnv: specifies any Environment variables to add to the Lean 4 language server environment. Note that when opening a remote folder using VS Code the Lean 4 language server will be running on that remote machine.

  • lean4.serverEnvPaths: specifies any additional paths to add to the Lean 4 language server environment PATH variable.

  • lean4.serverArgs: specifies any additional arguments to pass on the lean --server command line.

  • lean4.serverLogging.enabled: specifies whether to do additional logging of commands sent to the Lean 4 language server. The default is false.

  • lean4.serverLogging.path: if serverLogging.enabled is true this provides the name of the relative path to the store the logs.

  • lean4.autofocusOutput: if true, automatically show the Output panel when the Lean 4 server prints a new message.

  • lean4.elaborationDelay: Time (in milliseconds) which must pass since latest edit until elaboration begins. Lower values may make editing feel faster at the cost of higher CPU usage. The default is 200.

Input / editing settings

  • lean4.input.enabled: enables abbreviation input completion mode. For example, it allows you to type \alpha and have that be replaced with the greek letter (α).

  • lean4.input.eagerReplacementEnabled: enables/disables eager replacement as soon as the abbreviation is unique (true by default)

  • lean4.input.leader: character to type to trigger abbreviation input completion input mode (\ by default).

  • lean4.input.languages: specifies which VS Code programming languages the abbreviation input completion will be used in. The default is [lean4, lean].

  • lean4.input.customTranslations: add additional input Unicode translations. Example: {"foo": "☺"} will correct \foo to ☺ (assuming the lean.input.leader has its default value \).

  • lean4.typesInCompletionList: controls whether the types of all items in the list of completions are displayed. By default, only the type of the highlighted item is shown.

  • lean4.fallBackToStringOccurrenceHighlighting: controls whether the editor should fall back to string-based occurrence highlighting when there are no symbol occurrences found.

Infoview settings

  • lean4.infoview.autoOpen: controls whether the Infoview is automatically displayed when the Lean extension is activated for the first time in a given VS Code workspace(true by default). If you manually close the Infoview it will stay closed for that workspace until. You can then open it again using the Ctrl+Shift+P Lean 4: Infoview: Display Goal command.

  • lean4.infoview.autoOpenShowsGoal: auto open shows goal and messages for the current line (instead of all messages for the whole file). In this mode the Infoview updates often every time you move the cursor to a different position so it can show context sensitive information. Default is true.

  • lean4.infoview.allErrorsOnLine: show all errors on the current line, instead of just the ones to the right of the cursor, default true.

  • lean4.infoview.debounceTime: how long (in milliseconds) the infoview waits before displaying new information after the cursor has stopped moving. The optimal value depends on your machine - try experimenting. The default is 50.

  • lean4.infoview.showExpectedType: show the expected type by default. This display can then be toggled by clicking on the title or using the Ctrl+Alt+e Lean 4: Infoview: Show Expected Type command. The default is true.

  • lean4.infoview.showGoalNames: show goal names (e.g. case inl in the infoview). The default is true.

  • lean4.infoview.emphasizeFirstGoal: emphasize the first goal by displaying the other goals with reduced size and opacity. The default is false.

  • lean4.infoview.reverseTacticState: show each goal above its local context by default. This can also be toggled by clicking a button (see the Infoview panel description above). The default is false.

Colors

These colors are used to display proof states in the infoview:

  • lean4.infoView.hypothesisName: accessible hypothesis names
  • lean4.infoView.inaccessibleHypothesisName: inaccessible hypothesis names
  • lean4.infoView.goalCount: the number of goals
  • lean4.infoView.turnstile: the turnstile (⊢) that separates the hypotheses from the goal
  • lean4.infoView.caseLabel: case labels (e.g. case zero)

They can be set in a color theme, or under workbench.colorCustomizations in the settings file.

Extension commands

This extension also contributes the following commands, which can be bound to keys if desired using the VS Code keyboard bindings.

The format below is: "lean4.commandName (command name): description", where lean.commandName represents the name used in settings.json and "command name" is the name found in the command palette (accessed by hitting Ctrl+Shift+P).

Server commands

  • lean4.restartServer (Lean 4: Restart Server): restart the Lean 4 Language Server. Useful if you built new .olean files in your workspace.

  • lean4.stopServer (Lean 4: Stop Server): stop the Lean 4 Language Server.

  • lean4.restartFile (Lean 4: Restart File): restarts the Lean server for a single file. Useful if the server crashes. As a side-effect this command will also recompile all dependencies. This command has a default keyboard binding of Ctrl+Shift+X.

  • lean4.refreshFileDependencies (Lean 4: Refresh File Dependencies): An alias for lean4.restartFile. The Lean server does not automatically update a file when one of its dependencies is changed. So after changing a dependency, the server for the file needs to be restarted to pick up the changed dependency.

Editing commands

  • lean4.input.convert (Lean 4: Input: Convert Current Abbreviation): converts the current abbreviation (bound to tab by default)

Infoview commands

  • lean4.displayGoal (Lean 4: Infoview: Display Goal): open the Infoview panel (bound to Ctrl+Shift+Enter by default)

  • lean4.displayList (Lean 4: Infoview: Toggle "All Messages"): toggles the "All messages" widget in the Infoview (bound to ctrl+alt+shift+enter by default)

  • lean4.infoView.copyToComment (Lean 4: Infoview: Copy Contents to Comment"): if there is a valid value in the Infoview marked with the icon that can be copied to a comment, this command invokes that action in the editor.

  • lean4.infoView.toggleStickyPosition (Lean 4: Infoview: Toggle Pin): enable / disable "sticky" mode. On enable, a tactic state widget will be created and pinned to this position, reporting the goal from this point even as the cursor moves and edits are made to the file. On disable the pinned widget will be removed. (same as clicking on the or icon on the tactic state widget closest to the cursor.)

  • lean4.infoView.toggleUpdating (Lean 4: Infoview: Toggle Updating): pause / continue live updates of the main (unpinned) tactic state widget (same as clicking on the or icon on the main tactic state widget.)

  • lean4.infoView.toggleExpectedType (Lean 4: Infoview: Toggle Expected Type): toggles the "Expected Type" widget in the Infoview (bound to ctrl+alt+e by default)

Documentation commands

  • lean4.docView.open (Lean 4: Open Documentation View): Open documentation found in local 'html' folder in a separate web view panel.

  • lean4.docView.showAllAbbreviations (Lean 4: Show all abbreviations): Show help page containing all abbreviations and the Unicode characters they map to. This makes it easy to then search for the abbreviation for a given symbol you have in mind using Ctrl+F.

Complete Setup

The complete flow chart for determining how elan and lean are installed is shown below:

flow

The start state is when you have opened a folder in VS Code and opened a .lean file to activate this extension.

If the extension finds that elan is not in your path and is not installed in the default location then it will prompt you to install lean via elan. If the folder contains a lean-toolchain version it will install that version otherwise it will install leanprover/lean4:stable. You can override this default version by setting an DEFAULT_LEAN_TOOLCHAIN environment variable.

Otherwise, if there is a lean-toolchain (or leanpkg.toml) in the workspace folder or in a parent folder then it will use the version specified in the specified version.

Then with the selected version it runs lean --version to check if that version is installed yet. If this version is not yet installed lean --version will install it.

For VSCode extension developers

See Development.

More Repositories

1

lean4

Lean 4 programming language and theorem prover
Lean
4,612
star
2

lean3

Lean Theorem Prover
C++
2,143
star
3

elan

The Lean version manager
Rust
282
star
4

theorem_proving_in_lean4

Theorem Proving in Lean 4
JavaScript
157
star
5

lean2

Lean theorem prover version 0.2 (it supports standard and HoTT modes)
C++
121
star
6

vscode-lean

Extension for VS Code that provides support for the older Lean 3 language. Succeeded by vscode-lean4 ('lean4' in the extensions menu) for the Lean 4 language.
TypeScript
117
star
7

lake

**(Deprecated: Merged into Lean 4)** Lean 4 build system and package manager with configuration files written in Lean.
Lean
98
star
8

verso

Lean documentation authoring tool
Lean
96
star
9

logic_and_proof_lean3

CMU Undergrad Course
TeX
96
star
10

lean3-mode

Emacs mode for Lean
Emacs Lisp
69
star
11

fp-lean

Functional Programming in Lean
JavaScript
68
star
12

lean4-cli

A Lean 4 library for configuring Command Line Interfaces and parsing command line arguments.
Lean
67
star
13

doc-gen4

Document Generator for Lean 4
Lean
62
star
14

LeanInk

LeanInk is a command line helper tool for Alectryon which aims to ease the integration of Lean 4.
Lean
60
star
15

SampCert

SampCert : Verified Differential Privacy
Lean
59
star
16

tc

Reference type checker for the Lean theorem prover
Haskell
57
star
17

LNSym

Armv8 Native Code Symbolic Simulator in Lean
Lean
52
star
18

lean4-samples

Code samples for Lean 4
Lean
49
star
19

leansat

This package provides an interface and foundation for verified SAT reasoning
Lean
48
star
20

theorem_proving_in_lean

Theorem proving in Lean
Python
47
star
21

tutorial

Lean Tutorials
TeX
43
star
22

functional_programming_in_lean

A book about functional programming in Lean
38
star
23

lean-client-js

TypeScript
34
star
24

lean4-nightly

Nightly builds
22
star
25

lean.vim

Vim Script
18
star
26

lean4checker

Replay the `Environment` for a given Lean module, ensuring that all declarations are accepted by the kernel.
Lean
17
star
27

reservoir

Package registry for Lean/Lake.
Vue
16
star
28

super

Superposition prover
Lean
15
star
29

presentations

lean-related presentations
TeX
15
star
30

SHerLOC

A StableHLO analyzer in Lean
Lean
15
star
31

leanprover.github.io

www
HTML
15
star
32

lean-action

GitHub action for standard CI in Lean projects
Shell
14
star
33

leanbv

Lean
11
star
34

lean4export

Plain-text declaration export for Lean 4
Lean
11
star
35

lean3-web-editor

Lean web editor
TypeScript
9
star
36

smt2_interface

Interface to SMT2 solvers
Lean
9
star
37

lean-llvm

Custom-built LLVM toolchain for use in Lean 4
5
star
38

cmu-15815-s15

CMU 15-815 Spring 2015 : Interactive Theorem Proving
JavaScript
4
star
39

subverso

Lean
3
star
40

mini_crush

Mini crush tactic example
Lean
3
star
41

deprecated-homebrew-lean

See https://github.com/Homebrew/homebrew-core/blob/master/Formula/lean.rb
Ruby
3
star
42

NKL

A formalization of the NKI ISA
Lean
2
star
43

mkleanbook

JavaScript
2
star
44

Lean.tmbundle

A TextMate bundle for the Lean language
2
star
45

lean4-pr-releases

Automated releases from leanprover/lean4 PRs
2
star
46

lean-nightly

Lean nightly builds
2
star
47

macports

macports repository for Lean
Shell
1
star
48

ppa-updater

Ubuntu PPA updater for Lean
Shell
1
star
49

emacs-dependencies

Auxiliary repository to store emacs packages required by Lean Emacs mode
Emacs Lisp
1
star
50

reservoir-index

Registry index for Reservoir
Lean
1
star
51

TensorLib

A verified tensor library in Lean
Lean
1
star
52

TenCert

Verified tensor compilation in Lean
Lean
1
star