用z3的api修正例程并中文化

之前搭原型时,发现了 这个问题向z3定理证明库的开发组请教,原来应使用其 API 而非 Python 的原生逻辑运算符。 将 原始例程 修正并中文化后如下。 z3 版本 仍在 python 3.12 下测试。 import z3 ## You can install z3 using the following command: ## pip3 install z3-solver s = z3.Solver() 甲, 戊, 丁, 乙, 丙 = z3.Bools( ["甲", "戊", "丁", "乙", "丙"] ) # 1. 至少一个有问题 s.add(z3.Or(甲, 丁, 乙, 丙, 戊)) # 2. 若甲有问题,且乙没问题 # 则戊有问题 s.add(z3.If( z3.And(甲, z3.Not(乙)), # condition 戊, # then True)) # else # 3. 若甲没问题, 则丙没问题 s.add(z3.If(z3.Not(甲), z3.Not(丙), True)) # 4. 若乙有问题, 则丙有问题 s.add(z3.If(乙, 丙, True)) # 5. 甲和乙都有问题不可能 s.add(z3.Not(z3.And(甲, 乙))) # 6. 除非乙有问题否则戊没问题 s.add(z3.If(乙, True, z3.Not(戊))) # 输出解答 if s.check() != z3.sat: raise ValueError("无解") print(s.model()) 输出: [甲 = False, 乙 = False, 丙 = False, 丁 = True, 戊 = False] Prolog 版本 在线环境 运行 ?- 侦查(X甲, X戊, X丁, X乙, X丙)。 有问题 :- true. 没问题 :- false. 侦查(X甲, X戊, X丁, X乙, X丙):- % 每人没问题或有问题 member(X甲, [有问题, 没问题]), member(X戊, [有问题, 没问题]), member(X丁, [有问题, 没问题]), member(X乙, [有问题, 没问题]), member(X丙, [有问题, 没问题]), % 1. 至少一个有问题 (X甲 = 有问题 ; X戊 = 有问题 ; X丁 = 有问题 ; X乙 = 有问题 ; X丙 = 有问题), % 2. 若甲有问题,且乙没问题 % 则戊有问题 ((X甲 = 有问题, X乙 = 没问题) % 如果 -> X戊 = 有问题 % 则 ; true), % 否则 % 3. 若甲没问题, 则丙没问题 (X甲 = 没问题 -> X丙 = 没问题; true), % 4. 若乙有问题, 则丙有问题 (X乙 = 有问题 -> X丙 = 有问题; true), % 5. 甲和乙都有问题不可能 not(X甲 = 有问题), not(X乙 = 有问题), % 6. 除非乙有问题否则戊没问题 (X乙 = 有问题 -> true; X戊 = 没问题), true. 输出: X丁 = 有问题, X丙 = X乙, X乙 = X戊, X戊 = X甲, X甲 = 没问题 对比 语法相似: prolog 版本里的这句 not(X甲 = 有问题), not(X乙 = 有问题), 还是带疑惑,没找到如果要表达「甲没问题,乙也没问题」该怎么写。 https://github.com/Z3Prover/z3/issues/7724 I found a weird issue when trying out the sample. After checking the tutorial, I replaced the or clause with Or: s.add(z3.Or(Chase, Heath, Mullaney, Ellis, Decker)) And the issue is gone. I wonder what’s the difference between z3.Or and the python or? NikolajBjorner commented on Jul 8, 2025 NikolajBjorner on Jul 8, 2025 Contributor python or: not translated to z3. z3.Or: translated to z3. We don’t overload “or” in the BoolRef class in python. Not sure if it is desired/possible.

2025/7/27
articleCard.readMore

用z3搭个微型逻辑编程语言原型

