python Python中的关系/逻辑编程?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1917607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Relational/Logic Programming in Python?
提问by AJ.
I'm a longtime python developer and recently have been introduced to Prolog. I love the concept of using relationship rules for certain kinds of tasks, and would like to add this to my reperttheitroade.
我是一名长期的 Python 开发人员,最近被介绍到 Prolog。我喜欢将关系规则用于某些类型的任务的概念,并想将其添加到我的曲目中。
Are there any good libraries for logic programming in Python? I've done some searching on Google but only found the following:
Python 中是否有用于逻辑编程的好库?我在谷歌上做了一些搜索,但只找到了以下内容:
jtauber's blog series on relational_python
jtauber 关于relational_python 的博客系列
Would love to compare to some others...thanks!
很想与其他人进行比较......谢谢!
-aj
-aj
采纳答案by Richie
Perhaps you should google "Logic Programming in Python". Pykelooks promising:
也许你应该谷歌“Python 中的逻辑编程”。Pyke看起来很有希望:
Pyke introduces a form of Logic Programming (inspired by Prolog) to the Python community by providing a knowledge-based inference engine (expert system) written in 100% Python.
Unlike Prolog, Pyke integrates with Python allowing you to invoke Pyke from Python and intermingle Python statements and expressions within your expert system rules.
Pyke 通过提供 100% Python 编写的基于知识的推理引擎(专家系统),向 Python 社区引入了一种逻辑编程形式(受 Prolog 启发)。
与 Prolog 不同,Pyke 与 Python 集成,允许您从 Python 调用 Pyke 并在您的专家系统规则中混合 Python 语句和表达式。
回答by user474491
回答by Zhanwen Chen
回答by MRocklin
LogPyis an implementation of miniKanren, a relational programming language, in Python. It follows in th tradition of core.logic
, the preeminent logic programming solution in Clojure. LogPy was designed for interoperability with pre-existing codebases.
LogPy是miniKanren(一种关系编程语言)在 Python 中的实现。它遵循core.logic
Clojure 中卓越的逻辑编程解决方案的传统。LogPy 旨在实现与预先存在的代码库的互操作性。
回答by false
Another option is Yield Prolog
另一种选择是Yield Prolog
回答by Bernát
You should also check PyLog:
您还应该检查 PyLog:
It has a very clean and simple syntax and implementation.
它有一个非常干净和简单的语法和实现。
回答by greg
You could also look at Dee, which adds relations to Python: http://www.quicksort.co.uk
您还可以查看 Dee,它为 Python 添加了关系:http: //www.quicksort.co.uk
回答by false
A recent Prolog implementation in Python (or rather RPython) in Pyrolog. It is still rather experimental.
最近在Pyrolog 中用Python(或者更确切地说是 RPython)实现的 Prolog 实现 。它仍然是相当实验性的。
回答by m.noor
You can have a look at pytholog. It is written in python totally with no interfaces with prolog and it mimics prolog's syntax, approach and backtracking. Simply initiate a KnowledgeBase and feed it with facts and rules then run queries.
你可以看看pytholog。它完全用 python 编写,没有与 prolog 的接口,它模仿 prolog 的语法、方法和回溯。只需启动一个知识库并为其提供事实和规则,然后运行查询。
import pytholog as pl
food_kb = pl.KnowledgeBase("food")
food_kb(["food_type(gouda, cheese)",
"food_type(ritz, cracker)",
"food_type(steak, meat)",
"food_type(sausage, meat)",
"food_type(limonade, juice)",
"food_type(cookie, dessert)",
"flavor(sweet, dessert)",
"flavor(savory, meat)",
"flavor(savory, cheese)",
"flavor(sweet, juice)",
"food_flavor(X, Y) :- food_type(X, Z), flavor(Y, Z)"])
print(food_kb.query(pl.Expr("food_flavor(What, sweet)")))
# [{'What': 'limonade'}, {'What': 'cookie'}]
print(food_kb.query(pl.Expr("flavor(sweet, dessert)")))
# ['Yes']
It also supports calculations and probabilities
它还支持计算和概率
battery_kb = pl.KnowledgeBase("battery")
battery_kb([
"battery(dead, P) :- voltmeter(battery_terminals, abnormal, P2), P is P2 + 0.5",
"battery(dead, P) :- electrical_problem(P), P >= 0.8",
"battery(dead, P) :- electrical_problem(P2), age(battery, old, P3), P is P2 * P3 * 0.9",
"electrical_problem(0.7)",
"age(battery, old, 0.8)",
"voltmeter(battery_terminals, abnormal, 0.3)"])
battery_kb.query(pl.Expr("battery(dead, Probability)"))
# [{'Probability': 0.8}, {'Probability': 'No'}, {'Probability': 0.504}]
It can also be used to find a path between nodes in graphs.
它还可以用于查找图中节点之间的路径。
graph = pl.KnowledgeBase("graph")
graph([
"edge(a, b, 6)", "edge(a, c, 1)", "edge(b, e, 4)",
"edge(b, f, 3)", "edge(c, d, 3)", "edge(d, e, 8)",
"edge(e, f, 2)",
"path(X, Y, W) :- edge(X , Y, W)",
"path(X, Y, W) :- edge(X, Z, W1), path(Z, Y, W2), W is W1 + W2"])
answer, path = graph.query(pl.Expr("path(a, f, W)"), show_path = True)
print(answer)
# [{'W': 9}, {'W': 12}, {'W': 14}]
print([x for x in path if str(x) > "Z"])
# ['d', 'b', 'e', 'c']
answer, path = graph.query(pl.Expr("path(a, e, W)"), show_path = True, cut = True)
print(answer)
# [{'W': 10}]
print([x for x in path if str(x) > "Z"])
# ['b']
回答by user474491
Another option is to use in-memory relational databases. After all, SQL is the most popular relational language, and it has a lot of similarity with Prolog.
另一种选择是使用内存关系数据库。毕竟SQL是最流行的关系语言,它和Prolog有很多相似之处。