5 ms·
This has singlehandedly made me interested in learning more about prolog. I mean, if you can do things like that that simply... Holy crap. No wonder so many lis
by qwertyuiop924 10y ago
This has singlehandedly made me interested in learning more about prolog. I mean, if you can do things like that that simply... Holy crap. No wonder so many lisps incude a prolog implementation of some sort.
- coliveira 10y agoProlog was designed to do this kind of things easily. You can parse languages with easy, and perform all kinds of programming transformations that would take thousands of lines in other languages in just a few rules. The problem with prolog is when you try to do the run-of-the-mill stuff. Not because it is impossible or even hard, but because you need to change the way you think about programming. This is very difficult for programmers that are already used to imperative languages.
- chubot 10y agoReally, how does it handle parsing? What kind of languages can you parse? I recall this paper, by Joe Armstrong of Erlang... I'll have to go read it again. "Use of Prolog for developing a new programming language" https://scholar.google.com/scholar?cluster=2346610420909745067&hl=en&as_sdt=0,5&sciodt=0,5 https://scholar.google.com/scholar?cluster=23466104209097450... Hm after scanning it, I think the reason I never really got it the first time around is that I don't know Prolog :) Might be time to fix that. What turned me off to Prolog is that there's no way to reason about the computational complexity of the algorithm. Declarative languages often have this problem. That's why you have SQL queries that literally take thousands of times longer than a Python script in production (hours vs seconds). Programming models also need performance models.
- abecedarius 10y agohttps://en.wikipedia.org/wiki/Definite_clause_grammar https://en.wikipedia.org/wiki/Definite_clause_grammar -- parsing in Prolog. As usual with Wikipedia on technical topics it looks like a poor introduction, sorry. Reasoning about performance is quite possible; there's good advice in Richard O'Keefe's The Craft of Prolog. [Edit: fixed the book title, thanks to a friendly correction. The Practice of Prolog is a different book, also good, but less relevant.]
- qohen 10y agoAs usual with Wikipedia on technical topics it looks like a poor introduction, sorry. There are real tutorials on DCGs available, e.g. http://www.pathwayslms.com/swipltuts/dcg/ http://www.pathwayslms.com/swipltuts/dcg/ (BTW, SWI-Prolog, which the tutorial uses, is freely available here: http://www.swi-prolog.org/ http://www.swi-prolog.org/ )
- qohen 10y agoAlso, if you want to dive deeper, the book, "Prolog and Natural Language Analysis" by Fernando C. N. Pereira and Stuart M. Shieber, is available as a free PDF from the publisher: http://www.mtome.com/Publications/PNLA/prolog-digital.pdf http://www.mtome.com/Publications/PNLA/prolog-digital.pdf And there's a set of course materials in HTML for "Natural Language Processing in Prolog", a course at Union College in Schenectady, NY by Patrick Blackburn and Kristina Striegnitz that seems worthwhile: http://cs.union.edu/~striegnk/courses/nlp-with-prolog/html/toc.html http://cs.union.edu/~striegnk/courses/nlp-with-prolog/html/t...
- deleted 10y ago[deleted]
- YeGoblynQueenne 10y ago>> What turned me off to Prolog is that there's no way to reason about the computational complexity of the algorithm. That's easy to figure out once you realise your code also has an imperative reading. So for instance it's obvious that reversing a list: reverse(A, B) :- reverse(A, [], B, B). reverse([], A, A, []). reverse([B|A], C, D, [_|E]) :- reverse(A, [B|C], D, E). takes time proportional to the lists' length, since you're walking through the list an element at a time. And it's easy to figure out that consing, as in [B|C], is a constant-time operation. Or, for instance, say you wanted to apply reverse/4 to elements of a list-of-lists: findall(L_, (member(L, Ls), reverse(L, L_)), Ls_). You can see that this is going to take the time to go through Ls, times the time to reverse each L. Same goes for the recursive version, except we need to reverse the output also to get it back in the right order: revall(Ls, Ls_):- revall(Ls, [], Ls_). revall([], Ls, Ls_):- reverse(Ls, Ls_) ,!. revall([L|Ls],Acc,Bind):- reverse(L, L_) ,revall(Ls, [L_|Acc], Bind). So it's not different than any other language really, and in fact you can, up to a point, completely ignore the declarative meaning of your program if you just want to think about how fast it's going to go.
- pjmlp 10y agoOn my degree we had two tracks on logic programming, the first one just using a visual language for logic and the second one with Swift Prolog, shared between EE and CS degrees, for some of us like myself it just felt natural but the majority hated those lectures. Logic programming seems to be like monads, either you get it or not, but it is hard to explain to others. Prolog and Lisp were the two languages we were forbidden to use in compiler design project, as it was considered "cheating" versus using something harder like C.
- bjz_ 10y ago> Prolog and Lisp were the two languages we were forbidden to use in compiler design project Haskell/Ocaml/SML? :3
- pjmlp 10y agoOCaml was still called Objective Caml and far from replacing Caml Light, which we were starting to use. Haskell was still not a thing our department cared about, although we got to read some stuff about Miranda. SML was never a thing at our university.
- toolslive 10y agothen use this: https://en.wikipedia.org/wiki/Mercury_%28programming_language%29 https://en.wikipedia.org/wiki/Mercury_%28programming_languag... feels like lego where prolog is duplo.
- pjmlp 10y agoI know Mercury, but your advice is about 20 years too late. :)
- rvense 10y agoFor me Mercury is sort of an objective "best language". You can do logic programming, functional programming, and you get the same type-level expressivity as an ML. The implementation and ecosystem are quite far from something like Python, of course, but it does look like the most beautiful house in the street to me when you just walk past it.
- agumonkey 10y agoYes for a class of problème prolog allows for extremely short yet extremely general solutions. Even makes haskell feel procedural sometimes.
- kriro 10y agoIt's an interesting language no doubt. There are a lot of different implementations though. I generally prefer free implementations and would recommend SWI-Prolog as it's reasonably fast and has the most documentation. Of the proprietary Prologs I've mostly worked with Sicstus which was the fastest one I've used and had pretty good constraint libraries. It also has a lot of extra features (logical loops etc.) but I mostly used it for legacy reasons and generally prefer a free implementation (for any programming language). I've heard good things about Eclipse (unfortunate naming, hard to search for) when it comes to constraint programming. Some random observations: It usually takes my brain a bit to switch to "Prolog mode". You can build some fairly elegant things (constraint based poker hand rankings was the last thing I built) but it's usually not very fast. However I'm not a very proficient Prolog programmer so it could very well be on me for not optimizing enough. If you want some book recommendations I think the best first book is "Prolog Programming for Artificial Intelligence" (best intro section which is the first half of the book so highly recommended even if you don't care about AI at all). The classics are great followups to that (Art of Prolog first, Craft of Prolog second).
- profquail 10y agoI found "Learn Prolog Now" to be a simple, easy-to-read site for learning Prolog: http://www.learnprolognow.org/ http://www.learnprolognow.org/ The website text (free) is the first edition of the book; the authors have also published a second edition with updates.