几周前 浅尝 z3,貌似和 prolog 有点像。于是继 四年前 再做个原型:输入「甲有问题或乙有问题。若甲有问题则乙有问题。」输出「乙有问题」 为支持无空格语法,仍使用rply定制版的按语法分词。主要语法规则如下: @分析器母机.语法规则("代码 : 各句 句号") def 代码(片段): if s.check() != z3.sat: raise ValueError("有误") 结果 = s.model() for 某项 in 结果: if 结果[某项]: return str(某项) + '有问题' @分析器母机.语法规则("各句 : 结构句") @分析器母机.语法规则("各句 : 各句 句号 结构句") def 各句(片段): if len(片段) == 1: return f"{片段[0]}" if len(片段) == 3: return f"{片段[0]} {片段[2]}" @分析器母机.语法规则("结构句 : 句 或者 句") def 或者(片段): 新布尔量 = z3.Bools( [片段[0], 片段[2]] ) 各值[片段[0]] = 新布尔量[0] 各值[片段[2]] = 新布尔量[1] s.add(functools.reduce(self.或者, list(各值.values()))) @分析器母机.语法规则("结构句 : 假设 句 那么 句") def 假设(片段): 假定条件 = 片段[1] 关联结果 = 片段[3] s.add(z3.If( 各值[假定条件] == True, 各值[关联结果] == True, True, )) @分析器母机.语法规则("句 : 标识符 有问题") def 句(片段): return 片段[0].getstr() 为简短起见,z3部分直接集成在了语法分析处理里。虽然 分词时回退次数超出了内置上限,不过至少此例运行速度尚可。完整代码 在此。

2025/7/3
articleCard.readMore

以前都是鼓吹快乐教育,为什么现在有些人认为实际上就是愚民教育?

https://www.zhihu.com/question/1909951903275676395/answer/1922640741001311464 日本电影《机器人竞赛》里,小组在决赛准备的关键时刻,导师拉他们到海边玩几天同时在温泉旅馆打工帮各种杂活如迎来送往、洗碗擦浴池洗晒被褥等等换吃住。 每天累瘫但都有收获。 十几年脱离劳动,脱离社会,脱离人际,是很多教育问题的助燃剂。 之前到湾区公立初中校园随处看到心理辅导热线。 把与生俱来的随自然环境进行自我调节的本能在温室中消磨掉,替换为基于「心理理论」的技术指导,会有多少效果?

2025/6/28
articleCard.readMore

华为余承东谈智能驾驶「鸿蒙智行不是第一阵营而是第一名」,大家认为靠谱吗?

https://www.zhihu.com/question/1912770563677616090/answer/1919378919959823966 今天新闻联播里,听到不少「人工智能」、「大模型」,看来国内还没意识到这一技术路线的潜在危害。 几个月前对智驾的政策转向,现在看来是事实上将非基于「大模型」的智能技术路线排除在AI玩家圈子之外的操作。 基于文本数据训练的内容生成工具,是当下ai圈子的主流,也是「人云亦云」风潮的实体化。 在互联网兴起的十几年直到疫情前后,相当一部分文艺界敢于对社会问题、国际关系发声的人士被离间、集火、诬陷等排挤和消声,现在的自媒体界已极少有与国际各阶层有深厚联系又敢于发表自己意见的文艺界前辈。 而掌控百万粉丝、可轻易影响舆论风向的自媒体号的文案是怎样产生的呢?有多少写手在靠着xxgpt之流工具而毫不在意其背后的训练是基于何处而来的文字内容、甚至有无其他后端接管响应呢? 网络上所谓的「一边倒舆论」也就更易产生,这几年的各种社会和国际事件后屡见不鲜。 今年必定是多事之年,亡羊补牢未为晚也。

2025/6/19
articleCard.readMore

身边好几个朋友要润,都说是为了孩子的教育。国外的教育真的那么好吗,低压力教育就不会教出废柴来?

https://www.zhihu.com/question/1914258589318612464/answer/1919335224669627077 说自己在湾区校内外见闻之前,几天前看到某外网帖:帖主父亲在他32岁才告诉他,小时候会从礼品店买一袋贝壳,一边丢在沙滩一边让娃寻觅,现在当爷爷了,继续为孙辈这么做。 教育何尝不是在让娃发现知识海洋中的的美丽贝壳?一个大问题是有多少空间时间让娃自行寻觅。 先说参与的校外辅导:本地图书馆放学后每周三次,三位志愿者每次1.5小时面向K-8的免费作业辅导,每次来的学生五到二十人不等;学而思数学周末三四年级最多16人的班,两小时课时。 进校的:本地公立小学,每周四天上午,每天两课,每课半小时为K-1的移民学生(估计免费)补英文,每组6-8人;进私校的数学课后班1.5小时,各班三到八人不等;本地公立初中课后补习数学,每课时半小时,一人辅导最多三人。 在最后的初中补习,我可以在一对一时花十多分钟陪有兴趣的学生玩二十四点,顺便观察基本运算和思考习惯。 当然,国外像加州湾区这样教育资源富足又允许辅导时发挥的恐怕不是多数。在大环境影响下也有明显感觉各种逐渐拮据,对教师的条框限制也在增多,而且这几届学生受前几年疫情时远程学习的后遗症更明显。 同时,也听闻过 高分但颓废的。也听说过夸张的私校情况,老师某早上到教室发现大变样,问校长原来是某位家长为了自家娃连夜到校重新布置了。 再提一下 PTA,公立小学不少靠此家长组织来募集音乐艺术体育课的开销,为集资会举办一些游园会、长跑之类公益活动,全都需要家长作志愿者。就我看到的,华人家长 参与的比例有限。而这些参与往往会有和校长、老师等打交道的机会。你猜这是否会影响娃在校的人际、老师的时间分配? 自由和选择多,带来的诱惑和考验也更多。 教育自然不例外。

2025/6/19
articleCard.readMore

用月兔语言编写猜数字——出师未捷.mbt

类似之前 用仓颉试写猜数字例程,尤其想看看反馈方面如报错等的表现。 刚安装vsc插件,弹窗报错5分钟内崩溃三次,再看还有个弹窗说需要安装月兔工具链。按 官网 在vsc内安装后,仍弹窗报错5分钟内崩溃三次,暂时不管。 输入 mbt 可以插入代码块,看似插件起效了。 let a = 1 let b = 2 println(a) 点 Fmt, 报”Client is not running” 在终端运行 % source ~/.zshrc 后,可运行 moon 命令,再重启插件,点 Fmt 似乎不报错了,但也没看到格式化代码的效果。 % moon test error: could not find a moon.mod.json file in the source directory or its ancestors 插入测试代码块: test { } 空代码块点 Test: * 正在执行任务: moon test '2025-06-13-用月兔语言编写猜数字.mbt.md' -i 0 zsh:1: command not found: moon * 终端进程“/bin/zsh '-l', '-c', 'moon test '2025-06-13-用月兔语言编写猜数字.mbt.md' -i 0'”启动失败(退出代码: 127)。 * 终端将被任务重用,按任意键关闭。 加了两行: test { let a = 1 inspect(a, content="1") } inspect有代码补全,还是无效。 感觉是工具链路径问题,于是重启vsc,这下mbt test代码块的 Test 有效了: * 正在执行任务: moon test '2025-06-13-用月兔语言编写猜数字.mbt.md' -i 0 failed: moonc build-package -error-format json /Users/xuanwu/work/文章/team_website/_posts/target/wasm-gc/debug/test/single/__generated_driver_for_blackbox_test.mbt -o /Users/xuanwu/work/文章/team_website/_posts/target/wasm-gc/debug/test/single/single.blackbox_test.core -pkg moon/test/single_blackbox_test -is-main -std-path /Users/xuanwu/.moon/lib/core/target/wasm-gc/release/bundle -i /Users/xuanwu/work/文章/team_website/_posts/target/wasm-gc/debug/test/single/single.mi:single -pkg-sources moon/test/single_blackbox_test:/Users/xuanwu/work/文章/team_website/_posts -target wasm-gc -g -O0 -source-map -blackbox-test -no-mi -patch-file /Users/xuanwu/work/文章/team_website/_posts/target/wasm-gc/debug/test/single/__moonbit_internal_md_test.json -test-mode Warning: [0002] ╭─[/Users/xuanwu/work/文章/team_website/_posts/2025-06-13-用月兔语言编写猜数字.mbt.md:8:5] │ 8 │ let a = 1 │ ┬ │ ╰── Warning: Unused toplevel variable 'a'. Note if the body contains side effect, it will not happen. Use `fn init { .. }` to wrap the effect. ───╯ Warning: [0019] ╭─[/Users/xuanwu/work/文章/team_website/_posts/2025-06-13-用月兔语言编写猜数字.mbt.md:9:2] │ 9 │ let b = 2 │ ────┬──── │ ╰────── Warning: Toplevel declaration is not left aligned. ───╯ Warning: [0002] ╭─[/Users/xuanwu/work/文章/team_website/_posts/2025-06-13-用月兔语言编写猜数字.mbt.md:9:6] │ 9 │ let b = 2 │ ┬ │ ╰── Warning: Unused toplevel variable 'b'. Note if the body contains side effect, it will not happen. Use `fn init { .. }` to wrap the effect. ───╯ Warning: [0019] ╭─[/Users/xuanwu/work/文章/team_website/_posts/2025-06-13-用月兔语言编写猜数字.mbt.md:10:3] │ 10 │ println(a) │ ─────┬──── │ ╰────── Warning: Toplevel declaration is not left aligned. ────╯ Error: [3002] ╭─[/Users/xuanwu/work/文章/team_website/_posts/2025-06-13-用月兔语言编写猜数字.mbt.md:10:3] │ 10 │ println(a) │ ───┬─── │ ╰───── Parse error, unexpected token `identifier`, you may expect `pub` or `priv` or `type` or `suberror` or `typealias` or `async` or `fn` or `fnalias` or `struct` or `enum` or `let` or `const` or `extern` or `test` or `impl` or `trait` or `traitalias`. ────╯ error: failed when testing 报警里有缩进未对齐信息,但是代码块的 Fmt 还是无效。 于是手动改成对齐: let a = 1 let b = 2 println(a) 点 Test 结果报错: Error: [4051] ╭─[/Users/xuanwu/work/文章/team_website/_posts/2025-06-13-用月兔语言编写猜数字.mbt.md:102:5] │ 102 │ let b = 2 │ ┬ │ ╰── The toplevel identifier b is declared twice: it was previously defined at /Users/xuanwu/work/文章/team_website/_posts/2025-06-13-用月兔语言编写猜数字.mbt.md:9:6. 看起来,两个代码块的同名变量会被认为是重复定义。这在开发时也许合理,但技术文档常会有这种情况。不知如何取消。更重要的是还不知如何单独运行某个代码块。 其他日志 * 正在执行任务: curl -fsSL https://cli.moonbitlang.com/install/unix.sh | bash -s '0.1.20250612+600461c34' Downloading moonbit ... ######################################################################## 100.0% Downloading core ... ######################################################################## 100.0% Bundling core ... Finished. moon: ran 57 tasks, now up to date Finished. moon: ran 57 tasks, now up to date Finished. moon: ran 57 tasks, now up to date Finished. moon: ran 57 tasks, now up to date moonbit was installed successfully to ~/.moon To verify the downloaded binaries, check https://www.moonbitlang.com/download#verifying-binaries for instructions. To know how to add shell completions, run 'moon shell-completion --help' Added "~/.moon/bin" to $PATH in "~/.zshrc" To get started, run: source ~/.zshrc moon help * 按任意键关闭终端。

2025/6/13
articleCard.readMore

如何评价杨立昆认为大模型只是对海量文本的模式进行复杂拟合,根本不懂意义?

