Ask HN: What are some interesting examples of Prolog?
I'm trying to source some programs and libraries written in Prolog to gain a better understanding of how a complete application is put together. Lots of Hello, World-esque examples out there, but looking for things that are a little less trivial.
I did come across terminusdb: https://github.com/terminusdb/terminusdb which looks interesting.
Any other codebases people would recommend that are worth a read?
63 comments
[ 3.9 ms ] story [ 139 ms ] threadAlso, tests were surprisingly enjoyable in Prolog: [2].
[1] https://github.com/EarlGray/language-incubator/blob/29755c32... [2] https://github.com/EarlGray/language-incubator/blob/29755c32...
Is anyone aware of any higher level abstractions for logic based languages that is actually used in industry? From what I’ve seen in the various banks and fintech groups I’ve worked with, prolog itself was very rarely if ever used ‘in anger’, but I have heard of it being used for automation so I’m very curious what that looks like.
I used to be a specialist in a product called Tivoli Enterprise Console. Which was a late 90s/2000s era event correlation system that used an ancient prolog dialect as its rules engine.
I had an awesome boss who let me do whatever, a limited set of tools, and a partner in crime who complemented my style. So we ended up implementing an Oracle database interface in Perl and a client application that allowed others to add facts to the database. Periodically, we’d load the facts into the prolog system and could make queries to tell us what apps were impacted, did we care about them, who was responsible, etc.
The end system was able to identify app owners, system administrators and other information on any of about 20k servers and lots of network gear. We were able to take a flow of about 300k daily events and cook it down to about 40-50 actionable alerts and a few hundred automated actions.
The whole thing took about 3 months to build and another 4-6 to tweak. We got promoted and moved on, but the system hummed along for about 4-5 years until IBM started killing the underlying product. I think the place pivoted that functionality to ServiceNow.
I agree that this piece of code was written in a more academic mindset, with large dose of TAPL-specific jargon and abbreviations which can easily throw anybody off if not known beforehand. I do not have good examples of industrial-grade Prolog code bases, if that's what was asked.
I have rewritten all TAPL code in Prolog. It was very interesting.
https://github.com/mitsuchi/copl-in-prolog/
This is another textbook "Concept of Programming Language" implementations.
https://www.fos.kuis.kyoto-u.ac.jp/~igarashi/CoPL/
https://github.com/dcnorris/precautionary/blob/main/exec/pro...
This Prolog program can be used to exhaustively enumerate all possible arising cases, and also to complete partially given trials. In addition to the specific usage mode of telling the clinician what action to perform next after a sequence of events has occurred, it is also possible to ask interesting questions about the trial design as a whole, such as whether specific cases can arise at all, or whether a specific instance was performed according to the protocol. In this sense, the formulation truly serves as an executable specification of trial designs that are otherwise stated only comparatively informally in the medical literature, and may even be subject to divergent interpretations. The formulation uses Scryer Prolog and the latest Prolog language constructs (such as if_/3 and CLP(ℤ) constraints) to achieve a short and very general description. Such declarative specifications may help considerably to improve safety and efficiency of clinical trials, by making all steps and outcomes amenable to analysis and comparison.
Theorem provers and reasoning engines are also often implemented in Prolog. A recent example is solidarity by Jos De Roo:
https://github.com/josd/solidarity
Prolog is also frequently used for prototyping interpreters. For example, Adrián Arroyo Calle is working on a MIPS simulator written in Prolog:
https://github.com/aarroyoc/mipsie
Simon Forman has implemented a Prolog interpreter of Joy in which, remarkably, the declarative description also serves as a type inferencer and type checker:
https://git.sr.ht/~sforman/Thun/tree/master/item/source/thun...
Porting this code to Scryer Prolog could be an interesting project if it appeals to you. There is already an issue for it and pertaining discussion:
https://github.com/mthom/scryer-prolog/issues/388
toilet is a slightly more modern reimplementation with some added features (like color): http://caca.zoy.org/wiki/toilet
https://github.com/SallySoul/regexc
prolog looks like a third way, not C and not Lisp.
Source: https://github.com/maths/PRESS
- - - -
BTW, Does anyone know where I can find the source for MIXTUS partial evaluation system?
I don't distribute the home automation code however it's pretty specific to my house. The MQTT library provides some building block examples.
You can also see some other comments I've made on HN which describe some other details.
The send_display_vehicle_id(Id) in the end actually does a lot, it queries redis for previously announced display type devices, then looks up the capabilities (bit depth, resolution) of each device and formats accordingly then sends the display request for each. Also send_android_notification sends the request to some Java code that initiates a push message to my phone and watch.
None of this is rocket science in terms of Prolog message(MQTTPathAsList, MessageBody) is my general predicate for handling an incoming MQTT message once I've subscribed to it and it's convenient to deal with a MQTT topic path as a list.
message(['Smarthome','Notify', 'vehicle','motion'],_):- log('brain saw vehicle motion'), get_short_timestamp(S), atomic_list_concat([S, ' Vehicle Motion'], NotificationMessage), send_android_notification('Smarthome', NotificationMessage), request_image('alibi4').
request_image(Camera) :- publish(['Smarthome', 'Video', Camera, 'brain', 'request']).
message(['Smarthome','Video', _ ,'brain','response'],Image):- store_image(Image, 'brain'), analyze_image(Image, 'brain').
analyze_image(Image, Id) :- publish(['Smarthome', 'Sighthound', 'analyze', Id], Image).
message(['Smarthome','Vehicle', 'Sighthound'],VehicleJson):- vehicle_id(VehicleJson, Id), get_short_timestamp(S), atomic_list_concat([S, ' brain saw vehicle: ', Id], NotificationMessage), send_android_notification('vehicle', NotificationMessage), send_display_vehicle_id(Id), atom_concat(Id, VehicleJson, Description), atom_concat('brain saw vehicle: ', Description, Message), log(Message).
https://github.com/trending/prolog?since=monthly
As part of a knowledge systems course we built a small game in (mostly) Prolog
https://github.com/tobischo/mine-prolog-tba
I have one other Prolog project that is part of a tool. It explains the relationship between potentially related people. It is used in financial surveillance.
I'm using picat, a better Prolog dialect, and generate the facts automatically from C to generate the field layouts via picat automatically.
https://github.com/LibreDWG/libredwg/blob/master/examples/AC...
optimization problems as in compilers are extremely natural in Prolog.
[1] https://arxiv.org/abs/2101.06204
https://github.com/rug-compling/Alpino
Even in the age of neural parsers, it's still one of the most competitive parsers for Dutch.
https://github.com/danieldk/brainfuck-pl
Some interesting applications of Prolog specifically include using predicates to filter messages by certain criteria (e.g. if it was sent by the bot's account or not), being able to hot-reload by invoking the make/0 predicate, and homoiconicity to (in theory) easily evaluate random code supplied by a user.
[1]: https://github.com/prolord-pl/prolord