8 ms·
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?
- Ericson2314 4y agoNot an example of prolog but I wish Ed had time to continue developing https://github.com/ekmett/guanxi https://github.com/ekmett/guanxi.
- jb1991 4y agoLots of neat examples in this book: https://www.amazon.com/Clause-Effect-Programming-Working-Programmer/dp/3540629718 https://www.amazon.com/Clause-Effect-Programming-Working-Pro...
- dmytrish 4y agoNot exactly a big codebase, but it was a revelation for me how natural typecheckers can feel in Prolog: I basically rewrote typing rules with some tweaks: [1] Also, tests were surprisingly enjoyable in Prolog: [2]. [1] https://github.com/EarlGray/language-incubator/blob/29755c32edc631d3a296d06242e0e339b24453db/fp/tapl/fullsimple.pl https://github.com/EarlGray/language-incubator/blob/29755c32... [2] https://github.com/EarlGray/language-incubator/blob/29755c32edc631d3a296d06242e0e339b24453db/fp/tapl/fullsimple.plt https://github.com/EarlGray/language-incubator/blob/29755c32...
- PebblesHD 4y agoNot a criticism of the code here by any stretch, I always find it impressive when people manage to understand languages that are totally foreign to me, that being said however Prolog has always struck me as uniquely unreadable. Even back in university when we looked briefly at different styles of programming, the code always ended up looking something like lines 107-132 in your example, which to someone jumping in is pretty much indecipherable. 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.
- triska 4y agoRegarding the code in question, it is possible to write it a bit more elegantly and more compactly, with a combination of techniques. For example, let's consider this snippet from lines 110 to 119: type(Ctx, case(T0, {z, Tz}, {s(V), Ts}), Ty) :- !, isvar(V), type(Ctx, T0, nat), type(Ctx, Tz, Ty), CtxS = [{V, nat} | Ctx], type(CtxS, Ts, Ty). type(Ctx, case(T0, {inl(Vl), Tl}, {inr(Vr), Tr}), Ty) :- !, isvar(Vl), isvar(Vr), type(Ctx, T0, uni(Ty1, Ty2)), Ctx1 = [{Vl, Ty1} | Ctx], type(Ctx1, Tl, Ty), Ctx2 = [{Vr, Ty2} | Ctx], type(Ctx2, Tr, Ty). It is clear that the two clauses are mutually exclusive if the second argument is known, because they differ in various terms already in the head. For instance, {z, Tz} is clearly different from {inl(Vl), Tl}. A Prolog system with deep indexing, which is also used by Mercury, can detect this, and in such a system, we can therefore remove the two occurrences of !/0 in the code, yielding a more general and shorter version: type(Ctx, case(T0, {z, Tz}, {s(V), Ts}), Ty) :- isvar(V), type(Ctx, T0, nat), type(Ctx, Tz, Ty), CtxS = [{V, nat} | Ctx], type(CtxS, Ts, Ty). type(Ctx, case(T0, {inl(Vl), Tl}, {inr(Vr), Tr}), Ty) :- isvar(Vl), isvar(Vr), type(Ctx, T0, uni(Ty1, Ty2)), Ctx1 = [{Vl, Ty1} | Ctx], type(Ctx1, Tl, Ty), Ctx2 = [{Vr, Ty2} | Ctx], type(Ctx2, Tr, Ty). The isvar/1 tests can be removed if we use a better representation of our data. For instance, if we use the wrapper v/1 to denote variables in the data, we can replace these clauses by: type(Ctx, case(T0, {z, Tz}, {s(v(V)), Ts}), Ty) :- type(Ctx, T0, nat), type(Ctx, Tz, Ty), CtxS = [{V, nat} | Ctx], type(CtxS, Ts, Ty). type(Ctx, case(T0, {inl(v(Vl)), Tl}, {inr(v(Vr)), Tr}), Ty) :- type(Ctx, T0, uni(Ty1, Ty2)), Ctx1 = [{Vl, Ty1} | Ctx], type(Ctx1, Tl, Ty), Ctx2 = [{Vr, Ty2} | Ctx], type(Ctx2, Tr, Ty). Finally, the variables CtxS, Ctx1 and Ctx2 are only used once each, so we can simply write: type(Ctx, case(T0, {z, Tz}, {s(v(V)), Ts}), Ty) :- type(Ctx, T0, nat), type(Ctx, Tz, Ty), type([{V, nat} | Ctx], Ts, Ty). type(Ctx, case(T0, {inl(v(Vl)), Tl}, {inr(v(Vr)), Tr}), Ty) :- type(Ctx, T0, uni(Ty1, Ty2)), type([{Vl, Ty1} | Ctx], Tl, Ty), type([{Vr, Ty2} | Ctx], Tr, Ty). With the necessary provisions in place, it may be possible to shorten it further, by generating such code automatically from more declarative descriptions, using a mechanism called term expansion which is available in most Prolog systems. It is analogous to what other languages such as Rust call macros.
- Spooky23 4y ago
- molenzwiebel 4y agoWorth checking out if you're interested in using constraint/logic programming languages for type checking is the Statix (https://www.spoofax.dev/references/statix/ https://www.spoofax.dev/references/statix/) language, which is used in the Spoofax language workbench. It works similarly to Prolog in solving constraints, but adds support for scope graphs, which are a generic approach to specifying the name binding behavior of programming languages through logic constraints.
- h_sakurai 4y agohttps://github.com/hsk/simpletapl/blob/master/prolog5/fullupdate.pl https://github.com/hsk/simpletapl/blob/master/prolog5/fullup... I have rewritten all TAPL code in Prolog. It was very interesting. https://github.com/mitsuchi/copl-in-prolog/ 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://www.fos.kuis.kyoto-u.ac.jp/~igarashi/CoPL/
- zzbn00 4y agoGerrit's prolog rules may be worth a look: https://gerrit-documentation.storage.googleapis.com/Documentation/3.4.0/prolog-cookbook.html#_prolog_language https://gerrit-documentation.storage.googleapis.com/Document... .
- stunami 4y agoDon't think you'd get the codebase for it, but I know this is built in SWI prolog https://www.securitease.com/ https://www.securitease.com/ . Looks like it was presented a decade ago https://dtai.cs.kuleuven.be/CHR/files/Elston_SecuritEase.pdf https://dtai.cs.kuleuven.be/CHR/files/Elston_SecuritEase.pdf
- triska 4y agoOne of the most interesting applications of Prolog I have seen in the recent past is the formalization of dose-escalation trial designs that occur in clinical oncology. In particular, David C. Norris has implemented a Prolog formulation of the cumulative cohort design (CCD) as part of his precautionary package: https://github.com/dcnorris/precautionary/blob/main/exec/prolog/ccd.pl 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 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 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.pl 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 https://github.com/mthom/scryer-prolog/issues/388
- jart 4y agoWow this is the best thing since the Actually Portable Executable source code. https://justine.lol/thun.png https://justine.lol/thun.png How did the author generate those letters?
- ThemalSpan 4y agoI made this demo project a while back, you might find it useful: https://github.com/SallySoul/regexc https://github.com/SallySoul/regexc
- hyperpallium2 4y agoThe first version of erlang was written in prolog. https://wikipedia.org/wiki/Erlang_(programming_language) https://wikipedia.org/wiki/Erlang_(programming_language)
- fasquoika 4y agohttps://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.34.3972&rep=rep1&type=pdf https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.34...
- astrange 4y agoMercury (https://mercurylang.org https://mercurylang.org) is a language descended from Prolog that has some interesting examples. Personally I think it shows a lot more promise than many other languages people get very excited about.
- User23 4y agoNorvig’s AI book has a lot of prolog in it.
- daniel-cussen 4y agoI want to believe in prolog. I'm having trouble, like pg saying the definition of append is really really good and it's all downhill from there. Also Jet Propulsion Laboratory has a prolog dialect that's non-turing complete for its inspection of its internal systems. prolog looks like a third way, not C and not Lisp.
- ipnon 4y agoThere exists a formulation of the Japanese Constitution in Prolog, which can then be called with a proposition to determine its constitutionality.
- keeler 4y agoLink for those interested: https://github.com/bitlaw-jp/the-constitution-of-japan https://github.com/bitlaw-jp/the-constitution-of-japan
- carapace 4y agoYou could look at "PRESS: PRolog Equation Solving System", "a system for solving symbolic, transcendental, non-differential equations." Journal of Symbolic Computation Volume 7, Issue 1, January 1989, Pages 71-84 "Solving symbolic equations with PRESS" https://www.sciencedirect.com/science/article/pii/S0747717189800070 https://www.sciencedirect.com/science/article/pii/S074771718... Source: https://github.com/maths/PRESS https://github.com/maths/PRESS - - - - BTW, Does anyone know where I can find the source for MIXTUS partial evaluation system?
- monkeybutton 4y agoMake files are prolog programs in drag.
- sgt 4y agoThat's an interesting take. I learned Prolog at university but I never thought of it this way.
- sprior 4y agoNot a lot of code but a somewhat different use of Prolog than you're likely to see elsewhere. I used my fork of a MQTT library for Prolog (https://github.com/sprior/swi-mqtt-pack https://github.com/sprior/swi-mqtt-pack) to implement the central controller for my home automation system. The system responds to MQTT events and then coordinates the appropriate action by sending MQTT messages to other home services. Recent versions of SWI-Prolog also support redis and I've started using that to store device configuration and state between services. The MQTT version is actually a reimplementation of my previous version which used CORBA for inter-service communication. 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.
- sitkack 4y agoThat is cool! How compact is code that sets groups of behavior? Can you paste any examples? This seems like one of the really powerful aspects of Prolog. That we can work on the microscopic level, and the computer can prove correctness or find solutions to competing constraints.
- sprior 4y agoHere's part of a sample sequence: 1) Smarthome gets a notification of a vehicle detected in the driveway 2) sends a request to get an image from the driveway camera 3) gets the image, sends it off for storage and analysis 4) sends it to an image recognition service which returns a description(make/model/color + other info) as JSON 5) receives analysis, matches it to known vehicles and announces the results 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).
- segmondy 4y agohttps://www.metalevel.at/prolog/showcases/ https://www.metalevel.at/prolog/showcases/ https://www.metalevel.at/lisprolog/ https://www.metalevel.at/lisprolog/
- tbensky 4y agoThe Tau Prolog (http://tau-prolog.org http://tau-prolog.org) system (written in JS) is used here https://www.codebymath.com/index.php/welcome/lesson_menu#prolog https://www.codebymath.com/index.php/welcome/lesson_menu#pro... to use Prolog as a way of teaching mathematical concepts.
- andai 4y agoI usually check out the GitHub Trending page for a programming language. Amusingly, the project you linked is the only result for Prolog this month on GitHub. https://github.com/trending/prolog?since=monthly https://github.com/trending/prolog?since=monthly
- seth123456 4y agoNot sure it is recommended reading, but maybe to see what else you could do with Prolog besides interpreters, DSLs etc: As part of a knowledge systems course we built a small game in (mostly) Prolog https://github.com/tobischo/mine-prolog-tba https://github.com/tobischo/mine-prolog-tba
- tmaly 4y agoI used Prolog at my job to fill out complex regulatory forms required in the financial industry. I have a Perl module that queries an Oracle database and constructs the facts database. This is paired with a set of rules to write out the form elements. 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.
- tsujamin 4y agoNot sure how helpful it is for your purposes, but I found the usage of Prolog in Windows NT to dynamically build the network stack super fascinating: https://web.archive.org/web/20040603192757/research.microsoft.com/research/dtg/davidhov/pap.htm https://web.archive.org/web/20040603192757/research.microsof...
- rurban 4y agoI use it for reverse engineering unknown struct layouts for DWG classes. A field packing problem. 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/ACAD_EVALUATION_GRAPH.pi https://github.com/LibreDWG/libredwg/blob/master/examples/AC... optimization problems as in compilers are extremely natural in Prolog.
- Beldin 4y agoI've recently come across "The Eye of Horus", a paper + associated code base [1] analysing attacking transactions on smart contracts using DataLog (a subset of Prolog). Not all of it is written in DataLog, but the vulnerability analysis is - the core component of the framework. [1] https://arxiv.org/abs/2101.06204 https://arxiv.org/abs/2101.06204
- microtonal 4y agoAlpino is a natural language parser and generator for Dutch, written largely in (SICStus) Prolog and some C/C++: https://github.com/rug-compling/Alpino https://github.com/rug-compling/Alpino Even in the age of neural parsers, it's still one of the most competitive parsers for Dutch.
- mtsfz2 4y ago... and Prolog offers traceable brainf*ck: https://github.com/danieldk/brainfuck-pl https://github.com/danieldk/brainfuck-pl
- i_don_t_know 4y agoWhat’s the best way to get started with Prolog on macOS? Implementations, editors, etc?
- Avshalom 4y agoSWI is by far the best documented of the open source versions with Ciao coming second. They both ship with dev environments, the Ciao one is an elaborate Emacs mode and the SWI one is implemented in a custom GUI toolkit.
- Pako 4y agoNot sure if you'd consider this a "complete application" that is bigger than a "Hello-World-esque example" but I've worked on a Discord bot in SWI Prolog[1] for a while with a friend. It was an interesting experience and although a lot of the work (e.g. SWI has a ready-made library for interfacing with WebSockets and sending out HTTPS requests). 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 https://github.com/prolord-pl/prolord
- Vosporos 4y agoAt work we modelled our role-based access control logic in prolog. Once you realise that your authorization is logic programming, you gain access to better tools, more formal ways to think about it, and a treasure trove of literature. :)
- yakshaving_jgt 4y agoI would love to read more about this.
- p_l 4y agoSomewhat more widespread, the Open Policy Agent <https://www.openpolicyagent.org/ https://www.openpolicyagent.org/> uses essentially a subset of prolog written in form more familiar to most programmers - but while the code looks more familiar, the semantics are derived from Prolog.
- mtsfz2 4y agoDFS Tools [0] is a Prolog implementation of the Distributional Formal Semantics framework [1]. [0] https://github.com/hbrouwer/dfs-tools https://github.com/hbrouwer/dfs-tools [1] https://www.sciencedirect.com/science/article/pii/S089054012100078X https://www.sciencedirect.com/science/article/pii/S089054012...
- odiroot 4y agoI vaguely remember Maemo on Nokia N900 used Prolog to manage the sound system (notifications, priorities etc).
- Parker_Powell 4y agoProlog is a programming language that was developed in the 1970s. It is a logic-based programming language, which is pretty different from your typical programming language. Instead of writing functions and then having the computer execute them, Prolog lets you write rules and it will try to deduce the answers to your questions. It's a pretty wild concept, and there are tons of cool examples of what Prolog can do. Some examples are: - A solver for Suduko puzzles: https://rosettacode.org/wiki/Sudoku#Prolog https://rosettacode.org/wiki/Sudoku#Prolog - A simple game where you guess a person's age: https://rosettacode.org/wiki/Guess_the_number#Prolog https://rosettacode.org/wiki/Guess_the_number#Prolog - A family tree program: https://rosettacode.org/wiki/Family_tree#Prolog https://rosettacode.org/wiki/Family_tree#Prolog
- ggleason 4y agoTerminusDB CTO here. Echoing what triska said, CLP(ℤ) and friends are some of the most under-appreciated aspects of prolog implementations. I'm amazed that programmers still don't have access to CLP when trying to do scheduling and planning solutions. As an example in practice, what if you want to know about a transaction in which a number of entities transitively had holdings in one of the beneficiaries of the transaction at that particular time. The date window is not known, and the date windows are important in the ownership chain as well as the transactions that are being undertaken. With CLP(FD) you can ask for a window of time, and the solution will zoom in on an appropriate time window which exists for the entire chain and match the time of the transaction. Now try to do this query in SQL. It's almost impossibly hard. I can't wait until I have the time to implement constraint variables for TerminusDB, but at the minute we are still working on more prosaic features. Aside from that there are very interesting program correctness and optimisation systems which are based on prolog (usually a datalog). For instance Soufflé: https://souffle-lang.github.io https://souffle-lang.github.io
- sm001 4y agoI wrote commercial Prolog applications circa 1990 and it is the nicest programming language that I know (I used over 30 in my 50 year career); I recommend the book The Art of Prolog, by Ehud Shapiro. Also the idea that with Prolog, you can easily write programs that write programs (I wrote a few of those) and that it may be feasible to scale up a program to fill in the code to match input and output descriptions, and build possibly unlimited complex logic on top of simpler components, as I think Shapiro was working on this approach in the 1990's for the Israeli gov., before the project went under the covers, it seems.
- cloogshicer 4y agoHere's an example for a problem that's moderately difficult to do recursively, hard to do iteratively, and trivial in Prolog: Given that you have the following coins, 1¢ (i.e. 1 cent or $0.01), 5¢, 10¢, 25¢, 50¢, how many different ways are there to give change for a dollar? For example, one way would be to give 2x50c, another would be to give 100x1c. How many are there total? In most languages this is not super easy to do unless you're very comfy with recursion. In Prolog the solution is basically (pseudocode): 100 = a*50 + b*25 + c*10 + d*5 + e*1 and Prolog will just figure out all possible combinations for you. The challenge with Prolog as far as I understand is making things fast.
- Avshalom 4y agoWorth noting the entirety of the actual code would be :- use_module(library(clpfd)). main :- 100 #= H*50 + Q*25 + D*10 + N*5 + P*1. That's it.
- bjourne 4y agoTo be fair, that is not that difficult to accomplish in Python either: from ortools.sat.python.cp_model import * m = CpModel() a, b, c, d, e = [m.NewIntVar(0, 100, '') for _ in range(5)] m.Add(a*50 + b*25 + c*10 + d*5 + e*1 == 100) s = CpSolver() s.Solve(m) print([s.Value(x) for x in [a, b, c, d, e]])
- sm001 4y agoA great Prolog project would be the implementation of Godel's logic language.
- h_sakurai 4y agoeval(I,I):-integer(I). eval(E1+E2,I):- eval(E1,I1),eval(E2,I2), I is I1+I2. eval(E1E2,I):- eval(E1,I1),eval(E2,I2), I is I1I2. :- eval(12+34,R),writeln(R),R=14. :- halt. Very simple Operational Semantics. this mean syntax e ::= i | e+e | e * e evaluation rule -------------- (E-Int) i1-->i1 e1-->i1 e2-->i2 i is i1+i2 ------------------- (E-Plus) e1+e2 --> i e1-->i1 e2-->i2 i is i1i2 ------------------- (E-Times) e1*e2 --> i
- h_sakurai 4y agoI write Prolog version bulletML in Prolog. https://github.com/hsk/bulletpl/blob/master/bulletpl/1943_rolling_fire.xml https://github.com/hsk/bulletpl/blob/master/bulletpl/1943_ro... It is a movement of the rolling fire of CAPCOM shooting game 1943. Original Bullet ML DSL is XML base, however this version base Edinburgh Prolog format. http://www.asahi-net.or.jp/~cs8k-cyu/bulletml/index_e.html http://www.asahi-net.or.jp/~cs8k-cyu/bulletml/index_e.html
- hnbeck 4y agoAt the public funded project http://vvm-projekt.de http://vvm-projekt.de we are developing methods for verification and validation of autonomous driving. My team developed an analysis method for information flow in urban intersections. The software is implemented in PROLOG (Logtalk). See paper about here (German, will follow in English soon) https://arxiv.org/abs/2108.00252 https://arxiv.org/abs/2108.00252 and infos about implementation https://www.vvm-projekt.de/fileadmin/user_upload/Mid-Term/VVM_HZE_S1_P16_20220315_PSMFormalization.pdf https://www.vvm-projekt.de/fileadmin/user_upload/Mid-Term/VV... and https://www.vvm-projekt.de/fileadmin/user_upload/Mid-Term/VVM_HZE_S1_P17_20220315_PSMSWArchitectur.pdf https://www.vvm-projekt.de/fileadmin/user_upload/Mid-Term/VV... (English). The project will be finished mid 2023 Cheers Hans