https://www.zhihu.com/question/1913003206876853004/answer/1915201140573836227 为了最大噱头以吸引大众眼球、引诱全球范围跟进、并使其在编程等更实用领域达到「可用」程度让风投快速回本,AI圈在疫情前放弃了谷歌的游戏领域(记得阿尔法狗否?),选择了「通用」领域。 还记得疫情前就见新闻号称某AI做高考数学卷得了一百三十多,为何这几年闭口不提了呢? 陶哲轩的 不等式证明工具 用的是远在这十年AI潮兴起之前的技术、工具和算法,不需任何所谓大模型和训练过程,即便还远未充分优化也在多数情况下具备秒级的响应时间,更无需任何昂贵硬件,不更接近简约低能耗的「智能」吗? 开始证明。当前情况: x: pos_real y: pos_real z: pos_real h1: x < 2*y h2: y < 3*z + 1 求证:x < 7*z + 2 检验以下不等式可否成立: 1*x > 0 1*x + -2*y < 0 1*y > 0 1*y + -3*z < 1 1*z > 0 1*x + -7*z >= 2 以下式子取和,得证不可能成立: 1*x + -2*y < 0 两边乘 -1/4 1*y + -3*z < 1 两边乘 -1/2 1*z > 0 两边乘 1/4 1*x + -7*z >= 2 两边乘 1/4 证明完毕! 对当下风行的对话式ai叹为观止的,不妨看看简单的对话机器人在几十年前就可如何通过图灵测试再想想为何其不再被提及。 搞懂「鸟」,相比搞懂不等式证明,哪个是更值得当下投入的「智能」,每个投入学术和职场生涯、以及晚来的投资者该三思后行

2025/6/8
articleCard.readMore

怎样客观地看待中国应试教育?

https://www.zhihu.com/question/24091894/answer/1914133426665161155 记得三十多年前国内小学时,就时不时做整页的加减乘除运算。 现在娃在美国湾区小学,也要做类似的。 应试,或者说将对人的评估标准以题目答案对错为主,本应是在教育资源稀缺下的无奈之举,但无论在国内外似乎没有特意扭转的迹象。 之前在湾区某初中进校辅导,用了某在线评估工具在开始时摸底,有的学生分三天(每节课时半个小时)共一个半多钟头还没做完,快做完的时候放弃了辅导,结果做过的评估作废;有的花十分钟不到盲选完四十几道选择题,评估结果出来和花一个多钟头的差不多。然后评估工具所谓定制出的学习路径也大同小异,还是根据学生对应年级的知识点来走。 如果与有经验的辅导者一对一,也许一个课时就能把大概综合程度摸得七七八八并开始制订针对方案,不用说还有交流上的互相适应和熟悉。 选择题,在有限的考试时间内考察够多的知识点因而尽量减少学生的书写组织表达耗时,同时也降低了对表达和对题目理解的要求。 让学生从小就自我安排、规划时间这一最宝贵的财产、为大任务互相协商和分工,是很多家庭里忽视并难以组织的,完全可以与数学教学结合起来。教育者提供时间空间灵活性以发挥学生的能动创造性,当然也会对课时规划有更高要求。也许这是在无法改变教纲下的一种改良。 看美国的数学Common Core教纲,对小学初中阶段的进度似乎限制更大,到了高中则分几何代数等方面。而实际生活中很多任务是各知识点结合的,也可从小让其以已有知识进行观察和分析。 黑板报和班级日志等,是记录和表达的好途径,甚至可鼓励编写个人心得日志并互相交流,也可视为一种文(微型)史结合。

2025/6/5
articleCard.readMore

《laws of ux》选摘

