Isabelle 模型验证练习¶
用 Isabelle 学形式化方法 · 项目指南 · 速查表 · 下载示例 · English
读完指南后试做这些练习。请在文件副本中实验,方便随时回到原来的完整示例。Exercise_Answers.thy 导入 Isabelle_Tutorial.thy 并包含以下每个答案。
练习 1 修改角度上限¶
为 0 到 60 之间的整数角度定义 angle_ok_60。证明被接受的角度至多为 60。求值 60 和 90 以检查你对定义的理解。
查看定义与证明
definition angle_ok_60 :: "int ⇒ bool" where
"angle_ok_60 a ⟷ 0 ≤ a ∧ a ≤ 60"
lemma sixty_degree_guard_sound:
"angle_ok_60 a ⟹ a ≤ 60"
by (simp add: angle_ok_60_def)
练习 2 提取守卫条件¶
证明由 may_open 接受的每个操作都以未锁定的盖子开始。除了现有的守卫条件外,对目标无需任何假设。
查看证明
lemma admitted_open_starts_unlatched:
"may_open s target ⟹ ¬ lid_latched s"
by (simp add: may_open_def)
练习 3 要求对象静止¶
用布尔值 moving 表示对象是否在运动。定义守卫 stationary_open,要求 may_open s target 成立且对象静止。证明新守卫蕴涵 ¬ moving,并复用已有的不变式保持定理。
查看定义与两条证明
definition stationary_open :: "lid_state ⇒ int ⇒ bool ⇒ bool" where
"stationary_open s target moving ⟷ may_open s target ∧ ¬ moving"
lemma stationary_guard_requires_stop:
"stationary_open s target moving ⟹ ¬ moving"
by (simp add: stationary_open_def)
lemma stationary_open_preserves_invariant:
"stationary_open s target moving ⟹ lid_invariant (set_lid_angle s target)"
using opening_preserves_invariant
by (auto simp: stationary_open_def)
练习 4 缺少证据时拒绝操作¶
利用check_result数据类型,证明除Supported外的任何结果均不授予权限。复用permission_requires_support而非重复其案例分析。
查看证明
lemma non_supported_withholds:
"result ≠ Supported ⟹ ¬ permitted result"
by (simp add: permission_requires_support)
练习 5 找出遗漏的守卫条件¶
在副本中,从 may_open 移除 angle_ok target。从 demo_lid 开始并选择目标 120。初始角度合规、盖子未锁住、容器有支撑,但这次更新会破坏不变式。解释为何自动化无法证明该弱化守卫下的原始保持性陈述。
查看具体反例
lemma unguarded_counterexample:
"lid_invariant demo_lid ∧ ¬ lid_latched demo_lid ∧ lid_supported demo_lid ∧
¬ lid_invariant (set_lid_angle demo_lid 120)"
by (simp add: demo_lid_def lid_invariant_def angle_ok_def set_lid_angle_def)
may_open 即可证明该引理。它说明,其余初始条件不足以保证这次更新保持不变式。该错误的保持性陈述仅存在于你的实验副本中。
练习 6 找出错误的转换¶
在教程理论中找到 bad_lower,它把源角度与上限对调。对源角度 120、上限 90 的例子,分别判断源模型是否合规,以及转换结果是否通过检查,并解释这种差异。
查看错误的映射与反例
definition bad_lower :: "source_lid ⇒ checker_lid" where
"bad_lower m = ⦇checker_angle = source_limit m, checker_limit = source_angle m⦈"
lemma swapped_fields_can_admit_invalid_source:
"¬ source_ok ⦇source_angle = 120, source_limit = 90⦈ ∧
checker_accepts (bad_lower ⦇source_angle = 120, source_limit = 90⦈)"
by (simp add: source_ok_def checker_accepts_def bad_lower_def)
lower_preserves_satisfaction 给出等价性。错误转换后,检查器看到的是角度 90、上限 120。除了证明检查器本身的正确性,还需要证明源模型到检查器输入的转换正确。
练习 7 使用语义保持定理¶
证明可靠性方向:如果 lower m 通过检查,那么源模型满足规约。请复用教程中已经证明的等价关系。
查看证明
lemma translated_acceptance_is_source_valid:
"checker_accepts (lower m) ⟹ source_ok m"
by (simp add: lower_preserves_satisfaction)
练习 8 练习归纳证明¶
证明反转列表不改变step_count。在归纳步骤中复用step_count_append。
查看证明
lemma step_count_reverse:
"step_count (rev xs) = step_count xs"
by (induction xs) (simp_all add: step_count_append)
x # xs 进行反转得到 rev xs @ [x],再用列表拼接引理和归纳假设,即可完成归纳步骤。
检查全部解答¶
从包含下载的 ROOT 文件的目录运行:
/path/to/isabelle build -D .
会话检查两个理论以及每个答案。返回指南。