Small Prolog interpreter in Elixir

I am creating a Prolog interpreter to learn Elixir. This is not practical. It is a toy program.

https://github.com/sasagawa888/Prolog

Example: (from PAIP, Peter Norvig)
?- assert(likes(kim,robin)).
true
?- assert(likes(sandy,lee)).
true
?- assert(likes(sandy,kim)).
true
?- assert(likes(robin,cats)).
true
?- assert((likes(sandy,X) :- likes(X,cats))).
true
?- assert((likes(kim,X) :- likes(X,lee),likes(X,kim))).
true
?- assert(likes(X,X)).
true
?- listing.
likes(kim,robin)
likes(sandy,lee)
likes(sandy,kim)
likes(robin,cats)
likes(sandy,X) :- likes(X,cats)
likes(kim,X) :- likes(X,lee)likes(X,kim)
likes(X,X)
true
?- likes(sandy,Who).
Who = lee;
Who = kim;
Who = robin;
Who = sandy;
Who = cats;
Who = sandy;
false
?-halt.
goodbye

12 Likes

This super exciting (for me). I’ve been re-reading Prolog with the idea of either implementing a Prolog-like DSL, or maybe an interpreter. I’ve only got as far as starting to research how to write a Horne clause theorem solver (really, just started), So this is going to be a great project for me to follow.

3 Likes

Thank you for your reply.
I’m glad to be interested.
Thank you very much.

2 Likes

Of tangential relevance because it’s not Elixir, but Erlog might be worth looking at. While it seems not to have been worked on for a few years, it has good pedigree (@rvirding). I also recall that @zkessin had some interest in Prolog on the Beam.

Beyond exploration, I can see a possible use case (actually considered doing it some time ago): As a triage frontend for a Prolog database running something like SWI Prolog.

2 Likes

Erlog is pretty cool, but could use some love. If I evere were to be in a position to sponsor someone to work on open source it would be on my list of things to work on (Not happening anytime soon) but I would love to chat about it

3 Likes

Likewise. Maybe sometime in the future I will have time and resource to revisit it. The application I was looking at had no business model and would probably have required public funding - unlikely as things stand right now.

2 Likes

Thank you for the information of Erlog.

2 Likes

Yes, I haven’t worked on Erlog for a while but it is has not died. It has reached a certain level and I have left it there. It does implement a proper subset of standard prolog but of course it could be extended. The next step could be a rework of the interpreter maybe compiling the code a bit to improve speed.

Doing an elixir front-end to it would be relatively simple as the interface and internal data structures are quite straight forward. It is just that no one has asked for one yet. :smile:

There was a fork where someone else reworked the database to handle large tables but it used ETS and it wasn’t possible to make the database behave exactly as the prolog standard. Which was one of my requirements. There is a simple ETS interface which could be extended with more features.

4 Likes

LOL, we’ve come in full circle.

I’ve left Erlang because I was meh with the Prolog’s syntax for Elixir.

And now we have a project for prolog written in Elixir.

1 Like

I appreciate the talent of Jose.
Elixir has given modern syntax to the capabilities of Erlang.
If great talent gives Prolog modern syntax, Prolog may come back.

3 Likes

Nice! I love Prolog and considered the same idea a few years ago. Recent months have seen me learning Mercury too, I have just about finished wrapping the SDL2 media library with it so I can write games in Mercury.

Creating a clean Prolog in Elixir is my kind of mad idea. I’ll check out the git pages and have a look!

Sean.

1 Like

Thank you very much.
My Prolog implementation is still buggy. I am rewriting carefully by test code.

Bidirectional calculation is now possible.
It is a small interpreter of about 1000 lines.
Everyone, please enjoy remodeling as you like.
It is BSD license.

Example:
mix prolog
Compiling 1 file (.ex)
Prolog in Elixir
?- assert(append([], Xs, Xs)).
true
?- assert((append([X | Ls], Ys, [X | Zs]) :- append(Ls, Ys, Zs))).
true
?- append(X,Y,[1,2,3]).
Y = [1,2,3]
X = [];
Y = [2,3]
X = [1];
Y = [3]
X = [1,2];
Y = []
X = [1,2,3];
false

?-halt.
goodbye

1 Like

I wrote a brief introduction.

5 Likes

I started the next related project.

2 Likes

@sym_num, this is great - please keep going!

2 Likes

Thank you very much.

I am also trying to implement the parallel logic language GHC (guarded Horn Clause) by Elixir.

1 Like

Try looking at FCP (Flat Concurrent Prolog) which is really interesting. Or Parlog or Strand for that matter. We once did an implementation of Erlang in Strand but found that were something that wouldn’t work properly.

3 Likes

Thank you for the information.

2 Likes