仅选取感兴趣的部分浏览。英文部分为书中内容,中文部分为后感。 2020年《Laws of UX - Using Psychology to Design Better Products & Services》,作者:Jon Yablonski 前言 designers can, I believe, use psychology as a guide, enabling them to design for how people actually perceive, process, and interact not only with gigital interfaces but also with the world. 第一章 Jakob’s Law Users spend most of their time on other sites, and they prefer your site to work the same way as all the other sites they already know. 求同。 第四章 Miller’s Law The average person can keep only 7 (+/-2) items in their working memory. 第五章 Postel’s Law Be conservative in what you do, be liberal in what you accept from others. Anticipate virtually anything in terms of input, access, and capability while providing a reliable and accessible interface. 对各色输入尽量宽容,同时保持提供易理解的反馈。 第六章 Peak-End Rule People judge an experience largely based on how they felt at its peak and at its end, rather than on the total sum or average of every moment of the experience. Remember that people recall negative experiences more vividly than positive ones. 对负面体验更深刻,也许是进化中避险本能的一部分。但这在育儿时,不应放任其自溺于负面回忆中,而应以身作则看到无处不在的正面色彩。 在编程工具方面,用户最需要帮助的时刻之一应该是出错或与预期效果不同时。这也是现在多数ai编程工具提供的无缝反馈的价值所在,即无论什么要求(结合上一章),在工具内总有一定反馈。 第七章 Aesthetic-Usability Effect Users often perceive aesthetically pleasing design as design that’s more usable. People are more tolerant of minor usability issues when the design of a product or service is aesthetically pleasing. 第八章 von Restorff Effect When multiple similar objects are present, the one that differs from the rest is most likely to be remembered. 求异。 第九章 Tesler’s Law also known as the law of conservation of complexity, states that for any system there is a certain amount of complexity that cannot be reduced. All processes have a core of complexity that cannot be designed away and therefore must be assumed by either the system or the user. 对AI编程的过分乐观态度往往来自于在极短时间内生成大量看似可用的代码的「峰值」体验,而往往忽视一旦进入维护改进阶段就呈非线性上升的人力开销。更不用说并不自动保留、无法复现的开发过程。 第十章 Doherty Threshold Provide system feedback within 400 ms in order to keep users’ attention and increase productivity. 对于编程工具开发来说,可以尝试从语言环境层面提供一个「准实时系统」,即任何输入都可以在400ms内得到某个反馈,无论执行情况如何。即便是死循环,也可以提供一个旁白。 Purposefully adding a delay to a process can actually increase its percieved value and instill a sense of trust, even when the process itself actually takes much less time. 如果代码执行非常迅速并返回,可以考虑提供某些类似profile的深度反馈,用户可选择性查阅。比如「遍历了xx文件中的xxx行」。 第十一章 With Power Comes Responsibility Skinner’s research 《Addiction by Design》 Data collected about user behavior can be used to fine-tune how a system responds to an individual, and these methods are constantly incresing in sophistication and accuracy, while the psychological hardware we share as humans remains the same. 从古到今,用于操弄民意的手段其实一以贯之。网络只是一个超大的传声筒和及其灵活的声量放大/缩小工具。内容创造自动化实际上让更多民众更依赖于极少数信道,更降低了操弄信息交流的难度。 When FB introduced the ‘like’ button in 2009, they probably didn’t intend for it to become such an addictive feedback mechanism, providing a small dopamine hit of social affirmation to uses who found themselves returning to the app time an time again to measure their self-worth. we should build technology that augments the human experience rither than replacing it with virtual interaction and rewards. 当下的ai交互是加强还是削弱人际交流呢? No longer is “mosing fast and breaking things” an acceptable means of building technology-instead, we must slow down and be intentional with the technology we create, and consider how it’s impacting people’s lives. 此书出版后这这五年来,世界大环境的变化使得“慢下来”愈发成为奢侈。更多的资金和人力卷入到基于大数据和高算力的 ai 泡沫中。 We can make ethical design decisions by acknowledging how the human mind can be exploited and take accountability for our work by thinking beyond the happy path scenarios, building more diverse teams, and talking with users to gain qualitative feedback on how the products and experiences we build affect their lives. 首先要有和「真人」交互的机会,而现有大多数网络平台都被热榜的对立话题吸引了眼球,使得越来越少真人愿意参与发声以避负面言论冲击。通过蓝牙等本地组网 也许是条路。 第十二章 Applying Psychological Principles in Design According to Hick’s law (第三章),… To achive this goal, we must: Limit choices to no more than 3 items at a time. Provide brief explanations when useful that are clear and no more than 80 characters. 为减轻用户心智负担,选项和解释都会尽量简约。这就很考验语言的效率。在可读性方面中文有 相对优势。

2025/5/27
articleCard.readMore

陶哲轩的不等式证明工具当前版探究

