English 中文(简体)
Relational/Logic Programming in Python?
原标题:

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

最佳回答

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.

问题回答

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.

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

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.

Another option is Yield Prolog

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

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

You should also check PyLog:

http://cdsoft.fr/pylog/

It has a very clean and simple syntax and implementation.

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 ]

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.





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签