Skip to content

Commit

Permalink
Updates (#191)
Browse files Browse the repository at this point in the history
Python to Prolog value exchange using string interpolation
  • Loading branch information
yuce authored Oct 27, 2024
1 parent 4441ad7 commit cc850b2
Show file tree
Hide file tree
Showing 31 changed files with 672 additions and 240 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ See the [Change Log](https://pyswip.org/change-log.html).

If you have SWI-Prolog installed, it's just:
```
pip install pyswip
pip install -U pyswip
```

See [Get Started](https://pyswip.readthedocs.io/en/latest/get_started.html) for detailed instructions.
Expand Down
14 changes: 14 additions & 0 deletions docs/source/api/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,17 @@ Sudoku

.. automodule:: pyswip.examples.sudoku
:members:

Hanoi
^^^^^

.. automodule:: pyswip.examples.hanoi
:members:


Coins
^^^^^

.. automodule:: pyswip.examples.coins
:members:

5 changes: 3 additions & 2 deletions docs/source/get_started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ PySwip is available to install from `Python Package Index <https://pypi.org/proj

You can install PySwip using::

pip install pyswip
pip install -U pyswip

You will need to have SWI-Prolog installed on your system.
See :ref:`install_swi_prolog`.
Expand Down Expand Up @@ -153,8 +153,9 @@ SWI-Prolog can be installed using ``pkg``::
Test Drive
----------

Run a quick test by running following code at your Python console::
Run a quick test by running following code at your Python console:

.. code-block:: python
from pyswip import Prolog
Prolog.assertz("father(michael,john)")
Expand Down
1 change: 1 addition & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ It features an SWI-Prolog foreign language interface, a utility class that makes
:caption: Contents:

get_started
value_exchange
api/modules

Indices and Tables
Expand Down
65 changes: 65 additions & 0 deletions docs/source/value_exchange.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
Value Exchange Between Python and Prolog
========================================

String Interpolation from Python to Prolog
------------------------------------------

Currently there's limited support for converting Python values automatically to Prolog via a string interpolation mechanism.
This mechanism is available to be used with the following ``Prolog`` class methods:

* ``assertz``
* ``asserta``
* ``retract``
* ``query``

These methods take one string format argument, and zero or more arguments to replace placeholders with in the format to produce the final string.
Placeholder is ``%p`` for all types.

The following types are recognized:

* String
* Integer
* Float
* Boolean
* ``pyswip.Atom``
* ``pyswip.Variable``
* Lists of the types above

Other types are converted to strings using the ``str`` function.

.. list-table:: String Interpolation to Prolog
:widths: 50 50
:header-rows: 1

* - Python Value
- String
* - str ``"Some value"``
- ``"Some value"``
* - int ``38``
- ``38``
* - float ``38.42``
- ``38.42``
* - bool ``True``
- ``1``
* - bool ``False``
- ``0``
* - ``pyswip.Atom("carrot")``
- ``'carrot'``
* - ``pyswip.Variable("Width")``
- ``Width``
* - list ``["string", 12, 12.34, Atom("jill")]``
- ``["string", 12, 12.34, 'jill']``
* - Other ``value``
- ``str(value)``


The placeholders are set using ``%p``.

Example:

.. code-block:: python
ids = [1, 2, 3]
joe = Atom("joe")
Prolog.assertz("user(%p,%p)", joe, ids)
list(Prolog.query("user(%p,IDs)", joe))
4 changes: 2 additions & 2 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ This directory contains examples for PySwip.

The ones marked with (clp) requires `clp` library of SWI-Prolog.

* (clp) `coins/`
* (clp) `coins/` : Moved to `pyswip.examples.coins` package
* (clp) `draughts/`
* `hanoi/` : Towers of Hanoi
* `hanoi/` : Moved to `pyswip.examples.sudoku` package
* (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
Expand Down
8 changes: 4 additions & 4 deletions examples/coins/coins.pl
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

:- use_module(library('bounds')).

coins(S, Count, Total) :-
coins(Count, Total, Solution) :-
% A=1, B=5, C=10, D=50, E=100
S = [A, B, C, D, E],
Solution = [A, B, C, D, E],

Av is 1,
Bv is 5,
Expand All @@ -31,8 +31,8 @@
VD #= D*Dv,
VE #= E*Ev,

sum(S, #=, Count),
sum(Solution, #=, Count),
VA + VB + VC + VD + VE #= Total,

label(S).
label(Solution).

41 changes: 0 additions & 41 deletions examples/coins/coins.py

This file was deleted.

2 changes: 1 addition & 1 deletion examples/coins/coins_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def main():
total = int(input("What should be the total (default: 500)? ") or 500)
coins = Functor("coins", 3)
S = Variable()
q = Query(coins(S, count, total))
q = Query(coins(count, total, S))
i = 0
while q.nextSolution():
## [1,5,10,50,100]
Expand Down
2 changes: 1 addition & 1 deletion examples/create_term.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
# SOFTWARE.

from pyswip.core import *
from pyswip.prolog import Prolog
from pyswip import Prolog


def main():
Expand Down
2 changes: 1 addition & 1 deletion examples/draughts/puzzle1.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
# are four guards watching each wall. How can they be rearranged such
# that there are five watching each wall?"

from pyswip.prolog import Prolog
from pyswip import Prolog


def main():
Expand Down
87 changes: 0 additions & 87 deletions examples/hanoi/hanoi.py

This file was deleted.

40 changes: 0 additions & 40 deletions examples/hanoi/hanoi_simple.py

This file was deleted.

4 changes: 2 additions & 2 deletions examples/knowledgebase.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from pyswip import *
from pyswip import Prolog, Functor, Variable, Query, newModule, call


def main():
_ = Prolog()
_ = Prolog() # not strictly required, but helps to silence the linter

assertz = Functor("assertz")
parent = Functor("parent", 2)
Expand Down
7 changes: 2 additions & 5 deletions examples/register_foreign_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,8 @@
from pyswip.easy import registerForeign


def hello(t):
print("Hello,", t)


hello.arity = 1
def hello(who):
print("Hello,", who)


def main():
Expand Down
Loading

0 comments on commit cc850b2

Please sign in to comment.