Entertainment at it's peak. The news is by your side.

Pylo: Python front-end for several Prolog engines


pylo is a Python entrance-finish for diverse Prolog engines.
It lets you write your program as soon as and extinguish it with diversified Prolog engines merely by switching the support-finish.

Supported OS: The library changed into as soon as tested on Linux (Ubuntu) and OSX.

Supported Prolog engines:

  • GNU Prolog (works perfect on OSX to this level; GNNU PROLOG’s foreign fair interface would now not bring collectively properly on Linux)
  • XSB Prolog

Under trend:

Per chance supported in the waste:

Set up with pip

STEP 1: pylo relies on ENV variables to detect which Prolog engines to enhance.
The desired Prolog engines can private to be installed first.

To put in the enhance for GNU Prolog, you private got to produce the GNUPROLOG_HOME variable pointing to the set up folder of GNU Prolog:

# For OSX with default configuration (installed from sources)
export GNUPROLOG_HOME=/usr/native/gprolog-1.4.5
# On Ubuntu
export GNUPROLOG_HOMe=/usr/lib/gprolog-1.4.5

You can moreover be attempting to search out the folder that contains the following:

COPYING     NEWS        VERSION     doc         gprolog.ico lib
ChangeLog   README      bin         examples    consist of

To put in the enhance for XSB_PROLOG, you private got to produce XSB_HOME variable pointing to the source of XSB Prolog.
That is the folder wherein you unpacked the XSB source files.
As an illustration

export XSB_HOME=/Users/user/Documents/programs/XSB

You can moreover be attempting to search out a folder that contains the following:

FAQ             README          cmplib          etc             lib             location
InstallXSB.jar  admin           config          examples        packages        syslib
LICENSE         bin             doctors            gpp             prolog-commons
Makefile        kind           emu             installer       prolog_includes

To put in the enhance for SWI Prolog, you private got to produce SWIPL_HOME variable pointing to the set up folder of SWIPL.
On OSX, this appears to be like like

# on OSX, (installed from Homebrew)
export SWIPL_HOME=/usr/native/Cellar/swi-prolog/8.2.0/libexec/lib/swipl

You can moreover be attempting to search out that contains the following objects:

LICENSE  bin        boot       boot.prc   customise  demo       doc        consist of    lib        library    swipl.home

On Ubuntu, it wants to be adequate to place it to /usr/lib

# On Ubuntu, installed from the repository
export SWIPL_HOME=/usr/lib

This folder can private to possess (Linux) file and the swi-prolog folder.

STEP 2: Clone this repository. You may perhaps private to pull it recursively to get the submodules.

git clone
git submodule init
git submodule substitute

STEP 3: Rush to the folder you cloned the repository to.


That’s it! You wants in enlighten to exercise Pylo now.

STEP 4 (elective): Check

from pylo.assessments import all_swipl_tests, all_gnu_tests, all_xsb_tests

all_xsb_tests("[path_to_XSB_folder]")  # the identical folder for set up


STEP 1: Pylo relies on ENV variables to configure name the main libraries.

To put in the enhance for GNU Prolog, you private got to produce the course to the GNu-prolog’s set up folder by the GNU_LIB_PATH.
As an illustration, using the default set up on OSX results in the following course

export GNU_LIB_PATH=/usr/native/gprolog-1.4.5

To put in the enhance for XSB Prolog, you private got to produce the course to the foreign interface library by the XSB_LIB_PATH.
To hit upon this folder, engage that incorporates the folder wherein the XSB source changed into as soon as unpacked.

export XSB_LIB_PATH=<XSB_ROOT>/config/<arch>

the set is the structure of your map (this may perhaps well well also be one of the best folder in /config folder).

To put in the enhance for SWI Prolog, you private got to produce the course to the SWIPL library by the SWIPL_LIB_PATH.
To search out the profitable folder, stare for the lib/ folder in the SWIPL set up folder ( is the structure of you map).
As an illustration, on OSX the EVN var can private to stare like this

export SWIPL_LIB_PATH=/usr/native/Cellar/swi-prolog/8.2.0/libexec/lib/swipl/lib/x86_64-darwin

STEP 2: As soon as the atmosphere variables were setup, that you may perhaps well well install pylo (and the specified Prolog engines) with the following instructions

mkdir kind
cd kind
cmake .. -D<prolog engine options>

