Hacker News new | past | comments | ask | show | jobs | submit login
Prolog.c: a simple Prolog interpreter written in 200 LOC of C++ (2000) (cam.ac.uk)
109 points by adamnemecek on July 30, 2016 | hide | past | favorite | 19 comments



I found Paul Tarau's work the easiest to follow on making a functional Prolog without WAM, for example: https://code.google.com/archive/p/kernel-prolog/

He also has theory papers explaining the architecture. For example: http://logic.cse.unt.edu/tarau/research/NewJinniPapers/fluen...


WAM: "an abstract machine for the execution of Prolog" https://en.wikipedia.org/wiki/Warren_Abstract_Machine

(a couple of others in the Wikipedia disambiguation page are worrisome ...)


Is there a good reason to still be using Prolog? I used Prolog back in the day, but is it still the tool of choice for some projects?


Using DCGs (https://en.wikipedia.org/wiki/Definite_clause_grammar), you can easily create grammars for automated document parsing and generation.

You could even write a compiler (http://comjnl.oxfordjournals.org/content/34/1/64.full.pdf) or disassembler (http://www.dtic.mil/dtic/tr/fulltext/u2/a242987.pdf) in Prolog fairly easy. All thanks to the really smooth support of tree-like structures.

I do wonder why all this seems to be a little forgotten. Granted, the syntax and semantics are a bit unusual, but the expressive power can hardly be matched.


Thanks for these links. Another DCG tutorial http://www.pathwayslms.com/swipltuts/dcg/index.html


Your link lightly addresses one of my questions about using DCGs for parsing. How hard is it to get a sensible error message out of a DCG parser? I've basically written off LALR parsers because getting a reasonable error message out of them is about as much fun as repeatedly poking yourself in the eye with a sharp stick. Good old recursive decent using precedence climbing for expressions seems to be more maintainable in the long run for production parsers.


Prolog is my scripting language of choice for personal projects. Like HN uses a custom Lisp, I prefer doing stuff with my custom Prolog. But then, i'm a curmudgeonly old fart.


Pedantic: C++ code should generally have a .cpp extension.


Author Alan Mycroft is co-founder of Raspberry Pi foundation. I assume, he was aware what he was doing back in the day.


Looks like it was written in the year 2000 so it is more than likely it was written on a UNIX system and used the .C suffix. I can't really tell though.


Agreed, but I would take this further. To call this C++ and be using character arrays for strings rather than std::string and performing manual memory management suggests this is "C with Classes" at best, notwithstanding the fact that it HEAP allocates a bunch of stuff that it never reclaims.

I understand this isn't a code review and I'm not trying to be critical but it's saddens me to see poor C++ code being shown online because that's partly responsible for the bad rap of the language - I know there are other reasons, but this doesn't help.


For the record, .C (uppercase "C" on case sensitive file systems like *NIXen), .cxx, and .cc are also fairly common and perfectly acceptable.


You're right: that is pedantic -- in the worst sense of the word.


Actually, worse: there are actually lots of C++ file extensions and .cpp doesn't even seem to be the most popular. I was mistaken, apologies.


Would love to see it in C++11 or later.


It wouldn't necessary be much different if using C++11. Maybe using unique_ptr instead of new, etc.

It certainly _could_ be a lot different, using a more functional approach, but it likely wouldn't be, and would remain a basic OO program dressed in new syntax.


C++202x would be good too.


It leaks memory all over the place. Is it intended for tiny, short-lived programs that don't generate a lot of garbage?


program termination is a valid reclamation strategy




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

Search: