Skip to content

Latest commit

 

History

History
262 lines (202 loc) · 17.6 KB

File metadata and controls

262 lines (202 loc) · 17.6 KB

Glimpse to Game — 设计文档

1. 项目概述

项目 说明
标题 Glimpse to Game
目标 Glimpse of Lean 教程完整转化为基于 lean4game 的关卡制游戏
受众 希望用交互式、分步闯关方式学习 Lean 4 的数学/逻辑初学者
参考 实现与校对时可对照 GlimpseOfLean/GlimpseOfLean/Solutions/ 中的解答

设计原则

  • 内容覆盖:每个原始 example/lemma 都应对应到某一关的 Statement,不遗漏、不擅自删减。
  • 风格统一:遵循 lean4game 约定:每 World 一个总 .lean 文件 + 子目录下按关的 Lxx_Name.lean,每关使用 StatementIntroductionHintNewTacticConclusion 等。
  • 依赖一致:关卡顺序与原始教程一致;先 Computing → Logic,再开放 Analysis / Set Theory / Algebra / Probability 等分支。

2. 源文件与 World 对应表

原始路径 (GlimpseOfLean) 游戏 World 说明
Exercises/01Rewriting.lean Rewriting 计算与改写:ring, rw, calc, exact
Exercises/02Iff.lean Logic(前半) 蕴涵、等价、↔
Exercises/03Forall.lean Logic(中段) 全称 ∀、unfold、specialize、simp、apply?
Exercises/04Exists.lean Logic(后半) 合取 ∧、存在 ∃、rcases、use
Exercises/Topics/SequenceLimits.lean Analysis 数列极限、linarith、子列、聚点、Cauchy
Exercises/Topics/GaloisAdjunctions.lean SetTheory 偏序、inf/sup、完备格、Galois 连接
Exercises/Topics/RingTheory.lean Algebra 环、同态、理想、商环、同构定理、中国剩余
Exercises/Topics/Probability.lean Probability 独立性、条件概率、贝叶斯
Exercises/Topics/ClassicalPropositionalLogic.lean PropositionalLogic(可选) 经典命题逻辑语法与语义
Exercises/Topics/IntuitionisticPropositionalLogic.lean PropositionalLogic(可选) 直觉主义命题逻辑、Heyting 代数

说明:Exercises/Shorter.lean 为“短轨”,与 01–04 + Topics 有重叠,可不单独建 World,其题目可视为对主轨的补充或合并到对应 World。


3. World 与关卡设计(与原始题目一一对应)

3.1 World: Rewriting(Computing)

源文件GlimpseOfLean/Exercises/01Rewriting.lean
SolutionsGlimpseOfLean/Solutions/01Rewriting.lean

