pyDatalog——Python逻辑编程引擎(一)

Datalog logic programming in python

from pyDatalog import pyDatalog pyDatalog.create_terms('factorial, N')   
factorial[N] = N*factorial[N-1]  
factorial[1] = 1   
print(factorial[3]==N)  # prints 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). It is inspired by LogicBlox.

Python中的Datalog逻辑编程

from pyDatalog import pyDatalog pyDatalog.create_terms('factorial, N')   
factorial[N] = N*factorial[N-1]  
factorial[1] = 1   
print(factorial[3]==N)  # prints N=6
  • 易学
    基于易于编写的Python语言
  • 高产
    编写简短的声明性程序
  • 标准
    可以完全访问诸多的Python库
  • 可读性
    要你进行文学编程
  • 查询11个SQL数据库
    以及NoSQL和Python类
  • 使用人工智能
    实现专家系统和复杂算法

pyDatalog以“Python式”的方式添加了逻辑编程的扩展工具箱。

现在,逻辑编程人员可以使用广泛的Python标准库,Python程序员可以快速表达复杂的算法。
Datalog是一种真正的声明性语言,它源于Prolog,具有强大的学术基础。Datalog擅长管理复杂性。Datalog程序比与之等效的Python程序要短,Datalog语句可以按任意顺序指定,简单得像电子表格中的公式。

pyDatalog可用于:

  • 模拟智能行为(用于游戏或专家系统),
  • 复杂关系查询(例如,在数据集成或自然语言处理中),
  • 执行递归算法(例如,在分层数据结构上)

pyDatalog源自John D.Ramsdell以前的工作。这是一个由Pierre Carbonnelle(比利时)领导的开源项目(采用LGPL)。它的灵感来自LogicBlox

Core technology

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中的核心技术 优点 示例应用程序
解析引擎确定用于实现目标的最佳子句序列 表格式编程:开发更快;
bug更少,可读性
基于规则的模型,具有许多输入和输出,例如价格计算或税务规划专家系统、访问权管理、机器人控制、游戏中的智能代理或自动助理
解析引擎可以解析递归算法 易于编写分层结构查询 使用分层参考数据(如组织结构)进行报告
相同的子句可以解决已知和未知参数的各种混合 最大限度地实现代码重用:更短的程序、比SQL更简单的查询语言 跨数据库查询、数据集成
记录中间结果 减少重复代码,高效工作 三层体系结构中的业务逻辑

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 a literal 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 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)

特性

pyDatalog在Python中嵌入逻辑编程:

  • 您可以在Datalog知识库中断言事实,并查询之。
  • 您可以使用逻辑子句通过SQLAlchemy查询任何关系数据库,SQLAlchemy是Python的数据访问层,支持11种SQL语言。
  • 您可以通过逻辑子句定义Python类的属性,并对Python对象使用逻辑查询。

具体地:

  • 可以使用聚合函数len_, sum_, concat_, min_, max_, tuple_, rank_, running_sum_, mean_, linear_regression
  • 可以定义逻辑函数(例如p[X]==Y),即具有唯一性的逻辑谓词。
  • 嵌套列表的切片索引可以作为变量。
  • 可以使用Python类名作为值和函数的前缀(例如Employee.name[X]==Y):它们的第一个参数引用类的实例。常量和函数的前缀可以从父类继承。
  • 可以用算式(X==Y+1)。表达式可以包含Python函数和lambda表达式
  • 您可以对子句主体中的值求反:~p(X)

pyDatalog是一个用Python编写的快速轻量级Datalog解释器:

  • 它可以在PC上在0.07秒内解决八皇后问题
  • 线程安全
  • 采用 归结的部分完备性(SLDNF) 进行求解和列表
  • 使用Datalog事实数据库的索引
  • 可以在pypy(带有即时编译器的Python)上运行
  • 有hooks,可以让您在纯Python中重写性能关键的子句
  • 它的可执行代码少于两千行。

递归的深度不受堆栈大小的限制。

pyDatalog已经在Windows上使用Python2.7、3.3、3.4、PyPy1.9-2.0和SQLAlchemy 0.7-0.9进行了测试。(超过400项测试)

Guess you like

Origin blog.csdn.net/dscn15848078969/article/details/121549226