# Problem with unicode characters (e.g. →) in prolog file on swiplserver

**URL:** <https://swi-prolog.discourse.group/t/problem-with-unicode-characters-e-g-in-prolog-file-on-swiplserver/4945>\
**Category:** Help!\
**Created:** [January 27, 2022, 10:56am UTC](https://swi-prolog.discourse.group/t/problem-with-unicode-characters-e-g-in-prolog-file-on-swiplserver/4945 "2022-01-27T10:56:21Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Losbarthos](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/losbarthos/32/4037_2.png) [@Losbarthos](https://swi-prolog.discourse.group/u/Losbarthos)\
**Post date:** [January 27, 2022, 10:56am UTC](https://swi-prolog.discourse.group/t/problem-with-unicode-characters-e-g-in-prolog-file-on-swiplserver/4945/1 "2022-01-27T10:56:21Z")

</div>

I have some simple prolog file logic.pl introducing some operator with unicode characters:

```prolog
:-op(803, xfy, →).

binary_connective(X → Y, X, Y).

```

The problem now is, that my python query does not work on that operator:

```prolog
from swiplserver import PrologMQI, PrologThread

with PrologMQI() as mqi:
    with mqi.create_thread() as prolog_thread:
    	prolog_thread.query(f"consult('logic.pl').")
    	prolog_thread.query_async("binary_connective(p → q, X, Y).",
                                  find_all=False)
    	while True:
    		result = prolog_thread.query_async_result()
    		if result is None:
    			break
    		else:
    			print(result)

```

If I change → in – it gives me some working result.

Is their some way to solve this in swiplserver libary?

I also made some post on this site ([Problem with unicode characters (e.g. →) in prolog file · Issue #14 · SWI-Prolog/packages-mqi · GitHub](https://github.com/SWI-Prolog/packages-mqi/issues/14))

---

<div class="post-metadata">

**Author:** ![ericzinda](https://avatars.discourse-cdn.com/v4/letter/e/b3f665/32.png) [@ericzinda](https://swi-prolog.discourse.group/u/ericzinda)\
**Post date:** [January 27, 2022, 5:45pm UTC](https://swi-prolog.discourse.group/t/problem-with-unicode-characters-e-g-in-prolog-file-on-swiplserver/4945/3 "2022-01-27T17:45:19Z")

</div>

> [@Losbarthos](#):
>
> that my python query does not work on that operator:

What is the error you see when you run the query from Python?

Also: Does it work if you run it from the top level (it should (almost) always work the same if you are using the MQI library)? I.e.:

```prolog
?- consult('logic.pl').
true

?- binary_connective(p → q, X, Y).
????

```

Edit: If I try putting your code, exactly as you have it above, in a `logic.pl` file and consulting it, I get the following error. I may have missed some magic unicode incantation though, I haven’t done anything with unicode and SWI Prolog before:

```prolog
ERROR: logic.pl:1:17: Syntax error: illegal_character
ERROR: logic.pl:3:21: Syntax error: Operator expected
true.

```

---

<div class="post-metadata">

**Author:** ![Losbarthos](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/losbarthos/32/4037_2.png) [@Losbarthos](https://swi-prolog.discourse.group/u/Losbarthos)\
**Post date:** [January 27, 2022, 9:04pm UTC](https://swi-prolog.discourse.group/t/problem-with-unicode-characters-e-g-in-prolog-file-on-swiplserver/4945/4 "2022-01-27T21:04:34Z")

</div>

Hello,  
thank you for your response. I don’t get some error if I put the predicate directly into swipl with  
SWI-Prolog version 8.2.4 for x86\_64-linux.

My prolog file simply contains the following content

```prolog
:-op(803, xfy, →).

binary_connective(X → Y, X, Y).

```

My output in swipl is:

```prolog
?- consult('logic.pl').
true.

?- binary_connective(p → q, X, Y).
X = p,
Y = q.

```

I also tested it on [https://swish.swi-prolog.org/](https://swish.swi-prolog.org/) and it worked exactly the same.

If I run the query from Python I don’t get some error. It seems the program stucks at some point in some endless loop.

How can I run the program from toplevel? Did you mean use:

```prolog
from swiplserver import PrologServer, PrologThread

with PrologServer() as server:
    with server.create_thread() as prolog_thread:
        prolog_thread.query("consult('logic.pl').")
        result = prolog_thread.query("binary_connective(p → q, X, Y).")
        print(result)

```

This doesn’t work, cause of error  
ImportError: cannot import name ‘PrologServer’ from ‘swiplserver’ (/home/~/.local/lib/python3.9/site-packages/swiplserver/ **init**.py)

It seams the ` __init__.py` looks like this:

```prolog
from swiplserver.prologmqi import (
    PrologMQI,
    PrologThread,
    PrologError,
    PrologLaunchError,
    PrologQueryTimeoutError,
    PrologQueryCancelledError,
    PrologConnectionFailedError,
    PrologResultNotAvailableError,
    PrologNoQueryError,
    is_prolog_variable,
    is_prolog_list,
    is_prolog_functor,
    is_prolog_atom,
    create_posix_path,
    prolog_name,
    prolog_args,
    quote_prolog_identifier,
    json_to_prolog,
)

# make "from swiplserver import *" work
__all__ = [
    "PrologLaunchError",
    "PrologQueryTimeoutError",
    "PrologQueryCancelledError",
    "PrologConnectionFailedError",
    "PrologResultNotAvailableError",
    "PrologNoQueryError",
    "PrologError",
    "PrologMQI",
    "PrologThread",
    "create_posix_path",
    "is_prolog_functor",
    "is_prolog_list",
    "is_prolog_variable",
    "is_prolog_atom",
    "prolog_name",
    "prolog_args",
    "quote_prolog_identifier",
    "json_to_prolog",
]

```

---

<div class="post-metadata">

**Author:** ![ericzinda](https://avatars.discourse-cdn.com/v4/letter/e/b3f665/32.png) [@ericzinda](https://swi-prolog.discourse.group/u/ericzinda)\
**Post date:** [January 27, 2022, 11:15pm UTC](https://swi-prolog.discourse.group/t/problem-with-unicode-characters-e-g-in-prolog-file-on-swiplserver/4945/5 "2022-01-27T23:15:22Z")

</div>

> [@Losbarthos](#):
>
> I don’t get some error if I put the predicate directly into swipl

I am able to replicate the hang you are hitting on my mac if I do the below. On my mac, at least, I need to set the Prolog encoding to utf8 in order to load the `logic.pl` file properly:

```prolog
with PrologMQI() as mqi:
	with mqi.create_thread() as prolog_thread:
		prolog_thread.query(f"set_prolog_flag(encoding,utf8).")
		prolog_thread.query(f"consult('logic.pl').")
		unicode = "binary_connective(p → q, X, Y)."
		prolog_thread.query(unicode)

```

The problem is a bug in the swiplserver library that sends the wrong count of characters to Prolog. I was able to fix the hang with the below change to the prologmqip.py file that is part of the library. I need to do some more research to make sure this is a good overall fix but I think it should unblock you for the moment. I’ll post when I have the final fix.

```prolog
    def _send(self, value):
        value = value.strip()
        value = value.rstrip("\n.")
        value += ".\n"
        _log.debug("PrologMQI send: %s", value)
        utf8Value = value.encode("utf-8")

        # Comment out this line:
        # msgHeader = f"{str(len(utf8Value))}.\n".encode("utf-8")

        # Replace with this line:
        msgHeader = f"{str(len(value))}.\n".encode("utf-8")

        self._socket.sendall(msgHeader)
        self._socket.sendall(utf8Value)

```

---

<div class="post-metadata">

**Author:** ![Losbarthos](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/losbarthos/32/4037_2.png) [@Losbarthos](https://swi-prolog.discourse.group/u/Losbarthos)\
**Post date:** [January 28, 2022, 11:34am UTC](https://swi-prolog.discourse.group/t/problem-with-unicode-characters-e-g-in-prolog-file-on-swiplserver/4945/6 "2022-01-28T11:34:25Z")

</div>

Thank you, with that replacement it seems to be correct for me too.

---

<div class="post-metadata">

**Author:** ![ericzinda](https://avatars.discourse-cdn.com/v4/letter/e/b3f665/32.png) [@ericzinda](https://swi-prolog.discourse.group/u/ericzinda)\
**Post date:** [January 29, 2022, 12:54am UTC](https://swi-prolog.discourse.group/t/problem-with-unicode-characters-e-g-in-prolog-file-on-swiplserver/4945/7 "2022-01-29T00:54:56Z")

</div>

I have submitted a more comprehensive fix to both the server and the Python client, but the client fix is basically what I posted above, the submitted fix just works against the existing MQI interface and the new fixed one. It also includes a really simple protocol versioning “feature” to more robustly handle versioning issues going forward.

I’ll push the Python library 1.0.2 to [pypi.org](http://pypi.org) early next week.

> <https://github.com/SWI-Prolog/packages-mqi/commit/6a25a6896c2c716c13cf186ec3ee6b16cf18e09a>
>
> Details in: https://github.com/SWI-Prolog/packages-mqi/issues/14
> 
> The MQI implem…entation had a bug where sends to MQI expected the message header to provide a count of Unicode code points instead of what it should have been: UTF8 bytes. This has been corrected but is a breaking change, so:
> 
> ADDED: A simple protocol versioning interface to the MQI. There is now a mqi\_version/2 predicate and the MQI returns Major and Minor version in its response to an initial connect message.
> 
> ADDED: The swiplserver Python library properly detects the original and fixed versions and works against both of them.
> 
> TEST: Added tests for multi-byte UTF8 characters

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [January 29, 2022, 8:26am UTC](https://swi-prolog.discourse.group/t/problem-with-unicode-characters-e-g-in-prolog-file-on-swiplserver/4945/8 "2022-01-29T08:26:35Z")

</div>

Thanks! Updated the git submodule.

---

<div class="post-metadata">

**Author:** ![Losbarthos](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/losbarthos/32/4037_2.png) [@Losbarthos](https://swi-prolog.discourse.group/u/Losbarthos)\
**Post date:** [January 30, 2022, 1:12pm UTC](https://swi-prolog.discourse.group/t/problem-with-unicode-characters-e-g-in-prolog-file-on-swiplserver/4945/9 "2022-01-30T13:12:34Z")

</div>

Thank you I have closed the GitHub issue.

---

<div class="post-metadata">

**Author:** ![ericzinda](https://avatars.discourse-cdn.com/v4/letter/e/b3f665/32.png) [@ericzinda](https://swi-prolog.discourse.group/u/ericzinda)\
**Post date:** [January 31, 2022, 7:38pm UTC](https://swi-prolog.discourse.group/t/problem-with-unicode-characters-e-g-in-prolog-file-on-swiplserver/4945/10 "2022-01-31T19:38:47Z")

</div>

Thanks! You can now `pip install swiplserver` and get the latest version (1.0.2) with the above fix in it.

> **[swiplserver](https://pypi.org/project/swiplserver/1.0.2/)**
>
> swiplserver integrates SWI Prolog with Python.
