Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a9c85db

Browse files
committedJan 30, 2025
update lean-toolchain-tutorial
1 parent ade3387 commit a9c85db

File tree

3 files changed

+98
-24
lines changed

3 files changed

+98
-24
lines changed
 

‎docs/install.md

+16-12
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
1-
# LEAN 4 安装教程
1+
# LEAN 4 安装教程
22

3-
Lean 4 工作环境由以下几部分组成:
3+
Lean 是一个开源的定理证明助手和函数式编程语言,由微软研究院开发。它既可以用于形式化数学证明,也可以用于通用程序设计。
44

5-
1. [elan](https://github.com/leanprover/elan):Lean 版本管理器。功能类似于 Rust 的 `rustup` 或 Node.js 的 `nvm`,用于安装、管理和切换不同版本的 Lean。
6-
2. [lake](https://github.com/leanprover/lake):Lean 的包管理器,全称 Lean Make,已合并为 Lean 4 仓库源码的一部分。用于创建 Lean 项目,构建 Lean 包,配置 Mathlib 和编译 Lean 可执行文件。
7-
3. (非必须)[Mathlib](https://leanprover-community.github.io/mathlib4_docs/):Lean 的数学库。
5+
## Lean 4 工具链
86

9-
为了使用 Lean,你需要先安装 [VS Code](https://code.visualstudio.com/) 和 Git,然后安装 elan,elan 会自动帮你安装 Lean 4 stable 以及 lake 包管理器,接下来就可以使用 lake 创建自己的 Lean 项目。
7+
Lean 4 的完整开发环境由以下核心组件构成:
108

11-
> 如果希望快速了解和使用 Lean,除了在本地安装,也可以通过 [live.lean-lang.org](https://live.lean-lang.org)[live.leanprover.cn](https://live.leanprover.cn) 在线体验。
9+
1. [elan](https://github.com/leanprover/elan):Lean 的版本管理工具,用于安装、管理和切换不同版本的 Lean,类似于 Rust 的 `rustup` 或 Node.js 的 `nvm`
10+
2. [lake](https://github.com/leanprover/lake)(Lean Make):Lean 的包管理器和构建器,已集成到 Lean 4 核心仓库中。用于创建 Lean 项目,构建 Lean 包,编译 Lean 可执行文件等。
11+
3. [lean](https://github.com/leanprover/lean4):语言本身的核心组件,负责实际的代码编译和语言服务器协议(LSP)支持,用户不需要直接与 `lean` 交互。
1212

13-
## Linux 安装 elan
13+
此外,还有 Lean 的标准数学库 [Mathlib](https://leanprover-community.github.io/mathlib4_docs/),包含大量数学定义和定理,以及实用的证明工具和策略。
14+
15+
为了使用 Lean,需要先安装 [VS Code](https://code.visualstudio.com/) 和 Git,然后安装 elan。再通过 elan 安装各个 Lean4 版本以及 lake 包管理器,接下来就可以使用 lake 创建自己的 Lean 项目。工具链的用法示例参见 [Lean 工具链](tutorial/elan-lake.md) 一节或 [lake 文档](references/lake-doc.md)
1416

15-
以下内容在 Ubuntu 22.04 系统上测试通过。
17+
> 如果希望快速了解和使用 Lean,除了在本地安装,也可以直接访问 [live.lean-lang.org](https://live.lean-lang.org)[live.leanprover.cn](https://live.leanprover.cn) 在线体验。
18+
19+
## Linux 安装 elan
1620

1721
如果没有网络问题,用一行命令安装:
1822

1923
```bash
2024
wget -q https://raw.githubusercontent.com/leanprover-community/mathlib4/master/scripts/install_debian.sh && bash install_debian.sh ; rm -f install_debian.sh && source ~/.profile
2125
```
2226

23-
脚本执行内容包括:检查并安装 VsCode,Lean 插件以及 elan。
27+
脚本内容包括:检查并安装 VsCode,Lean 插件,并安装 elan。
2428

2529
如果访问 GitHub 存在问题,可以在安装 vscode 后,手动安装 elan。
2630

@@ -72,11 +76,11 @@ powershell -ExecutionPolicy Bypass -f elan-init.ps1
7276
del elan-init.ps1
7377
```
7478

75-
如果遇到了 `"SSL connect error", "Timeout was reached","Failed to connect to github.com port 443"...` 等错误,就是说明你的网络环境不理想。如果这样可以通过[上交镜像源](https://s3.jcloud.sjtu.edu.cn/899a892efef34b1b944a19981040f55b-oss01/elan/mirror_clone_list.html)安装。下面演示在网络环境不理想的条件下的安装。
79+
如果遇到了 `"SSL connect error", "Timeout was reached","Failed to connect to github.com port 443"...` 等错误,就是说明你的网络环境不理想。如果这样可以通过[上交镜像源](https://s3.jcloud.sjtu.edu.cn/899a892efef34b1b944a19981040f55b-oss01/elan/elan/releases/download/mirror_clone_list.html)下载安装。下面演示在网络环境不理想的条件下的安装。
7680

7781
打开 cmd 或 Powershell,运行
7882
```
79-
curl -O --location https://s3.jcloud.sjtu.edu.cn/899a892efef34b1b944a19981040f55b-oss01/elan/elan/releases/download/eager-resolution-v5/elan-x86_64-pc-windows-msvc.zip
83+
curl -O --location https://s3.jcloud.sjtu.edu.cn/899a892efef34b1b944a19981040f55b-oss01/elan/elan/releases/download/v4.0.0-rc1/elan-x86_64-pc-windows-msvc.zip
8084
unzip -o elan-x86_64-pc-windows-msvc.zip
8185
.\elan-init.exe
8286
```

‎docs/tutorial/elan-lake.md

+81-11
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,49 @@
1-
# elan 及 Lake 用法示例
1+
# Lean 工具链使用指南
2+
3+
[上篇](../install.md)安装 Lean4 提到了 Lean 项目开发的三件套:版本管理器 [elan](https://github.com/leanprover/elan) + 包管理器和构建工具 [lake](https://github.com/leanprover/lake) + 语言本身的核心组件 [lean](https://github.com/leanprover/lean4)。下边分别介绍这三个工具的基本用法。
4+
5+
> 这类设计与其他编程语言类似,如 Rust(rustup + cargo + rustc)或 Node.js(nvm + npm + node)。
26
37
## elan 常用功能
48

5-
[elan](https://github.com/leanprover/elan) 是 Lean 环境版本管理器,用于安装、管理和切换不同版本的 Lean。
9+
[elan](https://github.com/leanprover/elan) 是 Lean 版本管理器,用于安装、管理和切换不同版本的 Lean。
10+
11+
版本管理:
612

713
```bash
814
elan --version # 输出版本号来测试安装是否成功
915
elan self update # 更新 elan
1016
elan show # 显示已安装的 Lean 版本
1117

12-
# 切换默认的 Lean 版本,例如 leanprover/lean4:v4.11.0-rc1
13-
# stable 是最新稳定版本,所有版本可见 https://github.com/leanprover/lean4/releases
18+
# 下载指定 Lean 版本,所有版本可见 https://github.com/leanprover/lean4/releases
19+
elan install leanprover/lean4:v4.10.0
20+
21+
# 下载最新稳定版本 stable
1422
elan default leanprover/lean4:stable
1523

16-
# 也可设置,只在当前目录下使用的 Lean 版本
24+
# 切换默认的 Lean 版本
25+
# 切换到 leanprover/lean4:v4.11.0-rc1
26+
elan default leanprover/lean4:v4.11.0-rc1
27+
28+
# 设置在当前目录下使用的 Lean 版本
1729
elan override set leanprover/lean4:stable
30+
# info: info: override toolchain for 'xxx' set to 'leanprover/lean4:stable'
31+
```
32+
33+
指定版本运行 `lake``lean` 命令:
34+
35+
```bash
36+
lake --version # 使用 elan 默认版本
37+
# 使用指定版本
38+
elan run leanprover/lean4:v4.10.0 lake --version
39+
elan run leanprover/lean4:v4.10.0 lean --version
40+
# 创建指定版本的项目
41+
elan run leanprover/lean4:v4.10.0 lake new package
1842
```
1943

20-
elan 在 Windows 下的管理目录为 `%USERPROFILE%\.elan\bin`,在 Linux 下的管理目录为 `$HOME/.elan`,内容形如
44+
elan 配置记录可以在 `~/.elan/settings.toml` 查看。
45+
46+
具体地,Windows 下的 elan 管理目录为 `%USERPROFILE%\.elan\bin`,Linux/Mac 下的管理目录为 `$HOME/.elan`,内容形如
2147

2248
```bash
2349
❯ tree .elan -L 2
@@ -45,12 +71,12 @@ elan 在 Windows 下的管理目录为 `%USERPROFILE%\.elan\bin`,在 Linux 下
4571
- `settings.toml` 是 elan 的配置文件。
4672
- `bin` 存放常用的二进制文件,比如 `lake`
4773

48-
## 通过 Lake 创建 Lean 项目
49-
50-
对创建 Lean 项目的详细介绍参考[这个教程](https://www.leanprover.cn/fp-lean-zh/hello-world/starting-a-project.html)。此处演示最基本的用法。
74+
## Lake 基本用法
5175

5276
[lake](https://github.com/leanprover/lake) 全称 Lean Make,是 Lean 4 的包管理器,用于创建 Lean 项目,构建 Lean 包和编译 Lean 可执行文件。
5377

78+
本节介绍 `lake` 的基本用法,[Lean 函数式编程](https://www.leanprover.cn/fp-lean-zh/hello-world/starting-a-project.html)也提供了创建 Lean 项目的例子,而更全面的参数介绍可参考 [lake 文档](../references/lake-doc.md)
79+
5480
在终端中运行(`your_project_name` 替换为你自己起的名字)
5581

5682
```bash
@@ -73,7 +99,8 @@ your_project_name
7399
└── ...
74100
```
75101

76-
其中 `lakefile.lean` 是当前项目的配置文件,`lean-toolchain` 是当前项目使用的 Lean 版本。其他文件的功能以及更多细节请参考[这个教程](https://www.leanprover.cn/fp-lean-zh/hello-world/starting-a-project.html)
102+
103+
其中 `lakefile.lean` 是当前项目的配置文件,`lean-toolchain` 是当前项目使用的 Lean 版本。
77104

78105
初次让 Lean Server 运行该项目时会添加
79106

@@ -152,4 +179,47 @@ lake update
152179
lake run <script>
153180
```
154181

155-
关于 Lake 的更多用法可参考 [lake 文档](../references/lake-doc.md)
182+
## lean
183+
184+
[lean](https://github.com/leanprover/lean4):语言本身的核心组件,通常不需要直接与 `lean` 交互。
185+
186+
这里介绍常见的两个操作:运行 Lean 脚本,以及验证 Lean 代码。
187+
188+
执行 Lean 脚本,入口为 `main` 函数:
189+
190+
```lean
191+
-- hello.lean
192+
def main : IO Unit := IO.println s!"Version: {Lean.versionString}"
193+
```
194+
195+
在终端中运行:
196+
197+
```bash
198+
elan default leanprover/lean4:v4.11.0-rc1
199+
lean --run hello.lean
200+
# Version: 4.11.0-rc1
201+
elan run leanprover/lean4:v4.10.0 lean --run hello.lean
202+
# Version: 4.10.0
203+
```
204+
205+
验证 Lean 代码:
206+
207+
```lean
208+
-- proof.lean
209+
theorem my_first_theorem : 1 + 1 = 2 := by
210+
simp
211+
212+
theorem my_false_theorem : 1 + 1 = 1 := by
213+
simp
214+
215+
theorem my_syntax_error_themore 1 + 1 = 2 := by
216+
simp
217+
```
218+
219+
在终端中运行:`lean proof.lean`,返回错误信息:
220+
221+
```bash
222+
hello.lean:5:40: error: unsolved goals
223+
⊢ False
224+
hello.lean:8:31: error: unexpected token; expected ':'
225+
```

‎mkdocs.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ nav:
7070
- 主页:
7171
- Lean-zh: index.md
7272
- 安装指南: install.md
73-
- 版本管理与包管理: tutorial/elan-lake.md
73+
- Lean4 工具链: tutorial/elan-lake.md
7474
- Lake 包管理: references/lake-doc.md
7575
- 交互工具:
7676
- LeanDojo 教程: tutorial/lean-dojo.md

0 commit comments

Comments
 (0)
Please sign in to comment.