1

I'm trying to write a prolog server to be able query a knowledge base written in prolog from a client written in another programing language (Python, JavaScript, whatever...) much as one can an SQL database.

I would have thought this was quite a common thing to do, but I can't find any examples on the web.

All the required socket functions seem to be listed at https://www.dcc.fc.up.pt/~vsc/Yap/documentation.html#Sockets and the reason I'm using yap rather than swi-prolog is I want to use a Unix socket, not a TCP/IP socket. But I'll use AF_INET here since AF_UNIX isn't supported by swi-prolog.

Writing a server that writes a string from the client is easy:

#!/usr/bin/yap -L --

:- initialization(main).

main :-
    socket('AF_INET', Socket),
    socket_bind(Socket, 'AF_INET'('localhost', 1234)),
    socket_listen(Socket, 2),
    socket_accept(Socket, _Client, Stream),
    read(Stream, Term),
    write(Term),
    socket_close(Socket).

And the simple Python 3 client I'm using looks like this:

import socket

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('localhost', 1234))
sock.send(b"'Hello World\n'")
sock.close()

Where it gets tricky is getting the server to echo the string back to the client, which requires socket_select/5 as far as I understand. But I've no idea from the available documentation how to use it.

joeblog
  • 1,101
  • 11
  • 17
  • why do you using sockets? It is easy to run web server locally and communicate over HTTP –  May 03 '17 at 09:27

0 Answers0