Level 标题建议 对应原始内容 要点
1 The ring tactic (结合律) (a * b) * c = b * (a * c) ring
2 Expansion (a+b)^2 = a^2 + 2*a*b + b^2 ring
3 Basic rewriting a + e = d + ch : a = b+c, h' : b = d-e 分步 rw [h] rw [h'] rwring
4 Multiple rewrites 同上,一次 rw [h, h'] rw 多步
5 Rewriting with assumptions a + b = c + 4*dh : b = d+d, h' : a = b+c rw + ring
6 Rewriting with lemmas exp (a+b+c) = exp a * exp b * exp c(exp_add) rw [exp_add]、可带参数
7 exp_sub / exp_zero exp (a+b-c) = (exp a * exp b)/(exp c * exp 0) exp_subexp_zeroring/mul_one
8 Reverse rewriting b + c + e = d + ch : a = b+c 从右到左 rw [← h] rw [← h]
9 Reverse rewriting exercise b + c = dh : a = b+b, h' : b = c, h'' : a = d rw [← …]
10 Rewriting in an assumption c = d*a + drw [h'] at hexact h rw [...] at hexact
11 Calculation layout (calc) c = 0h : c = b*a - d, h' : d = a*b 的 calc calcrwring
12 calc with exp exp (2*a) = (exp b)^2 * (exp c)^2(填空式 calc) calcexp_add
13 calc exercise c = 2*a*dh : c = d*a + b, h' : b = a*d calcring

NewTactic 建议:按关依次引入 ringrwexactcalc;需要时在 Introduction 中说明 at h


3.2 World: Logic

源文件02Iff.lean03Forall.lean04Exists.lean(严格按此顺序)

3.2.1 来自 02Iff:蕴涵与等价

Level 标题建议 对应原始内容 要点
L01 Using implications 0 < a^2sq_pos_of_pos ha exact
L02 Backward reasoning (apply) 0 < (a^2)^2 两次 apply sq_pos_of_posexact ha apply、多目标
L03 add_pos (两个正数之和为正) 0 < a^2 + b^2add_possq_pos_of_pos apply、分目标
L04 Forward reasoning (have) 0 < (a^2)^2have h2 : 0 < a^2 := …exact sq_pos_of_pos h2 have
L05 have exercise 同 L03 用 have 证明 0 < a^2 + b^2 have
L06 Proving implications (intro) a > 0 → b > 0 → a + b > 0intro ha hbexact add_pos intro
L07 Propositional logic (p → q) → (p → q → r) → p → r introexact
L08 Equivalences (rw with ↔) c + a ≤ c + b ↔ a ≤ bsub_nonnegringrw rw + ↔
L09 add_le_add_iff_right a + c ≤ b + c ↔ a ≤ b 同上,可对照 add_le_add_iff_right
L10 Using .2 of equivalence b ≤ a + b(add_le_add_iff_right b).2 ha h.1 / h.2
L11 add_le_add_iff_left a ≤ a + badd_le_add_iff_left ↔ 的方向
L12 Proving equivalences (constructor) (a-b)*(a+b) = 0 ↔ a^2 = b^2constructor、两向 calc constructor·
L13 a = b ↔ b - a = 0 a = b ↔ b - a = 0 证明 ↔

3.2.2 来自 03Forall:全称量词

Level 标题建议 对应原始内容 要点
L14 even_fun, unfold even_fun (f + g)unfoldintrocalcrw [hf x] unfoldintrorfl
L15 Compressed ∀ proof 同上,用 intro hf hg xrw [hf, hg] 简化写法
L16 even_fun (g ∘ f) even_fun (g ∘ f) ∀ 与函数复合
L17 non_decreasing, forward non_decreasing (g ∘ f)have step₁exact hg … haveexact
L18 specialize 同上,用 specialize hf x₁ x₂ hexact hg … specialize
L19 Backward with apply 同上,用 apply hgapply hfexact h apply 与推断
L20 non_increasing (g ∘ f) non_increasing (g ∘ f) 当 f 单调增、g 单调减 组合
L21 simp (Set) x ∈ (X ∩ Y) ∪ (X \ Y) simp
L22 apply? 紧支撑连续函数有全局最小值 ∃ x, ∀ y, f x ≤ f y apply?

3.2.3 来自 04Exists:合取与存在

Level 标题建议 对应原始内容 要点
L23 Conjunction (rcases, constructor) p ∧ q → r ∧ srcasesconstructor rcasesconstructor
L24 Conjunction (⟨ , ⟩) 同上,用 ⟨h hpq.1, h' hpq.2⟩ 项式证明
L25 (p → (q → r)) ↔ p ∧ q → r 命题逻辑等价,可选 tauto constructortauto
L26 Existential (use) ∃ n : ℕ, 8 = 2*nuse 4 use
L27 Using ∃ (rcases) n > 0h : ∃ k, n = k+1rcasesrwexact Nat.succ_pos rcases
L28 Divisibility (a ∣ b) a ∣ bb ∣ ca ∣ cuse + calc) a ∣ buse
L29 Surjective Surjective (g ∘ f)Surjective g Surjectiveusercases

Logic World 实现注意:当前已有 L01–L07(Implications, Forward, ProvingImplications, Equivalences, Forall, Exists, Conjunction),需对照上表补全 L08–L13(等价多关)与 L14–L22(∀、simp、apply?),并确保 L23–L29 与 04Exists 一致。Solutions 见 Solutions/02Iff.lean03Forall.lean04Exists.lean


3.3 World: Analysis(Sequence Limits)

源文件GlimpseOfLean/Exercises/Topics/SequenceLimits.lean
SolutionsGlimpseOfLean/Solutions/Topics/SequenceLimits.lean

Level 标题建议 对应原始内容 要点
1 linarith 0 ≤ a + ba + c ≤ b + d linarith
2 seq_limit definition 引入 seq_limit,常数列 ∀ n, u n = lseq_limit u l intro ε ε_posuse 0simp
3 Positive limit lower bound l > 0 时 ∃ N, ∀ n≥N, u n ≥ l/2 rcasesuselinarithabs_*
4 Limit of sum (triangle ineq) seq_limit (u+v) (l+l') 用 ε/2、max N₁ N₂、ge_max_iffabs_add_lelinarith rcasesuse maxcalc
5 Squeeze theorem u ≤ v ≤ w 且 u,w → l 则 v → l specializelinarithabs_le
6 Uniqueness of limit seq_limit u lseq_limit u l'l = l' eq_of_abs_sub_le_all
7 non_decreasing, is_seq_sup is_seq_sup M u + non_decreasing useq_limit u M 定义展开、use、不等式
8 extraction, id_le_extraction' extraction φ → ∀ n, n ≤ φ n(归纳,可提供) extraction 定义
9 extraction_ge extraction φ → ∀ N N', ∃ n ≥ N', φ n ≥ N ∃ n ≥ N', _、归纳或构造
10 cluster_point, near_cluster 聚点定义与“任意 ε,N 存在 n≥N 使 |u n - a| ≤ ε” rcasesuse
11 Subsequence tends to limit seq_limit u lextraction φseq_limit (u ∘ φ) l introuse、与 φ 单调性
12 cluster_limit seq_limit u lcluster_point u aa = l near_clusterunique_limit
13 Cauchy from convergent (∃ l, seq_limit u l)CauchySequence u rcases、ε/2、max、linarith
14 Limit from Cauchy + cluster CauchySequence ucluster_point u lseq_limit u l near_cluster、Cauchy 定义

当前 GlimpseToGame 已有 L01–L04(SequenceLimit, SumLimit, SqueezeTheorem, Uniqueness),需与上表对齐并补全 L05–L14(若合并 linarith 为 L00 则编号顺延)。每关的 Statement 应与 Solutions 中对应 example/lemma 一致以保证可编译。


3.4 World: SetTheory(Galois Connections)

源文件GlimpseOfLean/Exercises/Topics/GaloisAdjunctions.lean
SolutionsGlimpseOfLean/Solutions/Topics/GaloisAdjunctions.lean

Level 标题建议 对应原始内容 要点
1 lowerBounds / isInf isInf s x₀ 的定义与 isInf.lowerBound lowerBoundsisInf
2 isInf.eq (唯一性) isInf s x₀isInf s x₁x₀ = x₁ le_antisymmisInf
3 isSup isSup 定义、isSup.upperBoundisSup.eq(可引用对偶) upperBounds、OrderDual
4 isInfFun / isSupFun isInfFun IisSupFun S 定义 函数版 inf/sup
5 isSup_of_isInf / isInf_of_isSup 由 Inf 函构造 Sup、由 Sup 函构造 Inf 对偶构造
6 CompleteLattice.mk_of_Inf/Sup 从 Inf/Sup 函数得到完备格 CompleteLattice
7 Inf_pair / Sup_pair x ≤ x' ↔ Inf {x,x'} = x 二元 inf/sup
8 isInfInter / isSupUnion 集合的 ⋂₀、⋃₀ 为 inf/sup Set 族
9 adjunction 定义 adjunction l r 与基本性质 adjunction
10 image_preimage_adjunction 映射的像-原像 Galois 连接 具体例子
11 mk_right / mk_left 由 l 构造 r、由 r 构造 l Galois 构造
12+ Topology(可选) isOpen_emptyisOpen_univSupTop 若时间允许

当前仅有 SetTheory/L01_InfSup,需按上表扩展至至少 L02–L11,并与 Solutions 一致。


3.5 World: Algebra(Commutative Rings)

源文件GlimpseOfLean/Exercises/Topics/RingTheory.lean
Solutions:需从仓库或本地 Solutions 目录核对

Level 标题建议 对应原始内容 要点
1 ring in CommRing x*(y+z) + y*(z+x) + z*(x+y) = … ringCommRing
2 Ring homomorphism (simp) f (1 + x*y) + f 0 = 1 + f x * f y simp、环同态
3 RingHom.comp 定义 comp 并证明 map_one'/mul'/zero'/add' introsimp
4 RingEquiv.comp 同构的复合 同构、symm
5 Ideal (加法与乘法闭) y*x + x*y - 0 ∈ I IdealI.add_mem
6 ker 与 x ∈ ker f ↔ f x = 0 x ∈ ker f ↔ f x = 0 Ideal.ker
7 Ideal.inter 定义并证明两个理想的交仍是理想 Ideal 构造
8 Quotient 运算 商环中乘法等(如 x*y*z Ideal.Quotient
9 Quotient 与等价 x - y ∈ I → 商中相等 Quotient.eq
10 ker (Quotient.mk I) = I 商映射的核 Ideal.kerQuotient
11 kerLift 从 R →+* S 得到 R/ker f →+* S 同态分解
12 First Isomorphism Theorem firstIsomorphismTheorem(满射时 R/ker f ≃+* S) 同构定理
13 chineseMap, injective/surjective 中国剩余定理的映射与单/满性 chineseMapFintype、理想互素

当前仅有 Algebra/L01_RingHom,需按上表补全 L02–L13,并确保依赖 GlimpseOfLean.Library.Ring(或游戏内等价 Library)。


3.6 World: Probability

源文件GlimpseOfLean/Exercises/Topics/Probability.lean
SolutionsGlimpseOfLean/Solutions/Topics/Probability.lean

Level 标题建议 对应原始内容 要点
1 IndepSet.symm IndepSet A BIndepSet B A 独立性对称
2 IndepSet.compl_right IndepSet A BIndepSet A Bᶜ(可测) compl_eq_univ_diffmeasure_diff
3 IndepSet.compl_right_iff IndepSet A Bᶜ ↔ IndepSet A B iff、measurability
4 IndepSet.compl_left IndepSet Aᶜ BIndepSet A B 推出 用 compl_right
5 indep_self IndepSet A A → ℙ A = 0 ∨ ℙ A = 1 讨论 ℙ A
6 condProb 定义与 zero_left/zero_right ℙ(A|B)、分子/分母为 0 的引理 condProb、ENNReal
7 condProb_of_indepSet 独立时 ℙ(A|B) = ℙ A 条件概率与独立性
8 Bayes(若原文件有) 贝叶斯公式 条件概率等式

当前已有 Probability/L01_Independence,需与上表对齐并补全 L02–L08;注意 MeasureSpaceIsProbabilityMeasuremeasurability 等前置条件。


3.7 World: PropositionalLogic(可选)

源文件ClassicalPropositionalLogic.leanIntuitionisticPropositionalLogic.lean

  • 经典:Formula、IsTrue、Satisfies、Models、Valid;Valid (~~A ⇔ A) 等;Provable、weakening、deduction_theorem、Provable.mp;Provable (A || ~A) 等。
  • 直觉主义:eval(Heyting 代数)、Models、Valid;Valid (~(A && ~A));Provable、De Morgan 等。

可单独做一个 World(如 5–8 关)或作为 Logic 的进阶分支,与 02/03/04 无强依赖,但需要命题逻辑符号(∧∨→↔¬)已在前面引入。


4. 依赖与解锁关系

  • 入口:Rewriting(Computing)为第一个 World。
  • Rewriting 通关后:解锁 Logic
  • Logic 通关后:同时解锁 AnalysisSetTheoryAlgebraProbability(四者平行,无先后)。
  • PropositionalLogic(若实现):建议在 Logic 通关后解锁,或与 SetTheory/Algebra 并列。

与当前 Game.lean 中的 Dependency Rewriting → LogicDependency Logic → Analysis/SetTheory/Algebra/Probability 一致。


5. lean4game 实现规范

  • 文件结构
    • 每个 World 一个“总文件”,如 Game/Levels/Rewriting.leanGame/Levels/Logic.lean,其中只做 import Game.Levels.Rewriting.Lxx_*World "Rewriting"TitleIntroduction
    • 每关一个文件,如 Game/Levels/Rewriting/L01_Associativity.lean,内含 World "Rewriting"Level nTitleIntroductionStatement、可选 HintNewTacticNewDefinitionConclusion
  • Statement
    • 与 GlimpseOfLean 中对应 example/lemma 的命题和类型一致;仅将 example 改为 Statement ... := by ...,证明可从 Solutions 移植后微调(如把注释变成 Hint)。
  • Hint
    • 将原始教程中的“提示句”改为关卡内的 Hint "...",便于玩家在卡住时逐步查看。
  • NewTactic / NewDefinition
    • 在该 tactic/定义首次出现的关卡中声明,避免提前暴露后续内容。
  • 编译
    • 每关必须能通过 lake build(或项目使用的构建命令);新增或修改关卡后应全量编译并跑测试(若有)。

6. 校对与开发清单

  • Rewriting:13 关与 01Rewriting.lean 的 13 个 example 一一对应,且含“在假设中改写”与“exact”关。
  • Logic:02Iff(L01–L13)、03Forall(L14–L22)、04Exists(L23–L29)全部覆盖;顺序与源文件一致;Solutions 已对照。
  • Analysis:SequenceLimits 中 linarith、seq_limit、和极限、夹逼、唯一性、单调+上确界、子列、聚点、Cauchy 等全部成关;与 Solutions 一致且可编译。
  • SetTheory:GaloisAdjunctions 从 isInf/isSup 到 adjunction、mk_left/mk_right 均有对应关卡。
  • Algebra:RingTheory 从 ring、RingHom、Ideal、商、同构定理到中国剩余定理均有对应关卡;依赖 Library 正确。
  • Probability:IndepSet、条件概率、贝叶斯等与 Probability.lean 一致;可测性等前提在 Introduction 或 Hint 中说明。
  • Game.lean:Title、Introduction、Dependency 与本文档一致;所有 World 的 Level 编号连续且无遗漏导入。
  • 全局:无未使用的 import;所有 Statement 可证明且与原始题目等价;Hint 与原始教程表述对齐。

实现时优先保证“内容覆盖 + 编译通过”,再优化每关的 Introduction/Conclusion 文案和 Hint 的粒度。