Python 中的关系/逻辑编程?

发布于 2024-08-15 06:43:50 字数 285 浏览 2 评论 0原文

我是一名长期的 Python 开发人员,最近接触了 Prolog。我喜欢在某些类型的任务中使用关系规则的概念,并且想将其添加到我的技能中。

Python 有什么好的逻辑编程库吗?我在 Google 上进行了一些搜索,但只找到了以下内容:

jtauber's blog series onoretal_python

很想与其他一些...谢谢!

-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 repertoire.

Are there any good libraries for logic programming in Python? I've done some searching on Google but only found the following:

jtauber's blog series on relational_python

Would love to compare to some others...thanks!

-aj

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

故事灯 2024-08-22 06:43:50

您可能需要使用 pyDatalog,这是我为实现 数据日志。它还与 SQLAlchemy 配合使用逻辑子句查询关系数据库。

You may want to use pyDatalog, a logic programming library that I developed for Python implementing Datalog. It also works with SQLAlchemy to query relational databases using logic clauses.

花之痕靓丽 2024-08-22 06:43:50

也许你应该谷歌搜索“Python 逻辑编程”。 Pyke 看起来很有希望:

Pyke 引入了一种逻辑编程形式(受 Prolog 启发)
通过提供基于知识的推理引擎来建立 Python 社区
(专家系统)用 100% Python 编写。

与 Prolog 不同,Pyke 与 Python 集成,允许您调用 Pyke
来自 Python 并在其中混合 Python 语句和表达式
您的专家系统规则。

Perhaps you should google "Logic Programming in Python". Pyke looks promising:

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.

独﹏钓一江月 2024-08-22 06:43:50

在我们即将迈向 2019 年之际,我推荐 PySWIP,而不是此处推荐的其他工具。与 Pyke(9 年前)或 PyLog(6 年前)不同,它得到积极维护并具有简单的界面。

As we are heading toward 2019, I recommend PySWIP over others recommended here. It is actively maintained and has an easy interface, unlike Pyke (9 years ago) or PyLog (6 years ago).

一百个冬季 2024-08-22 06:43:50

LogPyminiKanren,一种关系型编程语言,Python 语言。它遵循卓越的逻辑编程 core.logic 的传统Clojure 中的解决方案。 LogPy 旨在与预先存在的代码库进行互操作。

LogPy is 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.

与风相奔跑 2024-08-22 06:43:50

另一个选项是 Yield Prolog

Another option is Yield Prolog

自由如风 2024-08-22 06:43:50

您还可以查看 Dee,它添加了与 Python 的关系:http://www.quicksort.co.uk

You could also look at Dee, which adds relations to Python: http://www.quicksort.co.uk

随遇而安 2024-08-22 06:43:50

最近在 Python(或者更确切地说 RPython)中实现的 Prolog
Pyrolog。它仍然是相当实验性的。

A recent Prolog implementation in Python (or rather RPython) in
Pyrolog. It is still rather experimental.

甜是你 2024-08-22 06:43:50

您还应该检查 PyLog:

http://cdsoft.fr/pylog/

它有一个非常干净和简单的语法和实施。

You should also check PyLog:

http://cdsoft.fr/pylog/

It has a very clean and simple syntax and implementation.

醉南桥 2024-08-22 06:43:50

你可以看看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']

它还支持计算和概率

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}]

它还可以用于查找图中节点之间的路径。

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']

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.

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']
被你宠の有点坏 2024-08-22 06:43:50

另一种选择是使用内存关系数据库。毕竟SQL是最流行的关系语言,它和Prolog有很多相似之处。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文