python 中的命题演算
我正在寻找一个在 python 中工作的命题微积分模块。
我的用户需要在文本区域中输入公式,然后我必须检查它是否正确。
我无法直接测试输入文本是否等于正确的文本,因为它没有考虑排列或此类因素。
这样的模块存在吗?
- 编辑-
这是该项目的屏幕截图(设计未完成):
I'm looking for a propositional calculus module that works in python.
My users need to input a formula in a text area, then I have to check whether it's correct or not.
I cannot directly test if the input text equals the correct one as it didn't take permutations or such things into account.
Does such a module exist ?
- EDIT -
Here is a screenshot of the project (design not complete) :
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这并不太难。您所需要做的就是(a)找到或(b)编写一个实用程序,该实用程序接受任意命题并生成真值表。然后,对于两个命题,您所需要做的就是生成两个真值表并检查所有行中的原子变量和最后一列是否匹配。
原子变量的数量是 O(2^n),并且假设每个命题包含相同数量的原子变量。如果可能包含额外无用的原子变量(例如 OR(b 或 NOT b)相当于 a),则需要填充更简单命题的真值表以获得相同的行数。如果允许使用不同的原子变量,那么这会变得更加困难。
假设 P != NP,你不可能做得比 O(2^n) 更好,因为多项式解可以解决命题演算的一般可满足性问题。
要生成真值表,您需要 (a) 生成原子变量真值的所有 2^n 排列的列表(有很多方法可以做到这一点),以及 (b) 评估对真值的任意赋值的命题原子变量。然后制作两个表格并进行比较。瞧!
This isn't too hard. All you need to do is to either (a) find or (b) write a utility that takes in an arbitrary proposition and produces a truth table. Then, for two propositions, all you need to do is to generate two truth tables and check that the the atomic variables and last column match in all rows.
This is O(2^n) in the number of atomic variables, and assumes that each proposition contains the same number of atomic variables. If extra useless atomic variables may be included (like a OR (b or NOT b) is equivalent to a), you will need to pad the truth tables of the simpler proposition in order to get the same number of rows. If different atomic variables are allowed to be used, then this gets even harder.
You cannot do better than O(2^n), assuming P != NP, since a polynomial solution would solve the general satisfiability problem over propositional calculus.
To generate a truth table, you need to (a) generate a list of all 2^n permutations of truth values of the atomic variables (lots of ways to do this), and (b) evaluate propositions for arbitary assignments to the truth values of atomic variables. Then just make both tables and compare. Voila!
我刚刚偶然发现了这个问题。不知道是否还需要答案,但我建议使用 SymPy:
http:// /docs.sympy.org/dev/modules/logic.html
i just stumpled upon this question. don't know if an answer is needed anymore, but I would suggest using SymPy:
http://docs.sympy.org/dev/modules/logic.html
A,B,C ...在您提供的示例中似乎是集合,
不是提议。这是有可能的
也关于这些类型的陈述,但不是
据我所知,命题逻辑。
从语义上比较这些陈述,这就是
你想要在这里,需要更复杂的逻辑,
但更简单的方法也许是重写
所有陈述都采用可通过纯文本进行比较的形式
比较。即通过忽略交换律,这
语句
也将与此语句相同
即使这不是一个完美的设置, ,因为可能
是不被认可的等效陈述,
使用逻辑等价来解决这个问题的过程将
会困难得多。使用重写逻辑或多或少
只需很少的努力就能得到你想要的东西。基本上都是你
需要指定哪些二元运算符是
可交换的。重写等价的几个方程
还添加了语句,您可能需要添加更多...
我用 Maude http://maude.cs.uiuc.edu/ 写了一些东西
A, B, C... in the example you present seem to be sets,
not propositions. it's possible to reason
about these types of statements as well, but not as
propositional logic, as far as I can see.
comparing these statements semantically, which is what
you want here, would require a more complex logic,
but an easier way would perhaps be to rewrite
all statements to a form comparable through a plain text
comparison. I.e. by ignoring commutativity, this
statement
would be the same as this statement
even though this is not a perfect setup, since there might
be equivalent statements which are not recognized, the
process of figuring this out using logical equivalence would
be much harder. using rewriting logic does more or less
what you want with very little effort. basically all you
need is to specify which of the binary operators which are
commutative. a few equations which rewrite equivalent
statements are also added, you may have to add more...
i've written up something in Maude http://maude.cs.uiuc.edu/