Skip to content

Commit

Permalink
Add examples package (#179)
Browse files Browse the repository at this point in the history
Added the pyswip.examples.sudoku package
  • Loading branch information
yuce authored Oct 20, 2024
1 parent 660fa89 commit 3a8e115
Show file tree
Hide file tree
Showing 25 changed files with 285 additions and 549 deletions.
19 changes: 8 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,10 @@ Thanks to all [contributors](CONTRIBUTORS.txt).

```python
from pyswip import Prolog
prolog = Prolog()
prolog.assertz("father(michael,john)")
prolog.assertz("father(michael,gina)")
list(prolog.query("father(michael,X)")) == [{'X': 'john'}, {'X': 'gina'}]
for soln in prolog.query("father(X,Y)"):
Prolog.assertz("father(michael,john)")
Prolog.assertz("father(michael,gina)")
list(Prolog.query("father(michael,X)")) == [{'X': 'john'}, {'X': 'gina'}]
for soln in Prolog.query("father(X,Y)"):
print(soln["X"], "is the father of", soln["Y"])
# michael is the father of john
# michael is the father of gina
Expand All @@ -52,8 +51,7 @@ Assuming the filename "knowledge_base.pl" and the Python is being run in the sam

```python
from pyswip import Prolog
prolog = Prolog()
prolog.consult("knowledge_base.pl")
Prolog.consult("knowledge_base.pl")
```

### Foreign Functions
Expand All @@ -67,10 +65,9 @@ hello.arity = 1

registerForeign(hello)

prolog = Prolog()
prolog.assertz("father(michael,john)")
prolog.assertz("father(michael,gina)")
print(list(prolog.query("father(michael,X), hello(X)")))
Prolog.assertz("father(michael,john)")
Prolog.assertz("father(michael,gina)")
print(list(Prolog.query("father(michael,X), hello(X)")))
```

### Pythonic interface (Experimental)
Expand Down
8 changes: 4 additions & 4 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ The ones marked with (clp) requires `clp` library of SWI-Prolog.
* (clp) `coins/`
* (clp) `draughts/`
* `hanoi/` : Towers of Hanoi
* (clp) `sendmoremoney/` : if, SEND * MORE = MONEY, what is S, E, N, D, M, O, R, Y?
* (clp) `sudoku/` : Sudoku solver (Prolog code is contributed by Markus Triska)
* `create_term.py` : shows creating a Prolog term
* `register_foreign.py` : shows registering a foreign function
* (clp) `sendmoremoney/` : If, SEND * MORE = MONEY, what is S, E, N, D, M, O, R, Y?
* (clp) `sudoku/` : Moved to `pyswip.examples.sudoku` package
* `create_term.py` : Shows creating a Prolog term
* `register_foreign.py` : Shows registering a foreign function
7 changes: 3 additions & 4 deletions examples/coins/coins.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,16 @@


def main():
prolog = Prolog()
prolog.consult("coins.pl", relative_to=__file__)
Prolog.consult("coins.pl", relative_to=__file__)
count = int(input("How many coins (default: 100)? ") or 100)
total = int(input("What should be the total (default: 500)? ") or 500)
for i, soln in enumerate(prolog.query("coins(S, %d, %d)." % (count, total))):
for i, soln in enumerate(Prolog.query("coins(S, %d, %d)." % (count, total))):
S = zip(soln["S"], [1, 5, 10, 50, 100])
print(i, end=" ")
for c, v in S:
print(f"{c}x{v}", end=" ")
print()
list(prolog.query(f"coins(S, {count}, {total})."))
list(Prolog.query(f"coins(S, {count}, {total})."))


if __name__ == "__main__":
Expand Down
3 changes: 1 addition & 2 deletions examples/coins/coins_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@


def main():
prolog = Prolog()
prolog.consult("coins.pl", relative_to=__file__)
Prolog.consult("coins.pl", relative_to=__file__)
count = int(input("How many coins (default: 100)? ") or 100)
total = int(input("What should be the total (default: 500)? ") or 500)
coins = Functor("coins", 3)
Expand Down
4 changes: 1 addition & 3 deletions examples/create_term.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@


def main():
prolog = Prolog()

a1 = PL_new_term_refs(2)
a2 = a1 + 1
t = PL_new_term_ref()
Expand All @@ -42,7 +40,7 @@ def main():
PL_cons_functor_v(ta, assertz, t)
PL_call(ta, None)

print(list(prolog.query("animal(X,Y)", catcherrors=True)))
print(list(Prolog.query("animal(X,Y)", catcherrors=True)))


if __name__ == "__main__":
Expand Down
5 changes: 2 additions & 3 deletions examples/draughts/puzzle1.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@


def main():
prolog = Prolog()
prolog.consult("puzzle1.pl", relative_to=__file__)
Prolog.consult("puzzle1.pl", relative_to=__file__)

for soln in prolog.query("solve(B)."):
for soln in Prolog.query("solve(B)."):
B = soln["B"]

# [NW,N,NE,W,E,SW,S,SE]
Expand Down
11 changes: 4 additions & 7 deletions examples/father.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,16 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from __future__ import print_function
from pyswip import *


def main():
p = Prolog()

father = Functor("father", 2)
mother = Functor("mother", 2)

p.assertz("father(john,mich)")
p.assertz("father(john,gina)")
p.assertz("mother(jane,mich)")
Prolog.assertz("father(john,mich)")
Prolog.assertz("father(john,gina)")
Prolog.assertz("mother(jane,mich)")

Y = Variable()
Z = Variable()
Expand All @@ -47,7 +44,7 @@ def main():
q.closeQuery() # Newer versions of SWI-Prolog do not allow nested queries

print("\nQuery with strings\n")
for s in p.query("father(john,Y),mother(Z,Y)"):
for s in Prolog.query("father(john,Y),mother(Z,Y)"):
print(s["Y"], s["Z"])


Expand Down
5 changes: 2 additions & 3 deletions examples/hanoi/hanoi.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,11 @@ def draw(self):

def main():
n = 3
prolog = Prolog()
tower = Tower(n, True)
notifier = Notifier(tower.move)
registerForeign(notifier.notify)
prolog.consult("hanoi.pl", relative_to=__file__)
list(prolog.query("hanoi(%d)" % n))
Prolog.consult("hanoi.pl", relative_to=__file__)
list(Prolog.query("hanoi(%d)" % n))


if __name__ == "__main__":
Expand Down
5 changes: 2 additions & 3 deletions examples/hanoi/hanoi_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ def notify(t):

notify.arity = 1

prolog = Prolog()
registerForeign(notify)
prolog.consult("hanoi.pl", relative_to=__file__)
list(prolog.query(f"hanoi({N})"))
Prolog.consult("hanoi.pl", relative_to=__file__)
list(Prolog.query(f"hanoi({N})"))


if __name__ == "__main__":
Expand Down
3 changes: 1 addition & 2 deletions examples/register_foreign.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,5 @@ def atom_checksum(*a):
return False


p = Prolog()
registerForeign(atom_checksum, arity=2)
print(list(p.query("X='Python', atom_checksum(X, Y)", catcherrors=False)))
print(list(Prolog.query("X='Python', atom_checksum(X, Y)", catcherrors=False)))
8 changes: 3 additions & 5 deletions examples/register_foreign_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

# Demonstrates registering a Python function as a Prolog predicate through SWI-Prolog's FFI.

from __future__ import print_function
from pyswip.prolog import Prolog
from pyswip.easy import registerForeign

Expand All @@ -37,10 +36,9 @@ def hello(t):

def main():
registerForeign(hello)
prolog = Prolog()
prolog.assertz("father(michael,john)")
prolog.assertz("father(michael,gina)")
list(prolog.query("father(michael,X), hello(X)"))
Prolog.assertz("father(michael,john)")
Prolog.assertz("father(michael,gina)")
list(Prolog.query("father(michael,X), hello(X)"))


if __name__ == "__main__":
Expand Down
5 changes: 2 additions & 3 deletions examples/sendmoremoney/money.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@
from pyswip import Prolog

letters = list("SENDMORY")
prolog = Prolog()
prolog.consult("money.pl", relative_to=__file__)
for result in prolog.query("sendmore(X)"):
Prolog.consult("money.pl", relative_to=__file__)
for result in Prolog.query("sendmore(X)"):
r = result["X"]
for i, letter in enumerate(letters):
print(letter, "=", r[i])
Expand Down
3 changes: 1 addition & 2 deletions examples/sendmoremoney/money_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@

def main():
letters = list("SENDMORY")
prolog = Prolog()
sendmore = Functor("sendmore")
prolog.consult("money.pl", relative_to=__file__)
Prolog.consult("money.pl", relative_to=__file__)

X = Variable()
call(sendmore(X))
Expand Down
89 changes: 0 additions & 89 deletions examples/sudoku/sudoku.py

This file was deleted.

Loading

0 comments on commit 3a8e115

Please sign in to comment.