Datalog logic programming in python
Logic programming in Python
>>> from pyDatalog import pyDatalog
>>> pyDatalog.create_symbols('factorial, N')
>>> factorial[N] = N*factorial[N-1]
>>> factorial[1] = 1
>>> print(factorial[3]==N)
N
-
6
- Easy to learn: Based on programmer-friendly Python.
- Be productive: Write short, declarative programs.
- Batteries included: With full access to the extensive Python library.
- Be readable: Order your statements to be literate.
- Query 11 SQL databases: and noSQL, and Python classes.
- Use AI: to implement expert systems and complex algorithms. pyDatalog adds the logic programming paradigm to Python's extensive toolbox, in a pythonic way.
Logic programmers can now use the extensive standard library of Python, and Python programmers can now express complex algorithms quickly.
Datalog 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)
pyDatalog is derived from previous work by John D. Ramsdell. It is an open-source project (LGPL) lead by Pierre Carbonnelle (in Belgium).
Core technology
pyDatalog uses a tabled resolution algorithm based on SLG resolution, simplified to support negation as failure on stratified programs only.
| 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 . |
Features
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.
More specifically:
- 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 an atom in the body of a clause :
~p(X)
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 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.
Testimonials
pyDatalog is cited in several scientific papers !
"Very neat. Datalog rocks" - Marck Carter. He has written a nice blog entry on unit conversion with Datalog.
"Congratulations for your wonderful work done in pyDatalog project" - Karamajit Kaur, researcher in India
You are also using pyDatalog ? Let us know.