Logic programming in Python
from pyDatalog import pyDatalog
factorial[N] = N*factorial[N-1]
factorial = 1
print(factorial==N) # prints N=6
No if's, no loops, just the plain mathematical definition !
Easy to learn
Based on programmer-friendly Python
| Be productive
Write short, declarative programs
With full access to the extensive Python library
Order your statements to be literate
|Query 11 SQL databases
and noSQL, and Python classes
to implement expert systems and complex algorithms
Logic programmers can now use the extensive standard library of Python, and Python programmers can now express complex algorithms quickly.
is a truly declarative language derived from Prolog, with strong academic foundations
. Datalog excels at managing complexity. Datalog programs are shorter than their Python equivalent, and Datalog statements can be specified in any order, as simply as formula in a spreadsheet.
In particular, Datalog can be used for:
- simulating intelligent behavior (for games or expert systems),
- performing recursive algorithms (e.g. in network protocol, code and graph analysis),
- querying complex sets of related information (e.g. in data integration or Natural Language Processing),
- solving discrete constraint problems
pyDatalog embeds logic programming in Python :
- you can assert facts in a datalog knowledge base, and query it.
- you can use logic clauses to query any relational database via SQLAlchemy, the data access layer for Python which supports 11 dialects of SQL.
- you can define attributes of Python classes through logic clauses, and use logic queries on Python objects.
- you can use aggregate functions :
len_, sum_, concat_, min_, max_, tuple_, rank_, running_sum_
- you can define logic functions (e.g.
p[X]==Y) , i.e. logic predicate with unicity
- variables can represent nested lists that you can index and slice
- you can prefix literals and functions with a Python class name (e.g.
Employee.name[X]==Y): their first argument refers to instances of the class.
- prefixed literals and functions can be inherited from parent classes.
- you can use arithmetic literals
(X == Y+1). An expression can contain Python functions and lambda expressions
- you can negate a literal in the body: ~
pyDatalog is a fast and lightweight datalog interpreter written in Python:
- it can solve the 8-queen problem in 0.18 seconds on a PC
- it is thread safe
- it is based on the SLG resolution algorithm with memoization
- it uses indexes for the database of datalog facts
- it can run on pypy (Python with Just-In-Time compiler)
- it has hooks to let you rewrite performance-critical clauses in pure Python
- it has less than 2 K lines of executable code.
The depth of recursion is not limited by the stack size.
pyDatalog has been tested with python 2.7 - 3.3, pypy 1.9 - 2.0, and SQLAlchemy 0.7 - 0.9, on Windows. (over 400 tests