Lorelai aims to be Keras for Logical reasoning and learning in AI. It provides a unified language for expressing logical theories and connects it to various backends (Prolog, Answer Set Programming, Datalog, ...) to reason with the provided theories.
THIS IS STILL WORK IN PROGRESS, EXPECT CHANGES!
loreleai
depends on pylo to interface with Prolog engines.
Follow the instructions to install pylo
here.
If you will be using a Datalog engine, follow the instructions to install z3.
Then clone this repository and run
pip install .
loreleai
allows you to easy specify you knowledge and ask queries about it
More details: for more details on usage of logic and Prolog engines, check the instructions of pylo
from loreleai.language.lp import c_const, c_var, c_pred
from loreleai.reasoning.lp.datalog import MuZ
p1 = c_const("p1") # create a constant with the name 'p1'
p2 = c_const("p2")
p3 = c_const("p3")
parent = c_pred("parent", 2) # create a predicate/relation 'parent'
grandparent = c_pred("grandparent", 2)
f1 = parent(p1, p2) # create the fact 'parent(p1, p2)'
f2 = parent(p2, p3)
V1 = c_var("X") # create a variable named 'X'
V2 = c_var("Y")
V3 = c_var("Z")
# create a clause defining the grandparent relation
cl = (grandparent(V1, V3) <= parent(V1, V2) & parent(V2, V3))
solver = MuZ() # instantiate the solver
# Z3 datalog (muZ)I pro
solver.assert_fact(f1) # assert a fact
solver.assert_fact(f2)
solver.assert_rule(cl) # assert a rule
solver.has_solution(grandparent(p1, p3))# ask whether there is a solution to a query
solver.query(parent(V1, V2)) # ask for all solutions
solver.query(grandparent(p1, V1), max_solutions=1)# ask for a single solution
Alternatively, loreleai
provides shortcuts to defining facts
from loreleai.language.lp import c_pred
parent = c_pred("parent", 2) # create a predicate/relation 'parent'
grandparent = c_pred("grandparent", 2)
f1 = parent("p1", "p2") # 'p1' and 'p2' are automatically parsed into a Constant
f2 = parent("p2", "p3")
query_literal = grandparent("p1", "X") # 'X' is automatically parsed into a Variable
Reasoning engines are located in loreleai.reasoning.lp
followed by the specific type of logic programming
prolog
: supported Prolog engines:SWIProlog
for SWI PrologGNUProlog
for GNU PrologXSBProlog
for XSB Prolog
datalog
: supported Datalog engines:MuZ
for Z3's datalog engine
kanren
: for relational programmingMiniKanren
Currently supported (via pylo):
Prolog without side-effects (cut and so on)
Currently supported:
- miniKanren; seems to be actively maintained
A subset of Prolog without functors/structures
Currently supported:
Considering:
Currently supported:
- none yet
Considering:
Currently supported:
- none yet
Considering:
- integrate one solver for each of the representative categories
- add support for external predicates (functionality specified in Python)
- SWI prolog wrapper
- include probabilistic engines (Problog, PSL, MLNs)
- add parsers for each dialect
- different ways of loading data (input language, CSV, ...)
- add learning primitives such as search, hypothesis space generation
- wrap state of the art learners (ACE, Metagol, Aleph)
The language constructs are in loreleai/language
folder.
There is a folder for each dialect of first-order logic.
Currently there are logic programming (loreleai/language/lp
) and relational programming (loreleai/language/kanren
).
The implementations of all shared concepts are in loreleai/language/commons.py
and the idea is to use __init__.py
files to provide the allowed constructs for each dialect.
The reasoning constructs are in loreleai/reasoning
folder.
The structure is the same as with language.
Different dialects of logic programming are in the folder lorelai/reasoning/lp
.
The learning primitives are supposed to be in the loreleai/learning
folder.
- pyswip
- problog
- ortools
- minikanren
- z3-solver
- black
For using SWI prolog, check the install instructions: https://github.com/yuce/pyswip/blob/master/INSTALL.md
Z3Py scripts stored in arbitrary directories can be executed if the 'build/python' directory is added to the PYTHONPATH environment variable and the 'build' directory is added to the DYLD_LIBRARY_PATH environment variable.