Logic programming in Python
from pyDatalog import pyDatalog
factorial[N] = N*factorial[N-1]
factorial = 1
print(factorial==N) # prints N=6
pyDatalog extends Datalog with logic functions, an alternative to Prolog's cut
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.
pyDatalog can be used for:
- simulating intelligent behavior (for games or expert systems),
- querying complex sets of related information (e.g. in data integration or Natural Language Processing),
- performing recursive algorithms (e.g. on hierarchical data structure)
| Core technology in pyDatalog|| Benefits|| Sample applications|
|The resolution engine determines the best sequence of clauses to use to reach a goal||Spreadsheet-style programming : faster development; fewer bugs, easier to read afterwards||Rule-based models with many input and output, e.g. expert system for price calculation or tax planning, access right management, robot control, intelligent agent in games or automated assistants|
|The resolution engine can resolve recursive algorithm||Easy to write queries on hierarchical structure||Reporting with hierarchical reference data such as organisational structure|
|The same clause can solve various mixes of known and unknown parameters||Maximize code reuse : shorter program, simpler query language than SQL||Cross-database queries, data integration|
|Intermediate results are memoized.||Improved speed by avoiding duplicate work.||Business logic in 3-tier architecture.|
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_, mean_, linear_regression
- 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 of a clause : ~
pyDatalog is a fast and lightweight datalog interpreter written in Python:
- it can solve the 8-queen problem in 0.07 seconds on a PC
- it is thread safe
- it uses SLDNF resolution with tabling
- 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, 3.4, pypy 1.9 - 2.0, and SQLAlchemy 0.7 - 0.9, on Windows. (over 400 tests