The takes the following ticket:

  • -DGPROLOG=ON to put in the enhance for GNU-Prolog
  • -DXSBPROLOG=ON to put in the enhance for XSB Prolog
  • -DSWIPL=ON to put in the enhance for SWI Prolog

Important: due to incompatibility of the compiling alternate choices, that you may perhaps well well no longer specify all three alternate choices on the identical time.
Must it’s good to need to kind the enhance for diverse Prolog engines, you private got to bring collectively the engines individually (with perfect one flag specified on the time).

STEP 3: As soon as the compilation is carried out, create definite that the kind folder is obtainable by the PATH and PYTHONPATH variables.

Numerous purposeful concerns

  • In describe to exercise SWIPL, you private got to produce a course to the dynamic library using the LD_LIBRARY_PATH env var. As an illustration, on OSX
   export LD_LIBRARY_PATH=/usr/native/Cellar/swi-prolog/8.2.0/libexec/lib/swipl/lib/x86_64-darwin
  • For Ciao prolog, you also want in enlighten to search out the dynamic library. On OSX
    export LD_LIBRARY_PATH=/Users/seb/.ciaoroot/master/kind/eng/ciaoengine/objs/DARWINx86_64/

Language create

pylo follows the good judgment programming terminology, as a substitute of Prolog terminology,
The elemental constructs consist of terms:

  • constants: luke, leia, anakin or Python numbers like 1, -3, 3.14
  • variables: X, Y
  • buildings: sabre(green,long), date(2020,january,1)
  • lists (a special extra or much less structure): Listing([1,2,3,4,5])

To ticket clauses, one wants:

  • predicates: guardian, ...
  • literals:
    • atoms (predicate utilized to terms): guardian(vader,luke), mother(padme, leia)
    • negations of atoms: Not(guardian(vader,luke))
  • clauses: guardian(X,Y) :- mother(X,Y)

To gash the memory utilization, constants, variables, functors and predicates wants to be constructed by context:

  • constants: c_const('luke') creates a fixed luke (for numbers, perfect exercise Python buildings)
  • variables: c_var('X') creates a variables X
  • predicates: c_pred('guardian', 2) creates a predicate guardian with arity 2
  • functors: c_functor('date', 3) creates a functor date with arity 3

Must it’s good to need to exercise a proposition in a clause (a functor with out arguments), like a :- ..., develop it as a Predicate with arity 0.
Watch out to ticket clauses with literals, no longer buildings.

Specifying files

Pylo lets you with ease specify the files dreadful and the depend upon it with diversified prolog engines.
All frequent constructs (constants, variables, functors and predicates) wants to be created using the global context (capabilities prefixed with c_: c_const, c_pred, c_var, c_functor), which ensures that there are no longer duplicates.

from pylo import c_pred, c_var, c_const, c_functor, Atom, Clause, Conj, Construction, Listing

# develop some constants 
luke = c_const("luke")             
anakin = c_const("anakin")
leia = c_const("leia")
padme = c_const("padme")

# develop predicates
father = c_pred("father", 2)      
mother = c_pred("mother", 2)
guardian = c_pred("guardian", 2)

# develop literals
f1 = Atom(father, [anakin, luke])                    
f2 = Atom(father, [anakin, leia])
f3 = Atom(mother, [padme, luke])
f4 = Atom(mother, [padme, leia])

# develop Variables
X = c_var("X")                   
Y = c_var("Y")

# develop clauses 
head = Atom(guardian, [X,Y])                           
body1 = Atom(father, [X,Y])
body2 = Atom(mother, [X,Y])
rule1 = Clause(head, Conj(body1))
rule2 = Clause(head, Conj(body2))

# develop buildings
sabre = c_const("sabre")
green = c_const("green")
merchandise = c_functor("merchandise")

struct = Construction(merchandise, [sabre, green])

# develop lists
l = Listing([padme, leia, 1, 2, 3])

Such an verbalize object-oriented manner may perhaps well moreover be smartly-behaved for automatic constructing of programs.
Pylo also provides many convenient shortcuts for much less insensible constructing of files bases.
The above instance may perhaps well moreover were constructed in the following manner

from pylo import c_pred, c_const, c_var, c_functor, Listing

# ticket predicates
father = c_pred("father", 2)
mother = c_pred("mother", 2)
guardian = c_pred("guardian", 2)
belongs_to = c_pred("belongs_to", 2)

