13 ms·
Could someone knowledgable in the topic explain in simple terms what can be done in Prolog and not in other mainstream languages? Thanks.
by leandot 5y ago
Could someone knowledgable in the topic explain in simple terms what can be done in Prolog and not in other mainstream languages? Thanks.
- alfiedotwtf 5y agoMost languages we use these days are imperative - we tell the compiler the steps to take in order to complete a goal. Prolog is a declarative, logic language - we tell the compiler what the goal is, and the compiler works out the steps to take in order to complete that goal.
- triska 5y agoAs I see it, the most striking feature of Prolog that sets it apart from all other languages, mainstream or not, is its ability to write concise meta-interpreters in it. A Prolog meta-interpreter is a Prolog program that interprets Prolog programs. With this facility, you can dynamically extend your programs, by reading code and executing it as if it were part of the original program. But not only that, you can also reason about it, and execute it in different ways than a regular Prolog engine would do it. You can analyze it, and execute only safe parts. You can remove fragments, and show explanations why specific phenomena occur. You can debug it declaratively by constructing generalizations and specializations. You can add features to the language that are not present in its actual implementation, but only in the programs you interpret. No other programming language provides this ability to such an extreme extent as Prolog. For example, how much C++ code would it take to write an interpreter for a sensible subset of C++? How much Lisp code would it take to interpret the most essential aspects of Lisp? It takes only 3 lines of Prolog code to interpret the essence of Prolog. Most programmers may currently not "miss" this ability, because what they think of when they think of a "program" is in comparison quite static: The idea that they could interpret external code dynamically as part of their programs is alien to them because the languages they are using either does not allow it, or only allows it in a sense "blindly", by not truly looking into the code and interpreting it in flexible ways, or by making the analysis of given code too hard. Yet, the ability to conveniently adapt and augment the logic of an existing program is a fundamental necessity for truly general and flexible programs that must dynamically take into account changing requirements.
- heavenlyblue 5y agoNone of what you just said seems to have any substance as it all sounds very abstract and easily doable in other languages (I.e. Python through exec). What should I read in order to understand the difference?
- eismcc 5y agohere’s some info from the author: https://www.metalevel.at/acomip/ https://www.metalevel.at/acomip/
- coliveira 5y agoThe parent's point is very clear, but I guess you never had the opportunity to see it in action. Basically, you can write a Prolog interpreter for the full language very easily (a few lines), and this interpreter can extend the original language in any way you want. So you can not only write your own DLS, but do any kind of reasoning/transformation/interpretation/debugging with the new constructs, almost for free, no knowledge of compilers is needed. This is how people write new languages based on Prolog.
- kazinator 5y ago> How much Lisp code would it take to interpret the most essential aspects of Lisp? Just: (eval code).
- jodrellblank 5y agoA contrived basic example, if you want to validate that a number is between 100 and 150, and is a multiple of 5 then you can write this in Prolog or something very like it in Python: :- use_module(library(clpfd)). valid(Num) :- Num in 100..150, Num mod 5 #= 0, label([Num]). # Python def valid(num): return (num in range(100, 151)) and (num % 5 == 0) Then testing it, `valid(120)` is true, `valid(121)` is false. What the Prolog one does that the Python one doesn't do is if you pass in a placeholder, it can search for numbers that fit the cosntraints and output them, which is cool and a different focus on the problem and what the code exists to do. The Prolog ideal (not always easy to attain) is that you describe the patterns and constraints around the solution and Prolog finds the solution. len(some_list) in Python can tell you the length of a list. length(Somelist, Len) in Prolog can tell you the length of a list, check if a list is the given length, generate a list of a specified length containing placeholders, or generate lists of increasing lengths containing placeholders. All languages can do what all other languages can do with enough effort; Prolog brings to the foreground relations and constraints and patterns instead of state manipulation and variable assignment.