40

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

theheadofabroom
  • 20,639
  • 5
  • 33
  • 65
AJ.
  • 27,586
  • 18
  • 84
  • 94
  • 2
    wow man... i got respect for anyone who can even read prolog! +1 – Perpetualcoder Dec 16 '09 at 20:59
  • I gave it a try as well: https://github.com/evertheylen/logicpy. It tries to find a good balance between too much Python magic and too cumbersome to use. – Evert Heylen Nov 02 '17 at 14:55
  • Here is a credible "real world" example in a blog post. Looks decent, except the error messages (from validation) are done poorly (you get pass/fail, without explanation _why exactly_) https://jeffersonheard.github.io/2016/11/simplifying-complex-business-logic-with-pythons-kanren/ – user7610 Sep 21 '18 at 11:11

10 Answers10

14

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.

Pierre Carbonnelle
  • 2,305
  • 19
  • 25
12

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.

Cristian Ciupitu
  • 20,270
  • 7
  • 50
  • 76
Richie
  • 535
  • 3
  • 3
  • @Richie, looks like a useful package. Thanks for the pointer! – AJ. Dec 17 '09 at 03:31
  • from Pyke's page: "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." – heltonbiker Mar 26 '11 at 22:45
8

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

Zhanwen Chen
  • 1,295
  • 17
  • 21
5

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.

MRocklin
  • 55,641
  • 23
  • 163
  • 235
3

Another option is Yield Prolog

false
  • 10,264
  • 13
  • 101
  • 209
2

You should also check PyLog:

http://cdsoft.fr/pylog/

It has a very clean and simple syntax and implementation.

Bernát
  • 1,362
  • 14
  • 19
2

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

greg
  • 1,111
  • 14
  • 17
2

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

false
  • 10,264
  • 13
  • 101
  • 209
  • that's very cool, but does it inter-operate with python? it is written on top of pypy, which also supports (famously) a python implementation, but it's not clear to me that implies inter-op. also, while i am here, http://blog.herraiz.org/archives/238 is a few years old, but listed various options (it implies pyrolog inter-op, but also sounds like it is simply assumed because of pypy, which is what i am questioning). – andrew cooke Mar 29 '12 at 17:53
  • 1
    Proof-of-concept rather. So going with the soruces might permit that. – false Mar 29 '12 at 19:09
1

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']
mnoorfawi
  • 111
  • 1
  • 2
  • 10
0

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.

Pierre Carbonnelle
  • 2,305
  • 19
  • 25