# develop info at the moment
#    making exercise of the predicate symbol to terms/strings creates a literal
#        it also converts strings to one of the best buildings
f1 = father("anakin", "luke")
f2 = father("anakin", "leia")
f3 = mother("padme", "luke")
f4 = mother("padme", "leia")

# develop buildings at the moment
#     making exercise of the functor to a chain of terms/strings creates a structure
#     it also knows techniques to convert strings to the profitable ticket
merchandise = c_functor("merchandise", 2)
struct = merchandise("sabre", "green")

f5 = belongs_to(struct, "luke")

# develop list
l = Listing(["padme", "leia", 1, 2, 3])  # if constants are numbers, perfect exercise python files buildings

# constructing clauses
rule1 = guardian("X", "Y") <= father("X", "Y")
rule2 = guardian("X", "Y") <= mother("X", "Y")

Querying Prolog

The first step is to develop a Prolog instance

from pylo.engines import SWIProlog
from pylo.engines import XSBProlog
from pylo.engines import GNUProlog

# Compose GNU Prolog instance
pl_gnu = GNUProlog()

# develop XSB Prolog
# you private got to produce the course
pl_xsb = XSBProlog("[path to the XSB folder used in installation]")

# develop SWI Prolog
# the course to SWIPL binary is elective if it corresponds
pl_swi = SWIProlog('/usr/native/bin/swipl') 

Two issues can private to be indispensable:

  • SWIProlog takes the course to the executable as an argument. If the course is frequent, i.e. is /usr/native/bin/swipl on OSX, no argument wants to be passed
  • XSBProlog takes a course to the XSB sources as an argument

All Prolog engines private a unified interface:

from pylo import Prolog
pl = Prolog()

# search the advice of file the advice of("")

# load module
# Pylo would now not present a standardization of modules accross diversified engines
#    this scheme that it's your job to create definite that the coolest mofule nam is supplied 
#    let's assume, GNU prolog would now not private modules
#                  XSB Prolog wants to specify which predicates to load frm the library, 
#                               right here's supplied as a further names arguments 'predicates', which takes a list of predicates

# asserta a reality or a clause

# assertz a reality or a clause

# win literal

# tell whether or no longer it's miles doable to meet a depend upon
# has_solution(...) can exercise any different of literals as an argument, which is interpreted as a conjunction

# depend upon all solutions
# depend upon(...) takes any different of literals as an enter, which is interpreted as a conjuncion
#       it moreover takes 'max_solutions' arguments, which is in a region to be feeble to restrict the different of solutions to stare for
pl.depend upon()

A extra elaborate instance

from pylo.engines import XSBProlog
from pylo import c_pred 

pl = XSBProlog("/Users/seb/Documents/programs/XSB")

particular person = c_pred("particular person", 1)
guests = c_pred("guests", 2)
stress = c_pred("stress", 1)
influences = c_pred("influences", 2)
smokes = c_pred("smokes", 1)
bronchial asthma = c_pred("bronchial asthma", 1)

pl.assertz(particular person("a"))
pl.assertz(particular person("b"))
pl.assertz(particular person("c"))
pl.assertz(guests("a", "b"))
pl.assertz(guests("a", "c"))

pl.assertz(stress("X") <= particular person("X"))
pl.assertz(influences("X", "Y") <= particular person("X") & particular person("Y"))
pl.assertz(smokes("X") <= stress("X"))
pl.assertz(smokes("X") <= guests("X", "Y") & influences("Y", "X") & smokes("Y"))
pl.assertz(bronchial asthma("X") <= smokes("X"))

query_p = particular person("X")
tv = pl.depend upon(query_p)
print("all other folks: ", tv)

query_f = guests("X", "Y")
tv = pl.depend upon(query_f)
print("all guests: ", tv)

query_st = stress("Y")
tv = pl.depend upon(query_st)
print("all pressured other folks: ", tv)

tv = pl.depend upon(influences("X", "Y"))
print("all influences: ", tv)

tv = pl.depend upon(smokes("X"))
print("all smokers: ", tv)

tv = pl.depend upon(bronchial asthma("X"), max_solutions=3)
print("all bronchial asthma: ", tv)
  • offering python capabilities as predicates
  • enhance for pairs ([|]/2 or ./2)

Read More

Leave A Reply

Your email address will not be published.