I did a thing: minimal 'Prolog' interpreter and parser in Python

I wished to understand better how unification and backtracking work, and managed to implement a simple and inefficient solver in Python:

I got to admit that I’m now even more impressed with logic programming, as it was surprising to me how much can be done with this little capability. I implemented a limited Prolog parser in the same Gist, and plan to follow through until I get a REPL.

I hope this can be useful to others learning Prolog, because at least I am not content until I grasp the innards of a system. If there’s any easy way to also implement choicepoints and cuts, I’d be glad to learn.

Nice work! Next step in your understanding is probably to read about the Warren Abstract Machine, a technology that is (was?) underlying efficient implementations of Prolog.

1 Like

Here I am, a pandemic later, having written a WAM interpreter in Python!

As I’m self-taught I’ve probably messed up somewhere, but I hope it’s useful to other people learning about compilers. I’d love some book/papers/online courses recommendations, that can give me both a solid ground, and also is up-to-date into logic programming developments from the last 30 years. I feel I’m missing on so much!

Special thanks to Markus Triska for his personal page and to everyone that colaborated to the SWI-Prolog glossary, they have been my companions for a while!

4 Likes