10 ms·
Introduction to logic programming with Prolog
- dmitriid 9y ago> Prolog has a straightforward syntax. You will pick up the few rules very quickly. Let us transform the Socrates example from earlier into an actual Prolog program and analyse what is happing inside the compiler. Yeah, and no. This skips over the fact that this will not work in swipl, and you need to write those rules in a separate file, and compile/load them. This is mentioned way after the entire introduction, after the reader has already given up on trying anything with prolog
- jwi 9y agoFYI, there's an updated version of the 99 Prolog problems by its original author here: https://sites.google.com/site/prologsite/prolog-problems https://sites.google.com/site/prologsite/prolog-problems
- matchilling 9y agoThanks for the hint, I've updated the URL to point to the new version.
- legionth 9y agoI hated it in university and hate it until this very moment. I get what this language is doing and why it is great for special usecases. But I can absolutly can not read code written in Prolog. Staring at 3 lines of code and not knowing what the hell it does, gives me nightmares till today. You can write a quicksort in a few lines. You can traverse over a tree very easily. But understanding these few lines takes me more time than writing it in C, Java, PHP etc.. But I guess it is all training...
- simion314 9y agoI agree that is hard to think in prolog, formulating the problem as logic rules is not natural for us after we done imperative programming. I imagine this is true on small problems that are popular in education but in a real world scenario a hard problem(that is suitable for prolog) would be much harder to do it in C or other imperative language, and probably adding new rules would be easier in prolog.
- negativekarma_ 9y agoKIT?
- chatmasta 9y agoYou can learn prolog at more than one university in the world.
- negativekarma_ 9y agoSure, but some courses are taught in more or less enjoyable ways.
- pvinis 9y agoI've had the opposite experience. In university and later, everytime I play with prolog, I love it. Some things make so much more sense when you write them this way, and it's not just the few very specific cases. When I learned about prolog and had the first couple of classes using it, I was delighted! Finally a language that works in a way my mind likes to think in. I am not saying that one way or language is better than the other. But I'm saying that, like you are faster to rewrite in c or java, there are people that can rewrite or understand faster the other way. :) Programming is fun!
- simias 9y agoSomething tells me that it would probably be easier if this was your first programming language. Actually I expect that some would find this declarative style a lot more intuitive and natural. But when you've spent many years writing imperative code it feels so alien. It reminds me of the first time I attempted to write Verilog, it's hard to break the mold and change the way you think about the flow of a program and how you construct algorithms. Eventually it clicked for me and Verilog though, so I expect with enough dedication one could do the same with Prolog.
- pjmlp 9y agoI love Prolog, to the point of having taken part in the Portuguese university national logic championship back in the 90's. On our degree, we had three logic programming classes, starting from logic programming as pure math all the way to deep Prolog exercises.
- flavio81 9y agoI didn't know there were Logic championships using Prolog! P.S: Vocé é Portuguese, lisper e tambén fá de Prolog? You impress me more each day, pjmlp!
- pjmlp 9y agoObrigado, but actually it is due to quality of many of the teachers I had the opportunity to interact with during the 90's version of FCT/UNL. :)
- yummy 9y agoI don't think working with numbers is a strong side of Prolog. Quicksort in Haskell is also 3 lines, but it's totally readable. I can make use of functional programming in my daily life, but logic? Never. But think of the Zebra puzzle. In Prolog the solution is 100% declarative. BTW, I hate Prolog too.
- flavio81 9y agoSome problems are harder to express in Prolog (than in conventional imperative logic), but then there are some problems that are far, far easier to solve in Prolog.
- kbp 9y ago> You can write a quicksort in a few lines. You can traverse over a tree very easily. But understanding these few lines takes me more time than writing it in C, Java, PHP etc I'm still only a beginner at Prolog, but that's what I enjoy about it: it's an entirely different way of tackling problems that really stretches my brain, not just new syntax on the same old paradigm that I already know.
- charlysl 9y agoI found that these kinds of languages make a LOT more sense after learning some logic [1], specially relational logic, to the extent that it's essentially the same thing. [1] http://logic.stanford.edu/ http://logic.stanford.edu/
- virgilp 9y agoI find the section 5.3 from the paper "Out of the tar pit"[1] describes it very well: > It is for this reason that Prolog falls short of the ideals of logic programming. Specifically it is necessary to be concerned with the operational interpretation of the program whilst writing the axioms. I haven't heard of any approach that generalizes the goals of logic programming in a far better way. Is there any? (on specific usecases, having a sort of rule engine for running your "business rules" can indeed be an excellent approach) [1] https://github.com/papers-we-love/papers-we-love/blob/master/design/out-of-the-tar-pit.pdf https://github.com/papers-we-love/papers-we-love/blob/master...
- mathetic 9y agoAh, that's a problem with many facets. It is not that we can't come up with a logic language that is more declarative, it is just that telling the program everything about the universe is so damn dull. For example, ordering of your body predicates will often be based on what you think intuitively their sizes of solutions are going to be e.g. `plus(A,B,C), solution(C,X).` You know that plus is injective, so you put it first but from a logical perspective putting solution predicate first is just as sensible. If the language allowed you to express relative sizes of the predicates, then this could have been done automatically, but it would involve coding the order of predicates somewhere else in the program! You also need to get rid of cuts in Prolog, if you want to be closer to true declarative programming. You might like to then try Datalog (alas, not Turing complete). Another approach is to separate concerns more clearly. The part of Prolog that encodes and solves a logical problem is often different than the one dealing with IO for example. Ideally, you want to be more declarative in the former domain and more imperative in the latter. This can (and probably have been done) with a monadic approach.
- Hendrikto 9y agoI do not get why you mean by plus being injective. `plus(1, 2, C). C = 3.` `plus(0, 3, C). C = 3.` Isn't that a counterexample, showing how it is not injective?
- 9y ago
- vanderZwan 9y agoI've seen a few contalks about logic programming, read a few articles, and aside from the intellectual curiosity in itself I can see that it is very elegant in certain circumstances (although I lack the hands-on experience to feel what those circumstances are). Since doing a whole program in prolog isn't really practical for me, is there any way to use the logic programming paradigm in mainstream programming languages to deal with specific problems that fit it?
- ninjamayo 9y agoNot 100% sure I answer your question but I have used Prolog mixed with Java a while ago and as a high level layer for problem representation. It's not brilliant but at that time it did help me implement quite complex agent behavior within a multi-agent environment.
- markhollis 9y agoI can come up with the following possibilities, although I have no experience with either: - Using OWL in Java (with OWL API). So you use Java in combination with XML - Use a Prolog embedding in Haskell.
- Vosporos 9y agoCheck out https://github.com/rvirding/erlog https://github.com/rvirding/erlog with Erlang/Elixir
- lou1306 9y agoIt seems we have come full circle (the Erlang VM was originally written in Prolog)
- cblop 9y agoMiniKanren is an embeddable language for logic programming, with implementations in a wide variety of languages: http://minikanren.org/ http://minikanren.org/
- erikj 9y agoYou can use embedded Prolog to implement type inference in your compiler. AFAIK that's how it's done in the Rust compiler.
- barry0079 9y agoWhen writing a Prolog program a while ago I stumbled across an old mailing list where someone posted the following joke. Q. "How many Prolog Programmers does it take to change a lightbulb?" A. No.
- ninjamayo 9y agoCan't resist, I will add https://en.wikipedia.org/wiki/Answer_set_programming https://en.wikipedia.org/wiki/Answer_set_programming programming on this. Q. How many Answerset programmers does it take to change a lightbulb? A. {1}, {1,2}, {1,2,3}, {2,1}, {2,1,3}, {2,3,1}
- sp332 9y ago... {3,1}, {1,3}
- tmaly 9y agoLOL thank you
- tmaly 9y agoI guess the down voter has not programmed in Prolog before to get the joke.
- chkte 9y agoI know how to code in Prolog. How can I take profit of it, apart from my academic (undergraduate) interests? Can I build something in particular, can I look for specific jobs?
- gota 9y agoThere are not that many companies using prolog code, but specialized AI companies still do. I've worked as a consultant programmer developing large decision-support systems for engineering projects and the bulk of the domain logic was written in prolog. Most operational stuff was in C++ or Python, still, so even in that project there were as many jobs for Python programmers as there were prolog programmers You don't truly appreciate the declarative paradigm until you are able to describe (and find the best among) all billions of complex possibilities of configurations of nuts and bolts following all these engineering laws, practices and rules of thumb... in a hundred lines or less I love Python and I'll introduce myself as a Python programmer, but part of me will always have a deep respect for prolog and how utterly awesome it is in the right context
- pgtan 9y agoI try to introduce my boy (12) in programming trough Scratch but still no big success. However, we solved recently two math homework exercises with Prolog (logic) and MetaPost (geometry) which caused some effect, so I'm thinking to deepen in that languages or may be switch to Logo as intermediate lisp like solution.
- flavio81 9y agoThat is the best way to introduce computers to a kid: "let it do the hard work for you". Logo used to be very popular for kids here in the '80s. I learnt Logo when i was 7, then i "progressed" over the years to Basic, C, Pascal, C++, Java, C#, Python and now, after about 28 years, Lisp. And now I realize Logo is close to Lisp in many ways and a far more powerful language than some of the ones I "progressed" to.
- pgtan 9y agoYes, my kid was impressed about solving the problem just by describing it.
- flavio81 9y agoExactly in the same way my mind was blown the first time I tried Prolog!
- kbp 9y agoIt's always been interesting to me how Logo, a Lisp dialect, was so successful as a language for teaching children to program, but so many people still go around with the idea that Lisp is really hard to wrap your head around.
- flavio81 9y ago>that Lisp is really hard to wrap your head around. It deserves a blog post, but I'd say Lisp (at least Lisp in its mainstream Common Lisp incarnation) is easy to learn but extremely hard to master, since it allows many possibilities and paradigms, and because of the power of meta programming. So to master it, you would be familiarized with all the paradigms and you would know how to leverage meta programming, etc. Also, since it allows improving performance by going to low-level details (like observing the assembly output of your function), you would better know a bit of assembly/machine language if you want to be able to improve the performance of Lisp to approach (or equal) C code. So previous experience with low-level programming, at least with C, would be a good prerequisite. But yes, using it for simple stuff is easy, and in fact, while I consider Python a great "first language to learn", sometimes I feel that Lisp isn't much difficult than Python, for the same tasks. Python is much easier to master, but then it is much more limited.
- pjmlp 9y agoFor those looking to gentle introduction to logic programming, Tarski's World was a very good book. https://ggweb.gradegrinder.net/tarskisworld https://ggweb.gradegrinder.net/tarskisworld https://web.stanford.edu/group/cslipublications/cslipublications/site/1575864843.shtml https://web.stanford.edu/group/cslipublications/cslipublicat... Might help, instead of jumping straight into Prolog.
- agumonkey 9y agooh you reminded me of a program we used in college named the same. It was about model theory and valid logic clauses (blurry memories)..
- telekid 9y agoYep, showed up in a First Order Logic course for me. IIRC, it was bundled with a book? Edit - this one: https://web.stanford.edu/group/cslipublications/cslipublications/site/1575864843.shtml https://web.stanford.edu/group/cslipublications/cslipublicat...
- ptrkrlsrd 9y agoGreat tutorial! Made me want to go back to Prolog again. A little sidenote for those who run MacOS with brew - you can install Prolog with "brew install swi-prolog" instead of using the binary version.
- sgt101 9y agoIs anyone reading this aware of any work on logic programming in the large as in efforts that are harnessing the fact that answerset programming avoids committed choice and so enables us to write reliable tractable programs, but aims to make that possible in teams or in distributed settings?
- sgt101 9y agoAlso work that tackles industry things like testing your logic programs?
- ninjamayo 9y agoI haven't encountered anything across answer set programming in the large as you put it. It is still pretty academic and mainly used for solving puzzle and optimization problems.
- sgt101 9y agoAnd yet declaring all business logic was once the way that all decisions would be encoded!
- ninjamayo 9y agoLooking at this from a more general perspective of the Symbolic AI approach I would say it had its days. Data driven systems are proving to be a lot more powerful and easy to build. I remember I used to spend ages trying to encode my problems in AnswerSet or AgentSpeak and come up with all the rules. It never worked!
- deleted 9y ago[deleted]
- zmonx 9y agoI agree with what Sylvain Soliman said about this on Reddit: It's a nice tutorial for the 80's parts of the language: https://www.reddit.com/r/programming/comments/7hp2xw/introduction_to_logic_programming_with_prolog/dqspj4p/ https://www.reddit.com/r/programming/comments/7hp2xw/introdu... In modern Prolog systems, you would use more declarative features such as constraints to model combinatorial tasks like map coloring. Make sure to check out modern Prolog features if you are interested in learning the language seriously!
- kccqzy 9y agoAre there any good materials (articles, books) for this kind of modern Prolog? I’m afraid my only experience with Prolog is those 80s features of Prolog and I’m not sure what you mean by constraints.
- zmonx 9y agoPlease see my profile page: It contains links that I find relevant for learning modern Prolog features. Every predicate you impose on the set of solutions can be regarded as a constraint, because it can at most restrict the set of solutions, never increase it. So, in fact, every Prolog goal you invoke is a constraint. When reasoning over Herbrand terms, the only constraints are equality and disequality of terms, which are implemented by the predicates (=)/2 and dif/2, respectively. In addition to these basic constraints over terms, modern Prolog systems also provide constraints over more specialized domains, such as constraints on integers, rational numbers and Boolean values. For combinatorial tasks such as map coloring, constraints over integers are especially useful. For example, here is a constraint on integers, using GNU Prolog: | ?- 3 #= 1+Y. Y = 2 In this case, the system has correctly deduced that Y can only be 2 subject to the constraint that 3 is equal to the result of the arithmetic integer expression 1+Y, where Y is constrained to integral values. Constraints implement relations between their arguments and can be used in all directions. This is the reason why Prolog predicates are typically more general than functions in other languages. However, to truly benefit from this, you must consistently use these comparatively new language features instead of lower-level ones. I say comparatively because these language features have been available for several decades by now in professional Prolog systems such as SICStus.
- 1001101 9y agoAnother excellent resource for learning Prolog is the book "The Art of Prolog." One of the finest language texts you will ever read.
- sizeofchar 9y agoI read it recently and I can't recommend it enough. Even if you aren't really interested in Prolog, it is a must read.
- heneryville 9y agoI don't know if this is just coincidence, but this blog post is fantastically similar to another [1] on Prolog, down the the same quotes, and the same example, though different code. [1] https://bernardopires.com/2013/10/try-logic-programming-a-gentle-introduction-to-prolog/ https://bernardopires.com/2013/10/try-logic-programming-a-ge...
- maxxxxx 9y agoHow does Prolog relate to ML systems? It seems they work in a similar way. Input some learning data and then the system produces output based on these.
- lou1306 9y agoEh, they are quite different under the hood. To put it real simple (and probably quite sloppy): Prolog determines the output by applying logic. ML uses mathematics to analyze the input and "guesses" the output, based on the training set. As a corollary: Prolog will negate anything that can't be proven true from the rules you give it. A ML algorithm will always give you some answer, though the quality depends on the learning data.
- maxxxxx 9y agoCool. Thanks! I am watching these trends from the outside and it's hard to keep up if you don't work directly with the tools.
- gregdeveloper 9y agoI think he meant ML as the family of meta programming languages? He did say systems though so you are probably right
- lou1306 9y agoAlso, the part about "Input some learning data" makes me think to Machine Learning rather than Meta Language. Btw, Prolog and ML might look like distant cousins: e.g. you use recursion and pattern matching in both. But under the hood there are big differences that arise from Prolog's logic programming paradigm: for instance, you can use append(L1, L2, L3) both to concatenate and split a list, depending on the variables you set: append([1,2], [3,4], X) % ==> X=[1,2,3,4] append([1,2], Y, [1,2,3,4]) % ==> Y=[3,4]
- cmpb 9y agoAja Hammerly[0] have a talk[1] at RubyConf this year titled “4 Programming Paradigms in 45 Minutes”, and she used Prolog as her declarative language. She implements a simple cash register in OO (Ruby), Functional (Racket), Declarative (Prolog) and Procedural (Assembly). The Prolog one turned out to be the shortest. It was really interesting! [0] https://github.com/thagomizer https://github.com/thagomizer [1] https://confreaks.tv/videos/rubyconf2017-4-programming-paradigms-in-45-minutes https://confreaks.tv/videos/rubyconf2017-4-programming-parad...
- guruparan18 9y agoBack in 2006, I studied Prolong in my college as a pre-up course for A.I. From my experience then, Prolog was easy for programming small tasks. I ended up building a card game with some intelligence as a final project. Even then, I spent most of the time designing cards to show up in console than time on actual coding for game play/intelligence. It could be fun to retry Prolog now.
- pulsarpietro 9y agoThat Uni course is rubbish
- jldupont 9y agoprolog interpreter in javascript, no server side code: http://prolog.jldupont.com/ http://prolog.jldupont.com/