Hacker News new | past | comments | ask | show | jobs | submit login
Show HN: A simple Prolog Interpreter written in a few lines of Python 3 (github.com)
148 points by photon_lines 6 days ago | hide | past | web | favorite | 20 comments





Cool tests! PyDatalog does Datalog (which is ~Prolog, but similar and very capable) logic programming with SQLAlchemy (and database indexes) and apparently NoSQL support. https://sites.google.com/site/pydatalog/

Datalog: https://en.wikipedia.org/wiki/Datalog

... TBH, IDK about logic programming and bad facts. Resilience to incorrect and incredible information is - I suppose - a desirable feature of any learning system that reevaluates its learnings as additional and contradictory information makes its way into the datastores.


Thanks for the feedback :) I'll definitely check out Datalog - I didn't realize they had logic programming integrated with SQLAlchemy, so it definitely sounds interesting!

Nicely done! I have been trying to implement actual usable (as in, they can be used as a scripting language for instance to build anything) languages in as few lines as I can (without making them unreadable). Ofcourse that's more as a challenge than anything serious, but it is fun. A minimal implementation I define as being something that can parse the language, interpret it and has FFI to add whatever else you may need. So far I got down to around 50 lines in C# [0].

0. https://gist.github.com/tluyben/16ee2645c4c8aed813005d51488d...


Thank you :)

If you want real power for implementing things in as few lines as possible, F# is definitely worth taking a look at as well!


That's so cool! Here is my try in 120 lines of PHP: https://gist.github.com/sanxiyn/3159161

talking about forth, jvtoups elisp forth is a nice one

https://github.com/VincentToups/emacs-utils/blob/master/with...

another blog entry discusses it (scroll down): https://github.com/VincentToups/emacs-utils/blob/master/mona...


This is about 10x larger than i would call a few lines :)

still way less lines then I would have thought, especially because it has no dependencies.

But the title does not mention that. It says few lines. Maybe 100, 200, 300 lines are few. Its misleading and otherwise discredits a good project.

This project even has a GUI/IDE. I believe that is a stronger selling point rather than "few" lines of code or "little work", "easy way" or any of these other colloquial 'flexing' terms.


It's worse than that - the title says "a few", which is basically always less than ten. If it said "quite a few", it would certainly be accurate. If it actually said "few", it wouldn't be as bad, though still misleading.

FYI, there is a mature implementation of this idea called YieldProlog.

Thanks :) Yup, there are also constraint programming libraries in Python which can be used for providing similar functionality, and they're definitely worth checking out!

Very impressive.

Does anybody use Prolog scripting engine as part of their applications? For example, to write a solution lookup function for some specific problem that would be a nightmare to solve otherwise.

As a side note, I found that Lisp (the Scheme flavor) is essential in many applications. First of all, as a small and capable templating engine. Secondly, as a sophisticated NLP (natural language processing) engine.


I've used ad-hoc test case generation using Prolog (MC/DC, all-permutations and other combinatorials) which is a quite natural application for Prolog. Around 2005 I've also used Prolog to generate Java code for analysing/monitoring business processes based on formal business process descriptions on a research project for a Telco who went all-in on SOA. As part of that, I also started to develop decision procedures for static type checking of XML-manipulating programs, along with validators for common markup meta-languages (XSD and subsets). Many years ago I've also developed custom file parsing/business rules checking using Prolog. Plus, a couple ad-hoc parsers and DSLs. And, having developed 2 1/2 prolog engines so far, I'm planning to include ISO Prolog in my upcoming document storage/search system based on SGML, where Prolog is an excellent match since it's both an ISO standard (like SGML), and even has a binding to a document query language based on ISO/IEC 13250 (eg. the proposed "tolog" language, though alternatives were proposed and an ISO/IEC 13250 query language was never completed as a standard).

I wrote a simple date extractor (parsing stuff like "two days ago" from strings) for my hobby website project in prolog [0]. It was great fun to write, and prolog really shines in such applications. And running a prolog microservice in docker feels just a little perverse in a good way. :)

[0] https://github.com/jvnn/date-extractor


How long did it take you to grok unification? It has always seemed quite mindbending to me and I don't think that I have ever properly understood what's happening

I didn't grok anything at first actually. I had familiarity with Prolog and wanted a refresher, so I decided to take a look at implementation attempts done in other languages. I found one which did it in a few lines of Javascript, and you can find the full read and implementation here: https://curiosity-driven.org/prolog-interpreter . My apologies if I didn't make it clear that this is a port of the original write up - I did include this in the first paragraph of the README though, so maybe I should have been clearer?

Originally, I was trying to learn Kotlin along with wanting a refresher in Prolog, so I attempted to do the port using Kotlin and gave up half way though. I didn't have the patience to try to grok too many things at once, so I decided to go with Python due to it's simplicity, popularity, as well as the fact that I'm a lot more fluent in it than I am in Kotlin.

Anyways - sorry if I'm going off track. To answer your question, I grokked the implementation details through porting / refactoring the original code. The concepts / unification I was already familiar with from taking a university course which involved Prolog and from using it in a large AI project. From what I remember, it took me quite a long time to grasp the language and its power!!


Nice. For complex operations with reasonably sized data pypy would be a good choice.

Racket also has a Prolog-Style Logic Programming DSL named Racklog.

Absolutely Brilliant!



Applications are open for YC Summer 2019

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: