Skip to content

Instantly share code, notes, and snippets.

@yuce
Created December 10, 2024 19:08
Show Gist options
  • Save yuce/85d121e756d191b63fca7520e777a968 to your computer and use it in GitHub Desktop.
Save yuce/85d121e756d191b63fca7520e777a968 to your computer and use it in GitHub Desktop.
Python interface example for using SWI-Prolog modules with PySwip
from pyswip import Prolog, Functor, Variable, Atom, Query
from pyswip.easy import module, call
_ = Prolog()
assertz = Functor("assertz")
if_ = Functor(":-", arity=2)
or_ = Functor(";", arity=2)
parent = Functor("parent", 2)
father = Functor("father", 2)
mother = Functor("mother", 2)
m1 = module("m1")
A = Variable()
B = Variable()
call(assertz(if_(parent(A, B), or_(father(A, B), mother(A, B)))), module=m1)
call(assertz(father("f", "c")), module=m1)
call(assertz(mother("m", "c")), module=m1)
X = Variable()
Y = Variable()
q = Query(parent(X, Y), module=m1)
while q.nextSolution():
print(f"{X.value} parent_of {Y.value}")
q.closeQuery()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment