10 ms·
The Simplicity of Prolog
- andsoitis 2y ago> While SQL isn't normally used as a general purpose programming language I think it's quite illustrative for those otherwise unfamiliar with declarative languages (though apparently it is Turing complete) SQL itself is not Turing complete in its standard form because it lacks some essential features of a Turing machine, such as the ability to simulate unbounded loops or recursion within a single query. SQL with procedural extensions, such as PL/SQL (Oracle), T-SQL (Microsoft), and PL/pgSQL (PostgreSQL), are Turing complete because they allow constructs such as loops, conditionals, and recursive functions.
- GregDavidson 2y agoSQL with CTEs is also Turing complete, which is not necessarily ideal. The Rule of Least Power https://en.m.wikipedia.org/wiki/Rule_of_least_power https://en.m.wikipedia.org/wiki/Rule_of_least_power suggests avoiding OP languages. Less powerful languages can be more transparent (great for expressing contracts, e.g. specifications), tractability (proofs, optimization, termination, etc.) and so on. Many systems have been ruined by making them Turing complete.
- qu1j0t3 2y agoI'd upvote this twice if I could. It is a great strength of the popular subset of SQL that it is _not_ Turing complete.
- 29athrowaway 2y agoUsing SQL is perfectly fine. Datalog on the other hand is absurdly hard to work with.
- guskel 2y agoTo each their own. Others would say datalog elegant and makes it easy to compose statements whereas SQL has an ugly syntax. I mean, ORMs were invented to try to avoid writing SQL but they too have their own problems.
- sgarland 2y ago> SQL has an ugly syntax Speak for yourself. I’ve always found it to be very clear and concise. SELECT <tuples> FROM <table> [[type of] JOIN other_table ON ?, …] [WHERE ? [boolean operator], …] [ORDER BY ? [DESC]] [LIMIT ?]
- baq 2y agoAnsi SQL 99 defines recursive ctes. https://en.m.wikipedia.org/wiki/SQL:1999 https://en.m.wikipedia.org/wiki/SQL:1999
- anonzzzies 2y agoIn a long forgotten past a large part of my graduation was Prolog (I was as far ahead then as i'm behind now with the ol' AI thing; I worked on mixing neural nets, reasoning with uncertainty with Prolog at the time) and, after hallucinatory episodes living, sleeping, (day)dreaming in Prolog for months on end, when something was finished I was always so surprised how clean, readable and 'too little' the code looked for what it did. Once you really grok it, it's a magic tool. I replaced it with Common Lisp after (in my uni, there was no mention of Lisp; we didn't even know it existed basically besides some weird scribbles in papers which were easy enough to study so we didn't give it more thought) and have the same feelings/experiences there but CL is usable for basically anything, including logic programming.
- ramses0 2y agoI really wish there was "regex for prolog", ie: when pounding away in JavaScript be able to do: let options = { foo: 1, bar: 2, ... } ```prolog $X :- fromJson( options ) Solution = Optimize( $X ) ... ``` ...like writing a whole web app in prolog sounds terrifying (same as writing a whole web app in regex), but recognizing and having some excellent interop between "modes" is obviously useful for regex, sometimes sql is supported in other languages, but even with the usefulness of prolog outcomes, there's almost never been that convenient "this section is logic" in the same way that we've universally adopted "regex" for string matching.
- Avshalom 2y agoYou might want http://tau-prolog.org/documentation#js http://tau-prolog.org/documentation#js
- andrewflnr 2y agoIt's been a while since I used it, but look at the miniKanren implementations for your favorite programming language (js in your case, but there are a lot). It's basically a stripped-down version of logic programming.
- qu1j0t3 2y agoIt may "sound terrifying" but it's likely to be much more pleasant than you think: https://github.com/Anniepoo/swiplwebtut/blob/master/web.adoc https://github.com/Anniepoo/swiplwebtut/blob/master/web.adoc
- llsf 2y agoI think Lambda Prolog, is the only language that physically hurt my brain... in a good way. But damn, it felt like an extreme muscle stretching exercise. Painful when doing it, but you feel SO good after :)
- 3willows 2y agoLove the links to SWISH playground: really got me to want to try it out!
- jfmc 2y agoOther playground (wasm based): https://ciao-lang.org/playground https://ciao-lang.org/playground
- GregDavidson 2y agoI have a long standing love affair with Prolog and frustration for its failure to grow. Prolog has poor features for abstraction and is actually not declarative enough! Prolog's limitations can be great for starting to get the paradigm but then you hit a wall. (Kind of like standard Pascal!) Mercury and Curry fix some of these limitations as does miniKanren. Integrating CLP is important yet so far always clumsy as standard Prolog provides no way to reify the environment. Some LP resources: https://minikanren.org/ https://minikanren.org/ https://mercurylang.org/ https://mercurylang.org/ https://www.curry-lang.org/ https://www.curry-lang.org/ https://en.m.wikipedia.org/wiki/Constraint_logic_programming https://en.m.wikipedia.org/wiki/Constraint_logic_programming https://logtalk.org/ https://logtalk.org/
- cess11 2y agoI take it you have some gripe with https://www.scryer.pl/clpz https://www.scryer.pl/clpz? Could you explain in more detail?
- derdi 2y ago> Prolog has poor features for abstraction That's actually what this article presents as its strength and simplicity. The straightforward choice in Prolog is to use global "who may do what" tables. In contrast, the author overengineers a Kotlin solution and then says "look, this is overengineered". I think global tables would make the Kotlin code half as long and much simpler too. > Mercury and Curry fix some of these limitations At the cost of introducing new ones. Mercury makes it effectively impossible to pass around partially instantiated structures.
- Pet_Ant 2y agoDoesn’t Mercury declare each form of a rule independently?
- upghost 2y agoRespectfully have to disagree. Prolog has the best features for abstraction of any language I've used, I suspect it may be near an optimal fixed point for metaprogramming expression[3]. I will say the other side of that wall is breathtaking. Also not sure what you mean, CLP is a first class consideration in many Prologs, esp Scryer Prolog. Check these crazy demos out: [1] https://youtu.be/h5Xy4YjCZxM https://youtu.be/h5Xy4YjCZxM [2] https://youtu.be/5KUdEZTu06o https://youtu.be/5KUdEZTu06o Just look at this Sudoku solver code (see [2] for explanation): sudoku(Rows) :- length(Rows, 9), maplist(same_length(Rows), Rows), append(Rows, Vs), Vs ins 1..9, maplist(all_distinct, Rows), transpose(Rows, Columns), maplist(all_distinct, Columns), Rows = [As,Bs,Cs,Ds,Es,Fs,Gs,Hs,Is], blocks(As, Bs, Cs), blocks(Ds, Es, Fs), blocks(Gs, Hs, Is). blocks([], [], []). blocks([N1,N2,N3|Ns1], [N4,N5,N6|Ns2], [N7,N8,N9|Ns3]) :- all_distinct([N1,N2,N3,N4,N5,N6,N7,N8,N9]), blocks(Ns1, Ns2, Ns3). I have yet to see more elegant code in a general purpose language. [3]: https://github.com/mthom/scryer-prolog/discussions/2347#discussioncomment-11691870 https://github.com/mthom/scryer-prolog/discussions/2347#disc...
- lincpa 2y ago[dead]
- lionkor 2y agoHow can I use Prolog to actually get something done, other than academic tasks? Say I want to use it as a database query language, presumably that's not going to happen, right?
- vixen99 2y agoDitto!
- aktuel 2y agoThere you go: https://github.com/aarroyoc/postgresql-prolog https://github.com/aarroyoc/postgresql-prolog
- Twey 2y agoIn a sense, Prolog _is_ a database query language. The base facts in Prolog are equivalent to the rows of a table. You can also ‘table’ predicates with variables (over a finite subset of values) by memoizing the results.
- tgamblin 2y ago> Datalog is a declarative logic programming language. While it is syntactically a subset of Prolog, Datalog generally uses a bottom-up rather than top-down evaluation model. This difference yields significantly different behavior and properties from Prolog. It is often used as a query language for deductive databases. https://en.m.wikipedia.org/wiki/Datalog https://en.m.wikipedia.org/wiki/Datalog
- 2-3-7-43-1807 2y agodo you know what you are writing about? I mean have you actually done something with datalog? and then _which_ datalog? if yes, then you are probably someone working with it academically or the answer is no. because try to even set a toy project up with it (for the purpose of learning how to use it) and you'll quickly run into unmaintained interpreters, discussions of what datalog is and what not and you can choose between difficult to understand academic papers or simplistic introductions that lead you no where.
- brabel 2y ago> The reverse predicate from this section should not be used in practice - most Prolog implementations actually provide a built-in version which is a lot more performant than our implementation. That feels like cheating. If you shouldn't use the implementation shown, then why do you even show it? Let us see the performant version! That's like Haskell's quickSort implementation in a couple of lines... beautiful but horrendously non-performant as it doesn't actually implement the algorithm, it just implements the "idea" (while losing all the performance of the actual algorithm, which requires in-place mutation).
- derdi 2y agoNo built-in is needed for an efficient reverse. It's just one or two lines longer because you need to write a tail-recursive helper with an accumulator, same as in functional languages: https://stackoverflow.com/a/74777180 https://stackoverflow.com/a/74777180
- aktuel 2y agoI started learning prolog just a few months ago, when I stumbled upon https://linusakesson.net/dialog/ https://linusakesson.net/dialog/ which is a spin on prolog optimized for writing interactive fiction. As a sweet and short tutorial I can recommend these slides: https://www.cs.toronto.edu/~hojjat/384w10/ https://www.cs.toronto.edu/~hojjat/384w10/ If you want to dive into how Prolog works under the hood I can recommend https://github.com/a-yiorgos/wambook https://github.com/a-yiorgos/wambook I terms of Prolog implementations I played a bit with https://www.scryer.pl https://www.scryer.pl but it still feels rough around the edges. SWI-Prolog is the most popular and most batteries included Prolog: https://www.swi-prolog.org https://www.swi-prolog.org With its libraries and documentation it is a very practical language. What surprised me is, that you can easily produce amazingly small stand-alone binaries.
- 0x3444ac53 2y agoI read about dialog in another HN thread a few weeks ago. Between then and now I've had the itch to write some interactive fiction, but I could not for the life of me remember the name of that project. Thanks!
- qu1j0t3 2y agoI assume you're already au fait with Inform* family of languages...
- upghost 2y agoI always enjoy reading positive pieces about Prolog but without touching on the implications of the principles on display, this article may do more harm than good. You would ask reasonable questions like, "for God's sake, why?". It feels like you wouldn't use Prolog for anything besides an intellectual game. Regarding the why: Some of those reasons include Definite Clause Grammars, Meta-Interpreters, 1st class constraint logic programming, reified conditionals, and term/goal expansion. There are a lot of exciting modern advances with Prolog, especially Scryer Prolog. Check out Power of Prolog and get your mind blown: https://youtube.com/@thepowerofprolog https://youtube.com/@thepowerofprolog
- agumonkey 2y agoone example that changed my view of prolog was http://faculty.cooper.edu/smyth/cs225/ch7/prolog.htm http://faculty.cooper.edu/smyth/cs225/ch7/prolog.htm (a toy compiler in prolog)
- coliveira 2y agoAnd this is nothing new. People already knew how to write compilers in Prolog (easily) in the 90s. The problem with Prolog is that it requires a change in the way you think about problems, to a more declarative way. Programmers are in general not willing to do this since the result will be not as performant as what they can do with C. There must be a revolution in programming education and tools before people fully understand how Prolog works.
- agumonkey 2y agoyeah yeah i got that too after a while, which is also why i like non mainstream programming languages, after a while you're tired writing more versions of the same routines/procedures/methods that won't help you find better solutions
- simplify 2y agoSomething doesn't have to be new to be valuable (XKCD "Ten Thousand" et al). A revolution starts with inspiration.
- ParticlesRWaves 2y agoAm I the only who finds this article to be inaccessible due to color choices and implementation? I'm really interested in Prolog, but my eyes feel the strain after a few sentences. The reason is that the contrast ratio of some parts is very low, of others excessively high. The constant alternations between these extremes intensifies the effect. The page looks shredded in Safaris reader mode, so that's no help either.
- qu1j0t3 2y agoIt's not just you: Its typography and design is quite unsuitable to reading. The lines are too long, there is not enough leading, the typeface is badly chosen, and the black blocks everywhere are unpleasant and off putting.
- Knaapje 2y agoHey, I'm the author of the blog. I agree with your criticisms - I originally designed the website for dark mode users and hadn't really revisited the styling afterwards. I'll be doing some tweaking to increase readability.
- Knaapje 2y agoHey, I'm the author of the blog. I agree with your criticisms - I originally designed the website for dark mode users and hadn't really revisited the styling afterwards. I'll be doing some tweaking to increase readability.
- fifilura 2y agoHaving used Trino/Athena and BigQuery extensively, I am curious about the state of parallelism in the available prolog implementations. Being able to push the calculations to CUDA or the google/AWS "cluster" feels like it could be the game changer for a system like this. The declarative nature of the language should leave any imperative implementation far behind when it comes to complex calculations.
- tannhaeuser 2y agoIndeed. Not using CUDA but Quantum Prolog has parallel maplist and co [1], and the article also contains a short overview and brief comparison with other approaches, including a short discussion of imperative implementations like SWI's (historic?) threading package. Discussion is necessarily limited to that particular workload since parallel Prolog has a long history dating back to the 1990s (the book "Past, Present, Parallel" was an early survey at the time and already contains tens of approaches/academic implementations). [1]: https://quantumprolog.sgml.net/bioinformatics-demo/part2.html https://quantumprolog.sgml.net/bioinformatics-demo/part2.htm...
- titanomachy 2y agoI liked the authorization example. I've encountered Prolog articles before, but showing the code alongside an OOP implementation was a nice demonstration of its expressive power. As a follow-up, I'd love to learn how this auth system could be put into production. In this example, authorization rules are provided in the code (`user_role(mike, supervisor)`) and queried similarly. What if I wanted this system to expose authorization as an HTTP endpoint with REST semantics, and store authorization rules on disk? Would this be straightforward, involved, or impossible? Would I use another language for HTTP, and query a prolog "server" running on the same machine?
- rscho 2y agohttps://github.com/Anniepoo/swiplwebtut/blob/master/web.adoc https://github.com/Anniepoo/swiplwebtut/blob/master/web.adoc
- derdi 2y ago> What if I wanted this system to expose authorization as an HTTP endpoint with REST semantics, and store authorization rules on disk? Would this be straightforward, involved, or impossible? Straightforward. Prolog supports self-modifying code. You can mark the user_role predicate as "dynamic", i.e., modifiable. At runtime you can then read rules from disk or receive them via HTTP or construct them based on some other form of input, and add them to the code, or remove them as needed. The HTTP part is not standardized; you would need to use libraries specific to some concrete implementation. But the libraries exist.
- axefrog 2y agoFun fact: A large portion of TerminusDB's codebase is written in Prolog. https://github.com/terminusdb https://github.com/terminusdb
- Guthur 2y agoIt's actually quite pleasing to see much health debate in these comments. Shows life in this amazingly powerful language.
- capitanazo77 2y agoFYI, composer 2 (the official PHP package manager) uses prolog to figure out if a package can be installed or not. When it’s not possible, you get a detailed explanation why it’s not possible
- qu1j0t3 2y agoIt uses a SAT solver, not Prolog. https://news.ycombinator.com/item?id=17686801 https://news.ycombinator.com/item?id=17686801
- VPNBlade 2y ago[dead]
- ralphc 2y agoAre there any implementations, of either Prolog or other declarative languages, that try to parallelize parts of their search, then have multiple cores or even a GPU take the problem to speed up solutions?
- ceving 2y agoI am wondering what this statement actually means: > The reverse predicate from this section should not be used in practice - most Prolog implementations actually provide a built-in version which is a lot more performant than our implementation. I think it can be read as: If you care about speed do not use Prolog, instead write a "built-in" solution.