在尝试 第一版 后,继续对照 5月9日博文「A tool to verify estimates, II: a flexible proof assistant」 提到的例程,对输出进行中文化。使用的是5月14日的源码内容,修改后的代码库 在此。最后是一些个人感受。 第一例 反证不等式 linarith_solution 输出如下: 开始证明。当前情况: x: pos_real y: pos_real z: pos_real h1: x < 2*y h2: y < 3*z + 1 求证:x < 7*z + 2 检验以下不等式可否成立: 1*x > 0 1*z > 0 1*y + -3*z < 1 1*x + -7*z >= 2 1*x + -2*y < 0 1*y > 0 以下式子取和,得证不可能成立: 1*z > 0 两边乘 1/4 1*y + -3*z < 1 两边乘 -1/2 1*x + -7*z >= 2 两边乘 1/4 1*x + -2*y < 0 两边乘 -1/4 证明完毕! 对反证法的使用未直接反映在输出中。取和方法与上一版相同。 当前版描述证明目标是这段: def linarith_exercise() -> ProofAssistant: p = ProofAssistant() x, y, z = p.vars("pos_real", "x", "y", "z") p.assume(x < 2 * y, "h1") p.assume(y < 3 * z + 1, "h2") p.begin_proof(x < 7 * z + 2) return p 对比前一版里这样写: def 一次不等式(): inequalities = set() inequalities.add(不等式({'x': 1}, 'gt', 0)) inequalities.add(不等式({'y': 1}, 'gt', 0)) inequalities.add(不等式({'z': 1}, 'gt', 0)) inequalities.add(不等式({'x': 1, 'y': -2}, 'lt', 0)) inequalities.add(不等式({'y': 1, 'z': -3}, 'lt', 1)) inequalities.add(不等式({'x': 1, 'z': -7}, 'geq', 2)) verbose_feasibility(inequalities) 直接写不等式使得可读性增加。是改用了与上一版的Variable-Expression等类似功能的SymPy库(Symbol-Basic等)。 求证目标用的是符号 - ,也许是Lean风格。中文化时改为「求证」。 分情况证不等式 split_solution 输出: 开始证明。当前情况: x: real y: real h1: (x > -1) & (x < 1) h2: (y > -2) & (y < 2) 求证:(x + y > -3) & (x + y < 3) 分情况 h1: (x > -1) & (x < 1) 为 x > -1, x < 1. 尚余一目标。 分情况 h2: (y > -2) & (y < 2) 为 y > -2, y < 2. 尚余一目标。 拆分目标为:x + y > -3, x + y < 3 尚余 2 目标。 目标由 linear arithmetic 求解! 尚余一目标。 目标由 linear arithmetic 求解! 证明完毕! 仅从这个输出看不清每步达成了哪个目标。 渐进不等式 中文化时将输出化为更接近数学公式的表达,如省去 **1 等: Theta(x)**1 * Theta(N)**-2 <= Theta(1) raised to power -1 => 𝚹(x) * 𝚹(N)^-2 <= 𝚹(1) 的 -1 次幂 loglinarith_solution 改为 p.use(LogLinarith(verbose=True)) 输出如下: 开始证明。当前情况: N: pos_int x: pos_real y: pos_real h1: x <= 2*N**2 h2: y < 3*N 求证:𝚹(x)*𝚹(y) <= 𝚹(N)^4 为反证,化为以下渐进不等式: ['𝚹(N) >= 𝚹(1)'] ['𝚹(x) * 𝚹(N)^-2 <= 𝚹(1)'] ['𝚹(y) * 𝚹(N)^-1 <= 𝚹(1)'] ['𝚹(x) * 𝚹(y) * 𝚹(N)^-4 > 𝚹(1)'] 检验以下不等式可否成立: 𝚹(N) >= 𝚹(1) 𝚹(x) * 𝚹(N)^-2 <= 𝚹(1) 𝚹(y) * 𝚹(N)^-1 <= 𝚹(1) 𝚹(x) * 𝚹(y) * 𝚹(N)^-4 > 𝚹(1) 以下之积可得矛盾: 𝚹(N) >= 𝚹(1) 的 1 次幂 𝚹(x) * 𝚹(N)^-2 <= 𝚹(1) 的 -1 次幂 𝚹(y) * 𝚹(N)^-1 <= 𝚹(1) 的 -1 次幂 𝚹(x) * 𝚹(y) * 𝚹(N)^-4 > 𝚹(1) 的 1 次幂 证明完毕! 此部分功能与第一版的类似。 继续逼近 目标是在上一例基础上作修改。这里想到,也许可基于这个自动工具的例程生成一些猜想。 loglinarith_hard_solution2 穷举法,输出90行。 比较 loglinarith_hard_solution 输出: 开始证明。当前情况: N: pos_int x: pos_real y: pos_real h1: x <= 2*N**2 + 1 h2: y < 3*N + 4 求证:𝚹(x)*𝚹(y) <= 𝚹(N)^3 将 h1: x <= 2*N**2 + 1 转化为渐进形式 h1_theta: 𝚹(x) <= Max(𝚹(1), 𝚹(N)^2). 尚余一目标。 将 h2: y < 3*N + 4 转化为渐进形式 h2_theta: 𝚹(y) <= Max(𝚹(1), 𝚹(N)). 尚余一目标。 可断言 𝚹(1) <= 𝚹(N). 尚余 2 目标。 由 log-linear arithmetic 达成目标! 尚余一目标。 可断言 𝚹(1) <= 𝚹(N)^2. 尚余 2 目标。 由 log-linear arithmetic 达成目标! 尚余一目标。 简化 𝚹(x) <= Max(𝚹(1), 𝚹(N)^2) 为 𝚹(x) <= 𝚹(N)^2,由于 𝚹(1) <= 𝚹(N)^2. 简化 𝚹(y) <= Max(𝚹(1), 𝚹(N)) 为 𝚹(y) <= 𝚹(N),由于 𝚹(1) <= 𝚹(N). 尚余一目标。 由 log-linear arithmetic 达成目标! 证明完毕! 可见在人工指导下的证明过程要简短的多,同时输出可读性仍有很大改进空间。 使用定理 amgm_solution 输出: 开始证明。当前情况: x: nonneg_real y: nonneg_real 求证:2*x*y <= x**2 + y**2 用定理 算术-几何均值不等式(x**2, y**2) 推导 this: x*y <= x**2/2 + y**2/2. 尚余一目标。 检验以下不等式可否成立: -1*x**2 + -1*y**2 + 2*x*y > 0 -1/2*x**2 + -1/2*y**2 + 1*x*y <= 0 1*y >= 0 1*x >= 0 以下式子取和,得证不可能成立: -1*x**2 + -1*y**2 + 2*x*y > 0 两边乘 1 -1/2*x**2 + -1/2*y**2 + 1*x*y <= 0 两边乘 -2 证明完毕! 要是能查看定理内容就更好了。 如果把使用定理这两行去掉: x, y = p.get_vars("x", "y") p.use_lemma(Amgm(x**2, y**2)) 输出如下: 开始证明。当前情况: x: nonneg_real y: nonneg_real 求证:2*x*y <= x**2 + y**2 Checking feasibility of the following inequalities: 1*x >= 0 -1*x**2 + -1*y**2 + 2*x*y > 0 1*y >= 0 Feasible with the following values: x*y = 0 y = 0 x = 0 y**2 = -1/2 x**2 = 0 看起来有问题。y 和 y**2 的值没有关联起来。回头看看有没有更简短的例子复现。 初感 在数学和编程结合方面,是一次非常珍贵的尝试。尤其是选取的内容与编程如算法复杂度很有关联,并且使用了接近初等数学公式的输入和输出格式,让更多人尤其是国内广大程序员群体对自动证明有了更直观的亲身体验。希望看到开枝散叶。 在中文化过程中,有些输出内容在不同文件中有重复,比如 Checking feasibility of the following inequalities: 在 linarith, linprog, log_linarith 中都有,也许是历史版本的冗余代码。 当前版用 SymPy 省去了自行定义各种基本类型,另一方面是对表达式符号的使用限制,比如 p.use_lemma(Amgm(x^2, y**2)) 就会报错:TypeError: unsupported operand type(s) for ^: 'Symbol' and 'int' main.py 还有很多其他例子未尝试。在继续开发之前,如果重构代码并编写批量自动测试集,相信会对后续参与开发者降低门槛,也利于长远发展。

2025/5/17
articleCard.readMore